From cc7d58d4ecb1f0d5ae9a517adba7e27bd2552551 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Thu, 29 Sep 2022 01:11:21 -0400 Subject: [PATCH] fix: proper svg icon rendering on hidpi screen. --- app/actionmanager.cpp | 41 ++++++++++++++++++++++++++------------- app/actionmanager.h | 2 ++ app/bottombuttongroup.cpp | 2 +- app/main.cpp | 3 +++ app/mainwindow.cpp | 9 +++++---- app/toolbutton.cpp | 6 ++++++ app/toolbutton.h | 1 + 7 files changed, 46 insertions(+), 18 deletions(-) diff --git a/app/actionmanager.cpp b/app/actionmanager.cpp index 2b99244..3306782 100644 --- a/app/actionmanager.cpp +++ b/app/actionmanager.cpp @@ -6,22 +6,15 @@ #include "mainwindow.h" -#include +#include +#include +#include #define ICON_NAME(name)\ - QStringLiteral(":/icons/" #name "") + QStringLiteral(":/icons/" #name ".svg") -#define SETUP_NEW_ACTION(window, action)\ - action->setObjectName(QString::fromUtf8( #action ));\ - window->addAction(action); - -#define CREATE_NEW_ACTION(window, action)\ - action = new QAction(window);\ - SETUP_NEW_ACTION(window, action) - -#define CREATE_NEW_ICON_ACTION(window, action, iconname)\ - action = new QAction(QIcon(ICON_NAME(iconname)), QString(), window);\ - SETUP_NEW_ACTION(window, action) +#define ACTION_NAME(s) QStringLiteral(STRIFY(s)) +#define STRIFY(s) #s ActionManager::ActionManager() { @@ -33,15 +26,36 @@ ActionManager::~ActionManager() } +QIcon ActionManager::loadHidpiIcon(const QString &resp, QSize sz) +{ + QSvgRenderer r(resp); + QPixmap pm = QPixmap(sz * qApp->devicePixelRatio()); + pm.fill(Qt::transparent); + QPainter p(&pm); + r.render(&p); + pm.setDevicePixelRatio(qApp->devicePixelRatio()); + return QIcon(pm); +} + void ActionManager::setupAction(MainWindow *mainWindow) { + auto create_action = [] (QWidget *w, QAction **a, QString i, QString an) { + *a = new QAction(w); + if (!i.isNull()) + (*a)->setIcon(ActionManager::loadHidpiIcon(i)); + (*a)->setObjectName(an); + w->addAction(*a); + }; + #define CREATE_NEW_ICON_ACTION(w, a, i) create_action(w, &a, ICON_NAME(i), ACTION_NAME(a)) CREATE_NEW_ICON_ACTION(mainWindow, actionActualSize, zoom-original); CREATE_NEW_ICON_ACTION(mainWindow, actionToggleMaximize, view-fullscreen); CREATE_NEW_ICON_ACTION(mainWindow, actionZoomIn, zoom-in); CREATE_NEW_ICON_ACTION(mainWindow, actionZoomOut, zoom-out); CREATE_NEW_ICON_ACTION(mainWindow, actionToggleCheckerboard, view-background-checkerboard); CREATE_NEW_ICON_ACTION(mainWindow, actionRotateClockwise, object-rotate-right); + #undef CREATE_NEW_ICON_ACTION + #define CREATE_NEW_ACTION(w, a) create_action(w, &a, QString(), ACTION_NAME(a)) CREATE_NEW_ACTION(mainWindow, actionPrevPicture); CREATE_NEW_ACTION(mainWindow, actionNextPicture); @@ -59,6 +73,7 @@ void ActionManager::setupAction(MainWindow *mainWindow) CREATE_NEW_ACTION(mainWindow, actionLocateInFileManager); CREATE_NEW_ACTION(mainWindow, actionProperties); CREATE_NEW_ACTION(mainWindow, actionQuitApp); + #undef CREATE_NEW_ACTION retranslateUi(mainWindow); diff --git a/app/actionmanager.h b/app/actionmanager.h index 780f628..ede536c 100644 --- a/app/actionmanager.h +++ b/app/actionmanager.h @@ -19,6 +19,8 @@ public: void retranslateUi(MainWindow *MainWindow); void setupShortcuts(); + static QIcon loadHidpiIcon(const QString &resp, QSize sz = QSize(32, 32)); + public: QAction *actionOpen; diff --git a/app/bottombuttongroup.cpp b/app/bottombuttongroup.cpp index 5e7945c..8d0234d 100644 --- a/app/bottombuttongroup.cpp +++ b/app/bottombuttongroup.cpp @@ -37,7 +37,7 @@ BottomButtonGroup::BottomButtonGroup(const std::vector &actionList, Q auto newActionBtn = [this](QAction * action) -> QToolButton * { QToolButton * btn = new QToolButton(this); btn->setDefaultAction(action); - btn->setIconSize(QSize(40, 40)); + btn->setIconSize(QSize(32, 32)); btn->setFixedSize(40, 40); return btn; }; diff --git a/app/main.cpp b/app/main.cpp index f86fd79..7847c4a 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -20,6 +20,9 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + a.setAttribute(Qt::ApplicationAttribute::AA_UseHighDpiPixmaps); +#endif QTranslator translator; QString qmDir; diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index e54e4b9..9c0c770 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -92,20 +92,21 @@ MainWindow::MainWindow(QWidget *parent) this, &MainWindow::loadGalleryBySingleLocalFile); m_closeButton = new ToolButton(true, m_graphicsView); - m_closeButton->setIcon(QIcon(":/icons/window-close")); - m_closeButton->setIconSize(QSize(50, 50)); + m_closeButton->setIconSize(QSize(32, 32)); + m_closeButton->setFixedSize(QSize(50, 50)); + m_closeButton->setIconResourcePath(":/icons/window-close.svg"); connect(m_closeButton, &QAbstractButton::clicked, this, &MainWindow::closeWindow); m_prevButton = new ToolButton(false, m_graphicsView); - m_prevButton->setIcon(QIcon(":/icons/go-previous")); m_prevButton->setIconSize(QSize(75, 75)); + m_prevButton->setIconResourcePath(":/icons/go-previous.svg"); m_prevButton->setVisible(false); m_prevButton->setOpacity(0, false); m_nextButton = new ToolButton(false, m_graphicsView); - m_nextButton->setIcon(QIcon(":/icons/go-next")); m_nextButton->setIconSize(QSize(75, 75)); + m_nextButton->setIconResourcePath(":/icons/go-next.svg"); m_nextButton->setVisible(false); m_nextButton->setOpacity(0, false); diff --git a/app/toolbutton.cpp b/app/toolbutton.cpp index bc7daaf..5a98e78 100644 --- a/app/toolbutton.cpp +++ b/app/toolbutton.cpp @@ -4,6 +4,7 @@ #include "toolbutton.h" +#include "actionmanager.h" #include "opacityhelper.h" #include @@ -26,6 +27,11 @@ ToolButton::ToolButton(bool hoverColor, QWidget *parent) setStyleSheet(qss); } +void ToolButton::setIconResourcePath(const QString &iconp) +{ + this->setIcon(ActionManager::loadHidpiIcon(iconp, this->iconSize())); +} + void ToolButton::setOpacity(qreal opacity, bool animated) { m_opacityHelper->setOpacity(opacity, animated); diff --git a/app/toolbutton.h b/app/toolbutton.h index dd8539d..8c62f67 100644 --- a/app/toolbutton.h +++ b/app/toolbutton.h @@ -13,6 +13,7 @@ class ToolButton : public QPushButton Q_OBJECT public: ToolButton(bool hoverColor = false, QWidget * parent = nullptr); + void setIconResourcePath(const QString &iconp); public slots: void setOpacity(qreal opacity, bool animated = true);