Codebase list dtkwidget / upstream/2.0.0
Add DThemeManager::registerWidget Change-Id: I3e8093fb13c25e4d7f8290b22a044246a8cc610b Iceyer 6 years ago
6 changed file(s) with 53 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
0 #include "darrowbutton.h"
0 #include "dthememanager.h"
2626
2727 Q_GLOBAL_STATIC(DThemeManagerPrivate, DThemeManagerStatic)
2828
29 DThemeManager * DThemeManager::instance()
29 DThemeManager *DThemeManager::instance()
3030 {
3131 return DThemeManagerStatic;
3232 }
6060 m_theme = "dark";
6161 }
6262
63 if (style)
63 if (style) {
6464 qApp->setStyle(style);
65 }
6566
6667 Q_EMIT themeChanged(m_theme);
6768 }
99100 widget->setProperty("_d_dtk_theme", "dark");
100101 }
101102
102 if (style)
103 if (style) {
103104 widget->setStyle(style);
105 }
104106
105107 widget->setStyleSheet(getQssForWidget(getObjectClassName(widget), theme));
106108 }
110112 QString qss;
111113
112114 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);
114117
115118 if (themeFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
116119 qss = themeFile.readAll();
117
118120 themeFile.close();
121 } else {
122 qWarning() << "open qss file failed" << themeURL;
119123 }
120124
121125 return qss;
129133
130134 do {
131135 theme = widget->property("_d_dtk_theme").toString();
132 if (!theme.isEmpty()) break;
136 if (!theme.isEmpty()) { break; }
133137 widget = widget->parentWidget();
134 } while(widget);
138 } while (widget);
135139
136140 return getQssForWidget(className, theme);
137141 }
141145 return getQssForWidget(getObjectClassName(widget), widget);
142146 }
143147
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
144179 void DThemeManager::updateQss()
145180 {
146 QWidget *w = qobject_cast<QWidget*>(sender());
147 if(w){
181 QWidget *w = qobject_cast<QWidget *>(sender());
182 if (w) {
148183 w->setStyleSheet(w->styleSheet());
149184 }
150185 }
2222 Q_OBJECT
2323
2424 public:
25 static DThemeManager * instance();
25 static DThemeManager *instance();
2626
2727 QString theme() const;
2828 void setTheme(const QString theme);
3131 QString getQssForWidget(const QString className, const QString &theme = "") const;
3232 QString getQssForWidget(const QString className, const QWidget *widget) const;
3333 QString getQssForWidget(const QWidget *widget) const;
34
35 static void registerWidget(QWidget *widget, QStringList propertys = QStringList());
3436
3537 public Q_SLOTS:
3638 void updateQss();
3636 <file>light/dialogs/DSettingsDialog.theme</file>
3737 <file>light/DSpinBox.theme</file>
3838 <file>light/DTitlebar.theme</file>
39 <file>light/DBoxWidget.theme</file>
3940 </qresource>
4041 <qresource prefix="/images">
4142 <file>light/images/arrow_down_hover.png</file>
188188 $$PWD/DVideoWidget \
189189 $$PWD/DForeignWindow \
190190 $$PWD/DExpandGroup \
191 $$PWD/DArrowLineExpand
191 $$PWD/DArrowButton \
192 $$PWD/DArrowLineExpand \
193 $$PWD/DThemeManager
192194