Skip to content

Commit

Permalink
fix: save last visited url in filedialog.
Browse files Browse the repository at this point in the history
- if directory is not setted when using FileDialog, use lastVisitedUrl
as default;

Log: as above.
  • Loading branch information
itsXuSt authored and Johnson-zs committed Dec 26, 2024
1 parent a021236 commit 5c238bd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions src/plugins/filedialog/filedialogplugin-core/views/filedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include <QJsonArray>

Check warning on line 39 in src/plugins/filedialog/filedialogplugin-core/views/filedialog.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QJsonArray> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 39 in src/plugins/filedialog/filedialogplugin-core/views/filedialog.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QJsonArray> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QJsonObject>

Check warning on line 40 in src/plugins/filedialog/filedialogplugin-core/views/filedialog.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QJsonObject> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 40 in src/plugins/filedialog/filedialogplugin-core/views/filedialog.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QJsonObject> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
#include <QDesktopWidget>
# include <QDesktopWidget>
#endif

#include <qpa/qplatformdialoghelper.h>

Check warning on line 45 in src/plugins/filedialog/filedialogplugin-core/views/filedialog.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <qpa/qplatformdialoghelper.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 45 in src/plugins/filedialog/filedialogplugin-core/views/filedialog.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <qpa/qplatformdialoghelper.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
Expand All @@ -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()
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class FileDialogPrivate : public QObject

public:
explicit FileDialogPrivate(FileDialog *qq);
~FileDialogPrivate();

void handleSaveAcceptBtnClicked();
void handleOpenAcceptBtnClicked();
Expand All @@ -54,6 +55,7 @@ class FileDialogPrivate : public QObject
bool allowMixedSelection { false };
QFileDialog::Options options;
QUrl currentUrl;
QUrl lastVisitedDir;
};

}
Expand Down

0 comments on commit 5c238bd

Please sign in to comment.