commit 9e6cf6e05a25d38e8d7d9ecbf360b2aa5fd06d03
Author: Enrico Forestieri <[email protected]>
Date: Sun Jan 22 01:06:00 2017 +0100
Allow to properly scale the GUI with Qt5
Starting with Qt 5.6, setting the environment variable QT_SCALE_FACTOR
makes everything accordingly bigger. So, if QT_SCALE_FACTOR=1.2, all
text and GUI elements are rendered 20% bigger. However, if an application
does not account for this, everything will also look "blocky".
With this commit, all text and images will be scaled remaining sharp.
This works whether a HiDpi screen is used or not, but is mostly useful
with a HiDpi screen, as all GUI elements are more spaced apart and one
can use the mouse for selecting things without requiring a high precision.
---
src/LyX.cpp | 18 ++++++++++++++++++
src/LyX.h | 1 +
src/frontends/qt4/GuiApplication.cpp | 5 ++++-
src/frontends/qt4/GuiView.cpp | 4 ++--
src/frontends/qt4/GuiWorkArea.cpp | 2 +-
5 files changed, 26 insertions(+), 4 deletions(-)
diff --git a/src/LyX.cpp b/src/LyX.cpp
index 0f6b9b6..bf40298 100644
--- a/src/LyX.cpp
+++ b/src/LyX.cpp
@@ -52,6 +52,7 @@
#include "frontends/Application.h"
#include "support/ConsoleApplication.h"
+#include "support/convert.h"
#include "support/lassert.h"
#include "support/debug.h"
#include "support/environment.h"
@@ -121,6 +122,14 @@ RunMode run_mode = PREFERRED;
OverwriteFiles force_overwrite = UNSPECIFIED;
+// Scale the GUI by this factor. This works whether we have a HiDpi screen
+// or not and scales everything, also fonts. Can only be changed by setting
+// the QT_SCALE_FACTOR environment variable before launching LyX and only
+// works properly with Qt 5.6 or higher.
+
+double qt_scale_factor = 1.0;
+
+
namespace {
// Filled with the command line arguments "foo" of "-sysdir foo" or
@@ -303,6 +312,15 @@ int LyX::exec(int & argc, char * argv[])
// we need to parse for "-dbg" and "-help"
easyParse(argc, argv);
+ // Check whether Qt will scale all GUI elements and accordingly
+ // set the scale factor so that to avoid blurred images and text
+ char const * const scale_factor = getenv("QT_SCALE_FACTOR");
+ if (scale_factor) {
+ qt_scale_factor = convert<double>(scale_factor);
+ if (qt_scale_factor < 1.0)
+ qt_scale_factor = 1.0;
+ }
+
try {
init_package(os::utf8_argv(0), cl_system_support,
cl_user_support);
} catch (ExceptionMessage const & message) {
diff --git a/src/LyX.h b/src/LyX.h
index 11c67a3..85decbc 100644
--- a/src/LyX.h
+++ b/src/LyX.h
@@ -55,6 +55,7 @@ extern bool verbose;
extern bool ignore_missing_glyphs;
extern RunMode run_mode;
extern OverwriteFiles force_overwrite;
+extern double qt_scale_factor;
namespace frontend {
class Application;
diff --git a/src/frontends/qt4/GuiApplication.cpp
b/src/frontends/qt4/GuiApplication.cpp
index 2f6f8f2..0eb55ba 100644
--- a/src/frontends/qt4/GuiApplication.cpp
+++ b/src/frontends/qt4/GuiApplication.cpp
@@ -1017,6 +1017,9 @@ GuiApplication::GuiApplication(int & argc, char ** argv)
QCoreApplication::setOrganizationName(app_name);
QCoreApplication::setOrganizationDomain("lyx.org");
QCoreApplication::setApplicationName(lyx_package);
+#if QT_VERSION >= 0x050000
+ QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
+#endif
qsrand(QDateTime::currentDateTime().toTime_t());
@@ -1101,7 +1104,7 @@ GuiApplication * theGuiApp()
double GuiApplication::pixelRatio() const
{
#if QT_VERSION >= 0x050000
- return devicePixelRatio();
+ return qt_scale_factor * devicePixelRatio();
#else
return 1.0;
#endif
diff --git a/src/frontends/qt4/GuiView.cpp b/src/frontends/qt4/GuiView.cpp
index 2c48124..a6b5877 100644
--- a/src/frontends/qt4/GuiView.cpp
+++ b/src/frontends/qt4/GuiView.cpp
@@ -227,7 +227,7 @@ private:
/// Current ratio between physical pixels and device-independent pixels
double pixelRatio() const {
#if QT_VERSION >= 0x050000
- return devicePixelRatio();
+ return qt_scale_factor * devicePixelRatio();
#else
return 1.0;
#endif
@@ -1341,7 +1341,7 @@ void GuiView::resetCommandExecute()
double GuiView::pixelRatio() const
{
#if QT_VERSION >= 0x050000
- return devicePixelRatio();
+ return qt_scale_factor * devicePixelRatio();
#else
return 1.0;
#endif
diff --git a/src/frontends/qt4/GuiWorkArea.cpp
b/src/frontends/qt4/GuiWorkArea.cpp
index aa5e285..8a02372 100644
--- a/src/frontends/qt4/GuiWorkArea.cpp
+++ b/src/frontends/qt4/GuiWorkArea.cpp
@@ -275,7 +275,7 @@ GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & gv)
double GuiWorkArea::pixelRatio() const
{
#if QT_VERSION >= 0x050000
- return devicePixelRatio();
+ return qt_scale_factor * devicePixelRatio();
#else
return 1.0;
#endif