aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/items/qquickpalette.cpp
blob: a64b3094267a445d2c2c112570f7aa6c74efcea2 (plain)
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
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtQuick module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qquickpalette_p.h"

#include <QtQuick/private/qquickpalettecolorprovider_p.h>

QT_BEGIN_NAMESPACE

/*!
    \internal

    \class QQuickPalette
    \brief The QQuickPalette class contains color groups for each QML item state.
    \inmodule QtQuick
    \since 6.0

    This class is the wrapper around QPalette.

    \sa QQuickColorGroup, QQuickAbstractPaletteProvider, QPalette
 */

/*!
    \qmltype Palette
    \instantiates QQuickPalette
    \inherits QQuickColorGroup
    \inqmlmodule QtQuick
    \ingroup qtquick-visual
    \brief The QQuickPalette class contains color groups for each QML item state.

    A palette consists of three color groups: Active, Disabled, and Inactive.
    Active color group is the default group, its colors are used for other groups
    if colors of these groups aren't explicitly specified.

    In the following example, color is applied for all color groups:
    \code
    ApplicationWindow {
        palette.buttonText: "salmon"

        ColumnLayout {
            Button {
                text: qsTr("Disabled button")
                enabled: false
            }

            Button {
                text: qsTr("Enabled button")
            }
        }
    }
    \endcode
    It means that text color will be the same for both buttons.

    In the following example, colors will be different for enabled and disabled states:
    \code
    ApplicationWindow {
        palette.buttonText: "salmon"
        palette.disabled.buttonText: "lavender"

        ColumnLayout {
            Button {
                text: qsTr("Disabled button")
                enabled: false
            }

            Button {
                text: qsTr("Enabled button")
            }
        }
    }
    \endcode

    It is also possible to specify colors like this:
    \code
    palette {
        buttonText: "azure"
        button: "khaki"

        disabled {
            buttonText: "lavender"
            button: "coral"
        }
    }
    \endcode
    This approach is convenient when you need to specify a whole palette with all color groups.
*/

/*!
    \qmlproperty QQuickColorGroup QtQuick::Palette::active

    The Active group is used for windows that are in focus.

    \sa QPalette::Active
*/

/*!
    \qmlproperty QQuickColorGroup QtQuick::Palette::inactive

    The Inactive group is used for windows that have no keyboard focus.

    \sa QPalette::Inactive
*/

/*!
    \qmlproperty QQuickColorGroup QtQuick::Palette::disabled

    The Disabled group is used for elements that are disabled for some reason.

    \sa QPalette::Disabled
*/

QQuickPalette::QQuickPalette(QObject *parent)
    : QQuickColorGroup(parent)
    , m_currentGroup(defaultCurrentGroup())
{
}

QQuickColorGroup *QQuickPalette::active() const
{
    return colorGroup(QPalette::Active);
}

QQuickColorGroup *QQuickPalette::inactive() const
{
    return colorGroup(QPalette::Inactive);
}

QQuickColorGroup *QQuickPalette::disabled() const
{
    return colorGroup(QPalette::Disabled);
}

/*!
    \internal

    Returns the palette's current color group.
    The default value is Active.
 */
QPalette::ColorGroup QQuickPalette::currentColorGroup() const
{
    return m_currentGroup;
}

/*!
    \internal

    Sets \a currentGroup for this palette.

    The current color group is used when accessing colors of this palette.
    For example, if color group is Disabled, color accessors will be
    returning colors form the respective group.
    \code
    QQuickPalette palette;

    palette.setAlternateBase(Qt::green);
    palette.disabled()->setAlternateBase(Qt::red);

    auto color = palette.alternateBase(); // Qt::green

    palette.setCurrentGroup(QPalette::Disabled);
    color = palette.alternateBase(); // Qt::red
    \endcode

    Emits QColorGroup::changed().
 */
void QQuickPalette::setCurrentGroup(QPalette::ColorGroup currentGroup)
{
    if (m_currentGroup != currentGroup) {
        m_currentGroup = currentGroup;
        Q_EMIT changed();
    }
}

void QQuickPalette::fromQPalette(QPalette palette)
{
    if (colorProvider().fromQPalette(std::move(palette))) {
        Q_EMIT changed();
    }
}

QPalette QQuickPalette::toQPalette() const
{
    return colorProvider().palette();
}

const QQuickAbstractPaletteProvider *QQuickPalette::paletteProvider() const
{
    return colorProvider().paletteProvider();
}

void QQuickPalette::setPaletteProvider(const QQuickAbstractPaletteProvider *paletteProvider)
{
    colorProvider().setPaletteProvider(paletteProvider);
}

void QQuickPalette::reset()
{
    if (colorProvider().reset()) {
        Q_EMIT changed();
    }
}

void QQuickPalette::inheritPalette(const QPalette &palette)
{
    if (colorProvider().inheritPalette(palette)) {
        Q_EMIT changed();
    }
}

void QQuickPalette::setActive(QQuickColorGroup *active)
{
    setColorGroup(QPalette::Active, active, &QQuickPalette::activeChanged);
}

void QQuickPalette::setInactive(QQuickColorGroup *inactive)
{
    setColorGroup(QPalette::Inactive, inactive, &QQuickPalette::inactiveChanged);
}

void QQuickPalette::setDisabled(QQuickColorGroup *disabled)
{
    setColorGroup(QPalette::Disabled, disabled, &QQuickPalette::disabledChanged);
}


void QQuickPalette::setColorGroup(QPalette::ColorGroup groupTag,
                                  const QQuickColorGroup::GroupPtr &group,
                                  void (QQuickPalette::*notifier)())
{
    if (isValidColorGroup(groupTag, group)) {
        if (colorProvider().copyColorGroup(groupTag, group->colorProvider())) {
            Q_EMIT (this->*notifier)();
            Q_EMIT changed();
        }
    }
}

QQuickColorGroup::GroupPtr QQuickPalette::colorGroup(QPalette::ColorGroup groupTag) const
{
    if (auto group = findColorGroup(groupTag)) {
        return group;
    }

    auto group = QQuickColorGroup::createWithParent(*const_cast<QQuickPalette*>(this));
    const_cast<QQuickPalette*>(this)->registerColorGroup(group, groupTag);
    return group;
}

QQuickColorGroup::GroupPtr QQuickPalette::findColorGroup(QPalette::ColorGroup groupTag) const
{
    if (auto it = m_colorGroups.find(groupTag); it != m_colorGroups.end()) {
        return it->second;
    }

    return nullptr;
}

void QQuickPalette::registerColorGroup(QQuickColorGroup *group, QPalette::ColorGroup groupTag)
{
    if (auto it = m_colorGroups.find(groupTag); it != m_colorGroups.end() && it->second) {
        it->second->deleteLater();
    }

    m_colorGroups[groupTag] = group;

    group->setGroupTag(groupTag);

    QQuickColorGroup::connect(group, &QQuickColorGroup::changed, this, &QQuickPalette::changed);
}

bool QQuickPalette::isValidColorGroup(QPalette::ColorGroup groupTag,
                                      const QQuickColorGroup::GroupPtr &colorGroup) const
{
    if (!colorGroup) {
        qWarning("Color group cannot be null.");
        return false;
    }

    if (!colorGroup->parent()) {
        qWarning("Color group should have a parent.");
        return false;
    }

    if (colorGroup->parent() && !qobject_cast<QQuickPalette*>(colorGroup->parent())) {
        qWarning("Color group should be a part of QQuickPalette.");
        return false;
    }

    if (groupTag == defaultGroupTag()) {
        qWarning("Register %i color group is not allowed."
                 " QQuickPalette is %i color group itself.", groupTag, groupTag);
        return false;
    }

    if (findColorGroup(groupTag) == colorGroup) {
        qWarning("The color group is already a part of the current palette.");
        return false;
    }

    return true;
}

QT_END_NAMESPACE