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:
242 case HighlightedTextColor:
248 case VisitedLinkColor:
258class PlatformThemePrivate
261 PlatformThemePrivate()
263 , supportsIconColoring(false)
264 , pendingColorChange(false)
265 , pendingChildUpdate(false)
266 , useAlternateBackgroundColor(false)
267 , colorSet(PlatformTheme::
Window)
268 , colorGroup(PlatformTheme::Active)
272 inline QColor color(
const PlatformTheme *theme, PlatformThemeData::ColorRole color)
const
278 QColor value = data->colors.at(color);
280 if (data->owner != theme && localOverrides) {
281 auto itr = localOverrides->find(color);
282 if (itr != localOverrides->end()) {
290 inline void setColor(PlatformTheme *theme, PlatformThemeData::ColorRole color,
const QColor &value)
292 if (!localOverrides) {
293 localOverrides = std::make_unique<PlatformThemeData::ColorMap>();
298 auto itr = localOverrides->find(color);
299 if (itr != localOverrides->end()) {
300 localOverrides->erase(itr);
310 emitCompressedColorChanged(theme);
316 auto itr = localOverrides->find(color);
317 if (itr != localOverrides->end() && itr->second == value && (data && data->owner != theme)) {
321 (*localOverrides)[color] = value;
324 data->setColor(theme, color, value);
327 emitCompressedColorChanged(theme);
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()) {
344 data->setColor(theme, color, value);
348 inline void emitCompressedColorChanged(PlatformTheme *theme)
350 if (pendingColorChange) {
354 pendingColorChange =
true;
358 inline void queueChildUpdate(PlatformTheme *theme)
360 if (pendingChildUpdate) {
364 pendingChildUpdate =
true;
368 pendingChildUpdate =
false;
369 theme->updateChildren(theme->parent());
384 std::shared_ptr<PlatformThemeData> data;
387 std::unique_ptr<PlatformThemeData::ColorMap> localOverrides;
390 bool supportsIconColoring : 1;
391 bool pendingColorChange : 1;
392 bool pendingChildUpdate : 1;
393 bool useAlternateBackgroundColor : 1;
399 uint8_t colorSet : 4;
400 uint8_t colorGroup : 4;
404 static_assert(PlatformTheme::ColorGroupCount <= 16,
"PlatformTheme::ColorGroup contains more elements than can be stored in PlatformThemePrivate");
405 static_assert(
PlatformTheme::ColorSetCount <= 16,
"PlatformTheme::ColorSet contains more elements than can be stored in PlatformThemePrivate");
407 inline static PlatformPluginFactory *s_pluginFactory =
nullptr;
410PlatformTheme::PlatformTheme(
QObject *parent)
412 , d(new PlatformThemePrivate)
414 if (
QQuickItem *item = qobject_cast<QQuickItem *>(parent)) {
422PlatformTheme::~PlatformTheme()
425 d->data->removeChangeWatcher(
this);
431void PlatformTheme::setColorSet(PlatformTheme::ColorSet colorSet)
433 d->colorSet = colorSet;
436 d->data->setColorSet(
this, colorSet);
440PlatformTheme::ColorSet PlatformTheme::colorSet()
const
442 return d->data ? d->data->colorSet :
Window;
445void PlatformTheme::setColorGroup(PlatformTheme::ColorGroup colorGroup)
447 d->colorGroup = colorGroup;
450 d->data->setColorGroup(
this, colorGroup);
454PlatformTheme::ColorGroup PlatformTheme::colorGroup()
const
456 return d->data ? d->data->colorGroup : Active;
459bool PlatformTheme::inherit()
const
464void PlatformTheme::setInherit(
bool inherit)
466 if (inherit == d->inherit) {
470 d->inherit = inherit;
473 Q_EMIT inheritChanged(inherit);
476QColor PlatformTheme::textColor()
const
478 return d->color(
this, PlatformThemeData::TextColor);
481QColor PlatformTheme::disabledTextColor()
const
483 return d->color(
this, PlatformThemeData::DisabledTextColor);
486QColor PlatformTheme::highlightColor()
const
488 return d->color(
this, PlatformThemeData::HighlightColor);
491QColor PlatformTheme::highlightedTextColor()
const
493 return d->color(
this, PlatformThemeData::HighlightedTextColor);
496QColor PlatformTheme::backgroundColor()
const
498 return d->color(
this, PlatformThemeData::BackgroundColor);
501QColor PlatformTheme::alternateBackgroundColor()
const
503 return d->color(
this, PlatformThemeData::AlternateBackgroundColor);
506QColor PlatformTheme::activeTextColor()
const
508 return d->color(
this, PlatformThemeData::ActiveTextColor);
511QColor PlatformTheme::activeBackgroundColor()
const
513 return d->color(
this, PlatformThemeData::ActiveBackgroundColor);
516QColor PlatformTheme::linkColor()
const
518 return d->color(
this, PlatformThemeData::LinkColor);
521QColor PlatformTheme::linkBackgroundColor()
const
523 return d->color(
this, PlatformThemeData::LinkBackgroundColor);
526QColor PlatformTheme::visitedLinkColor()
const
528 return d->color(
this, PlatformThemeData::VisitedLinkColor);
531QColor PlatformTheme::visitedLinkBackgroundColor()
const
533 return d->color(
this, PlatformThemeData::VisitedLinkBackgroundColor);
536QColor PlatformTheme::negativeTextColor()
const
538 return d->color(
this, PlatformThemeData::NegativeTextColor);
541QColor PlatformTheme::negativeBackgroundColor()
const
543 return d->color(
this, PlatformThemeData::NegativeBackgroundColor);
546QColor PlatformTheme::neutralTextColor()
const
548 return d->color(
this, PlatformThemeData::NeutralTextColor);
551QColor PlatformTheme::neutralBackgroundColor()
const
553 return d->color(
this, PlatformThemeData::NeutralBackgroundColor);
556QColor PlatformTheme::positiveTextColor()
const
558 return d->color(
this, PlatformThemeData::PositiveTextColor);
561QColor PlatformTheme::positiveBackgroundColor()
const
563 return d->color(
this, PlatformThemeData::PositiveBackgroundColor);
566QColor PlatformTheme::focusColor()
const
568 return d->color(
this, PlatformThemeData::FocusColor);
571QColor PlatformTheme::hoverColor()
const
573 return d->color(
this, PlatformThemeData::HoverColor);
577void PlatformTheme::setTextColor(
const QColor &color)
579 d->setDataColor(
this, PlatformThemeData::TextColor, color);
582void PlatformTheme::setDisabledTextColor(
const QColor &color)
584 d->setDataColor(
this, PlatformThemeData::DisabledTextColor, color);
587void PlatformTheme::setBackgroundColor(
const QColor &color)
589 d->setDataColor(
this, PlatformThemeData::BackgroundColor, color);
592void PlatformTheme::setAlternateBackgroundColor(
const QColor &color)
594 d->setDataColor(
this, PlatformThemeData::AlternateBackgroundColor, color);
597void PlatformTheme::setHighlightColor(
const QColor &color)
599 d->setDataColor(
this, PlatformThemeData::HighlightColor, color);
602void PlatformTheme::setHighlightedTextColor(
const QColor &color)
604 d->setDataColor(
this, PlatformThemeData::HighlightedTextColor, color);
607void PlatformTheme::setActiveTextColor(
const QColor &color)
609 d->setDataColor(
this, PlatformThemeData::ActiveTextColor, color);
612void PlatformTheme::setActiveBackgroundColor(
const QColor &color)
614 d->setDataColor(
this, PlatformThemeData::ActiveBackgroundColor, color);
617void PlatformTheme::setLinkColor(
const QColor &color)
619 d->setDataColor(
this, PlatformThemeData::LinkColor, color);
622void PlatformTheme::setLinkBackgroundColor(
const QColor &color)
624 d->setDataColor(
this, PlatformThemeData::LinkBackgroundColor, color);
627void PlatformTheme::setVisitedLinkColor(
const QColor &color)
629 d->setDataColor(
this, PlatformThemeData::VisitedLinkColor, color);
632void PlatformTheme::setVisitedLinkBackgroundColor(
const QColor &color)
634 d->setDataColor(
this, PlatformThemeData::VisitedLinkBackgroundColor, color);
637void PlatformTheme::setNegativeTextColor(
const QColor &color)
639 d->setDataColor(
this, PlatformThemeData::NegativeTextColor, color);
642void PlatformTheme::setNegativeBackgroundColor(
const QColor &color)
644 d->setDataColor(
this, PlatformThemeData::NegativeBackgroundColor, color);
647void PlatformTheme::setNeutralTextColor(
const QColor &color)
649 d->setDataColor(
this, PlatformThemeData::NeutralTextColor, color);
652void PlatformTheme::setNeutralBackgroundColor(
const QColor &color)
654 d->setDataColor(
this, PlatformThemeData::NeutralBackgroundColor, color);
657void PlatformTheme::setPositiveTextColor(
const QColor &color)
659 d->setDataColor(
this, PlatformThemeData::PositiveTextColor, color);
662void PlatformTheme::setPositiveBackgroundColor(
const QColor &color)
664 d->setDataColor(
this, PlatformThemeData::PositiveBackgroundColor, color);
667void PlatformTheme::setHoverColor(
const QColor &color)
669 d->setDataColor(
this, PlatformThemeData::HoverColor, color);
672void PlatformTheme::setFocusColor(
const QColor &color)
674 d->setDataColor(
this, PlatformThemeData::FocusColor, color);
677QFont PlatformTheme::defaultFont()
const
679 return d->data ? d->data->defaultFont :
QFont{};
682void PlatformTheme::setDefaultFont(
const QFont &font)
685 d->data->setDefaultFont(
this, font);
689QFont PlatformTheme::smallFont()
const
691 return d->data ? d->data->smallFont :
QFont{};
694void PlatformTheme::setSmallFont(
const QFont &font)
697 d->data->setSmallFont(
this, font);
701qreal PlatformTheme::frameContrast()
const
709qreal PlatformTheme::lightFrameContrast()
const
713 return frameContrast() / 2.0;
717void PlatformTheme::setCustomTextColor(
const QColor &color)
719 d->setColor(
this, PlatformThemeData::TextColor, color);
722void PlatformTheme::setCustomDisabledTextColor(
const QColor &color)
724 d->setColor(
this, PlatformThemeData::DisabledTextColor, color);
727void PlatformTheme::setCustomBackgroundColor(
const QColor &color)
729 d->setColor(
this, PlatformThemeData::BackgroundColor, color);
732void PlatformTheme::setCustomAlternateBackgroundColor(
const QColor &color)
734 d->setColor(
this, PlatformThemeData::AlternateBackgroundColor, color);
737void PlatformTheme::setCustomHighlightColor(
const QColor &color)
739 d->setColor(
this, PlatformThemeData::HighlightColor, color);
742void PlatformTheme::setCustomHighlightedTextColor(
const QColor &color)
744 d->setColor(
this, PlatformThemeData::HighlightedTextColor, color);
747void PlatformTheme::setCustomActiveTextColor(
const QColor &color)
749 d->setColor(
this, PlatformThemeData::ActiveTextColor, color);
752void PlatformTheme::setCustomActiveBackgroundColor(
const QColor &color)
754 d->setColor(
this, PlatformThemeData::ActiveBackgroundColor, color);
757void PlatformTheme::setCustomLinkColor(
const QColor &color)
759 d->setColor(
this, PlatformThemeData::LinkColor, color);
762void PlatformTheme::setCustomLinkBackgroundColor(
const QColor &color)
764 d->setColor(
this, PlatformThemeData::LinkBackgroundColor, color);
767void PlatformTheme::setCustomVisitedLinkColor(
const QColor &color)
769 d->setColor(
this, PlatformThemeData::TextColor, color);
772void PlatformTheme::setCustomVisitedLinkBackgroundColor(
const QColor &color)
774 d->setColor(
this, PlatformThemeData::VisitedLinkBackgroundColor, color);
777void PlatformTheme::setCustomNegativeTextColor(
const QColor &color)
779 d->setColor(
this, PlatformThemeData::NegativeTextColor, color);
782void PlatformTheme::setCustomNegativeBackgroundColor(
const QColor &color)
784 d->setColor(
this, PlatformThemeData::NegativeBackgroundColor, color);
787void PlatformTheme::setCustomNeutralTextColor(
const QColor &color)
789 d->setColor(
this, PlatformThemeData::NeutralTextColor, color);
792void PlatformTheme::setCustomNeutralBackgroundColor(
const QColor &color)
794 d->setColor(
this, PlatformThemeData::NeutralBackgroundColor, color);
797void PlatformTheme::setCustomPositiveTextColor(
const QColor &color)
799 d->setColor(
this, PlatformThemeData::PositiveTextColor, color);
802void PlatformTheme::setCustomPositiveBackgroundColor(
const QColor &color)
804 d->setColor(
this, PlatformThemeData::PositiveBackgroundColor, color);
807void PlatformTheme::setCustomHoverColor(
const QColor &color)
809 d->setColor(
this, PlatformThemeData::HoverColor, color);
812void PlatformTheme::setCustomFocusColor(
const QColor &color)
814 d->setColor(
this, PlatformThemeData::FocusColor, color);
817bool PlatformTheme::useAlternateBackgroundColor()
const
819 return d->useAlternateBackgroundColor;
822void PlatformTheme::setUseAlternateBackgroundColor(
bool alternate)
824 if (alternate == d->useAlternateBackgroundColor) {
828 d->useAlternateBackgroundColor = alternate;
829 Q_EMIT useAlternateBackgroundColorChanged(alternate);
832QPalette PlatformTheme::palette()
const
838 auto palette = d->data->palette;
840 if (d->localOverrides) {
841 PlatformThemeData::updatePalette(palette, *d->localOverrides);
849 Q_UNUSED(customColor);
854bool PlatformTheme::supportsIconColoring()
const
856 return d->supportsIconColoring;
859void PlatformTheme::setSupportsIconColoring(
bool support)
861 d->supportsIconColoring = support;
864PlatformTheme *PlatformTheme::qmlAttachedProperties(
QObject *
object)
873 auto plugin = PlatformPluginFactory::findPlugin(pluginName);
874 if (!plugin && !pluginName.
isEmpty()) {
875 plugin = PlatformPluginFactory::findPlugin();
879 if (
auto theme = plugin->createPlatformTheme(
object)) {
884 return new BasicTheme(
object);
887bool PlatformTheme::event(
QEvent *event)
889 if (
event->type() == PlatformThemeEvents::DataChangedEvent::type) {
890 auto changeEvent =
static_cast<PlatformThemeEvents::DataChangedEvent *
>(
event);
892 if (changeEvent->sender !=
this) {
896 if (changeEvent->oldValue) {
897 changeEvent->oldValue->removeChangeWatcher(
this);
900 if (changeEvent->newValue) {
901 auto data = changeEvent->newValue;
902 data->addChangeWatcher(
this);
904 Q_EMIT colorSetChanged(data->colorSet);
905 Q_EMIT colorGroupChanged(data->colorGroup);
906 Q_EMIT defaultFontChanged(data->defaultFont);
907 Q_EMIT smallFontChanged(data->smallFont);
908 d->emitCompressedColorChanged(
this);
914 if (
event->type() == PlatformThemeEvents::ColorSetChangedEvent::type) {
916 Q_EMIT colorSetChanged(d->data->colorSet);
921 if (
event->type() == PlatformThemeEvents::ColorGroupChangedEvent::type) {
923 Q_EMIT colorGroupChanged(d->data->colorGroup);
928 if (
event->type() == PlatformThemeEvents::ColorChangedEvent::type) {
929 d->emitCompressedColorChanged(
this);
933 if (
event->type() == PlatformThemeEvents::FontChangedEvent::type) {
935 Q_EMIT defaultFontChanged(d->data->defaultFont);
936 Q_EMIT smallFontChanged(d->data->smallFont);
944void PlatformTheme::update()
946 d->queueChildUpdate(
this);
948 auto oldData = d->data;
953 candidate = determineParent(candidate);
958 auto t =
static_cast<PlatformTheme *
>(qmlAttachedPropertiesObject<PlatformTheme>(candidate,
false));
959 if (t && t->d->data && t->d->data->owner == t) {
960 if (d->data == t->d->data) {
965 d->data = t->d->data;
967 PlatformThemeEvents::DataChangedEvent
event{
this, oldData, t->d->data};
973 }
else if (d->data->owner !=
this) {
980 d->data = std::make_shared<PlatformThemeData>();
981 d->data->owner =
this;
982 d->data->setColorSet(
this,
static_cast<ColorSet
>(d->colorSet));
983 d->data->setColorGroup(
this,
static_cast<ColorGroup
>(d->colorGroup));
986 if (d->localOverrides) {
987 for (
auto entry : *d->localOverrides) {
988 d->data->setColor(
this, PlatformThemeData::ColorRole(entry.first), entry.second);
992 PlatformThemeEvents::DataChangedEvent
event{
this, oldData, d->data};
996void PlatformTheme::updateChildren(
QObject *
object)
1002 const auto children =
object->children();
1003 for (
auto child : children) {
1004 auto t =
static_cast<PlatformTheme *
>(qmlAttachedPropertiesObject<PlatformTheme>(child,
false));
1008 updateChildren(child);
1013void PlatformTheme::emitColorChanged()
1016 Q_EMIT paletteChanged(d->data->palette);
1019 Q_EMIT colorsChanged();
1020 d->pendingColorChange =
false;
1032 auto item = qobject_cast<QQuickItem *>(
object);
1034 return item->parentItem();
1036 return object->parent();
1043#include "moc_platformtheme.cpp"
1044#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