From ec40e6c7d8e0bc0ce53504dd8e79ea10237f2802 Mon Sep 17 00:00:00 2001 From: jkriege2 Date: Fri, 26 Aug 2022 21:23:19 +0200 Subject: [PATCH] added JKQTPAppSettingController to libexampletools --- examples/libexampletools/CMakeLists.txt | 2 + .../jkqtpappsettingcontroller.cpp | 37 +++++++++++++++++++ .../jkqtpappsettingcontroller.h | 9 +++++ .../jkqtpexampleapplication.cpp | 36 +++++++++++++++--- .../libexampletools/jkqtpexampleapplication.h | 4 +- examples/libexampletools/libexampletools.pri | 6 ++- 6 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 examples/libexampletools/jkqtpappsettingcontroller.cpp create mode 100644 examples/libexampletools/jkqtpappsettingcontroller.h diff --git a/examples/libexampletools/CMakeLists.txt b/examples/libexampletools/CMakeLists.txt index 72313a0a6d..efb2947228 100644 --- a/examples/libexampletools/CMakeLists.txt +++ b/examples/libexampletools/CMakeLists.txt @@ -10,10 +10,12 @@ set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) # Set up source files set(SOURCES ${CMAKE_CURRENT_LIST_DIR}/jkqtpexampleapplication.cpp + ${CMAKE_CURRENT_LIST_DIR}/jkqtpappsettingcontroller.cpp ) set(HEADERS ${CMAKE_CURRENT_LIST_DIR}/jkqtpexampleapplication.h + ${CMAKE_CURRENT_LIST_DIR}/jkqtpappsettingcontroller.h ) diff --git a/examples/libexampletools/jkqtpappsettingcontroller.cpp b/examples/libexampletools/jkqtpappsettingcontroller.cpp new file mode 100644 index 0000000000..b4d589a0c1 --- /dev/null +++ b/examples/libexampletools/jkqtpappsettingcontroller.cpp @@ -0,0 +1,37 @@ +#include "jkqtpappsettingcontroller.h" +#include +#include + + +JKQTPAppSettingController::JKQTPAppSettingController(int &argc, char **argv, bool enableHighDPIIfAVailable) +{ + bool deactivated=false; + for (int i=0; i= QT_VERSION_CHECK(5,6,0) && QT_VERSION < QT_VERSION_CHECK(6,0,0) + QApplication::setAttribute(Qt::AA_EnableHighDpiScaling, false); // disable DPI support + QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, false); // disable HiDPI pixmaps +#endif +#if QT_VERSION >= QT_VERSION_CHECK(6,0,0) + QApplication::setAttribute(Qt::AA_Use96Dpi, true); // disable DPI support +#endif + deactivated=true; + } + } + if (enableHighDPIIfAVailable && !deactivated) { + //std::cout<<"enabling High-DPI\n"; +#if QT_VERSION >= QT_VERSION_CHECK(5,6,0) && QT_VERSION < QT_VERSION_CHECK(6,0,0) + QApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true); // enable DPI support + QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true); // enable HiDPI pixmaps +#endif +#if QT_VERSION >= QT_VERSION_CHECK(6,0,0) + QApplication::setAttribute(Qt::AA_Use96Dpi, false); // disable revert to 96DPI support +#endif + } +} + +JKQTPAppSettingController::~JKQTPAppSettingController() +{ + +} diff --git a/examples/libexampletools/jkqtpappsettingcontroller.h b/examples/libexampletools/jkqtpappsettingcontroller.h new file mode 100644 index 0000000000..2dcfe1cdf5 --- /dev/null +++ b/examples/libexampletools/jkqtpappsettingcontroller.h @@ -0,0 +1,9 @@ +#pragma once + +class JKQTPAppSettingController { +public: + JKQTPAppSettingController(int &argc, char **argv, bool enableHighDPIIfAVailable=true); + + ~JKQTPAppSettingController(); + +}; diff --git a/examples/libexampletools/jkqtpexampleapplication.cpp b/examples/libexampletools/jkqtpexampleapplication.cpp index b35279b9d0..f5250c08ec 100644 --- a/examples/libexampletools/jkqtpexampleapplication.cpp +++ b/examples/libexampletools/jkqtpexampleapplication.cpp @@ -13,10 +13,21 @@ JKQTPExampleApplication::JKQTPExampleApplication(int &argc, char **argv): saveSmallScreenshot(false), saveScreenshotPlot(false), saveSmallScreenshotPlot(false), + scaleDownFromHighDPI(false), screenshotBasename("screenshot") { screenshotDir=QDir::current(); - + for (int i=0; i= QT_VERSION_CHECK(5,6,0) && QT_VERSION < QT_VERSION_CHECK(6,0,0) + QApplication::setAttribute(Qt::AA_EnableHighDpiScaling, false); // DPI support + QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, false); //HiDPI pixmaps +#endif +#if QT_VERSION >= QT_VERSION_CHECK(6,0,0) + QApplication::setAttribute(Qt::AA_Use96Dpi); // disable DPI support +#endif + } + } } JKQTPExampleApplication::~JKQTPExampleApplication() @@ -41,15 +52,20 @@ void JKQTPExampleApplication::readCmdLine() { parser.addOption(screenshotPlotOption); QCommandLineOption smallscreenshotPlotOption(QStringList()<<"smallscreenshotplot", "save screenshot(s) of the plot(s)."); parser.addOption(smallscreenshotPlotOption); + QCommandLineOption scaleDownFromHighDPIOption(QStringList()<<"scalescreenshotdownfromhighdpi", "if on high-dpi device, rescale to standard size."); + parser.addOption(scaleDownFromHighDPIOption); + QCommandLineOption disablehighdpiOption(QStringList()<<"disablehighdpi", "idisable high-dpi support."); + parser.addOption(disablehighdpiOption); parser.process(*this); screenshotDir=QDir(parser.value(outputDirectoryOption)); - screenshotBasename=parser.value(basenameOption); + screenshotBasename=parser.value(basenameOption).split(','); saveScreenshot = parser.isSet(screenshotOption); saveSmallScreenshot = parser.isSet(smallscreenshotOption); saveScreenshotPlot = parser.isSet(screenshotPlotOption); saveSmallScreenshotPlot = parser.isSet(smallscreenshotPlotOption); + scaleDownFromHighDPI = parser.isSet(scaleDownFromHighDPIOption); } QRect JKQTPExampleApplication::getBoundsWithoutColor(QImage qImage, const QColor &exclusionColor) @@ -92,8 +108,8 @@ int JKQTPExampleApplication::exec() QWidget* w=widgets[i]; if (w->isVisible()) { JKQTPlotter* plot=dynamic_cast(w); - QString bn=screenshotBasename; - if (iVisible>0) { + QString bn=screenshotBasename.value(iVisible, screenshotBasename.value(0)); + if (iVisible>0 && screenshotBasename.value(iVisible, "")=="") { bn+=QString("_win%1").arg(iVisible, 2, 10, QLatin1Char('0')); } if (w) { @@ -105,7 +121,11 @@ int JKQTPExampleApplication::exec() pix=pix_win; }*/ if (saveScreenshot) { - pix_win.save(screenshotDir.absoluteFilePath(bn+".png")); + if (scaleDownFromHighDPI && pix_win.devicePixelRatio()>1.0) { + pix_win.scaled((QSizeF(pix_win.size())/pix_win.devicePixelRatio()).toSize()).save(screenshotDir.absoluteFilePath(bn+".png")); + } else { + pix_win.save(screenshotDir.absoluteFilePath(bn+".png")); + } } if (saveSmallScreenshot) { QPixmap img=pix_win.scaledToWidth(150, Qt::SmoothTransformation); @@ -119,7 +139,11 @@ int JKQTPExampleApplication::exec() if (saveScreenshotPlot) { QString fn=bn+"_small.png"; if (saveScreenshot) fn=bnp+"_small.png"; - gr.save(screenshotDir.absoluteFilePath(fn)); + if (scaleDownFromHighDPI && gr.devicePixelRatio()>1.0) { + gr.scaled((QSizeF(gr.size())/gr.devicePixelRatio()).toSize()).save(screenshotDir.absoluteFilePath(fn)); + } else { + gr.save(screenshotDir.absoluteFilePath(fn)); + } } if (saveSmallScreenshotPlot) { QString fn=bn+"_small.png"; diff --git a/examples/libexampletools/jkqtpexampleapplication.h b/examples/libexampletools/jkqtpexampleapplication.h index 67f1b5ddb6..d38b415bf8 100644 --- a/examples/libexampletools/jkqtpexampleapplication.h +++ b/examples/libexampletools/jkqtpexampleapplication.h @@ -1,6 +1,7 @@ #pragma once #include #include "jkqtplotter/jkqtplotter.h" +#include "jkqtpappsettingcontroller.h" #include @@ -20,7 +21,8 @@ protected: bool saveSmallScreenshot; bool saveScreenshotPlot; bool saveSmallScreenshotPlot; - QString screenshotBasename; + bool scaleDownFromHighDPI; + QStringList screenshotBasename; void readCmdLine(); QRect getBoundsWithoutColor(QImage qImage, const QColor &exclusionColor = Qt::white); }; diff --git a/examples/libexampletools/libexampletools.pri b/examples/libexampletools/libexampletools.pri index e562b87238..5f7e40a2a5 100644 --- a/examples/libexampletools/libexampletools.pri +++ b/examples/libexampletools/libexampletools.pri @@ -5,10 +5,12 @@ isEmpty(JKQTP_LIBEXAMPLETOOLS_PRI_INCLUDED) { INCLUDEPATH += $PWD - HEADERS += $$PWD/jkqtpexampleapplication.h + HEADERS += $$PWD/jkqtpexampleapplication.h \ + $$PWD/jkqtpappsettingcontroller.h - SOURCES += $$PWD/jkqtpexampleapplication.cpp + SOURCES += $$PWD/jkqtpexampleapplication.cpp \ + $$PWD/jkqtpappsettingcontroller.cpp }