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
qquickcolordialog.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// Qt-Security score:significant reason:default
4
6
7#include <QtCore/qloggingcategory.h>
8#include <QtQuickDialogs2QuickImpl/private/qquickplatformcolordialog_p.h>
9#include <QtQuickDialogs2QuickImpl/private/qquickcolordialogimpl_p.h>
10
12
13/*!
14 \qmltype ColorDialog
15 \inherits Dialog
16 \inqmlmodule QtQuick.Dialogs
17 \since 6.4
18 \brief A color dialog.
19
20 The ColorDialog type provides a QML API for color dialogs.
21
22 \image qtquickdialogs-colordialog-gtk.png {The color dialog can be customised using the options property}
23
24 To show a color dialog, construct an instance of ColorDialog, set the
25 desired properties, and call \l {Dialog::}{open()}. The \l selectedColor
26 property can be used to determine the initially selected color in the
27 dialog.
28
29 \code
30 MenuItem {
31 text: qsTr("Color")
32 onTriggered: colorDialog.open()
33 }
34
35 ColorDialog {
36 id: colorDialog
37 selectedColor: document.color
38 onAccepted: document.color = selectedColor
39 }
40
41 MyDocument {
42 id: document
43 }
44 \endcode
45
46 \section2 Availability
47
48 A native platform color dialog is currently available on the following platforms:
49
50 \list
51 \li iOS
52 \li Linux (when running with the GTK+ platform theme)
53 \li macOS
54 \endlist
55
56 \include includes/fallback.qdocinc
57*/
58
59QQuickColorDialog::QQuickColorDialog(QObject *parent)
60 : QQuickAbstractDialog(QQuickDialogType::ColorDialog, parent),
61 m_options(QColorDialogOptions::create()),
62 m_selectedColor(QColorConstants::White)
63{
64}
65
66/*!
67 \qmlproperty color QtQuick.Dialogs::ColorDialog::selectedColor
68
69 This property holds the currently selected color in the dialog.
70
71 The \l {Dialog::}{accepted()} signal can be handled to get the final selection.
72 When the user has clicked \uicontrol Open to accept a color, a signal handler
73 for the \l {Dialog::}{accepted()} signal can query the selectedColor property to
74 get the final color that was selected by the user.
75
76 \sa {Dialog::}{accepted()}
77*/
78QColor QQuickColorDialog::selectedColor() const
79{
80 return m_selectedColor;
81}
82
83void QQuickColorDialog::setSelectedColor(const QColor &color)
84{
85 if (color == m_selectedColor)
86 return;
87
88 m_selectedColor = color;
89
90 emit selectedColorChanged();
91}
92
93/*!
94 \qmlproperty flags QtQuick.Dialogs::ColorDialog::options
95
96 This property holds the various options that affect the look and feel of the dialog.
97
98 By default, all options are disabled.
99
100 Options should be set before showing the dialog. Setting them while the dialog is
101 visible is not guaranteed to have an immediate effect on the dialog (depending on
102 the option and on the platform).
103
104 Available options:
105 \value ColorDialog.ShowAlphaChannel Show a slider and additional input fields for the alpha value.
106 \value ColorDialog.NoButtons Don't display \uicontrol Open and \uicontrol Cancel buttons (useful
107 for "live dialogs").
108 \value ColorDialog.NoEyeDropperButton Don't display \uicontrol {Eye Dropper} button. This value was added in Qt 6.6.
109 \value ColorDialog.DontUseNativeDialog Forces the dialog to use a non-native quick implementation.
110*/
111
112QColorDialogOptions::ColorDialogOptions QQuickColorDialog::options() const
113{
114 return m_options->options();
115}
116
117void QQuickColorDialog::setOptions(QColorDialogOptions::ColorDialogOptions options)
118{
119 if (options == m_options->options())
120 return;
121
122 m_options->setOptions(options);
123 emit optionsChanged();
124}
125
126void QQuickColorDialog::resetOptions()
127{
128 setOptions({});
129}
130
131bool QQuickColorDialog::useNativeDialog() const
132{
133 return QQuickAbstractDialog::useNativeDialog()
134 && !(m_options->testOption(QColorDialogOptions::DontUseNativeDialog));
135}
136
137void QQuickColorDialog::onCreate(QPlatformDialogHelper *dialog)
138{
139 if (auto colorDialog = qobject_cast<QPlatformColorDialogHelper *>(dialog)) {
140 connect(colorDialog, &QPlatformColorDialogHelper::currentColorChanged, this,
141 [this, colorDialog]() { setSelectedColor(colorDialog->currentColor()); });
142 colorDialog->setOptions(m_options);
143 }
144}
145
146void QQuickColorDialog::onShow(QPlatformDialogHelper *dialog)
147{
148 m_options->setWindowTitle(title());
149 if (auto colorDialog = qobject_cast<QPlatformColorDialogHelper *>(dialog)) {
150 colorDialog->setOptions(m_options);
151 colorDialog->setCurrentColor(m_selectedColor);
152 }
153 if (QQuickPlatformColorDialog *colorDialog = qobject_cast<QQuickPlatformColorDialog *>(dialog))
154 colorDialog->dialog()->setPopupType(m_popupType);
155
156 QQuickAbstractDialog::onShow(dialog);
157}
158
159QT_END_NAMESPACE