Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/libtiled/templatemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ ObjectTemplate *TemplateManager::loadObjectTemplate(const QString &fileName, QSt
return objectTemplate;
}

void TemplateManager::deleteTemplate(const QString &fileName)
{
mWatcher->removePath(fileName);
}

void TemplateManager::pathsChanged(const QStringList &paths)
{
for (const QString &fileName : paths) {
Expand Down
1 change: 1 addition & 0 deletions src/libtiled/templatemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class TILEDSHARED_EXPORT TemplateManager : public QObject
ObjectTemplate *findObjectTemplate(const QString &fileName);
ObjectTemplate *loadObjectTemplate(const QString &fileName,
QString *error = nullptr);
void deleteTemplate(const QString &fileName);

signals:
/**
Expand Down
38 changes: 38 additions & 0 deletions src/tiled/projectdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
#include "actionmanager.h"
#include "addremovetileset.h"
#include "documentmanager.h"
#include "layer.h"
#include "mapdocument.h"
#include "mapdocumentactionhandler.h"
#include "mapeditor.h"
#include "mapobject.h"
#include "objectgroup.h"
#include "objecttemplate.h"
#include "preferences.h"
#include "projectmanager.h"
Expand All @@ -36,9 +40,11 @@
#include "utils.h"

#include <QBoxLayout>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QMenu>
#include <QMessageBox>
#include <QMouseEvent>
#include <QScrollBar>
#include <QSet>
Expand Down Expand Up @@ -80,6 +86,7 @@ class ProjectView final : public QTreeView
void onRowsInserted(const QModelIndex &parent);

void restoreExpanded(const QModelIndex &parent);
void deleteTemplate(const QString &templatePath);

ProjectModel *mProjectModel;
ProjectProxyModel *mProxyModel;
Expand Down Expand Up @@ -276,6 +283,9 @@ void ProjectView::contextMenuEvent(QContextMenuEvent *event)
menu.addAction(tr("Select Template Instances"), [=] {
mapDocumentActionHandler->selectAllInstances(objectTemplate);
})->setEnabled(mapDocument != nullptr);
menu.addAction(tr("Delete"), [=] {
deleteTemplate(path);
});
}
// Add tileset-specific actions
else if (auto tileset = TilesetManager::instance()->loadTileset(path)) {
Expand Down Expand Up @@ -345,6 +355,34 @@ void ProjectView::restoreExpanded(const QModelIndex &parent)
}
}

static bool isTemplateInUse(const ObjectTemplate *objectTemplate)
{
for (const auto &doc : DocumentManager::instance()->documents()) {
if (auto mapDoc = qobject_cast<MapDocument*>(doc.data())) {
for (Layer *layer : mapDoc->map()->objectGroups()) {
for (MapObject *obj : static_cast<ObjectGroup*>(layer)->objects()) {
if (obj->objectTemplate() == objectTemplate)
return true;
}
}
}
}
return false;
}

void ProjectView::deleteTemplate(const QString &templatePath)
{
auto objectTemplate = TemplateManager::instance()->loadObjectTemplate(templatePath);

if (isTemplateInUse(objectTemplate)) {
if (QMessageBox::warning(window(), tr("Delete Template"), tr("This template is in use. Delete anyway?"),
QMessageBox::Yes | QMessageBox::Cancel) != QMessageBox::Yes)
return;
}
QFile::remove(templatePath);
TemplateManager::instance()->deleteTemplate(templatePath);
}

} // namespace Tiled

#include "projectdock.moc"
Expand Down
Loading