1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Labs Platform module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qquickplatformcolordialog_p.h"
38
39QT_BEGIN_NAMESPACE
40
41/*!
42 \qmltype ColorDialog
43 \inherits Dialog
44//! \instantiates QQuickPlatformColorDialog
45 \inqmlmodule Qt.labs.platform
46 \since 5.8
47 \brief A native color dialog.
48
49 The ColorDialog type provides a QML API for native platform color dialogs.
50
51 \image qtlabsplatform-colordialog-gtk.png
52
53 To show a color dialog, construct an instance of ColorDialog, set the
54 desired properties, and call \l {Dialog::}{open()}. The \l currentColor
55 property can be used to determine the currently selected color in the
56 dialog. The \l color property is updated only after the final selection
57 has been made by accepting the dialog.
58
59 \code
60 MenuItem {
61 text: "Color"
62 onTriggered: colorDialog.open()
63 }
64
65 ColorDialog {
66 id: colorDialog
67 currentColor: document.color
68 }
69
70 MyDocument {
71 id: document
72 color: colorDialog.color
73 }
74 \endcode
75
76 \section2 Availability
77
78 A native platform color dialog is currently available on the following platforms:
79
80 \list
81 \li macOS
82 \li Linux (when running with the GTK+ platform theme)
83 \endlist
84
85 \input includes/widgets.qdocinc 1
86
87 \labs
88*/
89
90QQuickPlatformColorDialog::QQuickPlatformColorDialog(QObject *parent)
91 : QQuickPlatformDialog(QPlatformTheme::ColorDialog, parent),
92 m_options(QColorDialogOptions::create())
93{
94}
95
96/*!
97 \qmlproperty color Qt.labs.platform::ColorDialog::color
98
99 This property holds the final accepted color.
100
101 Unlike the \l currentColor property, the \c color property is not updated
102 while the user is selecting colors in the dialog, but only after the final
103 selection has been made. That is, when the user has clicked \uicontrol OK
104 to accept a color. Alternatively, the \l {Dialog::}{accepted()} signal
105 can be handled to get the final selection.
106
107 \sa currentColor, {Dialog::}{accepted()}
108*/
109QColor QQuickPlatformColorDialog::color() const
110{
111 return m_color;
112}
113
114void QQuickPlatformColorDialog::setColor(const QColor &color)
115{
116 if (m_color == color)
117 return;
118
119 m_color = color;
120 setCurrentColor(color);
121 emit colorChanged();
122}
123
124/*!
125 \qmlproperty color Qt.labs.platform::ColorDialog::currentColor
126
127 This property holds the currently selected color in the dialog.
128
129 Unlike the \l color property, the \c currentColor property is updated
130 while the user is selecting colors in the dialog, even before the final
131 selection has been made.
132
133 \sa color
134*/
135QColor QQuickPlatformColorDialog::currentColor() const
136{
137 if (QPlatformColorDialogHelper *colorDialog = qobject_cast<QPlatformColorDialogHelper *>(object: handle()))
138 return colorDialog->currentColor();
139 return m_currentColor;
140}
141
142void QQuickPlatformColorDialog::setCurrentColor(const QColor &color)
143{
144 if (QPlatformColorDialogHelper *colorDialog = qobject_cast<QPlatformColorDialogHelper *>(object: handle()))
145 colorDialog->setCurrentColor(color);
146 m_currentColor = color;
147}
148
149/*!
150 \qmlproperty flags Qt.labs.platform::ColorDialog::options
151
152 This property holds the various options that affect the look and feel of the dialog.
153
154 By default, all options are disabled.
155
156 Options should be set before showing the dialog. Setting them while the dialog is
157 visible is not guaranteed to have an immediate effect on the dialog (depending on
158 the option and on the platform).
159
160 Available options:
161 \value ColorDialog.ShowAlphaChannel Allow the user to select the alpha component of a color.
162 \value ColorDialog.NoButtons Don't display \uicontrol OK and \uicontrol Cancel buttons (useful for "live dialogs").
163*/
164QColorDialogOptions::ColorDialogOptions QQuickPlatformColorDialog::options() const
165{
166 return m_options->options();
167}
168
169void QQuickPlatformColorDialog::setOptions(QColorDialogOptions::ColorDialogOptions options)
170{
171 if (options == m_options->options())
172 return;
173
174 m_options->setOptions(options);
175 emit optionsChanged();
176}
177
178bool QQuickPlatformColorDialog::useNativeDialog() const
179{
180 return QQuickPlatformDialog::useNativeDialog()
181 && !m_options->testOption(option: QColorDialogOptions::DontUseNativeDialog);
182}
183
184void QQuickPlatformColorDialog::onCreate(QPlatformDialogHelper *dialog)
185{
186 if (QPlatformColorDialogHelper *colorDialog = qobject_cast<QPlatformColorDialogHelper *>(object: dialog)) {
187 connect(sender: colorDialog, signal: &QPlatformColorDialogHelper::currentColorChanged, receiver: this, slot: &QQuickPlatformColorDialog::currentColorChanged);
188 colorDialog->setOptions(m_options);
189 colorDialog->setCurrentColor(m_currentColor);
190 }
191}
192
193void QQuickPlatformColorDialog::onShow(QPlatformDialogHelper *dialog)
194{
195 m_options->setWindowTitle(title());
196 if (QPlatformColorDialogHelper *colorDialog = qobject_cast<QPlatformColorDialogHelper *>(object: dialog))
197 colorDialog->setOptions(m_options);
198}
199
200void QQuickPlatformColorDialog::accept()
201{
202 setColor(currentColor());
203 QQuickPlatformDialog::accept();
204}
205
206QT_END_NAMESPACE
207

source code of qtquickcontrols2/src/imports/platform/qquickplatformcolordialog.cpp