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
qgtk3storage_p.h
Go to the documentation of this file.
1// Copyright (C) 2022 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#ifndef QGTK3STORAGE_P_H
6#define QGTK3STORAGE_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
20
21#include <QtCore/QJsonDocument>
22#include <QtCore/QCache>
23#include <QtCore/QString>
24#include <QtGui/QGuiApplication>
25#include <QtGui/QPalette>
26
27#include <qpa/qplatformtheme.h>
28#include <private/qflatmap_p.h>
29
31
32#if QT_CONFIG(dbus)
33class QGnomePortalInterface;
34#endif
35
37{
39public:
41 ~QGtk3Storage();
42
43 // Enum documented in cpp file. Please keep it in line with updates made here.
52
53 // Standard GTK source: Populate a brush from GTK
54 struct Gtk3Source {
58 int width = -1;
59 int height = -1;
61 {
62 return dbg << "QGtkStorage::Gtk3Source(gtkwidgetType=" << gtkWidgetType << ", source="
63 << source << ", state=" << state << ", width=" << width << ", height="
64 << height << ")";
65 }
66 };
67
68 // Recursive source: Populate a brush by altering another source
73 int lighter = 100;
74 int deltaRed = 0;
75 int deltaGreen = 0;
76 int deltaBlue = 0;
77 int width = -1;
78 int height = -1;
79 QDebug operator<<(QDebug dbg)
80 {
81 return dbg << "QGtkStorage::RecursiceSource(colorGroup=" << colorGroup << ", colorRole="
82 << colorRole << ", colorScheme=" << colorScheme << ", lighter=" << lighter
83 << ", deltaRed="<< deltaRed << "deltaBlue =" << deltaBlue << "deltaGreen="
84 << deltaGreen << ", width=" << width << ", height=" << height << ")";
85 }
86 };
87
88 // Mixed source: Populate a brush by mixing two brushes.
89 // Useful for creating disabled color by mixing,
90 // for example the background and foreground colors.
91 struct MixSources {
92 QPalette::ColorGroup sourceGroup; // source group of the mixing color roles
95 QDebug operator<<(QDebug dbg)
96 {
97 return dbg << "QGtkStorage::MixSources(sourceGroup=" << sourceGroup
98 << ", colorRole1=" << colorRole1
99 << ", colorRole2=" << colorRole2 << ")";
100 }
101 static inline QColor mixColors(const QColor &color1, const QColor &color2)
102 {
103 return QColor{ (color1.red() + color2.red()) / 2,
104 (color1.green() + color2.green()) / 2,
105 (color1.blue() + color2.blue()) / 2 };
106 }
107 };
108
109 // Fixed source: Populate a brush with fixed values rather than reading GTK
110 struct FixedSource {
112 QDebug operator<<(QDebug dbg)
113 {
114 return dbg << "QGtkStorage::FixedSource(" << fixedBrush << ")";
115 }
116 };
117
118 // Data source for brushes
119 struct Source {
125
126 // GTK constructor
127 Source(QGtk3Interface::QGtkWidget wtype, QGtk3Interface::QGtkColorSource csource,
128 GtkStateFlags cstate, int bwidth = -1, int bheight = -1) : sourceType(SourceType::Gtk)
129 {
130 gtk3.gtkWidgetType = wtype;
131 gtk3.source = csource;
132 gtk3.state = cstate;
133 gtk3.width = bwidth;
134 gtk3.height = bheight;
135 }
136
137 // Recursive constructor for darker/lighter colors
138 Source(QPalette::ColorGroup group, QPalette::ColorRole role,
139 Qt::ColorScheme scheme, int p_lighter = 100)
141 {
142 rec.colorGroup = group;
143 rec.colorRole = role;
144 rec.colorScheme = scheme;
145 rec.lighter = p_lighter;
146 }
147
148 // Recursive constructor for color modification
149 Source(QPalette::ColorGroup group, QPalette::ColorRole role,
150 Qt::ColorScheme scheme, int p_red, int p_green, int p_blue)
152 {
153 rec.colorGroup = group;
154 rec.colorRole = role;
155 rec.colorScheme = scheme;
156 rec.deltaRed = p_red;
157 rec.deltaGreen = p_green;
158 rec.deltaBlue = p_blue;
159 }
160
161 // Recursive constructor for all: color modification and darker/lighter
162 Source(QPalette::ColorGroup group, QPalette::ColorRole role,
163 Qt::ColorScheme scheme, int p_lighter,
164 int p_red, int p_green, int p_blue) : sourceType(SourceType::Modified)
165 {
166 rec.colorGroup = group;
167 rec.colorRole = role;
168 rec.colorScheme = scheme;
169 rec.lighter = p_lighter;
170 rec.deltaRed = p_red;
171 rec.deltaGreen = p_green;
172 rec.deltaBlue = p_blue;
173 }
174
175 // Mixed constructor for color modification
176 Source(QPalette::ColorGroup sourceGroup,
177 QPalette::ColorRole role1, QPalette::ColorRole role2)
179 {
180 mix.sourceGroup = sourceGroup;
181 mix.colorRole1 = role1;
182 mix.colorRole2 = role2;
183 }
184
185 // Fixed Source constructor
186 Source(const QBrush &brush) : sourceType(SourceType::Fixed)
187 {
188 fix.fixedBrush = brush;
189 }
190
191 // Invalid constructor and getter
193 bool isValid() const { return sourceType != SourceType::Invalid; }
194
195 // Debug
196 QDebug operator<<(QDebug dbg)
197 {
198 return dbg << "QGtk3Storage::Source(sourceType=" << sourceType << ")";
199 }
200 };
201
202 // Struct with key attributes to identify a brush: color group, color role and color scheme
203 struct TargetBrush {
207
208 // Generic constructor
209 TargetBrush(QPalette::ColorGroup group, QPalette::ColorRole role,
210 Qt::ColorScheme scheme = Qt::ColorScheme::Unknown) :
212
213 // Copy constructor with color scheme modifier for dark/light aware search
214 TargetBrush(const TargetBrush &other, Qt::ColorScheme scheme) :
216
217 // struct becomes key of a map, so operator< is needed
218 bool operator<(const TargetBrush& other) const {
219 return std::tie(colorGroup, colorRole, colorScheme) <
220 std::tie(other.colorGroup, other.colorRole, other.colorScheme);
221 }
222 };
223
224 // Mapping a palette's brushes to their GTK sources
226
227 // Storage of palettes and their GTK sources
229
230 // Public getters
231 const QPalette *palette(QPlatformTheme::Palette = QPlatformTheme::SystemPalette) const;
232 QPixmap standardPixmap(QPlatformTheme::StandardPixmap standardPixmap, const QSizeF &size) const;
233 Qt::ColorScheme colorScheme() const { return m_colorScheme; }
234 static QPalette standardPalette();
235 const QString themeName() const { return m_interface ? m_interface->themeName() : QString(); }
236 const QFont *font(QPlatformTheme::Font type) const;
237 QIcon fileIcon(const QFileInfo &fileInfo) const;
238
239 // Initialization
240 void populateMap();
241 void handleThemeChange();
242
243private:
244 // Storage for palettes and their brushes
245 PaletteMap m_palettes;
246
247 std::unique_ptr<QGtk3Interface> m_interface;
248#if QT_CONFIG(dbus)
250#endif
251
252 Qt::ColorScheme m_colorScheme = Qt::ColorScheme::Unknown;
253
254 // Caches for Pixmaps, fonts and palettes
255 mutable QCache<QPlatformTheme::StandardPixmap, QImage> m_pixmapCache;
258
259 // Search brush with a given GTK3 source
260 QBrush brush(const Source &source, const BrushMap &map) const;
261
262 // Get GTK3 source for a target brush
263 Source brush (const TargetBrush &brush, const BrushMap &map) const;
264
265 // clear cache, palettes and color scheme
266 void clear();
267
268 // Data creation, import & export
269 void createMapping ();
270 const PaletteMap savePalettes() const;
271 bool save(const QString &filename, const QJsonDocument::JsonFormat f = QJsonDocument::Indented) const;
272 QJsonDocument save() const;
273 bool load(const QString &filename);
274};
275
276QT_END_NAMESPACE
277#endif // QGTK3STORAGE_H
The QGtk3Interface class centralizes communication with the GTK3 library.
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
static QPalette standardPalette()
Returns a simple, hard coded base palette.
const QString themeName() const
Qt::ColorScheme colorScheme() const
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.
QIcon fileIcon(const QFileInfo &fileInfo, QPlatformTheme::IconOptions iconOptions={ }) const override
Return an icon for fileInfo, observing iconOptions.
Qt::ColorScheme colorScheme() const override
bool usePlatformNativeDialog(DialogType type) const override
virtual QVariant themeHint(ThemeHint hint) const override
void requestColorScheme(Qt::ColorScheme scheme) override
QPlatformDialogHelper * createPlatformDialogHelper(DialogType type) const override
static const char * name
Definition qgtk3theme.h:34
const QPalette * palette(Palette type=SystemPalette) const override
Return a color palette for type type.
virtual QString gtkFontName() const override
QPixmap standardPixmap(StandardPixmap sp, const QSizeF &size) const override
Return a pixmap for standardPixmap, at the given size.
const QFont * font(Font type=SystemFont) const override
QDebug operator<<(QDebug dbg)
QPalette::ColorRole colorRole2
QDebug operator<<(QDebug dbg)
QPalette::ColorGroup sourceGroup
QPalette::ColorRole colorRole1
static QColor mixColors(const QColor &color1, const QColor &color2)
QDebug operator<<(QDebug dbg)
QPalette::ColorRole colorRole
QPalette::ColorGroup colorGroup
Source(const QBrush &brush)
RecursiveSource rec
Source(QPalette::ColorGroup group, QPalette::ColorRole role, Qt::ColorScheme scheme, int p_lighter, int p_red, int p_green, int p_blue)
Source(QPalette::ColorGroup group, QPalette::ColorRole role, Qt::ColorScheme scheme, int p_red, int p_green, int p_blue)
Source(QGtk3Interface::QGtkWidget wtype, QGtk3Interface::QGtkColorSource csource, GtkStateFlags cstate, int bwidth=-1, int bheight=-1)
Source(QPalette::ColorGroup group, QPalette::ColorRole role, Qt::ColorScheme scheme, int p_lighter=100)
QDebug operator<<(QDebug dbg)
Source(QPalette::ColorGroup sourceGroup, QPalette::ColorRole role1, QPalette::ColorRole role2)
QPalette::ColorRole colorRole
QPalette::ColorGroup colorGroup
TargetBrush(QPalette::ColorGroup group, QPalette::ColorRole role, Qt::ColorScheme scheme=Qt::ColorScheme::Unknown)
TargetBrush(const TargetBrush &other, Qt::ColorScheme scheme)
bool operator<(const TargetBrush &other) const