Skip to content

Commit

Permalink
Introduced ProjectManager singleton
Browse files Browse the repository at this point in the history
The ScriptManager depends on the project to get the project-specific
extensions folder. However, the project was owned by the ProjectModel,
which was owned by the ProjectView, which was owned by the ProjectDock,
which was owned by the MainWindow. And the MainWindow is not created
when scripts are used for exporting on the command-line. Now the
ScriptManager can instead talk to the ProjectManager directly.

Similarly, the ScriptModule depended on the DocumentManager, which was
since recently however only instantiated as part of the MainWindow.
Since the relevant part of the scripting API is not useful when running
export scripts on the command-line, this dependency was made optional.

This change also reduces the rather hacky direct use of the MainWindow.

Closes mapeditor#2842
  • Loading branch information
bjorn committed Jun 23, 2020
1 parent 24e92bc commit 5d2169d
Show file tree
Hide file tree
Showing 18 changed files with 222 additions and 88 deletions.
4 changes: 2 additions & 2 deletions src/tiled/automappingmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@

#include "automapperwrapper.h"
#include "logginginterface.h"
#include "mainwindow.h"
#include "map.h"
#include "mapdocument.h"
#include "preferences.h"
#include "project.h"
#include "projectmanager.h"
#include "tilelayer.h"

#include <QDir>
Expand Down Expand Up @@ -270,7 +270,7 @@ void AutomappingManager::refreshRulesFile(const QString &ruleFileOverride)
rulesFile = mapPath + QLatin1String("/rules.txt");

if (!QFileInfo::exists(rulesFile)) {
auto &project = MainWindow::instance()->project();
auto &project = ProjectManager::instance()->project();
rulesFile = project.mAutomappingRulesFile;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/tiled/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#include "commandmanager.h"
#include "documentmanager.h"
#include "logginginterface.h"
#include "mainwindow.h"
#include "mapdocument.h"
#include "mapobject.h"
#include "projectmanager.h"
#include "worlddocument.h"
#include "worldmanager.h"

Expand Down Expand Up @@ -75,7 +75,7 @@ static QString replaceVariables(const QString &string, bool quoteValues = true)
const QString fileName = document->fileName();
QFileInfo fileInfo(fileName);
const QString mapPath = fileInfo.absolutePath();
const QString projectPath = QFileInfo(MainWindow::instance()->project().fileName()).absolutePath();
const QString projectPath = QFileInfo(ProjectManager::instance()->project().fileName()).absolutePath();

finalString.replace(QLatin1String("%mapfile"), replaceString.arg(fileName));
finalString.replace(QLatin1String("%mappath"), replaceString.arg(mapPath));
Expand Down
10 changes: 5 additions & 5 deletions src/tiled/commandmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#include "commanddatamodel.h"
#include "commanddialog.h"
#include "logginginterface.h"
#include "mainwindow.h"
#include "pluginmanager.h"
#include "preferences.h"
#include "projectmanager.h"
#include "utils.h"

#include <QApplication>
Expand Down Expand Up @@ -75,7 +75,7 @@ CommandManager::CommandManager()

updateActions();

connect(MainWindow::instance(), &MainWindow::projectChanged,
connect(ProjectManager::instance(), &ProjectManager::projectChanged,
this, &CommandManager::updateActions);
}

Expand Down Expand Up @@ -108,7 +108,7 @@ const QVector<Command> &CommandManager::globalCommands() const

const QVector<Command> &CommandManager::projectCommands() const
{
auto &project = MainWindow::instance()->project();
auto &project = ProjectManager::instance()->project();
return project.mCommands;
}

Expand All @@ -133,7 +133,7 @@ void CommandManager::showDialog()
mCommands = dialog.globalCommands();
commit();

auto &project = MainWindow::instance()->project();
auto &project = ProjectManager::instance()->project();
project.mCommands = dialog.projectCommands();
project.save();

Expand Down Expand Up @@ -180,7 +180,7 @@ void CommandManager::updateActions()
addSeparator();

// Add project-specific commands
const auto &project = MainWindow::instance()->project();
const auto &project = ProjectManager::instance()->project();
for (const Command &command : project.mCommands)
addAction(command);

Expand Down
5 changes: 5 additions & 0 deletions src/tiled/documentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ DocumentManager *DocumentManager::instance()
return mInstance;
}

DocumentManager *DocumentManager::maybeInstance()
{
return mInstance;
}

DocumentManager::DocumentManager(QObject *parent)
: QObject(parent)
, mTilesetDocumentsModel(new TilesetDocumentsModel(this))
Expand Down
1 change: 1 addition & 0 deletions src/tiled/documentmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class DocumentManager : public QObject

public:
static DocumentManager *instance();
static DocumentManager *maybeInstance();

QWidget *widget() const;

Expand Down
5 changes: 3 additions & 2 deletions src/tiled/locatorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@

#include "documentmanager.h"
#include "filteredit.h"
#include "mainwindow.h"
#include "projectmanager.h"
#include "projectmodel.h"
#include "rangeset.h"
#include "utils.h"

#include <QAbstractListModel>
#include <QApplication>
#include <QDir>
#include <QKeyEvent>
#include <QPainter>
#include <QScrollBar>
Expand Down Expand Up @@ -374,7 +375,7 @@ void LocatorWidget::setFilterText(const QString &text)
const QStringList words = QDir::fromNativeSeparators(text).split(QLatin1Char(' '),
QString::SkipEmptyParts);

auto projectModel = MainWindow::instance()->projectModel();
auto projectModel = ProjectManager::instance()->projectModel();
auto matches = projectModel->findFiles(words);

std::stable_sort(matches.begin(), matches.end(), [] (const ProjectModel::Match &a, const ProjectModel::Match &b) {
Expand Down
32 changes: 10 additions & 22 deletions src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "objecttypeseditor.h"
#include "offsetmapdialog.h"
#include "projectdock.h"
#include "projectmanager.h"
#include "projectpropertiesdialog.h"
#include "resizedialog.h"
#include "scriptmanager.h"
Expand Down Expand Up @@ -971,11 +972,9 @@ void MainWindow::initializeSession()

if (projectLoaded) {
Preferences::instance()->setObjectTypesFile(project.mObjectTypesFile);
mProjectDock->setProject(std::move(project));
ProjectManager::instance()->setProject(std::move(project));
updateWindowTitle();
updateActions();

emit projectChanged();
}

// Script manager initialization is delayed until after the project has
Expand Down Expand Up @@ -1018,16 +1017,6 @@ bool MainWindow::openFile(const QString &fileName, FileFormat *fileFormat)
return true;
}

Project &MainWindow::project() const
{
return mProjectDock->project();
}

ProjectModel *MainWindow::projectModel() const
{
return mProjectDock->projectModel();
}

void MainWindow::openFileDialog()
{
SessionOption<QString> lastUsedOpenFilter { "file.lastUsedOpenFilter" };
Expand Down Expand Up @@ -1335,7 +1324,7 @@ void MainWindow::saveProjectAs()
{
auto prefs = Preferences::instance();

Project &project = mProjectDock->project();
Project &project = ProjectManager::instance()->project();
QString fileName = project.fileName();
if (fileName.isEmpty()) {
if (!project.folders().isEmpty()) {
Expand Down Expand Up @@ -1394,7 +1383,7 @@ void MainWindow::saveProjectAs()

void MainWindow::closeProject()
{
const Project &project = mProjectDock->project();
const Project &project = ProjectManager::instance()->project();
if (project.fileName().isEmpty())
return;

Expand All @@ -1419,9 +1408,7 @@ void MainWindow::switchProject(Project project)
}

prefs->setObjectTypesFile(project.mObjectTypesFile);
mProjectDock->setProject(std::move(project));

emit projectChanged();
ProjectManager::instance()->setProject(std::move(project));

restoreSession();
updateWindowTitle();
Expand All @@ -1447,7 +1434,7 @@ void MainWindow::restoreSession()

void MainWindow::projectProperties()
{
Project &project = mProjectDock->project();
Project &project = ProjectManager::instance()->project();

if (ProjectPropertiesDialog(project, this).exec() == QDialog::Accepted) {
project.save();
Expand Down Expand Up @@ -1944,7 +1931,8 @@ void MainWindow::updateActions()
const auto document = mDocumentManager->currentDocument();
const auto mapDocument = qobject_cast<const MapDocument*>(document);
const auto tilesetDocument = qobject_cast<const TilesetDocument*>(document);
const bool projectHasFolders = !mProjectDock->project().folders().isEmpty();
const auto &project = ProjectManager::instance()->project();
const bool projectHasFolders = !project.folders().isEmpty();

Editor::StandardActions standardActions;
if (editor)
Expand Down Expand Up @@ -1980,7 +1968,7 @@ void MainWindow::updateActions()

mLayerMenu->menuAction()->setVisible(mapDocument);

const bool hasProject = !mProjectDock->project().fileName().isEmpty();
const bool hasProject = !project.fileName().isEmpty();
mUi->actionCloseProject->setEnabled(hasProject);
}

Expand Down Expand Up @@ -2062,7 +2050,7 @@ void MainWindow::readSettings()

void MainWindow::updateWindowTitle()
{
QString projectName = mProjectDock->project().fileName();
QString projectName = ProjectManager::instance()->project().fileName();
if (!projectName.isEmpty()) {
projectName = QFileInfo(projectName).completeBaseName();
projectName = QString(QLatin1String(" (%1)")).arg(projectName);
Expand Down
6 changes: 0 additions & 6 deletions src/tiled/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,10 @@ class MainWindow : public QMainWindow
*/
bool openFile(const QString &fileName, FileFormat *fileFormat = nullptr);

Project &project() const;
ProjectModel *projectModel() const;

bool addRecentProjectsActions(QMenu *menu) const;

static MainWindow *instance();

signals:
void projectChanged();

protected:
bool event(QEvent *event) override;

Expand Down
28 changes: 8 additions & 20 deletions src/tiled/projectdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "mapdocumentactionhandler.h"
#include "objecttemplate.h"
#include "preferences.h"
#include "projectmanager.h"
#include "projectmodel.h"
#include "session.h"
#include "templatemanager.h"
Expand Down Expand Up @@ -112,10 +113,12 @@ ProjectDock::ProjectDock(QWidget *parent)

void ProjectDock::addFolderToProject()
{
QString folder = QFileInfo(project().fileName()).path();
Project &project = ProjectManager::instance()->project();

QString folder = QFileInfo(project.fileName()).path();
if (folder.isEmpty()) {
if (!project().folders().isEmpty())
folder = QFileInfo(project().folders().last()).path();
if (!project.folders().isEmpty())
folder = QFileInfo(project.folders().last()).path();
else
folder = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
}
Expand All @@ -130,7 +133,7 @@ void ProjectDock::addFolderToProject()
mProjectView->model()->addFolder(folder);
mProjectView->addExpandedPath(folder);

project().save();
project.save();
}

void ProjectDock::refreshProjectFolders()
Expand Down Expand Up @@ -165,21 +168,6 @@ void ProjectDock::onCurrentRowChanged(const QModelIndex &current)
emit fileSelected(filePath);
}

Project &ProjectDock::project() const
{
return mProjectView->model()->project();
}

void ProjectDock::setProject(Project project)
{
mProjectView->model()->setProject(std::move(project));
}

ProjectModel *ProjectDock::projectModel() const
{
return mProjectView->model();
}

void ProjectDock::selectFile(const QString &filePath)
{
mProjectView->selectPath(filePath);
Expand All @@ -201,7 +189,7 @@ ProjectView::ProjectView(QWidget *parent)
setDefaultDropAction(Qt::MoveAction);
setDragDropMode(QAbstractItemView::DragOnly);

auto model = new ProjectModel(this);
auto model = ProjectManager::instance()->projectModel();
setModel(model);

connect(this, &QAbstractItemView::activated,
Expand Down
5 changes: 0 additions & 5 deletions src/tiled/projectdock.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ class ProjectDock final : public QDockWidget
public:
ProjectDock(QWidget *parent = nullptr);

Project &project() const;
void setProject(Project project);

ProjectModel *projectModel() const;

void selectFile(const QString &filePath);

void addFolderToProject();
Expand Down
51 changes: 51 additions & 0 deletions src/tiled/projectmanager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* projectmanager.cpp
* Copyright 2020, Thorbjørn Lindeijer <bjorn@lindeijer.nl>
*
* This file is part of Tiled.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "projectmanager.h"

#include "projectmodel.h"

namespace Tiled {

ProjectManager *ProjectManager::ourInstance;

ProjectManager::ProjectManager(QObject *parent)
: QObject(parent)
, mProjectModel(new ProjectModel(this))
{
Q_ASSERT(!ourInstance);
ourInstance = this;
}

/**
* Replaces the current project with the given \a project.
*/
void ProjectManager::setProject(Project project)
{
mProjectModel->setProject(std::move(project));
emit projectChanged();
}

Project &ProjectManager::project()
{
return mProjectModel->project();
}

} // namespace Tiled
Loading

0 comments on commit 5d2169d

Please sign in to comment.