diff --git a/src/plasma/applet.cpp b/src/plasma/applet.cpp index 7f0043c4b..37ee1296a 100644 --- a/src/plasma/applet.cpp +++ b/src/plasma/applet.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -223,6 +224,21 @@ void Applet::restore(KConfigGroup &group) //TODO: implement; the shortcut } */ + + // User background hints + // TODO support flags in the config + QByteArray hintsString = config().readEntry("UserBackgroundHints", QString()).toUtf8(); + QMetaEnum hintEnum = QMetaEnum::fromType(); + bool ok; + int value = hintEnum.keyToValue(hintsString.constData(), &ok); + if (ok) { + d->userBackgroundHints = Plasma::Types::BackgroundHints(value); + d->userBackgroundHintsInitialized = true; + emit userBackgroundHintsChanged(); + if (d->backgroundHints & Plasma::Types::ConfigurableBackground) { + emit effectiveBackgroundHintsChanged(); + } + } } void Applet::setLaunchErrorMessage(const QString &message) @@ -399,6 +415,64 @@ void Applet::setBusy(bool busy) emit busyChanged(busy); } +Plasma::Types::BackgroundHints Applet::backgroundHints() const +{ + return d->backgroundHints; +} + +void Applet::setBackgroundHints(Plasma::Types::BackgroundHints hint) +{ + if (d->backgroundHints == hint) { + return; + } + + Plasma::Types::BackgroundHints oldeffectiveHints = effectiveBackgroundHints(); + + d->backgroundHints = hint; + emit backgroundHintsChanged(); + + if (oldeffectiveHints != effectiveBackgroundHints()) { + emit effectiveBackgroundHintsChanged(); + } +} + +Plasma::Types::BackgroundHints Applet::effectiveBackgroundHints() const +{ + if (d->userBackgroundHintsInitialized + && (d->backgroundHints & Plasma::Types::ConfigurableBackground)) { + return d->userBackgroundHints; + } else { + return d->backgroundHints; + } +} + +Plasma::Types::BackgroundHints Applet::userBackgroundHints() const +{ + return d->userBackgroundHints; +} + +void Applet::setUserBackgroundHints(Plasma::Types::BackgroundHints hint) +{ + if (d->userBackgroundHints == hint && d->userBackgroundHintsInitialized) { + return; + } + + d->userBackgroundHints = hint; + d->userBackgroundHintsInitialized = true; + QMetaEnum hintEnum = QMetaEnum::fromType(); + config().writeEntry("UserBackgroundHints", hintEnum.valueToKey(d->userBackgroundHints)); + if (containment() && containment()->corona()) { + containment()->corona()->requestConfigSync(); + } + + emit userBackgroundHintsChanged(); + + if (d->backgroundHints & Plasma::Types::ConfigurableBackground) { + emit effectiveBackgroundHintsChanged(); + } +} + + KPluginInfo Applet::pluginInfo() const { return KPluginInfo(d->appletDescription); diff --git a/src/plasma/applet.h b/src/plasma/applet.h index 0d4ab0f86..3cf310b22 100644 --- a/src/plasma/applet.h +++ b/src/plasma/applet.h @@ -75,6 +75,21 @@ class PLASMA_EXPORT Applet : public QObject Q_PROPERTY(QString icon READ icon WRITE setIcon NOTIFY iconChanged FINAL) Q_PROPERTY(bool busy READ isBusy WRITE setBusy NOTIFY busyChanged FINAL) + /** + * How the applet wants its background to be drawn. The containment may chose to ignore this hint. + */ + Q_PROPERTY(Plasma::Types::BackgroundHints backgroundHints WRITE setBackgroundHints READ backgroundHints NOTIFY backgroundHintsChanged FINAL) + + /** + * The containment (and/or the user) may decide to use another kind of background instead (if supported by the applet) + */ + Q_PROPERTY(Plasma::Types::BackgroundHints userBackgroundHints WRITE setUserBackgroundHints READ userBackgroundHints NOTIFY userBackgroundHintsChanged FINAL) + + /** + * The effective background hints the applet has, internally decided how to mix with userBackgroundHints + */ + Q_PROPERTY(Plasma::Types::BackgroundHints effectiveBackgroundHints READ effectiveBackgroundHints NOTIFY effectiveBackgroundHintsChanged FINAL) + public: //CONSTRUCTORS /** @@ -370,6 +385,38 @@ public: */ void setBusy(bool busy); + /** + * How the applet wants its background to be drawn. The containment may chose to ignore this hint. + * @since 5.65 + */ + Plasma::Types::BackgroundHints backgroundHints() const; + + /** + * Sets the applet background hints. Only Applet implementations should write this property + * @since 5.65 + */ + void setBackgroundHints(Plasma::Types::BackgroundHints hint); + + /** + * The containment (and/or the user) may decide to use another kind of background instead if supported by the applet. + * In order for an applet to support user configuration of the + * background, it needs to have the Plasma::Types::ConfigurableBackground flag set in its backgroundHints + * @since 5.65 + */ + Plasma::Types::BackgroundHints userBackgroundHints() const; + + /** + * Sets the hints the user wished the background style for the applet to be. + * @since 5.65 + */ + void setUserBackgroundHints(Plasma::Types::BackgroundHints hint); + + /** + * The effective background hints the applet will have: it will follow userBackgroundHints only if backgroundHints has the Plasma::Types::ConfigurableBackground flag set + * @since 5.65 + */ + Plasma::Types::BackgroundHints effectiveBackgroundHints() const; + //ACTIONS /** * Returns a list of context-related QAction instances. @@ -484,6 +531,24 @@ Q_SIGNALS: */ void busyChanged(bool busy); + /** + * Emitted when the background hints have changed + * @since 5.65 + */ + void backgroundHintsChanged(); + + /** + * Emitted when the user background hints have changed + * @since 5.65 + */ + void userBackgroundHintsChanged(); + + /** + * Emitted when the effective background hints have changed + * @since 5.65 + */ + void effectiveBackgroundHintsChanged(); + //CONFIGURATION /** * Emitted when an applet has changed values in its configuration @@ -578,6 +643,7 @@ public Q_SLOTS: /** * Called when applet configuration values have changed. */ + //TODO KF6: make it not a slot anymore and protected virtual void configChanged(); //UTILS diff --git a/src/plasma/private/applet_p.h b/src/plasma/private/applet_p.h index 8c4221cf7..8514d0659 100644 --- a/src/plasma/private/applet_p.h +++ b/src/plasma/private/applet_p.h @@ -111,7 +111,11 @@ public: QString configurationRequiredReason; + Types::BackgroundHints backgroundHints = Types::DefaultBackground; + Types::BackgroundHints userBackgroundHints = Types::DefaultBackground; + // a great green field of booleans :) + bool userBackgroundHintsInitialized = false; bool hasConfigurationInterface : 1; bool failed : 1; bool transient : 1; diff --git a/src/scriptengines/qml/plasmoid/appletinterface.cpp b/src/scriptengines/qml/plasmoid/appletinterface.cpp index 989c8fbd9..c2974748f 100644 --- a/src/scriptengines/qml/plasmoid/appletinterface.cpp +++ b/src/scriptengines/qml/plasmoid/appletinterface.cpp @@ -53,8 +53,6 @@ AppletInterface::AppletInterface(DeclarativeAppletScript *script, const QVariant m_toolTipTextFormat(0), m_toolTipItem(nullptr), m_args(args), - m_backgroundHints(Plasma::Types::StandardBackground), - m_userBackgroundHints(Plasma::Types::StandardBackground), m_hideOnDeactivate(true), m_oldKeyboardShortcut(0), m_dummyNativeInterface(nullptr), @@ -94,6 +92,13 @@ AppletInterface::AppletInterface(DeclarativeAppletScript *script, const QVariant connect(applet(), &Plasma::Applet::busyChanged, this, &AppletInterface::busyChanged); + connect(applet(), &Plasma::Applet::backgroundHintsChanged, + this, &AppletInterface::backgroundHintsChanged); + connect(applet(), &Plasma::Applet::effectiveBackgroundHintsChanged, + this, &AppletInterface::effectiveBackgroundHintsChanged); + connect(applet(), &Plasma::Applet::userBackgroundHintsChanged, + this, &AppletInterface::userBackgroundHintsChanged); + connect(applet(), &Plasma::Applet::configurationRequiredChanged, this, [this](bool configurationRequired, const QString &reason) { Q_UNUSED(configurationRequired); @@ -194,19 +199,6 @@ void AppletInterface::init() } else if (!m_args.isEmpty()) { emit externalData(QString(), m_args); } - - QByteArray hintsString = applet()->config().readEntry("UserBackgroundHints", QString()).toUtf8(); - QMetaEnum hintEnum = QMetaEnum::fromType(); - bool ok; - int value = hintEnum.keyToValue(hintsString.constData(), &ok); - if (ok) { - m_userBackgroundHints = Plasma::Types::BackgroundHints(value); - m_userBackgroundHintsInitialized = true; - emit userBackgroundHintsChanged(); - if (m_backgroundHints & Plasma::Types::ConfigurableBackground) { - emit effectiveBackgroundHintsChanged(); - } - } } void AppletInterface::destroyedChanged(bool destroyed) @@ -400,59 +392,27 @@ void AppletInterface::setBusy(bool busy) Plasma::Types::BackgroundHints AppletInterface::backgroundHints() const { - return m_backgroundHints; + return applet()->backgroundHints(); } void AppletInterface::setBackgroundHints(Plasma::Types::BackgroundHints hint) { - if (m_backgroundHints == hint) { - return; - } - - Plasma::Types::BackgroundHints oldeffectiveHints = effectiveBackgroundHints(); - - m_backgroundHints = hint; - emit backgroundHintsChanged(); - - if (oldeffectiveHints != effectiveBackgroundHints()) { - emit effectiveBackgroundHintsChanged(); - } + applet()->setBackgroundHints(hint); } Plasma::Types::BackgroundHints AppletInterface::effectiveBackgroundHints() const { - if (m_userBackgroundHintsInitialized - && (m_backgroundHints & Plasma::Types::ConfigurableBackground)) { - return m_userBackgroundHints; - } else { - return m_backgroundHints; - } + return applet()->effectiveBackgroundHints(); } Plasma::Types::BackgroundHints AppletInterface::userBackgroundHints() const { - return m_userBackgroundHints; + return applet()->userBackgroundHints(); } void AppletInterface::setUserBackgroundHints(Plasma::Types::BackgroundHints hint) { - if (m_userBackgroundHints == hint && m_userBackgroundHintsInitialized) { - return; - } - - m_userBackgroundHints = hint; - m_userBackgroundHintsInitialized = true; - QMetaEnum hintEnum = QMetaEnum::fromType(); - applet()->config().writeEntry("UserBackgroundHints", hintEnum.valueToKey(m_userBackgroundHints)); - if (applet()->containment() && applet()->containment()->corona()) { - applet()->containment()->corona()->requestConfigSync(); - } - - emit userBackgroundHintsChanged(); - - if (m_backgroundHints & Plasma::Types::ConfigurableBackground) { - emit effectiveBackgroundHintsChanged(); - } + applet()->setUserBackgroundHints(hint); } void AppletInterface::setConfigurationRequired(bool needsConfiguring, const QString &reason) diff --git a/src/scriptengines/qml/plasmoid/appletinterface.h b/src/scriptengines/qml/plasmoid/appletinterface.h index 2043e5bf1..f495bde88 100644 --- a/src/scriptengines/qml/plasmoid/appletinterface.h +++ b/src/scriptengines/qml/plasmoid/appletinterface.h @@ -502,9 +502,6 @@ private: int m_toolTipTextFormat; QPointer m_toolTipItem; QVariantList m_args; - Plasma::Types::BackgroundHints m_backgroundHints; - Plasma::Types::BackgroundHints m_userBackgroundHints; - bool m_userBackgroundHintsInitialized = false; bool m_hideOnDeactivate : 1; bool m_loading = false; //this is used to build an emacs style shortcut