diff --git a/model/syncthingicons.cpp b/model/syncthingicons.cpp index a58ea3d..f63c6c1 100644 --- a/model/syncthingicons.cpp +++ b/model/syncthingicons.cpp @@ -9,13 +9,26 @@ namespace Data { /// \cond namespace Detail { -template QPixmap renderSvgImage(const SourceType &source, const QSize &size) +template QPixmap renderSvgImage(const SourceType &source, const QSize &size, int margin) { QSvgRenderer renderer(source); + QSize renderSize(renderer.defaultSize()); + renderSize.scale(size.width() - margin, size.height() - margin, Qt::KeepAspectRatio); + QRect renderBounds(QPoint(), size); + if (renderSize.width() < renderBounds.width()) { + const auto diff = (renderBounds.width() - renderSize.width()) / 2; + renderBounds.setX(diff); + renderBounds.setWidth(renderSize.width()); + } + if (renderSize.height() < renderBounds.height()) { + const auto diff = (renderBounds.height() - renderSize.height()) / 2; + renderBounds.setY(diff); + renderBounds.setHeight(renderSize.height()); + } QPixmap pm(size); pm.fill(QColor(Qt::transparent)); QPainter painter(&pm); - renderer.render(&painter, pm.rect()); + renderer.render(&painter, renderBounds); return pm; } } // namespace Detail @@ -26,17 +39,17 @@ template QPixmap renderSvgImage(const SourceType &source, * \remarks If instantiating QIcon directly from SVG image the icon is not displayed in the tray under Plasma 5. It works * with Tint2, however. */ -QPixmap renderSvgImage(const QString &path, const QSize &size) +QPixmap renderSvgImage(const QString &path, const QSize &size, int margin) { - return Detail::renderSvgImage(path, size); + return Detail::renderSvgImage(path, size, margin); } /*! * \brief Renders an SVG image to a QPixmap. */ -QPixmap renderSvgImage(const QByteArray &contents, const QSize &size) +QPixmap renderSvgImage(const QByteArray &contents, const QSize &size, int margin) { - return Detail::renderSvgImage(contents, size); + return Detail::renderSvgImage(contents, size, margin); } /*! diff --git a/model/syncthingicons.h b/model/syncthingicons.h index 87f73eb..e025738 100644 --- a/model/syncthingicons.h +++ b/model/syncthingicons.h @@ -10,9 +10,6 @@ QT_FORWARD_DECLARE_CLASS(QColor) namespace Data { -QPixmap LIB_SYNCTHING_MODEL_EXPORT renderSvgImage(const QString &path, const QSize &size = QSize(128, 128)); -QPixmap LIB_SYNCTHING_MODEL_EXPORT renderSvgImage(const QByteArray &contents, const QSize &size = QSize(128, 128)); - QByteArray LIB_SYNCTHING_MODEL_EXPORT loadFontAwesomeIcon(const QString &iconName, const QColor &color); struct StatusIcons { @@ -29,6 +26,8 @@ struct StatusIcons { QIcon newItem; }; +QPixmap LIB_SYNCTHING_MODEL_EXPORT renderSvgImage(const QString &path, const QSize &size = QSize(128, 128), int margin = 0); +QPixmap LIB_SYNCTHING_MODEL_EXPORT renderSvgImage(const QByteArray &contents, const QSize &size = QSize(128, 128), int margin = 0); const StatusIcons LIB_SYNCTHING_MODEL_EXPORT &statusIcons(); } // namespace Data