Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qquickpaletteproviderprivatebase_p.h
Go to the documentation of this file.
1
// Copyright (C) 2020 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
#
ifndef
QQUICKPALETTEPROVIDERPRIVATEBASE_H
5
#
define
QQUICKPALETTEPROVIDERPRIVATEBASE_H
6
7
//
8
// W A R N I N G
9
// -------------
10
//
11
// This file is not part of the Qt API. It exists purely as an
12
// implementation detail. This header file may change from version to
13
// version without notice, or even be removed.
14
//
15
// We mean it.
16
//
17
18
#
include
<
QtQuick
/
private
/
qquickpalette_p
.
h
>
19
#
include
<
QtQuick
/
private
/
qquickabstractpaletteprovider_p
.
h
>
20
#
include
<
QtGui
/
qwindow
.
h
>
21
#
include
<
QtCore
/
qthread
.
h
>
22
#
include
<
QtQml
/
private
/
qlazilyallocated_p
.
h
>
23
24
QT_BEGIN_NAMESPACE
25
26
class
QWindow;
27
class
QQuickWindow;
28
class
QQuickWindowPrivate;
29
class
QQuickItem;
30
class
QQuickItemPrivate;
31
class
QQuickPopup;
32
class
QQuickPopupPrivate;
33
34
/*!
35
\internal
36
37
Implements all required operations with palette.
38
39
I -- is interface class (e.g. QQuickItem).
40
Impl -- is implementation class (e.g. QQuickItemPrivate).
41
42
To use this class you need to inherit implementation class from it.
43
*/
44
template
<
class
I,
class
Impl>
45
class
QQuickPaletteProviderPrivateBase
:
public
QQuickAbstractPaletteProvider
46
{
47
static_assert
(
std
::
is_base_of
<
QObject
,
I
>{},
"The interface class must inherit QObject"
);
48
49
public
:
50
virtual
~
QQuickPaletteProviderPrivateBase
() =
default
;
51
52
/*!
53
\internal
54
55
Get current palette.
56
57
\note Palette might be lazily allocated. Signal \p paletteCreated()
58
will be emitted by an object of interface class in this case.
59
60
\note This function doesn't ask an object of interface class to emit
61
paletteChanged() signal in order to avoid problems with
62
property bindigns.
63
*/
64
virtual
QQuickPalette
*
palette
()
const
;
65
66
/*!
67
\internal
68
69
Set new palette. Doesn't transfer ownership.
70
*/
71
virtual
void
setPalette
(QQuickPalette *p);
72
73
/*!
74
\internal
75
76
Reset palette to the default one.
77
*/
78
virtual
void
resetPalette
();
79
80
/*!
81
\internal
82
83
Check if everything is internally allocated and palette exists.
84
85
Use before call \p palette() to avoid unnecessary allocations.
86
*/
87
virtual
bool
providesPalette
()
const
;
88
89
/*!
90
\internal
91
92
The default palette for this component.
93
*/
94
QPalette
defaultPalette
()
const
override
;
95
96
/*!
97
\internal
98
99
The parent palette for this component. Can be null.
100
*/
101
QPalette
parentPalette
(
const
QPalette &fallbackPalette)
const
override
;
102
103
/*!
104
\internal
105
106
Inherit from \p parentPalette. This function is also called when
107
either parent or window of this item is changed.
108
*/
109
void
inheritPalette
(
const
QPalette &parentPalette);
110
111
/*!
112
\internal
113
114
Updates children palettes. The default implementation invokes
115
inheritPalette for all visual children.
116
117
This function is also called when palette is changed
118
(signal changed() is emitted).
119
*/
120
virtual
void
updateChildrenPalettes
(
const
QPalette &parentPalette);
121
122
protected
:
123
void
setCurrentColorGroup
();
124
125
private
:
126
using
PalettePtr
=
std
::
unique_ptr
<
QQuickPalette
>;
127
using
Self =
QQuickPaletteProviderPrivateBase
<I, Impl>;
128
129
void
registerPalette(PalettePtr palette);
130
131
bool
isValidPalette(
const
QQuickPalette *palette)
const
;
132
133
QQuickPalette *windowPalette()
const
;
134
135
136
void
connectItem();
137
138
const
I *itemWithPalette()
const
;
139
I *itemWithPalette();
140
141
QQuickPalette *paletteData()
const
;
142
143
QPalette toQPalette()
const
;
144
145
private
:
146
PalettePtr m_palette;
147
};
148
149
template
<
class
I,
class
Impl>
150
QQuickPalette
*
QQuickPaletteProviderPrivateBase
<I, Impl>::
palette
()
const
151
{
152
if
(!
providesPalette
(
)
) {
153
// Create the palette on the item's thread: the item owns and deletes it,
154
// so a palette with another thread's affinity would receive events
155
// (e.g. QDisconnectNotifyEvent) after the item has deleted it.
156
Q_ASSERT_X(QThread::currentThread() == itemWithPalette()->thread(),
157
"QQuickPaletteProviderPrivateBase::palette()"
,
158
"The palette must be created on the item's thread, not e.g. the render thread"
);
159
const_cast
<Self*>(
this
)->registerPalette(std::make_unique<QQuickPalette>());
160
Q_EMIT
const_cast
<Self*>(
this
)->itemWithPalette()->paletteCreated();
161
}
162
163
return
paletteData();
164
}
165
166
template
<
class
I,
class
Impl>
167
bool
QQuickPaletteProviderPrivateBase
<I, Impl>::isValidPalette(
const
QQuickPalette *palette)
const
168
{
169
if
(!palette) {
170
qWarning(
"Palette cannot be null."
);
171
return
false
;
172
}
173
174
if
(providesPalette() && paletteData() == palette) {
175
qWarning(
"Self assignment makes no sense."
);
176
return
false
;
177
}
178
179
return
true
;
180
}
181
182
template
<
class
I,
class
Impl>
183
void
QQuickPaletteProviderPrivateBase
<I, Impl>::
setPalette
(QQuickPalette *p)
184
{
185
if
(isValidPalette(p)) {
186
palette()->fromQPalette(p->toQPalette());
187
}
188
}
189
190
template
<
class
I,
class
Impl>
191
void
QQuickPaletteProviderPrivateBase
<I, Impl>::
resetPalette
()
192
{
193
if
(providesPalette())
194
paletteData()->reset();
195
}
196
197
template
<
class
I,
class
Impl>
198
bool
QQuickPaletteProviderPrivateBase
<I, Impl>::
providesPalette
()
const
199
{
200
return
!!m_palette;
201
}
202
203
template
<
class
I,
class
Impl>
204
QPalette
QQuickPaletteProviderPrivateBase
<I, Impl>::
defaultPalette
()
const
205
{
206
return
QPalette();
207
}
208
209
template
<
class
Window>
210
inline
constexpr
bool
isRootWindow
() {
return
std::is_base_of_v<QWindow, Window>; }
211
212
template
<
class
I,
class
Impl>
213
void
QQuickPaletteProviderPrivateBase
<I, Impl>::registerPalette(PalettePtr palette)
214
{
215
if
constexpr
(!isRootWindow<I>()) {
216
// Connect item only once, before initial data allocation
217
if
(!
providesPalette
(
)
) {
218
connectItem();
219
}
220
}
221
222
m_palette = std::move(palette);
223
m_palette->setPaletteProvider(
this
);
224
m_palette->inheritPalette(parentPalette(defaultPalette()));
225
226
setCurrentColorGroup
(
)
;
227
228
// In order to avoid extra noise, we should connect
229
// the following signals only after everything is already setup
230
I::connect(paletteData(), &QQuickPalette::changed, itemWithPalette(), &I::paletteChanged);
231
I::connect(paletteData(), &QQuickPalette::changed, itemWithPalette(), [
this
]{ updateChildrenPalettes(toQPalette()); });
232
}
233
234
template
<
class
T>
struct
dependent_false
:
std
::
false_type
{};
235
template
<
class
Impl,
class
I>
decltype
(
auto
)
getPrivateImpl
(I &t) {
return
Impl::get(&t); }
236
237
template
<
class
T>
238
decltype
(
auto
)
getPrivate
(T &item)
239
{
240
if
constexpr
(std::is_base_of_v<T, QQuickWindow>) {
241
return
getPrivateImpl<QQuickWindowPrivate>(item);
242
}
else
if
constexpr
(std::is_base_of_v<T, QQuickItem>) {
243
return
getPrivateImpl<QQuickItemPrivate>(item);
244
}
else
{
245
static_assert
(
dependent_false
<T>::value,
"Extend please."
);
246
}
247
}
248
249
template
<
class
I,
class
Impl>
250
QQuickPalette *
QQuickPaletteProviderPrivateBase
<I, Impl>::windowPalette()
const
251
{
252
if
constexpr
(!isRootWindow<I>()) {
253
if
(
auto
window = itemWithPalette()->window()) {
254
if
(getPrivate(*window)->providesPalette()) {
255
return
getPrivate(*window)->palette();
256
}
257
}
258
}
259
260
return
nullptr
;
261
}
262
263
template
<
class
I,
class
Impl>
264
QPalette
QQuickPaletteProviderPrivateBase
<I, Impl>::
parentPalette
(
const
QPalette &fallbackPalette)
const
265
{
266
if
constexpr
(!isRootWindow<I>()) {
267
// Popups should always inherit from their window, even child popups: QTBUG-115707.
268
if
(!std::is_base_of_v<QQuickPopup, I>) {
269
for
(
auto
parentItem = itemWithPalette()->parentItem(); parentItem;
270
parentItem = parentItem->parentItem()) {
271
272
// Don't allocate a new palette here. Use only if it's already pre allocated
273
if
(parentItem && getPrivate(*parentItem)->providesPalette()) {
274
return
getPrivate(*parentItem)->palette()->toQPalette();
275
}
276
}
277
}
278
279
if
(
auto
wp = windowPalette()) {
280
return
wp->toQPalette();
281
}
282
}
283
284
return
fallbackPalette;
285
}
286
287
template
<
class
I>
288
const
QQuickItem*
rootItem
(
const
I &item)
289
{
290
if
constexpr
(isRootWindow<I>()) {
291
return
item.contentItem();
292
}
else
if
constexpr
(std::is_base_of_v<QQuickPopup, I>) {
293
return
nullptr
;
294
}
else
{
295
return
&item;
296
}
297
}
298
299
template
<
class
I,
class
Impl>
300
void
QQuickPaletteProviderPrivateBase
<I, Impl>::
inheritPalette
(
const
QPalette &parentPalette)
301
{
302
if
(
providesPalette
(
)
) {
303
// If palette is changed, then this function will be invoked
304
// for all children because of connection with signal changed()
305
palette()->inheritPalette(parentPalette);
306
}
else
{
307
// Otherwise, just propagate parent palette to all children
308
updateChildrenPalettes(parentPalette);
309
}
310
}
311
312
template
<
class
I,
class
Impl>
313
void
QQuickPaletteProviderPrivateBase
<I, Impl>::
setCurrentColorGroup
()
314
{
315
if
constexpr
(!isRootWindow<I>()) {
316
if
(
providesPalette
(
)
) {
317
const
bool
enabled = itemWithPalette()->isEnabled();
318
const
auto
window = itemWithPalette()->window();
319
const
bool
active = window ? window->isActive() :
true
;
320
palette()->setCurrentGroup(enabled ? (active ? QPalette::Active : QPalette::Inactive)
321
: QPalette::Disabled);
322
}
323
}
324
}
325
326
template
<
class
I,
class
Impl>
327
void
QQuickPaletteProviderPrivateBase
<I, Impl>::
updateChildrenPalettes
(
const
QPalette &parentPalette)
328
{
329
if
constexpr
(std::is_same_v<QQuickWindow, I> && std::is_same_v<QQuickWindowPrivate, Impl>) {
330
/* QQuickWindowPrivate instantiates this template, but does not include QQuickItemPrivate
331
* This causes an error with the QQuickItemPrivate::inheritPalette call below on MSVC in
332
* static builds, as QQuickItemPrivate is incomplete. To work around this situation, we do
333
* nothing in this instantiation of updateChildrenPalettes and instead add an override in
334
* QQuickWindowPrivate, which does the correct thing.
335
*/
336
Q_UNREACHABLE_RETURN();
// You are not supposed to call this function
337
}
else
{
338
if
(
auto
root = rootItem(*itemWithPalette())) {
339
for
(
auto
&&child : root->childItems()) {
340
if
(Q_LIKELY(child)) {
341
getPrivate(*child)->inheritPalette(parentPalette);
342
}
343
}
344
}
345
}
346
}
347
348
template
<
class
I,
class
Impl>
349
void
QQuickPaletteProviderPrivateBase
<I, Impl>::connectItem()
350
{
351
Q_ASSERT(itemWithPalette());
352
353
if
constexpr
(!isRootWindow<I>()) {
354
// Item with palette has the same lifetime as its implementation that inherits this class
355
I::connect(itemWithPalette(), &I::parentChanged,
356
itemWithPalette(), [
this
]() { inheritPalette(parentPalette(defaultPalette())); });
357
I::connect(itemWithPalette(), &I::windowChanged,
358
itemWithPalette(), [
this
]() { inheritPalette(parentPalette(defaultPalette())); });
359
I::connect(itemWithPalette(), &I::enabledChanged,
360
itemWithPalette(), [
this
]() {
setCurrentColorGroup
(
)
; });
361
}
362
}
363
364
template
<
class
I,
class
Impl>
365
const
I *
QQuickPaletteProviderPrivateBase
<I, Impl>::itemWithPalette()
const
366
{
367
static_assert
(std::is_base_of<QObjectData, Impl>{},
368
"The Impl class must inherit QObjectData"
);
369
370
return
static_cast
<
const
I*>(
static_cast
<
const
Impl*>(
this
)->q_ptr);
371
}
372
373
template
<
class
I,
class
Impl>
374
I *
QQuickPaletteProviderPrivateBase
<I, Impl>::itemWithPalette()
375
{
376
return
const_cast
<I*>(
const_cast
<
const
Self*>(
this
)->itemWithPalette());
377
}
378
379
template
<
class
I,
class
Impl>
380
QQuickPalette *
QQuickPaletteProviderPrivateBase
<I, Impl>::paletteData()
const
381
{
382
Q_ASSERT(m_palette);
return
m_palette.get();
383
}
384
385
template
<
class
I,
class
Impl>
386
QPalette
QQuickPaletteProviderPrivateBase
<I, Impl>::toQPalette()
const
387
{
388
return
palette()->toQPalette();
389
}
390
391
QT_END_NAMESPACE
392
393
#
endif
// QQUICKPALETTEPROVIDERPRIVATEBASE_H
QQuickPaletteProviderPrivateBase
Definition
qquickpaletteproviderprivatebase_p.h:46
QQuickPaletteProviderPrivateBase::inheritPalette
void inheritPalette(const QPalette &parentPalette)
Definition
qquickpaletteproviderprivatebase_p.h:300
QQuickPaletteProviderPrivateBase::providesPalette
virtual bool providesPalette() const
Definition
qquickpaletteproviderprivatebase_p.h:198
QQuickPaletteProviderPrivateBase::updateChildrenPalettes
virtual void updateChildrenPalettes(const QPalette &parentPalette)
Definition
qquickpaletteproviderprivatebase_p.h:327
QQuickPaletteProviderPrivateBase::palette
virtual QQuickPalette * palette() const
Definition
qquickpaletteproviderprivatebase_p.h:150
QQuickPaletteProviderPrivateBase::~QQuickPaletteProviderPrivateBase
virtual ~QQuickPaletteProviderPrivateBase()=default
QQuickPaletteProviderPrivateBase::setCurrentColorGroup
void setCurrentColorGroup()
Definition
qquickpaletteproviderprivatebase_p.h:313
QQuickPaletteProviderPrivateBase::resetPalette
virtual void resetPalette()
Definition
qquickpaletteproviderprivatebase_p.h:191
QQuickPaletteProviderPrivateBase::parentPalette
QPalette parentPalette(const QPalette &fallbackPalette) const override
Definition
qquickpaletteproviderprivatebase_p.h:264
QQuickPaletteProviderPrivateBase::defaultPalette
QPalette defaultPalette() const override
Definition
qquickpaletteproviderprivatebase_p.h:204
QQuickPaletteProviderPrivateBase::setPalette
virtual void setPalette(QQuickPalette *p)
Definition
qquickpaletteproviderprivatebase_p.h:183
rootItem
const QQuickItem * rootItem(const I &item)
Definition
qquickpaletteproviderprivatebase_p.h:288
getPrivate
decltype(auto) getPrivate(T &item)
Definition
qquickpaletteproviderprivatebase_p.h:238
isRootWindow
constexpr bool isRootWindow()
Definition
qquickpaletteproviderprivatebase_p.h:210
getPrivateImpl
decltype(auto) getPrivateImpl(I &t)
Definition
qquickpaletteproviderprivatebase_p.h:235
dependent_false
Definition
qquickpaletteproviderprivatebase_p.h:234
qtdeclarative
src
quick
items
qquickpaletteproviderprivatebase_p.h
Generated on
for Qt by
1.18.0