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