diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2016-08-13 22:25:39 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2016-08-14 09:26:14 +0000 |
commit | 51192e8ef5d9ab385de390b38409b73a56f55f84 (patch) | |
tree | 97dd174137fcb23889d6cc8533a8aa9e51620f34 | |
parent | 7b6180a2d6b5def11b95485a3323a37f92b57ba9 (diff) |
FileDialog: replace file(s)Selected() with declarative properties
Follow the same convention that ColorDialog, FontDialog and FolderDialog.
Change-Id: I960d4fc1ba275ab997f2a079a799d2b90796eca3
Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
-rw-r--r-- | examples/quickcontrols2/texteditor/qml/texteditor.qml | 4 | ||||
-rw-r--r-- | src/imports/platform/plugins.qmltypes | 11 | ||||
-rw-r--r-- | src/imports/platform/qquickplatformfiledialog.cpp | 145 | ||||
-rw-r--r-- | src/imports/platform/qquickplatformfiledialog_p.h | 21 |
4 files changed, 129 insertions, 52 deletions
diff --git a/examples/quickcontrols2/texteditor/qml/texteditor.qml b/examples/quickcontrols2/texteditor/qml/texteditor.qml index f55405ff..7d92bea9 100644 --- a/examples/quickcontrols2/texteditor/qml/texteditor.qml +++ b/examples/quickcontrols2/texteditor/qml/texteditor.qml @@ -162,7 +162,7 @@ ApplicationWindow { id: openDialog fileMode: FileDialog.OpenFile nameFilters: ["Text files (*.txt)", "HTML files (*.html *.htm)"] - onFileSelected: document.load(file) + onAccepted: document.load(file) } FileDialog { @@ -170,7 +170,7 @@ ApplicationWindow { fileMode: FileDialog.SaveFile defaultSuffix: document.fileType nameFilters: openDialog.nameFilters - onFileSelected: document.saveAs(file) + onAccepted: document.saveAs(file) } FontDialog { diff --git a/src/imports/platform/plugins.qmltypes b/src/imports/platform/plugins.qmltypes index d7e85c9e..dc9e1562 100644 --- a/src/imports/platform/plugins.qmltypes +++ b/src/imports/platform/plugins.qmltypes @@ -111,20 +111,15 @@ Module { } } Property { name: "fileMode"; type: "FileMode" } + Property { name: "file"; type: "QUrl" } + Property { name: "files"; type: "QList<QUrl>" } Property { name: "currentFile"; type: "QUrl" } + Property { name: "currentFiles"; type: "QList<QUrl>" } Property { name: "options"; type: "QFileDialogOptions::FileDialogOptions" } Property { name: "nameFilters"; type: "QStringList" } Property { name: "defaultSuffix"; type: "string" } Property { name: "acceptLabel"; type: "string" } Property { name: "rejectLabel"; type: "string" } - Signal { - name: "fileSelected" - Parameter { name: "file"; type: "QUrl" } - } - Signal { - name: "filesSelected" - Parameter { name: "files"; type: "QList<QUrl>" } - } } Component { name: "QQuickPlatformFolderDialog" diff --git a/src/imports/platform/qquickplatformfiledialog.cpp b/src/imports/platform/qquickplatformfiledialog.cpp index 1900952c..0a617a8c 100644 --- a/src/imports/platform/qquickplatformfiledialog.cpp +++ b/src/imports/platform/qquickplatformfiledialog.cpp @@ -59,9 +59,11 @@ QT_BEGIN_NAMESPACE \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). + desired properties, and call \l {Dialog::}{open()}. The \l currentFile + or \l currentFiles properties can be used to determine the currently + selected file(s) in the dialog. The \l file and \l files properties + are updated only after the final selection has been made by accepting + the dialog. \code MenuItem { @@ -72,7 +74,11 @@ QT_BEGIN_NAMESPACE FileDialog { id: fileDialog currentFile: document.source - onFileSelected: document.source = file + } + + MyDocument { + id: document + source: fileDialog.file } \endcode @@ -95,22 +101,6 @@ QT_BEGIN_NAMESPACE \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 -*/ - Q_DECLARE_LOGGING_CATEGORY(qtLabsPlatformDialogs) QQuickPlatformFileDialog::QQuickPlatformFileDialog(QObject *parent) @@ -162,19 +152,74 @@ void QQuickPlatformFileDialog::setFileMode(FileMode mode) } /*! + \qmlproperty url Qt.labs.platform::FileDialog::file + + This property holds the final accepted file. + + Unlike the \l currentFile property, the \c file property is not updated + while the user is selecting files in the dialog, but only after the final + selection has been made. That is, when the user has clicked \uicontrol OK + to accept a file. Alternatively, the \l {Dialog::}{accepted()} signal + can be handled to get the final selection. + + \sa currentFile, {Dialog::}{accepted()} +*/ +QUrl QQuickPlatformFileDialog::file() const +{ + return addDefaultSuffix(m_files.value(0)); +} + +void QQuickPlatformFileDialog::setFile(const QUrl &file) +{ + setFiles(QList<QUrl>() << file); +} + +/*! + \qmlproperty list<url> Qt.labs.platform::FileDialog::files + + This property holds the final accepted files. + + Unlike the \l currentFiles property, the \c files property is not updated + while the user is selecting files in the dialog, but only after the final + selection has been made. That is, when the user has clicked \uicontrol OK + to accept files. Alternatively, the \l {Dialog::}{accepted()} signal + can be handled to get the final selection. + + \sa currentFiles, {Dialog::}{accepted()} +*/ +QList<QUrl> QQuickPlatformFileDialog::files() const +{ + return addDefaultSuffixes(m_files); +} + +void QQuickPlatformFileDialog::setFiles(const QList<QUrl> &files) +{ + if (m_files == files) + return; + + bool firstChanged = m_files.value(0) != files.value(0); + m_files = files; + if (firstChanged) + emit fileChanged(); + emit filesChanged(); +} + +/*! \qmlproperty url Qt.labs.platform::FileDialog::currentFile This property holds the currently selected file in the dialog. - \sa fileSelected(), filesSelected() + Unlike the \l file property, the \c currentFile property is updated + while the user is selecting files in the dialog, even before the final + selection has been made. + + \sa file, currentFiles */ QUrl QQuickPlatformFileDialog::currentFile() const { - if (m_current.isEmpty()) { - if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(handle())) - m_current = fileDialog->selectedFiles().value(0); - } - return addDefaultSuffix(m_current); + if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(handle())) + return fileDialog->selectedFiles().value(0); + return QUrl(); } void QQuickPlatformFileDialog::setCurrentFile(const QUrl &file) @@ -184,6 +229,32 @@ void QQuickPlatformFileDialog::setCurrentFile(const QUrl &file) } /*! + \qmlproperty list<url> Qt.labs.platform::FileDialog::currentFiles + + This property holds the currently selected files in the dialog. + + Unlike the \l files property, the \c currentFiles property is updated + while the user is selecting files in the dialog, even before the final + selection has been made. + + \sa files, currentFile +*/ +QList<QUrl> QQuickPlatformFileDialog::currentFiles() const +{ + if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(handle())) + return fileDialog->selectedFiles(); + return QList<QUrl>(); +} + +void QQuickPlatformFileDialog::setCurrentFiles(const QList<QUrl> &files) +{ + if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(handle())) { + for (const QUrl &file : files) + 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. @@ -352,18 +423,9 @@ QPlatformDialogHelper *QQuickPlatformFileDialog::createHelper() qCDebug(qtLabsPlatformDialogs) << "FileDialog:" << dialog; if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(dialog)) { - connect(fileDialog, &QPlatformFileDialogHelper::fileSelected, [this](const QUrl &file) { - emit fileSelected(addDefaultSuffix(file)); - }); - connect(fileDialog, &QPlatformFileDialogHelper::filesSelected, [this](const QList<QUrl> &files) { - emit filesSelected(addDefaultSuffixes(files)); - }); - connect(fileDialog, &QPlatformFileDialogHelper::currentChanged, [this](const QUrl &url) { - if (m_current == url) - return; - m_current = url; - emit currentFileChanged(); - }); + // TODO: emit currentFileChanged only when the first entry in currentFiles changes + connect(fileDialog, &QPlatformFileDialogHelper::currentChanged, this, &QQuickPlatformFileDialog::currentFileChanged); + connect(fileDialog, &QPlatformFileDialogHelper::currentChanged, this, &QQuickPlatformFileDialog::currentFilesChanged); fileDialog->setOptions(m_options); } return dialog; @@ -374,6 +436,13 @@ void QQuickPlatformFileDialog::applyOptions() m_options->setWindowTitle(title()); } +void QQuickPlatformFileDialog::accept() +{ + if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(handle())) + setFiles(fileDialog->selectedFiles()); + QQuickPlatformDialog::accept(); +} + QUrl QQuickPlatformFileDialog::addDefaultSuffix(const QUrl &file) const { QUrl url = file; diff --git a/src/imports/platform/qquickplatformfiledialog_p.h b/src/imports/platform/qquickplatformfiledialog_p.h index c3c6a490..76acd105 100644 --- a/src/imports/platform/qquickplatformfiledialog_p.h +++ b/src/imports/platform/qquickplatformfiledialog_p.h @@ -58,7 +58,10 @@ class QQuickPlatformFileDialog : public QQuickPlatformDialog { Q_OBJECT Q_PROPERTY(FileMode fileMode READ fileMode WRITE setFileMode NOTIFY fileModeChanged FINAL) + Q_PROPERTY(QUrl file READ file WRITE setFile NOTIFY fileChanged FINAL) + Q_PROPERTY(QList<QUrl> files READ files WRITE setFiles NOTIFY filesChanged FINAL) Q_PROPERTY(QUrl currentFile READ currentFile WRITE setCurrentFile NOTIFY currentFileChanged FINAL) + Q_PROPERTY(QList<QUrl> currentFiles READ currentFiles WRITE setCurrentFiles NOTIFY currentFilesChanged 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) @@ -79,9 +82,18 @@ public: FileMode fileMode() const; void setFileMode(FileMode fileMode); + QUrl file() const; + void setFile(const QUrl &file); + + QList<QUrl> files() const; + void setFiles(const QList<QUrl> &files); + QUrl currentFile() const; void setCurrentFile(const QUrl &file); + QList<QUrl> currentFiles() const; + void setCurrentFiles(const QList<QUrl> &files); + QFileDialogOptions::FileDialogOptions options() const; void setOptions(QFileDialogOptions::FileDialogOptions options); void resetOptions(); @@ -104,26 +116,27 @@ public: Q_SIGNALS: void fileModeChanged(); + void fileChanged(); + void filesChanged(); void currentFileChanged(); + void currentFilesChanged(); void optionsChanged(); void nameFiltersChanged(); void defaultSuffixChanged(); void acceptLabelChanged(); void rejectLabelChanged(); - void fileSelected(const QUrl &file); - void filesSelected(const QList<QUrl> &files); - protected: QPlatformDialogHelper *createHelper() override; void applyOptions() override; + void accept() override; private: QUrl addDefaultSuffix(const QUrl &file) const; QList<QUrl> addDefaultSuffixes(const QList<QUrl> &files) const; FileMode m_fileMode; - mutable QUrl m_current; + QList<QUrl> m_files; QSharedPointer<QFileDialogOptions> m_options; }; |