8 #include "basictheme_p.h"
10 #include "styleselector_p.h"
12 #include <QGuiApplication>
14 #include "loggingcategory.h"
18 class CompatibilityThemeDefinition :
public BasicThemeDefinition
22 CompatibilityThemeDefinition(
QObject *component,
QObject *parent =
nullptr)
23 : BasicThemeDefinition(parent)
27 connect(m_object, SIGNAL(textColorChanged()),
this, SLOT(syncFromQml()));
28 connect(m_object, SIGNAL(disabledTextColorChanged()),
this, SLOT(syncFromQml()));
29 connect(m_object, SIGNAL(highlightColorChanged()),
this, SLOT(syncFromQml()));
30 connect(m_object, SIGNAL(highlightedTextColorChanged()),
this, SLOT(syncFromQml()));
31 connect(m_object, SIGNAL(backgroundColorChanged()),
this, SLOT(syncFromQml()));
32 connect(m_object, SIGNAL(alternateBackgroundColorChanged()),
this, SLOT(syncFromQml()));
33 connect(m_object, SIGNAL(linkColorChanged()),
this, SLOT(syncFromQml()));
34 connect(m_object, SIGNAL(visitedLinkColorChanged()),
this, SLOT(syncFromQml()));
35 connect(m_object, SIGNAL(buttonTextColorChanged()),
this, SLOT(syncFromQml()));
36 connect(m_object, SIGNAL(buttonBackgroundColorChanged()),
this, SLOT(syncFromQml()));
37 connect(m_object, SIGNAL(buttonAlternateBackgroundColorChanged()),
this, SLOT(syncFromQml()));
38 connect(m_object, SIGNAL(buttonHoverColorChanged()),
this, SLOT(syncFromQml()));
39 connect(m_object, SIGNAL(buttonFocusColorChanged()),
this, SLOT(syncFromQml()));
40 connect(m_object, SIGNAL(viewTextColorChanged()),
this, SLOT(syncFromQml()));
41 connect(m_object, SIGNAL(viewBackgroundColorChanged()),
this, SLOT(syncFromQml()));
42 connect(m_object, SIGNAL(viewAlternateBackgroundColorChanged()),
this, SLOT(syncFromQml()));
43 connect(m_object, SIGNAL(viewHoverColorChanged()),
this, SLOT(syncFromQml()));
44 connect(m_object, SIGNAL(viewFocusColorChanged()),
this, SLOT(syncFromQml()));
45 connect(m_object, SIGNAL(complementaryTextColorChanged()),
this, SLOT(syncFromQml()));
46 connect(m_object, SIGNAL(complementaryBackgroundColorChanged()),
this, SLOT(syncFromQml()));
47 connect(m_object, SIGNAL(complementaryAlternateBackgroundColorChanged()),
this, SLOT(syncFromQml()));
48 connect(m_object, SIGNAL(complementaryHoverColorChanged()),
this, SLOT(syncFromQml()));
49 connect(m_object, SIGNAL(complementaryFocusColorChanged()),
this, SLOT(syncFromQml()));
52 void syncToQml(PlatformTheme *
object)
override
54 BasicThemeDefinition::syncToQml(
object);
58 "__propagateTextColor",
60 Q_ARG(
QVariant, object->textColor()));
62 "__propagateBackgroundColor",
64 Q_ARG(
QVariant, object->backgroundColor()));
66 "__propagatePrimaryColor",
68 Q_ARG(
QVariant, object->highlightColor()));
70 "__propagateAccentColor",
72 Q_ARG(
QVariant, object->highlightColor()));
75 Q_SLOT
void syncFromQml()
77 textColor = m_object->property(
"textColor").value<
QColor>();
78 disabledTextColor = m_object->property(
"disabledTextColor").
value<
QColor>();
79 highlightColor = m_object->property(
"highlightColor").
value<
QColor>();
80 highlightedTextColor = m_object->property(
"highlightedTextColor").
value<
QColor>();
81 backgroundColor = m_object->property(
"backgroundColor").
value<
QColor>();
82 alternateBackgroundColor = m_object->property(
"alternateBackgroundColor").
value<
QColor>();
83 linkColor = m_object->property(
"linkColor").
value<
QColor>();
84 visitedLinkColor = m_object->property(
"visitedLinkColor").
value<
QColor>();
85 buttonTextColor = m_object->property(
"buttonTextColor").
value<
QColor>();
86 buttonBackgroundColor = m_object->property(
"buttonBackgroundColor").
value<
QColor>();
87 buttonAlternateBackgroundColor = m_object->property(
"buttonAlternateBackgroundColor").
value<
QColor>();
88 buttonHoverColor = m_object->property(
"buttonHoverColor").
value<
QColor>();
89 buttonFocusColor = m_object->property(
"buttonFocusColor").
value<
QColor>();
90 viewTextColor = m_object->property(
"viewTextColor").
value<
QColor>();
91 viewBackgroundColor = m_object->property(
"viewBackgroundColor").
value<
QColor>();
92 viewAlternateBackgroundColor = m_object->property(
"viewAlternateBackgroundColor").
value<
QColor>();
93 viewHoverColor = m_object->property(
"viewHoverColor").
value<
QColor>();
94 viewFocusColor = m_object->property(
"viewFocusColor").
value<
QColor>();
95 complementaryTextColor = m_object->property(
"complementaryTextColor").
value<
QColor>();
96 complementaryBackgroundColor = m_object->property(
"complementaryBackgroundColor").
value<
QColor>();
97 complementaryAlternateBackgroundColor = m_object->property(
"complementaryAlternateBackgroundColor").
value<
QColor>();
98 complementaryHoverColor = m_object->property(
"complementaryHoverColor").
value<
QColor>();
99 complementaryFocusColor = m_object->property(
"complementaryFocusColor").
value<
QColor>();
108 BasicThemeDefinition::BasicThemeDefinition(
QObject *parent)
111 defaultFont = qGuiApp->font();
113 smallFont = qGuiApp->font();
114 smallFont.setPointSize(smallFont.pointSize() - 2);
117 void BasicThemeDefinition::syncToQml(PlatformTheme *
object)
119 auto item = qobject_cast<QQuickItem *>(object->parent());
120 if (item && qmlAttachedPropertiesObject<PlatformTheme>(item,
false) ==
object) {
125 BasicThemeInstance::BasicThemeInstance(
QObject *parent)
130 BasicThemeDefinition &BasicThemeInstance::themeDefinition(
QQmlEngine *engine)
132 if (m_themeDefinition) {
133 return *m_themeDefinition;
136 auto componentUrl = StyleSelector::componentUrl(QStringLiteral(
"Theme.qml"));
137 QString path{componentUrl.toLocalFile()};
138 if (path.isEmpty() && componentUrl.scheme() ==
QLatin1String(
"qrc")) {
141 QFile themeFile{path};
143 auto data = themeFile.
readAll();
153 data.
replace(
"\npragma Singleton\n",
"");
156 component.setData(data, componentUrl);
157 auto result = component.create();
160 const auto errors = component.errors();
161 for (
auto error : errors) {
162 qCWarning(KirigamiLog) <<
error.toString();
165 qCWarning(KirigamiLog) <<
"Invalid Theme file, using default Basic theme.";
166 m_themeDefinition = std::make_unique<BasicThemeDefinition>();
167 }
else if (qobject_cast<BasicThemeDefinition *>(result)) {
168 m_themeDefinition.reset(qobject_cast<BasicThemeDefinition *>(result));
170 qCWarning(KirigamiLog) <<
"Warning: Theme implementations should use Kirigami.BasicThemeDefinition for its root item";
171 m_themeDefinition = std::make_unique<CompatibilityThemeDefinition>(result);
174 qCDebug(KirigamiLog) <<
"No Theme file found, using default Basic theme";
175 m_themeDefinition = std::make_unique<BasicThemeDefinition>();
178 connect(m_themeDefinition.get(), &BasicThemeDefinition::changed,
this, &BasicThemeInstance::onDefinitionChanged);
180 return *m_themeDefinition;
183 void BasicThemeInstance::onDefinitionChanged()
185 for (
auto watcher : std::as_const(watchers)) {
192 BasicTheme::BasicTheme(
QObject *parent)
193 : PlatformTheme(parent)
195 basicThemeInstance()->watchers.append(
this);
200 BasicTheme::~BasicTheme()
202 basicThemeInstance()->watchers.removeOne(
this);
205 void BasicTheme::sync()
207 auto &definition = basicThemeInstance()->themeDefinition(qmlEngine(parent()));
209 switch (colorSet()) {
210 case BasicTheme::Button:
211 setTextColor(
tint(definition.buttonTextColor));
212 setBackgroundColor(
tint(definition.buttonBackgroundColor));
213 setAlternateBackgroundColor(
tint(definition.buttonAlternateBackgroundColor));
214 setHoverColor(
tint(definition.buttonHoverColor));
215 setFocusColor(
tint(definition.buttonFocusColor));
217 case BasicTheme::View:
218 setTextColor(
tint(definition.viewTextColor));
219 setBackgroundColor(
tint(definition.viewBackgroundColor));
220 setAlternateBackgroundColor(
tint(definition.viewAlternateBackgroundColor));
221 setHoverColor(
tint(definition.viewHoverColor));
222 setFocusColor(
tint(definition.viewFocusColor));
224 case BasicTheme::Selection:
225 setTextColor(
tint(definition.selectionTextColor));
226 setBackgroundColor(
tint(definition.selectionBackgroundColor));
227 setAlternateBackgroundColor(
tint(definition.selectionAlternateBackgroundColor));
228 setHoverColor(
tint(definition.selectionHoverColor));
229 setFocusColor(
tint(definition.selectionFocusColor));
231 case BasicTheme::Tooltip:
232 setTextColor(
tint(definition.tooltipTextColor));
233 setBackgroundColor(
tint(definition.tooltipBackgroundColor));
234 setAlternateBackgroundColor(
tint(definition.tooltipAlternateBackgroundColor));
235 setHoverColor(
tint(definition.tooltipHoverColor));
236 setFocusColor(
tint(definition.tooltipFocusColor));
238 case BasicTheme::Complementary:
239 setTextColor(
tint(definition.complementaryTextColor));
240 setBackgroundColor(
tint(definition.complementaryBackgroundColor));
241 setAlternateBackgroundColor(
tint(definition.complementaryAlternateBackgroundColor));
242 setHoverColor(
tint(definition.complementaryHoverColor));
243 setFocusColor(
tint(definition.complementaryFocusColor));
245 case BasicTheme::Window:
247 setTextColor(
tint(definition.textColor));
248 setBackgroundColor(
tint(definition.backgroundColor));
249 setAlternateBackgroundColor(
tint(definition.alternateBackgroundColor));
250 setHoverColor(
tint(definition.hoverColor));
251 setFocusColor(
tint(definition.focusColor));
255 setDisabledTextColor(
tint(definition.disabledTextColor));
256 setHighlightColor(
tint(definition.highlightColor));
257 setHighlightedTextColor(
tint(definition.highlightedTextColor));
258 setActiveTextColor(
tint(definition.activeTextColor));
259 setActiveBackgroundColor(
tint(definition.activeBackgroundColor));
260 setLinkColor(
tint(definition.linkColor));
261 setLinkBackgroundColor(
tint(definition.linkBackgroundColor));
262 setVisitedLinkColor(
tint(definition.visitedLinkColor));
263 setVisitedLinkBackgroundColor(
tint(definition.visitedLinkBackgroundColor));
264 setNegativeTextColor(
tint(definition.negativeTextColor));
265 setNegativeBackgroundColor(
tint(definition.negativeBackgroundColor));
266 setNeutralTextColor(
tint(definition.neutralTextColor));
267 setNeutralBackgroundColor(
tint(definition.neutralBackgroundColor));
268 setPositiveTextColor(
tint(definition.positiveTextColor));
269 setPositiveBackgroundColor(
tint(definition.positiveBackgroundColor));
271 setDefaultFont(definition.defaultFont);
272 setSmallFont(definition.smallFont);
275 bool BasicTheme::event(
QEvent *event)
277 if (
event->type() == PlatformThemeEvents::DataChangedEvent::type) {
281 if (
event->type() == PlatformThemeEvents::ColorSetChangedEvent::type) {
285 if (
event->type() == PlatformThemeEvents::ColorGroupChangedEvent::type) {
289 if (
event->type() == PlatformThemeEvents::ColorChangedEvent::type) {
290 basicThemeInstance()->themeDefinition(qmlEngine(parent())).syncToQml(
this);
293 if (
event->type() == PlatformThemeEvents::FontChangedEvent::type) {
294 basicThemeInstance()->themeDefinition(qmlEngine(parent())).syncToQml(
this);
297 return PlatformTheme::event(event);
302 switch (colorGroup()) {
303 case PlatformTheme::Inactive:
305 case PlatformTheme::Disabled:
314 #include "basictheme.moc"