1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "mystyle.h"
// If no value was inherited from a parent or explicitly set, the "global" values are used.
static MyStyle::Theme globalTheme = MyStyle::Light;
MyStyle::MyStyle(QObject *parent)
: QQuickAttachedPropertyPropagator(parent)
, m_theme(globalTheme)
{
// A static function could be called here that reads globalTheme from a
// settings file once at startup. That value would override the global
// value. This is similar to what the Imagine and Material styles do, for
// example.
initialize();
}
MyStyle *MyStyle::qmlAttachedProperties(QObject *object)
{
return new MyStyle(object);
}
MyStyle::Theme MyStyle::theme() const
{
return m_theme;
}
void MyStyle::setTheme(Theme theme)
{
// When this function is called, we know that the user has explicitly
// set a theme on this attached object. We set this to true even if
// the effective theme didn't change, because it's important that
// the user's specified value is respected (and not inherited from
// from the parent).
m_explicitTheme = true;
if (m_theme == theme)
return;
m_theme = theme;
propagateTheme();
themeChange();
}
void MyStyle::inheritTheme(Theme theme)
{
if (m_explicitTheme || m_theme == theme)
return;
m_theme = theme;
propagateTheme();
themeChange();
}
void MyStyle::propagateTheme()
{
const auto styles = attachedChildren();
for (QQuickAttachedPropertyPropagator *child : styles) {
MyStyle *myStyle = qobject_cast<MyStyle *>(child);
if (myStyle)
myStyle->inheritTheme(m_theme);
}
}
void MyStyle::resetTheme()
{
if (!m_explicitTheme)
return;
m_explicitTheme = false;
MyStyle *myStyle = qobject_cast<MyStyle *>(attachedParent());
inheritTheme(myStyle ? myStyle->theme() : globalTheme);
}
void MyStyle::themeChange()
{
emit themeChanged();
// Emit any other change signals for properties that depend on the theme here...
}
QColor MyStyle::windowColor() const
{
return m_theme == Light ? QColor::fromRgb(0xf0f0f0) : QColor::fromRgb(0x303030);
}
QColor MyStyle::windowTextColor() const
{
return m_theme == Light ? QColor::fromRgb(0x5c5c5c) : QColor::fromRgb(0xe0e0e0);
}
QColor MyStyle::buttonColor() const
{
return m_theme == Light ? QColor::fromRgb(0xc2e1ff) : QColor::fromRgb(0x74bbff);
}
QColor MyStyle::buttonTextColor() const
{
return m_theme == Light ? QColor::fromRgb(0x5c5c5c) : QColor::fromRgb(0xffffff);
}
QColor MyStyle::toolBarColor() const
{
return m_theme == Light ? QColor::fromRgb(0x4da6ff) : QColor::fromRgb(0x0066cc);
}
QColor MyStyle::popupColor() const
{
return windowColor().lighter(120);
}
QColor MyStyle::popupBorderColor() const
{
const QColor winColor = windowColor();
return m_theme == Light ? winColor.darker(140) : winColor.lighter(140);
}
QColor MyStyle::backgroundDimColor() const
{
const QColor winColor = windowColor().darker();
return QColor::fromRgb(winColor.red(), winColor.green(), winColor.blue(), 100);
}
void MyStyle::attachedParentChange(QQuickAttachedPropertyPropagator *newParent, QQuickAttachedPropertyPropagator *oldParent)
{
Q_UNUSED(oldParent);
MyStyle *attachedParentStyle = qobject_cast<MyStyle *>(newParent);
if (attachedParentStyle) {
inheritTheme(attachedParentStyle->theme());
// Do any other inheriting here...
}
}
|