Skip to content

Commit

Permalink
Fixed file dialog start location
Browse files Browse the repository at this point in the history
It was using Session.activeFile, which only gets updated when switching
the session. In addition, since recent files now only get added when
closing files, the file dialog would repeatedly open in the user's HOME
folder, which is just annoying.
  • Loading branch information
bjorn committed Jun 25, 2020
1 parent 8376b44 commit 8b8a7ee
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
28 changes: 27 additions & 1 deletion src/tiled/documentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "mapview.h"
#include "noeditorwidget.h"
#include "preferences.h"
#include "projectmanager.h"
#include "session.h"
#include "tabbar.h"
#include "terrain.h"
Expand All @@ -61,6 +62,7 @@
#include <QMessageBox>
#include <QScrollBar>
#include <QStackedLayout>
#include <QStandardPaths>
#include <QTabBar>
#include <QTabWidget>
#include <QUndoGroup>
Expand Down Expand Up @@ -714,7 +716,7 @@ bool DocumentManager::saveDocumentAs(Document *document)

auto getSaveFileName = [&](const QString &filter, const QString &defaultFileName) {
if (fileName.isEmpty()) {
fileName = Preferences::instance()->fileDialogStartLocation();
fileName = fileDialogStartLocation();
fileName += QLatin1Char('/');
fileName += defaultFileName;
fileName += Utils::firstExtension(selectedFilter);
Expand Down Expand Up @@ -1270,6 +1272,30 @@ bool DocumentManager::isWorldModified(const QString &fileName) const
return false;
}

/**
* Returns a logical start location for a file dialog to open a file, based on
* the currently selected file, a recent file, the project path or finally, the
* home location.
*/
QString DocumentManager::fileDialogStartLocation() const
{
if (auto doc = currentDocument()) {
QString path = QFileInfo(doc->fileName()).path();
if (!path.isEmpty())
return path;
}

const auto &session = Session::current();
if (!session.recentFiles.isEmpty())
return QFileInfo(session.recentFiles.first()).path();

const auto &project = ProjectManager::instance()->project();
if (!project.fileName().isEmpty())
return QFileInfo(project.fileName()).path();

return QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
}

void DocumentManager::onWorldUnloaded(const QString &worldFile)
{
delete mWorldDocuments.take(worldFile);
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/documentmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ class DocumentManager : public QObject
bool isAnyWorldModified() const;
bool isWorldModified(const QString &fileName) const;

QString fileDialogStartLocation() const;

signals:
void documentCreated(Document *document);
void documentOpened(Document *document);
Expand Down
3 changes: 1 addition & 2 deletions src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,9 +1048,8 @@ void MainWindow::openFileDialog()

FormatHelper<FileFormat> helper(FileFormat::Read, allFilesFilter);

auto preferences = Preferences::instance();
const auto fileNames = QFileDialog::getOpenFileNames(this, tr("Open File"),
preferences->fileDialogStartLocation(),
mDocumentManager->fileDialogStartLocation(),
helper.filter(),
&selectedFilter);
if (fileNames.isEmpty())
Expand Down
12 changes: 0 additions & 12 deletions src/tiled/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,18 +570,6 @@ void Preferences::setDonationDialogReminder(const QDate &date)
setValue(QLatin1String("Install/DonationDialogTime"), date.toString(Qt::ISODate));
}

QString Preferences::fileDialogStartLocation() const
{
const auto &session = Session::current();
if (!session.activeFile.isEmpty())
return QFileInfo(session.activeFile).path();

if (!session.recentFiles.isEmpty())
return QFileInfo(session.recentFiles.first()).path();

return QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
}

/**
* Adds the given file to the recent files list.
*/
Expand Down
1 change: 0 additions & 1 deletion src/tiled/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ class Preferences : public QSettings
void setDonationDialogReminder(const QDate &date);

enum { MaxRecentFiles = 12 };
QString fileDialogStartLocation() const;
void addRecentFile(const QString &fileName);

QStringList recentProjects() const;
Expand Down

0 comments on commit 8b8a7ee

Please sign in to comment.