diff options
Diffstat (limited to 'src/widgets/dialogs/qfiledialog.cpp')
-rw-r--r-- | src/widgets/dialogs/qfiledialog.cpp | 208 |
1 files changed, 104 insertions, 104 deletions
diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index f8e4299397..094f2680c6 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -1,31 +1,37 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtWidgets module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** 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. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://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 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 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-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -526,7 +532,6 @@ QFileDialogPrivate::QFileDialogPrivate() deleteAction(0), showHiddenAction(0), useDefaultCaption(true), - defaultFileTypes(true), qFileDialogUi(0), options(new QFileDialogOptions) { @@ -657,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; @@ -700,10 +705,11 @@ bool QFileDialogPrivate::canBeNativeDialog() const const QDialog * const q = static_cast<const QDialog*>(q_ptr); if (nativeDialogInUse) return true; - if (q->testAttribute(Qt::WA_DontShowOnScreen)) - return false; - if (options->options() & QFileDialog::DontUseNativeDialog) + if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs) + || q->testAttribute(Qt::WA_DontShowOnScreen) + || (options->options() & QFileDialog::DontUseNativeDialog)) { return false; + } QLatin1String staticName(QFileDialog::staticMetaObject.className()); QLatin1String dynamicName(q->metaObject()->className()); @@ -782,10 +788,8 @@ void QFileDialog::setOptions(Options options) if (changed & DontUseCustomDirectoryIcons) { QFileIconProvider::Options providerOptions = iconProvider()->options(); - if (options & DontUseCustomDirectoryIcons) - providerOptions |= QFileIconProvider::DontUseCustomDirectoryIcons; - else - providerOptions &= ~QFileIconProvider::DontUseCustomDirectoryIcons; + providerOptions.setFlag(QFileIconProvider::DontUseCustomDirectoryIcons, + options & DontUseCustomDirectoryIcons); iconProvider()->setOptions(providerOptions); } } @@ -981,7 +985,7 @@ void QFileDialog::setDirectoryUrl(const QUrl &directory) d->setDirectory_sys(directory); else if (directory.isLocalFile()) setDirectory(directory.toLocalFile()); - else if (d->usingWidgets()) + else if (Q_UNLIKELY(d->usingWidgets())) qWarning("Non-native QFileDialog supports only local files"); } @@ -1096,46 +1100,43 @@ void QFileDialog::selectUrl(const QUrl &url) } #ifdef Q_OS_UNIX -Q_AUTOTEST_EXPORT QString qt_tildeExpansion(const QString &path, bool *expanded = 0) +Q_AUTOTEST_EXPORT QString qt_tildeExpansion(const QString &path) { - if (expanded != 0) - *expanded = false; if (!path.startsWith(QLatin1Char('~'))) return path; - QString ret = path; - QStringList tokens = ret.split(QDir::separator()); - if (tokens.first() == QLatin1String("~")) { - ret.replace(0, 1, QDir::homePath()); + int separatorPosition = path.indexOf(QDir::separator()); + if (separatorPosition < 0) + separatorPosition = path.size(); + if (separatorPosition == 1) { + return QDir::homePath() + path.midRef(1); } else { - QString userName = tokens.first(); - userName.remove(0, 1); -#if defined(Q_OS_VXWORKS) +#if defined(Q_OS_VXWORKS) || defined(Q_OS_INTEGRITY) const QString homePath = QDir::homePath(); -#elif defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD) +#else + const QByteArray userName = path.midRef(1, separatorPosition - 1).toLocal8Bit(); +# if defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD) passwd pw; passwd *tmpPw; char buf[200]; const int bufSize = sizeof(buf); int err = 0; -#if defined(Q_OS_SOLARIS) && (_POSIX_C_SOURCE - 0 < 199506L) - tmpPw = getpwnam_r(userName.toLocal8Bit().constData(), &pw, buf, bufSize); -#else - err = getpwnam_r(userName.toLocal8Bit().constData(), &pw, buf, bufSize, &tmpPw); -#endif +# if defined(Q_OS_SOLARIS) && (_POSIX_C_SOURCE - 0 < 199506L) + tmpPw = getpwnam_r(userName.constData(), &pw, buf, bufSize); +# else + err = getpwnam_r(userName.constData(), &pw, buf, bufSize, &tmpPw); +# endif if (err || !tmpPw) - return ret; + return path; const QString homePath = QString::fromLocal8Bit(pw.pw_dir); -#else - passwd *pw = getpwnam(userName.toLocal8Bit().constData()); +# else + passwd *pw = getpwnam(userName.constData()); if (!pw) - return ret; + return path; const QString homePath = QString::fromLocal8Bit(pw->pw_dir); +# endif #endif - ret.replace(0, tokens.first().length(), homePath); + return homePath + path.midRef(separatorPosition); } - if (expanded != 0) - *expanded = true; - return ret; } #endif @@ -1192,13 +1193,13 @@ QList<QUrl> QFileDialogPrivate::userSelectedFiles() const const QModelIndexList selectedRows = qFileDialogUi->listView->selectionModel()->selectedRows(); files.reserve(selectedRows.size()); - foreach (const QModelIndex &index, selectedRows) + for (const QModelIndex &index : selectedRows) files.append(QUrl::fromLocalFile(index.data(QFileSystemModel::FilePathRole).toString())); if (files.isEmpty() && !lineEdit()->text().isEmpty()) { const QStringList typedFilesList = typedFiles(); files.reserve(typedFilesList.size()); - foreach (const QString &path, typedFilesList) + for (const QString &path : typedFilesList) files.append(QUrl::fromLocalFile(path)); } @@ -1262,7 +1263,7 @@ QStringList QFileDialog::selectedFiles() const QStringList files; const QList<QUrl> userSelectedFiles = d->userSelectedFiles(); files.reserve(userSelectedFiles.size()); - foreach (const QUrl &file, userSelectedFiles) + for (const QUrl &file : userSelectedFiles) files.append(file.toLocalFile()); if (files.isEmpty() && d->usingWidgets()) { const FileMode fm = fileMode(); @@ -1289,7 +1290,7 @@ QList<QUrl> QFileDialog::selectedUrls() const QList<QUrl> urls; const QStringList selectedFileList = selectedFiles(); urls.reserve(selectedFileList.size()); - foreach (const QString &file, selectedFileList) + for (const QString &file : selectedFileList) urls.append(QUrl::fromLocalFile(file)); return urls; } @@ -1403,7 +1404,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); @@ -1532,7 +1532,7 @@ static QString nameFilterForMime(const QString &mimeType) return QFileDialog::tr("All files (*)"); } else { const QString patterns = mime.globPatterns().join(QLatin1Char(' ')); - return mime.comment() + QStringLiteral(" (") + patterns + QLatin1Char(')'); + return mime.comment() + QLatin1String(" (") + patterns + QLatin1Char(')'); } } return QString(); @@ -1559,7 +1559,7 @@ void QFileDialog::setMimeTypeFilters(const QStringList &filters) { Q_D(QFileDialog); QStringList nameFilters; - foreach (const QString &mimeType, filters) { + for (const QString &mimeType : filters) { const QString text = nameFilterForMime(mimeType); if (!text.isEmpty()) nameFilters.append(text); @@ -2225,7 +2225,7 @@ QStringList QFileDialog::getOpenFileNames(QWidget *parent, 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) + for (const QUrl &url : selectedUrls) fileNames << url.toLocalFile(); return fileNames; } @@ -2429,8 +2429,7 @@ QUrl QFileDialog::getSaveFileUrl(QWidget *parent, native file dialog and not a QFileDialog. However, the native Windows file dialog does not support displaying files in the directory chooser. You need to pass \l{QFileDialog::}{DontUseNativeDialog} to display files using a - QFileDialog. On Windows CE, if the device has no native file dialog, a - QFileDialog will be used. + QFileDialog. On Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if \c{/usr/tmp} is a symlink to \c{/var/tmp}, @@ -2594,7 +2593,7 @@ void QFileDialog::accept() return; } - QStringList files = selectedFiles(); + const QStringList files = selectedFiles(); if (files.isEmpty()) return; QString lineEditText = d->lineEdit()->text(); @@ -2664,10 +2663,10 @@ void QFileDialog::accept() case ExistingFile: case ExistingFiles: - for (int i = 0; i < files.count(); ++i) { - QFileInfo info(files.at(i)); + for (const auto &file : files) { + QFileInfo info(file); if (!info.exists()) - info = QFileInfo(d->getEnvironmentVariable(files.at(i))); + info = QFileInfo(d->getEnvironmentVariable(file)); if (!info.exists()) { #ifndef QT_NO_MESSAGEBOX QString message = tr("%1\nFile not found.\nPlease verify the " @@ -2696,14 +2695,14 @@ void QFileDialogPrivate::saveSettings() settings.beginGroup(QLatin1String("FileDialog")); if (usingWidgets()) { - settings.setValue(QLatin1String("sidebarWidth"), qFileDialogUi->splitter->sizes().first()); + settings.setValue(QLatin1String("sidebarWidth"), qFileDialogUi->splitter->sizes().constFirst()); settings.setValue(QLatin1String("shortcuts"), QUrl::toStringList(qFileDialogUi->sidebar->urls())); settings.setValue(QLatin1String("treeViewHeader"), qFileDialogUi->treeView->header()->saveState()); } QStringList historyUrls; const QStringList history = q->history(); historyUrls.reserve(history.size()); - foreach (const QString &path, history) + for (const QString &path : history) historyUrls << QUrl::fromLocalFile(path).toString(); settings.setValue(QLatin1String("history"), historyUrls); settings.setValue(QLatin1String("lastVisited"), lastVisitedDir()->toString()); @@ -2737,7 +2736,8 @@ bool QFileDialogPrivate::restoreFromSettings() return true; QStringList history; - foreach (const QString &urlStr, settings.value(QLatin1String("history")).toStringList()) { + const auto urlStrings = settings.value(QLatin1String("history")).toStringList(); + for (const QString &urlStr : urlStrings) { QUrl url(urlStr); if (url.isLocalFile()) history << url.toLocalFile(); @@ -2767,8 +2767,10 @@ bool QFileDialogPrivate::restoreWidgetState(QStringList &history, int splitterPo } qFileDialogUi->sidebar->setUrls(sidebarUrls); - while (history.count() > 5) - history.pop_front(); + + static const int MaxHistorySize = 5; + if (history.size() > MaxHistorySize) + history.erase(history.begin(), history.end() - MaxHistorySize); q->setHistory(history); QHeaderView *headerView = qFileDialogUi->treeView->header(); @@ -2804,7 +2806,7 @@ void QFileDialogPrivate::init(const QUrl &directory, const QString &nameFilter, } q->setAcceptMode(QFileDialog::AcceptOpen); - nativeDialogInUse = (canBeNativeDialog() && platformFileDialogHelper() != 0); + nativeDialogInUse = platformFileDialogHelper() != 0; if (!nativeDialogInUse) createWidgets(); q->setFileMode(QFileDialog::AnyFile); @@ -3005,9 +3007,10 @@ void QFileDialogPrivate::createWidgets() q->selectNameFilter(options->initiallySelectedNameFilter()); q->setDefaultSuffix(options->defaultSuffix()); q->setHistory(options->history()); - if (options->initiallySelectedFiles().count() == 1) - q->selectFile(options->initiallySelectedFiles().first().fileName()); - foreach (const QUrl &url, options->initiallySelectedFiles()) + const auto initiallySelectedFiles = options->initiallySelectedFiles(); + if (initiallySelectedFiles.size() == 1) + q->selectFile(initiallySelectedFiles.first().fileName()); + for (const QUrl &url : initiallySelectedFiles) q->selectUrl(url); lineEdit()->selectAll(); _q_updateOkButton(); @@ -3451,15 +3454,13 @@ void QFileDialogPrivate::_q_autoCompleteFileName(const QString &text) return; } - QStringList multipleFiles = typedFiles(); + const QStringList multipleFiles = typedFiles(); if (multipleFiles.count() > 0) { QModelIndexList oldFiles = qFileDialogUi->listView->selectionModel()->selectedRows(); - QModelIndexList newFiles; - for (int i = 0; i < multipleFiles.count(); ++i) { - QModelIndex idx = model->index(multipleFiles.at(i)); - if (oldFiles.contains(idx)) - oldFiles.removeAll(idx); - else + QVector<QModelIndex> newFiles; + for (const auto &file : multipleFiles) { + QModelIndex idx = model->index(file); + if (oldFiles.removeAll(idx) == 0) newFiles.append(idx); } for (int i = 0; i < newFiles.count(); ++i) @@ -3486,7 +3487,7 @@ void QFileDialogPrivate::_q_updateOkButton() bool enableButton = true; bool isOpenDirectory = false; - QStringList files = q->selectedFiles(); + const QStringList files = q->selectedFiles(); QString lineEditText = lineEdit()->text(); if (lineEditText.startsWith(QLatin1String("//")) || lineEditText.startsWith(QLatin1Char('\\'))) { @@ -3545,10 +3546,10 @@ void QFileDialogPrivate::_q_updateOkButton() } case QFileDialog::ExistingFile: case QFileDialog::ExistingFiles: - for (int i = 0; i < files.count(); ++i) { - QModelIndex idx = model->index(files.at(i)); + for (const auto &file : files) { + QModelIndex idx = model->index(file); if (!idx.isValid()) - idx = model->index(getEnvironmentVariable(files.at(i))); + idx = model->index(getEnvironmentVariable(file)); if (!idx.isValid()) { enableButton = false; break; @@ -3689,14 +3690,14 @@ void QFileDialogPrivate::_q_useNameFilter(int index) void QFileDialogPrivate::_q_selectionChanged() { const QFileDialog::FileMode fileMode = q_func()->fileMode(); - QModelIndexList indexes = qFileDialogUi->listView->selectionModel()->selectedRows(); + const QModelIndexList indexes = qFileDialogUi->listView->selectionModel()->selectedRows(); bool stripDirs = (fileMode != QFileDialog::DirectoryOnly && fileMode != QFileDialog::Directory); QStringList allFiles; - for (int i = 0; i < indexes.count(); ++i) { - if (stripDirs && model->isDir(mapToSource(indexes.at(i)))) + for (const auto &index : indexes) { + if (stripDirs && model->isDir(mapToSource(index))) continue; - allFiles.append(indexes.at(i).data().toString()); + allFiles.append(index.data().toString()); } if (allFiles.count() > 1) for (int i = 0; i < allFiles.count(); ++i) { @@ -3719,10 +3720,7 @@ void QFileDialogPrivate::_q_showHidden() { Q_Q(QFileDialog); QDir::Filters dirFilters = q->filter(); - if (showHiddenAction->isChecked()) - dirFilters |= QDir::Hidden; - else - dirFilters &= ~QDir::Hidden; + dirFilters.setFlag(QDir::Hidden, showHiddenAction->isChecked()); q->setFilter(dirFilters); } @@ -3764,7 +3762,7 @@ void QFileDialogPrivate::_q_emitUrlsSelected(const QList<QUrl> &files) Q_Q(QFileDialog); emit q->urlsSelected(files); QStringList localFiles; - foreach (const QUrl &file, files) + for (const QUrl &file : files) if (file.isLocalFile()) localFiles.append(file.toLocalFile()); if (!localFiles.isEmpty()) @@ -3831,11 +3829,11 @@ QString QFileDialogPrivate::getEnvironmentVariable(const QString &string) { #ifdef Q_OS_UNIX if (string.size() > 1 && string.startsWith(QLatin1Char('$'))) { - return QString::fromLocal8Bit(qgetenv(string.mid(1).toLatin1().constData())); + return QString::fromLocal8Bit(qgetenv(string.midRef(1).toLatin1().constData())); } #else if (string.size() > 2 && string.startsWith(QLatin1Char('%')) && string.endsWith(QLatin1Char('%'))) { - return QString::fromLocal8Bit(qgetenv(string.mid(1, string.size() - 2).toLatin1().constData())); + return QString::fromLocal8Bit(qgetenv(string.midRef(1, string.size() - 2).toLatin1().constData())); } #endif return string; @@ -4051,15 +4049,17 @@ QStringList QFSCompleter::splitPath(const QString &path) const else doubleSlash.clear(); #elif defined(Q_OS_UNIX) - bool expanded; - pathCopy = qt_tildeExpansion(pathCopy, &expanded); - if (expanded) { - QFileSystemModel *dirModel; - if (proxyModel) - dirModel = qobject_cast<QFileSystemModel *>(proxyModel->sourceModel()); - else - dirModel = sourceModel; - dirModel->fetchMore(dirModel->index(pathCopy)); + { + QString tildeExpanded = qt_tildeExpansion(pathCopy); + if (tildeExpanded != pathCopy) { + QFileSystemModel *dirModel; + if (proxyModel) + dirModel = qobject_cast<QFileSystemModel *>(proxyModel->sourceModel()); + else + dirModel = sourceModel; + dirModel->fetchMore(dirModel->index(tildeExpanded)); + } + pathCopy = std::move(tildeExpanded); } #endif @@ -4101,7 +4101,7 @@ QStringList QFSCompleter::splitPath(const QString &path) const parts.removeFirst(); currentLocationList.removeLast(); } - if (!currentLocationList.isEmpty() && currentLocationList.last().isEmpty()) + if (!currentLocationList.isEmpty() && currentLocationList.constLast().isEmpty()) currentLocationList.removeLast(); return currentLocationList + parts; } |