Skip to content

Commit

Permalink
Save the last export target and format in the map/tileset file
Browse files Browse the repository at this point in the history
Saving this information in the file allows the export to be repeated
without prompt even after restarting Tiled. This is supported for both
TMX and JSON formats.

At the same time, the export format is now remembered by its short name
rather than a smart pointer, which makes sure that when the right format
can still be picked after loading/unloading a plugin or changing an
export script.

The fallback to TMX or TSX formats has also been removed, since it would
only cause problems to silently export in these formats when a
previously used format can't be found.

The export target is stored with a relative path, so that the setting
may be generally useful also when the file is shared.

Issue #1610
  • Loading branch information
bjorn committed Sep 4, 2019
1 parent 6671c1d commit 86c82f8
Show file tree
Hide file tree
Showing 19 changed files with 248 additions and 89 deletions.
8 changes: 8 additions & 0 deletions src/libtiled/fileformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,12 @@ class FormatHelper
QMap<QString, Format*> mFormatByNameFilter;
};

template<typename Format>
Format *findFileFormat(const QString &shortName, FileFormat::Capabilities capabilities = FileFormat::Write)
{
return PluginManager::find<Format>([&](Format *format) {
return format->hasCapabilities(capabilities) && format->shortName() == shortName;
});
}

} // namespace Tiled
6 changes: 3 additions & 3 deletions src/libtiled/logginginterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,22 @@ std::function<void (const SelectTile &)> SelectTile::activated;


JumpToTile::JumpToTile(const Map *map, QPoint tilePos, const Layer *layer)
: mapFile(map->fileName())
: mapFile(map->fileName)
, tilePos(tilePos)
, layerId(layer ? layer->id() : -1)
{
Q_ASSERT(!mapFile.isEmpty());
}

JumpToObject::JumpToObject(const MapObject *object)
: mapFile(object->objectGroup()->map()->fileName())
: mapFile(object->objectGroup()->map()->fileName)
, objectId(object->id())
{
Q_ASSERT(!mapFile.isEmpty());
}

SelectLayer::SelectLayer(const Layer *layer)
: mapFile(layer->map()->fileName())
: mapFile(layer->map()->fileName)
, layerId(layer->id())
{
Q_ASSERT(!mapFile.isEmpty());
Expand Down
4 changes: 3 additions & 1 deletion src/libtiled/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ bool Map::isTilesetUsed(const Tileset *tileset) const
Map *Map::clone() const
{
Map *o = new Map(mOrientation, mWidth, mHeight, mTileWidth, mTileHeight, mInfinite);
o->mFileName = mFileName;
o->fileName = fileName;
o->exportFileName = exportFileName;
o->exportFormat = exportFormat;
o->mRenderOrder = mRenderOrder;
o->mHexSideLength = mHexSideLength;
o->mStaggerAxis = mStaggerAxis;
Expand Down
8 changes: 4 additions & 4 deletions src/libtiled/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class TILEDSHARED_EXPORT Map : public Object
};

public:
QString fileName;
QString exportFileName;
QString exportFormat;

/**
* The orientation of the map determines how it should be rendered. An
* Orthogonal map is using rectangular tiles that are aligned on a
Expand Down Expand Up @@ -158,9 +162,6 @@ class TILEDSHARED_EXPORT Map : public Object

~Map();

QString fileName() const { return mFileName; }
void setFileName(const QString &fileName) { mFileName = fileName; }

/**
* Returns the orientation of the map.
*/
Expand Down Expand Up @@ -483,7 +484,6 @@ class TILEDSHARED_EXPORT Map : public Object

void recomputeDrawMargins() const;

QString mFileName;
Orientation mOrientation;
RenderOrder mRenderOrder;
int mCompressionLevel;
Expand Down
4 changes: 2 additions & 2 deletions src/libtiled/mapformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ std::unique_ptr<Map> readMap(const QString &fileName, QString *error)
}

if (map)
map->setFileName(fileName);
map->fileName = fileName;

return map;
}
Expand All @@ -64,7 +64,7 @@ std::unique_ptr<Map> readMap(const QString &fileName, QString *error)
}

if (map)
map->setFileName(fileName);
map->fileName = fileName;

return map;
}
Expand Down
46 changes: 44 additions & 2 deletions src/libtiled/mapreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ class MapReaderPrivate
void readUnknownElement();

std::unique_ptr<Map> readMap();
void readMapEditorSettings(Map &map);

SharedTileset readTileset();
void readTilesetEditorSettings(Tileset &tileset);
void readTilesetTile(Tileset &tileset);
void readTilesetGrid(Tileset &tileset);
void readTilesetImage(Tileset &tileset);
Expand Down Expand Up @@ -293,7 +295,9 @@ std::unique_ptr<Map> MapReaderPrivate::readMap()
mMap->setBackgroundColor(QColor(bgColorString.toString()));

while (xml.readNextStartElement()) {
if (std::unique_ptr<Layer> layer = tryReadLayer())
if (xml.name() == QLatin1String("editorsettings"))
readMapEditorSettings(*mMap);
else if (std::unique_ptr<Layer> layer = tryReadLayer())
mMap->addLayer(std::move(layer));
else if (xml.name() == QLatin1String("properties"))
mMap->mergeProperties(readProperties());
Expand Down Expand Up @@ -334,6 +338,24 @@ std::unique_ptr<Map> MapReaderPrivate::readMap()
return std::move(mMap);
}

void MapReaderPrivate::readMapEditorSettings(Map &map)
{
Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("editorsettings"));

while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("export")) {
const QXmlStreamAttributes atts = xml.attributes();

map.exportFileName = QDir::cleanPath(mPath.filePath(atts.value(QLatin1String("target")).toString()));
map.exportFormat = atts.value(QLatin1String("format")).toString();

xml.skipCurrentElement();
} else {
readUnknownElement();
}
}
}

SharedTileset MapReaderPrivate::readTileset()
{
Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("tileset"));
Expand Down Expand Up @@ -368,7 +390,9 @@ SharedTileset MapReaderPrivate::readTileset()
tileset->setBackgroundColor(QColor(bgColorString.toString()));

while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("tile")) {
if (xml.name() == QLatin1String("editorsettings")) {
readTilesetEditorSettings(*tileset);
} else if (xml.name() == QLatin1String("tile")) {
readTilesetTile(*tileset);
} else if (xml.name() == QLatin1String("tileoffset")) {
const QXmlStreamAttributes oa = xml.attributes();
Expand Down Expand Up @@ -419,6 +443,24 @@ SharedTileset MapReaderPrivate::readTileset()
return tileset;
}

void MapReaderPrivate::readTilesetEditorSettings(Tileset &tileset)
{
Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("editorsettings"));

while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("export")) {
const QXmlStreamAttributes atts = xml.attributes();

tileset.exportFileName = QDir::cleanPath(mPath.filePath(atts.value(QLatin1String("target")).toString()));
tileset.exportFormat = atts.value(QLatin1String("format")).toString();

xml.skipCurrentElement();
} else {
readUnknownElement();
}
}
}

void MapReaderPrivate::readTilesetTile(Tileset &tileset)
{
Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("tile"));
Expand Down
47 changes: 36 additions & 11 deletions src/libtiled/maptovariantconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static QString colorToString(const QColor &color)

QVariant MapToVariantConverter::toVariant(const Map &map, const QDir &mapDir)
{
mMapDir = mapDir;
mDir = mapDir;
mGidMapper.clear();

QVariantMap mapVariant;
Expand All @@ -66,6 +66,17 @@ QVariant MapToVariantConverter::toVariant(const Map &map, const QDir &mapDir)
mapVariant[QLatin1String("nextobjectid")] = map.nextObjectId();
mapVariant[QLatin1String("compressionlevel")] = map.compressionLevel();

if (!map.exportFileName.isEmpty() || !map.exportFormat.isEmpty()) {
QVariantMap editorSettingsVariant;

QVariantMap exportVariant;
exportVariant[QLatin1String("target")] = mDir.relativeFilePath(map.exportFileName);
exportVariant[QLatin1String("format")] = map.exportFormat;
editorSettingsVariant[QLatin1String("export")] = exportVariant;

mapVariant[QLatin1String("editorsettings")] = editorSettingsVariant;
}

addProperties(mapVariant, map.properties());

if (map.orientation() == Map::Hexagonal) {
Expand Down Expand Up @@ -102,14 +113,14 @@ QVariant MapToVariantConverter::toVariant(const Map &map, const QDir &mapDir)
QVariant MapToVariantConverter::toVariant(const Tileset &tileset,
const QDir &directory)
{
mMapDir = directory;
mDir = directory;
return toVariant(tileset, 0);
}

QVariant MapToVariantConverter::toVariant(const ObjectTemplate &objectTemplate,
const QDir &directory)
{
mMapDir = directory;
mDir = directory;
QVariantMap objectTemplateVariant;

objectTemplateVariant[QLatin1String("type")] = QLatin1String("template");
Expand All @@ -136,7 +147,7 @@ QVariant MapToVariantConverter::toVariant(const Tileset &tileset,

const QString &fileName = tileset.fileName();
if (!fileName.isEmpty()) {
QString source = mMapDir.relativeFilePath(fileName);
QString source = mDir.relativeFilePath(fileName);
tilesetVariant[QLatin1String("source")] = source;

// Tileset is external, so no need to write any of the stuff below
Expand All @@ -159,6 +170,20 @@ QVariant MapToVariantConverter::toVariant(const Tileset &tileset,
tilesetVariant[QLatin1String("tilecount")] = tileset.tileCount();
tilesetVariant[QLatin1String("columns")] = tileset.columnCount();

// Write editor settings when saving external tilesets
if (firstGid == 0) {
if (!tileset.exportFileName.isEmpty() || !tileset.exportFormat.isEmpty()) {
QVariantMap editorSettingsVariant;

QVariantMap exportVariant;
exportVariant[QLatin1String("target")] = mDir.relativeFilePath(tileset.exportFileName);
exportVariant[QLatin1String("format")] = tileset.exportFormat;
editorSettingsVariant[QLatin1String("export")] = exportVariant;

tilesetVariant[QLatin1String("editorsettings")] = editorSettingsVariant;
}
}

const QColor bgColor = tileset.backgroundColor();
if (bgColor.isValid())
tilesetVariant[QLatin1String("backgroundcolor")] = colorToString(bgColor);
Expand All @@ -184,7 +209,7 @@ QVariant MapToVariantConverter::toVariant(const Tileset &tileset,
// Write the image element
const QUrl &imageSource = tileset.imageSource();
if (!imageSource.isEmpty()) {
const QString rel = toFileReference(imageSource, mMapDir);
const QString rel = toFileReference(imageSource, mDir);

tilesetVariant[QLatin1String("image")] = rel;

Expand Down Expand Up @@ -231,7 +256,7 @@ QVariant MapToVariantConverter::toVariant(const Tileset &tileset,
if (tile->probability() != 1.0)
tileVariant[QLatin1String("probability")] = tile->probability();
if (!tile->imageSource().isEmpty()) {
const QString rel = toFileReference(tile->imageSource(), mMapDir);
const QString rel = toFileReference(tile->imageSource(), mDir);
tileVariant[QLatin1String("image")] = rel;

const QSize tileSize = tile->size();
Expand Down Expand Up @@ -308,7 +333,7 @@ QVariant MapToVariantConverter::toVariant(const Properties &properties) const
Properties::const_iterator it = properties.constBegin();
Properties::const_iterator it_end = properties.constEnd();
for (; it != it_end; ++it) {
const QVariant value = toExportValue(it.value(), mMapDir);
const QVariant value = toExportValue(it.value(), mDir);
variantMap[it.key()] = value;
}

Expand Down Expand Up @@ -508,7 +533,7 @@ QVariant MapToVariantConverter::toVariant(const MapObject &object) const
addProperties(objectVariant, object.properties());

if (const ObjectTemplate *objectTemplate = object.objectTemplate()) {
QString relativeFileName = mMapDir.relativeFilePath(objectTemplate->fileName());
QString relativeFileName = mDir.relativeFilePath(objectTemplate->fileName());
objectVariant[QLatin1String("template")] = relativeFileName;
}

Expand Down Expand Up @@ -646,7 +671,7 @@ QVariant MapToVariantConverter::toVariant(const ImageLayer &imageLayer) const

addLayerAttributes(imageLayerVariant, imageLayer);

const QString rel = toFileReference(imageLayer.imageSource(), mMapDir);
const QString rel = toFileReference(imageLayer.imageSource(), mDir);
imageLayerVariant[QLatin1String("image")] = rel;

const QColor transColor = imageLayer.transparentColor();
Expand Down Expand Up @@ -737,7 +762,7 @@ void MapToVariantConverter::addProperties(QVariantMap &variantMap,
Properties::const_iterator it_end = properties.constEnd();
for (; it != it_end; ++it) {
int type = it.value().userType();
const QVariant value = toExportValue(it.value(), mMapDir);
const QVariant value = toExportValue(it.value(), mDir);

propertiesMap[it.key()] = value;
propertyTypesMap[it.key()] = typeToName(type);
Expand All @@ -752,7 +777,7 @@ void MapToVariantConverter::addProperties(QVariantMap &variantMap,
Properties::const_iterator it_end = properties.constEnd();
for (; it != it_end; ++it) {
int type = it.value().userType();
const QVariant value = toExportValue(it.value(), mMapDir);
const QVariant value = toExportValue(it.value(), mDir);

QVariantMap propertyVariantMap;
propertyVariantMap[QLatin1String("name")] = it.key();
Expand Down
2 changes: 1 addition & 1 deletion src/libtiled/maptovariantconverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class TILEDSHARED_EXPORT MapToVariantConverter
const Properties &properties) const;

int mVersion;
QDir mMapDir;
QDir mDir;
GidMapper mGidMapper;
};

Expand Down
Loading

0 comments on commit 86c82f8

Please sign in to comment.