QFileDialog (widgets-based): Remember selection in history

Extend the history by a persistent model index list
reflecting the selection.

[ChangeLog][QtWidgets][QFileDialog] The widgets-based dialog now
remembers the selected files when navigating the history

Fixes: QTBUG-71415
Change-Id: I86774439be070c1b922acd0e9a27d029f02f68d3
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
Friedemann Kleint 2018-11-09 11:26:00 +01:00
parent 9d1905da9c
commit 84ce5022ce
3 changed files with 73 additions and 11 deletions

View File

@ -77,6 +77,8 @@
#include <private/qwasmlocalfileaccess_p.h> #include <private/qwasmlocalfileaccess_p.h>
#endif #endif
#include <algorithm>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
Q_GLOBAL_STATIC(QUrl, lastVisitedDir) Q_GLOBAL_STATIC(QUrl, lastVisitedDir)
@ -3372,6 +3374,18 @@ void QFileDialogPrivate::_q_goHome()
q->setDirectory(QDir::homePath()); q->setDirectory(QDir::homePath());
} }
void QFileDialogPrivate::saveHistorySelection()
{
if (qFileDialogUi.isNull() || currentHistoryLocation < 0 || currentHistoryLocation >= currentHistory.size())
return;
auto &item = currentHistory[currentHistoryLocation];
item.selection.clear();
const auto selectedIndexes = qFileDialogUi->listView->selectionModel()->selectedRows();
for (const auto &index : selectedIndexes)
item.selection.append(QPersistentModelIndex(index));
}
/*! /*!
\internal \internal
@ -3385,17 +3399,49 @@ void QFileDialogPrivate::_q_pathChanged(const QString &newPath)
qFileDialogUi->sidebar->selectUrl(QUrl::fromLocalFile(newPath)); qFileDialogUi->sidebar->selectUrl(QUrl::fromLocalFile(newPath));
q->setHistory(qFileDialogUi->lookInCombo->history()); q->setHistory(qFileDialogUi->lookInCombo->history());
if (currentHistoryLocation < 0 || currentHistory.value(currentHistoryLocation) != QDir::toNativeSeparators(newPath)) { const QString newNativePath = QDir::toNativeSeparators(newPath);
// equal paths indicate this was invoked by _q_navigateBack/Forward()
if (currentHistoryLocation < 0 || currentHistory.value(currentHistoryLocation).path != newNativePath) {
if (currentHistoryLocation >= 0)
saveHistorySelection();
while (currentHistoryLocation >= 0 && currentHistoryLocation + 1 < currentHistory.count()) { while (currentHistoryLocation >= 0 && currentHistoryLocation + 1 < currentHistory.count()) {
currentHistory.removeLast(); currentHistory.removeLast();
} }
currentHistory.append(QDir::toNativeSeparators(newPath)); currentHistory.append({newNativePath, PersistentModelIndexList()});
++currentHistoryLocation; ++currentHistoryLocation;
} }
qFileDialogUi->forwardButton->setEnabled(currentHistory.size() - currentHistoryLocation > 1); qFileDialogUi->forwardButton->setEnabled(currentHistory.size() - currentHistoryLocation > 1);
qFileDialogUi->backButton->setEnabled(currentHistoryLocation > 0); qFileDialogUi->backButton->setEnabled(currentHistoryLocation > 0);
} }
void QFileDialogPrivate::navigate(HistoryItem &historyItem)
{
Q_Q(QFileDialog);
q->setDirectory(historyItem.path);
// Restore selection unless something has changed in the file system
if (qFileDialogUi.isNull() || historyItem.selection.isEmpty())
return;
if (std::any_of(historyItem.selection.cbegin(), historyItem.selection.cend(),
[](const QPersistentModelIndex &i) { return !i.isValid(); })) {
historyItem.selection.clear();
return;
}
QAbstractItemView *view = q->viewMode() == QFileDialog::List
? static_cast<QAbstractItemView *>(qFileDialogUi->listView)
: static_cast<QAbstractItemView *>(qFileDialogUi->treeView);
auto selectionModel = view->selectionModel();
const QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::Select
| QItemSelectionModel::Rows;
selectionModel->select(historyItem.selection.constFirst(),
flags | QItemSelectionModel::Clear | QItemSelectionModel::Current);
for (int i = 1, size = historyItem.selection.size(); i < size; ++i)
selectionModel->select(historyItem.selection.at(i), flags);
view->scrollTo(historyItem.selection.constFirst());
}
/*! /*!
\internal \internal
@ -3403,11 +3449,9 @@ void QFileDialogPrivate::_q_pathChanged(const QString &newPath)
*/ */
void QFileDialogPrivate::_q_navigateBackward() void QFileDialogPrivate::_q_navigateBackward()
{ {
Q_Q(QFileDialog);
if (!currentHistory.isEmpty() && currentHistoryLocation > 0) { if (!currentHistory.isEmpty() && currentHistoryLocation > 0) {
--currentHistoryLocation; saveHistorySelection();
QString previousHistory = currentHistory.at(currentHistoryLocation); navigate(currentHistory[--currentHistoryLocation]);
q->setDirectory(previousHistory);
} }
} }
@ -3418,11 +3462,9 @@ void QFileDialogPrivate::_q_navigateBackward()
*/ */
void QFileDialogPrivate::_q_navigateForward() void QFileDialogPrivate::_q_navigateForward()
{ {
Q_Q(QFileDialog);
if (!currentHistory.isEmpty() && currentHistoryLocation < currentHistory.size() - 1) { if (!currentHistory.isEmpty() && currentHistoryLocation < currentHistory.size() - 1) {
++currentHistoryLocation; saveHistorySelection();
QString nextHistory = currentHistory.at(currentHistoryLocation); navigate(currentHistory[++currentHistoryLocation]);
q->setDirectory(nextHistory);
} }
} }

View File

@ -116,6 +116,14 @@ class Q_WIDGETS_EXPORT QFileDialogPrivate : public QDialogPrivate
Q_DECLARE_PUBLIC(QFileDialog) Q_DECLARE_PUBLIC(QFileDialog)
public: public:
using PersistentModelIndexList = QVector<QPersistentModelIndex>;
struct HistoryItem
{
QString path;
PersistentModelIndexList selection;
};
QFileDialogPrivate(); QFileDialogPrivate();
QPlatformFileDialogHelper *platformFileDialogHelper() const QPlatformFileDialogHelper *platformFileDialogHelper() const
@ -193,9 +201,11 @@ public:
void retranslateWindowTitle(); void retranslateWindowTitle();
void retranslateStrings(); void retranslateStrings();
void emitFilesSelected(const QStringList &files); void emitFilesSelected(const QStringList &files);
void saveHistorySelection();
void _q_goHome(); void _q_goHome();
void _q_pathChanged(const QString &); void _q_pathChanged(const QString &);
void navigate(HistoryItem &);
void _q_navigateBackward(); void _q_navigateBackward();
void _q_navigateForward(); void _q_navigateForward();
void _q_navigateToParent(); void _q_navigateToParent();
@ -237,7 +247,7 @@ public:
QString setWindowTitle; QString setWindowTitle;
QStringList currentHistory; QList<HistoryItem> currentHistory;
int currentHistoryLocation; int currentHistoryLocation;
QAction *renameAction; QAction *renameAction;

View File

@ -1421,6 +1421,16 @@ void tst_QFiledialog::clearLineEdit()
QTRY_VERIFY(fd.directory().absolutePath() != workDirPath); QTRY_VERIFY(fd.directory().absolutePath() != workDirPath);
QVERIFY(lineEdit->text().isEmpty()); QVERIFY(lineEdit->text().isEmpty());
// QTBUG-71415: When pressing back, the selection (activated
// directory) should be restored.
QToolButton *backButton = fd.findChild<QToolButton*>("backButton");
QVERIFY(backButton);
QTreeView *treeView = fd.findChildren<QTreeView*>("treeView").value(0);
QVERIFY(treeView);
backButton->click();
QTRY_COMPARE(treeView->selectionModel()->selectedIndexes().value(0).data().toString(),
dirName);
} }
void tst_QFiledialog::enableChooseButton() void tst_QFiledialog::enableChooseButton()