From 02f52e7adb3a37c4835d8fb0da9540794a42a2ef Mon Sep 17 00:00:00 2001 From: Damien Marchal Date: Tue, 19 Aug 2025 01:00:34 +0200 Subject: [PATCH 1/3] Add support for compatibility with light/dark mode and fix palette and colors style. --- src/sofa/qt/RealGUI.cpp | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/sofa/qt/RealGUI.cpp b/src/sofa/qt/RealGUI.cpp index 1f21437..ecd0bd8 100644 --- a/src/sofa/qt/RealGUI.cpp +++ b/src/sofa/qt/RealGUI.cpp @@ -94,6 +94,7 @@ using sofa::simulation::SceneLoaderFactory; #include #include #include +#include #if (QT_VERSION < QT_VERSION_CHECK(5, 11, 0)) #include @@ -143,12 +144,54 @@ using namespace sofa::core::visual; class QSOFAApplication : public QApplication { public: + bool isDarkMode() { + #if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) + const auto scheme = QGuiApplication::styleHints()->colorScheme(); + return scheme == Qt::ColorScheme::Dark; + #else + const QPalette defaultPalette; + const auto text = defaultPalette.color(QPalette::WindowText); + const auto window = defaultPalette.color(QPalette::Window); + return text.lightness() > window.lightness(); + #endif // QT_VERSION + } + + void setLightModeColors(){ + msg_info("runSofa") << "Use light mode color scheme."; + setStyle("Fusion"); + QPalette palette = QApplication::style()->standardPalette(); + setStyleSheet(R"( + QToolTip { color: black; background-color: #fff8dc; border: 1px solid white; } + )"); + setPalette(palette); + } + + void setDarkModeColors() + { + msg_info("runSofa") << "Use dark mode color scheme."; + setStyle("Fusion"); + + QPalette palette = QApplication::style()->standardPalette(); + QColor text = palette.color(QPalette::ButtonText); + setStyleSheet(R"( + QToolTip { color: black; background-color: #fff8dc; border: 1px solid white; } + )"); + palette.setColor(QPalette::Button, palette.color(QPalette::Base)); + setPalette(palette); + } + QSOFAApplication(int &argc, char ** argv) : QApplication(argc,argv) { QCoreApplication::setOrganizationName("Sofa Consortium"); QCoreApplication::setOrganizationDomain("sofa"); QCoreApplication::setApplicationName("runSofa"); + + if(isDarkMode()){ + setDarkModeColors(); + }else{ + setLightModeColors(); + } } #if QT_VERSION < 0x050000 From 47aa366803430583d349a2f1775ef614c302391f Mon Sep 17 00:00:00 2001 From: Damien Marchal Date: Tue, 19 Aug 2025 01:16:46 +0200 Subject: [PATCH 2/3] Simpler fix, no more need to detect the underlying style. --- src/sofa/qt/QDataDescriptionWidget.cpp | 1 + src/sofa/qt/QTabulationModifyObject.cpp | 1 + src/sofa/qt/RealGUI.cpp | 35 ++----------------------- 3 files changed, 4 insertions(+), 33 deletions(-) diff --git a/src/sofa/qt/QDataDescriptionWidget.cpp b/src/sofa/qt/QDataDescriptionWidget.cpp index e6040bd..89b98be 100644 --- a/src/sofa/qt/QDataDescriptionWidget.cpp +++ b/src/sofa/qt/QDataDescriptionWidget.cpp @@ -76,6 +76,7 @@ QDataDescriptionWidget::QDataDescriptionWidget(QWidget* parent, core::objectmode tabLayout->setContentsMargins(0,0,0,0); tabLayout->setSpacing(1); tabLayout->setObjectName("tabInfoLayout"); + setAutoFillBackground(true); //Instance { diff --git a/src/sofa/qt/QTabulationModifyObject.cpp b/src/sofa/qt/QTabulationModifyObject.cpp index 7805b34..624d37e 100644 --- a/src/sofa/qt/QTabulationModifyObject.cpp +++ b/src/sofa/qt/QTabulationModifyObject.cpp @@ -44,6 +44,7 @@ QTabulationModifyObject::QTabulationModifyObject(QWidget* parent, vbox->setObjectName("tabVisualizationLayout"); vbox->setContentsMargins(0, 0, 0, 0); vbox->setSpacing(0); + setAutoFillBackground(true); this->setLayout(vbox); diff --git a/src/sofa/qt/RealGUI.cpp b/src/sofa/qt/RealGUI.cpp index ecd0bd8..659549d 100644 --- a/src/sofa/qt/RealGUI.cpp +++ b/src/sofa/qt/RealGUI.cpp @@ -144,40 +144,13 @@ using namespace sofa::core::visual; class QSOFAApplication : public QApplication { public: - bool isDarkMode() { - #if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) - const auto scheme = QGuiApplication::styleHints()->colorScheme(); - return scheme == Qt::ColorScheme::Dark; - #else - const QPalette defaultPalette; - const auto text = defaultPalette.color(QPalette::WindowText); - const auto window = defaultPalette.color(QPalette::Window); - return text.lightness() > window.lightness(); - #endif // QT_VERSION - } - - void setLightModeColors(){ - msg_info("runSofa") << "Use light mode color scheme."; + void setDefaultStyle(){ setStyle("Fusion"); QPalette palette = QApplication::style()->standardPalette(); - setStyleSheet(R"( - QToolTip { color: black; background-color: #fff8dc; border: 1px solid white; } - )"); setPalette(palette); - } - - void setDarkModeColors() - { - msg_info("runSofa") << "Use dark mode color scheme."; - setStyle("Fusion"); - - QPalette palette = QApplication::style()->standardPalette(); - QColor text = palette.color(QPalette::ButtonText); setStyleSheet(R"( QToolTip { color: black; background-color: #fff8dc; border: 1px solid white; } )"); - palette.setColor(QPalette::Button, palette.color(QPalette::Base)); - setPalette(palette); } QSOFAApplication(int &argc, char ** argv) @@ -187,11 +160,7 @@ class QSOFAApplication : public QApplication QCoreApplication::setOrganizationDomain("sofa"); QCoreApplication::setApplicationName("runSofa"); - if(isDarkMode()){ - setDarkModeColors(); - }else{ - setLightModeColors(); - } + setDefaultStyle(); } #if QT_VERSION < 0x050000 From 62ecac88ec7ef7a012390f1ee71cba3e38e4bd23 Mon Sep 17 00:00:00 2001 From: Damien Marchal Date: Tue, 19 Aug 2025 01:18:38 +0200 Subject: [PATCH 3/3] Simpler --- src/sofa/qt/RealGUI.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/sofa/qt/RealGUI.cpp b/src/sofa/qt/RealGUI.cpp index 659549d..84e0b84 100644 --- a/src/sofa/qt/RealGUI.cpp +++ b/src/sofa/qt/RealGUI.cpp @@ -144,8 +144,7 @@ using namespace sofa::core::visual; class QSOFAApplication : public QApplication { public: - void setDefaultStyle(){ - setStyle("Fusion"); + void setStyle(){ QPalette palette = QApplication::style()->standardPalette(); setPalette(palette); setStyleSheet(R"( @@ -160,7 +159,7 @@ class QSOFAApplication : public QApplication QCoreApplication::setOrganizationDomain("sofa"); QCoreApplication::setApplicationName("runSofa"); - setDefaultStyle(); + setStyle(); } #if QT_VERSION < 0x050000