From 204267d211b0858c301786f28bdef657a14c4279 Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Tue, 14 May 2024 15:03:38 +0400 Subject: [PATCH] React to scale or color changes in editor tables --- .../opencs/view/world/datadisplaydelegate.cpp | 30 +++++++++++++++++++ .../opencs/view/world/datadisplaydelegate.hpp | 5 ++++ 2 files changed, 35 insertions(+) diff --git a/apps/opencs/view/world/datadisplaydelegate.cpp b/apps/opencs/view/world/datadisplaydelegate.cpp index 989d1af464..adaea848a0 100644 --- a/apps/opencs/view/world/datadisplaydelegate.cpp +++ b/apps/opencs/view/world/datadisplaydelegate.cpp @@ -41,14 +41,44 @@ CSVWorld::DataDisplayDelegate::DataDisplayDelegate(const ValueList& values, cons , mIconSize(QSize(16, 16)) , mHorizontalMargin(QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1) , mTextLeftOffset(8) + , mPixmapsColor(QApplication::palette().text().color()) + , mUiScale(static_cast(QGuiApplication::instance())->devicePixelRatio()) , mSettingKey(pageName + '/' + settingName) { + parent->installEventFilter(this); + buildPixmaps(); if (!pageName.empty()) updateDisplayMode(CSMPrefs::get()[pageName][settingName].toString()); } +bool CSVWorld::DataDisplayDelegate::eventFilter(QObject* target, QEvent* event) +{ + if (event->type() == QEvent::Resize) + { + auto uiScale = static_cast(QGuiApplication::instance())->devicePixelRatio(); + if (mUiScale != uiScale) + { + mUiScale = uiScale; + + buildPixmaps(); + } + } + else if (event->type() == QEvent::PaletteChange) + { + QColor themeColor = QApplication::palette().text().color(); + if (themeColor != mPixmapsColor) + { + mPixmapsColor = themeColor; + + buildPixmaps(); + } + } + + return false; +} + void CSVWorld::DataDisplayDelegate::buildPixmaps() { if (!mPixmaps.empty()) diff --git a/apps/opencs/view/world/datadisplaydelegate.hpp b/apps/opencs/view/world/datadisplaydelegate.hpp index 087fc2a084..3b3c935d8b 100755 --- a/apps/opencs/view/world/datadisplaydelegate.hpp +++ b/apps/opencs/view/world/datadisplaydelegate.hpp @@ -41,6 +41,7 @@ namespace CSVWorld class DataDisplayDelegate : public EnumDelegate { + Q_OBJECT public: typedef std::vector IconList; typedef std::vector> ValueList; @@ -61,6 +62,8 @@ namespace CSVWorld QSize mIconSize; int mHorizontalMargin; int mTextLeftOffset; + QColor mPixmapsColor; + qreal mUiScale; std::string mSettingKey; @@ -80,6 +83,8 @@ namespace CSVWorld /// offset the horizontal position of the text from the right edge of the icon. Default is 8 pixels. void setTextLeftOffset(int offset); + bool eventFilter(QObject* target, QEvent* event) override; + private: /// update the display mode based on a passed string void updateDisplayMode(const std::string&);