Skip to content
Permalink
Browse files

Fixed double loading of tilesets

When an external tileset was opened as part of a map, and then opened
explicitly, it would get loaded again and get listed twice in the
Tilesets view. Now the previously created document is used instead.
  • Loading branch information...
bjorn committed Nov 1, 2016
1 parent 5f27116 commit 3abe086ea8d60354cb6a5d2a943443fdee1f0490
Showing with 28 additions and 6 deletions.
  1. +15 −0 src/tiled/documentmanager.cpp
  2. +1 −0 src/tiled/documentmanager.h
  3. +12 −6 src/tiled/mainwindow.cpp
@@ -654,6 +654,21 @@ TilesetDocument *DocumentManager::findTilesetDocument(const SharedTileset &tiles
return mTilesetToDocument.value(tileset);
}

TilesetDocument *DocumentManager::findTilesetDocument(const QString &fileName) const
{
const QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
if (canonicalFilePath.isEmpty()) // file doesn't exist
return nullptr;

for (auto tilesetDocument : mTilesetDocuments) {
QString name = tilesetDocument->fileName();
if (!name.isEmpty() && QFileInfo(name).canonicalFilePath() == canonicalFilePath)
return tilesetDocument;
}

return nullptr;
}

/**
* Searches for a document for the given tileset, creating it if it does not
* exist already.
@@ -164,6 +164,7 @@ class DocumentManager : public QObject
const QList<TilesetDocument*> &tilesetDocuments() const;

TilesetDocument *findTilesetDocument(const SharedTileset &tileset) const;
TilesetDocument *findTilesetDocument(const QString &fileName) const;
TilesetDocument *findOrCreateTilesetDocument(const SharedTileset &tileset);

/**
@@ -563,7 +563,7 @@ bool MainWindow::openFile(const QString &fileName, FileFormat *fileFormat)

if (!fileFormat) {
// Try to find a plugin that implements support for this format
auto formats = PluginManager::objects<FileFormat>();
const auto formats = PluginManager::objects<FileFormat>();
for (FileFormat *format : formats) {
if (format->supportsFile(fileName)) {
fileFormat = format;
@@ -583,11 +583,17 @@ bool MainWindow::openFile(const QString &fileName, FileFormat *fileFormat)
if (MapFormat *mapFormat = qobject_cast<MapFormat*>(fileFormat)) {
document = MapDocument::load(fileName, mapFormat, &error);
} else if (TilesetFormat *tilesetFormat = qobject_cast<TilesetFormat*>(fileFormat)) {
SharedTileset tileset = tilesetFormat->read(fileName);
if (tileset.isNull())
error = tilesetFormat->errorString();
else
document = new TilesetDocument(tileset, fileName);
// It could be, that we have already loaded this tileset while loading
// some map.
if (TilesetDocument *tilesetDocument = mDocumentManager->findTilesetDocument(fileName)) {
document = tilesetDocument;
} else {
SharedTileset tileset = tilesetFormat->read(fileName);
if (tileset.isNull())
error = tilesetFormat->errorString();
else
document = new TilesetDocument(tileset, fileName);
}
}

if (!document) {

0 comments on commit 3abe086

Please sign in to comment.
You can’t perform that action at this time.