1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Quick Dialogs module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
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 https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qquickplatformcolordialog_p.h" |
41 | #include "qquickitem.h" |
42 | |
43 | #include <private/qguiapplication_p.h> |
44 | #include <QWindow> |
45 | #include <QQuickView> |
46 | #include <QQuickWindow> |
47 | |
48 | QT_BEGIN_NAMESPACE |
49 | |
50 | /*! |
51 | \qmltype ColorDialog |
52 | \instantiates QQuickPlatformColorDialog1 |
53 | \inqmlmodule QtQuick.Dialogs |
54 | \ingroup qtquickdialogs |
55 | \brief Dialog component for choosing a color. |
56 | \since 5.1 |
57 | |
58 | ColorDialog allows the user to select a color. The dialog is initially |
59 | invisible. You need to set the properties as desired first, then set |
60 | \l visible to true or call \l open(). |
61 | |
62 | Here is a minimal example to open a color dialog and exit after the user |
63 | chooses a color: |
64 | |
65 | \qml |
66 | import QtQuick 2.2 |
67 | import QtQuick.Dialogs 1.0 |
68 | |
69 | ColorDialog { |
70 | id: colorDialog |
71 | title: "Please choose a color" |
72 | onAccepted: { |
73 | console.log("You chose: " + colorDialog.color) |
74 | Qt.quit() |
75 | } |
76 | onRejected: { |
77 | console.log("Canceled") |
78 | Qt.quit() |
79 | } |
80 | Component.onCompleted: visible = true |
81 | } |
82 | \endqml |
83 | |
84 | A ColorDialog window is automatically transient for its parent window. So |
85 | whether you declare the dialog inside an \l Item or inside a \l Window, the |
86 | dialog will appear centered over the window containing the item, or over |
87 | the Window that you declared. |
88 | |
89 | The implementation of ColorDialog will be a platform color dialog if |
90 | possible. If that isn't possible, then it will try to instantiate a |
91 | \l QColorDialog. If that also isn't possible, then it will fall back to a QML |
92 | implementation, DefaultColorDialog.qml. In that case you can customize the |
93 | appearance by editing this file. DefaultColorDialog.qml contains a Rectangle |
94 | to hold the dialog's contents, because certain embedded systems do not |
95 | support multiple top-level windows. When the dialog becomes visible, it |
96 | will automatically be wrapped in a Window if possible, or simply reparented |
97 | on top of the main window if there can only be one window. |
98 | */ |
99 | |
100 | /*! |
101 | \qmlsignal QtQuick::Dialogs::ColorDialog::accepted |
102 | |
103 | This signal is emitted when the user has finished using the |
104 | dialog. You can then inspect the \l color property to get the selection. |
105 | |
106 | Example: |
107 | |
108 | \qml |
109 | ColorDialog { |
110 | onAccepted: { console.log("Selected color: " + color) } |
111 | } |
112 | \endqml |
113 | |
114 | The corresponding handler is \c onAccepted. |
115 | */ |
116 | |
117 | /*! |
118 | \qmlsignal QtQuick::Dialogs::ColorDialog::rejected |
119 | |
120 | This signal is emitted when the user has dismissed the dialog, |
121 | either by closing the dialog window or by pressing the Cancel button. |
122 | |
123 | The corresponding handler is \c onRejected. |
124 | */ |
125 | |
126 | /*! |
127 | \class QQuickPlatformColorDialog1 |
128 | \inmodule QtQuick.Dialogs |
129 | \internal |
130 | |
131 | \brief The QQuickPlatformColorDialog1 class provides a color dialog |
132 | |
133 | The dialog is implemented via the QPlatformColorDialogHelper when possible; |
134 | otherwise it falls back to a QColorDialog or a QML implementation. |
135 | |
136 | \since 5.1 |
137 | */ |
138 | |
139 | /*! |
140 | Constructs a color dialog with parent window \a parent. |
141 | */ |
142 | QQuickPlatformColorDialog1::QQuickPlatformColorDialog1(QObject *parent) : |
143 | QQuickAbstractColorDialog(parent) |
144 | { |
145 | } |
146 | |
147 | /*! |
148 | Destroys the color dialog. |
149 | */ |
150 | QQuickPlatformColorDialog1::~QQuickPlatformColorDialog1() |
151 | { |
152 | if (m_dlgHelper) |
153 | m_dlgHelper->hide(); |
154 | delete m_dlgHelper; |
155 | } |
156 | |
157 | QPlatformColorDialogHelper *QQuickPlatformColorDialog1::helper() |
158 | { |
159 | QQuickItem *parentItem = qobject_cast<QQuickItem *>(object: parent()); |
160 | if (parentItem) |
161 | m_parentWindow = parentItem->window(); |
162 | |
163 | if ( !m_dlgHelper && QGuiApplicationPrivate::platformTheme()-> |
164 | usePlatformNativeDialog(type: QPlatformTheme::ColorDialog) ) { |
165 | m_dlgHelper = static_cast<QPlatformColorDialogHelper *>(QGuiApplicationPrivate::platformTheme() |
166 | ->createPlatformDialogHelper(type: QPlatformTheme::ColorDialog)); |
167 | if (!m_dlgHelper) |
168 | return m_dlgHelper; |
169 | connect(sender: m_dlgHelper, SIGNAL(accept()), receiver: this, SLOT(accept())); |
170 | connect(sender: m_dlgHelper, SIGNAL(reject()), receiver: this, SLOT(reject())); |
171 | connect(sender: m_dlgHelper, SIGNAL(currentColorChanged(QColor)), receiver: this, SLOT(setCurrentColor(QColor))); |
172 | connect(sender: m_dlgHelper, SIGNAL(colorSelected(QColor)), receiver: this, SLOT(setColor(QColor))); |
173 | } |
174 | |
175 | return m_dlgHelper; |
176 | } |
177 | |
178 | /*! |
179 | \qmlproperty bool ColorDialog::visible |
180 | |
181 | This property holds whether the dialog is visible. By default this is |
182 | false. |
183 | |
184 | \sa modality |
185 | */ |
186 | |
187 | /*! |
188 | \qmlproperty Qt::WindowModality ColorDialog::modality |
189 | |
190 | Whether the dialog should be shown modal with respect to the window |
191 | containing the dialog's parent Item, modal with respect to the whole |
192 | application, or non-modal. |
193 | |
194 | By default it is \c Qt.NonModal. |
195 | |
196 | Modality does not mean that there are any blocking calls to wait for the |
197 | dialog to be accepted or rejected; it's only that the user will be |
198 | prevented from interacting with the parent window and/or the application |
199 | windows at the same time. |
200 | |
201 | You probably need to write an onAccepted handler if you wish to change a |
202 | color after the user has pressed the OK button, or an |
203 | onCurrentColorChanged handler if you wish to react to every change the |
204 | user makes while the dialog is open. |
205 | |
206 | On MacOS the color dialog is only allowed to be non-modal. |
207 | */ |
208 | |
209 | /*! |
210 | \qmlmethod void ColorDialog::open() |
211 | |
212 | Shows the dialog to the user. It is equivalent to setting \l visible to |
213 | true. |
214 | */ |
215 | |
216 | /*! |
217 | \qmlmethod void ColorDialog::close() |
218 | |
219 | Closes the dialog. |
220 | */ |
221 | |
222 | /*! |
223 | \qmlproperty string ColorDialog::title |
224 | |
225 | The title of the dialog window. |
226 | */ |
227 | |
228 | /*! |
229 | \qmlproperty bool ColorDialog::showAlphaChannel |
230 | |
231 | Whether the dialog will provide a means of changing the opacity. |
232 | |
233 | By default, this property is true. This property must be set to the desired |
234 | value before opening the dialog. Usually the alpha channel is represented |
235 | by an additional slider control. |
236 | */ |
237 | |
238 | /*! |
239 | \qmlproperty color ColorDialog::color |
240 | |
241 | The color which the user selected. |
242 | |
243 | \note This color is not always the same as the color held by the |
244 | currentColor property since the user can choose different colors before |
245 | finally selecting the one to use. |
246 | |
247 | \sa currentColor |
248 | */ |
249 | |
250 | /*! |
251 | \qmlproperty color ColorDialog::currentColor |
252 | |
253 | The color which the user has currently selected. |
254 | |
255 | For the color that is set when the dialog is accepted, use the \l color |
256 | property. |
257 | |
258 | \sa color |
259 | */ |
260 | |
261 | QT_END_NAMESPACE |
262 | |