2023-09-28 15:29:43 +02:00
|
|
|
/*
|
|
|
|
* translations.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-09-28 16:20:21 +02:00
|
|
|
#include "StdInc.h"
|
2023-09-28 15:29:43 +02:00
|
|
|
#include "translations.h"
|
|
|
|
#include "ui_translations.h"
|
2023-09-28 16:20:21 +02:00
|
|
|
#include "../../lib/Languages.h"
|
|
|
|
#include "../../lib/CGeneralTextHandler.h"
|
|
|
|
#include "../../lib/VCMI_Lib.h"
|
2023-09-28 15:29:43 +02:00
|
|
|
|
2023-09-28 23:15:36 +02:00
|
|
|
void Translations::cleanupRemovedItems(CMap & map)
|
|
|
|
{
|
|
|
|
std::set<std::string> existingObjects;
|
|
|
|
for(auto object : map.objects)
|
|
|
|
existingObjects.insert(object->instanceName);
|
|
|
|
|
|
|
|
for(auto & translations : map.translations.Struct())
|
|
|
|
{
|
|
|
|
auto updateTranslations = JsonNode(JsonNode::JsonType::DATA_STRUCT);
|
|
|
|
for(auto & s : translations.second.Struct())
|
|
|
|
{
|
|
|
|
for(auto part : QString::fromStdString(s.first).split('.'))
|
|
|
|
{
|
2023-09-29 00:24:45 +02:00
|
|
|
if(part == "map" || existingObjects.count(part.toStdString()))
|
2023-09-28 23:15:36 +02:00
|
|
|
{
|
|
|
|
updateTranslations.Struct()[s.first] = s.second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
translations.second = updateTranslations;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Translations::cleanupRemovedItems(CMap & map, const std::string & pattern)
|
|
|
|
{
|
|
|
|
for(auto & translations : map.translations.Struct())
|
|
|
|
{
|
|
|
|
auto updateTranslations = JsonNode(JsonNode::JsonType::DATA_STRUCT);
|
|
|
|
for(auto & s : translations.second.Struct())
|
|
|
|
{
|
|
|
|
if(s.first.find(pattern) == std::string::npos)
|
|
|
|
updateTranslations.Struct()[s.first] = s.second;
|
|
|
|
}
|
|
|
|
translations.second = updateTranslations;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 15:29:43 +02:00
|
|
|
Translations::Translations(CMapHeader & mh, QWidget *parent) :
|
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::Translations),
|
|
|
|
mapHeader(mh)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2023-09-28 16:20:21 +02:00
|
|
|
|
|
|
|
//fill languages list
|
2023-09-29 00:24:45 +02:00
|
|
|
std::set<int> indexFoundLang;
|
|
|
|
int foundLang = -1;
|
|
|
|
ui->languageSelect->blockSignals(true);
|
2023-09-28 16:20:21 +02:00
|
|
|
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)));
|
2023-09-29 00:24:45 +02:00
|
|
|
if(mapHeader.translations.Struct().count(language.identifier) && !mapHeader.translations[language.identifier].Struct().empty())
|
|
|
|
indexFoundLang.insert(ui->languageSelect->count() - 1);
|
2023-09-28 16:20:21 +02:00
|
|
|
if(language.identifier == VLC->generaltexth->getPreferredLanguage())
|
2023-09-29 00:24:45 +02:00
|
|
|
foundLang = ui->languageSelect->count() - 1;
|
2023-09-28 16:20:21 +02:00
|
|
|
}
|
2023-09-29 00:24:45 +02:00
|
|
|
ui->languageSelect->blockSignals(false);
|
|
|
|
|
|
|
|
if(foundLang >= 0 && !indexFoundLang.empty() && !indexFoundLang.count(foundLang))
|
|
|
|
{
|
|
|
|
foundLang = *indexFoundLang.begin();
|
|
|
|
mapPreferredLanguage = ui->languageSelect->itemData(foundLang).toString().toStdString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(foundLang >= 0)
|
|
|
|
ui->languageSelect->setCurrentIndex(foundLang);
|
|
|
|
|
|
|
|
if(mapPreferredLanguage.empty())
|
|
|
|
mapPreferredLanguage = VLC->generaltexth->getPreferredLanguage();
|
2023-09-28 15:29:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Translations::~Translations()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2023-09-28 22:48:52 +02:00
|
|
|
void Translations::fillTranslationsTable(const std::string & language)
|
|
|
|
{
|
2023-09-28 23:15:36 +02:00
|
|
|
Translations::cleanupRemovedItems(dynamic_cast<CMap&>(mapHeader));
|
2023-09-28 22:48:52 +02:00
|
|
|
auto & translation = mapHeader.translations[language];
|
|
|
|
ui->translationsTable->blockSignals(true);
|
|
|
|
ui->translationsTable->setRowCount(0);
|
|
|
|
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++, 1, wText);
|
|
|
|
}
|
|
|
|
ui->translationsTable->resizeColumnToContents(0);
|
|
|
|
ui->translationsTable->blockSignals(false);
|
|
|
|
}
|
|
|
|
|
2023-09-28 15:29:43 +02:00
|
|
|
void Translations::on_languageSelect_currentIndexChanged(int index)
|
|
|
|
{
|
2023-09-28 16:20:21 +02:00
|
|
|
auto language = ui->languageSelect->currentData().toString().toStdString();
|
2023-09-29 00:24:45 +02:00
|
|
|
bool hasLanguage = mapHeader.translations.Struct().count(language);
|
2023-09-28 16:20:21 +02:00
|
|
|
ui->supportedCheck->blockSignals(true);
|
|
|
|
ui->supportedCheck->setChecked(hasLanguage);
|
|
|
|
ui->supportedCheck->blockSignals(false);
|
|
|
|
ui->translationsTable->setEnabled(hasLanguage);
|
|
|
|
if(hasLanguage)
|
2023-09-28 22:48:52 +02:00
|
|
|
fillTranslationsTable(language);
|
|
|
|
else
|
|
|
|
ui->translationsTable->setRowCount(0);
|
2023-09-28 15:29:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Translations::on_supportedCheck_toggled(bool checked)
|
|
|
|
{
|
2023-09-28 16:20:21 +02:00
|
|
|
auto language = ui->languageSelect->currentData().toString().toStdString();
|
|
|
|
auto & translation = mapHeader.translations[language];
|
|
|
|
bool hasRecord = !translation.Struct().empty();
|
|
|
|
|
|
|
|
if(checked)
|
|
|
|
{
|
|
|
|
//copy from default language
|
2023-09-29 00:24:45 +02:00
|
|
|
translation = mapHeader.translations[mapPreferredLanguage];
|
2023-09-28 16:20:21 +02:00
|
|
|
|
2023-09-28 22:48:52 +02:00
|
|
|
fillTranslationsTable(language);
|
|
|
|
ui->translationsTable->setEnabled(true);
|
2023-09-28 16:20:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-29 00:24:45 +02:00
|
|
|
bool canRemove = language != mapPreferredLanguage;
|
2023-09-28 16:20:21 +02:00
|
|
|
if(!canRemove)
|
|
|
|
{
|
|
|
|
QMessageBox::information(this, tr("Remove translation"), tr("Default language cannot be removed"));
|
|
|
|
}
|
|
|
|
else if(hasRecord)
|
|
|
|
{
|
2023-09-29 00:24:45 +02:00
|
|
|
auto sure = QMessageBox::question(this, tr("Remove translation"), tr("All existing text records for this language will be removed. Continue?"));
|
2023-09-28 16:20:21 +02:00
|
|
|
canRemove = sure != QMessageBox::No;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!canRemove)
|
|
|
|
{
|
|
|
|
ui->supportedCheck->blockSignals(true);
|
|
|
|
ui->supportedCheck->setChecked(true);
|
|
|
|
ui->supportedCheck->blockSignals(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ui->translationsTable->blockSignals(true);
|
2023-09-28 22:48:52 +02:00
|
|
|
ui->translationsTable->setRowCount(0);
|
|
|
|
translation = JsonNode(JsonNode::JsonType::DATA_NULL);
|
2023-09-28 16:20:21 +02:00
|
|
|
ui->translationsTable->blockSignals(false);
|
|
|
|
ui->translationsTable->setEnabled(false);
|
|
|
|
}
|
2023-09-28 15:29:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-28 22:48:52 +02:00
|
|
|
void Translations::on_translationsTable_itemChanged(QTableWidgetItem * item)
|
2023-09-28 15:29:43 +02:00
|
|
|
{
|
2023-09-28 22:48:52 +02:00
|
|
|
assert(item->column() == 1);
|
|
|
|
|
|
|
|
auto language = ui->languageSelect->currentData().toString().toStdString();
|
|
|
|
auto & translation = mapHeader.translations[language];
|
|
|
|
|
|
|
|
assert(!translation.isNull());
|
|
|
|
|
|
|
|
auto textId = ui->translationsTable->item(item->row(), 0)->text().toStdString();
|
|
|
|
assert(!textId.empty());
|
|
|
|
if(textId.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
translation[textId].String() = item->text().toStdString();
|
2023-09-28 15:29:43 +02:00
|
|
|
}
|
|
|
|
|