Gui: make BitmapFactory::pixmapFromSvg dpi aware

added getMaximumDPR method and removed a overload of pixmapFromSvg with dpr parameter

update
This commit is contained in:
captain0xff
2025-04-02 21:01:13 +05:30
parent db08735e10
commit d4de12061e
2 changed files with 21 additions and 19 deletions

View File

@@ -32,6 +32,7 @@
# include <QImageReader>
# include <QPainter>
# include <QPalette>
# include <QScreen>
# include <QString>
# include <QSvgRenderer>
# include <QStyleOption>
@@ -277,6 +278,8 @@ QPixmap BitmapFactoryInst::pixmap(const char* name) const
QPixmap BitmapFactoryInst::pixmapFromSvg(const char* name, const QSizeF& size,
const ColorMap& colorMapping) const
{
static qreal dpr = getMaximumDPR();
// If an absolute path is given
QPixmap icon;
QString iconPath;
@@ -304,21 +307,15 @@ QPixmap BitmapFactoryInst::pixmapFromSvg(const char* name, const QSizeF& size,
QFile file(iconPath);
if (file.open(QFile::ReadOnly | QFile::Text)) {
QByteArray content = file.readAll();
icon = pixmapFromSvg(content, size, colorMapping);
icon = pixmapFromSvg(content, size * dpr, colorMapping);
}
}
return icon;
}
if (!icon.isNull()) {
icon.setDevicePixelRatio(dpr);
}
QPixmap BitmapFactoryInst::pixmapFromSvg(const char* name, const QSizeF& size, qreal dpr,
const ColorMap& colorMapping) const
{
qreal width = size.width() * dpr;
qreal height = size.height() * dpr;
QPixmap px(pixmapFromSvg(name, QSizeF(width, height), colorMapping));
px.setDevicePixelRatio(dpr);
return px;
return icon;
}
QPixmap BitmapFactoryInst::pixmapFromSvg(const QByteArray& originalContents, const QSizeF& size,
@@ -677,3 +674,14 @@ QIcon BitmapFactoryInst::mergePixmap (const QIcon &base, const QPixmap &px, Gui:
return overlayedIcon;
}
qreal BitmapFactoryInst::getMaximumDPR()
{
qreal dpr = 1.0F;
for (QScreen* screen: QGuiApplication::screens()) {
dpr = std::max(screen->devicePixelRatio(), dpr);
}
return dpr;
}

View File

@@ -87,14 +87,6 @@ public:
*/
QPixmap pixmapFromSvg(const char* name, const QSizeF& size,
const ColorMap& colorMapping = ColorMap()) const;
/** Retrieves a pixmap by name and size created by an
* scalable vector graphics (SVG) and a device pixel ratio
*
* @param colorMapping - a dictionary of substitute colors.
* Can be used to customize icon color scheme, e.g. crosshair color
*/
QPixmap pixmapFromSvg(const char* name, const QSizeF& size, qreal dpr,
const ColorMap& colorMapping = ColorMap()) const;
/** This method is provided for convenience and does the same
* as the method above except that it creates the pixmap from
* a byte array.
@@ -149,6 +141,8 @@ public:
/// Helper method to merge a pixmap into one corner of a QIcon
static QIcon mergePixmap (const QIcon &base, const QPixmap &px, Gui::BitmapFactoryInst::Position position);
static qreal getMaximumDPR();
private:
bool loadPixmap(const QString& path, QPixmap&) const;
void restoreCustomPaths();