From ab85e724eb0ba7c2d93537d2d694248a9a2aea85 Mon Sep 17 00:00:00 2001 From: nordsoft Date: Thu, 28 Sep 2023 16:20:21 +0200 Subject: [PATCH] Read-only UI logic for translations --- mapeditor/mapsettings/translations.cpp | 93 +++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/mapeditor/mapsettings/translations.cpp b/mapeditor/mapsettings/translations.cpp index e8f181551..6d4f6fc43 100644 --- a/mapeditor/mapsettings/translations.cpp +++ b/mapeditor/mapsettings/translations.cpp @@ -8,8 +8,12 @@ * */ +#include "StdInc.h" #include "translations.h" #include "ui_translations.h" +#include "../../lib/Languages.h" +#include "../../lib/CGeneralTextHandler.h" +#include "../../lib/VCMI_Lib.h" Translations::Translations(CMapHeader & mh, QWidget *parent) : QDialog(parent), @@ -17,6 +21,15 @@ Translations::Translations(CMapHeader & mh, QWidget *parent) : mapHeader(mh) { ui->setupUi(this); + + //fill languages list + for(auto & language : Languages::getLanguageList()) + { + ui->languageSelect->addItem(QString("%1 (%2)").arg(QString::fromStdString(language.nameEnglish), QString::fromStdString(language.nameNative))); + ui->languageSelect->setItemData(ui->languageSelect->count() - 1, QVariant(QString::fromStdString(language.identifier))); + if(language.identifier == VLC->generaltexth->getPreferredLanguage()) + ui->languageSelect->setCurrentIndex(ui->languageSelect->count() - 1); + } } Translations::~Translations() @@ -26,13 +39,89 @@ Translations::~Translations() void Translations::on_languageSelect_currentIndexChanged(int index) { - + auto language = ui->languageSelect->currentData().toString().toStdString(); + auto & translation = mapHeader.translations[language]; + bool hasLanguage = !translation.isNull(); + ui->supportedCheck->blockSignals(true); + ui->supportedCheck->setChecked(hasLanguage); + ui->supportedCheck->blockSignals(false); + ui->translationsTable->setEnabled(hasLanguage); + if(hasLanguage) + { + ui->translationsTable->blockSignals(true); + ui->translationsTable->setRowCount(translation.Struct().size()); + int i = 0; + for(auto & s : translation.Struct()) + { + auto * wId = new QTableWidgetItem(QString::fromStdString(s.first)); + auto * wText = new QTableWidgetItem(QString::fromStdString(s.second.String())); + wId->setFlags(wId->flags() & ~Qt::ItemIsEditable); + wText->setFlags(wId->flags() | Qt::ItemIsEditable); + ui->translationsTable->setItem(i, 0, wId); + ui->translationsTable->setItem(i++, 0, wText); + } + ui->translationsTable->blockSignals(false); + } } void Translations::on_supportedCheck_toggled(bool checked) { - + auto language = ui->languageSelect->currentData().toString().toStdString(); + auto & translation = mapHeader.translations[language]; + bool hasLanguage = !translation.isNull(); + bool hasRecord = !translation.Struct().empty(); + + if(checked) + { + if(!hasLanguage) + translation = JsonNode(JsonNode::JsonType::DATA_STRUCT); + + //copy from default language + translation = mapHeader.translations[VLC->generaltexth->getPreferredLanguage()]; + hasLanguage = !translation.isNull(); + + ui->translationsTable->blockSignals(true); + ui->translationsTable->setRowCount(translation.Struct().size()); + int i = 0; + for(auto & s : translation.Struct()) + { + auto * wId = new QTableWidgetItem(QString::fromStdString(s.first)); + auto * wText = new QTableWidgetItem(QString::fromStdString(s.second.String())); + wId->setFlags(wId->flags() & ~Qt::ItemIsEditable); + wText->setFlags(wId->flags() | Qt::ItemIsEditable); + ui->translationsTable->setItem(i, 0, wId); + ui->translationsTable->setItem(i++, 0, wText); + } + ui->translationsTable->blockSignals(false); + ui->translationsTable->setEnabled(hasLanguage); + } + else + { + bool canRemove = language != VLC->generaltexth->getPreferredLanguage(); + if(!canRemove) + { + QMessageBox::information(this, tr("Remove translation"), tr("Default language cannot be removed")); + } + else if(hasRecord) + { + auto sure = QMessageBox::question(this, tr("Remove translation"), tr("This language has text records which will be removed. Continue?")); + canRemove = sure != QMessageBox::No; + } + + if(!canRemove) + { + ui->supportedCheck->blockSignals(true); + ui->supportedCheck->setChecked(true); + ui->supportedCheck->blockSignals(false); + return; + } + ui->translationsTable->blockSignals(true); + ui->translationsTable->clear(); + translation = JsonNode(); + ui->translationsTable->blockSignals(false); + ui->translationsTable->setEnabled(false); + } }