[LyX/master] Properly scale some icons for HiDPI (#12695)

Juergen Spitzmueller spitz at lyx.org
Sat Mar 11 12:46:32 UTC 2023


commit 8863d6d785358680d05b4b34b35ca09a15cccb82
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Sat Mar 11 14:49:07 2023 +0100

    Properly scale some icons for HiDPI (#12695)
---
 src/frontends/qt/GuiApplication.cpp |   25 +++++++++++++++++++++++++
 src/frontends/qt/GuiApplication.h   |    3 +++
 src/frontends/qt/GuiDocument.cpp    |   18 ++++++++++++------
 src/frontends/qt/GuiLyXFiles.cpp    |   26 ++++++++++++++++++--------
 src/frontends/qt/PanelStack.cpp     |    3 ++-
 5 files changed, 60 insertions(+), 15 deletions(-)

diff --git a/src/frontends/qt/GuiApplication.cpp b/src/frontends/qt/GuiApplication.cpp
index c316456..cee49ad 100644
--- a/src/frontends/qt/GuiApplication.cpp
+++ b/src/frontends/qt/GuiApplication.cpp
@@ -114,6 +114,7 @@
 #include <QSocketNotifier>
 #include <QSortFilterProxyModel>
 #include <QStandardItemModel>
+#include <QSvgRenderer>
 #include <QTimer>
 #include <QTranslator>
 #include <QThreadPool>
@@ -2666,6 +2667,30 @@ bool GuiApplication::unhide(Buffer * buf)
 }
 
 
+QPixmap GuiApplication::getScaledPixmap(QString imagedir, QString name) const
+{
+	qreal dpr = 1.0;
+	// Consider device/pixel ratio (HiDPI)
+	if (currentView())
+		dpr = currentView()->devicePixelRatio();
+	// We render SVG directly for HiDPI scalability
+	QPixmap pm = getPixmap(imagedir, name, "svgz,png");
+	FileName fname = imageLibFileSearch(imagedir, name, "svgz,png");
+	QString fpath = toqstr(fname.absFileName());
+	if (!fpath.isEmpty()) {
+		QSvgRenderer svgRenderer(fpath);
+		if (svgRenderer.isValid()) {
+			pm = QPixmap(pm.size() * dpr);
+			pm.fill(Qt::transparent);
+			QPainter painter(&pm);
+			svgRenderer.render(&painter);
+			pm.setDevicePixelRatio(dpr);
+		}
+	}
+	return pm;
+}
+
+
 Clipboard & GuiApplication::clipboard()
 {
 	return d->clipboard_;
diff --git a/src/frontends/qt/GuiApplication.h b/src/frontends/qt/GuiApplication.h
index f0483c3..3eab5ec 100644
--- a/src/frontends/qt/GuiApplication.h
+++ b/src/frontends/qt/GuiApplication.h
@@ -94,6 +94,9 @@ public:
 	/// Return true if current position is RTL of if no document is open and interface if RTL
 	bool rtlContext() const;
 
+	/// Scale Pixmaps properly (also for HiDPI)
+	QPixmap getScaledPixmap(QString imagedir, QString name) const;
+
 	///
 	Clipboard & clipboard();
 	///
diff --git a/src/frontends/qt/GuiDocument.cpp b/src/frontends/qt/GuiDocument.cpp
index 3a8ae85..f92c129 100644
--- a/src/frontends/qt/GuiDocument.cpp
+++ b/src/frontends/qt/GuiDocument.cpp
@@ -614,7 +614,8 @@ void PreambleModule::editExternal() {
 	preambleTE->setReadOnly(true);
 	theFormats().edit(*current_id_, tempfilename, format);
 	editPB->setText(qt_("&End Edit"));
-	QIcon warn(getPixmap("images/", "emblem-shellescape", "svgz,png"));
+	QIcon warn(guiApp ? guiApp->getScaledPixmap("images/", "emblem-shellescape-user")
+			  : getPixmap("images/", "emblem-shellescape", "svgz,png"));
 	editPB->setIcon(warn);
 	changed();
 }
@@ -793,7 +794,8 @@ void LocalLayout::editExternal() {
 	locallayoutTE->setReadOnly(true);
 	theFormats().edit(*current_id_, tempfilename, format);
 	editPB->setText(qt_("&End Edit"));
-	QIcon warn(getPixmap("images/", "emblem-shellescape", "svgz,png"));
+	QIcon warn(guiApp ? guiApp->getScaledPixmap("images/", "emblem-shellescape-user")
+			  : getPixmap("images/", "emblem-shellescape", "svgz,png"));
 	editPB->setIcon(warn);
 	validatePB->setEnabled(false);
 	hideConvert();
@@ -1960,8 +1962,10 @@ void GuiDocument::filterModules(QString const & str)
 			return 0 < b.name.localeAwareCompare(a.name);
 		});
 
-	QIcon user_icon(getPixmap("images/", "lyxfiles-user", "svgz,png"));
-	QIcon system_icon(getPixmap("images/", "lyxfiles-system", "svgz,png"));
+	QIcon user_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-user")
+			       : getPixmap("images/", "lyxfiles-user", "svgz,png"));
+	QIcon system_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-system")
+				 : getPixmap("images/", "lyxfiles-system", "svgz,png"));
 
 	int i = 0;
 	for (modInfoStruct const & m : modInfoList) {
@@ -4672,8 +4676,10 @@ void GuiDocument::updateAvailableModules()
 	modInfoList.sort([](modInfoStruct const & a, modInfoStruct const & b) {
 			return 0 < b.name.localeAwareCompare(a.name);
 		});
-	QIcon user_icon(getPixmap("images/", "lyxfiles-user", "svgz,png"));
-	QIcon system_icon(getPixmap("images/", "lyxfiles-system", "svgz,png"));
+	QIcon user_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-user")
+			       : getPixmap("images/", "lyxfiles-user", "svgz,png"));
+	QIcon system_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-system")
+				 : getPixmap("images/", "lyxfiles-system", "svgz,png"));
 	int i = 0;
 	QFont catfont;
 	catfont.setBold(true);
diff --git a/src/frontends/qt/GuiLyXFiles.cpp b/src/frontends/qt/GuiLyXFiles.cpp
index 1bfbdbb..ed2b399 100644
--- a/src/frontends/qt/GuiLyXFiles.cpp
+++ b/src/frontends/qt/GuiLyXFiles.cpp
@@ -214,8 +214,10 @@ GuiLyXFiles::GuiLyXFiles(GuiView & lv)
 	//filesLW->setViewMode(QListView::ListMode);
 	filesLW->setIconSize(QSize(22, 22));
 
-	QIcon user_icon(getPixmap("images/", "lyxfiles-user", "svgz,png"));
-	QIcon system_icon(getPixmap("images/", "lyxfiles-system", "svgz,png"));
+	QIcon user_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-user")
+			       : getPixmap("images/", "lyxfiles-user", "svgz,png"));
+	QIcon system_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-system")
+				 : getPixmap("images/", "lyxfiles-system", "svgz,png"));
 	fileTypeCO->addItem(qt_("User and System Files"), toqstr("all"));
 	fileTypeCO->addItem(user_icon, qt_("User Files Only"), toqstr("user"));
 	fileTypeCO->addItem(system_icon, qt_("System Files Only"), toqstr("system"));
@@ -308,8 +310,10 @@ void GuiLyXFiles::on_filesLW_itemClicked(QTreeWidgetItem * item, int)
 	setLanguage();
 	QString const realpath = getRealPath();
 	filesLW->currentItem()->setData(0, Qt::ToolTipRole, realpath);
-	QIcon user_icon(getPixmap("images/", "lyxfiles-user", "svgz,png"));
-	QIcon system_icon(getPixmap("images/", "lyxfiles-system", "svgz,png"));
+	QIcon user_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-user")
+			       : getPixmap("images/", "lyxfiles-user", "svgz,png"));
+	QIcon system_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-system")
+				 : getPixmap("images/", "lyxfiles-system", "svgz,png"));
 	QIcon file_icon = (realpath.startsWith(toqstr(package().user_support().absFileName()))) ?
 			user_icon : system_icon;
 	item->setIcon(0, file_icon);
@@ -410,10 +414,16 @@ void GuiLyXFiles::updateContents()
 	languageCO->model()->sort(0);
 
 	filesLW->clear();
-	QIcon user_icon(getPixmap("images/", "lyxfiles-user", "svgz,png"));
-	QIcon system_icon(getPixmap("images/", "lyxfiles-system", "svgz,png"));
-	QIcon user_folder_icon(getPixmap("images/", "lyxfiles-user-folder", "svgz,png"));
-	QIcon system_folder_icon(getPixmap("images/", "lyxfiles-system-folder", "svgz,png"));
+
+	QIcon user_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-user")
+			       : getPixmap("images/", "lyxfiles-user", "svgz,png"));
+	QIcon system_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-system")
+				 : getPixmap("images/", "lyxfiles-system", "svgz,png"));
+	QIcon user_folder_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-user-folder")
+				      : getPixmap("images/", "lyxfiles-user-folder", "svgz,png"));
+	QIcon system_folder_icon(guiApp ? guiApp->getScaledPixmap("images/", "lyxfiles-system-folder")
+					: getPixmap("images/", "lyxfiles-system-folder", "svgz,png"));
+
 	QStringList cats;
 	QMap<QString, QString>::const_iterator it = files.constBegin();
 	QFont capfont;
diff --git a/src/frontends/qt/PanelStack.cpp b/src/frontends/qt/PanelStack.cpp
index 9b6cb63..4649b97 100644
--- a/src/frontends/qt/PanelStack.cpp
+++ b/src/frontends/qt/PanelStack.cpp
@@ -155,7 +155,8 @@ void PanelStack::markPanelValid(QString const & name, bool valid)
 		item->setIcon(0, QIcon());
 		item->setToolTip(0, QString());
 	} else {
-		QIcon warn(getPixmap("images/", "emblem-shellescape", "svgz,png"));
+		QIcon warn(guiApp ? guiApp->getScaledPixmap("images/", "emblem-shellescape-user")
+				  : getPixmap("images/", "emblem-shellescape", "svgz,png"));
 		item->setIcon(0, warn);
 		item->setToolTip(0, qt_("This section contains invalid input. Please fix!"));
 	}


More information about the lyx-cvs mailing list