From 7256e1c8276598dc968d97af024ea79c922aab02 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 27 Mar 2024 10:29:48 +0100 Subject: [PATCH] Gui: add overloaded method pixmapFromSvg() to pass the device pixel ratio of a widget --- src/Gui/BitmapFactory.cpp | 14 ++++++++++++-- src/Gui/BitmapFactory.h | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Gui/BitmapFactory.cpp b/src/Gui/BitmapFactory.cpp index 7a3adbc7a5..2739e276bc 100644 --- a/src/Gui/BitmapFactory.cpp +++ b/src/Gui/BitmapFactory.cpp @@ -285,7 +285,7 @@ QPixmap BitmapFactoryInst::pixmap(const char* name) const } QPixmap BitmapFactoryInst::pixmapFromSvg(const char* name, const QSizeF& size, - const std::map& colorMapping) const + const ColorMap& colorMapping) const { // If an absolute path is given QPixmap icon; @@ -321,8 +321,18 @@ QPixmap BitmapFactoryInst::pixmapFromSvg(const char* name, const QSizeF& size, return icon; } +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; +} + QPixmap BitmapFactoryInst::pixmapFromSvg(const QByteArray& originalContents, const QSizeF& size, - const std::map& colorMapping) const + const ColorMap& colorMapping) const { QString stringContents = QString::fromUtf8(originalContents); for ( const auto &colorToColor : colorMapping ) { diff --git a/src/Gui/BitmapFactory.h b/src/Gui/BitmapFactory.h index 4f71b3659c..b17239484c 100644 --- a/src/Gui/BitmapFactory.h +++ b/src/Gui/BitmapFactory.h @@ -34,6 +34,8 @@ class QImage; namespace Gui { +using ColorMap = std::map; + /** The Bitmap Factory * the main purpose is to collect all build in Bitmaps and * hold all paths for the extern bitmaps (files) to serve @@ -82,14 +84,22 @@ public: * Can be used to customize icon color scheme, e.g. crosshair color */ QPixmap pixmapFromSvg(const char* name, const QSizeF& size, - const std::map& colorMapping = std::map()) const; + 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. * @param colorMapping - see above. */ QPixmap pixmapFromSvg(const QByteArray& contents, const QSizeF& size, - const std::map& colorMapping = std::map()) const; + const ColorMap& colorMapping = ColorMap()) const; /** Returns the names of all registered pixmaps. * To get the appropriate pixmaps call pixmap() for each name. */