From cb7c6d16cbfc3fed930257142c70282729e4e4d1 Mon Sep 17 00:00:00 2001 From: Dominic Clark Date: Sun, 9 Feb 2025 04:33:44 +0000 Subject: [PATCH] Reload stylesheet when CSS file changes (#6331) * Reload stylesheet when CSS file changes --------- Co-authored-by: Tres Finocchiaro --- include/LmmsStyle.h | 2 ++ src/gui/LmmsStyle.cpp | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/include/LmmsStyle.h b/include/LmmsStyle.h index d17bbed98..4c1c60e3d 100644 --- a/include/LmmsStyle.h +++ b/include/LmmsStyle.h @@ -26,6 +26,7 @@ #ifndef LMMS_GUI_LMMS_STYLE_H #define LMMS_GUI_LMMS_STYLE_H +#include #include @@ -60,6 +61,7 @@ public: private: QImage colorizeXpm( const char * const * xpm, const QBrush& fill ) const; void hoverColors( bool sunken, bool hover, bool active, QColor& color, QColor& blend ) const; + QFileSystemWatcher m_styleReloader; }; diff --git a/src/gui/LmmsStyle.cpp b/src/gui/LmmsStyle.cpp index 50a2a9de4..26f4c853c 100644 --- a/src/gui/LmmsStyle.cpp +++ b/src/gui/LmmsStyle.cpp @@ -25,14 +25,17 @@ #include -#include #include +#include +#include #include #include #include #include +#include "embed.h" #include "LmmsStyle.h" +#include "TextFloat.h" namespace lmms::gui @@ -137,6 +140,29 @@ LmmsStyle::LmmsStyle() : file.open( QIODevice::ReadOnly ); qApp->setStyleSheet( file.readAll() ); + m_styleReloader.addPath(QFileInfo{file}.absoluteFilePath()); + connect(&m_styleReloader, &QFileSystemWatcher::fileChanged, this, + [this](const QString& path) + { + if (auto file = QFile{path}; file.exists()) + { + file.open(QIODevice::ReadOnly); + qApp->setStyleSheet(file.readAll()); + TextFloat::displayMessage( + tr("Theme updated"), + tr("LMMS theme file %1 has been reloaded.").arg(file.fileName()), + embed::getIconPixmap("colorize"), + 3000 + ); + // Handle delete + overwrite events + if (!m_styleReloader.files().contains(path)) + { + m_styleReloader.addPath(path); + } + } + } + ); + if( s_palette != nullptr ) { qApp->setPalette( *s_palette ); } setBaseStyle( QStyleFactory::create( "Fusion" ) );