QFileDialogOptions: Expose default name filter setting.

Move the bool QFileDialogPrivate::defaultFileTypes to QFileDialogOptions
as defaultNameFilters and add a static function returning the
translated default filter string. Let QFileDialogOptions::nameFilters()
return the default filter until a value has been set.

This removes the need for special handling for empty filter lists
in the QPA plugins. As a side effect, Qt Quick Controls's FileDialog
will then also default to "All files" if no filters have been set.

Task-number: QTBUG-50644
Change-Id: I9ba271a472d4fa03767b540ef6f1399f5ca4408e
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@theqtcompany.com>
This commit is contained in:
Friedemann Kleint 2016-02-03 12:45:21 +01:00
parent 6129aade00
commit fc6d5ed18d
4 changed files with 46 additions and 7 deletions

View File

@ -39,6 +39,7 @@
#include "qplatformdialoghelper.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QVariant>
#include <QtCore/QSharedData>
#include <QtCore/QSettings>
@ -411,7 +412,8 @@ public:
viewMode(QFileDialogOptions::Detail),
fileMode(QFileDialogOptions::AnyFile),
acceptMode(QFileDialogOptions::AcceptOpen),
filters(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs)
filters(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs),
useDefaultNameFilters(true)
{}
QFileDialogOptions::FileDialogOptions options;
@ -423,6 +425,7 @@ public:
QString labels[QFileDialogOptions::DialogLabelCount];
QDir::Filters filters;
QList<QUrl> sidebarUrls;
bool useDefaultNameFilters;
QStringList nameFilters;
QStringList mimeTypeFilters;
QString defaultSuffix;
@ -534,14 +537,48 @@ QList<QUrl> QFileDialogOptions::sidebarUrls() const
return d->sidebarUrls;
}
/*!
\since 5.7
\internal
The bool property useDefaultNameFilters indicates that no name filters have been
set or that they are equivalent to \gui{All Files (*)}. If it is true, the
platform can choose to hide the filter combo box.
\sa defaultNameFilterString().
*/
bool QFileDialogOptions::useDefaultNameFilters() const
{
return d->useDefaultNameFilters;
}
void QFileDialogOptions::setUseDefaultNameFilters(bool dnf)
{
d->useDefaultNameFilters = dnf;
}
void QFileDialogOptions::setNameFilters(const QStringList &filters)
{
d->useDefaultNameFilters = filters.size() == 1
&& filters.first() == QFileDialogOptions::defaultNameFilterString();
d->nameFilters = filters;
}
QStringList QFileDialogOptions::nameFilters() const
{
return d->nameFilters;
return d->useDefaultNameFilters ?
QStringList(QFileDialogOptions::defaultNameFilterString()) : d->nameFilters;
}
/*!
\since 5.6
\internal
\return The translated default name filter string (\gui{All Files (*)}).
\sa defaultNameFilters(), nameFilters()
*/
QString QFileDialogOptions::defaultNameFilterString()
{
return QCoreApplication::translate("QFileDialog", "All Files (*)");
}
void QFileDialogOptions::setMimeTypeFilters(const QStringList &filters)

View File

@ -349,6 +349,9 @@ public:
void setSidebarUrls(const QList<QUrl> &urls);
QList<QUrl> sidebarUrls() const;
bool useDefaultNameFilters() const;
void setUseDefaultNameFilters(bool d);
void setNameFilters(const QStringList &filters);
QStringList nameFilters() const;
@ -377,6 +380,8 @@ public:
void setSupportedSchemes(const QStringList &schemes);
QStringList supportedSchemes() const;
static QString defaultNameFilterString();
private:
QSharedDataPointer<QFileDialogOptionsPrivate> d;
};

View File

@ -532,7 +532,6 @@ QFileDialogPrivate::QFileDialogPrivate()
deleteAction(0),
showHiddenAction(0),
useDefaultCaption(true),
defaultFileTypes(true),
qFileDialogUi(0),
options(new QFileDialogOptions)
{
@ -663,8 +662,8 @@ void QFileDialogPrivate::retranslateStrings()
{
Q_Q(QFileDialog);
/* WIDGETS */
if (defaultFileTypes)
q->setNameFilter(QFileDialog::tr("All Files (*)"));
if (options->useDefaultNameFilters())
q->setNameFilter(QFileDialogOptions::defaultNameFilterString());
if (nativeDialogInUse)
return;
@ -1400,7 +1399,6 @@ QStringList qt_strip_filters(const QStringList &filters)
void QFileDialog::setNameFilters(const QStringList &filters)
{
Q_D(QFileDialog);
d->defaultFileTypes = (filters == QStringList(QFileDialog::tr("All Files (*)")));
QStringList cleanedFilters;
const int numFilters = filters.count();
cleanedFilters.reserve(numFilters);

View File

@ -250,7 +250,6 @@ public:
QAction *newFolderAction;
bool useDefaultCaption;
bool defaultFileTypes;
// setVisible_sys returns true if it ends up showing a native
// dialog. Returning false means that a non-native dialog must be