Skip to content

Commit

Permalink
Merge pull request #318 from cbdillon/master
Browse files Browse the repository at this point in the history
Command line export for Tiled
  • Loading branch information
bjorn committed Dec 2, 2014
2 parents 661844e + 1669fad commit 091dcaa
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/tiled/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
#include "mainwindow.h"
#include "languagemanager.h"
#include "pluginmanager.h"
#include "mapdocument.h"
#include "mapreader.h"
#include "mapwriterinterface.h"
#include "preferences.h"
#include "tiledapplication.h"

#include <QDebug>
#include <QFileInfo>
#include <QtPlugin>
#include <QStyle>
#include <QStyleFactory>
Expand All @@ -53,11 +57,13 @@ class CommandLineHandler : public CommandLineParser
bool quit;
bool showedVersion;
bool disableOpenGL;
bool exportMap;

private:
void showVersion();
void justQuit();
void setDisableOpenGL();
void setExportMap();

// Convenience wrapper around registerOption
template <void (CommandLineHandler::*memberFunction)()>
Expand All @@ -79,6 +85,7 @@ CommandLineHandler::CommandLineHandler()
: quit(false)
, showedVersion(false)
, disableOpenGL(false)
, exportMap(false)
{
option<&CommandLineHandler::showVersion>(
QLatin1Char('v'),
Expand All @@ -94,6 +101,11 @@ CommandLineHandler::CommandLineHandler()
QChar(),
QLatin1String("--disable-opengl"),
QLatin1String("Disable hardware accelerated rendering"));

option<&CommandLineHandler::setExportMap>(
QChar(),
QLatin1String("--export-map"),
QLatin1String("Export the specified tmx file to target"));
}

void CommandLineHandler::showVersion()
Expand All @@ -116,6 +128,10 @@ void CommandLineHandler::setDisableOpenGL()
disableOpenGL = true;
}

void CommandLineHandler::setExportMap()
{
exportMap = true;
}

int main(int argc, char *argv[])
{
Expand Down Expand Up @@ -179,6 +195,60 @@ int main(int argc, char *argv[])

PluginManager::instance()->loadPlugins();

if (commandLine.exportMap)
{
// Get the path to the source file and target file
if (commandLine.filesToOpen().length() <= 2) {
qWarning() << QObject::tr("Export syntax is --export-map <tmx file> <target file>");
return 1;
}
int index = 0;
const QString *filter = commandLine.filesToOpen().length() > 2 ? &commandLine.filesToOpen().at(index++) : 0;
const QString &sourceFile = commandLine.filesToOpen().at(index++);
const QString &targetFile = commandLine.filesToOpen().at(index++);

// Find the map writer interface for the target file
Tiled::MapWriterInterface *chosenWriter = 0;
QString suffix = QFileInfo(targetFile).completeSuffix();
QList<Tiled::MapWriterInterface*> writers = PluginManager::instance()->interfaces<Tiled::MapWriterInterface>();
foreach (Tiled::MapWriterInterface *writer, writers) {
if (filter) {
if (writer->nameFilters().contains(*filter, Qt::CaseInsensitive)) {
chosenWriter = writer;
}
}
else if (!writer->nameFilters().filter(suffix, Qt::CaseInsensitive).isEmpty()) {
if (chosenWriter) {
qWarning() << QObject::tr("Non-unique file extension. Can't determine correct export format.");
return 1;
}
chosenWriter = writer;
}
}
if (!chosenWriter) {
qWarning() << QObject::tr("No exporter found for target file.");
return 1;
}

// Load the source file
Tiled::MapReader reader;
Tiled::Map *map = reader.readMap(sourceFile);
if (!map) {
qWarning() << QObject::tr("Failed to load source map.");
return 1;
}

// Write out the file
bool success = chosenWriter->write(map, targetFile);
delete map;

if (!success) {
qWarning() << QObject::tr("Failed to export map to target file.");
return 1;
}
return 0;
}

MainWindow w;
w.show();

Expand Down

0 comments on commit 091dcaa

Please sign in to comment.