26 | 26 |
|
27 | 27 |
Q_GLOBAL_STATIC(DThemeManagerPrivate, DThemeManagerStatic)
|
28 | 28 |
|
29 | |
DThemeManager * DThemeManager::instance()
|
|
29 |
DThemeManager *DThemeManager::instance()
|
30 | 30 |
{
|
31 | 31 |
return DThemeManagerStatic;
|
32 | 32 |
}
|
|
60 | 60 |
m_theme = "dark";
|
61 | 61 |
}
|
62 | 62 |
|
63 | |
if (style)
|
|
63 |
if (style) {
|
64 | 64 |
qApp->setStyle(style);
|
|
65 |
}
|
65 | 66 |
|
66 | 67 |
Q_EMIT themeChanged(m_theme);
|
67 | 68 |
}
|
|
99 | 100 |
widget->setProperty("_d_dtk_theme", "dark");
|
100 | 101 |
}
|
101 | 102 |
|
102 | |
if (style)
|
|
103 |
if (style) {
|
103 | 104 |
widget->setStyle(style);
|
|
105 |
}
|
104 | 106 |
|
105 | 107 |
widget->setStyleSheet(getQssForWidget(getObjectClassName(widget), theme));
|
106 | 108 |
}
|
|
110 | 112 |
QString qss;
|
111 | 113 |
|
112 | 114 |
QString themeName = theme.isEmpty() ? m_theme : theme;
|
113 | |
QFile themeFile(QString(":/%1/%2.theme").arg(themeName).arg(className));
|
|
115 |
QString themeURL = QString(":/%1/%2.theme").arg(themeName).arg(className);
|
|
116 |
QFile themeFile(themeURL);
|
114 | 117 |
|
115 | 118 |
if (themeFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
116 | 119 |
qss = themeFile.readAll();
|
117 | |
|
118 | 120 |
themeFile.close();
|
|
121 |
} else {
|
|
122 |
qWarning() << "open qss file failed" << themeURL;
|
119 | 123 |
}
|
120 | 124 |
|
121 | 125 |
return qss;
|
|
129 | 133 |
|
130 | 134 |
do {
|
131 | 135 |
theme = widget->property("_d_dtk_theme").toString();
|
132 | |
if (!theme.isEmpty()) break;
|
|
136 |
if (!theme.isEmpty()) { break; }
|
133 | 137 |
widget = widget->parentWidget();
|
134 | |
} while(widget);
|
|
138 |
} while (widget);
|
135 | 139 |
|
136 | 140 |
return getQssForWidget(className, theme);
|
137 | 141 |
}
|
|
141 | 145 |
return getQssForWidget(getObjectClassName(widget), widget);
|
142 | 146 |
}
|
143 | 147 |
|
|
148 |
/*!
|
|
149 |
\since 2.0
|
|
150 |
\relates DThemeManager
|
|
151 |
Find QSS file on the qrc by getQssForWidget, className is form metaObject
|
|
152 |
*/
|
|
153 |
void DThemeManager::registerWidget(QWidget *widget, QStringList propertys)
|
|
154 |
{
|
|
155 |
auto meta = widget->metaObject();
|
|
156 |
Q_ASSERT(meta);
|
|
157 |
|
|
158 |
auto className = meta->className();
|
|
159 |
auto dtm = DThemeManager::instance();
|
|
160 |
auto qss = dtm->getQssForWidget(className, widget);
|
|
161 |
widget->setStyleSheet(widget->styleSheet() + qss);
|
|
162 |
|
|
163 |
connect(dtm, &DThemeManager::themeChanged,
|
|
164 |
widget, [widget, dtm, className](QString) {
|
|
165 |
widget->setStyleSheet(dtm->getQssForWidget(className, widget));
|
|
166 |
});
|
|
167 |
|
|
168 |
for (auto property : propertys) {
|
|
169 |
auto propertyIndex = meta->indexOfProperty(property.toLatin1().data());
|
|
170 |
if (-1 == propertyIndex) {
|
|
171 |
continue;
|
|
172 |
}
|
|
173 |
|
|
174 |
connect(widget, meta->property(propertyIndex).notifySignal(),
|
|
175 |
dtm, dtm->metaObject()->method(dtm->metaObject()->indexOfMethod("updateQss()")));
|
|
176 |
}
|
|
177 |
}
|
|
178 |
|
144 | 179 |
void DThemeManager::updateQss()
|
145 | 180 |
{
|
146 | |
QWidget *w = qobject_cast<QWidget*>(sender());
|
147 | |
if(w){
|
|
181 |
QWidget *w = qobject_cast<QWidget *>(sender());
|
|
182 |
if (w) {
|
148 | 183 |
w->setStyleSheet(w->styleSheet());
|
149 | 184 |
}
|
150 | 185 |
}
|