diff --git a/src/gui/formmain.cpp b/src/gui/formmain.cpp index 493436c2d..0244e1382 100644 --- a/src/gui/formmain.cpp +++ b/src/gui/formmain.cpp @@ -1,5 +1,9 @@ +// for testing +#include + #include "gui/formmain.h" #include "gui/formsettings.h" +#include "gui/themefactory.h" #include "core/settings.h" #include "qtsingleapplication/qtsingleapplication.h" @@ -29,6 +33,22 @@ void FormMain::cleanupResources() { qDebug("Cleaning up resources before the application exits."); } +#if defined(Q_OS_LINUX) +bool FormMain::event(QEvent *event) { + if (event->type() == ThemeFactoryEvent::type()) { + // Handle the change of icon theme. + setupIcons(); + return true; + } + + return QMainWindow::event(event); +} + +void FormMain::setupIcons() { + // NOTE: Call QIcon::fromTheme for all needed widgets here. +} +#endif + void FormMain::createConnections() { // Menu "File" connections. connect(m_ui->m_actionQuit, &QAction::triggered, this, &FormMain::quit); diff --git a/src/gui/formmain.h b/src/gui/formmain.h index c303e5a2a..1da197ca3 100644 --- a/src/gui/formmain.h +++ b/src/gui/formmain.h @@ -16,6 +16,13 @@ class FormMain : public QMainWindow { protected: void createConnections(); +#if defined(Q_OS_LINUX) + bool event(QEvent *event); + + // Sets up proper icons for this widget. + void setupIcons(); +#endif + public slots: void processExecutionMessage(const QString &message); void quit(); diff --git a/src/gui/themefactory.cpp b/src/gui/themefactory.cpp index 6045fa8ab..2a16051bc 100644 --- a/src/gui/themefactory.cpp +++ b/src/gui/themefactory.cpp @@ -4,10 +4,36 @@ #include #include "gui/themefactory.h" +#include "qtsingleapplication/qtsingleapplication.h" #include "core/settings.h" #include "core/defs.h" +QEvent::Type ThemeFactoryEvent::m_typeOfEvent = QEvent::None; + +// +// ThemeFactoryEvent class +// + +ThemeFactoryEvent::ThemeFactoryEvent() : QEvent(ThemeFactoryEvent::type()) { +} + +ThemeFactoryEvent::~ThemeFactoryEvent() { +} + +QEvent::Type ThemeFactoryEvent::type() { + if (m_typeOfEvent == QEvent::None) { + m_typeOfEvent = static_cast(QEvent::registerEventType(2000)); + } + + return m_typeOfEvent; +} + + +// +// ThemeFactory class +// + ThemeFactory::ThemeFactory() { } @@ -64,6 +90,15 @@ void ThemeFactory::loadCurrentIconTheme() { else { qDebug("Loading theme '%s'.", qPrintable(theme_name)); QIcon::setThemeName(theme_name); + + // In Linux, we need to deliver custom event for all widgets + // to make sure they get a chance to redraw their icons. +#if defined(Q_OS_LINUX) + foreach (QWidget *widget, QtSingleApplication::allWidgets()) { + QtSingleApplication::postEvent((QObject*) widget, + new ThemeFactoryEvent()); + } +#endif } } diff --git a/src/gui/themefactory.h b/src/gui/themefactory.h index f2c9ee793..b13a91376 100644 --- a/src/gui/themefactory.h +++ b/src/gui/themefactory.h @@ -2,6 +2,7 @@ #define THEMEFACTORY_H #include +#include class ThemeFactory { @@ -36,4 +37,19 @@ class ThemeFactory { static void setCurrentIconTheme(const QString &theme_name); }; +class ThemeFactoryEvent : public QEvent { + public: + enum Type { + IconThemeChange = 2000 + }; + + ThemeFactoryEvent(); + virtual ~ThemeFactoryEvent(); + + static QEvent::Type type(); + + private: + static QEvent::Type m_typeOfEvent; +}; + #endif // THEMEFACTORY_H