#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include #include #include MainWindow::MainWindow(Translator *translator, QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), translator(translator) { #ifdef PORTABLE //Attention: This paths could be non writable locations! pathAppData = qApp->applicationDirPath(); //use the Applications directory pathProfiles = pathAppData + "/profiles/"; //manipulate settingsfile on portable mode settings = new QSettings(pathAppData + "/MinetestMapperGui.ini", QSettings::IniFormat); #else QSettings dummySettings(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), "dummy"); pathAppData = QFileInfo(dummySettings.fileName()).path(); pathProfiles = pathAppData + "/profiles/"; dummySettings.deleteLater(); //non portable use OS defaults settings = new QSettings(); #endif // PORTABLE ui->setupUi(this); finishUiInitialisation(); //profile = new ProfileSettings(ui, portable); //backends->setStringList(QStringList()); ui->backend->setModel(backends); readSettings(); progressBar = new QProgressBar(ui->statusBar); progressBar->setAlignment(Qt::AlignRight); progressBar->setMaximumSize(180, 19); progressBar->setMaximum(0); progressBar->hide(); ui->statusBar->addPermanentWidget(progressBar); connect(ui->actionAbout_QT, &QAction::triggered, qApp, &QApplication::aboutQt); connect(ui->actionStart_colors_txt_assistant, &QAction::triggered,this,&MainWindow::startColorsTxtAssistant); createLanguageMenu(); createProfilesMenu(); ui->figureSelect->addItems(DrawMapFigure::getFigureList()); drawMapFigureTable = new DrawMapFigureTableModel(this); ui->figures_list->setModel(drawMapFigureTable); ui->figures_list->setItemDelegateForColumn(0, new FigureDelegate(this)); drawMapFigureTableMapper = new QDataWidgetMapper(this); profile = new QSettings(QString("%1/%2.ini").arg(pathProfiles).arg(currentProfile),QSettings::IniFormat); readProfile(); drawMapFigureTableMapper->setModel(drawMapFigureTable); drawMapFigureTableMapper->addMapping(ui->figureSelect, 0, "currentIndex"); drawMapFigureTableMapper->addMapping(ui->figureUseImageCoordinates,1); drawMapFigureTableMapper->addMapping(ui->figure_point,2); drawMapFigureTableMapper->addMapping(ui->figure_geometry, 3, "geometry"); drawMapFigureTableMapper->addMapping(ui->figure_color,4); drawMapFigureTableMapper->addMapping(ui->figure_text,5); connect(ui->figures_list->selectionModel(), &QItemSelectionModel::currentRowChanged, drawMapFigureTableMapper, &QDataWidgetMapper::setCurrentModelIndex); ui->figures_list->setSelectionBehavior(QAbstractItemView::SelectRows); ui->figures_list->resizeColumnsToContents(); QCompleter *completer = new QCompleter(this); QDirModel *model =new QDirModel(completer); model->setFilter(QDir::Dirs|QDir::NoDotAndDotDot|QDir::Drives); completer->setModel(model); ui->path_World->setCompleter(completer); if (currentSettings.mapperPath == "") { minetestMapper = new MinetestMapperExe(ConfigSettings::getDefaultMapperExecutable(), this); } else { minetestMapper = new MinetestMapperExe(currentSettings.mapperPath, this); } connect(minetestMapper, &MinetestMapperExe::stateChanged, [=](const QString &state){ ui->statusBar->showMessage(state); });//ui->statusBar, &QStatusBar::showMessage); connect(minetestMapper, &MinetestMapperExe::busyStateChanged, progressBar, &QWidget::setVisible); connect(minetestMapper, &MinetestMapperExe::progressRangeChanged, progressBar, &QProgressBar::setRange); connect(minetestMapper, &MinetestMapperExe::progressChanged, progressBar, &QProgressBar::setValue); connect(minetestMapper, &MinetestMapperExe::busyStateChanged, ui->button_generate, &QWidget::setDisabled); connect(minetestMapper, &MinetestMapperExe::busyStateChanged, ui->button_cancel, &QWidget::setEnabled); connect(minetestMapper, &MinetestMapperExe::mappingFinished, this, &MainWindow::mapperFinisched); connect(minetestMapper, &MinetestMapperExe::initialisationFinished, this, &MainWindow::mapperInitialized); connect(minetestMapper, &MinetestMapperExe::mappingStandardOutput, ui->standardOutput, &QTextEdit::append); connect(minetestMapper, &MinetestMapperExe::mappingStandardError, this, &MainWindow::readError); #ifdef Q_OS_WIN taskbarButton = new QWinTaskbarButton(this); taskbarProgress = taskbarButton->progress(); connect(minetestMapper, &MinetestMapperExe::progressRangeChanged, taskbarProgress, &QWinTaskbarProgress::setRange); connect(minetestMapper, &MinetestMapperExe::progressChanged, taskbarProgress, &QWinTaskbarProgress::setValue); connect(minetestMapper, &MinetestMapperExe::busyStateChanged, taskbarProgress, &QWinTaskbarProgress::setVisible); #endif // Align the Console and the Help action to the right QWidget *spacerWidget = new QWidget(this); spacerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); spacerWidget->setVisible(true); ui->mainToolBar->insertWidget(ui->actionOutputLog, spacerWidget); minetestWorldsModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs); ui->treeView->setItemsExpandable(false); ui->treeView->setModel(minetestWorldsModel); QString worldsPath = ui->path_minetestWorlds->text(); minetestWorldsModel->setRootPath(worldsPath); auto index = minetestWorldsModel->index(worldsPath); ui->treeView->setRootIndex(index); minetestMapper->init(); } void MainWindow::showEvent( QShowEvent* event ) { QMainWindow::showEvent( event ); #ifdef Q_OS_WIN static bool first = true; if(first) { taskbarButton->setWindow(this->windowHandle()); first = false; } #endif } void MainWindow::finishUiInitialisation() { ui->geometrymode_granularity_group->setId(ui->geometrymode_pixel, static_cast(GeometryGranularity::pixel) ); ui->geometrymode_granularity_group->setId(ui->geometrymode_block, static_cast(GeometryGranularity::block) ); ui->geometrymode_size_group->setId(ui->geometrymode_auto, static_cast(GeometrySizeMode::automatic)); ui->geometrymode_size_group->setId(ui->geometrymode_fixed, static_cast(GeometrySizeMode::fixed)); ui->geometrymode_size_group->setId(ui->geometrymode_shrink, static_cast(GeometrySizeMode::shrink)); } // we create the language menu entries dynamically, dependent on the existing translations. void MainWindow::createLanguageMenu() { QActionGroup* langGroup = new QActionGroup(ui->menuLanguage); //langGroup->setExclusive(true); connect(langGroup, &QActionGroup::triggered, this, &MainWindow::slotLanguageChanged); QStringList translations = translator->getTranslations(); QLocale::Language defaultLanguage = translator->getCurrentLocale().language(); for (const QString &locale : translations) { QLocale l = QLocale(locale); QLocale::Language language = l.language(); QString lang = QLocale::languageToString(language); QIcon ico(QString("%1/%2.png").arg(translator->translationsPath).arg(locale)); QAction *action = new QAction(ico, lang, this); action->setCheckable(true); action->setData(l); ui->menuLanguage->addAction(action); langGroup->addAction(action); // set default translators and language checked if (defaultLanguage == language) { action->setChecked(true); } } } // Called every time, when a menu entry of the language menu is called void MainWindow::slotLanguageChanged(QAction* action) { if(action) { // load the language dependant on the action content qDebug() << action->data(); QLocale locale = QLocale(action->data().toLocale()); translator->setLocale(locale); } } void MainWindow::changeEvent(QEvent* event) { if (event) { switch (event->type()) { case QEvent::LanguageChange: // this event is send if a translator is loaded and installed to the Application ui->retranslateUi(this); break; case QEvent::LocaleChange: // this event is send by QWidget::setLocale translator->setLocale(QLocale::system()); break; default: // Ignore other events break; } } QMainWindow::changeEvent(event); } MainWindow::~MainWindow() { minetestMapper->cancel(); delete settings; delete profile; delete ui; } void MainWindow::on_button_generate_clicked() { if(!minetestMapper->isMinetestMapper()) { QString mapperPath = minetestMapper->getMinetestMapperExecutableFile(); QString message; if(mapperPath.isEmpty()){ message = tr("ERROR: No minetestmapper executable could not be found.\n" "Please configure one. "); } else { message = tr("ERROR: The Minetetmapper Application (%1) does not look like a Minetetestmapper\n" "Please configure a correct MinetestMapper Application. ").arg(mapperPath); } int ret = QMessageBox::critical(this, tr("Minetestmapper not found"), message + tr("(Edit->Preferences)") + "\n\n" + tr("Do you want to open Preferences now?"), QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes); if(ret == QMessageBox::Yes) on_actionPreferences_triggered(); return; } qDebug() << "Minetestmapper version: " << minetestMapper->getVersion() << minetestMapper->getTreeString(); QDir worldDir = QDir(ui->path_World->text()); if(!worldDir.exists()||worldDir.path()=="."||worldDir.path()=="/"){ QMessageBox::critical(this, tr("No input world selected"), tr("ERROR: No MinetestWorld selected.\n\n" "please select a world")); return; } QString imgName = getOutputFileName(); if(imgName.isEmpty()){ QMessageBox::critical(this, tr("No output image selected"), tr("ERROR: No output image selected.\n\n" "Please select a output image")); return; } if(QFile::exists(imgName)){ int ret = QMessageBox::question(this, tr("The image file does already exist"), tr("The File %1 does already exist.

" "Do you want to overwrite?") .arg(imgName)); if(ret != QMessageBox::Yes) return; } QDir imgPath = QFileInfo(imgName).absoluteDir(); if(!imgPath.exists()) { int ret = QMessageBox::question(this, tr("The directory does not exist"), tr("The directory %1 does not exist.

" "Should it be created?") .arg(imgPath.absolutePath())); if(ret == QMessageBox::Yes) { imgPath.mkpath(imgPath.absolutePath()); } else return; } QString appDir = qApp->applicationDirPath(); //qDebug()<path_World->text() <<"--output" << imgName <<"--colors" << colorsTxtFilePath <<"--progress" //<< "--verbose-search-colors=2" //<<"--verbose" <<"--drawalpha="+ui->drawAlpha->currentText() <<"--bgcolor" << ui->bgcolor->text() <<"--blockcolor" << ui->blockcolor->text() <<"--scalecolor" << ui->scalecolor->text() <<"--origincolor" << ui->origincolor->text() <<"--playercolor" << ui->playercolor->text() <<"--tilebordercolor" << ui->tilebordercolor->text(); if(ui->backend->currentIndex() !=0){ arguments <<"--backend" << ui->backend->currentText(); } if(ui->geometry->getFormat() != Geometry::FormatNone && ui->geometry->getGeometry() !=""){ arguments <<"--geometry" << ui->geometry->getGeometry().trimmed(); } if(ui->scalefactor->currentIndex() !=0){ arguments <<"--scalefactor" << ui->scalefactor->currentText(); } if(ui->checkBox_maxY->isChecked()){ arguments <<"--max-y" << ui->maxY->cleanText(); } if(ui->checkBox_minY->isChecked()){ arguments <<"--min-y" << ui->minY->cleanText(); } if(ui->geometrymode_pixel->isChecked()||ui->geometrymode_block->isChecked()||ui->geometrymode_shrink->isChecked()||ui->geometrymode_fixed->isChecked()){ arguments <<"--geometrymode"; if(ui->geometrymode_pixel->isChecked()){ arguments <<"pixel"; } if(ui->geometrymode_block->isChecked()){ arguments <<"block"; } if(ui->geometrymode_shrink->isChecked()){ arguments <<"shrink"; } if(ui->geometrymode_fixed->isChecked()){ arguments <<"fixed"; } } if(ui->drawScaleLeft->isChecked() && ui->drawScaleTop->isChecked()){ arguments <<"--drawscale=left,top"; } else if(ui->drawScaleLeft->isChecked()){ arguments <<"--drawscale=left"; } else if(ui->drawScaleTop->isChecked()){ arguments <<"--drawscale=top"; } if(ui->drawOrigin->isChecked()){ arguments <<"--draworigin"; } if(ui->drawPlayers->isChecked()){ arguments <<"--drawplayers"; } if(ui->drawAir->isChecked()){ arguments <<"--drawair"; } if(ui->noShading->isChecked()){ arguments <<"--noshading"; } //Heightmap Tab if(ui->generateHeightmap->isChecked()){ if(ui->heightmapColor->isChecked()){ arguments <<"--heightmap="+ui->colorHeightmap->text(); } else{ arguments <<"--heightmap"; } arguments <<"--heightmap-nodes" << ui->path_HeightmapNodes->text() <<"--heightmap-colors" << ui->path_HeightmapColors->text() <<"--heightmap-yscale" << ui->heightmapYscale->cleanText().replace(',','.') <<"--height-level-0" << ui->heightLevelNull->cleanText(); qDebug() << ui->heightLevelNull->cleanText(); if(ui->drawHeightscale->isChecked()){ arguments <<"--drawheightscale"; } } //Tiles Tab if(ui->tiles->isChecked()){ arguments <<"--tiles"; if(ui->tiles_sizeAndBorder->isChecked()){ arguments <tilesize->cleanText()+"+"+ui->tileborder->cleanText(); } else if(ui->tile_block->isChecked()){ arguments <<"block"; } else if(ui->tile_chunk->isChecked()){ arguments <<"chunk"; } if(ui->tileorigin->isChecked()){ arguments <<"--tileorigin"; } else{ arguments <<"--tilecenter"; } if(ui->tiles_coordinates->isChecked()){ arguments << ui->tiles_coordinateX->cleanText() +","+ ui->tiles_coordinateY->cleanText(); } else if(ui->tiles_world) arguments<<"world"; else { arguments<<"map"; } } // Draw figures tab arguments << drawMapFigureTable->getArguments(); ui->button_generate->setDisabled(true); if(ui->actionExpert_Mode->isChecked()){ bool ok; QString parameters = QInputDialog::getMultiLineText(this, tr("Expert Mode"),//title tr("MinetestMapper will be executed using this arguments. \n" "The arguments can be removed, modified, or new arguments can be added."),//label arguments.join("\n"),//text &ok); if (ok) arguments = parameters.split("\n"); else { ui->button_generate->setDisabled(false); return; } } minetestMapper->startMapping(arguments); } void MainWindow::on_button_cancel_clicked() { minetestMapper->cancel(); //myProcess->kill(); #ifdef Q_OS_WIN taskbarProgress->stop(); #endif } void MainWindow::readError(const QString &str) { ui->statusBar->showMessage(str); ui->standardOutput->append(""+str.toHtmlEscaped()+""); } void MainWindow::mapperInitialized() { backends->setStringList(minetestMapper->getSupportedBackends()); //ui->backend->setCurrentIndex(1); ui->backend->setCurrentIndex(profile->value("common/backend", 0).toInt()); } QString MainWindow::getOutputFileName() { QString worldPath = ui->path_World->text(); QString worldName = QDir(worldPath).dirName(); QString imgName = ui->path_OutputImage->text() .replace("", QDate::currentDate().toString(Qt::SystemLocaleShortDate)) .replace("", QDate::currentDate().toString(Qt::SystemLocaleLongDate)) .replace("", QDate::currentDate().toString(Qt::ISODate)) .replace("", worldName) .replace("