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