18#include <qpa/qwindowsysteminterface.h>
20# include <QtGui/private/qgnomeportalinterface_p.h>
25QGtk3Storage::QGtk3Storage()
27 m_interface.reset(
new QGtk3Interface(
this));
29 m_portalInterface.reset(
new QGnomePortalInterface);
37
38
39
40
41
42
43
44
45
48
49
50
51
52
53
54
55
60 return m_interface ? QBrush(m_interface->brush(source.gtk3.gtkWidgetType,
61 source.gtk3.source, source.gtk3.state))
67 source.rec.colorScheme), map);
73 QBrush b = brush(recSource, map);
74 if (source.rec.width > 0 && source.rec.height > 0)
75 b.setTexture(QPixmap(source.rec.width, source.rec.height));
76 QColor c = b.color().lighter(source.rec.lighter);
77 c = QColor((c.red() + source.rec.deltaRed),
78 (c.green() + source.rec.deltaGreen),
79 (c.blue() + source.rec.deltaBlue));
86 constexpr auto check_source = [](
const Source &source) ->
bool
92 source.mix.colorRole1), map);
93 if (!check_source(source1))
97 source.mix.colorRole2), map);
98 if (!check_source(source2))
101 const QBrush brush2 = brush(source2, map);
103 QBrush brush1 = brush(source1, map);
105 brush1.setColor(MixSources::mixColors(brush1.color(), brush2.color()));
110 return source.fix.fixedBrush;
121
122
123
124
125
126
127
130#define FIND(brush) if (map.contains(brush))
131 return map.value(brush)
137 if (b.colorScheme == Qt::ColorScheme::Unknown) {
138 FIND(TargetBrush(b, Qt::ColorScheme::Dark));
139 FIND(TargetBrush(b, Qt::ColorScheme::Light));
143 if (b.colorGroup != QPalette::All)
144 return brush(TargetBrush(QPalette::All, b.colorRole, b.colorScheme), map);
152
153
154
155
156
157
158
159
160
163 QColor backgroundColor(0xd4, 0xd0, 0xc8);
164 QColor lightColor(backgroundColor.lighter());
165 QColor darkColor(backgroundColor.darker());
166 const QBrush darkBrush(darkColor);
167 QColor midColor(Qt::gray);
168 QPalette palette(Qt::black, backgroundColor, lightColor, darkColor,
169 midColor, Qt::black, Qt::white);
170 palette.setBrush(QPalette::Disabled, QPalette::WindowText, darkBrush);
171 palette.setBrush(QPalette::Disabled, QPalette::Text, darkBrush);
172 palette.setBrush(QPalette::Disabled, QPalette::ButtonText, darkBrush);
173 palette.setBrush(QPalette::Disabled, QPalette::Base, QBrush(backgroundColor));
178
179
180
181
182
183
186 if (type >= QPlatformTheme::NPalettes)
189 if (m_paletteCache[type].has_value()) {
190 qCDebug(lcQGtk3Interface) <<
"Returning palette from cache:"
191 << QGtk3Json::fromPalette(type);
193 return &m_paletteCache[type].value();
197 if (!m_paletteCache[QPlatformTheme::SystemPalette].has_value() && type != QPlatformTheme::SystemPalette)
201 if (!m_palettes.contains(type) && type != QPlatformTheme::SystemPalette) {
202 qCDebug(lcQGtk3Interface) <<
"Returning system palette for unknown type"
203 << QGtk3Json::fromPalette(type);
207 BrushMap brushes = m_palettes.value(type);
210 QPalette p = QPalette( type == QPlatformTheme::SystemPalette ? standardPalette()
211 : m_paletteCache[QPlatformTheme::SystemPalette].value());
213 qCDebug(lcQGtk3Interface) <<
"Creating palette:" << QGtk3Json::fromPalette(type);
214 for (
auto i = brushes.begin(); i != brushes.end(); ++i) {
215 Source source = i.value();
220 const auto appSource = i.key().colorScheme;
221 const auto appTheme = colorScheme();
222 const bool setBrush = (appSource == appTheme) ||
223 (appSource == Qt::ColorScheme::Unknown) ||
224 (appTheme == Qt::ColorScheme::Unknown);
227 p.setBrush(i.key().colorGroup, i.key().colorRole, brush(source, brushes));
231 m_paletteCache[type].emplace(p);
232 if (type == QPlatformTheme::SystemPalette)
233 qCDebug(lcQGtk3Interface) <<
"System Palette defined" << themeName() << colorScheme() << p;
235 return &m_paletteCache[type].value();
239
240
241
242
243
246 if (m_fontCache[type].has_value())
247 return &m_fontCache[type].value();
249 m_fontCache[type].emplace(m_interface->font(type));
250 return &m_fontCache[type].value();
254
255
256
257
258
259
261 const QSizeF &size)
const
263 if (m_pixmapCache.contains(standardPixmap))
264 return QPixmap::fromImage(m_pixmapCache.object(standardPixmap)->scaled(size.toSize()));
269 QImage image = m_interface->standardPixmap(standardPixmap);
273 m_pixmapCache.insert(standardPixmap,
new QImage(image));
274 return QPixmap::fromImage(image.scaled(size.toSize()));
278
279
280
283 return m_interface ? m_interface->fileIcon(fileInfo) : QIcon();
287
288
289
292 m_colorScheme = Qt::ColorScheme::Unknown;
294 for (
auto &cache : m_paletteCache)
297 for (
auto &cache : m_fontCache)
302
303
304
305
306
307
311 QWindowSystemInterface::handleThemeChange();
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
364 static QString m_themeName;
367 Qt::ColorScheme newColorScheme = Qt::ColorScheme::Unknown;
368 const QString newThemeName = themeName();
373 newColorScheme = m_portalInterface->colorScheme();
376 if (newColorScheme == Qt::ColorScheme::Unknown) {
378 newColorScheme = newThemeName.contains(
"dark"_L1, Qt::CaseInsensitive)
379 ? Qt::ColorScheme::Dark : m_interface->colorSchemeByColors();
382 if (m_themeName == newThemeName && m_colorScheme == newColorScheme)
387 if (m_themeName.isEmpty()) {
388 qCDebug(lcQGtk3Interface) <<
"GTK theme initialized:" << newThemeName << newColorScheme;
390 qCDebug(lcQGtk3Interface) <<
"GTK theme changed to:" << newThemeName << newColorScheme;
392 m_colorScheme = newColorScheme;
393 m_themeName = newThemeName;
396 const QString jsonInput = qEnvironmentVariable(
"QT_GUI_GTK_JSON");
397 if (!jsonInput.isEmpty()) {
398 if (load(jsonInput)) {
401 qWarning() <<
"Falling back to standard GTK mapping.";
407 const QString jsonOutput = qEnvironmentVariable(
"QT_GUI_GTK_JSON_SAVE");
408 if (!jsonOutput.isEmpty() && !save(jsonOutput))
409 qWarning() <<
"File" << jsonOutput <<
"could not be saved.\n";
413
414
415
416
417
418
419
420
423 const QString hard = qEnvironmentVariable(
"QT_GUI_GTK_JSON_HARDCODED");
424 if (!hard.contains(
"true"_L1, Qt::CaseInsensitive))
430 for (
auto paletteIterator = map.begin(); paletteIterator != map.end();
433 for (
auto brushIterator = bm.begin(); brushIterator != bm.end();
440 const QBrush fixedBrush = brush(s, bm);
441 s.fix.fixedBrush = fixedBrush;
457
458
459
460
461
462
463
464bool QGtk3Storage::save(
const QString &filename, QJsonDocument::JsonFormat f)
const
466 return QGtk3Json::save(savePalettes(), filename, f);
470
471
472
473
474
475
476
479 return QGtk3Json::save(savePalettes());
483
484
485
486
487
490 return QGtk3Json::load(m_palettes, filename);
494
495
496
497
498
499
507#define GTK(wtype, colorSource, state)
508 source = Source(QGtk3Interface::QGtkWidget::gtk_ ##wtype,
509 QGtk3Interface::QGtkColorSource::colorSource, GTK_STATE_FLAG_ ##state)
512#define LIGHTER(group, role, lighter)
513 source = Source(QPalette::group, QPalette::role,
514 Qt::ColorScheme::Unknown, lighter)
515#define MODIFY(group, role, red, green, blue)
516 source = Source(QPalette::group, QPalette::role,
517 Qt::ColorScheme::Unknown, red, green, blue)
520#define FIX(color) source = FixedSource(color);
524#define ADD_2(group, role) map.insert(TargetBrush(QPalette::group, QPalette::role), source);
525#define ADD_3(group, role, app) map.insert(TargetBrush(QPalette::group, QPalette::role,
526 Qt::ColorScheme::app), source);
527#define ADD_X(x, group, role, app, FUNC, ...) FUNC
528#define ADD(...) ADD_X(,##__VA_ARGS__, ADD_3(__VA_ARGS__), ADD_2(__VA_ARGS__))
530#define SAVE(palette) m_palettes.insert(QPlatformTheme::palette, map)
532#define CLEAR map.clear()
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
574 GTK(Default, Background, INSENSITIVE);
580 ADD(Inactive, Light);
587 GTK(button, Foreground, ACTIVE);
588 ADD(Inactive, WindowText);
590 auto ADD_MIX = [&map](QPalette::ColorGroup targetGroup,
591 QPalette::ColorRole targetRole,
592 QPalette::ColorGroup sourceGroup,
593 QPalette::ColorRole role1,
594 QPalette::ColorRole role2)
596 const Source source{sourceGroup, role1, role2};
597 map.insert(
TargetBrush(targetGroup, targetRole), source);
599 ADD_MIX(QPalette::Disabled, QPalette::Text,
600 QPalette::Normal, QPalette::Base, QPalette::Text);
601 ADD_MIX(QPalette::Disabled, QPalette::WindowText,
602 QPalette::Normal, QPalette::Window, QPalette::WindowText);
603 ADD_MIX(QPalette::Disabled, QPalette::ButtonText,
604 QPalette::Normal, QPalette::Button, QPalette::ButtonText);
606 GTK(button, Text, NORMAL);
607 ADD(Inactive, ButtonText);
610 GTK(Default, Background, SELECTED);
611 ADD(Disabled, Highlight);
612 ADD(Normal, Highlight);
613 ADD(Inactive, Highlight);
615 GTK(entry, Foreground, SELECTED);
616 ADD(Normal, HighlightedText);
617 ADD(Inactive, HighlightedText);
620 GTK(entry, Text, NORMAL);
621 ADD(Normal, ButtonText);
622 ADD(Normal, WindowText);
623 ADD(Disabled, HighlightedText);
625 GTK(Default, Text, NORMAL);
628 ADD(Normal, HighlightedText);
630 ADD(All, AlternateBase);
632 GTK(Default, Foreground, NORMAL);
633 MODIFY(Normal, Text, 100, 100, 100);
634 ADD(All, PlaceholderText, Light);
635 MODIFY(Normal, Text, -100, -100, -100);
636 ADD(All, PlaceholderText, Dark);
656 GTK(entry, Text, NORMAL);
657 ADD(Normal, WindowText);
658 ADD(Inactive, WindowText);
667 GTK(button, Text, ACTIVE);
668 ADD(Normal, Base, Dark);
669 ADD(Inactive, WindowText, Dark);
671 GTK(Default, Foreground, NORMAL);
674 GTK(Default, Background, NORMAL);
677 GTK(button, Text, NORMAL);
678 ADD(Normal, Base, Light);
679 ADD(Inactive, WindowText, Light);
681 SAVE(CheckBoxPalette);
682 SAVE(RadioButtonPalette);
688 GTK(combo_box, Text, NORMAL);
689 ADD(Normal, ButtonText, Dark);
690 ADD(Normal, Text, Dark);
691 ADD(Inactive, WindowText, Dark);
693 GTK(combo_box, Text, ACTIVE);
694 ADD(Normal, ButtonText, Light);
695 ADD(Normal, Text, Light);
696 ADD(Inactive, WindowText, Light);
698 SAVE(ComboBoxPalette);
699 SAVE(GroupBoxPalette);
705 GTK(Default, Text, ACTIVE);
706 ADD(Normal, ButtonText);
713 GTK(Default, Background, NORMAL);
715 SAVE(TextLineEditPalette);
void handleThemeChange()
Handles a theme change at runtime.
const QPalette * palette(QPlatformTheme::Palette=QPlatformTheme::SystemPalette) const
Return a GTK styled QPalette.
QFlatMap< QPlatformTheme::Palette, BrushMap > PaletteMap
QFlatMap< TargetBrush, Source > BrushMap
QIcon fileIcon(const QFileInfo &fileInfo) const
Returns a GTK styled file icon corresponding to.
void populateMap()
Populates a map with information about how to locate colors in GTK.
SourceType
This enum represents the type of a color source.
const QFont * font(QPlatformTheme::Font type) const
Return a GTK styled font.
QPixmap standardPixmap(QPlatformTheme::StandardPixmap standardPixmap, const QSizeF &size) const
Return a GTK styled standard pixmap if available.
#define SAVE(src, state, prop, def)
#define GTK(wtype, colorSource, state)
#define LIGHTER(group, role, lighter)
#define MODIFY(group, role, red, green, blue)