Revamp resource embedding (#7241)
This commit is contained in:
@@ -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
|
||||
@@ -63,7 +65,7 @@ private:
|
||||
|
||||
private:
|
||||
TrackView * m_trackView;
|
||||
QString m_iconName;
|
||||
std::string m_iconName;
|
||||
TrackRenameLineEdit * m_renameLineEdit;
|
||||
QRect m_buttonRect;
|
||||
QString elideName( const QString &name );
|
||||
|
||||
132
include/embed.h
132
include/embed.h
@@ -25,118 +25,82 @@
|
||||
#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 );
|
||||
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() = default;
|
||||
|
||||
explicit PixmapLoader(std::string name, const char* const* xpm = nullptr) :
|
||||
m_name{std::move(name)},
|
||||
m_xpm{xpm}
|
||||
{ }
|
||||
|
||||
virtual ~PixmapLoader() = default;
|
||||
|
||||
auto pixmap(int width = -1, int height = -1) const -> QPixmap
|
||||
{
|
||||
return embed::getIconPixmap(m_name, width, height, m_xpm);
|
||||
}
|
||||
|
||||
auto pixmapName() const -> const std::string& { return m_name; }
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
const char* const* m_xpm = nullptr;
|
||||
};
|
||||
|
||||
#ifdef PLUGIN_NAME
|
||||
namespace PLUGIN_NAME
|
||||
{
|
||||
|
||||
inline QPixmap getIconPixmap( const QString& _name,
|
||||
int _w = -1, int _h = -1, const char** xpm = nullptr )
|
||||
class PluginPixmapLoader : public PixmapLoader
|
||||
{
|
||||
return embed::getIconPixmap(QString("%1/%2").arg(LMMS_STRINGIFY(PLUGIN_NAME), _name), _w, _h, xpm);
|
||||
public:
|
||||
PluginPixmapLoader() = default;
|
||||
|
||||
explicit PluginPixmapLoader(std::string name, const char* const* xpm = nullptr) :
|
||||
PixmapLoader{LMMS_STRINGIFY(PLUGIN_NAME) "/" + name, xpm}
|
||||
{ }
|
||||
};
|
||||
|
||||
namespace 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);
|
||||
}
|
||||
//QString getText( const char * _name );
|
||||
|
||||
} // namespace PLUGIN_NAME
|
||||
|
||||
#endif // PLUGIN_NAME
|
||||
|
||||
|
||||
class PixmapLoader
|
||||
{
|
||||
public:
|
||||
PixmapLoader( const PixmapLoader * _ref ) :
|
||||
m_name( _ref != nullptr ? _ref->m_name : QString() ),
|
||||
m_xpm( _ref->m_xpm )
|
||||
{
|
||||
}
|
||||
|
||||
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() );
|
||||
}
|
||||
|
||||
virtual ~PixmapLoader() = default;
|
||||
|
||||
virtual QString pixmapName() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
protected:
|
||||
QString m_name;
|
||||
const char** m_xpm = nullptr;
|
||||
} ;
|
||||
|
||||
|
||||
#ifdef PLUGIN_NAME
|
||||
class PluginPixmapLoader : public PixmapLoader
|
||||
{
|
||||
public:
|
||||
PluginPixmapLoader( const QString & _name = QString() ) :
|
||||
PixmapLoader( _name )
|
||||
{
|
||||
}
|
||||
|
||||
QPixmap pixmap() const override
|
||||
{
|
||||
if( !m_name.isEmpty() )
|
||||
{
|
||||
return( PLUGIN_NAME::getIconPixmap(
|
||||
m_name.toLatin1().constData() ) );
|
||||
}
|
||||
return( QPixmap() );
|
||||
}
|
||||
|
||||
QString pixmapName() const override
|
||||
{
|
||||
return QString( LMMS_STRINGIFY(PLUGIN_NAME) ) + "::" + m_name;
|
||||
}
|
||||
|
||||
} ;
|
||||
#endif // PLUGIN_NAME
|
||||
|
||||
|
||||
} // namespace lmms
|
||||
|
||||
#endif // LMMS_EMBED_H
|
||||
|
||||
@@ -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 );
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -86,7 +86,8 @@ public:
|
||||
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);
|
||||
|
||||
@@ -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"};
|
||||
|
||||
@@ -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 {
|
||||
|
||||
namespace {
|
||||
|
||||
auto loadPixmap(const QString& name, int width, int height, const char* const* xpm) -> QPixmap
|
||||
{
|
||||
if (xpm) { return QPixmap{xpm}; }
|
||||
|
||||
QPixmap getIconPixmap(const QString& pixmapName,
|
||||
int width, int height, const char** xpm )
|
||||
const auto resourceName = QDir::isAbsolutePath(name) ? name : "artwork:" + name;
|
||||
auto reader = QImageReader{resourceName};
|
||||
if (width > 0 && height > 0) { reader.setScaledSize(QSize{width, height}); }
|
||||
|
||||
const auto pixmap = QPixmap::fromImageReader(&reader);
|
||||
if (pixmap.isNull()) {
|
||||
qWarning().nospace() << "Error loading icon pixmap " << name << ": " << reader.errorString();
|
||||
return QPixmap{1, 1};
|
||||
}
|
||||
return pixmap;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
auto getIconPixmap(std::string_view 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 (name.empty()) { return QPixmap{}; }
|
||||
|
||||
// Return cached pixmap
|
||||
QPixmap pixmap;
|
||||
if( QPixmapCache::find(cacheName, &pixmap) )
|
||||
{
|
||||
return pixmap;
|
||||
}
|
||||
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;
|
||||
|
||||
if(xpm)
|
||||
{
|
||||
pixmap = QPixmap(xpm);
|
||||
}
|
||||
else
|
||||
{
|
||||
QImageReader reader(QString("artwork:%1").arg(pixmapName));
|
||||
// Return cached pixmap if it exists
|
||||
if (auto pixmap = QPixmap{}; QPixmapCache::find(cacheName, &pixmap)) { return pixmap; }
|
||||
|
||||
if (width > 0 && height > 0)
|
||||
{
|
||||
reader.setScaledSize(QSize(width, height));
|
||||
}
|
||||
|
||||
pixmap = QPixmap::fromImageReader(&reader);
|
||||
|
||||
if (pixmap.isNull())
|
||||
{
|
||||
qWarning().nospace() << "Error loading icon pixmap " << pixmapName << ": " <<
|
||||
reader.errorString().toLocal8Bit().data();
|
||||
return QPixmap(1,1);
|
||||
}
|
||||
}
|
||||
|
||||
// Save to cache and return
|
||||
// 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
|
||||
|
||||
@@ -25,6 +25,10 @@
|
||||
|
||||
#include "EnvelopeAndLfoView.h"
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include <QBoxLayout>
|
||||
|
||||
#include "EnvelopeGraph.h"
|
||||
#include "LfoGraph.h"
|
||||
#include "EnvelopeAndLfoParameters.h"
|
||||
@@ -39,9 +43,6 @@
|
||||
#include "TextFloat.h"
|
||||
#include "Track.h"
|
||||
|
||||
#include <QBoxLayout>
|
||||
|
||||
|
||||
namespace lmms
|
||||
{
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user