Skip to content

Commit

Permalink
Refactoring of Session and Preferences
Browse files Browse the repository at this point in the history
* Session is now based on QSettings with a JSON backend, instead of
  manually saving and loading to JSON.

* Preferences just forwards to QSettings backend instead of storing
  the settings internally as well.
  • Loading branch information
bjorn committed Mar 24, 2020
1 parent 15dd197 commit a9ad617
Show file tree
Hide file tree
Showing 9 changed files with 466 additions and 758 deletions.
4 changes: 2 additions & 2 deletions src/tiled/documentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1225,8 +1225,8 @@ void DocumentManager::updateSession() const
auto doc = currentDocument();
auto prefs = Preferences::instance();

prefs->session().setOpenFiles(fileList);
prefs->session().setActiveFile(doc ? doc->fileName() : QString());
prefs->session().openFiles = fileList;
prefs->session().activeFile = doc ? doc->fileName() : QString();
prefs->saveSession();
}

Expand Down
28 changes: 14 additions & 14 deletions src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,17 +896,15 @@ void MainWindow::initializeSession()
if (!prefs->restoreSessionOnStartup())
return;

Session session = Session::load(prefs->lastSession());
const auto &session { prefs->session() };

// Restore associated project if applicable
Project project;
if (!session.project().isEmpty() && project.load(session.project())) {
if (!session.project.isEmpty() && project.load(session.project)) {
mProjectDock->setProject(std::move(project));
updateWindowTitle();
}

prefs->switchSession(std::move(session));

restoreSession();
}

Expand Down Expand Up @@ -1228,8 +1226,8 @@ void MainWindow::openProjectFile(const QString &fileName)
if (!closeAllFiles())
return;

Session session = Session::load(Session::defaultFileNameForProject(fileName));
session.setProject(fileName);
Session session { Session::defaultFileNameForProject(fileName) };
session.project = fileName;

mProjectDock->setProject(std::move(project));
prefs->addRecentProject(fileName);
Expand Down Expand Up @@ -1291,10 +1289,12 @@ void MainWindow::saveProjectAs()
}

prefs->addRecentProject(fileName);
prefs->session().setProject(fileName);
prefs->session().project = fileName;

const auto sessionFileName = Session::defaultFileNameForProject(fileName);
prefs->saveSessionNow(sessionFileName);
prefs->session().setFileName(sessionFileName);

prefs->saveSessionNow();
prefs->setLastSession(sessionFileName);

updateWindowTitle();
Expand All @@ -1314,7 +1314,7 @@ void MainWindow::closeProject()
return;

mProjectDock->setProject(Project{});
prefs->switchSession(Session::load(Session::defaultFileName()));
prefs->switchSession(Session { Session::defaultFileName() });

restoreSession();
updateWindowTitle();
Expand All @@ -1326,14 +1326,14 @@ void MainWindow::restoreSession()
const auto &session = Preferences::instance()->session();

// Copy values because the session will get changed while restoring it
const auto openFiles = session.openFiles();
const auto activeFile = session.activeFile();
const auto openFiles = session.openFiles;
const auto activeFile = session.activeFile;

for (const QString &file : openFiles)
openFile(file);
mDocumentManager->switchToDocument(activeFile);

mProjectDock->setExpandedPaths(session.expandedProjectPaths());
mProjectDock->setExpandedPaths(session.expandedProjectPaths);
}

void MainWindow::cut()
Expand Down Expand Up @@ -1707,7 +1707,7 @@ void MainWindow::openRecentFile()

void MainWindow::reopenClosedFile()
{
const auto recentFiles = Preferences::instance()->session().recentFiles();
const auto recentFiles = Preferences::instance()->session().recentFiles;
for (const QString &file : recentFiles) {
if (mDocumentManager->findDocument(file) == -1) {
openFile(file);
Expand All @@ -1728,7 +1728,7 @@ void MainWindow::openRecentProject()
*/
void MainWindow::updateRecentFilesMenu()
{
const QStringList files = Preferences::instance()->session().recentFiles();
const QStringList files = Preferences::instance()->session().recentFiles;
const int numRecentFiles = qMin<int>(files.size(), Preferences::MaxRecentFiles);

for (int i = 0; i < numRecentFiles; ++i) {
Expand Down

0 comments on commit a9ad617

Please sign in to comment.