8#include "platformtheme.h"
9#include "basictheme_p.h"
10#include "platformpluginfactory.h"
13#include <QGuiApplication>
14#include <QPluginLoader>
19#include <QQuickWindow>
25#include <unordered_map>
47struct TypeInitializer {
57static TypeInitializer initializer;
62class PlatformThemeData :
public QObject
82 AlternateBackgroundColor,
84 ActiveBackgroundColor,
86 VisitedLinkBackgroundColor,
87 NegativeBackgroundColor,
88 NeutralBackgroundColor,
89 PositiveBackgroundColor,
98 using ColorMap = std::unordered_map<std::underlying_type<ColorRole>::type,
QColor>;
105 PlatformTheme::ColorGroup colorGroup = PlatformTheme::Active;
107 std::array<QColor, ColorRoleCount> colors;
118 using Watcher = PlatformTheme *;
123 if (
sender != owner || colorSet == set) {
127 auto oldValue = colorSet;
131 notifyWatchers<PlatformTheme::ColorSet>(
sender, oldValue, set);
134 inline void setColorGroup(PlatformTheme *
sender, PlatformTheme::ColorGroup group)
136 if (
sender != owner || colorGroup == group) {
140 auto oldValue = colorGroup;
145 notifyWatchers<PlatformTheme::ColorGroup>(
sender, oldValue, group);
148 inline void setColor(PlatformTheme *
sender, ColorRole role,
const QColor &color)
150 if (
sender != owner || colors[role] == color) {
154 auto oldValue = colors[role];
156 colors[role] = color;
157 updatePalette(palette, colors);
159 notifyWatchers<QColor>(
sender, oldValue, colors[role]);
162 inline void setDefaultFont(PlatformTheme *
sender,
const QFont &font)
164 if (
sender != owner || font == defaultFont) {
168 auto oldValue = defaultFont;
172 notifyWatchers<QFont>(
sender, oldValue, font);
175 inline void setSmallFont(PlatformTheme *
sender,
const QFont &font)
177 if (
sender != owner || font == smallFont) {
181 auto oldValue = smallFont;
185 notifyWatchers<QFont>(
sender, oldValue, smallFont);
188 inline void addChangeWatcher(PlatformTheme *
object)
193 inline void removeChangeWatcher(PlatformTheme *
object)
199 inline void notifyWatchers(PlatformTheme *
sender,
const T &oldValue,
const T &newValue)
201 for (
auto object : std::as_const(watchers)) {
202 PlatformThemeEvents::PropertyChangedEvent<T>
event(
sender, oldValue, newValue);
208 inline static void updatePalette(
QPalette &palette,
const std::array<QColor, ColorRoleCount> &colors)
210 for (std::size_t i = 0; i < colors.size(); ++i) {
211 setPaletteColor(palette, ColorRole(i), colors.at(i));
216 inline static void updatePalette(
QPalette &palette,
const ColorMap &colors)
218 for (
auto entry : colors) {
219 setPaletteColor(palette, ColorRole(entry.first), entry.second);
223 inline static void setPaletteColor(
QPalette &palette, ColorRole role,
const QColor &color)
231 case BackgroundColor:
236 case AlternateBackgroundColor:
243 case HighlightedTextColor:
249 case VisitedLinkColor:
259class PlatformThemePrivate
262 PlatformThemePrivate()
264 , supportsIconColoring(false)
265 , pendingColorChange(false)
266 , pendingChildUpdate(false)
267 , useAlternateBackgroundColor(false)
268 , colorSet(PlatformTheme::
Window)
269 , colorGroup(PlatformTheme::Active)
273 inline QColor color(
const PlatformTheme *theme, PlatformThemeData::ColorRole color)
const
279 QColor value = data->colors.at(color);
281 if (data->owner != theme && localOverrides) {
282 auto itr = localOverrides->find(color);
283 if (itr != localOverrides->end()) {
291 inline void setColor(PlatformTheme *theme, PlatformThemeData::ColorRole color,
const QColor &value)
293 if (!localOverrides) {
294 localOverrides = std::make_unique<PlatformThemeData::ColorMap>();
299 auto itr = localOverrides->find(color);
300 if (itr != localOverrides->end()) {
301 PlatformThemeChangeTracker tracker(theme, PlatformThemeChangeTracker::PropertyChange::Color);
302 localOverrides->erase(itr);
316 auto itr = localOverrides->find(color);
317 if (itr != localOverrides->end() && itr->second == value && (data && data->owner != theme)) {
321 PlatformThemeChangeTracker tracker(theme, PlatformThemeChangeTracker::PropertyChange::Color);
323 (*localOverrides)[color] = value;
326 data->setColor(theme, color, value);
330 inline void setDataColor(PlatformTheme *theme, PlatformThemeData::ColorRole color,
const QColor &value)
336 if (localOverrides) {
337 auto itr = localOverrides->find(color);
338 if (itr != localOverrides->end()) {
343 PlatformThemeChangeTracker tracker(theme, PlatformThemeChangeTracker::PropertyChange::Color);
346 data->setColor(theme, color, value);
360 std::shared_ptr<PlatformThemeData> data;
363 std::unique_ptr<PlatformThemeData::ColorMap> localOverrides;
366 bool supportsIconColoring : 1;
367 bool pendingColorChange : 1;
368 bool pendingChildUpdate : 1;
369 bool useAlternateBackgroundColor : 1;
375 uint8_t colorSet : 4;
376 uint8_t colorGroup : 4;
380 static_assert(PlatformTheme::ColorGroupCount <= 16,
"PlatformTheme::ColorGroup contains more elements than can be stored in PlatformThemePrivate");
381 static_assert(PlatformTheme::ColorSetCount <= 16,
"PlatformTheme::ColorSet contains more elements than can be stored in PlatformThemePrivate");
383 inline static PlatformPluginFactory *s_pluginFactory =
nullptr;
386PlatformTheme::PlatformTheme(
QObject *parent)
388 , d(new PlatformThemePrivate)
390 if (
QQuickItem *item = qobject_cast<QQuickItem *>(parent)) {
407PlatformTheme::~PlatformTheme()
410 d->data->removeChangeWatcher(
this);
416void PlatformTheme::setColorSet(PlatformTheme::ColorSet colorSet)
418 PlatformThemeChangeTracker tracker(
this, PlatformThemeChangeTracker::PropertyChange::ColorSet);
419 d->colorSet = colorSet;
422 d->data->setColorSet(
this, colorSet);
426PlatformTheme::ColorSet PlatformTheme::colorSet()
const
428 return d->data ? d->data->colorSet :
Window;
431void PlatformTheme::setColorGroup(PlatformTheme::ColorGroup colorGroup)
433 PlatformThemeChangeTracker tracker(
this, PlatformThemeChangeTracker::PropertyChange::ColorGroup);
434 d->colorGroup = colorGroup;
437 d->data->setColorGroup(
this, colorGroup);
441PlatformTheme::ColorGroup PlatformTheme::colorGroup()
const
443 return d->data ? d->data->colorGroup : Active;
446bool PlatformTheme::inherit()
const
451void PlatformTheme::setInherit(
bool inherit)
453 if (inherit == d->inherit) {
457 d->inherit = inherit;
460 Q_EMIT inheritChanged(inherit);
463QColor PlatformTheme::textColor()
const
465 return d->color(
this, PlatformThemeData::TextColor);
468QColor PlatformTheme::disabledTextColor()
const
470 return d->color(
this, PlatformThemeData::DisabledTextColor);
473QColor PlatformTheme::highlightColor()
const
475 return d->color(
this, PlatformThemeData::HighlightColor);
478QColor PlatformTheme::highlightedTextColor()
const
480 return d->color(
this, PlatformThemeData::HighlightedTextColor);
483QColor PlatformTheme::backgroundColor()
const
485 return d->color(
this, PlatformThemeData::BackgroundColor);
488QColor PlatformTheme::alternateBackgroundColor()
const
490 return d->color(
this, PlatformThemeData::AlternateBackgroundColor);
493QColor PlatformTheme::activeTextColor()
const
495 return d->color(
this, PlatformThemeData::ActiveTextColor);
498QColor PlatformTheme::activeBackgroundColor()
const
500 return d->color(
this, PlatformThemeData::ActiveBackgroundColor);
503QColor PlatformTheme::linkColor()
const
505 return d->color(
this, PlatformThemeData::LinkColor);
508QColor PlatformTheme::linkBackgroundColor()
const
510 return d->color(
this, PlatformThemeData::LinkBackgroundColor);
513QColor PlatformTheme::visitedLinkColor()
const
515 return d->color(
this, PlatformThemeData::VisitedLinkColor);
518QColor PlatformTheme::visitedLinkBackgroundColor()
const
520 return d->color(
this, PlatformThemeData::VisitedLinkBackgroundColor);
523QColor PlatformTheme::negativeTextColor()
const
525 return d->color(
this, PlatformThemeData::NegativeTextColor);
528QColor PlatformTheme::negativeBackgroundColor()
const
530 return d->color(
this, PlatformThemeData::NegativeBackgroundColor);
533QColor PlatformTheme::neutralTextColor()
const
535 return d->color(
this, PlatformThemeData::NeutralTextColor);
538QColor PlatformTheme::neutralBackgroundColor()
const
540 return d->color(
this, PlatformThemeData::NeutralBackgroundColor);
543QColor PlatformTheme::positiveTextColor()
const
545 return d->color(
this, PlatformThemeData::PositiveTextColor);
548QColor PlatformTheme::positiveBackgroundColor()
const
550 return d->color(
this, PlatformThemeData::PositiveBackgroundColor);
553QColor PlatformTheme::focusColor()
const
555 return d->color(
this, PlatformThemeData::FocusColor);
558QColor PlatformTheme::hoverColor()
const
560 return d->color(
this, PlatformThemeData::HoverColor);
564void PlatformTheme::setTextColor(
const QColor &color)
566 d->setDataColor(
this, PlatformThemeData::TextColor, color);
569void PlatformTheme::setDisabledTextColor(
const QColor &color)
571 d->setDataColor(
this, PlatformThemeData::DisabledTextColor, color);
574void PlatformTheme::setBackgroundColor(
const QColor &color)
576 d->setDataColor(
this, PlatformThemeData::BackgroundColor, color);
579void PlatformTheme::setAlternateBackgroundColor(
const QColor &color)
581 d->setDataColor(
this, PlatformThemeData::AlternateBackgroundColor, color);
584void PlatformTheme::setHighlightColor(
const QColor &color)
586 d->setDataColor(
this, PlatformThemeData::HighlightColor, color);
589void PlatformTheme::setHighlightedTextColor(
const QColor &color)
591 d->setDataColor(
this, PlatformThemeData::HighlightedTextColor, color);
594void PlatformTheme::setActiveTextColor(
const QColor &color)
596 d->setDataColor(
this, PlatformThemeData::ActiveTextColor, color);
599void PlatformTheme::setActiveBackgroundColor(
const QColor &color)
601 d->setDataColor(
this, PlatformThemeData::ActiveBackgroundColor, color);
604void PlatformTheme::setLinkColor(
const QColor &color)
606 d->setDataColor(
this, PlatformThemeData::LinkColor, color);
609void PlatformTheme::setLinkBackgroundColor(
const QColor &color)
611 d->setDataColor(
this, PlatformThemeData::LinkBackgroundColor, color);
614void PlatformTheme::setVisitedLinkColor(
const QColor &color)
616 d->setDataColor(
this, PlatformThemeData::VisitedLinkColor, color);
619void PlatformTheme::setVisitedLinkBackgroundColor(
const QColor &color)
621 d->setDataColor(
this, PlatformThemeData::VisitedLinkBackgroundColor, color);
624void PlatformTheme::setNegativeTextColor(
const QColor &color)
626 d->setDataColor(
this, PlatformThemeData::NegativeTextColor, color);
629void PlatformTheme::setNegativeBackgroundColor(
const QColor &color)
631 d->setDataColor(
this, PlatformThemeData::NegativeBackgroundColor, color);
634void PlatformTheme::setNeutralTextColor(
const QColor &color)
636 d->setDataColor(
this, PlatformThemeData::NeutralTextColor, color);
639void PlatformTheme::setNeutralBackgroundColor(
const QColor &color)
641 d->setDataColor(
this, PlatformThemeData::NeutralBackgroundColor, color);
644void PlatformTheme::setPositiveTextColor(
const QColor &color)
646 d->setDataColor(
this, PlatformThemeData::PositiveTextColor, color);
649void PlatformTheme::setPositiveBackgroundColor(
const QColor &color)
651 d->setDataColor(
this, PlatformThemeData::PositiveBackgroundColor, color);
654void PlatformTheme::setHoverColor(
const QColor &color)
656 d->setDataColor(
this, PlatformThemeData::HoverColor, color);
659void PlatformTheme::setFocusColor(
const QColor &color)
661 d->setDataColor(
this, PlatformThemeData::FocusColor, color);
664QFont PlatformTheme::defaultFont()
const
666 return d->data ? d->data->defaultFont :
QFont{};
669void PlatformTheme::setDefaultFont(
const QFont &font)
671 PlatformThemeChangeTracker tracker(
this, PlatformThemeChangeTracker::PropertyChange::Font);
673 d->data->setDefaultFont(
this, font);
677QFont PlatformTheme::smallFont()
const
679 return d->data ? d->data->smallFont :
QFont{};
682void PlatformTheme::setSmallFont(
const QFont &font)
684 PlatformThemeChangeTracker tracker(
this, PlatformThemeChangeTracker::PropertyChange::Font);
686 d->data->setSmallFont(
this, font);
690qreal PlatformTheme::frameContrast()
const
698qreal PlatformTheme::lightFrameContrast()
const
702 return frameContrast() / 2.0;
706void PlatformTheme::setCustomTextColor(
const QColor &color)
708 d->setColor(
this, PlatformThemeData::TextColor, color);
711void PlatformTheme::setCustomDisabledTextColor(
const QColor &color)
713 d->setColor(
this, PlatformThemeData::DisabledTextColor, color);
716void PlatformTheme::setCustomBackgroundColor(
const QColor &color)
718 d->setColor(
this, PlatformThemeData::BackgroundColor, color);
721void PlatformTheme::setCustomAlternateBackgroundColor(
const QColor &color)
723 d->setColor(
this, PlatformThemeData::AlternateBackgroundColor, color);
726void PlatformTheme::setCustomHighlightColor(
const QColor &color)
728 d->setColor(
this, PlatformThemeData::HighlightColor, color);
731void PlatformTheme::setCustomHighlightedTextColor(
const QColor &color)
733 d->setColor(
this, PlatformThemeData::HighlightedTextColor, color);
736void PlatformTheme::setCustomActiveTextColor(
const QColor &color)
738 d->setColor(
this, PlatformThemeData::ActiveTextColor, color);
741void PlatformTheme::setCustomActiveBackgroundColor(
const QColor &color)
743 d->setColor(
this, PlatformThemeData::ActiveBackgroundColor, color);
746void PlatformTheme::setCustomLinkColor(
const QColor &color)
748 d->setColor(
this, PlatformThemeData::LinkColor, color);
751void PlatformTheme::setCustomLinkBackgroundColor(
const QColor &color)
753 d->setColor(
this, PlatformThemeData::LinkBackgroundColor, color);
756void PlatformTheme::setCustomVisitedLinkColor(
const QColor &color)
758 d->setColor(
this, PlatformThemeData::TextColor, color);
761void PlatformTheme::setCustomVisitedLinkBackgroundColor(
const QColor &color)
763 d->setColor(
this, PlatformThemeData::VisitedLinkBackgroundColor, color);
766void PlatformTheme::setCustomNegativeTextColor(
const QColor &color)
768 d->setColor(
this, PlatformThemeData::NegativeTextColor, color);
771void PlatformTheme::setCustomNegativeBackgroundColor(
const QColor &color)
773 d->setColor(
this, PlatformThemeData::NegativeBackgroundColor, color);
776void PlatformTheme::setCustomNeutralTextColor(
const QColor &color)
778 d->setColor(
this, PlatformThemeData::NeutralTextColor, color);
781void PlatformTheme::setCustomNeutralBackgroundColor(
const QColor &color)
783 d->setColor(
this, PlatformThemeData::NeutralBackgroundColor, color);
786void PlatformTheme::setCustomPositiveTextColor(
const QColor &color)
788 d->setColor(
this, PlatformThemeData::PositiveTextColor, color);
791void PlatformTheme::setCustomPositiveBackgroundColor(
const QColor &color)
793 d->setColor(
this, PlatformThemeData::PositiveBackgroundColor, color);
796void PlatformTheme::setCustomHoverColor(
const QColor &color)
798 d->setColor(
this, PlatformThemeData::HoverColor, color);
801void PlatformTheme::setCustomFocusColor(
const QColor &color)
803 d->setColor(
this, PlatformThemeData::FocusColor, color);
806bool PlatformTheme::useAlternateBackgroundColor()
const
808 return d->useAlternateBackgroundColor;
811void PlatformTheme::setUseAlternateBackgroundColor(
bool alternate)
813 if (alternate == d->useAlternateBackgroundColor) {
817 d->useAlternateBackgroundColor = alternate;
818 Q_EMIT useAlternateBackgroundColorChanged(alternate);
821QPalette PlatformTheme::palette()
const
827 auto palette = d->data->palette;
829 if (d->localOverrides) {
830 PlatformThemeData::updatePalette(palette, *d->localOverrides);
838 Q_UNUSED(customColor);
843bool PlatformTheme::supportsIconColoring()
const
845 return d->supportsIconColoring;
848void PlatformTheme::setSupportsIconColoring(
bool support)
850 d->supportsIconColoring = support;
853PlatformTheme *PlatformTheme::qmlAttachedProperties(
QObject *
object)
862 auto plugin = PlatformPluginFactory::findPlugin(pluginName);
863 if (!plugin && !pluginName.
isEmpty()) {
864 plugin = PlatformPluginFactory::findPlugin();
868 if (
auto theme = plugin->createPlatformTheme(
object)) {
873 return new BasicTheme(
object);
876void PlatformTheme::emitSignalsForChanges(
int changes)
882 auto propertyChanges = PlatformThemeChangeTracker::PropertyChanges::fromInt(changes);
884 if (propertyChanges & PlatformThemeChangeTracker::PropertyChange::ColorSet) {
885 Q_EMIT colorSetChanged(ColorSet(d->data->colorSet));
888 if (propertyChanges & PlatformThemeChangeTracker::PropertyChange::ColorGroup) {
889 Q_EMIT colorGroupChanged(ColorGroup(d->data->colorGroup));
892 if (propertyChanges & PlatformThemeChangeTracker::PropertyChange::Color) {
893 Q_EMIT colorsChanged();
896 if (propertyChanges & PlatformThemeChangeTracker::PropertyChange::Palette) {
897 Q_EMIT paletteChanged(d->data->palette);
900 if (propertyChanges & PlatformThemeChangeTracker::PropertyChange::Font) {
901 Q_EMIT defaultFontChanged(d->data->defaultFont);
902 Q_EMIT smallFontChanged(d->data->smallFont);
905 if (propertyChanges & PlatformThemeChangeTracker::PropertyChange::Data) {
906 updateChildren(parent());
910bool PlatformTheme::event(
QEvent *event)
912 PlatformThemeChangeTracker tracker(
this);
914 if (
event->type() == PlatformThemeEvents::DataChangedEvent::type) {
915 auto changeEvent =
static_cast<PlatformThemeEvents::DataChangedEvent *
>(
event);
917 if (changeEvent->sender !=
this) {
921 if (changeEvent->oldValue) {
922 changeEvent->oldValue->removeChangeWatcher(
this);
925 if (changeEvent->newValue) {
926 auto data = changeEvent->newValue;
927 data->addChangeWatcher(
this);
930 tracker.markDirty(PlatformThemeChangeTracker::PropertyChange::All);
934 if (
event->type() == PlatformThemeEvents::ColorSetChangedEvent::type) {
935 tracker.markDirty(PlatformThemeChangeTracker::PropertyChange::ColorSet);
939 if (
event->type() == PlatformThemeEvents::ColorGroupChangedEvent::type) {
940 tracker.markDirty(PlatformThemeChangeTracker::PropertyChange::ColorGroup);
944 if (
event->type() == PlatformThemeEvents::ColorChangedEvent::type) {
945 tracker.markDirty(PlatformThemeChangeTracker::PropertyChange::Color | PlatformThemeChangeTracker::PropertyChange::Palette);
949 if (
event->type() == PlatformThemeEvents::FontChangedEvent::type) {
950 tracker.markDirty(PlatformThemeChangeTracker::PropertyChange::Font);
957void PlatformTheme::update()
959 auto oldData = d->data;
961 bool actualInherit = d->inherit;
962 if (
QQuickItem *item = qobject_cast<QQuickItem *>(parent())) {
964 if (colorGroup() != Disabled && !item->isEnabled()) {
965 actualInherit =
false;
972 candidate = determineParent(candidate);
977 auto t =
static_cast<PlatformTheme *
>(qmlAttachedPropertiesObject<PlatformTheme>(candidate,
false));
978 if (t && t->d->data && t->d->data->owner == t) {
979 if (d->data == t->d->data) {
984 d->data = t->d->data;
986 PlatformThemeEvents::DataChangedEvent
event{
this, oldData, t->d->data};
992 }
else if (d->data && d->data->owner !=
this) {
999 d->data = std::make_shared<PlatformThemeData>();
1000 d->data->owner =
this;
1001 d->data->setColorSet(
this,
static_cast<ColorSet
>(d->colorSet));
1002 d->data->setColorGroup(
this,
static_cast<ColorGroup
>(d->colorGroup));
1005 if (d->localOverrides) {
1006 for (
auto entry : *d->localOverrides) {
1007 d->data->setColor(
this, PlatformThemeData::ColorRole(entry.first), entry.second);
1011 PlatformThemeEvents::DataChangedEvent
event{
this, oldData, d->data};
1015void PlatformTheme::updateChildren(
QObject *
object)
1021 const auto children =
object->children();
1022 for (
auto child : children) {
1023 auto t =
static_cast<PlatformTheme *
>(qmlAttachedPropertiesObject<PlatformTheme>(child,
false));
1027 updateChildren(child);
1041 auto item = qobject_cast<QQuickItem *>(
object);
1043 return item->parentItem();
1045 return object->parent();
1049PlatformThemeChangeTracker::PlatformThemeChangeTracker(PlatformTheme *theme, PropertyChanges changes)
1052 auto itr = s_blockedChanges.constFind(theme);
1053 if (itr == s_blockedChanges.constEnd() || (*itr).expired()) {
1054 m_data = std::make_shared<Data>();
1055 s_blockedChanges.insert(theme, m_data);
1057 m_data = (*itr).lock();
1060 m_data->changes |= changes;
1063PlatformThemeChangeTracker::~PlatformThemeChangeTracker() noexcept
1065 std::weak_ptr<Data> dataWatcher = m_data;
1067 auto changes = m_data->changes;
1070 if (dataWatcher.use_count() <= 0) {
1071 m_theme->emitSignalsForChanges(changes);
1072 s_blockedChanges.remove(m_theme);
1076void PlatformThemeChangeTracker::markDirty(PropertyChanges changes)
1078 m_data->changes |= changes;
1083#include "moc_platformtheme.cpp"
1084#include "platformtheme.moc"
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
void update(Part *part, const QByteArray &data, qint64 dataSize)
bool isValid() const const
bool sendEvent(QObject *receiver, QEvent *event)
int registerEventType(int hint)
QIcon fromTheme(const QString &name)
void append(QList< T > &&value)
bool removeOne(const AT &t)
virtual bool event(QEvent *e)
QVariant property(const char *name) const const
QObject * sender() const const
void setColor(ColorGroup group, ColorRole role, const QColor &color)
void setCurrentColorGroup(ColorGroup cg)
void parentChanged(QQuickItem *)
void windowChanged(QQuickWindow *window)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const