diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2016-06-18 14:45:56 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2016-06-27 13:07:49 +0000 |
commit | 4960f639e338e36b595667f02e2b5fac51aaa07a (patch) | |
tree | 2d1d6784cfe24d96312e67e0090a60f1edf48a50 /src | |
parent | beb7d695a8051bb800f6f595140c4b5e1c5c15bc (diff) |
Platform: add FileDialog and FolderDialog
Change-Id: I4328c273e48139d6ddd5a3d3c8492d06f48a5c24
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/imports/platform/doc/images/qtlabsplatform-filedialog-gtk.png | bin | 0 -> 49506 bytes | |||
-rw-r--r-- | src/imports/platform/doc/images/qtlabsplatform-folderdialog-gtk.png | bin | 0 -> 49092 bytes | |||
-rw-r--r-- | src/imports/platform/platform.pri | 4 | ||||
-rw-r--r-- | src/imports/platform/qquickplatformfiledialog.cpp | 365 | ||||
-rw-r--r-- | src/imports/platform/qquickplatformfiledialog_p.h | 130 | ||||
-rw-r--r-- | src/imports/platform/qquickplatformfolderdialog.cpp | 262 | ||||
-rw-r--r-- | src/imports/platform/qquickplatformfolderdialog_p.h | 105 | ||||
-rw-r--r-- | src/imports/platform/qtlabsplatformplugin.cpp | 4 | ||||
-rw-r--r-- | src/imports/platform/widgets/qwidgetplatformfiledialog.cpp | 152 | ||||
-rw-r--r-- | src/imports/platform/widgets/qwidgetplatformfiledialog_p.h | 84 | ||||
-rw-r--r-- | src/imports/platform/widgets/widgets.pri | 2 |
11 files changed, 1108 insertions, 0 deletions
diff --git a/src/imports/platform/doc/images/qtlabsplatform-filedialog-gtk.png b/src/imports/platform/doc/images/qtlabsplatform-filedialog-gtk.png Binary files differnew file mode 100644 index 00000000..3bc963d0 --- /dev/null +++ b/src/imports/platform/doc/images/qtlabsplatform-filedialog-gtk.png diff --git a/src/imports/platform/doc/images/qtlabsplatform-folderdialog-gtk.png b/src/imports/platform/doc/images/qtlabsplatform-folderdialog-gtk.png Binary files differnew file mode 100644 index 00000000..ea4d929d --- /dev/null +++ b/src/imports/platform/doc/images/qtlabsplatform-folderdialog-gtk.png diff --git a/src/imports/platform/platform.pri b/src/imports/platform/platform.pri index 6b3cd04d..14b39246 100644 --- a/src/imports/platform/platform.pri +++ b/src/imports/platform/platform.pri @@ -1,6 +1,8 @@ HEADERS += \ $$PWD/qquickplatformcolordialog_p.h \ $$PWD/qquickplatformdialog_p.h \ + $$PWD/qquickplatformfiledialog_p.h \ + $$PWD/qquickplatformfolderdialog_p.h \ $$PWD/qquickplatformfontdialog_p.h \ $$PWD/qquickplatformiconloader_p.h \ $$PWD/qquickplatformmenu_p.h \ @@ -13,6 +15,8 @@ HEADERS += \ SOURCES += \ $$PWD/qquickplatformcolordialog.cpp \ $$PWD/qquickplatformdialog.cpp \ + $$PWD/qquickplatformfiledialog.cpp \ + $$PWD/qquickplatformfolderdialog.cpp \ $$PWD/qquickplatformfontdialog.cpp \ $$PWD/qquickplatformiconloader.cpp \ $$PWD/qquickplatformmenu.cpp \ diff --git a/src/imports/platform/qquickplatformfiledialog.cpp b/src/imports/platform/qquickplatformfiledialog.cpp new file mode 100644 index 00000000..f6d99cb3 --- /dev/null +++ b/src/imports/platform/qquickplatformfiledialog.cpp @@ -0,0 +1,365 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Labs Platform module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickplatformfiledialog_p.h" + +#include <QtGui/qpa/qplatformtheme.h> +#include <QtGui/private/qguiapplication_p.h> + +#ifdef QT_WIDGETS_LIB +#include "widgets/qwidgetplatformfiledialog_p.h" +#endif + +QT_BEGIN_NAMESPACE + +/*! + \qmltype FileDialog + \inherits Dialog + \instantiates QQuickPlatformFileDialog + \inqmlmodule Qt.labs.platform + \since 5.8 + \brief A native file dialog. + + The FileDialog type provides a QML API for native platform file dialogs. + + \image qtlabsplatform-filedialog-gtk.png + + To show a file dialog, construct an instance of FileDialog, set the + desired properties, and call \l {Dialog::}{open()}. FileDialog emits + the \l fileSelected() and \l filesSelected() signals when the user has + selected file(s). + + \code + MenuItem { + text: "Open..." + onTriggered: fileDialog.open() + } + + FileDialog { + id: fileDialog + currentFile: document.source + onFileSelected: document.source = file + } + \endcode + + \section2 Availability + + A native platform file dialog is currently available on the following platforms: + + \list + \li iOS + \li Linux (when running with the GTK+ platform theme) + \li macOS + \li Windows + \li WinRT + \endlist + + \input includes/widgets.qdocinc 1 + + \labs + + \sa FolderDialog +*/ + +/*! + \qmlsignal void Qt.labs.platform::FileDialog::fileSelected(url file) + + This signal is emitted when a \a file has been selected. + + \sa filesSelected(), currentFile +*/ + +/*! + \qmlsignal void Qt.labs.platform::FileDialog::filesSelected(list<url> files) + + This signal is emitted when multiple \a files have been selected. + + \sa fileSelected(), currentFile +*/ + +QQuickPlatformFileDialog::QQuickPlatformFileDialog(QObject *parent) + : QQuickPlatformDialog(parent), m_fileMode(OpenFile), m_options(QFileDialogOptions::create()) +{ + m_options->setFileMode(QFileDialogOptions::ExistingFile); + m_options->setAcceptMode(QFileDialogOptions::AcceptOpen); + + QPlatformDialogHelper *dialog = QGuiApplicationPrivate::platformTheme()->createPlatformDialogHelper(QPlatformTheme::FileDialog); +#ifdef QT_WIDGETS_LIB + if (!dialog) + dialog = new QWidgetPlatformFileDialog(this); +#endif + if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(dialog)) { + connect(fileDialog, &QPlatformFileDialogHelper::fileSelected, this, &QQuickPlatformFileDialog::fileSelected); + connect(fileDialog, &QPlatformFileDialogHelper::filesSelected, this, &QQuickPlatformFileDialog::filesSelected); + connect(fileDialog, &QPlatformFileDialogHelper::currentChanged, [this](const QUrl &url) { + if (m_current == url) + return; + m_current = url; + emit currentFileChanged(); + }); + fileDialog->setOptions(m_options); + } + setHandle(dialog); +} + +/*! + \qmlproperty enumeration Qt.labs.platform::FileDialog::fileMode + + This property holds the mode of the dialog. + + Available values: + \value FileDialog.OpenFile The dialog is used to select an existing file (default). + \value FileDialog.OpenFiles The dialog is used to select multiple existing files. + \value FileDialog.SaveFile The dialog is used to select any file. The file does not have to exist. +*/ +QQuickPlatformFileDialog::FileMode QQuickPlatformFileDialog::fileMode() const +{ + return m_fileMode; +} + +void QQuickPlatformFileDialog::setFileMode(FileMode mode) +{ + if (mode == m_fileMode) + return; + + switch (mode) { + case OpenFile: + m_options->setFileMode(QFileDialogOptions::ExistingFile); + m_options->setAcceptMode(QFileDialogOptions::AcceptOpen); + break; + case OpenFiles: + m_options->setFileMode(QFileDialogOptions::ExistingFiles); + m_options->setAcceptMode(QFileDialogOptions::AcceptOpen); + break; + case SaveFile: + m_options->setFileMode(QFileDialogOptions::AnyFile); + m_options->setAcceptMode(QFileDialogOptions::AcceptSave); + break; + default: + break; + } + + m_fileMode = mode; + emit fileModeChanged(); +} + +/*! + \qmlproperty url Qt.labs.platform::FileDialog::currentFile + + This property holds the currently selected file in the dialog. + + \sa fileSelected(), filesSelected() +*/ +QUrl QQuickPlatformFileDialog::currentFile() const +{ + if (m_current.isEmpty()) { + if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(handle())) + m_current = fileDialog->selectedFiles().value(0); + } + return m_current; +} + +void QQuickPlatformFileDialog::setCurrentFile(const QUrl &file) +{ + if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(handle())) + fileDialog->selectFile(file); +} + +/*! + \qmlproperty flags Qt.labs.platform::FileDialog::options + + This property holds the various options that affect the look and feel of the dialog. + + By default, all options are disabled. + + Options should be set before showing the dialog. Setting them while the dialog is + visible is not guaranteed to have an immediate effect on the dialog (depending on + the option and on the platform). + + Available options: + \value FileDialog.DontResolveSymlinks Don't resolve symlinks in the file dialog. By default symlinks are resolved. + \value FileDialog.DontConfirmOverwrite Don't ask for confirmation if an existing file is selected. By default confirmation is requested. + \value FileDialog.ReadOnly Indicates that the dialog doesn't allow creating directories. + \value FileDialog.HideNameFilterDetails Indicates if the file name filter details are hidden or not. +*/ +QFileDialogOptions::FileDialogOptions QQuickPlatformFileDialog::options() const +{ + return m_options->options(); +} + +void QQuickPlatformFileDialog::setOptions(QFileDialogOptions::FileDialogOptions options) +{ + if (options == m_options->options()) + return; + + m_options->setOptions(options); + emit optionsChanged(); +} + +void QQuickPlatformFileDialog::resetOptions() +{ + setOptions(0); +} + +/*! + \qmlproperty list<string> Qt.labs.platform::FileDialog::nameFilters + + This property holds the filters that restrict the types of files that + can be selected. + + \note \b{*.*} is not a portable filter, because the historical assumption + that the file extension determines the file type is not consistent on every + operating system. It is possible to have a file with no dot in its name (for + example, \c Makefile). In a native Windows file dialog, \b{*.*} will match + such files, while in other types of file dialogs it may not. So it is better + to use \b{*} if you mean to select any file. +*/ +QStringList QQuickPlatformFileDialog::nameFilters() const +{ + return m_options->nameFilters(); +} + +void QQuickPlatformFileDialog::setNameFilters(const QStringList &filters) +{ + if (filters == m_options->nameFilters()) + return; + + m_options->setNameFilters(filters); + emit nameFiltersChanged(); +} + +void QQuickPlatformFileDialog::resetNameFilters() +{ + setNameFilters(QStringList()); +} + +/*! + \qmlproperty string Qt.labs.platform::FileDialog::defaultSuffix + + This property holds a suffix that is added to selected files that have + no suffix specified. The suffix is typically used to indicate the file + type (e.g. "txt" indicates a text file). + + If the first character is a dot ('.'), it is removed. +*/ +QString QQuickPlatformFileDialog::defaultSuffix() const +{ + return m_options->defaultSuffix(); +} + +void QQuickPlatformFileDialog::setDefaultSuffix(const QString &suffix) +{ + if (suffix == m_options->defaultSuffix()) + return; + + m_options->setDefaultSuffix(suffix); + emit defaultSuffixChanged(); +} + +void QQuickPlatformFileDialog::resetDefaultSuffix() +{ + setDefaultSuffix(QString()); +} + +/*! + \qmlproperty string Qt.labs.platform::FileDialog::acceptLabel + + This property holds the label text shown on the button that accepts the dialog. + + When set to an empty string, the default label of the underlying platform is used. + The default label is typically \uicontrol Open or \uicontrol Save depending on which + \l fileMode the dialog is used in. + + The default value is an empty string. + + \sa rejectLabel +*/ +QString QQuickPlatformFileDialog::acceptLabel() const +{ + return m_options->labelText(QFileDialogOptions::Accept); +} + +void QQuickPlatformFileDialog::setAcceptLabel(const QString &label) +{ + if (label == m_options->labelText(QFileDialogOptions::Accept)) + return; + + m_options->setLabelText(QFileDialogOptions::Accept, label); + emit acceptLabelChanged(); +} + +void QQuickPlatformFileDialog::resetAcceptLabel() +{ + setAcceptLabel(QString()); +} + +/*! + \qmlproperty string Qt.labs.platform::FileDialog::rejectLabel + + This property holds the label text shown on the button that rejects the dialog. + + When set to an empty string, the default label of the underlying platform is used. + The default label is typically \uicontrol Cancel. + + The default value is an empty string. + + \sa acceptLabel +*/ +QString QQuickPlatformFileDialog::rejectLabel() const +{ + return m_options->labelText(QFileDialogOptions::Reject); +} + +void QQuickPlatformFileDialog::setRejectLabel(const QString &label) +{ + if (label == m_options->labelText(QFileDialogOptions::Reject)) + return; + + m_options->setLabelText(QFileDialogOptions::Reject, label); + emit rejectLabelChanged(); +} + +void QQuickPlatformFileDialog::resetRejectLabel() +{ + setRejectLabel(QString()); +} + +void QQuickPlatformFileDialog::applyOptions() +{ + m_options->setWindowTitle(title()); +} + +QT_END_NAMESPACE diff --git a/src/imports/platform/qquickplatformfiledialog_p.h b/src/imports/platform/qquickplatformfiledialog_p.h new file mode 100644 index 00000000..643372c5 --- /dev/null +++ b/src/imports/platform/qquickplatformfiledialog_p.h @@ -0,0 +1,130 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Labs Platform module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKPLATFORMFILEDIALOG_P_H +#define QQUICKPLATFORMFILEDIALOG_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qquickplatformdialog_p.h" +#include <QtCore/qurl.h> +#include <QtQml/qqml.h> + +QT_BEGIN_NAMESPACE + +class QQuickPlatformFileDialog : public QQuickPlatformDialog +{ + Q_OBJECT + Q_PROPERTY(FileMode fileMode READ fileMode WRITE setFileMode NOTIFY fileModeChanged FINAL) + Q_PROPERTY(QUrl currentFile READ currentFile WRITE setCurrentFile NOTIFY currentFileChanged FINAL) + Q_PROPERTY(QFileDialogOptions::FileDialogOptions options READ options WRITE setOptions RESET resetOptions NOTIFY optionsChanged FINAL) + Q_PROPERTY(QStringList nameFilters READ nameFilters WRITE setNameFilters RESET resetNameFilters NOTIFY nameFiltersChanged FINAL) + Q_PROPERTY(QString defaultSuffix READ defaultSuffix WRITE setDefaultSuffix RESET resetDefaultSuffix NOTIFY defaultSuffixChanged FINAL) + Q_PROPERTY(QString acceptLabel READ acceptLabel WRITE setAcceptLabel RESET resetAcceptLabel NOTIFY acceptLabelChanged FINAL) + Q_PROPERTY(QString rejectLabel READ rejectLabel WRITE setRejectLabel RESET resetRejectLabel NOTIFY rejectLabelChanged FINAL) + Q_FLAGS(QFileDialogOptions::FileDialogOptions) + +public: + explicit QQuickPlatformFileDialog(QObject *parent = nullptr); + + enum FileMode { + OpenFile, + OpenFiles, + SaveFile + }; + Q_ENUM(FileMode) + + FileMode fileMode() const; + void setFileMode(FileMode fileMode); + + QUrl currentFile() const; + void setCurrentFile(const QUrl &file); + + QFileDialogOptions::FileDialogOptions options() const; + void setOptions(QFileDialogOptions::FileDialogOptions options); + void resetOptions(); + + QStringList nameFilters() const; + void setNameFilters(const QStringList &filters); + void resetNameFilters(); + + QString defaultSuffix() const; + void setDefaultSuffix(const QString &suffix); + void resetDefaultSuffix(); + + QString acceptLabel() const; + void setAcceptLabel(const QString &label); + void resetAcceptLabel(); + + QString rejectLabel() const; + void setRejectLabel(const QString &label); + void resetRejectLabel(); + +Q_SIGNALS: + void fileModeChanged(); + void currentFileChanged(); + void optionsChanged(); + void nameFiltersChanged(); + void defaultSuffixChanged(); + void acceptLabelChanged(); + void rejectLabelChanged(); + + void fileSelected(const QUrl &file); + void filesSelected(const QList<QUrl> &files); + +protected: + void applyOptions() override; + +private: + FileMode m_fileMode; + mutable QUrl m_current; + QSharedPointer<QFileDialogOptions> m_options; +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQuickPlatformFileDialog) + +#endif // QQUICKPLATFORMFILEDIALOG_P_H diff --git a/src/imports/platform/qquickplatformfolderdialog.cpp b/src/imports/platform/qquickplatformfolderdialog.cpp new file mode 100644 index 00000000..b31d4d52 --- /dev/null +++ b/src/imports/platform/qquickplatformfolderdialog.cpp @@ -0,0 +1,262 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Labs Platform module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickplatformfolderdialog_p.h" + +#include <QtGui/qpa/qplatformtheme.h> +#include <QtGui/private/qguiapplication_p.h> + +#ifdef QT_WIDGETS_LIB +#include "widgets/qwidgetplatformfiledialog_p.h" +#endif + +QT_BEGIN_NAMESPACE + +/*! + \qmltype FolderDialog + \inherits Dialog + \instantiates QQuickPlatformFolderDialog + \inqmlmodule Qt.labs.platform + \since 5.8 + \brief A native folder dialog. + + The FolderDialog type provides a QML API for native platform folder dialogs. + + \image qtlabsplatform-folderdialog-gtk.png + + To show a folder dialog, construct an instance of FolderDialog, set the + desired properties, and call \l {Dialog::}{open()}. FolderDialog emits + the \l folderSelected() and \l foldersSelected() signals when the user + has selected folder(s). + + \code + MenuItem { + text: "Open..." + onTriggered: folderDialog.open() + } + + FolderDialog { + id: folderDialog + currentFolder: viewer.folder + onFolderSelected: viewer.folder = folder + } + \endcode + + \section2 Availability + + A native platform folder dialog is currently available on the following platforms: + + \list + \li iOS + \li Linux (when running with the GTK+ platform theme) + \li macOS + \li Windows + \li WinRT + \endlist + + \input includes/widgets.qdocinc 1 + + \labs + + \sa FileDialog +*/ + +/*! + \qmlsignal void Qt.labs.platform::FolderDialog::folderSelected(url folder) + + This signal is emitted when a \a folder has been selected. + + \sa foldersSelected(), currentFolder +*/ + +/*! + \qmlsignal void Qt.labs.platform::FolderDialog::foldersSelected(list<url> folders) + + This signal is emitted when multiple \a folders have been selected. + + \sa folderSelected(), currentFolder +*/ + +QQuickPlatformFolderDialog::QQuickPlatformFolderDialog(QObject *parent) + : QQuickPlatformDialog(parent), m_options(QFileDialogOptions::create()) +{ + m_options->setFileMode(QFileDialogOptions::Directory); + m_options->setAcceptMode(QFileDialogOptions::AcceptOpen); + + QPlatformDialogHelper *dialog = QGuiApplicationPrivate::platformTheme()->createPlatformDialogHelper(QPlatformTheme::FileDialog); +#ifdef QT_WIDGETS_LIB + if (!dialog) + dialog = new QWidgetPlatformFileDialog(this); +#endif + if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(dialog)) { + connect(fileDialog, &QPlatformFileDialogHelper::fileSelected, this, &QQuickPlatformFolderDialog::folderSelected); + connect(fileDialog, &QPlatformFileDialogHelper::filesSelected, this, &QQuickPlatformFolderDialog::foldersSelected); + connect(fileDialog, &QPlatformFileDialogHelper::currentChanged, [this](const QUrl &url) { + if (m_current == url) + return; + m_current = url; + emit currentFolderChanged(); + }); + fileDialog->setOptions(m_options); + } + setHandle(dialog); +} + +/*! + \qmlproperty url Qt.labs.platform::FolderDialog::currentFolder + + This property holds the currently selected folder in the dialog. + + \sa folderSelected(), foldersSelected() +*/ +QUrl QQuickPlatformFolderDialog::currentFolder() const +{ + if (m_current.isEmpty()) { + if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(handle())) + m_current = fileDialog->directory(); + } + return m_current; +} + +void QQuickPlatformFolderDialog::setCurrentFolder(const QUrl &folder) +{ + if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(handle())) + fileDialog->setDirectory(folder); +} + +/*! + \qmlproperty flags Qt.labs.platform::FolderDialog::options + + This property holds the various options that affect the look and feel of the dialog. + + By default, all options are disabled. + + Options should be set before showing the dialog. Setting them while the dialog is + visible is not guaranteed to have an immediate effect on the dialog (depending on + the option and on the platform). + + Available options: + \value FolderDialog.ShowDirsOnly Only show directories in the folder dialog. By default both folders and directories are shown. + \value FolderDialog.DontResolveSymlinks Don't resolve symlinks in the folder dialog. By default symlinks are resolved. + \value FolderDialog.ReadOnly Indicates that the dialog doesn't allow creating directories. +*/ +QFileDialogOptions::FileDialogOptions QQuickPlatformFolderDialog::options() const +{ + return m_options->options(); +} + +void QQuickPlatformFolderDialog::setOptions(QFileDialogOptions::FileDialogOptions options) +{ + if (options == m_options->options()) + return; + + m_options->setOptions(options); + emit optionsChanged(); +} + +void QQuickPlatformFolderDialog::resetOptions() +{ + setOptions(0); +} + +/*! + \qmlproperty string Qt.labs.platform::FolderDialog::acceptLabel + + This property holds the label text shown on the button that accepts the dialog. + + When set to an empty string, the default label of the underlying platform is used. + The default label is typically \uicontrol Open. + + The default value is an empty string. + + \sa rejectLabel +*/ +QString QQuickPlatformFolderDialog::acceptLabel() const +{ + return m_options->labelText(QFileDialogOptions::Accept); +} + +void QQuickPlatformFolderDialog::setAcceptLabel(const QString &label) +{ + if (label == m_options->labelText(QFileDialogOptions::Accept)) + return; + + m_options->setLabelText(QFileDialogOptions::Accept, label); + emit acceptLabelChanged(); +} + +void QQuickPlatformFolderDialog::resetAcceptLabel() +{ + setAcceptLabel(QString()); +} + +/*! + \qmlproperty string Qt.labs.platform::FolderDialog::rejectLabel + + This property holds the label text shown on the button that rejects the dialog. + + When set to an empty string, the default label of the underlying platform is used. + The default label is typically \uicontrol Cancel. + + The default value is an empty string. + + \sa acceptLabel +*/ +QString QQuickPlatformFolderDialog::rejectLabel() const +{ + return m_options->labelText(QFileDialogOptions::Reject); +} + +void QQuickPlatformFolderDialog::setRejectLabel(const QString &label) +{ + if (label == m_options->labelText(QFileDialogOptions::Reject)) + return; + + m_options->setLabelText(QFileDialogOptions::Reject, label); + emit rejectLabelChanged(); +} + +void QQuickPlatformFolderDialog::resetRejectLabel() +{ + setRejectLabel(QString()); +} + +void QQuickPlatformFolderDialog::applyOptions() +{ + m_options->setWindowTitle(title()); +} + +QT_END_NAMESPACE diff --git a/src/imports/platform/qquickplatformfolderdialog_p.h b/src/imports/platform/qquickplatformfolderdialog_p.h new file mode 100644 index 00000000..4b5f6a9b --- /dev/null +++ b/src/imports/platform/qquickplatformfolderdialog_p.h @@ -0,0 +1,105 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Labs Platform module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKPLATFORMFOLDERDIALOG_P_H +#define QQUICKPLATFORMFOLDERDIALOG_P_H + +// +// W A R N I N G +// ------------- +// +// This folder is not part of the Qt API. It exists purely as an +// implementation detail. This header folder may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qquickplatformdialog_p.h" +#include <QtCore/qurl.h> +#include <QtQml/qqml.h> + +QT_BEGIN_NAMESPACE + +class QQuickPlatformFolderDialog : public QQuickPlatformDialog +{ + Q_OBJECT + Q_PROPERTY(QUrl currentFolder READ currentFolder WRITE setCurrentFolder NOTIFY currentFolderChanged FINAL) + Q_PROPERTY(QFileDialogOptions::FileDialogOptions options READ options WRITE setOptions RESET resetOptions NOTIFY optionsChanged FINAL) + Q_PROPERTY(QString acceptLabel READ acceptLabel WRITE setAcceptLabel RESET resetAcceptLabel NOTIFY acceptLabelChanged FINAL) + Q_PROPERTY(QString rejectLabel READ rejectLabel WRITE setRejectLabel RESET resetRejectLabel NOTIFY rejectLabelChanged FINAL) + Q_FLAGS(QFileDialogOptions::FileDialogOptions) + +public: + explicit QQuickPlatformFolderDialog(QObject *parent = nullptr); + + QUrl currentFolder() const; + void setCurrentFolder(const QUrl &url); + + QFileDialogOptions::FileDialogOptions options() const; + void setOptions(QFileDialogOptions::FileDialogOptions options); + void resetOptions(); + + QString acceptLabel() const; + void setAcceptLabel(const QString &label); + void resetAcceptLabel(); + + QString rejectLabel() const; + void setRejectLabel(const QString &label); + void resetRejectLabel(); + +Q_SIGNALS: + void currentFolderChanged(); + void optionsChanged(); + void acceptLabelChanged(); + void rejectLabelChanged(); + + void folderSelected(const QUrl &folder); + void foldersSelected(const QList<QUrl> &folders); + +protected: + void applyOptions() override; + +private: + mutable QUrl m_current; + QSharedPointer<QFileDialogOptions> m_options; +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQuickPlatformFolderDialog) + +#endif // QQUICKPLATFORMFOLDERDIALOG_P_H diff --git a/src/imports/platform/qtlabsplatformplugin.cpp b/src/imports/platform/qtlabsplatformplugin.cpp index f9a2818c..a9364bde 100644 --- a/src/imports/platform/qtlabsplatformplugin.cpp +++ b/src/imports/platform/qtlabsplatformplugin.cpp @@ -39,6 +39,8 @@ #include "qquickplatformdialog_p.h" #include "qquickplatformcolordialog_p.h" +#include "qquickplatformfiledialog_p.h" +#include "qquickplatformfolderdialog_p.h" #include "qquickplatformfontdialog_p.h" #include "qquickplatformmessagedialog_p.h" @@ -77,6 +79,8 @@ void QtLabsPlatformPlugin::registerTypes(const char *uri) { qmlRegisterUncreatableType<QQuickPlatformDialog>(uri, 1, 0, "Dialog", QQuickPlatformDialog::tr("Dialog is an abstract base class")); qmlRegisterType<QQuickPlatformColorDialog>(uri, 1, 0, "ColorDialog"); + qmlRegisterType<QQuickPlatformFileDialog>(uri, 1, 0, "FileDialog"); + qmlRegisterType<QQuickPlatformFolderDialog>(uri, 1, 0, "FolderDialog"); qmlRegisterType<QQuickPlatformFontDialog>(uri, 1, 0, "FontDialog"); qmlRegisterType<QQuickPlatformMessageDialog>(uri, 1, 0, "MessageDialog"); diff --git a/src/imports/platform/widgets/qwidgetplatformfiledialog.cpp b/src/imports/platform/widgets/qwidgetplatformfiledialog.cpp new file mode 100644 index 00000000..22e9c78a --- /dev/null +++ b/src/imports/platform/widgets/qwidgetplatformfiledialog.cpp @@ -0,0 +1,152 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Labs Platform module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qwidgetplatformfiledialog_p.h" +#include "qwidgetplatformdialog_p.h" +#include "qwidgetplatform_p.h" + +#include <QtWidgets/qfiledialog.h> + +QT_BEGIN_NAMESPACE + +QWidgetPlatformFileDialog::QWidgetPlatformFileDialog(QObject *parent) +{ + setParent(parent); + + static bool available = QWidgetPlatform::isAvailable("file dialog"); + if (available) { + m_dialog.reset(new QFileDialog); + connect(m_dialog.data(), &QDialog::accepted, this, &QPlatformDialogHelper::accept); + connect(m_dialog.data(), &QDialog::rejected, this, &QPlatformDialogHelper::reject); + + connect(m_dialog.data(), &QFileDialog::fileSelected, [this](const QString &file) { + emit fileSelected(QUrl::fromLocalFile(file)); + }); + connect(m_dialog.data(), &QFileDialog::filesSelected, [this](const QList<QString> &files) { + QList<QUrl> urls; + urls.reserve(files.count()); + for (const QString &file : files) + urls += QUrl::fromLocalFile(file); + emit filesSelected(urls); + }); + connect(m_dialog.data(), &QFileDialog::currentChanged, [this](const QString &path) { + emit currentChanged(QUrl::fromLocalFile(path)); + }); + connect(m_dialog.data(), &QFileDialog::directoryEntered, this, &QWidgetPlatformFileDialog::directoryEntered); + connect(m_dialog.data(), &QFileDialog::filterSelected, this, &QWidgetPlatformFileDialog::filterSelected); + } +} + +QWidgetPlatformFileDialog::~QWidgetPlatformFileDialog() +{ +} + +bool QWidgetPlatformFileDialog::defaultNameFilterDisables() const +{ + return false; // TODO: ? +} + +void QWidgetPlatformFileDialog::setDirectory(const QUrl &directory) +{ + if (m_dialog) + m_dialog->setDirectory(directory.toLocalFile()); +} + +QUrl QWidgetPlatformFileDialog::directory() const +{ + return m_dialog ? m_dialog->directoryUrl() : QUrl(); +} + +void QWidgetPlatformFileDialog::selectFile(const QUrl &filename) +{ + if (m_dialog) + m_dialog->selectUrl(filename); +} + +QList<QUrl> QWidgetPlatformFileDialog::selectedFiles() const +{ + return m_dialog ? m_dialog->selectedUrls() : QList<QUrl>(); +} + +void QWidgetPlatformFileDialog::setFilter() +{ + // TODO: ? +} + +void QWidgetPlatformFileDialog::selectNameFilter(const QString &filter) +{ + if (m_dialog) + m_dialog->selectNameFilter(filter); +} + +QString QWidgetPlatformFileDialog::selectedNameFilter() const +{ + return m_dialog ? m_dialog->selectedNameFilter() : QString(); +} + +void QWidgetPlatformFileDialog::exec() +{ + if (m_dialog) + m_dialog->exec(); +} + +bool QWidgetPlatformFileDialog::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent) +{ + if (!m_dialog) + return false; + + QSharedPointer<QFileDialogOptions> options = QPlatformFileDialogHelper::options(); + m_dialog->setWindowTitle(options->windowTitle()); + m_dialog->setAcceptMode(static_cast<QFileDialog::AcceptMode>(options->acceptMode())); + m_dialog->setFileMode(static_cast<QFileDialog::FileMode>(options->fileMode())); + m_dialog->setOptions(static_cast<QFileDialog::Options>(int(options->options()))); + m_dialog->setNameFilters(options->nameFilters()); + m_dialog->setDefaultSuffix(options->defaultSuffix()); + if (options->isLabelExplicitlySet(QFileDialogOptions::Accept)) + m_dialog->setLabelText(QFileDialog::Accept, options->labelText(QFileDialogOptions::Accept)); + if (options->isLabelExplicitlySet(QFileDialogOptions::Reject)) + m_dialog->setLabelText(QFileDialog::Reject, options->labelText(QFileDialogOptions::Reject)); + + return QWidgetPlatformDialog::show(m_dialog.data(), flags, modality, parent); +} + +void QWidgetPlatformFileDialog::hide() +{ + if (m_dialog) + m_dialog->hide(); +} + +QT_END_NAMESPACE diff --git a/src/imports/platform/widgets/qwidgetplatformfiledialog_p.h b/src/imports/platform/widgets/qwidgetplatformfiledialog_p.h new file mode 100644 index 00000000..743bc571 --- /dev/null +++ b/src/imports/platform/widgets/qwidgetplatformfiledialog_p.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Labs Platform module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QWIDGETPLATFORMFILEDIALOG_P_H +#define QWIDGETPLATFORMFILEDIALOG_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtGui/qpa/qplatformdialoghelper.h> + +QT_BEGIN_NAMESPACE + +class QFileDialog; + +class QWidgetPlatformFileDialog : public QPlatformFileDialogHelper +{ + Q_OBJECT + +public: + explicit QWidgetPlatformFileDialog(QObject *parent = nullptr); + ~QWidgetPlatformFileDialog(); + + bool defaultNameFilterDisables() const override; + void setDirectory(const QUrl &directory) override; + QUrl directory() const override; + void selectFile(const QUrl &filename) override; + QList<QUrl> selectedFiles() const override; + void setFilter() override; + void selectNameFilter(const QString &filter) override; + QString selectedNameFilter() const override; + + void exec() override; + bool show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent) override; + void hide() override; + +private: + QScopedPointer<QFileDialog> m_dialog; +}; + +QT_END_NAMESPACE + +#endif // QWIDGETPLATFORMFILEDIALOG_P_H diff --git a/src/imports/platform/widgets/widgets.pri b/src/imports/platform/widgets/widgets.pri index e09e3166..c904e924 100644 --- a/src/imports/platform/widgets/widgets.pri +++ b/src/imports/platform/widgets/widgets.pri @@ -5,6 +5,7 @@ HEADERS += \ $$PWD/qwidgetplatform_p.h \ $$PWD/qwidgetplatformcolordialog_p.h \ $$PWD/qwidgetplatformdialog_p.h \ + $$PWD/qwidgetplatformfiledialog_p.h \ $$PWD/qwidgetplatformfontdialog_p.h \ $$PWD/qwidgetplatformmenu_p.h \ $$PWD/qwidgetplatformmenuitem_p.h \ @@ -14,6 +15,7 @@ HEADERS += \ SOURCES += \ $$PWD/qwidgetplatformcolordialog.cpp \ $$PWD/qwidgetplatformdialog.cpp \ + $$PWD/qwidgetplatformfiledialog.cpp \ $$PWD/qwidgetplatformfontdialog.cpp \ $$PWD/qwidgetplatformmenu.cpp \ $$PWD/qwidgetplatformmenuitem.cpp \ |