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
qgtk3interface_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 QGTK3INTERFACE_H
6#define QGTK3INTERFACE_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
19#include <QtCore/QString>
20#include <QtCore/QCache>
21#include <private/qflatmap_p.h>
22#include <QtCore/QObject>
23#include <QtGui/QIcon>
24#include <QtGui/QPalette>
25#include <QtWidgets/QWidget>
26#include <QtCore/QLoggingCategory>
27#include <QtGui/QPixmap>
28#include <qpa/qplatformtheme.h>
29
30#undef signals // Collides with GTK symbols
31#include <gtk/gtk.h>
32#include <gdk/gdk.h>
33#include <glib.h>
34
36
38
39using namespace Qt::StringLiterals;
40
41class QGtk3Storage;
42
43/*!
44 \internal
45 \brief The QGtk3Interface class centralizes communication with the GTK3 library.
46
47 By encapsulating all GTK version specific syntax and conversions, it makes Qt's GTK theme
48 independent from GTK versions.
49
50 \note
51 Including GTK3 headers requires #undef signals, which disables Qt signal/slot handling.
52 */
53
55{
56 Q_GADGET
57 Q_DISABLE_COPY_MOVE(QGtk3Interface)
58public:
61
62 /*!
63 * \internal
64 \enum QGtk3Interface::QGtkWidget
65 \brief Represents GTK widget types used to obtain color information.
66
67 \note The enum value gtk_Default refers to the GTK default style, rather than to a specific widget.
68 */
92
93 /*!
94 \internal
95 \enum QGtk3Interface::QGtkColorSource
96 \brief The QGtkColorSource enum represents the source of a color within a GTK widgets style context.
97
98 If the current GTK theme provides such a color for a given widget, the color can be read
99 from the style context by passing the enum's key as a property name to the GTK method
100 gtk_style_context_lookup_color. The method will return false, if no color has been found.
101 */
109 Q_ENUM(QGtkColorSource)
110
111 /*!
112 \internal
113 \enum QGtk3Interface::QGtkColorDefault
114 \brief The QGtkColorDefault enum represents generic GTK colors.
115
116 The GTK3 methods gtk_style_context_get_color, gtk_style_context_get_background_color, and
117 gtk_style_context_get_foreground_color always return the respective colors with a widget's
118 style context. Unless set as a property by the current GTK theme, GTK's default colors will
119 be returned.
120 These generic default colors, represented by the GtkColorDefault enum, are used as a
121 back, if a specific color property is requested but not defined in the current GTK theme.
122 */
128 Q_ENUM(QGtkColorDefault)
129
130 // Create a brush from GTK widget type, color source and color state
132
133 // Font & icon getters
134 QImage standardPixmap(QPlatformTheme::StandardPixmap standardPixmap) const;
135 QFont font(QPlatformTheme::Font type) const;
136 QIcon fileIcon(const QFileInfo &fileInfo) const;
137
138 // Return current GTK theme name
139 QString themeName() const;
140
141 // Derive color scheme from default colors
143
144 // Convert GTK state to/from string
145 static int toGtkState(const QString &state);
146 static const QLatin1String fromGtkState(GtkStateFlags state);
147
148private:
149
150 // Map colors to GTK property names and default to generic color getters
151 struct ColorKey {
152 QGtkColorSource colorSource = QGtkColorSource::Background;
153 GtkStateFlags state = GTK_STATE_FLAG_NORMAL;
154
155 // struct becomes key of a map, so operator< is needed
156 bool operator<(const ColorKey& other) const {
157 return std::tie(colorSource, state) <
158 std::tie(other.colorSource, other.state);
159 }
160
161 QDebug operator<<(QDebug dbg)
162 {
163 return dbg << "QGtk3Interface::ColorKey(colorSource=" << colorSource << ", GTK state=" << fromGtkState(state) << ")";
164 }
165 };
166
167 struct ColorValue {
168 QString propertyName = QString();
169 QGtkColorDefault genericSource = QGtkColorDefault::Background;
170
171 QDebug operator<<(QDebug dbg)
172 {
173 return dbg << "QGtk3Interface::ColorValue(propertyName=" << propertyName << ", genericSource=" << genericSource << ")";
174 }
175 };
176
177 typedef QFlatMap<ColorKey, ColorValue> ColorMap;
178 ColorMap gtkColorMap;
179 void initColorMap();
180
181 GdkRGBA genericColor(GtkStyleContext *con, GtkStateFlags state, QGtkColorDefault def) const;
182
183 // Cache for GTK widgets
184 mutable QFlatMap<QGtkWidget, GtkWidget *> cache;
185
186 // Converters for GTK icon and GDK pixbuf
187 QImage qt_gtk_get_icon(const char *iconName) const;
188 QImage qt_convert_gdk_pixbuf(GdkPixbuf *buf) const;
189
190 // Create new GTK widget object
191 GtkWidget *qt_new_gtkWidget(QGtkWidget type) const;
192
193 // Deliver GTK Widget from cache or create new
194 GtkWidget *widget(QGtkWidget type) const;
195
196 // Get a GTK widget's style context. Default settings style context if nullptr
197 GtkStyleContext *context(GtkWidget *widget = nullptr) const;
198
199 // Convert GTK color into QColor
200 static inline QColor fromGdkColor (const GdkRGBA &c)
201 { return QColor::fromRgbF(c.red, c.green, c.blue, c.alpha); }
202
203 // get a QColor of a GTK widget (default settings style if nullptr)
204 QColor color (GtkWidget *widget, QGtkColorSource source, GtkStateFlags state) const;
205
206 // Mappings for GTK fonts
207 inline static constexpr QGtkWidget toWidgetType(QPlatformTheme::Font);
208 inline static constexpr QFont::Style toFontStyle(PangoStyle style);
209 inline static constexpr int toFontWeight(PangoWeight weight);
210
211};
212QT_END_NAMESPACE
213#endif // QGTK3INTERFACE_H
The QGtk3Interface class centralizes communication with the GTK3 library.
QIcon fileIcon(const QFileInfo &fileInfo) const
Returns a GTK styled file icon for.
QFont font(QPlatformTheme::Font type) const
Return a GTK styled font.
Qt::ColorScheme colorSchemeByColors() const
Determine color scheme by colors.
QImage standardPixmap(QPlatformTheme::StandardPixmap standardPixmap) const
Returns a QImage corresponding to.
QString themeName() const
Returns the name of the current GTK theme.
static const QLatin1String fromGtkState(GtkStateFlags state)
Returns.
static int toGtkState(const QString &state)
Converts a string into the GtkStateFlags enum.
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
QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(lcEventDispatcher)
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