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