fix: proper svg icon rendering on hidpi screen.
This commit is contained in:
parent
756d0be32f
commit
cc7d58d4ec
|
@ -6,22 +6,15 @@
|
|||
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QGuiApplication>
|
||||
#include <QSvgRenderer>
|
||||
#include <QPainter>
|
||||
|
||||
#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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@ BottomButtonGroup::BottomButtonGroup(const std::vector<QAction *> &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;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include "toolbutton.h"
|
||||
|
||||
#include "actionmanager.h"
|
||||
#include "opacityhelper.h"
|
||||
|
||||
#include <QPainter>
|
||||
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user