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 "qquickqcolordialog_p.h"
41#include "qquickitem.h"
42
43#include <private/qguiapplication_p.h>
44#include <private/qqmlcontext_p.h>
45#include <QWindow>
46#include <QQuickWindow>
47#include <QColorDialog>
48
49QT_BEGIN_NAMESPACE
50
51class QColorDialogHelper : public QPlatformColorDialogHelper
52{
53public:
54 QColorDialogHelper() :
55 QPlatformColorDialogHelper()
56 {
57 connect(sender: &m_dialog, SIGNAL(currentColorChanged(QColor)), receiver: this, SIGNAL(currentColorChanged(QColor)));
58 connect(sender: &m_dialog, SIGNAL(colorSelected(QColor)), receiver: this, SIGNAL(colorSelected(QColor)));
59 connect(sender: &m_dialog, SIGNAL(accepted()), receiver: this, SIGNAL(accept()));
60 connect(sender: &m_dialog, SIGNAL(rejected()), receiver: this, SIGNAL(reject()));
61 }
62
63 void setCurrentColor(const QColor &c) override { m_dialog.setCurrentColor(c); }
64 QColor currentColor() const override { return m_dialog.currentColor(); }
65
66 void exec() override { m_dialog.exec(); }
67
68 bool show(Qt::WindowFlags f, Qt::WindowModality m, QWindow *parent) override {
69 m_dialog.winId();
70 QWindow *window = m_dialog.windowHandle();
71 Q_ASSERT(window);
72 window->setTransientParent(parent);
73 window->setFlags(f);
74 m_dialog.setWindowModality(m);
75 m_dialog.setWindowTitle(QPlatformColorDialogHelper::options()->windowTitle());
76 m_dialog.setOptions((QColorDialog::ColorDialogOptions)((int)(QPlatformColorDialogHelper::options()->options())));
77 m_dialog.show();
78 return m_dialog.isVisible();
79 }
80
81 void hide() override { m_dialog.hide(); }
82
83private:
84 QColorDialog m_dialog;
85};
86
87/*!
88 \qmltype QtColorDialog
89 \instantiates QQuickQColorDialog
90 \inqmlmodule QtQuick.PrivateWidgets
91 \ingroup qtquick-visual
92 \brief Dialog component for choosing a color.
93 \since 5.1
94 \internal
95
96 QtColorDialog provides a means to instantiate and manage a QColorDialog.
97 It is not recommended to be used directly; it is an implementation
98 detail of \l ColorDialog in the \l QtQuick.Dialogs module.
99
100 To use this type, you will need to import the module with the following line:
101 \code
102 import QtQuick.PrivateWidgets 1.0
103 \endcode
104*/
105
106/*!
107 \qmlsignal QtQuick::Dialogs::ColorDialog::accepted
108
109 The \a accepted signal is emitted when the user has finished using the
110 dialog. You can then inspect the \a color property to get the selection.
111
112 Example:
113
114 \qml
115 ColorDialog {
116 onAccepted: { console.log("Selected color: " + color) }
117 }
118 \endqml
119
120 The corresponding handler is \c onAccepted.
121*/
122
123/*!
124 \qmlsignal QtQuick::Dialogs::ColorDialog::rejected
125
126 The \a rejected signal is emitted when the user has dismissed the dialog,
127 either by closing the dialog window or by pressing the Cancel button.
128
129 The corresponding handler is \c onRejected.
130*/
131
132/*!
133 \class QQuickQColorDialog
134 \inmodule QtQuick.PrivateWidgets
135 \internal
136
137 \brief The QQuickQColorDialog class is a wrapper for a QColorDialog.
138
139 \since 5.1
140*/
141
142/*!
143 Constructs a file dialog with parent window \a parent.
144*/
145QQuickQColorDialog::QQuickQColorDialog(QObject *parent)
146 : QQuickAbstractColorDialog(parent)
147{
148}
149
150/*!
151 Destroys the file dialog.
152*/
153QQuickQColorDialog::~QQuickQColorDialog()
154{
155 if (m_dlgHelper)
156 m_dlgHelper->hide();
157 delete m_dlgHelper;
158}
159
160QPlatformColorDialogHelper *QQuickQColorDialog::helper()
161{
162 QQuickItem *parentItem = qobject_cast<QQuickItem *>(object: parent());
163 if (parentItem)
164 m_parentWindow = parentItem->window();
165
166 if (!m_dlgHelper) {
167 m_dlgHelper = new QColorDialogHelper();
168 connect(sender: m_dlgHelper, SIGNAL(currentColorChanged(QColor)), receiver: this, SLOT(setCurrentColor(QColor)));
169 connect(sender: m_dlgHelper, SIGNAL(colorSelected(QColor)), receiver: this, SLOT(setColor(QColor)));
170 connect(sender: m_dlgHelper, SIGNAL(accept()), receiver: this, SLOT(accept()));
171 connect(sender: m_dlgHelper, SIGNAL(reject()), receiver: this, SLOT(reject()));
172 }
173
174 return m_dlgHelper;
175}
176
177QT_END_NAMESPACE
178

source code of qtquickcontrols/src/widgets/qquickqcolordialog.cpp