From a39ea9d0a6173f15ffa9904d7bb0315c24bd3383 Mon Sep 17 00:00:00 2001 From: Nikolay Zlatev Date: Sat, 12 Jun 2021 22:15:11 +0300 Subject: [PATCH] Application: Move DB4SProxyStyle to the Application unit. Set default maximum icon size for QToolBar to 20px, and for MenuItem to 16px, before that MenuItem icon was bigger than QToolBar icon. Pixel metric for SmallIconSize is set to 12px to avoid strange render behaviour for QDockWidget with custom stylesheet. --- src/Application.cpp | 46 ++++++++++++++++++++++++++++++++++++++++----- src/main.cpp | 31 +++--------------------------- 2 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index ee046078..f72a5b26 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include "Application.h" #include "MainWindow.h" @@ -15,6 +17,37 @@ #include "version.h" #include "sqlitedb.h" +class DB4SProxyStyle final : public QProxyStyle { +public: + DB4SProxyStyle(int toolBarIconSize, int menuItemIconSize, QStyle *style = nullptr) + : QProxyStyle(style), m_toolBarIconSize(toolBarIconSize), m_menuItemIconSize(menuItemIconSize) {} + + int pixelMetric(QStyle::PixelMetric metric, + const QStyleOption *option = nullptr, + const QWidget *widget = nullptr) const override { + if (metric == PM_ToolBarIconSize) { + return m_toolBarIconSize; + } else if (metric == PM_SmallIconSize) { + // set maximum icon size for SmallIcon + + // Set default MenuIcon size + if(option && option->type == QStyleOption::SO_MenuItem) { + return m_menuItemIconSize; + } + + // if we don't return size before QDockWidgets are created, + // any of the widgets with custom stylesheet are rendered with huge float,close buttons + // look void TableBrowserDock::setFocusStyle + return 12; + } + return QProxyStyle::pixelMetric(metric, option, widget); + } + +private: + int m_toolBarIconSize; + int m_menuItemIconSize; +}; + void printArgument(const QString& argument, const QString& description) { const int fieldWidth = 20; @@ -31,12 +64,15 @@ void printArgument(const QString& argument, const QString& description) Application::Application(int& argc, char** argv) : QApplication(argc, argv) { + // Set StyleProxy + setStyle(new DB4SProxyStyle(20, 16, style())); + // Get 'DB4S_SETTINGS_FILE' environment variable - const char* env = getenv("DB4S_SETTINGS_FILE"); + const auto env = qgetenv("DB4S_SETTINGS_FILE"); // If 'DB4S_SETTINGS_FILE' environment variable exists - if(env) - Settings::setUserSettingsFile(QString::fromStdString(env)); + if(!env.isEmpty()) + Settings::setUserSettingsFile(env); for(int i=1;i -#include static QString message = QString(); @@ -28,36 +27,15 @@ void boxMessageOutput(QtMsgType, const QMessageLogContext &, const QString &msg) message += msg + "\n"; } -class DB4SProxyStyle final : public QProxyStyle { -public: - DB4SProxyStyle(int toolBarIconSize) - : QProxyStyle(nullptr), toolBarIconSize_(toolBarIconSize) {} - - int pixelMetric(QStyle::PixelMetric metric, - const QStyleOption *option = nullptr, - const QWidget *widget = nullptr) const override { - - if (metric == QStyle::PM_ToolBarIconSize) { - return toolBarIconSize_; - } - - return QProxyStyle::pixelMetric(metric, option, widget); - } - -private: - int toolBarIconSize_; -}; - int main( int argc, char ** argv ) { - #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) - QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true); + QApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true); #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); + QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); #endif #endif - QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true); + QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true); #ifdef Q_OS_WIN // In Windows, there is no output to terminal for a graphical application, so we install // the output to message box, until the main window is shown or the application exits. @@ -67,9 +45,6 @@ int main( int argc, char ** argv ) // Create application object. All the initialisation stuff happens in there Application a(argc, argv); - // Set default QToolbar->iconSize via StyleProxy - a.setStyle(new DB4SProxyStyle(16)); - // If there has been invocations to the message handler, show it to user in a message box. if(!message.isEmpty()) { QMessageBox messageBox;