Skip to content

Commit

Permalink
Removed most world-specific signals
Browse files Browse the repository at this point in the history
Removed worldLoaded, worldReloaded, worldUnloaded and worldSaved.

Made sure the following signals are also emitted for worlds:

* assetOpened (replaces worldLoaded)
* assetAboutToBeSaved
* assetSaved (replaces worldSaved)

Moved World.save() up to Asset.save(). Its behavior changed slightly, in
that errors are now reported in a modal dialog, due to reusing the
existing saving code.

There is no replacement for worldReloaded and worldUnloaded, but
hopefully worldsChanged suffices for most use-cases.

Fixed tiled.worldsChanged to only fire when the editor is loaded, not on
the CLI.
  • Loading branch information
bjorn committed Jan 19, 2024
1 parent d42010c commit cdebf2e
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 77 deletions.
52 changes: 17 additions & 35 deletions docs/scripting-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ declare class WorldPattern {
*
* @since 1.10.3
*/
declare class World extends TiledObject {
declare class World extends Asset {
/**
* The maps that are explicitly added to this world. It does not include
* those maps which match due to patterns defined on the world.
Expand Down Expand Up @@ -1274,11 +1274,6 @@ declare class World extends TiledObject {
* @param map The TileMap instance to remove from this world.
*/
removeMap(map: TileMap): void;

/**
* Save this world to disk. Returns true if the world was saved successfully.
*/
save(): boolean;
}

/**
Expand Down Expand Up @@ -1572,6 +1567,21 @@ declare class Asset extends TiledObject {
* @note The undo system is only enabled for assets loaded in the editor!
*/
redo(): void;

/**
* Save this asset to disk. Returns true if the asset was saved successfully.
*
* Errors are reported by the UI. When an editor is open for this asset, this
* editor is activated when an error is reported.
*
* Only supported with the editor running, not when running scripts on the
* CLI. Also, the asset should already have an associated file.
*
* To save assets to a specific file or in a different format, use {@link
* tiled.mapFormat} or {@link tiled.tilesetFormat}. This is currently not
* supported for worlds.
*/
save(): boolean;
}

/**
Expand Down Expand Up @@ -4565,38 +4575,10 @@ declare namespace tiled {
export function unloadAllWorlds() : void;

/**
* Signal emitted when any world is saved, loaded, unloaded, or reloaded.
* Signal emitted when any world is loaded, unloaded, reloaded or changed.
* @since 1.10.3
*/
export const worldsChanged : Signal<void>;

/**
* Signal emitted when a world is loaded.
* Provides the file name of the world that was loaded.
* @since 1.10.3
*/
export const worldLoaded : Signal<string>;

/**
* Signal emitted when a world is reloaded.
* Provides the file name of the world that was reloaded.
* @since 1.10.3
*/
export const worldReloaded : Signal<string>;

/**
* Signal emitted when a world is reloaded.
* Provides the file name of the world that was unloaded.
* @since 1.10.3
*/
export const worldUnloaded : Signal<string>;

/**
* Signal emitted when a world is reloaded.
* Provides the file name of the world that was saved.
* @since 1.10.3
*/
export const worldSaved : Signal<string>;
}

/**
Expand Down
13 changes: 9 additions & 4 deletions src/tiled/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "mapobject.h"
#include "projectmanager.h"
#include "world.h"
#include "worlddocument.h" // used to know that WorldDocument is a Document
#include "worldmanager.h"

#include <QAction>
Expand Down Expand Up @@ -157,10 +158,14 @@ void Command::execute(bool inTerminal) const
if (saveBeforeExecute) {
ActionManager::instance()->action("Save")->trigger();

if (Document *document = DocumentManager::instance()->currentDocument())
if (document->type() == Document::MapDocumentType)
if (const World *world = WorldManager::instance().worldForMap(document->fileName()))
WorldManager::instance().saveWorld(world->fileName);
if (Document *document = DocumentManager::instance()->currentDocument()) {
if (document->type() == Document::MapDocumentType) {
if (const World *world = WorldManager::instance().worldForMap(document->fileName())) {
auto worldDocument = DocumentManager::instance()->ensureWorldDocument(world->fileName);
DocumentManager::instance()->saveDocument(worldDocument);
}
}
}
}

// Start the process
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/documentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,8 @@ void DocumentManager::onWorldLoaded(const QString &worldFile)
WorldDocument *worldDocument = new WorldDocument(worldFile);
mWorldDocuments.insert(worldFile, worldDocument);
mUndoGroup->addStack(worldDocument->undoStack());

emit documentOpened(worldDocument);
}

void DocumentManager::onWorldUnloaded(const QString &worldFile)
Expand Down
11 changes: 11 additions & 0 deletions src/tiled/documentmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class DocumentManager : public QObject
FileFormat *fileFormat = nullptr,
QString *error = nullptr);

bool saveDocument(Document *document);
bool saveDocument(Document *document, const QString &fileName);
bool saveDocumentAs(Document *document);

Expand Down Expand Up @@ -256,6 +257,16 @@ inline QUndoGroup *DocumentManager::undoGroup() const
return mUndoGroup;
}

/**
* Save the given document to its existing file name.
*
* @return <code>true</code> on success, <code>false</code> on failure
*/
inline bool DocumentManager::saveDocument(Document *document)
{
return saveDocument(document, document->fileName());
}

/**
* Returns all open documents.
*/
Expand Down
18 changes: 17 additions & 1 deletion src/tiled/editableasset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "editableasset.h"

#include "document.h"
#include "documentmanager.h"
#include "scriptmanager.h"

#include <QCoreApplication>
Expand Down Expand Up @@ -74,6 +74,22 @@ bool EditableAsset::push(std::unique_ptr<QUndoCommand> command)
return true;
}

bool EditableAsset::save()
{
auto documentManager = DocumentManager::maybeInstance();
if (!documentManager) {
ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "Editor not available"));
return false;
}

if (fileName().isEmpty()) {
ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "Asset not associated with a file"));
return false;
}

return documentManager->saveDocument(document());
}

QJSValue EditableAsset::macro(const QString &text, QJSValue callback)
{
if (!callback.isCallable()) {
Expand Down
1 change: 1 addition & 0 deletions src/tiled/editableasset.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class EditableAsset : public EditableObject
bool push(QUndoCommand *command);
bool push(std::unique_ptr<QUndoCommand> command);

Q_INVOKABLE bool save();
Q_INVOKABLE QJSValue macro(const QString &text, QJSValue callback);

Document *document() const;
Expand Down
7 changes: 2 additions & 5 deletions src/tiled/editableworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,6 @@ void EditableWorld::removeMap(EditableMap *map)
removeMap(map->fileName());
}

bool EditableWorld::save()
{
return WorldManager::instance().saveWorld(world()->fileName);
}

QSharedPointer<Document> EditableWorld::createDocument()
{
// We don't currently support opening a world in its own tab, which this
Expand All @@ -160,3 +155,5 @@ QSharedPointer<Document> EditableWorld::createDocument()
}

} // namespace Tiled

#include "moc_editableworld.cpp"
1 change: 0 additions & 1 deletion src/tiled/editableworld.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class EditableWorld final : public EditableAsset
Q_INVOKABLE void addMap(EditableMap *map, int x, int y);
Q_INVOKABLE void removeMap(const QString &mapFileName);
Q_INVOKABLE void removeMap(EditableMap *map);
Q_INVOKABLE bool save();

QSharedPointer<Document> createDocument() override;
};
Expand Down
32 changes: 12 additions & 20 deletions src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
#include "tmxmapformat.h"
#include "utils.h"
#include "world.h"
#include "worlddocument.h"
#include "worldmanager.h"
#include "zoomable.h"

Expand Down Expand Up @@ -651,13 +652,12 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
mUi->menuSaveWorld->clear();

for (const World *world : WorldManager::instance().worlds()) {
if (!mDocumentManager->isWorldModified(world->fileName))
auto worldDocument = mDocumentManager->ensureWorldDocument(world->fileName);
if (!worldDocument->isModified())
continue;

mUi->menuSaveWorld->addAction(world->fileName, this, [this, fileName = world->fileName] {
QString error;
if (!WorldManager::instance().saveWorld(fileName, &error))
QMessageBox::critical(this, tr("Error Saving World"), error);
mUi->menuSaveWorld->addAction(world->fileName, this, [this, worldDocument] {
mDocumentManager->saveDocument(worldDocument);
});
}
});
Expand Down Expand Up @@ -1213,14 +1213,12 @@ void MainWindow::saveAll()
}

for (const World *world : WorldManager::instance().worlds()) {
if (!mDocumentManager->isWorldModified(world->fileName))
auto worldDocument = mDocumentManager->ensureWorldDocument(world->fileName);
if (!worldDocument->isModified())
continue;

QString error;
if (!WorldManager::instance().saveWorld(world->fileName, &error)) {
QMessageBox::critical(this, tr("Error Saving World"), error);
if (!mDocumentManager->saveDocument(worldDocument))
return;
}
}
}

Expand Down Expand Up @@ -1264,7 +1262,8 @@ bool MainWindow::confirmAllSave()

bool MainWindow::confirmSaveWorld(const QString &fileName)
{
if (!mDocumentManager->isWorldModified(fileName))
auto worldDocument = mDocumentManager->ensureWorldDocument(fileName);
if (!worldDocument->isModified())
return true;

int ret = QMessageBox::warning(
Expand All @@ -1273,15 +1272,8 @@ bool MainWindow::confirmSaveWorld(const QString &fileName)
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);

switch (ret) {
case QMessageBox::Save: {
QString error;
if (!WorldManager::instance().saveWorld(fileName, &error)) {
QMessageBox::critical(window(), tr("Error Saving World"), error);
return false;
}

return true;
}
case QMessageBox::Save:
return mDocumentManager->saveDocument(worldDocument, fileName);
case QMessageBox::Discard:
return true;
case QMessageBox::Cancel:
Expand Down
9 changes: 2 additions & 7 deletions src/tiled/scriptmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,9 @@ ScriptModule::ScriptModule(QObject *parent)
connect(documentManager, &DocumentManager::documentSaved, this, &ScriptModule::documentSaved);
connect(documentManager, &DocumentManager::documentAboutToClose, this, &ScriptModule::documentAboutToClose);
connect(documentManager, &DocumentManager::currentDocumentChanged, this, &ScriptModule::currentDocumentChanged);
}

connect(&WorldManager::instance(), &WorldManager::worldsChanged, this, &ScriptModule::worldsChanged);
connect(&WorldManager::instance(), &WorldManager::worldLoaded, this, &ScriptModule::worldLoaded);
connect(&WorldManager::instance(), &WorldManager::worldReloaded, this, &ScriptModule::worldReloaded);
connect(&WorldManager::instance(), &WorldManager::worldUnloaded, this, &ScriptModule::worldUnloaded);
connect(&WorldManager::instance(), &WorldManager::worldSaved, this, &ScriptModule::worldSaved);

connect(&WorldManager::instance(), &WorldManager::worldsChanged, this, &ScriptModule::worldsChanged);
}
}

ScriptModule::~ScriptModule()
Expand Down
4 changes: 0 additions & 4 deletions src/tiled/scriptmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ class ScriptModule : public QObject
void activeAssetChanged(Tiled::EditableAsset *asset);

void worldsChanged();
void worldLoaded(const QString &fileName);
void worldReloaded(const QString &fileName);
void worldUnloaded(const QString &fileName);
void worldSaved(const QString &fileName);

public slots:
void trigger(const QByteArray &actionName) const;
Expand Down

0 comments on commit cdebf2e

Please sign in to comment.