1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
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
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qqstylekitdebug_p.h"
#include "qqstylekitcontrol_p.h"
#include "qqstylekitcontrols_p.h"
#include "qqstylekitstyle_p.h"
#include "qqstylekittheme_p.h"
QT_BEGIN_NAMESPACE
const QQStyleKitPropertyGroup *QQStyleKitDebug::groupBeingRead = nullptr;
QPointer<QQuickItem> QQStyleKitDebug::m_item;
QString QQStyleKitDebug::m_filter;
int QQStyleKitDebug::m_outputCount = 0;
static const QChar kDot = '.'_L1;
template <typename EnumType>
QString QQStyleKitDebug::enumToString(EnumType enumValue)
{
auto propertyMetaEnum = QMetaEnum::fromType<EnumType>();
return QString::fromUtf8(propertyMetaEnum.valueToKeys(quint64(enumValue)));
}
QString QQStyleKitDebug::objectName(const QObject *obj) {
QString str = QString::fromLatin1(obj->metaObject()->className());
int idx = str.indexOf("_QMLTYPE"_L1);
if (idx != -1)
str = str.left(idx);
else {
const QString prefix("QQStyleKit"_L1);
if (str.startsWith(prefix))
str = str.mid(prefix.length());
}
const QString name = obj->objectName();
if (!name.isEmpty())
str = str + "("_L1 + name + ")"_L1;
return str;
}
QString QQStyleKitDebug::stateToString(const QQSK::State state)
{
const QStringList list = enumToString(state).split('|'_L1);
return "["_L1 + list.join(','_L1) + "]"_L1;
}
QString QQStyleKitDebug::styleReaderToString(const QQStyleKitReader *reader)
{
return "StyleKitReader"_L1 + stateToString(reader->controlState());
}
QString QQStyleKitDebug::propertyPath(const QQStyleKitPropertyGroup *group, const PropertyPathId property)
{
QString path = enumToString(property.property());
path[0] = path[0].toLower();
const QQStyleKitPropertyGroup *childGroup = group;
const int startIndex = QQStyleKitPropertyGroup::staticMetaObject.propertyOffset();
while (childGroup) {
if (childGroup->isControlProperties())
break;
const QQStyleKitPropertyGroup *parentGroup =
qobject_cast<const QQStyleKitPropertyGroup *>(childGroup->parent());
if (!parentGroup)
break;
// Resolve group name by inspecting which property in the parent group it belongs to
const QMetaObject* parentMeta = parentGroup->metaObject();
for (int i = startIndex; i < parentMeta->propertyCount(); ++i) {
const QMetaProperty prop = parentMeta->property(i);
const QMetaObject* typeMeta = QMetaType::fromName(prop.typeName()).metaObject();
if (!typeMeta || !typeMeta->inherits(&QQStyleKitPropertyGroup::staticMetaObject))
continue;
QObject *propGroup = prop.read(parentGroup).value<QQStyleKitPropertyGroup *>();
if (propGroup == childGroup) {
path.prepend(QString::fromUtf8(prop.name()) + kDot);
break;
}
}
childGroup = parentGroup;
}
return path;
}
QString QQStyleKitDebug::controlToString(const QQStyleKitControlProperties *control)
{
const QObject *parentObj = control->parent();
if (!parentObj)
return "<no parent>"_L1;
auto *controls = qobject_cast<const QQStyleKitControls *>(parentObj);
if (!controls) {
return "<"_L1 + QString::fromUtf8(parentObj->metaObject()->className()) + ">"_L1;
}
const int startIndex = QQStyleKitControlProperties::staticMetaObject.propertyOffset();
const int endIndex = QQStyleKitControlProperties::staticMetaObject.propertyCount();
const QMetaObject* parentMeta = parentObj->metaObject();
for (int i = startIndex; i < endIndex; ++i) {
const QMetaProperty prop = parentMeta->property(i);
const QMetaObject* typeMeta = QMetaType::fromName(prop.typeName()).metaObject();
if (!typeMeta || !typeMeta->inherits(&QQStyleKitControl::staticMetaObject))
continue;
QObject *propObj = prop.read(parentObj).value<QObject *>();
if (propObj == control)
return QString::fromUtf8(prop.name());
}
return "<unknown control: no property found>"_L1;
}
QString QQStyleKitDebug::objectPath(const QQStyleKitControlProperties *properties, QObject *from)
{
QString path;
const QObject *obj = properties;
while (obj) {
if (!path.isEmpty())
path.prepend(kDot);
if (auto *theme = qobject_cast<const QQStyleKitCustomTheme *>(obj)) {
path.prepend(theme->name() + kDot);
} else if (auto *theme = qobject_cast<const QQStyleKitTheme *>(obj)) {
// Note: only one theme is instantiated at a time
if (auto style = theme->style())
path.prepend(style->themeName());
else
path.prepend(objectName(obj));
} else if (auto *control = qobject_cast<const QQStyleKitControl *>(obj)) {
path.prepend(controlToString(control));
} else if (auto *reader = qobject_cast<const QQStyleKitReader *>(obj)) {
path.prepend(styleReaderToString(reader));
} else {
path.prepend(objectName(obj));
}
if (obj == from)
break;
obj = obj->parent();
}
return path;
}
void QQStyleKitDebug::notifyPropertyRead(
const PropertyPathId property,
const QQStyleKitControlProperties *storage,
const QQSK::State state,
const QVariant &value)
{
Q_ASSERT(enabled());
const QQStyleKitControlProperties *reader = QQStyleKitDebug::groupBeingRead->controlProperties();
if (reader->subclass() == QQSK::Subclass::QQStyleKitState) {
/* The reader is in the UnfiedStyle, and not in the users application (which can happen
* when e.g resolving local bindings between properties in the style). Those are not
* interesting to print out when inspecting control-to-style mappings. Ignore. */
return;
}
if (!insideControl(reader)) {
// We should only debug reads that targets m_item. So return.
return;
}
const QString _readerPath = objectPath(reader, m_item);
const QString _readPropertyPath = propertyPath(QQStyleKitDebug::groupBeingRead, property);
const QString queriedPath = _readerPath + kDot +_readPropertyPath;
QString storagePath;
if (storage->subclass() == QQSK::Subclass::QQStyleKitReader) {
/* We read an interpolated value stored directly in the reader itself. While this
* can be interesting to print out whe debugging the styling engine itself, it
* comes across as noise when inspecting control-to-style mappings. Ignore. */
#if 0
storagePath = "[local storage] "_L1;
#else
return;
#endif
} else {
const QString _controlPathInStyle = objectPath(storage, storage->style());
const QString _statePath = stateToString(state);
storagePath = _controlPathInStyle + _statePath;
}
QString valueString = value.toString();
if (!value.isValid()) // value was set, but probably to undefined
valueString = "<undefined>"_L1;
else if (valueString.isEmpty())
valueString = "<object>"_L1;
const QString output = queriedPath + " -> "_L1 + storagePath + " = "_L1 + valueString;
if (!QRegularExpression(m_filter).match(output).hasMatch())
return;
qDebug().nospace().noquote() << m_outputCount++ << " | [read] "_L1 << output;
}
void QQStyleKitDebug::notifyPropertyWrite(
const QQStyleKitPropertyGroup *group,
const QQSK::Property property,
const QQStyleKitControlProperties *storage,
const QQSK::State state,
const PropertyStorageId key,
const QVariant &value)
{
#if 1
Q_UNUSED(group);
Q_UNUSED(property);
Q_UNUSED(storage);
Q_UNUSED(state);
Q_UNUSED(key);
Q_UNUSED(value);
#else
/* Note: in order to catch _all_ writes, we cannot depend on enabling writes from
* QML using a property, as that would resolve to 'true' too late. */
QString storagePath;
if (storage->subclass() == QQSK::Subclass::QQStyleKitReader) {
storagePath = "[local storage]"_L1;
} else {
const QString _controlPathInStyle = objectPath(storage, storage->style());
const QString _statePath = stateToString(state);
storagePath = _controlPathInStyle + _statePath;
}
QString valueString = value.toString();
if (!value.isValid()) // value was set, but probably to undefined
valueString = "<undefined>"_L1;
else if (valueString.isEmpty())
valueString = "<object>"_L1;
const QString path = propertyPath(group, property);
const QString output = storagePath + kDot + path + " (storage key:"_L1 + QString::number(key) + ") = "_L1 + valueString;
qDebug().nospace().noquote() << m_outputCount++ << " | [write] "_L1 << output;
#endif
}
void QQStyleKitDebug::notifyPropertyNotResolved(const PropertyPathId property)
{
const QQStyleKitControlProperties *reader = QQStyleKitDebug::groupBeingRead->controlProperties();
if (!insideControl(reader)) {
// We should only debug reads that targets m_item. So return.
return;
}
const QString _readerPath = objectPath(reader, m_item);
const QString _propertyPath = propertyPath(QQStyleKitDebug::groupBeingRead, property);
const QString queriedPath = _readerPath + kDot +_propertyPath;
const QString output = queriedPath + " -> <property not set>"_L1;
if (!QRegularExpression(m_filter).match(output).hasMatch())
return;
qDebug().nospace().noquote() << m_outputCount++ << " | [read] "_L1 << output;
}
void QQStyleKitDebug::trace(
const PropertyPathId property,
const QQStyleKitControlProperties *storage,
const QQSK::State state,
const PropertyStorageId key)
{
#if 1
Q_UNUSED(property);
Q_UNUSED(storage);
Q_UNUSED(state);
Q_UNUSED(key);
#else
const QQStyleKitControlProperties *reader = QQStyleKitDebug::groupBeingRead->controlProperties();
if (reader->subclass() == QQSK::Subclass::QQStyleKitState) {
/* The reader is in the UnfiedStyle, and not in the users application (which can happen
* when e.g resolving local bindings between properties in the style). Those are not
* interesting to print out when inspecting control-to-style mappings. Ignore. */
return;
}
if (!insideControl(reader)) {
// We should only debug reads that targets m_item. So return.
return;
}
const QString _readerPath = objectPath(reader, m_item);
const QString _readPropertyPath = propertyPath(QQStyleKitDebug::groupBeingRead, property);
const QString queriedPath = _readerPath + kDot +_readPropertyPath;
QString storagePath;
if (storage->subclass() == QQSK::Subclass::QQStyleKitReader) {
/* We read an interpolated value stored directly in the reader itself. While this
* can be interesting to print out whe debugging the styling engine itself, it
* comes across as noise when inspecting control-to-style mappings. Ignore. */
#if 0
storagePath = "[local storage]"_L1;
#else
return;
#endif
} else {
const QString _controlPathInStyle = objectPath(storage, storage->style());
const QString _statePath = stateToString(state);
storagePath = _controlPathInStyle + _statePath;
}
const QString output = queriedPath + ", checking "_L1 + storagePath + " (storage key:"_L1 + QString::number(key)+ ")"_L1;
if (!QRegularExpression(m_filter).match(output).hasMatch())
return;
qDebug().nospace().noquote() << m_outputCount++ << " | [trace] "_L1 << output;
#endif
}
QQuickItem *QQStyleKitDebug::control() const
{
return m_item;
}
void QQStyleKitDebug::setControl(QQuickItem *item)
{
if (m_item == item)
return;
m_item = item;
emit controlChanged();
}
QString QQStyleKitDebug::filter() const
{
return m_filter;
}
void QQStyleKitDebug::setFilter(const QString &filter)
{
if (m_filter == filter)
return;
m_filter = filter;
emit filterChanged();
}
bool QQStyleKitDebug::insideControl(const QObject *child)
{
if (!m_item)
return false;
const QObject *obj = child;
do {
if (obj == m_item)
return true;
obj = obj->parent();
} while (obj);
return false;
}
QT_END_NAMESPACE
#include "moc_qqstylekitdebug_p.cpp"
|