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