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
|
|
|
|
|
|
|
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
|
|
|
|
for(auto & language : Languages::getLanguageList())
|
|
|
|
{
|
2023-09-28 22:48:52 +02:00
|
|
|
ui->languageSelect->blockSignals(true);
|
2023-09-28 16:20:21 +02:00
|
|
|
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-28 22:48:52 +02:00
|
|
|
ui->languageSelect->blockSignals(false);
|
2023-09-28 16:20:21 +02:00
|
|
|
if(language.identifier == VLC->generaltexth->getPreferredLanguage())
|
|
|
|
ui->languageSelect->setCurrentIndex(ui->languageSelect->count() - 1);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
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-28 22:48:52 +02:00
|
|
|
bool hasLanguage = !mapHeader.translations[language].isNull();
|
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
|
|
|
|
translation = mapHeader.translations[VLC->generaltexth->getPreferredLanguage()];
|
|
|
|
|
2023-09-28 22:48:52 +02:00
|
|
|
fillTranslationsTable(language);
|
|
|
|
ui->translationsTable->setEnabled(true);
|
2023-09-28 16:20:21 +02:00
|
|
|
}
|
|
|
|
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);
|
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
|
|
|
}
|
|
|
|
|