diff --git a/mapeditor/mainwindow.cpp b/mapeditor/mainwindow.cpp index f4048c8e3..90bad7b69 100644 --- a/mapeditor/mainwindow.cpp +++ b/mapeditor/mainwindow.cpp @@ -313,7 +313,7 @@ void MainWindow::initializeMap(bool isNew) onPlayersChanged(); } -bool MainWindow::openMap(const QString & filenameSelect) +std::unique_ptr MainWindow::openMapInternal(const QString & filenameSelect) { QFileInfo fi(filenameSelect); std::string fname = fi.fileName().toStdString(); @@ -327,26 +327,30 @@ bool MainWindow::openMap(const QString & filenameSelect) CResourceHandler::addFilesystem("local", "mapEditor", mapEditorFilesystem); if(!CResourceHandler::get("mapEditor")->existsResource(resId)) - { - QMessageBox::warning(this, tr("Failed to open map"), tr("Cannot open map from this folder")); - return false; - } + throw std::runtime_error("Cannot open map from this folder"); CMapService mapService; + if(auto header = mapService.loadMapHeader(resId)) + { + auto missingMods = CMapService::verifyMapHeaderMods(*header); + ModIncompatibility::ModListWithVersion modList; + for(const auto & m : missingMods) + modList.push_back({m.second.name, m.second.version.toString()}); + + if(!modList.empty()) + throw ModIncompatibility(modList); + + return mapService.loadMap(resId); + } + else + throw std::runtime_error("Corrupted map"); +} + +bool MainWindow::openMap(const QString & filenameSelect) +{ try { - if(auto header = mapService.loadMapHeader(resId)) - { - auto missingMods = CMapService::verifyMapHeaderMods(*header); - ModIncompatibility::ModListWithVersion modList; - for(const auto & m : missingMods) - modList.push_back({m.second.name, m.second.version.toString()}); - - if(!modList.empty()) - throw ModIncompatibility(modList); - - controller.setMap(mapService.loadMap(resId)); - } + controller.setMap(openMapInternal(filenameSelect)); } catch(const ModIncompatibility & e) { @@ -356,7 +360,7 @@ bool MainWindow::openMap(const QString & filenameSelect) } catch(const std::exception & e) { - QMessageBox::critical(this, "Failed to open map", e.what()); + QMessageBox::critical(this, "Failed to open map", tr(e.what())); return false; } @@ -423,7 +427,7 @@ void MainWindow::on_actionSave_as_triggered() if(!controller.map()) return; - auto filenameSelect = QFileDialog::getSaveFileName(this, tr("Save map"), "", tr("VCMI maps (*.vmap)")); + auto filenameSelect = QFileDialog::getSaveFileName(this, tr("Save map"), lastSavingDir, tr("VCMI maps (*.vmap)")); if(filenameSelect.isNull()) return; @@ -432,6 +436,7 @@ void MainWindow::on_actionSave_as_triggered() return; filename = filenameSelect; + lastSavingDir = filenameSelect.remove(QUrl(filenameSelect).fileName()); saveMap(); } @@ -449,16 +454,9 @@ void MainWindow::on_actionSave_triggered() return; if(filename.isNull()) - { - auto filenameSelect = QFileDialog::getSaveFileName(this, tr("Save map"), "", tr("VCMI maps (*.vmap)")); - - if(filenameSelect.isNull()) - return; - - filename = filenameSelect; - } - - saveMap(); + on_actionSave_as_triggered(); + else + saveMap(); } void MainWindow::terrainButtonClicked(TerrainId terrain) @@ -1250,3 +1248,29 @@ void MainWindow::on_actionTranslations_triggered() translationsDialog->show(); } +void MainWindow::on_actionh3m_coverter_triggered() +{ + auto mapFiles = QFileDialog::getOpenFileNames(this, tr("Select maps to covert"), + QString::fromStdString(VCMIDirs::get().userCachePath().make_preferred().string()), + tr("HoMM3 maps(*.h3m)")); + if(mapFiles.empty()) + return; + + auto saveDirectory = QFileDialog::getExistingDirectory(this, tr("Choose directory to save coverted maps"), QCoreApplication::applicationDirPath()); + if(saveDirectory.isEmpty()) + return; + + try + { + for(auto & m : mapFiles) + { + CMapService mapService; + mapService.saveMap(openMapInternal(m), (saveDirectory + QFileInfo(m).fileName()).toStdString()); + } + } + catch(const std::exception & e) + { + QMessageBox::critical(this, tr("Failed to convert the map. Abort operation"), tr(e.what())); + } +} + diff --git a/mapeditor/mainwindow.h b/mapeditor/mainwindow.h index 3954ca173..74b051d80 100644 --- a/mapeditor/mainwindow.h +++ b/mapeditor/mainwindow.h @@ -31,6 +31,8 @@ class MainWindow : public QMainWindow #ifdef ENABLE_QT_TRANSLATIONS QTranslator translator; #endif + + std::unique_ptr openMapInternal(const QString &); public: explicit MainWindow(QWidget *parent = nullptr); @@ -118,6 +120,8 @@ private slots: void on_actionExport_triggered(); void on_actionTranslations_triggered(); + + void on_actionh3m_coverter_triggered(); public slots: @@ -155,7 +159,7 @@ private: ObjectBrowserProxyModel * objectBrowser = nullptr; QGraphicsScene * scenePreview; - QString filename; + QString filename, lastSavingDir; bool unsaved = false; QStandardItemModel objectsModel; diff --git a/mapeditor/mainwindow.ui b/mapeditor/mainwindow.ui index 6f7b0da5f..6017a6a9e 100644 --- a/mapeditor/mainwindow.ui +++ b/mapeditor/mainwindow.ui @@ -63,6 +63,7 @@ + @@ -1254,6 +1255,11 @@ Ctrl+T + + + h3m coverter + +