diff options
Diffstat (limited to 'src/widgets/dialogs/qfiledialog.cpp')
-rw-r--r-- | src/widgets/dialogs/qfiledialog.cpp | 163 |
1 files changed, 85 insertions, 78 deletions
diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index 3f3a6e7c65..288922d740 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$ ** @@ -976,7 +982,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"); } @@ -1091,46 +1097,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 @@ -1187,13 +1190,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)); } @@ -1257,7 +1260,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(); @@ -1284,7 +1287,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; } @@ -1554,7 +1557,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); @@ -2218,7 +2221,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; } @@ -2587,7 +2590,7 @@ void QFileDialog::accept() return; } - QStringList files = selectedFiles(); + const QStringList files = selectedFiles(); if (files.isEmpty()) return; QString lineEditText = d->lineEdit()->text(); @@ -2657,10 +2660,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,7 +2699,7 @@ void QFileDialogPrivate::saveSettings() 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()); @@ -2730,7 +2733,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(); @@ -2760,8 +2764,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(); @@ -3000,7 +3006,8 @@ void QFileDialogPrivate::createWidgets() 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(); + for (const QUrl &url : initiallySelectedFiles) q->selectUrl(url); lineEdit()->selectAll(); _q_updateOkButton(); @@ -3444,15 +3451,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 + 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) @@ -3479,7 +3484,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('\\'))) { @@ -3538,10 +3543,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; @@ -3682,14 +3687,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) { @@ -3757,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()) @@ -4044,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 |