1 | // Copyright (C) 2022 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 "qquickplatformcolordialog_p.h" |
5 | |
6 | #include <QtCore/qloggingcategory.h> |
7 | #include <QtGui/qwindow.h> |
8 | #include <QtQml/qqmlcontext.h> |
9 | #include <QtQml/qqmlinfo.h> |
10 | #include <QtQuick/qquickwindow.h> |
11 | #include <QtQuickTemplates2/private/qquickdialog_p.h> |
12 | #include <QtQuickTemplates2/private/qquickpopup_p_p.h> |
13 | #include <QtQuickTemplates2/private/qquickpopupanchors_p.h> |
14 | |
15 | #include "qquickcolordialogimpl_p.h" |
16 | |
17 | QT_BEGIN_NAMESPACE |
18 | |
19 | Q_LOGGING_CATEGORY(lcQuickPlatformColorDialog, "qt.quick.dialogs.quickplatformcolordialog" ) |
20 | |
21 | QQuickPlatformColorDialog::QQuickPlatformColorDialog(QObject *parent) |
22 | { |
23 | qCDebug(lcQuickPlatformColorDialog) |
24 | << "creating non-native Qt Quick ColorDialog with parent" << parent; |
25 | |
26 | // Set a parent so that we get deleted if we can't be shown for whatever reason. |
27 | // Our eventual parent should be the window, however. |
28 | setParent(parent); |
29 | |
30 | auto qmlContext = ::qmlContext(parent); |
31 | if (!qmlContext) { |
32 | qmlWarning(me: parent) << "No QQmlContext for QQuickPlatformColorDialog; can't create " |
33 | "non-native ColorDialog implementation" ; |
34 | return; |
35 | } |
36 | |
37 | const auto dialogQmlUrl = QUrl(QStringLiteral( |
38 | "qrc:/qt-project.org/imports/QtQuick/Dialogs/quickimpl/qml/ColorDialog.qml" )); |
39 | QQmlComponent colorDialogComponent(qmlContext->engine(), dialogQmlUrl, parent); |
40 | if (!colorDialogComponent.isReady()) { |
41 | qmlWarning(me: parent) << "Failed to load non-native ColorDialog implementation:\n" |
42 | << colorDialogComponent.errorString(); |
43 | return; |
44 | } |
45 | |
46 | m_dialog = qobject_cast<QQuickColorDialogImpl *>(object: colorDialogComponent.create()); |
47 | if (!m_dialog) { |
48 | qmlWarning(me: parent) << "Failed to create an instance of the non-native ColorDialog:\n" |
49 | << colorDialogComponent.errorString(); |
50 | return; |
51 | } |
52 | // Give it a parent until it's parented to the window in show(). |
53 | m_dialog->setParent(this); |
54 | |
55 | connect(sender: m_dialog, signal: &QQuickDialog::accepted, context: this, slot: &QPlatformDialogHelper::accept); |
56 | connect(sender: m_dialog, signal: &QQuickDialog::rejected, context: this, slot: &QPlatformDialogHelper::reject); |
57 | |
58 | connect(sender: m_dialog, signal: &QQuickColorDialogImpl::colorChanged, context: this, |
59 | slot: &QQuickPlatformColorDialog::currentColorChanged); |
60 | } |
61 | |
62 | bool QQuickPlatformColorDialog::isValid() const |
63 | { |
64 | return m_dialog; |
65 | } |
66 | |
67 | void QQuickPlatformColorDialog::setCurrentColor(const QColor &color) |
68 | { |
69 | if (m_dialog) |
70 | m_dialog->setColor(color); |
71 | } |
72 | |
73 | QColor QQuickPlatformColorDialog::currentColor() const |
74 | { |
75 | return m_dialog ? m_dialog->color().toRgb() : QColor(); |
76 | } |
77 | |
78 | void QQuickPlatformColorDialog::exec() |
79 | { |
80 | qCWarning(lcQuickPlatformColorDialog) |
81 | << "exec() is not supported for the Qt Quick ColorDialog fallback" ; |
82 | } |
83 | |
84 | bool QQuickPlatformColorDialog::show(Qt::WindowFlags flags, Qt::WindowModality modality, |
85 | QWindow *parent) |
86 | { |
87 | qCDebug(lcQuickPlatformColorDialog) |
88 | << "show called with flags" << flags << "modality" << modality << "parent" << parent; |
89 | |
90 | if (!m_dialog || !parent) |
91 | return false; |
92 | |
93 | auto quickWindow = qobject_cast<QQuickWindow *>(object: parent); |
94 | |
95 | if (!quickWindow) { |
96 | qmlInfo(me: this->parent()) << "Parent window (" << parent |
97 | << ") of non-native dialog is not a QQuickWindow" ; |
98 | return false; |
99 | } |
100 | |
101 | m_dialog->setParent(parent); |
102 | m_dialog->resetParentItem(); |
103 | |
104 | auto = QQuickPopupPrivate::get(popup: m_dialog); |
105 | popupPrivate->getAnchors()->setCenterIn(m_dialog->parentItem()); |
106 | |
107 | QSharedPointer<QColorDialogOptions> options = QPlatformColorDialogHelper::options(); |
108 | |
109 | m_dialog->setTitle(options->windowTitle()); |
110 | m_dialog->setOptions(options); |
111 | |
112 | m_dialog->open(); |
113 | return true; |
114 | } |
115 | |
116 | void QQuickPlatformColorDialog::hide() |
117 | { |
118 | if (!m_dialog) |
119 | return; |
120 | |
121 | m_dialog->close(); |
122 | } |
123 | |
124 | QQuickColorDialogImpl *QQuickPlatformColorDialog::dialog() const |
125 | { |
126 | return m_dialog; |
127 | } |
128 | |
129 | QT_END_NAMESPACE |
130 | |