Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qgtk3json.cpp
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
4//
5// W A R N I N G
6// -------------
7//
8// This file is not part of the Qt API. It exists purely as an
9// implementation detail. This header file may change from version to
10// version without notice, or even be removed.
11//
12// We mean it.
13//
14
15#include "qgtk3json_p.h"
16#include <QtCore/QFile>
17#include <QMetaEnum>
18
20
22{
23 return QLatin1String(QMetaEnum::fromType<QPlatformTheme::Palette>().valueToKey(static_cast<int>(palette)));
24}
25
30
32{
33 return QLatin1String(QByteArray(color.name(QColor::HexRgb).toLatin1()));
34}
35
37{
38 return QLatin1String(QMetaEnum::fromType<QPalette::ColorRole>().valueToKey(static_cast<int>(role)));
39}
40
42{
43 return QLatin1String(QMetaEnum::fromType<QPalette::ColorGroup>().valueToKey(static_cast<int>(group)));
44}
45
47{
48 return QLatin1String(QMetaEnum::fromType<QGtk3Interface::QGtkColorSource>().valueToKey(static_cast<int>(source)));
49}
50
52{
53 return QLatin1String(QMetaEnum::fromType<QGtk3Interface::QGtkWidget>().valueToKey(static_cast<int>(widgetType)));
54}
55
57{
58 return QLatin1String(QMetaEnum::fromType<Qt::ColorScheme>().valueToKey(static_cast<int>(app)));
59}
60
61#define CONVERT(type, key, def)\
62 bool ok;\
63 const int intVal = QMetaEnum::fromType<type>().keyToValue(key.toLatin1().constData(), &ok);\
64 return ok ? static_cast<type>(intVal) : type::def
65
67{
68 CONVERT(Qt::ColorScheme, colorScheme, Unknown);
69}
70
75
76GtkStateFlags QGtk3Json::toGtkState(const QString &type)
77{
79 if (i < 0)
80 return GTK_STATE_FLAG_NORMAL;
81 return static_cast<GtkStateFlags>(i);
82}
83
88
93
98
103
105{
106 return QLatin1String(QMetaEnum::fromType<QGtk3Storage::SourceType>().valueToKey(static_cast<int>(sourceType)));
107}
108
113
115{
116 CONVERT(QGtk3Interface::QGtkWidget, widgetType, gtk_offscreen_window);
117}
118
119#undef CONVERT
120
123{
124 QJsonDocument doc = save(map);
125 if (doc.isEmpty()) {
126 qWarning() << "Nothing to save to" << fileName;
127 return false;
128 }
129
132 qWarning() << "Unable to open file" << fileName << "for writing.";
133 return false;
134 }
135
136 if (!file.write(doc.toJson(format))) {
137 qWarning() << "Unable to serialize Json document.";
138 return false;
139 }
140
141 file.close();
142 qInfo() << "Saved mapping data to" << fileName;
143 return true;
144}
145
147{
148 QJsonObject paletteObject;
149 for (auto paletteIterator = map.constBegin(); paletteIterator != map.constEnd();
150 ++paletteIterator) {
151 const QGtk3Storage::BrushMap &bm = paletteIterator.value();
152 QFlatMap<QPalette::ColorRole, QGtk3Storage::BrushMap> brushMaps;
153 for (auto brushIterator = bm.constBegin(); brushIterator != bm.constEnd();
154 ++brushIterator) {
155 const QPalette::ColorRole role = brushIterator.key().colorRole;
156 if (brushMaps.contains(role)) {
157 brushMaps.value(role).insert(brushIterator.key(), brushIterator.value());
158 } else {
160 newMap.insert(brushIterator.key(), brushIterator.value());
161 brushMaps.insert(role, newMap);
162 }
163 }
164
165 QJsonObject brushArrayObject;
166 for (auto brushMapIterator = brushMaps.constBegin();
167 brushMapIterator != brushMaps.constEnd(); ++brushMapIterator) {
168
169 QJsonArray brushArray;
170 int brushIndex = 0;
171 const QGtk3Storage::BrushMap &bm = brushMapIterator.value();
172 for (auto brushIterator = bm.constBegin(); brushIterator != bm.constEnd();
173 ++brushIterator) {
174 QJsonObject brushObject;
175 const QGtk3Storage::TargetBrush tb = brushIterator.key();
176 QGtk3Storage::Source s = brushIterator.value();
177 brushObject.insert(ceColorGroup, fromColorGroup(tb.colorGroup));
178 brushObject.insert(ceColorScheme, fromColorScheme(tb.colorScheme));
179 brushObject.insert(ceSourceType, fromSourceType(s.sourceType));
180
181 QJsonObject sourceObject;
182 switch (s.sourceType) {
184 sourceObject.insert(ceGtkWidget, fromWidgetType(s.gtk3.gtkWidgetType));
185 sourceObject.insert(ceGdkSource, fromGdkSource(s.gtk3.source));
186 sourceObject.insert(ceGtkState, fromGtkState(s.gtk3.state));
187 sourceObject.insert(ceWidth, s.gtk3.width);
188 sourceObject.insert(ceHeight, s.gtk3.height);
189 }
190 break;
191
193 QJsonObject fixedObject;
194 fixedObject.insert(ceColor, s.fix.fixedBrush.color().name());
195 fixedObject.insert(ceWidth, s.fix.fixedBrush.texture().width());
196 fixedObject.insert(ceHeight, s.fix.fixedBrush.texture().height());
197 sourceObject.insert(ceBrush, fixedObject);
198 }
199 break;
200
202 sourceObject.insert(ceColorGroup, fromColorGroup(s.rec.colorGroup));
203 sourceObject.insert(ceColorRole, fromColorRole(s.rec.colorRole));
204 sourceObject.insert(ceColorScheme, fromColorScheme(s.rec.colorScheme));
205 sourceObject.insert(ceRed, s.rec.deltaRed);
206 sourceObject.insert(ceGreen, s.rec.deltaGreen);
207 sourceObject.insert(ceBlue, s.rec.deltaBlue);
208 sourceObject.insert(ceWidth, s.rec.width);
209 sourceObject.insert(ceHeight, s.rec.height);
210 sourceObject.insert(ceLighter, s.rec.lighter);
211 }
212 break;
213
215 break;
216 }
217
218 brushObject.insert(ceData, sourceObject);
219 brushArray.insert(brushIndex, brushObject);
220 ++brushIndex;
221 }
222 brushArrayObject.insert(fromColorRole(brushMapIterator.key()), brushArray);
223 }
224 paletteObject.insert(fromPalette(paletteIterator.key()), brushArrayObject);
225 }
226
228 top.insert(cePalettes, paletteObject);
229 return paletteObject.keys().isEmpty() ? QJsonDocument() : QJsonDocument(top);
230}
231
233{
236 qCWarning(lcQGtk3Interface) << "Unable to open file:" << fileName;
237 return false;
238 }
239
240 QJsonParseError err;
242 if (err.error != QJsonParseError::NoError) {
243 qWarning(lcQGtk3Interface) << "Unable to parse Json document from" << fileName
244 << err.error << err.errorString();
245 return false;
246 }
247
248 if (Q_LIKELY(load(map, doc))) {
249 qInfo() << "GTK mapping successfully imported from" << fileName;
250 return true;
251 }
252
253 qWarning() << "File" << fileName << "could not be loaded.";
254 return false;
255}
256
258{
259#define GETSTR(obj, key)\
260 if (!obj.contains(key)) {\
261 qCInfo(lcQGtk3Interface) << key << "missing for palette" << paletteName\
262 << ", Brush" << colorRoleName;\
263 return false;\
264 }\
265 value = obj[key].toString()
266
267#define GETINT(obj, key, var) GETSTR(obj, key);\
268 if (!obj[key].isDouble()) {\
269 qCInfo(lcQGtk3Interface) << key << "type mismatch" << value\
270 << "is not an integer!"\
271 << "(Palette" << paletteName << "), Brush" << colorRoleName;\
272 return false;\
273 }\
274 const int var = obj[key].toInt()
275
276 map.clear();
277 const QJsonObject top(doc.object());
278 if (doc.isEmpty() || top.isEmpty() || !top.contains(cePalettes)) {
279 qCInfo(lcQGtk3Interface) << "Document does not contain Palettes.";
280 return false;
281 }
282
283 const QStringList &paletteList = top[cePalettes].toObject().keys();
284 for (const QString &paletteName : paletteList) {
285 bool ok;
286 const int intVal = QMetaEnum::fromType<QPlatformTheme::Palette>().keyToValue(paletteName
287 .toLatin1().constData(), &ok);
288 if (!ok) {
289 qCInfo(lcQGtk3Interface) << "Invalid Palette name:" << paletteName;
290 return false;
291 }
292 const QJsonObject &paletteObject = top[cePalettes][paletteName].toObject();
293 const QStringList &brushList = paletteObject.keys();
294 if (brushList.isEmpty()) {
295 qCInfo(lcQGtk3Interface) << "Palette" << paletteName << "does not contain brushes";
296 return false;
297 }
298
301 const QStringList &colorRoles = paletteObject.keys();
302 for (const QString &colorRoleName : colorRoles) {
303 const int intVal = QMetaEnum::fromType<QPalette::ColorRole>().keyToValue(colorRoleName
304 .toLatin1().constData(), &ok);
305 if (!ok) {
306 qCInfo(lcQGtk3Interface) << "Palette" << paletteName
307 << "contains invalid color role" << colorRoleName;
308 return false;
309 }
310 const QPalette::ColorRole colorRole = static_cast<QPalette::ColorRole>(intVal);
311 const QJsonArray &brushArray = paletteObject[colorRoleName].toArray();
312 for (int brushIndex = 0; brushIndex < brushArray.size(); ++brushIndex) {
313 const QJsonObject brushObject = brushArray.at(brushIndex).toObject();
314 if (brushObject.isEmpty()) {
315 qCInfo(lcQGtk3Interface) << "Brush specification missing at for palette"
316 << paletteName << ", Brush" << colorRoleName;
317 return false;
318 }
319
321 GETSTR(brushObject, ceSourceType);
322 const QGtk3Storage::SourceType sourceType = toSourceType(value);
323 GETSTR(brushObject, ceColorGroup);
324 const QPalette::ColorGroup colorGroup = toColorGroup(value);
325 GETSTR(brushObject, ceColorScheme);
326 const Qt::ColorScheme colorScheme = toColorScheme(value);
327 QGtk3Storage::TargetBrush tb(colorGroup, colorRole, colorScheme);
329
330 if (!brushObject.contains(ceData) || !brushObject[ceData].isObject()) {
331 qCInfo(lcQGtk3Interface) << "Source specification missing for palette" << paletteName
332 << "Brush" << colorRoleName;
333 return false;
334 }
335 const QJsonObject &sourceObject = brushObject[ceData].toObject();
336
337 switch (sourceType) {
339 GETSTR(sourceObject, ceGdkSource);
341 GETSTR(sourceObject, ceGtkState);
342 const GtkStateFlags gtkState = toGtkState(value);
343 GETSTR(sourceObject, ceGtkWidget);
345 GETINT(sourceObject, ceHeight, height);
346 GETINT(sourceObject, ceWidth, width);
347 s = QGtk3Storage::Source(widgetType, gtkSource, gtkState, width, height);
348 }
349 break;
350
352 if (!sourceObject.contains(ceBrush)) {
353 qCInfo(lcQGtk3Interface) << "Fixed brush specification missing for palette" << paletteName
354 << "Brush" << colorRoleName;
355 return false;
356 }
357 const QJsonObject &fixedSource = sourceObject[ceBrush].toObject();
358 GETINT(fixedSource, ceWidth, width);
359 GETINT(fixedSource, ceHeight, height);
360 GETSTR(fixedSource, ceColor);
361 const QColor color(value);
362 if (!color.isValid()) {
363 qCInfo(lcQGtk3Interface) << "Color" << value << "can't be parsed for:" << paletteName
364 << "Brush" << colorRoleName;
365 return false;
366 }
367 const QBrush fixedBrush = (width < 0 && height < 0)
369 : QBrush(color);
370 s = QGtk3Storage::Source(fixedBrush);
371 }
372 break;
373
375 GETSTR(sourceObject, ceColorGroup);
376 const QPalette::ColorGroup colorGroup = toColorGroup(value);
377 GETSTR(sourceObject, ceColorRole);
378 const QPalette::ColorRole colorRole = toColorRole(value);
379 GETSTR(sourceObject, ceColorScheme);
380 const Qt::ColorScheme colorScheme = toColorScheme(value);
381 GETINT(sourceObject, ceLighter, lighter);
382 GETINT(sourceObject, ceRed, red);
383 GETINT(sourceObject, ceBlue, blue);
384 GETINT(sourceObject, ceGreen, green);
385 s = QGtk3Storage::Source(colorGroup, colorRole, colorScheme,
386 lighter, red, green, blue);
387 }
388 break;
389
391 qInfo(lcQGtk3Interface) << "Invalid source type for palette" << paletteName
392 << "Brush." << colorRoleName;
393 return false;
394 }
395 brushes.insert(tb, s);
396 }
397 }
398 map.insert(paletteType, brushes);
399 }
400 return true;
401}
402
404
\inmodule QtGui
Definition qbrush.h:30
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
static QColor fromString(QAnyStringView name) noexcept
Definition qcolor.cpp:980
@ HexRgb
Definition qcolor.h:36
void close() override
Calls QFileDevice::flush() and closes the file.
\inmodule QtCore
Definition qfile.h:93
QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:904
const_iterator constBegin() const
Definition qflatmap_p.h:772
const_iterator constEnd() const
Definition qflatmap_p.h:776
std::pair< iterator, bool > insert(const Key &key, const T &value)
Definition qflatmap_p.h:678
T value(const Key &key, const T &defaultValue) const
Definition qflatmap_p.h:636
static const QLatin1String fromGtkState(GtkStateFlags state)
Returns.
static int toGtkState(const QString &state)
Converts a string into the GtkStateFlags enum.
static QLatin1String fromColorGroup(QPalette::ColorGroup group)
Definition qgtk3json.cpp:41
static QLatin1String fromWidgetType(QGtk3Interface::QGtkWidget widgetType)
Definition qgtk3json.cpp:51
static QLatin1String fromColorRole(QPalette::ColorRole role)
Definition qgtk3json.cpp:36
static constexpr QLatin1StringView ceColorGroup
Definition qgtk3json_p.h:73
static constexpr QLatin1StringView ceGreen
Definition qgtk3json_p.h:78
static constexpr QLatin1StringView ceBrush
Definition qgtk3json_p.h:82
static constexpr QLatin1StringView ceWidth
Definition qgtk3json_p.h:80
static GtkStateFlags toGtkState(const QString &type)
Definition qgtk3json.cpp:76
static QPalette::ColorRole toColorRole(const QString &role)
Definition qgtk3json.cpp:89
static constexpr QLatin1StringView ceBlue
Definition qgtk3json_p.h:79
static QPalette::ColorGroup toColorGroup(const QString &group)
Definition qgtk3json.cpp:94
static constexpr QLatin1StringView ceHeight
Definition qgtk3json_p.h:81
static constexpr QLatin1StringView ceColorScheme
Definition qgtk3json_p.h:85
static bool save(const QGtk3Storage::PaletteMap &map, const QString &fileName, QJsonDocument::JsonFormat format=QJsonDocument::Indented)
static constexpr QLatin1StringView ceColor
Definition qgtk3json_p.h:71
static constexpr QLatin1StringView ceGtkWidget
Definition qgtk3json_p.h:70
static constexpr QLatin1StringView ceLighter
Definition qgtk3json_p.h:76
static constexpr QLatin1StringView ceSourceType
Definition qgtk3json_p.h:75
static bool load(QGtk3Storage::PaletteMap &map, const QString &fileName)
static constexpr QLatin1StringView ceData
Definition qgtk3json_p.h:83
static QLatin1String fromGdkSource(QGtk3Interface::QGtkColorSource source)
Definition qgtk3json.cpp:46
static QGtk3Interface::QGtkColorSource toGdkSource(const QString &source)
Definition qgtk3json.cpp:99
static constexpr QLatin1StringView cePalettes
Definition qgtk3json_p.h:67
static QLatin1String fromSourceType(QGtk3Storage::SourceType sourceType)
static QLatin1String fromPalette(QPlatformTheme::Palette palette)
Definition qgtk3json.cpp:21
static constexpr QLatin1StringView ceRed
Definition qgtk3json_p.h:77
static QGtk3Storage::SourceType toSourceType(const QString &sourceType)
static constexpr QLatin1StringView ceColorRole
Definition qgtk3json_p.h:72
static QPlatformTheme::Palette toPalette(const QString &palette)
Definition qgtk3json.cpp:71
static QGtk3Interface::QGtkWidget toWidgetType(const QString &widgetType)
static Qt::ColorScheme toColorScheme(const QString &colorScheme)
Definition qgtk3json.cpp:66
static QLatin1String fromColorScheme(Qt::ColorScheme colorScheme)
Definition qgtk3json.cpp:56
static QLatin1String fromGtkState(GtkStateFlags type)
Definition qgtk3json.cpp:26
static constexpr QLatin1StringView ceGdkSource
Definition qgtk3json_p.h:74
static constexpr QLatin1StringView ceGtkState
Definition qgtk3json_p.h:69
SourceType
This enum represents the type of a color source.
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
\inmodule QtCore\reentrant
Definition qjsonarray.h:18
\inmodule QtCore\reentrant
QByteArray toJson(JsonFormat format=Indented) const
QJsonObject object() const
Returns the QJsonObject contained in the document.
bool isEmpty() const
Returns true if the document doesn't contain any data.
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error=nullptr)
Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it.
\inmodule QtCore\reentrant
Definition qjsonobject.h:20
iterator insert(const QString &key, const QJsonValue &value)
Inserts a new item with the key key and a value of value.
iterator insert(const Key &key, const T &value)
Definition qmap.h:688
void clear()
Definition qmap.h:289
const_iterator constBegin() const
Definition qmap.h:600
const_iterator constEnd() const
Definition qmap.h:604
ColorGroup
\value Disabled \value Active \value Inactive \value Normal synonym for Active
Definition qpalette.h:49
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QMap< QString, QString > map
[6]
else opt state
[0]
Combined button and popup list for selecting options.
ColorScheme
Definition qnamespace.h:50
static int paletteType(const QString &androidControl)
#define Q_LIKELY(x)
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define CONVERT
#define GETINT(obj, key, var)
QColor toColor(const QStringView &color)
Definition qgtk3json.cpp:84
#define GETSTR(obj, key)
QLatin1String fromColor(const QColor &color)
Definition qgtk3json.cpp:31
#define qInfo
Definition qlogging.h:165
#define qWarning
Definition qlogging.h:166
#define qCInfo(category,...)
#define qCWarning(category,...)
@ Invalid
GLint GLsizei GLsizei height
GLdouble GLdouble GLdouble GLdouble top
GLint GLsizei width
GLuint color
[2]
GLenum type
GLboolean GLuint group
GLint GLsizei GLsizei GLenum format
GLsizei GLsizei GLchar * source
GLdouble s
[6]
Definition qopenglext.h:235
GLbyte GLbyte blue
Definition qopenglext.h:385
GLbyte green
Definition qopenglext.h:385
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
QFile file
[0]
QApplication app(argc, argv)
[0]
QGraphicsSvgItem * red
\inmodule QtCore\reentrant
ParseError error
QString errorString() const
\variable QJsonParseError::error