| 1 | // Copyright (C) 2021 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 "qquickplatformfiledialog_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 <QtQuickDialogs2Utils/private/qquickfilenamefilter_p.h> |
| 12 | #include <QtQuickTemplates2/private/qquickdialog_p.h> |
| 13 | #include <QtQuickTemplates2/private/qquickpopup_p_p.h> |
| 14 | #include <QtQuickTemplates2/private/qquickpopupanchors_p.h> |
| 15 | |
| 16 | #include "qquickfiledialogimpl_p.h" |
| 17 | |
| 18 | QT_BEGIN_NAMESPACE |
| 19 | |
| 20 | Q_LOGGING_CATEGORY(lcQuickPlatformFileDialog, "qt.quick.dialogs.quickplatformfiledialog" ) |
| 21 | |
| 22 | /*! |
| 23 | \class QQuickPlatformFileDialog |
| 24 | \internal |
| 25 | |
| 26 | An interface that QQuickFileDialog can use to access the non-native Qt Quick FileDialog. |
| 27 | |
| 28 | Both this and the native implementations are created in QQuickAbstractDialog::create(). |
| 29 | */ |
| 30 | QQuickPlatformFileDialog::QQuickPlatformFileDialog(QObject *parent) |
| 31 | { |
| 32 | qCDebug(lcQuickPlatformFileDialog) << "creating non-native Qt Quick FileDialog with parent" << parent; |
| 33 | |
| 34 | // Set a parent so that we get deleted if we can't be shown for whatever reason. |
| 35 | // Our eventual parent should be the window, though. |
| 36 | setParent(parent); |
| 37 | |
| 38 | auto qmlContext = ::qmlContext(parent); |
| 39 | if (!qmlContext) { |
| 40 | qmlWarning(me: parent) << "No QQmlContext for QQuickPlatformFileDialog; can't create non-native FileDialog implementation" ; |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | const auto dialogQmlUrl = QUrl(QStringLiteral("qrc:/qt-project.org/imports/QtQuick/Dialogs/quickimpl/qml/FileDialog.qml" )); |
| 45 | QQmlComponent fileDialogComponent(qmlContext->engine(), dialogQmlUrl, parent); |
| 46 | if (!fileDialogComponent.isReady()) { |
| 47 | qmlWarning(me: parent) << "Failed to load non-native FileDialog implementation:\n" << fileDialogComponent.errorString(); |
| 48 | return; |
| 49 | } |
| 50 | m_dialog = qobject_cast<QQuickFileDialogImpl*>(object: fileDialogComponent.create()); |
| 51 | if (!m_dialog) { |
| 52 | qmlWarning(me: parent) << "Failed to create an instance of the non-native FileDialog:\n" << fileDialogComponent.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(sender: m_dialog, signal: &QQuickDialog::accepted, context: this, slot: &QPlatformDialogHelper::accept); |
| 59 | connect(sender: m_dialog, signal: &QQuickDialog::rejected, context: this, slot: &QPlatformDialogHelper::reject); |
| 60 | |
| 61 | connect(sender: m_dialog, signal: &QQuickFileDialogImpl::fileSelected, context: this, slot: &QQuickPlatformFileDialog::fileSelected); |
| 62 | // TODO: add support for multiple file selection (QTBUG-92585) |
| 63 | // connect(m_dialog, &QQuickFileDialogImpl::filesSelected, [this](const QList<QString> &files) { |
| 64 | // QList<QUrl> urls; |
| 65 | // urls.reserve(files.count()); |
| 66 | // for (const QString &file : files) |
| 67 | // urls += QUrl::fromLocalFile(file); |
| 68 | // emit filesSelected(urls); |
| 69 | // }); |
| 70 | connect(sender: m_dialog, signal: &QQuickFileDialogImpl::selectedFileChanged, context: this, slot: &QQuickPlatformFileDialog::currentChanged); |
| 71 | connect(sender: m_dialog, signal: &QQuickFileDialogImpl::currentFolderChanged, context: this, slot: &QQuickPlatformFileDialog::directoryEntered); |
| 72 | connect(sender: m_dialog, signal: &QQuickFileDialogImpl::filterSelected, context: this, slot: &QQuickPlatformFileDialog::filterSelected); |
| 73 | } |
| 74 | |
| 75 | bool QQuickPlatformFileDialog::isValid() const |
| 76 | { |
| 77 | return m_dialog; |
| 78 | } |
| 79 | |
| 80 | bool QQuickPlatformFileDialog::defaultNameFilterDisables() const |
| 81 | { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | void QQuickPlatformFileDialog::setDirectory(const QUrl &directory) |
| 86 | { |
| 87 | if (!m_dialog) |
| 88 | return; |
| 89 | |
| 90 | m_dialog->setCurrentFolder(currentFolder: directory); |
| 91 | } |
| 92 | |
| 93 | QUrl QQuickPlatformFileDialog::directory() const |
| 94 | { |
| 95 | if (!m_dialog) |
| 96 | return {}; |
| 97 | |
| 98 | return m_dialog->currentFolder(); |
| 99 | } |
| 100 | |
| 101 | void QQuickPlatformFileDialog::selectFile(const QUrl &file) |
| 102 | { |
| 103 | if (!m_dialog) |
| 104 | return; |
| 105 | |
| 106 | if (m_dialog->isVisible()) { |
| 107 | qWarning() << "Cannot set an initial selectedFile while FileDialog is open" ; |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | // Since we're only called once each time the FileDialog is shown, |
| 112 | // we call setInitialCurrentFolderAndSelectedFile here, which will ensure that |
| 113 | // the first currentIndex change (to 0, as a result of the ListView's model changing |
| 114 | // as a result of the FolderListModel directory change) is effectively |
| 115 | // ignored and the correct index for the initial selectedFile is maintained. |
| 116 | m_dialog->setInitialCurrentFolderAndSelectedFile(file); |
| 117 | } |
| 118 | |
| 119 | // TODO: support for multiple selected files |
| 120 | QList<QUrl> QQuickPlatformFileDialog::selectedFiles() const |
| 121 | { |
| 122 | if (m_dialog->selectedFile().isEmpty()) |
| 123 | return {}; |
| 124 | |
| 125 | return { m_dialog->selectedFile() }; |
| 126 | } |
| 127 | |
| 128 | void QQuickPlatformFileDialog::setFilter() |
| 129 | { |
| 130 | } |
| 131 | |
| 132 | void QQuickPlatformFileDialog::selectNameFilter(const QString &filter) |
| 133 | { |
| 134 | // There is a bit of a problem with order here - QQuickFileDialog::onShow() |
| 135 | // is called before our show(), but it needs to set the selected filter |
| 136 | // (which we can't do in our show() because we don't know about QQuickFileDialog). |
| 137 | // So, delay setting the filter until we're shown. This shouldn't be an issue |
| 138 | // in practice, since it doesn't make sense for the filter to programmatically |
| 139 | // change while the dialog is visible. |
| 140 | m_pendingNameFilter = filter; |
| 141 | } |
| 142 | |
| 143 | QString QQuickPlatformFileDialog::selectedNameFilter() const |
| 144 | { |
| 145 | return m_dialog->selectedNameFilter()->name(); |
| 146 | } |
| 147 | |
| 148 | void QQuickPlatformFileDialog::exec() |
| 149 | { |
| 150 | qCWarning(lcQuickPlatformFileDialog) << "exec() is not supported for the Qt Quick FileDialog fallback" ; |
| 151 | } |
| 152 | |
| 153 | /*! |
| 154 | \internal |
| 155 | |
| 156 | This is called after QQuickFileDialog::onShow(). |
| 157 | Both are called in QQuickAbstractDialog::open(). |
| 158 | */ |
| 159 | bool QQuickPlatformFileDialog::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent) |
| 160 | { |
| 161 | qCDebug(lcQuickPlatformFileDialog) << "show called with flags" << flags << |
| 162 | "modality" << modality << "parent" << parent; |
| 163 | if (!m_dialog) |
| 164 | return false; |
| 165 | |
| 166 | if (!parent) |
| 167 | return false; |
| 168 | |
| 169 | auto quickWindow = qobject_cast<QQuickWindow*>(object: parent); |
| 170 | if (!quickWindow) { |
| 171 | qmlInfo(me: this->parent()) << "Parent window (" << parent << ") of non-native dialog is not a QQuickWindow" ; |
| 172 | return false; |
| 173 | } |
| 174 | m_dialog->setParent(parent); |
| 175 | m_dialog->resetParentItem(); |
| 176 | |
| 177 | auto = QQuickPopupPrivate::get(popup: m_dialog); |
| 178 | popupPrivate->getAnchors()->setCenterIn(m_dialog->parentItem()); |
| 179 | |
| 180 | QSharedPointer<QFileDialogOptions> options = QPlatformFileDialogHelper::options(); |
| 181 | m_dialog->setTitle(options->windowTitle()); |
| 182 | m_dialog->setOptions(options); |
| 183 | m_dialog->selectNameFilter(filter: m_pendingNameFilter); |
| 184 | m_pendingNameFilter.clear(); |
| 185 | m_dialog->setAcceptLabel(options->isLabelExplicitlySet(label: QFileDialogOptions::Accept) |
| 186 | ? options->labelText(label: QFileDialogOptions::Accept) : QString()); |
| 187 | m_dialog->setRejectLabel(options->isLabelExplicitlySet(label: QFileDialogOptions::Reject) |
| 188 | ? options->labelText(label: QFileDialogOptions::Reject) : QString()); |
| 189 | |
| 190 | if (options->initiallySelectedFiles().isEmpty()) { |
| 191 | if (m_dialog->currentFolder().isEmpty()) { |
| 192 | // The user didn't set an initial selectedFile nor currentFolder, so we'll set it to the working directory. |
| 193 | qCDebug(lcQuickPlatformFileDialog) << "- calling setCurrentFolder(QDir()) on quick dialog" << parent; |
| 194 | m_dialog->setCurrentFolder(currentFolder: QUrl::fromLocalFile(localfile: QDir().absolutePath())); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | m_dialog->open(); |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | void QQuickPlatformFileDialog::hide() |
| 203 | { |
| 204 | if (!m_dialog) |
| 205 | return; |
| 206 | |
| 207 | m_dialog->close(); |
| 208 | } |
| 209 | |
| 210 | QQuickFileDialogImpl *QQuickPlatformFileDialog::dialog() const |
| 211 | { |
| 212 | return m_dialog; |
| 213 | } |
| 214 | |
| 215 | QT_END_NAMESPACE |
| 216 | |
| 217 | #include "moc_qquickplatformfiledialog_p.cpp" |
| 218 | |