Allow to scale GUI on high DPI screens

This commit is contained in:
Klaus Basan
2018-05-24 11:15:54 +02:00
parent 26dcc547e6
commit 86d02716a2
6 changed files with 42 additions and 12 deletions

View File

@@ -85,6 +85,7 @@ namespace BlackGui
{
this->addWindowModeOption();
this->addWindowResetSizeOption();
this->addWindowScaleSizeOption();
// notify when app goes down
connect(qGuiApp, &QGuiApplication::lastWindowClosed, this, &CGuiApplication::gracefulShutdown);
@@ -117,9 +118,7 @@ namespace BlackGui
void CGuiApplication::addWindowModeOption()
{
m_cmdWindowMode = QCommandLineOption(QStringList() << "w" << "window",
QCoreApplication::translate("main", "Windows: (n)ormal, (f)rameless, (t)ool."),
"windowtype");
m_cmdWindowMode = QCommandLineOption({"w", "window"}, QCoreApplication::translate("main", "Windows: (n)ormal, (f)rameless, (t)ool."), "windowtype");
this->addParserOption(m_cmdWindowMode);
}
@@ -129,6 +128,14 @@ namespace BlackGui
this->addParserOption(m_cmdWindowSizeReset);
}
void CGuiApplication::addWindowScaleSizeOption()
{
// just added here to display it in help
// parseScaleFactor() is used since it is needed upfront (before application is created)
m_cmdWindowScaleSize = QCommandLineOption("scale", QCoreApplication::translate("main", "Scale: number."), "scalevalue");
this->addParserOption(m_cmdWindowScaleSize);
}
void CGuiApplication::addWindowStateOption()
{
m_cmdWindowStateMinimized = QCommandLineOption({{"m", "minimized"}, QCoreApplication::translate("main", "Start minimized in system tray.")});
@@ -259,7 +266,7 @@ namespace BlackGui
else
{
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); // DPI support
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); //HiDPI pixmaps
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); // HiDPI pixmaps
const QString sf = QString::number(scaleFactor, 'f', 2);
qputenv("QT_SCALE_FACTOR", sf.toLatin1());
}
@@ -371,6 +378,22 @@ namespace BlackGui
return html;
}
qreal CGuiApplication::parseScaleFactor(int argc, char *argv[])
{
for (int i = 1; i < argc; ++i)
{
if (qstrcmp(argv[i], "--scale") == 0 || qstrcmp(argv[i], "-scale") == 0)
{
if (i + 1 >= argc) { return -1.0; } // no value
const QString factor(argv[i + 1]);
bool ok;
qreal f = factor.toFloat(&ok);
return ok ? f : -1.0;
}
}
return -1.0;
}
bool CGuiApplication::cmdLineErrorMessage(const QString &errorMessage, bool retry) const
{
const QString helpText(beautifyHelpMessage(m_parser.helpText()));

View File

@@ -93,9 +93,12 @@ namespace BlackGui
//! CMD line arguments
void addWindowModeOption();
//! CMD line arguments
//! CMD line arguments (reset size store)
void addWindowResetSizeOption();
//! CMD line arguments (scale size on DPI screens)
void addWindowScaleSizeOption();
//! Window state
Qt::WindowState getWindowState() const;
@@ -234,6 +237,9 @@ namespace BlackGui
//! Uses the high DPI support?
static bool isUsingHighDpiScreenSupport();
//! Parse scale factor if any
static qreal parseScaleFactor(int argc, char *argv[]);
signals:
//! Style sheet changed
void styleSheetsChanged();
@@ -276,8 +282,9 @@ namespace BlackGui
int m_minWidthChars = -1; //!< min. width characters (based on current font metrics)
int m_minHeightChars = -1; //!< min. height characters (based on current font metrics)
QCommandLineOption m_cmdWindowStateMinimized { "empty" }; //!< window state (minimized)
QCommandLineOption m_cmdWindowMode { "empty" }; //!< window mode (flags: frameless ...)
QCommandLineOption m_cmdWindowSizeReset { "empty" }; //!< window size resizing
QCommandLineOption m_cmdWindowMode { "empty" }; //!< window mode (flags: frameless ...)
QCommandLineOption m_cmdWindowSizeReset { "empty" }; //!< window size reset
QCommandLineOption m_cmdWindowScaleSize { "empty" }; //!< window scale size
CStyleSheetUtility m_styleSheetUtility {{}, this}; //!< style sheet utility
bool m_uiSetupCompleted = false; //!< ui setup completed
bool m_saveMainWidgetState = true; //!< save/restore main widget's state