From 5b2147a8740503401aef4ab8b45e30376fcc5799 Mon Sep 17 00:00:00 2001
From: Martchus
Date: Sun, 12 Jul 2020 02:38:57 +0200
Subject: [PATCH] Apply scaling in renderSvgImage() similar to QIcon::pixmap()
does
---
.../syncthingfileitemactionstaticdata.cpp | 2 +-
model/syncthingicons.cpp | 13 ++++++++-----
model/syncthingicons.h | 4 ++--
plasmoid/lib/syncthingapplet.cpp | 2 +-
tray/gui/traywidget.cpp | 2 +-
widgets/settings/settingsdialog.cpp | 4 ++--
6 files changed, 15 insertions(+), 12 deletions(-)
diff --git a/fileitemactionplugin/syncthingfileitemactionstaticdata.cpp b/fileitemactionplugin/syncthingfileitemactionstaticdata.cpp
index 86d7633..76c3ee4 100644
--- a/fileitemactionplugin/syncthingfileitemactionstaticdata.cpp
+++ b/fileitemactionplugin/syncthingfileitemactionstaticdata.cpp
@@ -104,7 +104,7 @@ void SyncthingFileItemActionStaticData::showAboutDialog()
"icons from Font "
"Awesome (see their license)"),
QStringLiteral(APP_VERSION), CppUtilities::applicationInfo.dependencyVersions, QStringLiteral(APP_URL), QStringLiteral(APP_DESCRIPTION),
- renderSvgImage(makeSyncthingIcon()).toImage());
+ renderSvgImage(makeSyncthingIcon(), QSize(128, 128)).toImage());
aboutDialog->setWindowTitle(tr("About") + QStringLiteral(" - " APP_NAME));
aboutDialog->setWindowIcon(QIcon::fromTheme(QStringLiteral("syncthingtray")));
aboutDialog->setAttribute(Qt::WA_DeleteOnClose);
diff --git a/model/syncthingicons.cpp b/model/syncthingicons.cpp
index 075cfeb..116c000 100644
--- a/model/syncthingicons.cpp
+++ b/model/syncthingicons.cpp
@@ -1,6 +1,6 @@
#include "./syncthingicons.h"
-#include
+#include
#include
#include
#include
@@ -113,12 +113,14 @@ QByteArray makeSyncthingIcon(const StatusIconColorSet &colors, StatusEmblem stat
/// \cond
namespace Detail {
-template QPixmap renderSvgImage(const SourceType &source, const QSize &size, int margin)
+template QPixmap renderSvgImage(const SourceType &source, const QSize &givenSize, int margin)
{
+ const qreal scaleFactor = QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps) ? qGuiApp->devicePixelRatio() : 1.0;
QSvgRenderer renderer(source);
+ QSize scaledSize(givenSize.width() * scaleFactor, givenSize.height() * scaleFactor);
QSize renderSize(renderer.defaultSize());
- renderSize.scale(size.width() - margin, size.height() - margin, Qt::KeepAspectRatio);
- QRect renderBounds(QPoint(), size);
+ renderSize.scale(scaledSize.width() - margin, scaledSize.height() - margin, Qt::KeepAspectRatio);
+ QRect renderBounds(QPoint(), scaledSize);
if (renderSize.width() < renderBounds.width()) {
const auto diff = (renderBounds.width() - renderSize.width()) / 2;
renderBounds.setX(diff);
@@ -129,10 +131,11 @@ template QPixmap renderSvgImage(const SourceType &source,
renderBounds.setY(diff);
renderBounds.setHeight(renderSize.height());
}
- QPixmap pm(size);
+ QPixmap pm(scaledSize);
pm.fill(QColor(Qt::transparent));
QPainter painter(&pm);
renderer.render(&painter, renderBounds);
+ pm.setDevicePixelRatio(scaleFactor);
return pm;
}
} // namespace Detail
diff --git a/model/syncthingicons.h b/model/syncthingicons.h
index cb246b8..d967fe8 100644
--- a/model/syncthingicons.h
+++ b/model/syncthingicons.h
@@ -56,8 +56,8 @@ inline StatusIconColorSet::StatusIconColorSet(const QString &backgroundStart, co
LIB_SYNCTHING_MODEL_EXPORT QByteArray makeSyncthingIcon(
const StatusIconColorSet &colors = StatusIconColorSet{ QStringLiteral("#26B6DB"), QStringLiteral("#0882C8"), QStringLiteral("#FFFFFF") },
StatusEmblem statusEmblem = StatusEmblem::None);
-LIB_SYNCTHING_MODEL_EXPORT QPixmap renderSvgImage(const QString &path, const QSize &size = QSize(128, 128), int margin = 0);
-LIB_SYNCTHING_MODEL_EXPORT QPixmap renderSvgImage(const QByteArray &contents, const QSize &size = QSize(128, 128), int margin = 0);
+LIB_SYNCTHING_MODEL_EXPORT QPixmap renderSvgImage(const QString &path, const QSize &size = QSize(32, 32), int margin = 0);
+LIB_SYNCTHING_MODEL_EXPORT QPixmap renderSvgImage(const QByteArray &contents, const QSize &size = QSize(32, 32), int margin = 0);
LIB_SYNCTHING_MODEL_EXPORT QByteArray loadFontAwesomeIcon(const QString &iconName, const QColor &color, bool solid = true);
struct LIB_SYNCTHING_MODEL_EXPORT StatusIconSettings {
diff --git a/plasmoid/lib/syncthingapplet.cpp b/plasmoid/lib/syncthingapplet.cpp
index 2ac3ad0..09b1894 100644
--- a/plasmoid/lib/syncthingapplet.cpp
+++ b/plasmoid/lib/syncthingapplet.cpp
@@ -326,7 +326,7 @@ void SyncthingApplet::showAboutDialog()
"icons from Font "
"Awesome (see their license)
"),
QStringLiteral(APP_VERSION), CppUtilities::applicationInfo.dependencyVersions, QStringLiteral(APP_URL), QStringLiteral(APP_DESCRIPTION),
- renderSvgImage(makeSyncthingIcon()).toImage());
+ renderSvgImage(makeSyncthingIcon(), QSize(128, 128)).toImage());
m_aboutDlg->setWindowTitle(tr("About") + QStringLiteral(" - " APP_NAME));
m_aboutDlg->setWindowIcon(QIcon::fromTheme(QStringLiteral("syncthingtray")));
m_aboutDlg->setAttribute(Qt::WA_DeleteOnClose);
diff --git a/tray/gui/traywidget.cpp b/tray/gui/traywidget.cpp
index d576b3e..4369197 100644
--- a/tray/gui/traywidget.cpp
+++ b/tray/gui/traywidget.cpp
@@ -240,7 +240,7 @@ void TrayWidget::showAboutDialog()
"
Fallback icons from KDE/Breeze project
Syncthing icons from Syncthing project
Using "
"icons from Font "
"Awesome (see their license)"),
- QString(), {}, QStringLiteral(APP_URL), QString(), QImage(QStringLiteral(":/icons/hicolor/scalable/app/syncthingtray.svg")));
+ QString(), {}, QStringLiteral(APP_URL), QString(), renderSvgImage(makeSyncthingIcon(), QSize(128, 128)).toImage());
s_aboutDlg->setWindowTitle(tr("About") + QStringLiteral(" - " APP_NAME));
s_aboutDlg->setWindowIcon(QIcon(QStringLiteral(":/icons/hicolor/scalable/app/syncthingtray.svg")));
}
diff --git a/widgets/settings/settingsdialog.cpp b/widgets/settings/settingsdialog.cpp
index 7d787b0..c9b234c 100644
--- a/widgets/settings/settingsdialog.cpp
+++ b/widgets/settings/settingsdialog.cpp
@@ -564,6 +564,7 @@ QWidget *IconsOptionPage::setupWidget()
&colorMapping.setting,
colorMapping.defaultEmblem,
};
+ widgetsForColor.previewLabel->setMaximumSize(QSize(32, 32));
// add label for color name
gridLayout->addWidget(new QLabel(colorMapping.colorName, statusIconsGroupBox), index, 0, Qt::AlignRight | Qt::AlignVCenter);
@@ -577,8 +578,7 @@ QWidget *IconsOptionPage::setupWidget()
widgetsForColor.colorButtons[1]->color(),
widgetsForColor.colorButtons[2]->color(),
},
- widgetsForColor.statusEmblem),
- QSize(32, 32)));
+ widgetsForColor.statusEmblem), widgetsForColor.previewLabel->maximumSize()));
};
for (const auto &colorButton : widgetsForColor.colorButtons) {
QObject::connect(colorButton, &ColorButton::colorChanged, updatePreview);