From 5c238bd6a4ccbcdc5579b56bd4d62d5d35a74e8f Mon Sep 17 00:00:00 2001 From: xust Date: Fri, 20 Dec 2024 15:47:32 +0800 Subject: [PATCH] fix: save last visited url in filedialog. - if directory is not setted when using FileDialog, use lastVisitedUrl as default; Log: as above. --- .../dbus/filedialoghandle.cpp | 6 +++-- .../views/filedialog.cpp | 23 +++++++++++++++++-- .../filedialogplugin-core/views/filedialog.h | 1 + .../views/filedialog_p.h | 2 ++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/plugins/filedialog/filedialogplugin-core/dbus/filedialoghandle.cpp b/src/plugins/filedialog/filedialogplugin-core/dbus/filedialoghandle.cpp index e6620fec39..6ad009ce1e 100644 --- a/src/plugins/filedialog/filedialogplugin-core/dbus/filedialoghandle.cpp +++ b/src/plugins/filedialog/filedialogplugin-core/dbus/filedialoghandle.cpp @@ -48,8 +48,10 @@ FileDialogHandle::FileDialogHandle(QWidget *parent) fmCritical() << "File Dialog: Create window failed"; abort(); } - auto &&defaultPath { DFMBASE_NAMESPACE::StandardPaths::location(StandardPaths::kHomePath) }; - d_func()->dialog->cd(QUrl::fromLocalFile(defaultPath)); + auto defaultUrl = d_func()->dialog->lastVisitedUrl(); + if (!defaultUrl.isValid()) + defaultUrl = QUrl::fromLocalFile(DFMBASE_NAMESPACE::StandardPaths::location(StandardPaths::kHomePath)); + d_func()->dialog->cd(defaultUrl); //! no need to hide, if the dialog is showed in creating, it must be bug. //! see bug#22564 diff --git a/src/plugins/filedialog/filedialogplugin-core/views/filedialog.cpp b/src/plugins/filedialog/filedialogplugin-core/views/filedialog.cpp index 2c8c83dc26..4cc4984d69 100644 --- a/src/plugins/filedialog/filedialogplugin-core/views/filedialog.cpp +++ b/src/plugins/filedialog/filedialogplugin-core/views/filedialog.cpp @@ -39,7 +39,7 @@ #include #include #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) -#include +# include #endif #include @@ -57,6 +57,15 @@ FileDialogPrivate::FileDialogPrivate(FileDialog *qq) //! fix: FileDialog no needs to restore window state on creating. //! see FileManagerWindowsManager::createWindow q->setProperty("_dfm_Disable_RestoreWindowState_", true); + + QSettings qtSets(QSettings::UserScope, QLatin1String("QtProject")); + lastVisitedDir = qtSets.value("FileDialog/lastVisited").toUrl(); +} + +FileDialogPrivate::~FileDialogPrivate() +{ + QSettings qtSets(QSettings::UserScope, QLatin1String("QtProject")); + qtSets.setValue("FileDialog/lastVisited", lastVisitedDir.toString()); } void FileDialogPrivate::handleSaveAcceptBtnClicked() @@ -74,7 +83,7 @@ void FileDialogPrivate::handleSaveAcceptBtnClicked() if (!q->directory().exists()) return; - QString fileName = q->statusBar()->lineEdit()->text(); // 文件名 + QString fileName = q->statusBar()->lineEdit()->text(); // 文件名 // Check whether the suffix needs to be added QString suffix { "" }; if (checkFileSuffix(fileName, suffix)) { @@ -236,6 +245,8 @@ void FileDialog::cd(const QUrl &url) { FileManagerWindow::cd(url); + d->lastVisitedDir = url; + auto window = FMWindowsIns.findWindowById(this->internalWinId()); if (!window) return; @@ -267,6 +278,11 @@ QFileDialog::ViewMode FileDialog::currentViewMode() const return QFileDialog::List; } +QUrl FileDialog::lastVisitedUrl() const +{ + return d->lastVisitedDir; +} + void FileDialog::setDirectory(const QString &directory) { QUrl url = UrlRoute::fromLocalFile(directory); @@ -1105,6 +1121,9 @@ void FileDialog::initConnect() this, &FileDialog::selectedNameFilterChanged); #endif connect(this, &FileDialog::selectionFilesChanged, &FileDialog::updateAcceptButtonState); + connect(this, &FileDialog::currentUrlChanged, this, [this](auto url) { + d->lastVisitedDir = url; + }); } void FileDialog::initEventsConnect() diff --git a/src/plugins/filedialog/filedialogplugin-core/views/filedialog.h b/src/plugins/filedialog/filedialogplugin-core/views/filedialog.h index 8e6c5ff613..75f568648d 100644 --- a/src/plugins/filedialog/filedialogplugin-core/views/filedialog.h +++ b/src/plugins/filedialog/filedialogplugin-core/views/filedialog.h @@ -33,6 +33,7 @@ class FileDialog : public DFMBASE_NAMESPACE::FileManagerWindow void cd(const QUrl &url) override; bool saveClosedSate() const override; void updateAsDefaultSize(); + QUrl lastVisitedUrl() const; public: QFileDialog::ViewMode currentViewMode() const; diff --git a/src/plugins/filedialog/filedialogplugin-core/views/filedialog_p.h b/src/plugins/filedialog/filedialogplugin-core/views/filedialog_p.h index 7e0c7de8d4..d25228c8b9 100644 --- a/src/plugins/filedialog/filedialogplugin-core/views/filedialog_p.h +++ b/src/plugins/filedialog/filedialogplugin-core/views/filedialog_p.h @@ -29,6 +29,7 @@ class FileDialogPrivate : public QObject public: explicit FileDialogPrivate(FileDialog *qq); + ~FileDialogPrivate(); void handleSaveAcceptBtnClicked(); void handleOpenAcceptBtnClicked(); @@ -54,6 +55,7 @@ class FileDialogPrivate : public QObject bool allowMixedSelection { false }; QFileDialog::Options options; QUrl currentUrl; + QUrl lastVisitedDir; }; }