added config save
This commit is contained in:
@@ -20,8 +20,42 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
, ui(new Ui::MainWindow)
|
||||
{
|
||||
loadDeeplTranslationPossibilities();
|
||||
noConfigChange = true;
|
||||
ui->setupUi(this);
|
||||
loadDeeplTranslationPossibilities();
|
||||
QString configPath = QDir::homePath() + "/.renpytranslate.conf";
|
||||
QFile configFile(configPath);
|
||||
if (configFile.exists()) {
|
||||
if (configFile.open(QIODevice::ReadOnly)) {
|
||||
QByteArray data = configFile.readAll();
|
||||
QJsonDocument doc = QJsonDocument::fromJson(data);
|
||||
if (!doc.isNull()) {
|
||||
configuration = doc.object();
|
||||
} else {
|
||||
}
|
||||
configFile.close();
|
||||
}
|
||||
}
|
||||
if (configuration.contains("deepl-key")) {
|
||||
ui->deeplApiKey->setText(configuration["deepl-key"].toString());
|
||||
loadDeeplTranslationPossibilities();
|
||||
}
|
||||
if (configuration.contains("last-dir") && (QDir()).exists(configuration["last-dir"].toString())) {
|
||||
ui->projectDir->setText(configuration["last-dir"].toString());
|
||||
crawlProject();
|
||||
qDebug() << "project crawled";
|
||||
qDebug() << configuration["last-language"].toString();
|
||||
qDebug() << ui->languageCombo->count();
|
||||
for (int i=0; i < ui->languageCombo->count(); ++i) {
|
||||
qDebug() << ui->languageCombo->itemText(i);
|
||||
}
|
||||
qDebug() << ui->languageCombo->findText(configuration["last-language"].toString());
|
||||
if (configuration.contains("last-language") && ui->languageCombo->findText(configuration["last-language"].toString()) >= 0) {
|
||||
qDebug() << "language crawl";
|
||||
ui->languageCombo->setCurrentText(configuration["last-language"].toString());
|
||||
}
|
||||
}
|
||||
noConfigChange = false;
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
@@ -32,8 +66,12 @@ MainWindow::~MainWindow()
|
||||
|
||||
void MainWindow::on_selectProjectDirButton_clicked()
|
||||
{
|
||||
QString selectedDir = QFileDialog::getExistingDirectory(this, tr("Verzeichnis auswählen"), QDir::homePath());
|
||||
auto dir = configuration.contains("last-dir") && (QDir()).exists(configuration["last-dir"].toString())
|
||||
? configuration["last-dir"].toString()
|
||||
: QDir::homePath();
|
||||
QString selectedDir = QFileDialog::getExistingDirectory(this, tr("Verzeichnis auswählen"), dir);
|
||||
if (!selectedDir.isEmpty()) {
|
||||
setConfigValue("last-dir", selectedDir);
|
||||
ui->projectDir->setText(selectedDir);
|
||||
crawlProject();
|
||||
}
|
||||
@@ -69,6 +107,8 @@ void MainWindow::on_reloadButton_clicked()
|
||||
|
||||
void MainWindow::on_languageCombo_currentTextChanged(const QString &selectedLanguage)
|
||||
{
|
||||
qDebug() << "language selected";
|
||||
setConfigValue("last-language", selectedLanguage);
|
||||
fileContentsMap.clear();
|
||||
ui->treeWidget->clear();
|
||||
if (ui->languageCombo->currentIndex() == -1) {
|
||||
@@ -103,6 +143,7 @@ void MainWindow::populateTreeWidgetFromMap() {
|
||||
blockItem->setText(0, QString::number(block.line));
|
||||
blockItem->setText(1, block.oldText);
|
||||
blockItem->setText(2, block.newText);
|
||||
blockItem->setText(3, block.character);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +161,7 @@ QVector<MainWindow::TranslationItem> MainWindow::parseTextBlock(const QString& i
|
||||
QStringList lines = input.split("\n", Qt::SkipEmptyParts);
|
||||
int lineNumber = 0;
|
||||
QString identifier, originalText, translatedText;
|
||||
for (const QString& line : lines) {
|
||||
for (const QString& line : std::as_const(lines)) {
|
||||
if (line.contains("# game/")) {
|
||||
QRegExp regExp("# game/.+:(\\d+)");
|
||||
if (regExp.indexIn(line.trimmed()) != -1) {
|
||||
@@ -128,13 +169,19 @@ QVector<MainWindow::TranslationItem> MainWindow::parseTextBlock(const QString& i
|
||||
}
|
||||
} else if (line.startsWith(" # ") || line.startsWith(" old ")) {
|
||||
originalText = line.mid(line.indexOf("\"") + 1).trimmed();
|
||||
originalText.chop(1); // Remove trailing quotation mark
|
||||
originalText.chop(1);
|
||||
identifier = "";
|
||||
} else if ((line.startsWith(" ") && !line.startsWith(" # ")) || line.startsWith(" new ")) {
|
||||
translatedText = line.mid(4).trimmed();
|
||||
if (translatedText.startsWith("new \"")) {
|
||||
translatedText = translatedText.mid(5).trimmed();
|
||||
} else if (translatedText.startsWith("\"")) {
|
||||
translatedText = translatedText.mid(1).trimmed();
|
||||
} else {
|
||||
int firstQuoteIndex = translatedText.indexOf("\"");
|
||||
int lastQuoteIndex = translatedText.lastIndexOf("\"");
|
||||
identifier = translatedText.mid(0, firstQuoteIndex).trimmed();
|
||||
translatedText = translatedText.mid(firstQuoteIndex + 1, lastQuoteIndex - firstQuoteIndex).trimmed();
|
||||
}
|
||||
translatedText.chop(1);
|
||||
blocks.append(TranslationItem(lineNumber, identifier, originalText, translatedText));
|
||||
@@ -142,6 +189,9 @@ QVector<MainWindow::TranslationItem> MainWindow::parseTextBlock(const QString& i
|
||||
translatedText.clear();
|
||||
}
|
||||
}
|
||||
blocks.erase(std::remove_if(blocks.begin(), blocks.end(), [](const TranslationItem& item) {
|
||||
return item.oldText.trimmed().isEmpty(); // Prüft nun, ob oldText leer oder nur aus Leerzeichen besteht
|
||||
}), blocks.end());
|
||||
std::sort(blocks.begin(), blocks.end(), [](const TranslationItem& a, const TranslationItem& b) {
|
||||
return a.line < b.line;
|
||||
});
|
||||
@@ -297,16 +347,15 @@ bool MainWindow::editAndSaveFile(QTreeWidgetItem *fileItem, const QString &backu
|
||||
return false;
|
||||
}
|
||||
}
|
||||
qDebug() << "file created";
|
||||
qDebug() << fileName << " created";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MainWindow::parseAndEditFile(QFile &backupFile, QTreeWidgetItem *fileItem, QStringList &lines, bool &changed) {
|
||||
QTextStream backupStream(&backupFile);
|
||||
QString content = backupStream.readAll();
|
||||
QString dummyVariable = "Ihr Ersatztext";
|
||||
static QRegularExpression translateRegex(R"(# game\/(.+):(\d+)\ntranslate german (.*?)\n\n\s*#.*?\"(.*?)\"\s*\n\s*(.*?)\s*\"(.*?)\")");
|
||||
static QRegularExpression oldNewRegex(R"(\n\s*#\s*game\/(.+?):(\d+?)\n\s*old\s*\"[^"]+?\"\n\s*new\s*\"[^"]+?\")");
|
||||
static QRegularExpression oldNewRegex(R"(\n\s*#\s*game\/(.+?):(\d+?)\n\s*old\s*\"(.+?)\"\n\s*new\s*\"(.*?)\")");
|
||||
QRegularExpressionMatch match;
|
||||
int offset = 0;
|
||||
while ((match = translateRegex.match(content, offset)).hasMatch()) {
|
||||
@@ -356,7 +405,7 @@ void MainWindow::loadDeeplTranslationPossibilities() {
|
||||
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
|
||||
connect(manager, &QNetworkAccessManager::finished, this, &MainWindow::onDeeplTranslationPossibilitiesLoaded);
|
||||
QNetworkRequest request(QUrl("https://api-free.deepl.com/v2/glossary-language-pairs"));
|
||||
request.setRawHeader("Authorization", "DeepL-Auth-Key " + deeplAuthKey.toUtf8()); // Replace <key> with your DeepL API key
|
||||
request.setRawHeader("Authorization", "DeepL-Auth-Key " + ui->deeplApiKey->text().toUtf8());
|
||||
manager->get(request);
|
||||
}
|
||||
|
||||
@@ -440,13 +489,18 @@ void MainWindow::on_deeplTranslateFrom_currentTextChanged(const QString &sourceL
|
||||
ui->deeplTranslateTo->clear();
|
||||
auto it = translationsMap.find(sourceLanguage);
|
||||
if (it != translationsMap.end()) {
|
||||
for (const auto& targetLang : it->second) {
|
||||
for (QString& targetLang : it->second) {
|
||||
targetLang.replace("\"", "'");
|
||||
ui->deeplTranslateTo->addItem(targetLang);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_autoTranslateButton_clicked() {
|
||||
if (ui->deeplApiKey->text().isEmpty()) {
|
||||
QMessageBox::information(this, "Deepl error", "For auto translation, you need to add a deepl API key.");
|
||||
return;
|
||||
}
|
||||
QString originalText = ui->originalTextEdit->text();
|
||||
QString sourceLang = ui->deeplTranslateFrom->currentText();
|
||||
QString targetLang = ui->deeplTranslateTo->currentText();
|
||||
@@ -459,7 +513,7 @@ void MainWindow::on_autoTranslateButton_clicked() {
|
||||
QUrl url("https://api-free.deepl.com/v2/translate");
|
||||
QNetworkRequest request(url);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||
request.setRawHeader("Authorization", "DeepL-Auth-Key " + deeplAuthKey.toUtf8());
|
||||
request.setRawHeader("Authorization", "DeepL-Auth-Key " + ui->deeplApiKey->text().toUtf8());
|
||||
manager->post(request, QJsonDocument(jsonData).toJson());
|
||||
}
|
||||
|
||||
@@ -473,6 +527,7 @@ void MainWindow::translationRequestFinished(QNetworkReply *reply) {
|
||||
if (!translationsArray.isEmpty()) {
|
||||
QJsonObject translationObject = translationsArray[0].toObject();
|
||||
QString translatedText = translationObject["text"].toString();
|
||||
translatedText.replace("\"", "'");
|
||||
ui->translationEdit->setText(translatedText);
|
||||
}
|
||||
}
|
||||
@@ -534,3 +589,26 @@ void MainWindow::searchNext() {
|
||||
if (!startItem) startItem = ui->treeWidget->topLevelItem(0);
|
||||
searchInTree(startItem, searchQuery);
|
||||
}
|
||||
|
||||
void MainWindow::setConfigValue(const QString &key, const QString &value) {
|
||||
if (noConfigChange) {
|
||||
return;
|
||||
}
|
||||
configuration[key] = value;
|
||||
QString configPath = QDir::homePath() + "/.renpytranslate.conf";
|
||||
QJsonDocument doc(configuration);
|
||||
QFile configFile(configPath);
|
||||
if (configFile.open(QIODevice::WriteOnly)) {
|
||||
configFile.write(doc.toJson());
|
||||
configFile.close();
|
||||
} else {
|
||||
qWarning() << "Failed to open config file for writing:" << configPath;
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_deeplApiKey_editingFinished()
|
||||
{
|
||||
setConfigValue("deepl-key", ui->deeplApiKey->text());
|
||||
loadDeeplTranslationPossibilities();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QJsonObject>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui { class MainWindow; }
|
||||
@@ -40,6 +41,8 @@ private slots:
|
||||
void on_searchButton_clicked();
|
||||
void on_searchNextButton_clicked();
|
||||
|
||||
void on_deeplApiKey_editingFinished();
|
||||
|
||||
private:
|
||||
struct TranslationItem {
|
||||
TranslationItem(int line_, QString character_, QString oldText_, QString newText_):
|
||||
@@ -53,9 +56,10 @@ private:
|
||||
QString newText;
|
||||
};
|
||||
Ui::MainWindow *ui;
|
||||
QJsonObject configuration;
|
||||
bool noConfigChange{false};
|
||||
std::map<QString, QString> fileContentsMap;
|
||||
std::map<QString, std::vector<QString> > translationsMap;
|
||||
const QString deeplAuthKey = "5f6bc5cc-1e5d-4c69-9ef0-eb3cc2c1ece5:fx";
|
||||
QString searchQuery;
|
||||
void crawlProject();
|
||||
void populateTreeWidgetFromMap();
|
||||
@@ -72,5 +76,6 @@ private:
|
||||
void searchNext();
|
||||
void searchInTree(QTreeWidgetItem *item, const QString &query);
|
||||
QVector<TranslationItem> parseTextBlock(const QString &block);
|
||||
void setConfigValue(const QString &key, const QString &value);
|
||||
};
|
||||
#endif // MAINWINDOW_H
|
||||
|
||||
@@ -14,13 +14,13 @@
|
||||
<string>Renpy Translation Helper</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget">
|
||||
<widget class="QWidget" name="">
|
||||
<widget class="QWidget" name="layoutWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>10</x>
|
||||
<y>10</y>
|
||||
<width>1233</width>
|
||||
<height>681</height>
|
||||
<height>732</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
@@ -174,6 +174,23 @@
|
||||
<item>
|
||||
<widget class="QComboBox" name="deeplTranslateTo"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label_9">
|
||||
<property name="text">
|
||||
<string>Deepl API Key</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="deeplApiKey">
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>200</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="autoTranslateButton">
|
||||
<property name="text">
|
||||
@@ -181,6 +198,8 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_8">
|
||||
<item>
|
||||
@@ -199,8 +218,6 @@
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<item>
|
||||
|
||||
Reference in New Issue
Block a user