Debian Codegraph @

src/plugins/platform/xcb/dxcbplatforminterface.cpp

raw
1 // SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dxcbplatforminterface.h"
6 #include "dxcbplatforminterface_p.h"
7
8 #include <DNativeSettings>
9 #include <QPlatformSurfaceEvent>
10 #include <QGuiApplication>
11 #include <QMetaObject>
12 #include <QMetaProperty>
13
14 #include "dplatformtheme.h"
15 #include "dguiapplicationhelper.h"
16 #include <qpa/qplatformwindow.h>
17
18 DGUI_BEGIN_NAMESPACE
19
20 #define DEFINE_CONST_CHAR(Name) const char _##Name[] = "_d_" #Name
21
22 #define FETCH_PROPERTY(Name, Function) \
23 D_DC(DXCBPlatformInterface); \
24 QVariant value = d->theme->getSetting(QByteArrayLiteral(Name)); \
25 if (d->fallbackProperty && !value.isValid() && d->parent) \
26 return d->parent->Function(); \
27
28 #define FETCH_PROPERTY_WITH_ARGS(Name, Function, Args) \
29 D_DC(DXCBPlatformInterface); \
30 QVariant value = d->theme->getSetting(Name); \
31 if (d->fallbackProperty && !value.isValid() && d->parent) \
32 return d->parent->Function(Args); \
33
34
35 DXCBPlatformInterfacePrivate::DXCBPlatformInterfacePrivate(DXCBPlatformInterface *qq)
36 :DCORE_NAMESPACE::DObjectPrivate(qq)
37 {
38 }
39
40 void DXCBPlatformInterfacePrivate::_q_onThemePropertyChanged(const QByteArray &name, const QVariant &value)
41 {
42 D_Q(DXCBPlatformInterface);
43
44 // 转发属性变化的信号,此信号来源可能为parent theme或“非调色板”的属性变化。
45 // 使用队列的形式转发,避免多次发出同样的信号
46 // q->staticMetaObject.invokeMethod(q, "propertyChanged", Qt::QueuedConnection,
47 // Q_ARG(const QByteArray&, name), Q_ARG(const QVariant&, value));
48
49 if (QByteArrayLiteral("Gtk/FontName") == name) {
50 Q_EMIT q->m_platformTheme->gtkFontNameChanged(value.toByteArray());
51 return;
52 }
53
54 if (name.startsWith("Qt/DPI/")) {
55 const QString &screen_name = QString::fromLocal8Bit(name.mid(7));
56
57 if (!screen_name.isEmpty()) {
58 bool ok = false;
59 int dpi = value.toInt(&ok);
60
61 Q_EMIT q->m_platformTheme->dotsPerInchChanged(screen_name, ok ? dpi : -1);
62 }
63
64 return;
65 }
66
67 if (QByteArrayLiteral("Xft/DPI") == name) {
68 bool ok = false;
69 int dpi = value.toInt(&ok);
70 Q_EMIT q->m_platformTheme->dotsPerInchChanged(QString(), ok ? dpi : -1);
71 }
72
73 const QByteArrayList &list = name.split('/');
74
75 if (list.count() != 2)
76 return;
77
78 QByteArray pn = list.last();
79
80 if (pn.isEmpty())
81 return;
82
83
84 // 转换首字母为小写
85 pn[0] = QChar(pn.at(0)).toLower().toLatin1();
86
87 // 直接使用静态的meta object,防止通过metaObject函数调用到dynamic metaobject
88 const QMetaObject *mo = &DPlatformTheme::staticMetaObject;
89
90 int index = mo->indexOfProperty(pn.constData());
91
92
93 if (index < 0)
94 return;
95
96 const QMetaProperty &p = mo->property(index);
97 bool is_parent_signal = q->sender() != theme;
98
99 // 当自己的属性有效时应该忽略父主题的属性变化信号,优先以自身的属性值为准。
100 if (is_parent_signal && p.read(q).isValid()) {
101 return;
102 }
103
104 if (p.hasNotifySignal()) {
105 // invoke会做Q_ASSERT(mobj->cast(object))判断, DPlatformTheme的dynamic metaObject为
106 // qt5platform-plugin插件的DNativeSettings. 导致崩溃.
107 // invokeOnGadget与invoke代码逻辑一致, 只是少了异步支持.
108 // return;
109
110 if (!p.notifySignal().invokeOnGadget(q->m_platformTheme, QGenericArgument(value.typeName(), value.constData())))
111 qWarning() << "_q_onThemePropertyChanged() error when notify signal" << p.notifySignal().name();
112 }
113 }
114
115
116 DXCBPlatformInterface::DXCBPlatformInterface(quint32 window, DPlatformTheme *platformTheme)
117 : DPlatformInterface(platformTheme)
118 , Core::DObject(*new DXCBPlatformInterfacePrivate(this))
119 {
120 D_D(DXCBPlatformInterface);
121
122 d->theme = new DNativeSettings(window, QByteArray(), platformTheme);
123
124 connect(d->theme, SIGNAL(propertyChanged(const QByteArray &, const QVariant &)),
125 this, SLOT(_q_onThemePropertyChanged(const QByteArray &, const QVariant &)));
126 }
127
128 int DXCBPlatformInterface::cursorBlinkTime() const
129 {
130 FETCH_PROPERTY("Net/CursorBlinkTime", cursorBlinkTime)
131
132 return value.toInt();
133 }
134
135 int DXCBPlatformInterface::cursorBlinkTimeout() const
136 {
137 FETCH_PROPERTY("Net/CursorBlinkTimeout", cursorBlinkTimeout)
138
139 return value.toInt();
140 }
141
142 bool DXCBPlatformInterface::cursorBlink() const
143 {
144 FETCH_PROPERTY("Net/CursorBlink", cursorBlink)
145
146 return value.toInt();
147 }
148
149 int DXCBPlatformInterface::doubleClickDistance() const
150 {
151 FETCH_PROPERTY("Net/DoubleClickDistance", doubleClickDistance)
152
153 return value.toInt();
154 }
155
156 int DXCBPlatformInterface::doubleClickTime() const
157 {
158 FETCH_PROPERTY("Net/DoubleClickTime", doubleClickTime)
159
160 return value.toInt();
161 }
162
163 int DXCBPlatformInterface::dndDragThreshold() const
164 {
165 FETCH_PROPERTY("Net/DndDragThreshold", dndDragThreshold)
166
167 return value.toInt();
168 }
169
170 int DXCBPlatformInterface::windowRadius() const
171 {
172 return windowRadius(-1);
173 }
174
175 int DXCBPlatformInterface::windowRadius(int defaultValue) const
176 {
177 Q_D(const DXCBPlatformInterface);
178
179 QVariant value = d->theme->getSetting(QByteArrayLiteral("DTK/WindowRadius"));
180 bool ok = false;
181
182 if (d->fallbackProperty && !value.isValid() && d->parent)
183 return d->parent->windowRadius(defaultValue);
184
185 int radius = value.toInt(&ok);
186
187 return ok ? radius : defaultValue;
188 }
189
190 QByteArray DXCBPlatformInterface::themeName() const
191 {
192 FETCH_PROPERTY("Net/ThemeName", themeName)
193
194 return value.toByteArray();
195 }
196
197 QByteArray DXCBPlatformInterface::iconThemeName() const
198 {
199 FETCH_PROPERTY("Net/IconThemeName", iconThemeName)
200
201 return value.toByteArray();
202 }
203
204 QByteArray DXCBPlatformInterface::soundThemeName() const
205 {
206 FETCH_PROPERTY("Net/SoundThemeName", soundThemeName)
207
208 return value.toByteArray();
209 }
210
211 QByteArray DXCBPlatformInterface::fontName() const
212 {
213 FETCH_PROPERTY("Qt/FontName", fontName)
214
215 return value.toByteArray();
216 }
217
218 QByteArray DXCBPlatformInterface::monoFontName() const
219 {
220 FETCH_PROPERTY("Qt/MonoFontName", monoFontName)
221
222 return value.toByteArray();
223 }
224
225 qreal DXCBPlatformInterface::fontPointSize() const
226 {
227 FETCH_PROPERTY("Qt/FontPointSize", fontPointSize)
228
229 return value.toDouble();
230 }
231
232 QByteArray DXCBPlatformInterface::gtkFontName() const
233 {
234 FETCH_PROPERTY("Gtk/FontName", gtkFontName)
235
236 return value.toByteArray();
237 }
238
239 QColor DXCBPlatformInterface::activeColor() const
240 {
241 FETCH_PROPERTY("Qt/ActiveColor", activeColor)
242
243 return qvariant_cast<QColor>(value);
244 }
245
246 QColor DXCBPlatformInterface::darkActiveColor() const
247 {
248 FETCH_PROPERTY("Qt/DarkActiveColor", darkActiveColor)
249
250 return qvariant_cast<QColor>(value);
251 }
252
253 static QColor getSetting(const QByteArray &key)
254 {
255 qWarning() << "Not implemented, key:" << key;
256 return {};
257 }
258
259 #define GET_COLOR(Role) qvariant_cast<QColor>(getSetting(QByteArrayLiteral(#Role)))
260 #if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
261 QColor DXCBPlatformInterface::window() const
262 {
263 return GET_COLOR(window);
264 }
265
266 QColor DXCBPlatformInterface::windowText() const
267 {
268 return GET_COLOR(windowText);
269 }
270
271 QColor DXCBPlatformInterface::base() const
272 {
273 return GET_COLOR(base);
274 }
275
276 QColor DXCBPlatformInterface::alternateBase() const
277 {
278 return GET_COLOR(alternateBase);
279 }
280
281 QColor DXCBPlatformInterface::toolTipBase() const
282 {
283 return GET_COLOR(toolTipBase);
284 }
285
286 QColor DXCBPlatformInterface::toolTipText() const
287 {
288 return GET_COLOR(toolTipText);
289 }
290
291 QColor DXCBPlatformInterface::text() const
292 {
293 return GET_COLOR(text);
294 }
295
296 QColor DXCBPlatformInterface::button() const
297 {
298 return GET_COLOR(button);
299 }
300
301 QColor DXCBPlatformInterface::buttonText() const
302 {
303 return GET_COLOR(buttonText);
304 }
305
306 QColor DXCBPlatformInterface::brightText() const
307 {
308 return GET_COLOR(brightText);
309 }
310
311 QColor DXCBPlatformInterface::light() const
312 {
313 return GET_COLOR(light);
314 }
315
316 QColor DXCBPlatformInterface::midlight() const
317 {
318 return GET_COLOR(midlight);
319 }
320
321 QColor DXCBPlatformInterface::dark() const
322 {
323 return GET_COLOR(dark);
324 }
325
326 QColor DXCBPlatformInterface::mid() const
327 {
328 return GET_COLOR(mid);
329 }
330
331 QColor DXCBPlatformInterface::shadow() const
332 {
333 return GET_COLOR(shadow);
334 }
335
336 QColor DXCBPlatformInterface::highlight() const
337 {
338 return GET_COLOR(highlight);
339 }
340
341 QColor DXCBPlatformInterface::highlightedText() const
342 {
343 return GET_COLOR(highlightedText);
344 }
345
346 QColor DXCBPlatformInterface::link() const
347 {
348 return GET_COLOR(link);
349 }
350
351 QColor DXCBPlatformInterface::linkVisited() const
352 {
353 return GET_COLOR(linkVisited);
354 }
355
356 QColor DXCBPlatformInterface::itemBackground() const
357 {
358 return GET_COLOR(itemBackground);
359 }
360
361 QColor DXCBPlatformInterface::textTitle() const
362 {
363 return GET_COLOR(textTitle);
364 }
365
366 QColor DXCBPlatformInterface::textTips() const
367 {
368 return GET_COLOR(textTips);
369 }
370
371 QColor DXCBPlatformInterface::textWarning() const
372 {
373 return GET_COLOR(textWarning);
374 }
375
376 QColor DXCBPlatformInterface::textLively() const
377 {
378 return GET_COLOR(textLively);
379 }
380
381 QColor DXCBPlatformInterface::lightLively() const
382 {
383 return GET_COLOR(lightLively);
384 }
385
386 QColor DXCBPlatformInterface::darkLively() const
387 {
388 return GET_COLOR(darkLively);
389 }
390
391 QColor DXCBPlatformInterface::frameBorder() const
392 {
393 return GET_COLOR(frameBorder);
394 }
395 #endif
396
397 int DXCBPlatformInterface::dotsPerInch(const QString &screenName) const
398 {
399 bool ok = false;
400
401 if (!screenName.isEmpty()) {
402 FETCH_PROPERTY_WITH_ARGS("Qt/DPI/" + screenName.toLocal8Bit(), dotsPerInch, screenName);
403 int dpi = value.toInt(&ok);
404
405 if (ok)
406 return dpi;
407 }
408
409 FETCH_PROPERTY_WITH_ARGS("Xft/DPI", dotsPerInch, screenName);
410 int dpi = value.toInt(&ok);
411 return ok ? dpi : -1;
412 }
413
414 /*!
415 \property DXCBPlatformInterface::sizeMode
416 \brief This property holds the sizeMode of the system's SizeMode.
417 */
418 int DXCBPlatformInterface::sizeMode() const
419 {
420 D_DC(DXCBPlatformInterface);
421 QVariant value = d->theme->getSetting(QByteArrayLiteral("DTK/SizeMode"));
422 return value.toInt();
423 }
424
425 /*!
426 \property DXCBPlatformInterface::scrollBarPolicy
427 \brief This property holds the scrollBarPolicy of the system. same as Qt::ScrollBarPolicy
428 \retval 0 show as needed auto hide, default
429 \retval 1 always off
430 \retval 2 always on
431 */
432 int DXCBPlatformInterface::scrollBarPolicy() const
433 {
434 FETCH_PROPERTY("Qt/ScrollBarPolicy", scrollBarPolicy)
435
436 return qvariant_cast<int>(value);
437 }
438
439 void DXCBPlatformInterface::setCursorBlinkTime(int cursorBlinkTime)
440 {
441 D_D(DXCBPlatformInterface);
442
443 d->theme->setSetting("Net/CursorBlinkTime", cursorBlinkTime);
444 }
445
446 void DXCBPlatformInterface::setCursorBlinkTimeout(int cursorBlinkTimeout)
447 {
448 D_D(DXCBPlatformInterface);
449
450 d->theme->setSetting("Net/CursorBlinkTimeout", cursorBlinkTimeout);
451 }
452
453 void DXCBPlatformInterface::setCursorBlink(bool cursorBlink)
454 {
455 D_D(DXCBPlatformInterface);
456
457 d->theme->setSetting("Net/CursorBlink", cursorBlink);
458 }
459
460 void DXCBPlatformInterface::setDoubleClickDistance(int doubleClickDistance)
461 {
462 D_D(DXCBPlatformInterface);
463
464 d->theme->setSetting("Net/DoubleClickDistance", doubleClickDistance);
465 }
466
467 void DXCBPlatformInterface::setDoubleClickTime(int doubleClickTime)
468 {
469 D_D(DXCBPlatformInterface);
470
471 d->theme->setSetting("Net/DoubleClickTime", doubleClickTime);
472 }
473
474 void DXCBPlatformInterface::setDndDragThreshold(int dndDragThreshold)
475 {
476 D_D(DXCBPlatformInterface);
477
478 d->theme->setSetting("Net/DndDragThreshold", dndDragThreshold);
479 }
480
481 void DXCBPlatformInterface::setThemeName(const QByteArray &themeName)
482 {
483 D_D(DXCBPlatformInterface);
484
485 d->theme->setSetting("Net/ThemeName", themeName);
486 }
487
488 void DXCBPlatformInterface::setIconThemeName(const QByteArray &iconThemeName)
489 {
490 D_D(DXCBPlatformInterface);
491
492 d->theme->setSetting("Net/IconThemeName", iconThemeName);
493 }
494
495 void DXCBPlatformInterface::setSoundThemeName(const QByteArray &soundThemeName)
496 {
497 D_D(DXCBPlatformInterface);
498
499 d->theme->setSetting("Net/SoundThemeName", soundThemeName);
500 }
501
502 void DXCBPlatformInterface::setFontName(const QByteArray &fontName)
503 {
504 D_D(DXCBPlatformInterface);
505
506 d->theme->setSetting("Qt/FontName", fontName);
507 }
508
509 void DXCBPlatformInterface::setMonoFontName(const QByteArray &monoFontName)
510 {
511 D_D(DXCBPlatformInterface);
512
513 d->theme->setSetting("Qt/MonoFontName", monoFontName);
514 }
515
516 void DXCBPlatformInterface::setFontPointSize(qreal fontPointSize)
517 {
518 D_D(DXCBPlatformInterface);
519
520 d->theme->setSetting("Qt/FontPointSize", fontPointSize);
521 }
522
523 void DXCBPlatformInterface::setGtkFontName(const QByteArray &fontName)
524 {
525 D_D(DXCBPlatformInterface);
526
527 d->theme->setSetting("Gtk/FontName", fontName);
528 }
529
530 void DXCBPlatformInterface::setActiveColor(const QColor activeColor)
531 {
532 D_D(DXCBPlatformInterface);
533
534 d->theme->setSetting("Qt/ActiveColor", activeColor);
535 }
536
537 void DXCBPlatformInterface::setDarkActiveColor(const QColor &activeColor)
538 {
539 D_D(DXCBPlatformInterface);
540
541 d->theme->setSetting("Qt/DarkActiveColor", activeColor);
542 }
543
544 static void setSetting(const QByteArray &key, const QColor &color)
545 {
546 qWarning() << "Not implemented, key: " << key << "value: " << color;
547 }
548
549 #define SET_COLOR(Role) setSetting(QByteArrayLiteral(#Role), Role)
550 #if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
551 void DXCBPlatformInterface::setWindow(const QColor &window)
552 {
553 SET_COLOR(window);
554 }
555
556 void DXCBPlatformInterface::setWindowText(const QColor &windowText)
557 {
558 SET_COLOR(windowText);
559 }
560
561 void DXCBPlatformInterface::setBase(const QColor &base)
562 {
563 SET_COLOR(base);
564 }
565
566 void DXCBPlatformInterface::setAlternateBase(const QColor &alternateBase)
567 {
568 SET_COLOR(alternateBase);
569 }
570
571 void DXCBPlatformInterface::setToolTipBase(const QColor &toolTipBase)
572 {
573 SET_COLOR(toolTipBase);
574 }
575
576 void DXCBPlatformInterface::setToolTipText(const QColor &toolTipText)
577 {
578 SET_COLOR(toolTipText);
579 }
580
581 void DXCBPlatformInterface::setText(const QColor &text)
582 {
583 SET_COLOR(text);
584 }
585
586 void DXCBPlatformInterface::setButton(const QColor &button)
587 {
588 SET_COLOR(button);
589 }
590
591 void DXCBPlatformInterface::setButtonText(const QColor &buttonText)
592 {
593 SET_COLOR(buttonText);
594 }
595
596 void DXCBPlatformInterface::setBrightText(const QColor &brightText)
597 {
598 SET_COLOR(brightText);
599 }
600
601 void DXCBPlatformInterface::setLight(const QColor &light)
602 {
603 SET_COLOR(light);
604 }
605
606 void DXCBPlatformInterface::setMidlight(const QColor &midlight)
607 {
608 SET_COLOR(midlight);
609 }
610
611 void DXCBPlatformInterface::setDark(const QColor &dark)
612 {
613 SET_COLOR(dark);
614 }
615
616 void DXCBPlatformInterface::setMid(const QColor &mid)
617 {
618 SET_COLOR(mid);
619 }
620
621 void DXCBPlatformInterface::setShadow(const QColor &shadow)
622 {
623 SET_COLOR(shadow);
624 }
625
626 void DXCBPlatformInterface::setHighlight(const QColor &highlight)
627 {
628 SET_COLOR(highlight);
629 }
630
631 void DXCBPlatformInterface::setHighlightedText(const QColor &highlightText)
632 {
633 SET_COLOR(highlightText);
634 }
635
636 void DXCBPlatformInterface::setLink(const QColor &link)
637 {
638 SET_COLOR(link);
639 }
640
641 void DXCBPlatformInterface::setLinkVisited(const QColor &linkVisited)
642 {
643 SET_COLOR(linkVisited);
644 }
645
646 void DXCBPlatformInterface::setItemBackground(const QColor &itemBackground)
647 {
648 SET_COLOR(itemBackground);
649 }
650
651 void DXCBPlatformInterface::setTextTitle(const QColor &textTitle)
652 {
653 SET_COLOR(textTitle);
654 }
655
656 void DXCBPlatformInterface::setTextTips(const QColor &textTips)
657 {
658 SET_COLOR(textTips);
659 }
660
661 void DXCBPlatformInterface::setTextWarning(const QColor &textWarning)
662 {
663 SET_COLOR(textWarning);
664 }
665
666 void DXCBPlatformInterface::setTextLively(const QColor &textLively)
667 {
668 SET_COLOR(textLively);
669 }
670
671 void DXCBPlatformInterface::setLightLively(const QColor &lightLively)
672 {
673 SET_COLOR(lightLively);
674 }
675
676 void DXCBPlatformInterface::setDarkLively(const QColor &darkLively)
677 {
678 SET_COLOR(darkLively);
679 }
680
681 void DXCBPlatformInterface::setFrameBorder(const QColor &frameBorder)
682 {
683 SET_COLOR(frameBorder);
684 }
685 #endif
686
687 void DXCBPlatformInterface::setDotsPerInch(const QString &screenName, int dpi)
688 {
689 D_D(DXCBPlatformInterface);
690
691 if (screenName.isEmpty()) {
692 d->theme->setSetting("Xft/DPI", dpi);
693 } else {
694 d->theme->setSetting("Qt/DPI/" + screenName.toLocal8Bit(), dpi);
695 }
696 }
697
698 void DXCBPlatformInterface::setWindowRadius(int windowRadius)
699 {
700 D_D(DXCBPlatformInterface);
701
702 d->theme->setSetting("DTK/WindowRadius", windowRadius);
703 }
704
705 DGUI_END_NAMESPACE
706
707 #include "moc_dxcbplatforminterface.cpp"