Skip to content

Commit

Permalink
Project: Enabled opening a project from the command line
Browse files Browse the repository at this point in the history
This causes Tiled to resume the associated session.

The session is no longer created as part of the preferences, which
allows it to be initialized after we have parsed the command line
parameters.

Issue #1665
  • Loading branch information
bjorn committed Apr 15, 2020
1 parent e2a4181 commit 8a20a18
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 213 deletions.
4 changes: 3 additions & 1 deletion src/tiled/automappingmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

using namespace Tiled;

SessionOption<bool> AutomappingManager::automappingWhileDrawing { "automapping.whileDrawing", false };

AutomappingManager::AutomappingManager(QObject *parent)
: QObject(parent)
, mMapDocument(nullptr)
Expand Down Expand Up @@ -85,7 +87,7 @@ void AutomappingManager::autoMapRegion(const QRegion &region)

void AutomappingManager::onRegionEdited(const QRegion &where, Layer *touchedLayer)
{
if (Preferences::automappingWhileDrawing)
if (automappingWhileDrawing)
autoMapInternal(where, touchedLayer);
}

Expand Down
4 changes: 4 additions & 0 deletions src/tiled/automappingmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#pragma once

#include "session.h"

#include <QObject>
#include <QRegion>
#include <QString>
Expand Down Expand Up @@ -61,6 +63,8 @@ class AutomappingManager : public QObject
void autoMap();
void autoMapRegion(const QRegion &region);

static SessionOption<bool> automappingWhileDrawing;

signals:
/**
* This signal is emitted after automapping was done and an error occurred.
Expand Down
17 changes: 7 additions & 10 deletions 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 "session.h"
#include "tabbar.h"
#include "terrain.h"
#include "tilesetdocument.h"
Expand Down Expand Up @@ -142,6 +143,9 @@ DocumentManager::DocumentManager(QObject *parent)
connect(TilesetManager::instance(), &TilesetManager::tilesetImagesChanged,
this, &DocumentManager::tilesetImagesChanged);

connect(Preferences::instance(), &Preferences::aboutToSwitchSession,
this, &DocumentManager::updateSession);

OpenFile::activated = [this] (const OpenFile &open) {
openFile(open.file);
};
Expand Down Expand Up @@ -591,8 +595,6 @@ void DocumentManager::insertDocument(int index, const DocumentPtr &document)
if (mBrokenLinksModel->hasBrokenLinks())
mBrokenLinksWidget->show();

updateSession();

emit documentOpened(documentPtr);
}

Expand Down Expand Up @@ -891,8 +893,6 @@ void DocumentManager::closeDocumentAt(int index)

if (!document->fileName().isEmpty())
Preferences::instance()->addRecentFile(document->fileName());

updateSession();
}

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

mBrokenLinksModel->setDocument(document);

updateSession();

emit currentDocumentChanged(document);
}

Expand Down Expand Up @@ -1217,11 +1215,10 @@ void DocumentManager::updateSession() const
}

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

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

MapDocument *DocumentManager::openMapFile(const QString &path)
Expand Down
52 changes: 31 additions & 21 deletions src/tiled/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#include <QJsonDocument>
#include <QtPlugin>

#include "qtcompat_p.h"

#include <memory>

#ifdef Q_OS_WIN
Expand All @@ -66,12 +68,12 @@ class CommandLineHandler : public CommandLineParser
public:
CommandLineHandler();

bool quit;
bool showedVersion;
bool disableOpenGL;
bool exportMap;
bool exportTileset;
bool newInstance;
bool quit = false;
bool showedVersion = false;
bool disableOpenGL = false;
bool exportMap = false;
bool exportTileset = false;
bool newInstance = false;
Preferences::ExportOptions exportOptions;

private:
Expand Down Expand Up @@ -185,12 +187,6 @@ inline T *findExportFormat(const QString *filter,


CommandLineHandler::CommandLineHandler()
: quit(false)
, showedVersion(false)
, disableOpenGL(false)
, exportMap(false)
, exportTileset(false)
, newInstance(false)
{
option<&CommandLineHandler::showVersion>(
QLatin1Char('v'),
Expand Down Expand Up @@ -470,18 +466,33 @@ int main(int argc, char *argv[])
return 0;
}

if (!commandLine.filesToOpen().isEmpty() && !commandLine.newInstance) {
// Convert files to absolute paths because the already running Tiled
QStringList filesToOpen;

for (const QString &fileName : commandLine.filesToOpen()) {
const QFileInfo fileInfo(fileName);
const QString filePath = QDir::cleanPath(fileInfo.absoluteFilePath());

if (fileInfo.suffix() == QLatin1String("tiled-project")) {
if (!fileInfo.exists()) {
qWarning().noquote() << QCoreApplication::translate("Command line", "Project file '%1' not found.").arg(fileName);
return 1;
}
Preferences::setStartupProject(filePath);
} else {
filesToOpen.append(filePath);
}
}

if (!filesToOpen.isEmpty() && !commandLine.newInstance) {
// Files need to be absolute paths because the already running Tiled
// instance likely does not have the same working directory.
QStringList absolutePaths;
for (const QString &fileName : commandLine.filesToOpen())
absolutePaths.append(QFileInfo(fileName).absoluteFilePath());
QJsonDocument doc(QJsonArray::fromStringList(absolutePaths));
QJsonDocument doc(QJsonArray::fromStringList(filesToOpen));
if (a.sendMessage(QLatin1String(doc.toJson())))
return 0;
}

StyleHelper::initialize();
Session::initialize();

MainWindow w;
w.show();
Expand All @@ -498,9 +509,8 @@ int main(int argc, char *argv[])

w.initializeSession();

if (!commandLine.filesToOpen().isEmpty())
for (const QString &fileName : commandLine.filesToOpen())
w.openFile(fileName);
for (const QString &fileName : qAsConst(filesToOpen))
w.openFile(fileName);

return a.exec();
}
46 changes: 22 additions & 24 deletions src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
mUi->actionHighlightCurrentLayer->setChecked(preferences->highlightCurrentLayer());
mUi->actionHighlightHoveredObject->setChecked(preferences->highlightHoveredObject());

bindToOption(mUi->actionAutoMapWhileDrawing, Preferences::automappingWhileDrawing);
bindToOption(mUi->actionAutoMapWhileDrawing, AutomappingManager::automappingWhileDrawing);

mUi->actionHighlightCurrentLayer->setIcon(highlightCurrentLayerIcon);
mUi->actionHighlightCurrentLayer->setIconVisibleInMenu(false);
Expand Down Expand Up @@ -584,7 +584,7 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
if (!WorldManager::instance().loadWorld(worldFile, &errorString))
QMessageBox::critical(this, tr("Error Loading World"), errorString);
else
Preferences::loadedWorlds = WorldManager::instance().worlds().keys();
mLoadedWorlds = WorldManager::instance().worlds().keys();
});
connect(mUi->menuUnloadWorld, &QMenu::aboutToShow, this, [this] {
mUi->menuUnloadWorld->clear();
Expand All @@ -599,7 +599,7 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
return;

WorldManager::instance().unloadWorld(fileName);
Preferences::loadedWorlds = WorldManager::instance().worlds().keys();
mLoadedWorlds = WorldManager::instance().worlds().keys();
});
}
});
Expand All @@ -620,7 +620,7 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
if (!WorldManager::instance().addEmptyWorld(worldFile, &errorString))
QMessageBox::critical(this, tr("Error Creating World"), errorString);
else
Preferences::loadedWorlds = WorldManager::instance().worlds().keys();
mLoadedWorlds = WorldManager::instance().worlds().keys();
});
connect(mUi->menuSaveWorld, &QMenu::aboutToShow, this, [this] {
mUi->menuSaveWorld->clear();
Expand Down Expand Up @@ -810,7 +810,7 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)

MainWindow::~MainWindow()
{
Preferences::instance()->saveSessionNow();
emit Preferences::instance()->aboutToSwitchSession();

mDocumentManager->closeAllDocuments();
mDocumentManager->deleteEditors();
Expand Down Expand Up @@ -914,12 +914,13 @@ void MainWindow::newMap()

void MainWindow::initializeSession()
{
const auto prefs = Preferences::instance();
const auto &session = prefs->session();
const auto &session = Session::current();

// Restore associated project if applicable
Project project;
if (!session.project.isEmpty() && project.load(session.project)) {
bool projectLoaded = !session.project.isEmpty() && project.load(session.project);

if (projectLoaded) {
mProjectDock->setProject(std::move(project));
updateWindowTitle();
}
Expand All @@ -929,7 +930,7 @@ void MainWindow::initializeSession()
// adding the project's extension path.
ScriptManager::instance().initialize();

if (prefs->restoreSessionOnStartup())
if (projectLoaded || Preferences::instance()->restoreSessionOnStartup())
restoreSession();
}

Expand Down Expand Up @@ -1304,14 +1305,12 @@ void MainWindow::saveProjectAs()
tr("An error occurred while saving the project."));
}

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

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

prefs->saveSessionNow();
prefs->setLastSession(sessionFileName);
prefs->addRecentProject(fileName);
prefs->setLastSession(session.fileName());

updateWindowTitle();
updateActions();
Expand All @@ -1329,22 +1328,21 @@ void MainWindow::closeProject()
void MainWindow::switchProject(Project project)
{
auto prefs = Preferences::instance();
prefs->saveSessionNow();
emit prefs->aboutToSwitchSession();

if (!closeAllFiles())
return;

WorldManager::instance().unloadAllWorlds();

Session session { Session::defaultFileNameForProject(project.fileName()) };
auto &session = Session::switchCurrent(Session::defaultFileNameForProject(project.fileName()));

if (!project.fileName().isEmpty()) {
session.project = project.fileName();
session.setProject(project.fileName());
prefs->addRecentProject(project.fileName());
}

mProjectDock->setProject(std::move(project));
prefs->switchSession(std::move(session));

ScriptManager::instance().refreshExtensionsPaths();

Expand All @@ -1355,7 +1353,7 @@ void MainWindow::switchProject(Project project)

void MainWindow::restoreSession()
{
const auto &session = Preferences::instance()->session();
const auto &session = Session::current();

// Copy values because the session will get changed while restoring it
const auto openFiles = session.openFiles;
Expand All @@ -1365,7 +1363,7 @@ void MainWindow::restoreSession()
openFile(file);
mDocumentManager->switchToDocument(activeFile);

WorldManager::instance().loadWorlds(Preferences::loadedWorlds);
WorldManager::instance().loadWorlds(mLoadedWorlds);

mProjectDock->setExpandedPaths(session.expandedProjectPaths);
}
Expand Down Expand Up @@ -1746,7 +1744,7 @@ void MainWindow::openRecentFile()

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

for (int i = 0; i < numRecentFiles; ++i) {
Expand Down
3 changes: 3 additions & 0 deletions src/tiled/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "preferences.h"
#include "preferencesdialog.h"
#include "project.h"
#include "session.h"

#include <QMainWindow>
#include <QPointer>
Expand Down Expand Up @@ -246,6 +247,8 @@ class MainWindow : public QMainWindow

QMap<QMainWindow*, QByteArray> mMainWindowStates;

SessionOption<QStringList> mLoadedWorlds { "loadedWorlds" };

static MainWindow *mInstance;
};

Expand Down
9 changes: 3 additions & 6 deletions src/tiled/mapeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ MapEditor::MapEditor(QObject *parent)
connect(prefs, &Preferences::languageChanged, this, &MapEditor::retranslateUi);
connect(prefs, &Preferences::showTileCollisionShapesChanged,
this, &MapEditor::showTileCollisionShapesChanged);
connect(prefs, &Preferences::aboutToSaveSession,
connect(prefs, &Preferences::aboutToSwitchSession,
this, [this] { if (mCurrentMapDocument) saveDocumentState(mCurrentMapDocument); });

connect(&WorldManager::instance(), &WorldManager::worldsChanged,
Expand Down Expand Up @@ -622,9 +622,7 @@ void MapEditor::saveDocumentState(MapDocument *mapDocument) const
fileState.insert(QLatin1String("viewCenter"), toSettingsValue(viewCenter));
fileState.insert(QLatin1String("selectedLayer"), globalIndex(mapDocument->currentLayer()));

Preferences *prefs = Preferences::instance();
prefs->session().setFileState(mapDocument->fileName(), fileState);
prefs->saveSession();
Session::current().setFileState(mapDocument->fileName(), fileState);
}

void MapEditor::restoreDocumentState(MapDocument *mapDocument) const
Expand All @@ -633,8 +631,7 @@ void MapEditor::restoreDocumentState(MapDocument *mapDocument) const
if (!mapView)
return;

Preferences *prefs = Preferences::instance();
const QVariantMap fileState = prefs->session().fileState(mapDocument->fileName());
const QVariantMap fileState = Session::current().fileState(mapDocument->fileName());
if (fileState.isEmpty())
return;

Expand Down
1 change: 1 addition & 0 deletions src/tiled/mapscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "worldmanager.h"

#include <QApplication>
#include <QFileInfo>
#include <QGraphicsSceneMouseEvent>
#include <QKeyEvent>
#include <QMimeData>
Expand Down
Loading

0 comments on commit 8a20a18

Please sign in to comment.