Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qkdetheme.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qkdetheme_p.h"
6#include <qpa/qplatformtheme_p.h>
7#include <qpa/qplatformfontdatabase.h>
8#include <qpa/qplatformdialoghelper.h>
9#include <QPalette>
10#include <qpa/qwindowsysteminterface.h>
12#if QT_CONFIG(dbus) && QT_CONFIG(systemtrayicon)
13#include <private/qdbustrayicon_p.h>
14#endif
15#include <private/qdbusplatformmenu_p.h>
16#include <private/qdbusmenubar_p.h>
17
18#include <QSettings>
19#include <QStandardPaths>
20#include <QtCore/private/qenumindexedarray_p.h>
21
23
24using namespace Qt::StringLiterals;
25
26Q_STATIC_LOGGING_CATEGORY(lcQpaThemeKde, "qt.qpa.theme.kde")
27
29{
30
31public:
41
74
75 QKdeThemePrivate(const QStringList &kdeDirs, int kdeVersion);
76 ~QKdeThemePrivate() { clearResources(); }
77
78 static QString kdeGlobals(const QString &kdeDir, int kdeVersion)
79 {
80 if (kdeVersion > 4)
81 return kdeDir + "/kdeglobals"_L1;
82 return kdeDir + "/share/config/kdeglobals"_L1;
83 }
84
85 void refresh();
86 static QVariant readKdeSetting(KdeSetting s, const QStringList &kdeDirs, int kdeVersion, QHash<QString, QSettings*> &settings);
88 void clearKdeSettings() const;
89 static void readKdeSystemPalette(const QStringList &kdeDirs, int kdeVersion, QHash<QString, QSettings*> &kdeSettings, QPalette *pal);
90 static std::unique_ptr<QFont> kdeFont(const QVariant &fontValue);
91 static QStringList kdeIconThemeSearchPaths(const QStringList &kdeDirs);
92
94 const int kdeVersion;
95
101 bool singleClick = true;
106 int startDragTime = 500;
107 int cursorBlinkRate = 1000;
112 void updateColorScheme(const QString &themeName);
113 bool hasRequestedColorScheme() const { return m_requestedColorScheme != Qt::ColorScheme::Unknown
114 && m_requestedColorScheme != m_colorScheme; }
115
116private:
117 mutable QHash<QString, QSettings *> kdeSettings;
118#if QT_CONFIG(dbus)
120 bool initDbus();
123 const QVariant &value);
125#endif // QT_CONFIG(dbus)
126 void clearResources();
127};
128
129#if QT_CONFIG(dbus)
130void QKdeThemePrivate::settingChangedHandler(QDBusListener::Provider provider,
131 QDBusListener::Setting setting,
132 const QVariant &value)
133{
134 if (provider != QDBusListener::Provider::Kde)
135 return;
136
137 switch (setting) {
138 case QDBusListener::Setting::ColorScheme:
139 qCDebug(lcQpaThemeKde) << "KDE color theme changed to:" << value.value<Qt::ColorScheme>();
140 break;
141 case QDBusListener::Setting::Theme:
142 qCDebug(lcQpaThemeKde) << "KDE global theme changed to:" << value.toString();
143 break;
144 case QDBusListener::Setting::ApplicationStyle:
145 qCDebug(lcQpaThemeKde) << "KDE application style changed to:" << value.toString();
146 break;
147 case QDBusListener::Setting::Contrast:
148 qCDebug(lcQpaThemeKde) << "KDE contrast setting changed to: "
149 << value.value<Qt::ContrastPreference>();
150 break;
151 case QDBusListener::Setting::Motion:
152 qCDebug(lcQpaThemeKde) << "KDE motion setting changed to: "
153 << value.value<Qt::MotionPreference>();
154 break;
155 }
156
157 refresh();
158}
159
160void QKdeThemePrivate::clearResources()
161{
162 for (auto &e : fonts)
163 e.reset();
164 systemPalette.reset();
165}
166
167bool QKdeThemePrivate::initDbus()
168{
169 dbus.reset(new QDBusListener());
170 Q_ASSERT(dbus);
171
172 // Wrap slot in a lambda to avoid inheriting QKdeThemePrivate from QObject
173 auto wrapper = [this](QDBusListener::Provider provider,
174 QDBusListener::Setting setting,
175 const QVariant &value) {
176 settingChangedHandler(provider, setting, value);
177 };
178
179 return QObject::connect(dbus.get(), &QDBusListener::settingChanged, dbus.get(), wrapper);
180}
181#endif // QT_CONFIG(dbus)
182
183QKdeThemePrivate::QKdeThemePrivate(const QStringList &kdeDirs, int kdeVersion)
184 : kdeDirs(kdeDirs), kdeVersion(kdeVersion)
185{
186#if QT_CONFIG(dbus)
187 initDbus();
188#endif // QT_CONFIG(dbus)
189}
190
192{
193 switch (type) {
195 return QLatin1StringView();
197 return QLatin1StringView("KDE/");
199 return QLatin1StringView();
201 return QLatin1StringView("Colors:");
203 return QLatin1StringView("Icons/");
205 return QLatin1StringView("ToolbarIcons/");
207 return QLatin1StringView("Toolbar style/");
208 }
209 // GCC 8.x does not treat __builtin_unreachable() as constexpr
210# if !defined(Q_CC_GNU_ONLY) || (Q_CC_GNU >= 900)
211 // NOLINTNEXTLINE(qt-use-unreachable-return): Triggers on Clang, breaking GCC 8
212 Q_UNREACHABLE();
213# endif
214 return {};
215}
216
218{
219#define CASE(s, type) case QKdeThemePrivate::KdeSetting::s:
220 return QKdeThemePrivate::KdeSettingType::type
221
222 switch (setting) {
235 CASE(Font, Root);
236 CASE(Fixed, Root);
253 };
254 // GCC 8.x does not treat __builtin_unreachable() as constexpr
255# if !defined(Q_CC_GNU_ONLY) || (Q_CC_GNU >= 900)
256 // NOLINTNEXTLINE(qt-use-unreachable-return): Triggers on Clang, breaking GCC 8
257 Q_UNREACHABLE();
258# endif
260}
261#undef CASE
262
264{
265 switch (setting) {
267 return QLatin1StringView("widgetStyle");
269 return QLatin1StringView("ColorScheme");
271 return QLatin1StringView("SingleClick");
273 return QLatin1StringView("ShowIconsOnPushButtons");
275 return QLatin1StringView("Theme");
277 return QLatin1StringView("Size");
279 return QLatin1StringView("ToolButtonStyle");
281 return QLatin1StringView("WheelScrollLines");
283 return QLatin1StringView("DoubleClickInterval");
285 return QLatin1StringView("StartDragDist");
287 return QLatin1StringView("StartDragTime");
289 return QLatin1StringView("CursorBlinkRate");
291 return QLatin1StringView("font");
293 return QLatin1StringView("fixed");
295 return QLatin1StringView("menuFont");
297 return QLatin1StringView("toolBarFont");
299 return QLatin1StringView("Button/BackgroundNormal");
301 return QLatin1StringView("Window/BackgroundNormal");
303 return QLatin1StringView("View/ForegroundNormal");
305 return QLatin1StringView("View/ForegroundInactive");
307 return QLatin1StringView("Window/ForegroundNormal");
309 return QLatin1StringView("View/BackgroundNormal");
311 return QLatin1StringView("Selection/BackgroundNormal");
313 return QLatin1StringView("Selection/ForegroundNormal");
315 return QLatin1StringView("View/BackgroundAlternate");
317 return QLatin1StringView("Button/ForegroundNormal");
319 return QLatin1StringView("View/ForegroundLink");
321 return QLatin1StringView("View/ForegroundVisited");
323 return QLatin1StringView("Tooltip/BackgroundNormal");
325 return QLatin1StringView("Tooltip/ForegroundNormal");
326 };
327 // GCC 8.x does not treat __builtin_unreachable() as constexpr
328# if !defined(Q_CC_GNU_ONLY) || (Q_CC_GNU >= 900)
329 // NOLINTNEXTLINE(qt-use-unreachable-return): Triggers on Clang, breaking GCC 8
330 Q_UNREACHABLE();
331# endif
332 return {};
333}
334
336{
337 clearResources();
339
340 toolButtonStyle = Qt::ToolButtonTextBesideIcon;
341 toolBarIconSize = 0;
342 styleNames.clear();
343 if (kdeVersion >= 5)
344 styleNames << QStringLiteral("breeze");
345 styleNames << QStringLiteral("Oxygen") << QStringLiteral("Fusion") << QStringLiteral("windows");
346 if (kdeVersion >= 5)
347 iconFallbackThemeName = iconThemeName = QStringLiteral("breeze");
348 else
349 iconFallbackThemeName = iconThemeName = QStringLiteral("oxygen");
350
351 systemPalette.reset(new QPalette(QPalette()));
352 readKdeSystemPalette(kdeDirs, kdeVersion, kdeSettings, systemPalette.get());
353
354 const QVariant styleValue = readKdeSetting(KdeSetting::WidgetStyle);
355 if (styleValue.isValid()) {
356 const QString style = styleValue.toString();
357 if (style != styleNames.front())
358 styleNames.push_front(style);
359 }
360
361 const QVariant colorScheme = readKdeSetting(KdeSetting::ColorScheme);
362
363 updateColorScheme(colorScheme.toString());
364
365 const QVariant singleClickValue = readKdeSetting(KdeSetting::SingleClick);
366 if (singleClickValue.isValid())
367 singleClick = singleClickValue.toBool();
368 else if (kdeVersion >= 6) // Plasma 6 defaults to double-click
369 singleClick = false;
370 else // earlier version to single-click
371 singleClick = true;
372
373 const QVariant showIconsOnPushButtonsValue = readKdeSetting(KdeSetting::ShowIconsOnPushButtons);
374 if (showIconsOnPushButtonsValue.isValid())
375 showIconsOnPushButtons = showIconsOnPushButtonsValue.toBool();
376
377 const QVariant themeValue = readKdeSetting(KdeSetting::IconTheme);
378 if (themeValue.isValid())
379 iconThemeName = themeValue.toString();
380
381 const QVariant toolBarIconSizeValue = readKdeSetting(KdeSetting::ToolBarIconSize);
382 if (toolBarIconSizeValue.isValid())
383 toolBarIconSize = toolBarIconSizeValue.toInt();
384
385 const QVariant toolbarStyleValue = readKdeSetting(KdeSetting::ToolButtonStyle);
386 if (toolbarStyleValue.isValid()) {
387 const QString toolBarStyle = toolbarStyleValue.toString();
388 if (toolBarStyle == "TextBesideIcon"_L1)
389 toolButtonStyle = Qt::ToolButtonTextBesideIcon;
390 else if (toolBarStyle == "TextOnly"_L1)
391 toolButtonStyle = Qt::ToolButtonTextOnly;
392 else if (toolBarStyle == "TextUnderIcon"_L1)
393 toolButtonStyle = Qt::ToolButtonTextUnderIcon;
394 }
395
396 const QVariant wheelScrollLinesValue = readKdeSetting(KdeSetting::WheelScrollLines);
397 if (wheelScrollLinesValue.isValid())
398 wheelScrollLines = wheelScrollLinesValue.toInt();
399
400 const QVariant doubleClickIntervalValue = readKdeSetting(KdeSetting::DoubleClickInterval);
401 if (doubleClickIntervalValue.isValid())
402 doubleClickInterval = doubleClickIntervalValue.toInt();
403
404 const QVariant startDragDistValue = readKdeSetting(KdeSetting::StartDragDistance);
405 if (startDragDistValue.isValid())
406 startDragDist = startDragDistValue.toInt();
407
408 const QVariant startDragTimeValue = readKdeSetting(KdeSetting::StartDragTime);
409 if (startDragTimeValue.isValid())
410 startDragTime = startDragTimeValue.toInt();
411
412 const QVariant cursorBlinkRateValue = readKdeSetting(KdeSetting::CursorBlinkRate);
413 if (cursorBlinkRateValue.isValid()) {
414 cursorBlinkRate = cursorBlinkRateValue.toInt();
415 cursorBlinkRate = cursorBlinkRate > 0 ? qBound(200, cursorBlinkRate, 2000) : 0;
416 }
417
418 // Read system font, ignore 'smallestReadableFont'
419 if (auto systemFont = kdeFont(readKdeSetting(KdeSetting::Font))) {
420 fonts[QPlatformTheme::SystemFont] = std::move(systemFont);
421 } else {
422 fonts[QPlatformTheme::SystemFont]
423 = std::make_unique<QFont>(QLatin1StringView(QGenericUnixTheme::defaultSystemFontNameC),
424 QGenericUnixTheme::defaultSystemFontSize);
425 }
426
427 if (auto fixedFont = kdeFont(readKdeSetting(KdeSetting::Fixed))) {
428 fonts[QPlatformTheme::FixedFont] = std::move(fixedFont);
429 } else {
430 auto f = std::make_unique<QFont>(QLatin1StringView(QGenericUnixTheme::defaultFixedFontNameC),
431 QGenericUnixTheme::defaultSystemFontSize);
432 f->setStyleHint(QFont::TypeWriter);
433 fonts[QPlatformTheme::FixedFont] = std::move(f);
434 }
435
436 if (auto menuFont = kdeFont(readKdeSetting(KdeSetting::MenuFont))) {
437 fonts[QPlatformTheme::MenuBarFont] = std::make_unique<QFont>(*menuFont);
438 fonts[QPlatformTheme::MenuFont] = std::move(menuFont);
439 }
440
441 if (auto toolBarFont = kdeFont(readKdeSetting(KdeSetting::ToolBarFont)))
442 fonts[QPlatformTheme::ToolButtonFont] = std::move(toolBarFont);
443
444 QWindowSystemInterface::handleThemeChange();
445
446 qCDebug(lcQpaFonts) << "default fonts: system" << fonts[QPlatformTheme::SystemFont].get()
447 << "fixed" << fonts[QPlatformTheme::FixedFont].get();
448 qDeleteAll(kdeSettings);
449}
450
451QVariant QKdeThemePrivate::readKdeSetting(KdeSetting s, const QStringList &kdeDirs, int kdeVersion, QHash<QString, QSettings*> &kdeSettings)
452{
453 for (const QString &kdeDir : kdeDirs) {
454 QSettings *settings = kdeSettings.value(kdeDir);
455 if (!settings) {
456 const QString kdeGlobalsPath = kdeGlobals(kdeDir, kdeVersion);
457 if (QFileInfo(kdeGlobalsPath).isReadable()) {
458 settings = new QSettings(kdeGlobalsPath, QSettings::IniFormat);
459 kdeSettings.insert(kdeDir, settings);
460 }
461 }
462 if (settings) {
463 const QString key = settingsPrefix(settingsType(s)) + settingsKey(s);
464 const QVariant value = settings->value(key);
465 if (value.isValid())
466 return value;
467 }
468 }
469 return QVariant();
470}
471
473{
474 return readKdeSetting(s, kdeDirs, kdeVersion, kdeSettings);
475}
476
478{
479 kdeSettings.clear();
480}
481
482// Reads the color from the KDE configuration, and store it in the
483// palette with the given color role if found.
484static inline bool kdeColor(QPalette *pal, QPalette::ColorRole role, const QVariant &value)
485{
486 if (!value.isValid())
487 return false;
488 const QStringList values = value.toStringList();
489 if (values.size() != 3)
490 return false;
491 pal->setBrush(role, QColor(values.at(0).toInt(), values.at(1).toInt(), values.at(2).toInt()));
492 return true;
493}
494
495void QKdeThemePrivate::readKdeSystemPalette(const QStringList &kdeDirs, int kdeVersion, QHash<QString, QSettings*> &kdeSettings, QPalette *pal)
496{
497 if (!kdeColor(pal, QPalette::Button, readKdeSetting(KdeSetting::ButtonBackground, kdeDirs, kdeVersion, kdeSettings))) {
498 // kcolorscheme.cpp: SetDefaultColors
499 const QColor defaultWindowBackground(214, 210, 208);
500 const QColor defaultButtonBackground(223, 220, 217);
501 *pal = QPalette(defaultButtonBackground, defaultWindowBackground);
502 return;
503 }
504
505 kdeColor(pal, QPalette::Window, readKdeSetting(KdeSetting::WindowBackground, kdeDirs, kdeVersion, kdeSettings));
506 kdeColor(pal, QPalette::Text, readKdeSetting(KdeSetting::ViewForeground, kdeDirs, kdeVersion, kdeSettings));
507 kdeColor(pal, QPalette::WindowText, readKdeSetting(KdeSetting::WindowForeground, kdeDirs, kdeVersion, kdeSettings));
508 kdeColor(pal, QPalette::Base, readKdeSetting(KdeSetting::ViewBackground, kdeDirs, kdeVersion, kdeSettings));
509 kdeColor(pal, QPalette::Highlight, readKdeSetting(KdeSetting::SelectionBackground, kdeDirs, kdeVersion, kdeSettings));
510 kdeColor(pal, QPalette::HighlightedText, readKdeSetting(KdeSetting::SelectionForeground, kdeDirs, kdeVersion, kdeSettings));
511 kdeColor(pal, QPalette::AlternateBase, readKdeSetting(KdeSetting::ViewBackgroundAlternate, kdeDirs, kdeVersion, kdeSettings));
512 kdeColor(pal, QPalette::ButtonText, readKdeSetting(KdeSetting::ButtonForeground, kdeDirs, kdeVersion, kdeSettings));
513 kdeColor(pal, QPalette::Link, readKdeSetting(KdeSetting::ViewForegroundLink, kdeDirs, kdeVersion, kdeSettings));
514 kdeColor(pal, QPalette::LinkVisited, readKdeSetting(KdeSetting::ViewForegroundVisited, kdeDirs, kdeVersion, kdeSettings));
515 kdeColor(pal, QPalette::ToolTipBase, readKdeSetting(KdeSetting::TooltipBackground, kdeDirs, kdeVersion, kdeSettings));
516 kdeColor(pal, QPalette::ToolTipText, readKdeSetting(KdeSetting::TooltipForeground, kdeDirs, kdeVersion, kdeSettings));
517 kdeColor(pal, QPalette::PlaceholderText, readKdeSetting(KdeSetting::ViewForegroundInactive, kdeDirs, kdeVersion, kdeSettings));
518
519 // The above code sets _all_ color roles to "normal" colors. In KDE, the disabled
520 // color roles are calculated by applying various effects described in kdeglobals.
521 // We use a bit simpler approach here, similar logic than in qt_palette_from_color().
522 const QColor button = pal->color(QPalette::Button);
523 int h, s, v;
524 button.getHsv(&h, &s, &v);
525
526 const QBrush whiteBrush = QBrush(Qt::white);
527 const QBrush buttonBrush = QBrush(button);
528 const QBrush buttonBrushDark = QBrush(button.darker(v > 128 ? 200 : 50));
529 const QBrush buttonBrushDark150 = QBrush(button.darker(v > 128 ? 150 : 75));
530 const QBrush buttonBrushLight150 = QBrush(button.lighter(v > 128 ? 150 : 75));
531 const QBrush buttonBrushLight = QBrush(button.lighter(v > 128 ? 200 : 50));
532
533 pal->setBrush(QPalette::Disabled, QPalette::WindowText, buttonBrushDark);
534 pal->setBrush(QPalette::Disabled, QPalette::ButtonText, buttonBrushDark);
535 pal->setBrush(QPalette::Disabled, QPalette::Button, buttonBrush);
536 pal->setBrush(QPalette::Disabled, QPalette::Text, buttonBrushDark);
537 pal->setBrush(QPalette::Disabled, QPalette::BrightText, whiteBrush);
538 pal->setBrush(QPalette::Disabled, QPalette::Base, buttonBrush);
539 pal->setBrush(QPalette::Disabled, QPalette::Window, buttonBrush);
540 pal->setBrush(QPalette::Disabled, QPalette::Highlight, buttonBrushDark150);
541 pal->setBrush(QPalette::Disabled, QPalette::HighlightedText, buttonBrushLight150);
542
543 // set calculated colors for all groups
544 pal->setBrush(QPalette::Light, buttonBrushLight);
545 pal->setBrush(QPalette::Midlight, buttonBrushLight150);
546 pal->setBrush(QPalette::Mid, buttonBrushDark150);
547 pal->setBrush(QPalette::Dark, buttonBrushDark);
548}
549
550/*!
551 \class QKdeTheme
552 \brief QKdeTheme is a theme implementation for the KDE desktop (version 4 or higher).
553 \since 5.0
554 \internal
555 \ingroup qpa
556*/
557
558const char *QKdeTheme::name = "kde";
559
560QKdeTheme::QKdeTheme(const QStringList& kdeDirs, int kdeVersion)
561 : QGenericUnixTheme(new QKdeThemePrivate(kdeDirs, kdeVersion))
562{
563 d_func()->refresh();
564}
565
566QKdeTheme::~QKdeTheme()
567 = default;
568
569std::unique_ptr<QFont> QKdeThemePrivate::kdeFont(const QVariant &fontValue)
570{
571 if (fontValue.isValid()) {
572 // Read font value: Might be a QStringList as KDE stores fonts without quotes.
573 // Also retrieve the family for the constructor since we cannot use the
574 // default constructor of QFont, which accesses QGuiApplication::systemFont()
575 // causing recursion.
576 QString fontDescription;
577 QString fontFamily;
578 if (fontValue.userType() == QMetaType::QStringList) {
579 const QStringList list = fontValue.toStringList();
580 if (!list.isEmpty()) {
581 fontFamily = list.first();
582 fontDescription = list.join(u',');
583 }
584 } else {
585 fontDescription = fontFamily = fontValue.toString();
586 }
587 if (!fontDescription.isEmpty()) {
588 QFont font(fontFamily);
589 if (font.fromString(fontDescription))
590 return std::make_unique<QFont>(std::move(font));
591 }
592 }
593 return nullptr;
594}
595
596
597QStringList QKdeThemePrivate::kdeIconThemeSearchPaths(const QStringList &kdeDirs)
598{
599 QStringList paths = QGenericUnixTheme::xdgIconThemePaths();
600 const QString iconPath = QStringLiteral("/share/icons");
601 for (const QString &candidate : kdeDirs) {
602 const QFileInfo fi(candidate + iconPath);
603 if (fi.isDir())
604 paths.append(fi.absoluteFilePath());
605 }
606 return paths;
607}
608
609QVariant QKdeTheme::themeHint(QPlatformTheme::ThemeHint hint) const
610{
611 Q_D(const QKdeTheme);
612 switch (hint) {
613 case QPlatformTheme::UseFullScreenForPopupMenu:
614 return QVariant(true);
615 case QPlatformTheme::DialogButtonBoxButtonsHaveIcons:
616 return QVariant(d->showIconsOnPushButtons);
617 case QPlatformTheme::DialogButtonBoxLayout:
618 return QVariant(QPlatformDialogHelper::KdeLayout);
619 case QPlatformTheme::ToolButtonStyle:
620 return QVariant(d->toolButtonStyle);
621 case QPlatformTheme::ToolBarIconSize:
622 return QVariant(d->toolBarIconSize);
623 case QPlatformTheme::SystemIconThemeName:
624 return QVariant(d->iconThemeName);
625 case QPlatformTheme::SystemIconFallbackThemeName:
626 return QVariant(d->iconFallbackThemeName);
627 case QPlatformTheme::IconThemeSearchPaths:
628 return QVariant(d->kdeIconThemeSearchPaths(d->kdeDirs));
629 case QPlatformTheme::IconPixmapSizes:
630 return QVariant::fromValue(availableXdgFileIconSizes());
631 case QPlatformTheme::StyleNames:
632 return QVariant(d->styleNames);
633 case QPlatformTheme::KeyboardScheme:
634 return QVariant(int(KdeKeyboardScheme));
635 case QPlatformTheme::ItemViewActivateItemOnSingleClick:
636 return QVariant(d->singleClick);
637 case QPlatformTheme::WheelScrollLines:
638 return QVariant(d->wheelScrollLines);
639 case QPlatformTheme::MouseDoubleClickInterval:
640 return QVariant(d->doubleClickInterval);
641 case QPlatformTheme::StartDragTime:
642 return QVariant(d->startDragTime);
643 case QPlatformTheme::StartDragDistance:
644 return QVariant(d->startDragDist);
645 case QPlatformTheme::CursorFlashTime:
646 return QVariant(d->cursorBlinkRate);
647 case QPlatformTheme::UiEffects:
648 return QVariant(int(GeneralUiEffect|FadeMenuUiEffect|AnimateComboUiEffect|FadeTooltipUiEffect|AnimateToolBoxUiEffect|HoverEffect));
649 case QPlatformTheme::MouseCursorTheme:
650 return QVariant(mouseCursorTheme());
651 case QPlatformTheme::MouseCursorSize:
652 return QVariant(mouseCursorSize());
653 case QPlatformTheme::PreferFileIconFromTheme:
654 return true;
655 default:
656 break;
657 }
658 return QPlatformTheme::themeHint(hint);
659}
660
661QIcon QKdeTheme::fileIcon(const QFileInfo &fileInfo, QPlatformTheme::IconOptions) const
662{
663#if QT_CONFIG(mimetype)
664 return xdgFileIcon(fileInfo);
665#else
666 Q_UNUSED(fileInfo);
667 return QIcon();
668#endif
669}
670
671/*!
672 \internal
673 \reimp
674 \brief QKdeTheme::requestColorScheme Programmatically request a color scheme
675 If \a scheme is \c Dark or \c Light, \a scheme is applied to the application,
676 independently from the current KDE theme.
677 If \a scheme is \c Unknown, the current KDE theme's color scheme will be applied instead.
678 This is the default behavior.
679
680 \note
681 A KDE theme is considered either \c Dark or \c Light. When the requested color scheme
682 doesn't match the current KDE theme, a default \c Dark or \c Light fusion palette
683 is used instead.
684 \sa QKdeThemePrivate::hasRequestedColorScheme
685*/
686
687/*!
688 \internal
689 \brief QKdeThemePrivate::hasRequestedColorScheme Check if fusion palette fallback is necessary.
690 This internal helper function returns true, if
691 \list
692 \li a color scheme has been programmatically requested, and
693 \li the requested color scheme differs from the current KDE theme's color scheme.
694 \endlist
695 \sa QKdeTheme:requestColorScheme
696*/
697
698void QKdeTheme::requestColorScheme(Qt::ColorScheme scheme)
699{
700 Q_D(QKdeTheme);
701 if (d->m_requestedColorScheme == scheme)
702 return;
703 qCDebug(lcQpaThemeKde) << scheme << "has been requested. Theme supports color scheme:"
704 << d->m_colorScheme;
705 d->m_requestedColorScheme = scheme;
706 d->refresh();
707}
708
709Qt::ColorScheme QKdeTheme::colorScheme() const
710{
711 Q_D(const QKdeTheme);
712#ifdef QT_DEBUG
713 if (d->hasRequestedColorScheme()) {
714 qCDebug(lcQpaThemeKde) << "Reuqested color scheme" << d->m_requestedColorScheme
715 << "differs from theme color scheme" << d->m_colorScheme;
716 }
717#endif
718 return d->hasRequestedColorScheme() ? d->m_requestedColorScheme
719 : d->m_colorScheme;
720}
721
722/*!
723 \internal
724 \brief QKdeTheme::updateColorScheme - guess and set a color scheme for unix themes.
725 KDE themes do not have a color scheme property.
726 The key words "dark" or "light" are usually part of the theme name.
727 This is, however, not a mandatory convention.
728
729 If \param themeName contains a valid key word, the respective color scheme is set.
730 If it doesn't, the color scheme is heuristically determined by comparing text and base color
731 of the system palette.
732 */
733void QKdeThemePrivate::updateColorScheme(const QString &themeName)
734{
735 if (themeName.contains(QLatin1StringView("light"), Qt::CaseInsensitive)) {
736 m_colorScheme = Qt::ColorScheme::Light;
737 return;
738 }
739 if (themeName.contains(QLatin1StringView("dark"), Qt::CaseInsensitive)) {
740 m_colorScheme = Qt::ColorScheme::Dark;
741 return;
742 }
743
744 m_colorScheme = colorSchemeFromPalette();
745}
746
747Qt::ColorScheme QKdeThemePrivate::colorSchemeFromPalette() const
748{
749 if (!systemPalette)
750 return Qt::ColorScheme::Unknown;
751 if (systemPalette->text().color().lightness() < systemPalette->base().color().lightness())
752 return Qt::ColorScheme::Light;
753 if (systemPalette->text().color().lightness() > systemPalette->base().color().lightness())
754 return Qt::ColorScheme::Dark;
755 return Qt::ColorScheme::Unknown;
756}
757
758const QPalette *QKdeTheme::palette(Palette type) const
759{
760 Q_D(const QKdeTheme);
761 if (d->hasRequestedColorScheme()) {
762 qCDebug(lcQpaThemeKde) << "Current KDE theme doesn't support reuqested color scheme"
763 << d->m_requestedColorScheme << "Falling back to fusion palette:"
764 << type;
765 return QPlatformTheme::palette(type);
766 }
767
768 return d->systemPalette.get();
769}
770
771const QFont *QKdeTheme::font(Font type) const
772{
773 Q_D(const QKdeTheme);
774 return d->fonts[type].get();
775}
776
777QPlatformTheme *QKdeTheme::createKdeTheme()
778{
779 const QByteArray kdeVersionBA = qgetenv("KDE_SESSION_VERSION");
780 const int kdeVersion = kdeVersionBA.toInt();
781 if (kdeVersion < 4)
782 return nullptr;
783
784 if (kdeVersion > 4)
785 // Plasma 5 follows XDG spec
786 // but uses the same config file format:
787 return new QKdeTheme(QStandardPaths::standardLocations(QStandardPaths::GenericConfigLocation), kdeVersion);
788
789 // Determine KDE prefixes in the following priority order:
790 // - KDEHOME and KDEDIRS environment variables
791 // - ~/.kde(<version>)
792 // - read prefixes from /etc/kde<version>rc
793 // - fallback to /etc/kde<version>
794
795 QStringList kdeDirs;
796 const QString kdeHomePathVar = qEnvironmentVariable("KDEHOME");
797 if (!kdeHomePathVar.isEmpty())
798 kdeDirs += kdeHomePathVar;
799
800 const QString kdeDirsVar = qEnvironmentVariable("KDEDIRS");
801 if (!kdeDirsVar.isEmpty())
802 kdeDirs += kdeDirsVar.split(u':', Qt::SkipEmptyParts);
803
804 const QString kdeVersionHomePath = QDir::homePath() + "/.kde"_L1 + QLatin1StringView(kdeVersionBA);
805 if (QFileInfo(kdeVersionHomePath).isDir())
806 kdeDirs += kdeVersionHomePath;
807
808 const QString kdeHomePath = QDir::homePath() + "/.kde"_L1;
809 if (QFileInfo(kdeHomePath).isDir())
810 kdeDirs += kdeHomePath;
811
812 const QString kdeRcPath = "/etc/kde"_L1 + QLatin1StringView(kdeVersionBA) + "rc"_L1;
813 if (QFileInfo(kdeRcPath).isReadable()) {
814 QSettings kdeSettings(kdeRcPath, QSettings::IniFormat);
815 kdeSettings.beginGroup(QStringLiteral("Directories-default"));
816 kdeDirs += kdeSettings.value(QStringLiteral("prefixes")).toStringList();
817 }
818
819 const QString kdeVersionPrefix = "/etc/kde"_L1 + QLatin1StringView(kdeVersionBA);
820 if (QFileInfo(kdeVersionPrefix).isDir())
821 kdeDirs += kdeVersionPrefix;
822
823 kdeDirs.removeDuplicates();
824 if (kdeDirs.isEmpty()) {
825 qWarning("Unable to determine KDE dirs");
826 return nullptr;
827 }
828
829 return new QKdeTheme(kdeDirs, kdeVersion);
830}
831
832#if QT_CONFIG(dbus)
833QPlatformMenuBar *QKdeTheme::createPlatformMenuBar() const
834{
835 if (isDBusGlobalMenuAvailable())
836 return new QDBusMenuBar();
837 return nullptr;
838}
839#endif
840
841#if QT_CONFIG(dbus) && QT_CONFIG(systemtrayicon)
842QPlatformSystemTrayIcon *QKdeTheme::createPlatformSystemTrayIcon() const
843{
844 if (shouldUseDBusTray())
845 return new QDBusTrayIcon();
846 return nullptr;
847}
848#endif
849
850QT_END_NAMESPACE
static QStringList kdeIconThemeSearchPaths(const QStringList &kdeDirs)
std::unique_ptr< QPalette > systemPalette
void updateColorScheme(const QString &themeName)
QKdeTheme::updateColorScheme - guess and set a color scheme for unix themes. KDE themes do not have a...
bool hasRequestedColorScheme() const
QStringList styleNames
Definition qkdetheme.cpp:98
QString iconThemeName
Definition qkdetheme.cpp:96
void clearKdeSettings() const
const QStringList kdeDirs
Definition qkdetheme.cpp:93
static std::unique_ptr< QFont > kdeFont(const QVariant &fontValue)
static QString kdeGlobals(const QString &kdeDir, int kdeVersion)
Definition qkdetheme.cpp:78
bool showIconsOnPushButtons
Qt::ColorScheme m_colorScheme
Qt::ColorScheme m_requestedColorScheme
QString iconFallbackThemeName
Definition qkdetheme.cpp:97
QVariant readKdeSetting(KdeSetting s) const
static QVariant readKdeSetting(KdeSetting s, const QStringList &kdeDirs, int kdeVersion, QHash< QString, QSettings * > &settings)
const int kdeVersion
Definition qkdetheme.cpp:94
QKdeThemePrivate(const QStringList &kdeDirs, int kdeVersion)
static void readKdeSystemPalette(const QStringList &kdeDirs, int kdeVersion, QHash< QString, QSettings * > &kdeSettings, QPalette *pal)
Combined button and popup list for selecting options.
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
static constexpr QLatin1StringView settingsKey(QKdeThemePrivate::KdeSetting setting)
static constexpr QKdeThemePrivate::KdeSettingType settingsType(QKdeThemePrivate::KdeSetting setting)
static constexpr QLatin1StringView settingsPrefix(QKdeThemePrivate::KdeSettingType type)
static bool kdeColor(QPalette *pal, QPalette::ColorRole role, const QVariant &value)
#define CASE(mod, type)