Skip to content

Commit

Permalink
Introduced sessions
Browse files Browse the repository at this point in the history
The session is currently implicit, being either the default session or a
project-based session saved alongside a project.

It stores the list of open files, the active file, recent files and the
last state for previously opened files. This makes it easier to switch
between different projects, which swaps all these things along with it.

Also improved the start location for the open file dialog, which now
starts at the active file, then the first recent file and finally the
user home directory (instead of starting at the current working
directory, usually at the binary).

Issue #1665
  • Loading branch information
bjorn committed Dec 11, 2019
1 parent 5d970e3 commit ee65109
Show file tree
Hide file tree
Showing 18 changed files with 696 additions and 255 deletions.
31 changes: 30 additions & 1 deletion src/tiled/documentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,16 @@ void DocumentManager::switchToDocument(int index)
mTabBar->setCurrentIndex(index);
}

bool DocumentManager::switchToDocument(const QString &fileName)
{
const int index = findDocument(fileName);
if (index != -1) {
switchToDocument(index);
return true;
}
return false;
}

/**
* Switches to the given \a document, if there is already a tab open for it.
* \return whether the switch was succesful
Expand All @@ -418,7 +428,6 @@ bool DocumentManager::switchToDocument(Document *document)
switchToDocument(index);
return true;
}

return false;
}

Expand Down Expand Up @@ -532,6 +541,8 @@ void DocumentManager::insertDocument(int index, const DocumentPtr &document)
if (mBrokenLinksModel->hasBrokenLinks())
mBrokenLinksWidget->show();

updateSession();

emit documentOpened(documentPtr);
}

Expand Down Expand Up @@ -816,6 +827,8 @@ void DocumentManager::closeDocumentAt(int index)
tilesetDocument->disconnect(this);
}
}

updateSession();
}

/**
Expand Down Expand Up @@ -913,6 +926,8 @@ void DocumentManager::currentIndexChanged()

mBrokenLinksModel->setDocument(document);

updateSession();

emit currentDocumentChanged(document);
}

Expand Down Expand Up @@ -1134,6 +1149,20 @@ void DocumentManager::removeFromTilesetDocument(const SharedTileset &tileset, Ma
}
}

void DocumentManager::updateSession() const
{
QStringList fileList;
for (const auto &document : mDocuments)
fileList.append(document->fileName());

auto doc = currentDocument();
auto prefs = Preferences::instance();

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

MapDocument *DocumentManager::openMapFile(const QString &path)
{
openFile(path);
Expand Down
3 changes: 3 additions & 0 deletions src/tiled/documentmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class DocumentManager : public QObject
int findDocument(Document *document) const;

void switchToDocument(int index);
bool switchToDocument(const QString &fileName);
bool switchToDocument(Document *document);
void switchToDocument(MapDocument *mapDocument, QPointF viewCenter, qreal scale);

Expand Down Expand Up @@ -201,6 +202,8 @@ public slots:
void addToTilesetDocument(const SharedTileset &tileset, MapDocument *mapDocument);
void removeFromTilesetDocument(const SharedTileset &tileset, MapDocument *mapDocument);

void updateSession() const;

MapDocument *openMapFile(const QString &path);
TilesetDocument *openTilesetFile(const QString &path);

Expand Down
7 changes: 3 additions & 4 deletions src/tiled/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,11 @@ int main(int argc, char *argv[])
PluginManager::instance()->loadPlugins();
ScriptManager::instance().initialize();

if (!commandLine.filesToOpen().isEmpty()) {
w.initializeSession();

if (!commandLine.filesToOpen().isEmpty())
for (const QString &fileName : commandLine.filesToOpen())
w.openFile(fileName);
} else if (Preferences::instance()->openLastFilesOnStartup()) {
w.openLastFiles();
}

return a.exec();
}
Loading

0 comments on commit ee65109

Please # to comment.