Allow opening both map and tileset files · mapeditor/tiled@137949b (original) (raw)
`@@ -104,6 +104,8 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
`
104
104
` , mObjectTypesEditor(new ObjectTypesEditor(this))
`
105
105
` , mAutomappingManager(new AutomappingManager(this))
`
106
106
` , mDocumentManager(DocumentManager::instance())
`
``
107
`+
, mTmxMapFormat(new TmxMapFormat(this))
`
``
108
`+
, mTsxTilesetFormat(new TsxTilesetFormat(this))
`
107
109
`{
`
108
110
`mUi->setupUi(this);
`
109
111
``
`@@ -112,6 +114,9 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
`
112
114
``
113
115
`setCentralWidget(mDocumentManager->widget());
`
114
116
``
``
117
`+
PluginManager::addObject(mTmxMapFormat);
`
``
118
`+
PluginManager::addObject(mTsxTilesetFormat);
`
``
119
+
115
120
`#ifdef Q_OS_MAC
`
116
121
`MacSupport::addFullscreen(this);
`
117
122
`#endif
`
`@@ -469,6 +474,9 @@ MainWindow::~MainWindow()
`
469
474
`// delete mTileStampManager;
`
470
475
`// mTileStampManager = nullptr;
`
471
476
``
``
477
`+
PluginManager::removeObject(mTmxMapFormat);
`
``
478
`+
PluginManager::removeObject(mTsxTilesetFormat);
`
``
479
+
472
480
`DocumentManager::deleteInstance();
`
473
481
`TilesetManager::deleteInstance();
`
474
482
`Preferences::deleteInstance();
`
`@@ -549,8 +557,7 @@ void MainWindow::newMap()
`
549
557
`mDocumentManager->addDocument(mapDocument);
`
550
558
`}
`
551
559
``
552
``
`-
bool MainWindow::openFile(const QString &fileName,
`
553
``
`-
FileFormat *format)
`
``
560
`+
bool MainWindow::openFile(const QString &fileName, FileFormat *fileFormat)
`
554
561
`{
`
555
562
`if (fileName.isEmpty())
`
556
563
`return false;
`
`@@ -562,18 +569,44 @@ bool MainWindow::openFile(const QString &fileName,
`
562
569
`return true;
`
563
570
` }
`
564
571
``
565
``
`-
// todo: determine whether it's a tileset or a map
`
566
``
`-
// ... impossible since sometimes it's just XML or JSON!?
`
``
572
`+
if (!fileFormat) {
`
``
573
`+
// Try to find a plugin that implements support for this format
`
``
574
`+
auto formats = PluginManager::objects();
`
``
575
`+
for (FileFormat *format : formats) {
`
``
576
`+
if (format->supportsFile(fileName)) {
`
``
577
`+
fileFormat = format;
`
``
578
`+
break;
`
``
579
`+
}
`
``
580
`+
}
`
``
581
`+
}
`
``
582
+
``
583
`+
if (!fileFormat) {
`
``
584
`+
QMessageBox::critical(this, tr("Error Opening File"), tr("Unrecognized file format"));
`
``
585
`+
return false;
`
``
586
`+
}
`
567
587
``
568
588
` QString error;
`
569
``
`-
MapDocument *mapDocument = MapDocument::load(fileName, qobject_cast<MapFormat*>(format), &error);
`
570
``
`-
if (!mapDocument) {
`
571
``
`-
QMessageBox::critical(this, tr("Error Opening Map"), error);
`
``
589
`+
Document *document = nullptr;
`
``
590
+
``
591
`+
if (MapFormat *mapFormat = dynamic_cast<MapFormat*>(fileFormat)) {
`
``
592
`+
document = MapDocument::load(fileName, mapFormat, &error);
`
``
593
`+
} else if (TilesetFormat *tilesetFormat = dynamic_cast<TilesetFormat*>(fileFormat)) {
`
``
594
`+
SharedTileset tileset = tilesetFormat->read(fileName);
`
``
595
`+
if (tileset.isNull())
`
``
596
`+
error = tilesetFormat->errorString();
`
``
597
`+
else
`
``
598
`+
document = new TilesetDocument(tileset, fileName);
`
``
599
`+
}
`
``
600
+
``
601
`+
if (!document) {
`
``
602
`+
QMessageBox::critical(this, tr("Error Opening File"), error);
`
572
603
`return false;
`
573
604
` }
`
574
605
``
575
``
`-
mDocumentManager->addDocument(mapDocument);
`
576
``
`-
mDocumentManager->checkTilesetColumns(mapDocument);
`
``
606
`+
mDocumentManager->addDocument(document);
`
``
607
+
``
608
`+
if (MapDocument *mapDocument = qobject_cast<MapDocument*>(document))
`
``
609
`+
mDocumentManager->checkTilesetColumns(mapDocument);
`
577
610
``
578
611
`setRecentFile(fileName);
`
579
612
`return true;
`
`@@ -657,9 +690,6 @@ void MainWindow::openFile()
`
657
690
`{
`
658
691
` QString filter = tr("All Files (*)");
`
659
692
` QString selectedFilter = filter;
`
660
``
`-
filter += QLatin1String(";;");
`
661
``
`-
filter += TmxMapFormat().nameFilter();
`
662
``
`-
filter += TsxTilesetFormat().nameFilter();
`
663
693
``
664
694
` FormatHelper helper(FileFormat::Read, filter);
`
665
695
``