summaryrefslogtreecommitdiffstats
path: root/src/widgets/dialogs/qfiledialog.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgets/dialogs/qfiledialog.cpp')
-rw-r--r--src/widgets/dialogs/qfiledialog.cpp122
1 files changed, 77 insertions, 45 deletions
diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp
index 4e0d70fb9a..06102ec1d6 100644
--- a/src/widgets/dialogs/qfiledialog.cpp
+++ b/src/widgets/dialogs/qfiledialog.cpp
@@ -330,14 +330,8 @@ Q_GLOBAL_STATIC(QUrl, lastVisitedDir)
*/
QT_BEGIN_INCLUDE_NAMESPACE
-#ifdef Q_DEAD_CODE_FROM_QT4_WIN
-#include <qwindowsstyle_p.h>
-#endif
#include <QMetaEnum>
#include <qshortcut.h>
-#ifdef Q_DEAD_CODE_FROM_QT4_MAC
-#include <qmacstyle_mac_p.h>
-#endif
QT_END_INCLUDE_NAMESPACE
/*!
@@ -983,7 +977,7 @@ void QFileDialog::setDirectoryUrl(const QUrl &directory)
else if (directory.isLocalFile())
setDirectory(directory.toLocalFile());
else if (d->usingWidgets())
- qWarning() << "Non-native QFileDialog supports only local files";
+ qWarning("Non-native QFileDialog supports only local files");
}
/*!
@@ -1093,7 +1087,7 @@ void QFileDialog::selectUrl(const QUrl &url)
else if (url.isLocalFile())
selectFile(url.toLocalFile());
else
- qWarning() << "Non-native QFileDialog supports only local files";
+ qWarning("Non-native QFileDialog supports only local files");
}
#ifdef Q_OS_UNIX
@@ -1191,12 +1185,17 @@ QList<QUrl> QFileDialogPrivate::userSelectedFiles() const
if (!usingWidgets())
return addDefaultSuffixToUrls(selectedFiles_sys());
- foreach (const QModelIndex &index, qFileDialogUi->listView->selectionModel()->selectedRows())
+ const QModelIndexList selectedRows = qFileDialogUi->listView->selectionModel()->selectedRows();
+ files.reserve(selectedRows.size());
+ foreach (const QModelIndex &index, selectedRows)
files.append(QUrl::fromLocalFile(index.data(QFileSystemModel::FilePathRole).toString()));
- if (files.isEmpty() && !lineEdit()->text().isEmpty())
- foreach (const QString &path, typedFiles())
+ if (files.isEmpty() && !lineEdit()->text().isEmpty()) {
+ const QStringList typedFilesList = typedFiles();
+ files.reserve(typedFilesList.size());
+ foreach (const QString &path, typedFilesList)
files.append(QUrl::fromLocalFile(path));
+ }
return files;
}
@@ -1230,7 +1229,9 @@ QStringList QFileDialogPrivate::addDefaultSuffixToFiles(const QStringList &files
QList<QUrl> QFileDialogPrivate::addDefaultSuffixToUrls(const QList<QUrl> &urlsToFix) const
{
QList<QUrl> urls;
- for (int i=0; i<urlsToFix.size(); ++i) {
+ const int numUrlsToFix = urlsToFix.size();
+ urls.reserve(numUrlsToFix);
+ for (int i = 0; i < numUrlsToFix; ++i) {
QUrl url = urlsToFix.at(i);
// if the filename has no suffix, add the default suffix
const QString defaultSuffix = options->defaultSuffix();
@@ -1254,7 +1255,9 @@ QStringList QFileDialog::selectedFiles() const
Q_D(const QFileDialog);
QStringList files;
- foreach (const QUrl &file, d->userSelectedFiles())
+ const QList<QUrl> userSelectedFiles = d->userSelectedFiles();
+ files.reserve(userSelectedFiles.size());
+ foreach (const QUrl &file, userSelectedFiles)
files.append(file.toLocalFile());
if (files.isEmpty() && d->usingWidgets()) {
const FileMode fm = fileMode();
@@ -1279,7 +1282,9 @@ QList<QUrl> QFileDialog::selectedUrls() const
return d->userSelectedFiles();
} else {
QList<QUrl> urls;
- foreach (const QString &file, selectedFiles())
+ const QStringList selectedFileList = selectedFiles();
+ urls.reserve(selectedFileList.size());
+ foreach (const QString &file, selectedFileList)
urls.append(QUrl::fromLocalFile(file));
return urls;
}
@@ -1358,7 +1363,9 @@ QStringList qt_strip_filters(const QStringList &filters)
{
QStringList strippedFilters;
QRegExp r(QString::fromLatin1(QPlatformFileDialogHelper::filterRegExp));
- for (int i = 0; i < filters.count(); ++i) {
+ const int numFilters = filters.count();
+ strippedFilters.reserve(numFilters);
+ for (int i = 0; i < numFilters; ++i) {
QString filterName;
int index = r.indexIn(filters[i]);
if (index >= 0)
@@ -1393,7 +1400,9 @@ void QFileDialog::setNameFilters(const QStringList &filters)
Q_D(QFileDialog);
d->defaultFileTypes = (filters == QStringList(QFileDialog::tr("All Files (*)")));
QStringList cleanedFilters;
- for (int i = 0; i < filters.count(); ++i) {
+ const int numFilters = filters.count();
+ cleanedFilters.reserve(numFilters);
+ for (int i = 0; i < numFilters; ++i) {
cleanedFilters << filters[i].simplified();
}
d->options->setNameFilters(cleanedFilters);
@@ -1693,6 +1702,30 @@ void QFileDialog::setAcceptMode(QFileDialog::AcceptMode mode)
d->retranslateWindowTitle();
}
+/*!
+ \property QFileDialog::supportedSchemes
+ \brief the URL schemes that the file dialog should allow navigating to.
+ \since 5.6
+
+ Setting this property allows to restrict the type of URLs the
+ user will be able to select. It is a way for the application to declare
+ the protocols it will support to fetch the file content. An empty list
+ means that no restriction is applied (the default).
+ Supported for local files ("file" scheme) is implicit and always enabled;
+ it is not necessary to include it in the restriction.
+*/
+
+void QFileDialog::setSupportedSchemes(const QStringList &schemes)
+{
+ Q_D(QFileDialog);
+ d->options->setSupportedSchemes(schemes);
+}
+
+QStringList QFileDialog::supportedSchemes() const
+{
+ return d_func()->options->supportedSchemes();
+}
+
/*
Returns the file system model index that is the root index in the
views
@@ -2091,8 +2124,8 @@ QString QFileDialog::getOpenFileName(QWidget *parent,
user will be able to select. It is a way for the application to declare
the protocols it will support to fetch the file content. An empty list
means that no restriction is applied (the default).
- Supported for local files ("file" scheme) is implicit and always enabled.
- it is not necessary to include in the restriction.
+ Supported for local files ("file" scheme) is implicit and always enabled;
+ it is not necessary to include it in the restriction.
When possible, this static function will use the native file dialog and
not a QFileDialog. On platforms which don't support selecting remote
@@ -2109,8 +2142,6 @@ QUrl QFileDialog::getOpenFileUrl(QWidget *parent,
Options options,
const QStringList &supportedSchemes)
{
- Q_UNUSED(supportedSchemes); // TODO
-
QFileDialogArgs args;
args.parent = parent;
args.caption = caption;
@@ -2121,6 +2152,7 @@ QUrl QFileDialog::getOpenFileUrl(QWidget *parent,
args.options = options;
QFileDialog dialog(args);
+ dialog.setSupportedSchemes(supportedSchemes);
if (selectedFilter && !selectedFilter->isEmpty())
dialog.selectNameFilter(*selectedFilter);
if (dialog.exec() == QDialog::Accepted) {
@@ -2169,11 +2201,6 @@ QUrl QFileDialog::getOpenFileUrl(QWidget *parent,
see the QFileDialog::Option enum for more information on the flags you can
pass.
- \note If you want to iterate over the list of files, you should iterate
- over a copy. For example:
-
- \snippet code/src_gui_dialogs_qfiledialog.cpp 10
-
\warning Do not delete \a parent during the execution of the dialog. If you
want to do this, you should create the dialog yourself using one of the
QFileDialog constructors.
@@ -2190,6 +2217,7 @@ QStringList QFileDialog::getOpenFileNames(QWidget *parent,
const QStringList schemes = QStringList(QStringLiteral("file"));
const QList<QUrl> selectedUrls = getOpenFileUrls(parent, caption, QUrl::fromLocalFile(dir), filter, selectedFilter, options, schemes);
QStringList fileNames;
+ fileNames.reserve(selectedUrls.size());
foreach (const QUrl &url, selectedUrls)
fileNames << url.toLocalFile();
return fileNames;
@@ -2213,8 +2241,8 @@ QStringList QFileDialog::getOpenFileNames(QWidget *parent,
user will be able to select. It is a way for the application to declare
the protocols it will support to fetch the file content. An empty list
means that no restriction is applied (the default).
- Supported for local files ("file" scheme) is implicit and always enabled.
- it is not necessary to include in the restriction.
+ Supported for local files ("file" scheme) is implicit and always enabled;
+ it is not necessary to include it in the restriction.
When possible, this static function will use the native file dialog and
not a QFileDialog. On platforms which don't support selecting remote
@@ -2231,8 +2259,6 @@ QList<QUrl> QFileDialog::getOpenFileUrls(QWidget *parent,
Options options,
const QStringList &supportedSchemes)
{
- Q_UNUSED(supportedSchemes);
-
QFileDialogArgs args;
args.parent = parent;
args.caption = caption;
@@ -2243,6 +2269,7 @@ QList<QUrl> QFileDialog::getOpenFileUrls(QWidget *parent,
args.options = options;
QFileDialog dialog(args);
+ dialog.setSupportedSchemes(supportedSchemes);
if (selectedFilter && !selectedFilter->isEmpty())
dialog.selectNameFilter(*selectedFilter);
if (dialog.exec() == QDialog::Accepted) {
@@ -2332,8 +2359,8 @@ QString QFileDialog::getSaveFileName(QWidget *parent,
user will be able to select. It is a way for the application to declare
the protocols it will support to save the file content. An empty list
means that no restriction is applied (the default).
- Supported for local files ("file" scheme) is implicit and always enabled.
- it is not necessary to include in the restriction.
+ Supported for local files ("file" scheme) is implicit and always enabled;
+ it is not necessary to include it in the restriction.
When possible, this static function will use the native file dialog and
not a QFileDialog. On platforms which don't support selecting remote
@@ -2350,8 +2377,6 @@ QUrl QFileDialog::getSaveFileUrl(QWidget *parent,
Options options,
const QStringList &supportedSchemes)
{
- Q_UNUSED(supportedSchemes);
-
QFileDialogArgs args;
args.parent = parent;
args.caption = caption;
@@ -2362,6 +2387,7 @@ QUrl QFileDialog::getSaveFileUrl(QWidget *parent,
args.options = options;
QFileDialog dialog(args);
+ dialog.setSupportedSchemes(supportedSchemes);
dialog.setAcceptMode(AcceptSave);
if (selectedFilter && !selectedFilter->isEmpty())
dialog.selectNameFilter(*selectedFilter);
@@ -2442,8 +2468,8 @@ QString QFileDialog::getExistingDirectory(QWidget *parent,
user will be able to select. It is a way for the application to declare
the protocols it will support to fetch the file content. An empty list
means that no restriction is applied (the default).
- Supported for local files ("file" scheme) is implicit and always enabled.
- it is not necessary to include in the restriction.
+ Supported for local files ("file" scheme) is implicit and always enabled;
+ it is not necessary to include it in the restriction.
When possible, this static function will use the native file dialog and
not a QFileDialog. On platforms which don't support selecting remote
@@ -2458,8 +2484,6 @@ QUrl QFileDialog::getExistingDirectoryUrl(QWidget *parent,
Options options,
const QStringList &supportedSchemes)
{
- Q_UNUSED(supportedSchemes);
-
QFileDialogArgs args;
args.parent = parent;
args.caption = caption;
@@ -2468,6 +2492,7 @@ QUrl QFileDialog::getExistingDirectoryUrl(QWidget *parent,
args.options = options;
QFileDialog dialog(args);
+ dialog.setSupportedSchemes(supportedSchemes);
if (dialog.exec() == QDialog::Accepted)
return dialog.selectedUrls().value(0);
return QUrl();
@@ -2669,7 +2694,9 @@ void QFileDialogPrivate::saveSettings()
settings.setValue(QLatin1String("treeViewHeader"), qFileDialogUi->treeView->header()->saveState());
}
QStringList historyUrls;
- foreach (const QString &path, q->history())
+ const QStringList history = q->history();
+ historyUrls.reserve(history.size());
+ foreach (const QString &path, history)
historyUrls << QUrl::fromLocalFile(path).toString();
settings.setValue(QLatin1String("history"), historyUrls);
settings.setValue(QLatin1String("lastVisited"), lastVisitedDir()->toString());
@@ -2795,7 +2822,9 @@ void QFileDialogPrivate::init(const QUrl &directory, const QString &nameFilter,
qFileDialogUi->sidebar->hide();
#endif
- q->resize(q->sizeHint());
+ const QSize sizeHint = q->sizeHint();
+ if (sizeHint.isValid())
+ q->resize(sizeHint);
}
/*!
@@ -2971,7 +3000,7 @@ void QFileDialogPrivate::createWidgets()
q->setHistory(options->history());
if (options->initiallySelectedFiles().count() == 1)
q->selectFile(options->initiallySelectedFiles().first().fileName());
- foreach (QUrl url, options->initiallySelectedFiles())
+ foreach (const QUrl &url, options->initiallySelectedFiles())
q->selectUrl(url);
lineEdit()->selectAll();
_q_updateOkButton();
@@ -3764,6 +3793,12 @@ void QFileDialogPrivate::_q_nativeEnterDirectory(const QUrl &directory)
bool QFileDialogPrivate::itemViewKeyboardEvent(QKeyEvent *event) {
Q_Q(QFileDialog);
+
+ if (event->matches(QKeySequence::Cancel)) {
+ q->reject();
+ return true;
+ }
+
switch (event->key()) {
case Qt::Key_Backspace:
_q_navigateToParent();
@@ -3779,9 +3814,6 @@ bool QFileDialogPrivate::itemViewKeyboardEvent(QKeyEvent *event) {
return true;
}
break;
- case Qt::Key_Escape:
- q->hide();
- return true;
default:
break;
}
@@ -3792,7 +3824,7 @@ QString QFileDialogPrivate::getEnvironmentVariable(const QString &string)
{
#ifdef Q_OS_UNIX
if (string.size() > 1 && string.startsWith(QLatin1Char('$'))) {
- return QString::fromLocal8Bit(getenv(string.mid(1).toLatin1().constData()));
+ return QString::fromLocal8Bit(qgetenv(string.mid(1).toLatin1().constData()));
}
#else
if (string.size() > 2 && string.startsWith(QLatin1Char('%')) && string.endsWith(QLatin1Char('%'))) {
@@ -3968,7 +4000,7 @@ void QFileDialogLineEdit::keyPressEvent(QKeyEvent *e)
int key = e->key();
QLineEdit::keyPressEvent(e);
- if (key != Qt::Key_Escape && key != Qt::Key_Back)
+ if (!e->matches(QKeySequence::Cancel) && key != Qt::Key_Back)
e->accept();
}