Skip to content

Commit

Permalink
Revamp resource embedding (#7241)
Browse files Browse the repository at this point in the history
  • Loading branch information
DomClark authored May 12, 2024
1 parent 6d100d1 commit 95e5f97
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 130 deletions.
4 changes: 3 additions & 1 deletion include/TrackLabelButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifndef LMMS_GUI_TRACK_LABEL_BUTTON_H
#define LMMS_GUI_TRACK_LABEL_BUTTON_H

#include <string>

#include <QToolButton>

namespace lmms::gui
Expand Down Expand Up @@ -63,7 +65,7 @@ public slots:

private:
TrackView * m_trackView;
QString m_iconName;
std::string m_iconName;
TrackRenameLineEdit * m_renameLineEdit;
QRect m_buttonRect;
QString elideName( const QString &name );
Expand Down
110 changes: 37 additions & 73 deletions include/embed.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,117 +25,81 @@
#ifndef LMMS_EMBED_H
#define LMMS_EMBED_H

#include <string>
#include <string_view>

#include <QPixmap>
#include <QString>

#include "lmms_export.h"
#include "lmms_basics.h"

namespace lmms {

namespace lmms
{

namespace embed
{
namespace embed {

/**
* Return an image for the icon pixmap cache.
*
* @param _name Identifier for the pixmap. If it is not in the icon pixmap
* @param name Identifier for the pixmap. If it is not in the icon pixmap
* cache, it will be loaded from the artwork QDir search paths (exceptions are
* compiled-in XPMs, you need to provide @p xpm for loading them).
* @param xpm Must be XPM data if the source should be raw XPM data instead of
* a file
*/
QPixmap LMMS_EXPORT getIconPixmap( const QString& _name,
int _w = -1, int _h = -1 , const char** xpm = nullptr );
QString LMMS_EXPORT getText( const char * _name );

}


#ifdef PLUGIN_NAME
namespace PLUGIN_NAME
{

inline QPixmap getIconPixmap( const QString& _name,
int _w = -1, int _h = -1, const char** xpm = nullptr )
{
return embed::getIconPixmap(QString("%1/%2").arg(LMMS_STRINGIFY(PLUGIN_NAME), _name), _w, _h, xpm);
}
//QString getText( const char * _name );

} // namespace PLUGIN_NAME

#endif // PLUGIN_NAME
auto LMMS_EXPORT getIconPixmap(std::string_view name,
int width = -1, int height = -1, const char* const* xpm = nullptr) -> QPixmap;
auto LMMS_EXPORT getText(std::string_view name) -> QString;

} // namespace embed

class PixmapLoader
{
public:
PixmapLoader( const PixmapLoader * _ref ) :
m_name( _ref != nullptr ? _ref->m_name : QString() ),
m_xpm( _ref->m_xpm )
{
}
PixmapLoader() = default;

PixmapLoader( const QString & _name = QString(),
const char** xpm = nullptr ) :
m_name( _name ),
m_xpm(xpm)
{
}

virtual QPixmap pixmap() const
{
if( !m_name.isEmpty() )
{
return( embed::getIconPixmap(
m_name.toLatin1().constData(), -1, -1, m_xpm ));
}
return( QPixmap() );
}
explicit PixmapLoader(std::string name, const char* const* xpm = nullptr) :
m_name{std::move(name)},
m_xpm{xpm}
{ }

virtual ~PixmapLoader() = default;

virtual QString pixmapName() const
auto pixmap(int width = -1, int height = -1) const -> QPixmap
{
return m_name;
return embed::getIconPixmap(m_name, width, height, m_xpm);
}

protected:
QString m_name;
const char** m_xpm = nullptr;
} ;
auto pixmapName() const -> const std::string& { return m_name; }

private:
std::string m_name;
const char* const* m_xpm = nullptr;
};

#ifdef PLUGIN_NAME

class PluginPixmapLoader : public PixmapLoader
{
public:
PluginPixmapLoader( const QString & _name = QString() ) :
PixmapLoader( _name )
{
}
PluginPixmapLoader() = default;

QPixmap pixmap() const override
{
if( !m_name.isEmpty() )
{
return( PLUGIN_NAME::getIconPixmap(
m_name.toLatin1().constData() ) );
}
return( QPixmap() );
}
explicit PluginPixmapLoader(std::string name, const char* const* xpm = nullptr) :
PixmapLoader{LMMS_STRINGIFY(PLUGIN_NAME) "/" + name, xpm}
{ }
};

QString pixmapName() const override
{
return QString( LMMS_STRINGIFY(PLUGIN_NAME) ) + "::" + m_name;
}
namespace PLUGIN_NAME {

} ;
#endif // PLUGIN_NAME
inline auto getIconPixmap(std::string_view name,
int width = -1, int height = -1, const char* const* xpm = nullptr) -> QPixmap
{
return PluginPixmapLoader{std::string{name}, xpm}.pixmap(width, height);
}

} // namespace PLUGIN_NAME

#endif // PLUGIN_NAME

} // namespace lmms

Expand Down
8 changes: 4 additions & 4 deletions plugins/Eq/EqControlsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ EqControlsDialog::EqControlsDialog( EqControls *controls ) :
activeButton->setCheckable(true);
activeButton->setModel( m_parameterWidget->getBandModels( i )->active );

QString iconActiveFileName = "bandLabel" + QString::number(i+1);
QString iconInactiveFileName = "bandLabel" + QString::number(i+1) + "off";
activeButton->setActiveGraphic( PLUGIN_NAME::getIconPixmap( iconActiveFileName.toLatin1() ) );
activeButton->setInactiveGraphic( PLUGIN_NAME::getIconPixmap( iconInactiveFileName.toLatin1() ) );
const auto iconActiveFileName = "bandLabel" + std::to_string(i + 1);
const auto iconInactiveFileName = iconActiveFileName + "off";
activeButton->setActiveGraphic(PLUGIN_NAME::getIconPixmap(iconActiveFileName));
activeButton->setInactiveGraphic(PLUGIN_NAME::getIconPixmap(iconInactiveFileName));
activeButton->move( distance - 2, 276 );
activeButton->setModel( m_parameterWidget->getBandModels( i )->active );

Expand Down
6 changes: 3 additions & 3 deletions plugins/Eq/EqCurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ QPainterPath EqHandle::getCurvePath()

void EqHandle::loadPixmap()
{
QString fileName = "handle" + QString::number(m_numb+1);
if ( !isActiveHandle() ) { fileName = fileName + "inactive"; }
m_circlePixmap = PLUGIN_NAME::getIconPixmap( fileName.toLatin1() );
auto fileName = "handle" + std::to_string(m_numb + 1);
if (!isActiveHandle()) { fileName += "inactive"; }
m_circlePixmap = PLUGIN_NAME::getIconPixmap(fileName);
}

bool EqHandle::mousePressed() const
Expand Down
3 changes: 2 additions & 1 deletion plugins/LOMM/LOMMControlDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class LOMMControlDialog : public EffectControlDialog
return spinBox;
}

PixmapButton* createPixmapButton(const QString& text, QWidget* parent, int x, int y, BoolModel* model, const QString& activeIcon, const QString& inactiveIcon, const QString& tooltip)
PixmapButton* createPixmapButton(const QString& text, QWidget* parent, int x, int y, BoolModel* model,
std::string_view activeIcon, std::string_view inactiveIcon, const QString& tooltip)
{
PixmapButton* button = new PixmapButton(parent, text);
button->move(x, y);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/editors/PianoRoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ PianoRoll::PianoRoll() :
// Set up note length model
m_noteLenModel.addItem( tr( "Last note" ),
std::make_unique<PixmapLoader>( "edit_draw" ) );
const auto pixmaps = std::array<QString, 11>{"whole", "half", "quarter", "eighth",
const auto pixmaps = std::array<std::string, 11>{"whole", "half", "quarter", "eighth",
"sixteenth", "thirtysecond", "triplethalf",
"tripletquarter", "tripleteighth",
"tripletsixteenth", "tripletthirtysecond"};
Expand Down
80 changes: 37 additions & 43 deletions src/gui/embed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,68 +22,62 @@
*
*/

#include "embed.h"

#include <QDebug>
#include <QDir>
#include <QImageReader>
#include <QPixmapCache>
#include <QResource>
#include "embed.h"

namespace lmms::embed
{
namespace lmms::embed {

QPixmap getIconPixmap(const QString& pixmapName,
int width, int height, const char** xpm )
namespace {

auto loadPixmap(const QString& name, int width, int height, const char* const* xpm) -> QPixmap
{
QString cacheName;
if (width > 0 && height > 0)
{
cacheName = QString("%1_%2_%3").arg(pixmapName, width, height);
}
else
{
cacheName = pixmapName;
}
if (xpm) { return QPixmap{xpm}; }

// Return cached pixmap
QPixmap pixmap;
if( QPixmapCache::find(cacheName, &pixmap) )
{
return pixmap;
}
const auto resourceName = QDir::isAbsolutePath(name) ? name : "artwork:" + name;
auto reader = QImageReader{resourceName};
if (width > 0 && height > 0) { reader.setScaledSize(QSize{width, height}); }

if(xpm)
{
pixmap = QPixmap(xpm);
const auto pixmap = QPixmap::fromImageReader(&reader);
if (pixmap.isNull()) {
qWarning().nospace() << "Error loading icon pixmap " << name << ": " << reader.errorString();
return QPixmap{1, 1};
}
else
{
QImageReader reader(QString("artwork:%1").arg(pixmapName));
return pixmap;
}

if (width > 0 && height > 0)
{
reader.setScaledSize(QSize(width, height));
}
} // namespace

pixmap = QPixmap::fromImageReader(&reader);
auto getIconPixmap(std::string_view name, int width, int height, const char* const* xpm) -> QPixmap
{
if (name.empty()) { return QPixmap{}; }

if (pixmap.isNull())
{
qWarning().nospace() << "Error loading icon pixmap " << pixmapName << ": " <<
reader.errorString().toLocal8Bit().data();
return QPixmap(1,1);
}
}
const auto pixmapName = QString::fromUtf8(name.data(), name.size());
const auto cacheName = (width > 0 && height > 0)
? QStringLiteral("%1_%2_%3").arg(pixmapName, width, height)
: pixmapName;

// Save to cache and return
// Return cached pixmap if it exists
if (auto pixmap = QPixmap{}; QPixmapCache::find(cacheName, &pixmap)) { return pixmap; }

// Load the pixmap and cache it before returning
const auto pixmap = loadPixmap(pixmapName, width, height, xpm);
QPixmapCache::insert(cacheName, pixmap);
return pixmap;
}


QString getText( const char * name )
auto getText(std::string_view name) -> QString
{
return QString::fromUtf8( (const char*) QResource(QString(":/%1").arg(name)).data());
const auto resource = QResource{":/" + QString::fromUtf8(name.data(), name.size())};
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
return QString::fromUtf8(resource.uncompressedData());
#else
return QString::fromUtf8(reinterpret_cast<const char*>(resource.data()), resource.size());
#endif
}


} // namespace lmms::embed
9 changes: 5 additions & 4 deletions src/gui/instrument/EnvelopeAndLfoView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

#include "EnvelopeAndLfoView.h"

#include <string_view>

#include <QBoxLayout>

#include "EnvelopeGraph.h"
#include "LfoGraph.h"
#include "EnvelopeAndLfoParameters.h"
Expand All @@ -39,9 +43,6 @@
#include "TextFloat.h"
#include "Track.h"

#include <QBoxLayout>


namespace lmms
{

Expand All @@ -63,7 +64,7 @@ EnvelopeAndLfoView::EnvelopeAndLfoView(QWidget * parent) :
return knob;
};

auto buildPixmapButton = [&](const QString& activePixmap, const QString& inactivePixmap)
auto buildPixmapButton = [&](std::string_view activePixmap, std::string_view inactivePixmap)
{
auto button = new PixmapButton(this, nullptr);
button->setActiveGraphic(embed::getIconPixmap(activePixmap));
Expand Down

0 comments on commit 95e5f97

Please sign in to comment.