Use colors from Plasma theme in Plasmoid

One might configure a light color theme for applications and a dark theme
for Plasma (or vice versa) so this is an important difference, see
https://github.com/Martchus/syncthingtray/issues/126.
This commit is contained in:
Martchus 2022-02-02 22:38:36 +01:00
parent c5c34bd83c
commit 3e38a9917e
6 changed files with 56 additions and 19 deletions

View File

@ -10,7 +10,7 @@ set(META_APP_CATEGORIES "Network;FileTransfer")
set(META_GUI_OPTIONAL false) set(META_GUI_OPTIONAL false)
set(META_VERSION_MAJOR 1) set(META_VERSION_MAJOR 1)
set(META_VERSION_MINOR 1) set(META_VERSION_MINOR 1)
set(META_VERSION_PATCH 15) set(META_VERSION_PATCH 16)
set(META_VERSION_EXACT_SONAME ON) set(META_VERSION_EXACT_SONAME ON)
set(META_ADD_DEFAULT_CPP_UNIT_TEST_APPLICATION ON) set(META_ADD_DEFAULT_CPP_UNIT_TEST_APPLICATION ON)

View File

@ -300,30 +300,32 @@ ForkAwesomeIcons::ForkAwesomeIcons(QtForkAwesome::Renderer &renderer, const QCol
{ {
} }
IconManager::IconManager() IconManager::IconManager(const QPalette *palette)
: m_statusIcons() : m_statusIcons()
, m_trayIcons(m_statusIcons) , m_trayIcons(m_statusIcons)
, m_commonForkAwesomeIcons(m_forkAwesomeRenderer, QGuiApplication::palette().color(QPalette::Normal, QPalette::Text), QSize(64, 64)) , m_commonForkAwesomeIcons(m_forkAwesomeRenderer, (palette ? *palette : QGuiApplication::palette()).color(QPalette::Normal, QPalette::Text), QSize(64, 64))
{ {
#ifdef __GNUC__ #ifdef __GNUC__
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif #endif
QObject::connect(qGuiApp, &QGuiApplication::paletteChanged, this, &IconManager::handlePaletteChanged); if (!palette) {
QObject::connect(qGuiApp, &QGuiApplication::paletteChanged, this, &IconManager::setPalette);
}
#ifdef __GNUC__ #ifdef __GNUC__
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
} }
void IconManager::handlePaletteChanged(const QPalette &pal) void IconManager::setPalette(const QPalette &palette)
{ {
emit forkAwesomeIconsChanged( emit forkAwesomeIconsChanged(
m_commonForkAwesomeIcons = ForkAwesomeIcons(m_forkAwesomeRenderer, pal.color(QPalette::Normal, QPalette::Text), QSize(64, 64))); m_commonForkAwesomeIcons = ForkAwesomeIcons(m_forkAwesomeRenderer, palette.color(QPalette::Normal, QPalette::Text), QSize(64, 64)));
} }
IconManager &IconManager::instance() IconManager &IconManager::instance(const QPalette *palette)
{ {
static IconManager iconManager; static auto iconManager = IconManager(palette);
return iconManager; return iconManager;
} }

View File

@ -143,7 +143,7 @@ struct LIB_SYNCTHING_MODEL_EXPORT ForkAwesomeIcons {
class LIB_SYNCTHING_MODEL_EXPORT IconManager : public QObject { class LIB_SYNCTHING_MODEL_EXPORT IconManager : public QObject {
Q_OBJECT Q_OBJECT
public: public:
static IconManager &instance(); static IconManager &instance(const QPalette *palette = nullptr);
void applySettings(const StatusIconSettings *statusIconSettings = nullptr, const StatusIconSettings *trayIconSettings = nullptr); void applySettings(const StatusIconSettings *statusIconSettings = nullptr, const StatusIconSettings *trayIconSettings = nullptr);
const StatusIcons &statusIcons() const; const StatusIcons &statusIcons() const;
@ -151,15 +151,15 @@ public:
QtForkAwesome::Renderer &forkAwesomeRenderer(); QtForkAwesome::Renderer &forkAwesomeRenderer();
const ForkAwesomeIcons &commonForkAwesomeIcons() const; const ForkAwesomeIcons &commonForkAwesomeIcons() const;
Q_SIGNALS: public Q_SLOTS:
void statusIconsChanged(const StatusIcons &newStatusIcons, const StatusIcons &newTrayIcons); void setPalette(const QPalette &palette);
void forkAwesomeIconsChanged(const ForkAwesomeIcons &newForkAwesomeIcons);
private Q_SLOTS: Q_SIGNALS:
void handlePaletteChanged(const QPalette &pal); void statusIconsChanged(const Data::StatusIcons &newStatusIcons, const Data::StatusIcons &newTrayIcons);
void forkAwesomeIconsChanged(const Data::ForkAwesomeIcons &newForkAwesomeIcons);
private: private:
IconManager(); explicit IconManager(const QPalette *palette = nullptr);
StatusIcons m_statusIcons; StatusIcons m_statusIcons;
StatusIcons m_trayIcons; StatusIcons m_trayIcons;

View File

@ -12,7 +12,7 @@ find_package(${PACKAGE_NAMESPACE_PREFIX}qtutilities${CONFIGURATION_PACKAGE_SUFFI
use_qt_utilities() use_qt_utilities()
# find qtforkawesomequickimageprovider # find qtforkawesomequickimageprovider
find_package(${PACKAGE_NAMESPACE_PREFIX}qtquickforkawesome${CONFIGURATION_PACKAGE_SUFFIX_QTFORKAWESOME} 0.0.1 REQUIRED) find_package(${PACKAGE_NAMESPACE_PREFIX}qtquickforkawesome${CONFIGURATION_PACKAGE_SUFFIX_QTFORKAWESOME} 0.0.3 REQUIRED)
use_qt_quick_fork_awesome() use_qt_quick_fork_awesome()
# check whether qtutilities supports DBus notifications # check whether qtutilities supports DBus notifications

View File

@ -31,6 +31,8 @@
#include <KConfigGroup> #include <KConfigGroup>
#include <Plasma/Theme>
#include <QClipboard> #include <QClipboard>
#include <QDesktopServices> #include <QDesktopServices>
#include <QGuiApplication> #include <QGuiApplication>
@ -51,6 +53,8 @@ namespace Plasmoid {
SyncthingApplet::SyncthingApplet(QObject *parent, const QVariantList &data) SyncthingApplet::SyncthingApplet(QObject *parent, const QVariantList &data)
: Applet(parent, data) : Applet(parent, data)
, m_palette(m_theme.palette())
, m_iconManager(IconManager::instance(&m_palette))
, m_aboutDlg(nullptr) , m_aboutDlg(nullptr)
, m_connection() , m_connection()
, m_notifier(m_connection) , m_notifier(m_connection)
@ -61,6 +65,7 @@ SyncthingApplet::SyncthingApplet(QObject *parent, const QVariantList &data)
, m_downloadModel(m_connection) , m_downloadModel(m_connection)
, m_recentChangesModel(m_connection) , m_recentChangesModel(m_connection)
, m_settingsDlg(nullptr) , m_settingsDlg(nullptr)
, m_imageProvider(nullptr)
#ifndef SYNCTHINGWIDGETS_NO_WEBVIEW #ifndef SYNCTHINGWIDGETS_NO_WEBVIEW
, m_webViewDlg(nullptr) , m_webViewDlg(nullptr)
#endif #endif
@ -112,7 +117,8 @@ void SyncthingApplet::init()
connect(&m_dbusNotifier, &DBusStatusNotifier::showNotificationsRequested, this, &SyncthingApplet::showNotificationsDialog); connect(&m_dbusNotifier, &DBusStatusNotifier::showNotificationsRequested, this, &SyncthingApplet::showNotificationsDialog);
connect(&m_dbusNotifier, &DBusStatusNotifier::errorDetailsRequested, this, &SyncthingApplet::showInternalErrorsDialog); connect(&m_dbusNotifier, &DBusStatusNotifier::errorDetailsRequested, this, &SyncthingApplet::showInternalErrorsDialog);
connect(&m_dbusNotifier, &DBusStatusNotifier::webUiRequested, this, &SyncthingApplet::showWebUI); connect(&m_dbusNotifier, &DBusStatusNotifier::webUiRequested, this, &SyncthingApplet::showWebUI);
connect(&IconManager::instance(), &IconManager::statusIconsChanged, this, &SyncthingApplet::connectionStatusChanged); connect(&m_iconManager, &IconManager::statusIconsChanged, this, &SyncthingApplet::connectionStatusChanged);
connect(&m_theme, &Plasma::Theme::themeChanged, this, &SyncthingApplet::handleThemeChanged);
// restore settings // restore settings
Settings::restore(); Settings::restore();
@ -137,11 +143,14 @@ void SyncthingApplet::init()
void SyncthingApplet::initEngine(QObject *object) void SyncthingApplet::initEngine(QObject *object)
{ {
auto engine = qmlEngine(object); const auto engine = qmlEngine(object);
if (!engine) { if (!engine) {
return; return;
} }
engine->addImageProvider(QStringLiteral("fa"), new QtForkAwesome::QuickImageProvider(IconManager::instance().forkAwesomeRenderer())); const auto color = m_theme.color(Plasma::Theme::TextColor, Plasma::Theme::NormalColorGroup);
m_imageProvider = new QtForkAwesome::QuickImageProvider(m_iconManager.forkAwesomeRenderer(), color);
connect(engine, &QObject::destroyed, this, &SyncthingApplet::handleImageProviderDestroyed); // engine has ownership over image provider
engine->addImageProvider(QStringLiteral("fa"), m_imageProvider);
} }
QIcon SyncthingApplet::statusIcon() const QIcon SyncthingApplet::statusIcon() const
@ -524,6 +533,19 @@ void SyncthingApplet::handleSystemdServiceError(const QString &context, const QS
QNetworkReply::NoError, QNetworkRequest(), QByteArray()); QNetworkReply::NoError, QNetworkRequest(), QByteArray());
} }
void Plasmoid::SyncthingApplet::handleImageProviderDestroyed()
{
m_imageProvider = nullptr;
}
void SyncthingApplet::handleThemeChanged()
{
IconManager::instance().setPalette(m_theme.palette());
if (m_imageProvider) {
m_imageProvider->setDefaultColor(m_theme.color(Plasma::Theme::TextColor, Plasma::Theme::NormalColorGroup));
}
}
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD #ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
void SyncthingApplet::handleSystemdStatusChanged() void SyncthingApplet::handleSystemdStatusChanged()
{ {

View File

@ -24,17 +24,24 @@
#define PLASMA_NO_DEPRECATED_WARNINGS 1 #define PLASMA_NO_DEPRECATED_WARNINGS 1
#include <Plasma/Applet> #include <Plasma/Applet>
#include <Plasma/Theme>
#include <QPalette>
#include <QSize> #include <QSize>
namespace Data { namespace Data {
struct SyncthingConnectionSettings; struct SyncthingConnectionSettings;
class IconManager;
} // namespace Data } // namespace Data
namespace QtGui { namespace QtGui {
class WebViewDialog; class WebViewDialog;
} }
namespace QtForkAwesome {
class QuickImageProvider;
}
namespace Plasmoid { namespace Plasmoid {
class SettingsDialog; class SettingsDialog;
@ -171,9 +178,14 @@ private Q_SLOTS:
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD #ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
void handleSystemdStatusChanged(); void handleSystemdStatusChanged();
#endif #endif
void handleImageProviderDestroyed();
void handleThemeChanged();
void setPassive(bool passive); void setPassive(bool passive);
private: private:
Plasma::Theme m_theme;
QPalette m_palette;
Data::IconManager &m_iconManager;
QtUtilities::AboutDialog *m_aboutDlg; QtUtilities::AboutDialog *m_aboutDlg;
Data::SyncthingConnection m_connection; Data::SyncthingConnection m_connection;
Data::SyncthingOverallDirStatistics m_overallStats; Data::SyncthingOverallDirStatistics m_overallStats;
@ -192,6 +204,7 @@ private:
SettingsDialog *m_settingsDlg; SettingsDialog *m_settingsDlg;
QtGui::DBusStatusNotifier m_dbusNotifier; QtGui::DBusStatusNotifier m_dbusNotifier;
std::vector<Data::SyncthingLogEntry> m_notifications; std::vector<Data::SyncthingLogEntry> m_notifications;
QtForkAwesome::QuickImageProvider *m_imageProvider;
#ifndef SYNCTHINGWIDGETS_NO_WEBVIEW #ifndef SYNCTHINGWIDGETS_NO_WEBVIEW
QtGui::WebViewDialog *m_webViewDlg; QtGui::WebViewDialog *m_webViewDlg;
#endif #endif