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
qquicklabsplatformcolordialog.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 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#if QT_DEPRECATED_SINCE(6, 9)
8
9QT_BEGIN_NAMESPACE
10
11/*!
12 \qmltype ColorDialog
13 \inherits Dialog
14//! \nativetype QQuickLabsPlatformColorDialog
15 \inqmlmodule Qt.labs.platform
16 \since 5.8
17 \deprecated [6.9] Use QtQuick.Dialogs::ColorDialog instead.
18 \brief A native color dialog.
19
20 The ColorDialog type provides a QML API for native platform color dialogs.
21
22 \image {qtlabsplatform-colordialog-gtk.png} {A native color dialog}
23
24 To show a color dialog, construct an instance of ColorDialog, set the
25 desired properties, and call \l {Dialog::}{open()}. The \l currentColor
26 property can be used to determine the currently selected color in the
27 dialog. The \l color property is updated only after the final selection
28 has been made by accepting the dialog.
29
30 \code
31 MenuItem {
32 text: "Color"
33 onTriggered: colorDialog.open()
34 }
35
36 ColorDialog {
37 id: colorDialog
38 currentColor: document.color
39 }
40
41 MyDocument {
42 id: document
43 color: colorDialog.color
44 }
45 \endcode
46
47 \section2 Availability
48
49 A native platform color dialog is currently available on the following platforms:
50
51 \list
52 \li iOS
53 \li Linux (when running with the GTK+ platform theme)
54 \li macOS
55 \endlist
56
57 \input includes/widgets.qdocinc 1
58
59 \labs
60
61 \sa QtQuick.Dialogs::ColorDialog
62*/
63
64QQuickLabsPlatformColorDialog::QQuickLabsPlatformColorDialog(QObject *parent)
65 : QQuickLabsPlatformDialog(QPlatformTheme::ColorDialog, parent),
66 m_options(QColorDialogOptions::create())
67{
68}
69
70/*!
71 \qmlproperty color Qt.labs.platform::ColorDialog::color
72
73 This property holds the final accepted color.
74
75 Unlike the \l currentColor property, the \c color property is not updated
76 while the user is selecting colors in the dialog, but only after the final
77 selection has been made. That is, when the user has clicked \uicontrol OK
78 to accept a color. Alternatively, the \l {Dialog::}{accepted()} signal
79 can be handled to get the final selection.
80
81 \sa currentColor, {Dialog::}{accepted()}
82*/
83QColor QQuickLabsPlatformColorDialog::color() const
84{
85 return m_color;
86}
87
88void QQuickLabsPlatformColorDialog::setColor(const QColor &color)
89{
90 if (m_color == color)
91 return;
92
93 m_color = color;
94 setCurrentColor(color);
95 emit colorChanged();
96}
97
98/*!
99 \qmlproperty color Qt.labs.platform::ColorDialog::currentColor
100
101 This property holds the currently selected color in the dialog.
102
103 Unlike the \l color property, the \c currentColor property is updated
104 while the user is selecting colors in the dialog, even before the final
105 selection has been made.
106
107 \sa color
108*/
109QColor QQuickLabsPlatformColorDialog::currentColor() const
110{
111 if (QPlatformColorDialogHelper *colorDialog = qobject_cast<QPlatformColorDialogHelper *>(handle()))
112 return colorDialog->currentColor();
113 return m_currentColor;
114}
115
116void QQuickLabsPlatformColorDialog::setCurrentColor(const QColor &color)
117{
118 if (QPlatformColorDialogHelper *colorDialog = qobject_cast<QPlatformColorDialogHelper *>(handle()))
119 colorDialog->setCurrentColor(color);
120 m_currentColor = color;
121}
122
123/*!
124 \qmlproperty flags Qt.labs.platform::ColorDialog::options
125
126 This property holds the various options that affect the look and feel of the dialog.
127
128 By default, all options are disabled.
129
130 Options should be set before showing the dialog. Setting them while the dialog is
131 visible is not guaranteed to have an immediate effect on the dialog (depending on
132 the option and on the platform).
133
134 Available options:
135 \value ColorDialog.ShowAlphaChannel Allow the user to select the alpha component of a color.
136 \value ColorDialog.NoButtons Don't display \uicontrol OK and \uicontrol Cancel buttons (useful for "live dialogs").
137*/
138QColorDialogOptions::ColorDialogOptions QQuickLabsPlatformColorDialog::options() const
139{
140 return m_options->options();
141}
142
143void QQuickLabsPlatformColorDialog::setOptions(QColorDialogOptions::ColorDialogOptions options)
144{
145 if (options == m_options->options())
146 return;
147
148 m_options->setOptions(options);
149 emit optionsChanged();
150}
151
152bool QQuickLabsPlatformColorDialog::useNativeDialog() const
153{
154 return QQuickLabsPlatformDialog::useNativeDialog()
155 && !m_options->testOption(QColorDialogOptions::DontUseNativeDialog);
156}
157
158void QQuickLabsPlatformColorDialog::onCreate(QPlatformDialogHelper *dialog)
159{
160 if (QPlatformColorDialogHelper *colorDialog = qobject_cast<QPlatformColorDialogHelper *>(dialog)) {
161 connect(colorDialog, &QPlatformColorDialogHelper::currentColorChanged, this, &QQuickLabsPlatformColorDialog::currentColorChanged);
162 colorDialog->setOptions(m_options);
163 colorDialog->setCurrentColor(m_currentColor);
164 }
165}
166
167void QQuickLabsPlatformColorDialog::onShow(QPlatformDialogHelper *dialog)
168{
169 m_options->setWindowTitle(title());
170 if (QPlatformColorDialogHelper *colorDialog = qobject_cast<QPlatformColorDialogHelper *>(dialog))
171 colorDialog->setOptions(m_options);
172}
173
174void QQuickLabsPlatformColorDialog::accept()
175{
176 setColor(currentColor());
177 QQuickLabsPlatformDialog::accept();
178}
179
180QT_END_NAMESPACE
181
182#include "moc_qquicklabsplatformcolordialog_p.cpp"
183
184#endif // QT_DEPRECATED_SINCE(6, 9)