added config save
This commit is contained in:
@@ -20,8 +20,42 @@ MainWindow::MainWindow(QWidget *parent)
|
|||||||
: QMainWindow(parent)
|
: QMainWindow(parent)
|
||||||
, ui(new Ui::MainWindow)
|
, ui(new Ui::MainWindow)
|
||||||
{
|
{
|
||||||
loadDeeplTranslationPossibilities();
|
noConfigChange = true;
|
||||||
ui->setupUi(this);
|
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()
|
MainWindow::~MainWindow()
|
||||||
@@ -32,8 +66,12 @@ MainWindow::~MainWindow()
|
|||||||
|
|
||||||
void MainWindow::on_selectProjectDirButton_clicked()
|
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()) {
|
if (!selectedDir.isEmpty()) {
|
||||||
|
setConfigValue("last-dir", selectedDir);
|
||||||
ui->projectDir->setText(selectedDir);
|
ui->projectDir->setText(selectedDir);
|
||||||
crawlProject();
|
crawlProject();
|
||||||
}
|
}
|
||||||
@@ -69,6 +107,8 @@ void MainWindow::on_reloadButton_clicked()
|
|||||||
|
|
||||||
void MainWindow::on_languageCombo_currentTextChanged(const QString &selectedLanguage)
|
void MainWindow::on_languageCombo_currentTextChanged(const QString &selectedLanguage)
|
||||||
{
|
{
|
||||||
|
qDebug() << "language selected";
|
||||||
|
setConfigValue("last-language", selectedLanguage);
|
||||||
fileContentsMap.clear();
|
fileContentsMap.clear();
|
||||||
ui->treeWidget->clear();
|
ui->treeWidget->clear();
|
||||||
if (ui->languageCombo->currentIndex() == -1) {
|
if (ui->languageCombo->currentIndex() == -1) {
|
||||||
@@ -103,6 +143,7 @@ void MainWindow::populateTreeWidgetFromMap() {
|
|||||||
blockItem->setText(0, QString::number(block.line));
|
blockItem->setText(0, QString::number(block.line));
|
||||||
blockItem->setText(1, block.oldText);
|
blockItem->setText(1, block.oldText);
|
||||||
blockItem->setText(2, block.newText);
|
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);
|
QStringList lines = input.split("\n", Qt::SkipEmptyParts);
|
||||||
int lineNumber = 0;
|
int lineNumber = 0;
|
||||||
QString identifier, originalText, translatedText;
|
QString identifier, originalText, translatedText;
|
||||||
for (const QString& line : lines) {
|
for (const QString& line : std::as_const(lines)) {
|
||||||
if (line.contains("# game/")) {
|
if (line.contains("# game/")) {
|
||||||
QRegExp regExp("# game/.+:(\\d+)");
|
QRegExp regExp("# game/.+:(\\d+)");
|
||||||
if (regExp.indexIn(line.trimmed()) != -1) {
|
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 ")) {
|
} else if (line.startsWith(" # ") || line.startsWith(" old ")) {
|
||||||
originalText = line.mid(line.indexOf("\"") + 1).trimmed();
|
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 ")) {
|
} else if ((line.startsWith(" ") && !line.startsWith(" # ")) || line.startsWith(" new ")) {
|
||||||
translatedText = line.mid(4).trimmed();
|
translatedText = line.mid(4).trimmed();
|
||||||
if (translatedText.startsWith("new \"")) {
|
if (translatedText.startsWith("new \"")) {
|
||||||
translatedText = translatedText.mid(5).trimmed();
|
translatedText = translatedText.mid(5).trimmed();
|
||||||
} else if (translatedText.startsWith("\"")) {
|
} else if (translatedText.startsWith("\"")) {
|
||||||
translatedText = translatedText.mid(1).trimmed();
|
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);
|
translatedText.chop(1);
|
||||||
blocks.append(TranslationItem(lineNumber, identifier, originalText, translatedText));
|
blocks.append(TranslationItem(lineNumber, identifier, originalText, translatedText));
|
||||||
@@ -142,6 +189,9 @@ QVector<MainWindow::TranslationItem> MainWindow::parseTextBlock(const QString& i
|
|||||||
translatedText.clear();
|
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) {
|
std::sort(blocks.begin(), blocks.end(), [](const TranslationItem& a, const TranslationItem& b) {
|
||||||
return a.line < b.line;
|
return a.line < b.line;
|
||||||
});
|
});
|
||||||
@@ -297,16 +347,15 @@ bool MainWindow::editAndSaveFile(QTreeWidgetItem *fileItem, const QString &backu
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
qDebug() << "file created";
|
qDebug() << fileName << " created";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MainWindow::parseAndEditFile(QFile &backupFile, QTreeWidgetItem *fileItem, QStringList &lines, bool &changed) {
|
bool MainWindow::parseAndEditFile(QFile &backupFile, QTreeWidgetItem *fileItem, QStringList &lines, bool &changed) {
|
||||||
QTextStream backupStream(&backupFile);
|
QTextStream backupStream(&backupFile);
|
||||||
QString content = backupStream.readAll();
|
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 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;
|
QRegularExpressionMatch match;
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
while ((match = translateRegex.match(content, offset)).hasMatch()) {
|
while ((match = translateRegex.match(content, offset)).hasMatch()) {
|
||||||
@@ -356,7 +405,7 @@ void MainWindow::loadDeeplTranslationPossibilities() {
|
|||||||
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
|
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
|
||||||
connect(manager, &QNetworkAccessManager::finished, this, &MainWindow::onDeeplTranslationPossibilitiesLoaded);
|
connect(manager, &QNetworkAccessManager::finished, this, &MainWindow::onDeeplTranslationPossibilitiesLoaded);
|
||||||
QNetworkRequest request(QUrl("https://api-free.deepl.com/v2/glossary-language-pairs"));
|
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);
|
manager->get(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -440,13 +489,18 @@ void MainWindow::on_deeplTranslateFrom_currentTextChanged(const QString &sourceL
|
|||||||
ui->deeplTranslateTo->clear();
|
ui->deeplTranslateTo->clear();
|
||||||
auto it = translationsMap.find(sourceLanguage);
|
auto it = translationsMap.find(sourceLanguage);
|
||||||
if (it != translationsMap.end()) {
|
if (it != translationsMap.end()) {
|
||||||
for (const auto& targetLang : it->second) {
|
for (QString& targetLang : it->second) {
|
||||||
|
targetLang.replace("\"", "'");
|
||||||
ui->deeplTranslateTo->addItem(targetLang);
|
ui->deeplTranslateTo->addItem(targetLang);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::on_autoTranslateButton_clicked() {
|
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 originalText = ui->originalTextEdit->text();
|
||||||
QString sourceLang = ui->deeplTranslateFrom->currentText();
|
QString sourceLang = ui->deeplTranslateFrom->currentText();
|
||||||
QString targetLang = ui->deeplTranslateTo->currentText();
|
QString targetLang = ui->deeplTranslateTo->currentText();
|
||||||
@@ -459,7 +513,7 @@ void MainWindow::on_autoTranslateButton_clicked() {
|
|||||||
QUrl url("https://api-free.deepl.com/v2/translate");
|
QUrl url("https://api-free.deepl.com/v2/translate");
|
||||||
QNetworkRequest request(url);
|
QNetworkRequest request(url);
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
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());
|
manager->post(request, QJsonDocument(jsonData).toJson());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -473,6 +527,7 @@ void MainWindow::translationRequestFinished(QNetworkReply *reply) {
|
|||||||
if (!translationsArray.isEmpty()) {
|
if (!translationsArray.isEmpty()) {
|
||||||
QJsonObject translationObject = translationsArray[0].toObject();
|
QJsonObject translationObject = translationsArray[0].toObject();
|
||||||
QString translatedText = translationObject["text"].toString();
|
QString translatedText = translationObject["text"].toString();
|
||||||
|
translatedText.replace("\"", "'");
|
||||||
ui->translationEdit->setText(translatedText);
|
ui->translationEdit->setText(translatedText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -534,3 +589,26 @@ void MainWindow::searchNext() {
|
|||||||
if (!startItem) startItem = ui->treeWidget->topLevelItem(0);
|
if (!startItem) startItem = ui->treeWidget->topLevelItem(0);
|
||||||
searchInTree(startItem, searchQuery);
|
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
|
#define MAINWINDOW_H
|
||||||
|
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
|
#include <QJsonObject>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
namespace Ui { class MainWindow; }
|
namespace Ui { class MainWindow; }
|
||||||
@@ -40,6 +41,8 @@ private slots:
|
|||||||
void on_searchButton_clicked();
|
void on_searchButton_clicked();
|
||||||
void on_searchNextButton_clicked();
|
void on_searchNextButton_clicked();
|
||||||
|
|
||||||
|
void on_deeplApiKey_editingFinished();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct TranslationItem {
|
struct TranslationItem {
|
||||||
TranslationItem(int line_, QString character_, QString oldText_, QString newText_):
|
TranslationItem(int line_, QString character_, QString oldText_, QString newText_):
|
||||||
@@ -53,9 +56,10 @@ private:
|
|||||||
QString newText;
|
QString newText;
|
||||||
};
|
};
|
||||||
Ui::MainWindow *ui;
|
Ui::MainWindow *ui;
|
||||||
|
QJsonObject configuration;
|
||||||
|
bool noConfigChange{false};
|
||||||
std::map<QString, QString> fileContentsMap;
|
std::map<QString, QString> fileContentsMap;
|
||||||
std::map<QString, std::vector<QString> > translationsMap;
|
std::map<QString, std::vector<QString> > translationsMap;
|
||||||
const QString deeplAuthKey = "5f6bc5cc-1e5d-4c69-9ef0-eb3cc2c1ece5:fx";
|
|
||||||
QString searchQuery;
|
QString searchQuery;
|
||||||
void crawlProject();
|
void crawlProject();
|
||||||
void populateTreeWidgetFromMap();
|
void populateTreeWidgetFromMap();
|
||||||
@@ -72,5 +76,6 @@ private:
|
|||||||
void searchNext();
|
void searchNext();
|
||||||
void searchInTree(QTreeWidgetItem *item, const QString &query);
|
void searchInTree(QTreeWidgetItem *item, const QString &query);
|
||||||
QVector<TranslationItem> parseTextBlock(const QString &block);
|
QVector<TranslationItem> parseTextBlock(const QString &block);
|
||||||
|
void setConfigValue(const QString &key, const QString &value);
|
||||||
};
|
};
|
||||||
#endif // MAINWINDOW_H
|
#endif // MAINWINDOW_H
|
||||||
|
|||||||
@@ -14,13 +14,13 @@
|
|||||||
<string>Renpy Translation Helper</string>
|
<string>Renpy Translation Helper</string>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QWidget" name="centralwidget">
|
<widget class="QWidget" name="centralwidget">
|
||||||
<widget class="QWidget" name="">
|
<widget class="QWidget" name="layoutWidget">
|
||||||
<property name="geometry">
|
<property name="geometry">
|
||||||
<rect>
|
<rect>
|
||||||
<x>10</x>
|
<x>10</x>
|
||||||
<y>10</y>
|
<y>10</y>
|
||||||
<width>1233</width>
|
<width>1233</width>
|
||||||
<height>681</height>
|
<height>732</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QVBoxLayout" name="verticalLayout">
|
<layout class="QVBoxLayout" name="verticalLayout">
|
||||||
@@ -174,6 +174,23 @@
|
|||||||
<item>
|
<item>
|
||||||
<widget class="QComboBox" name="deeplTranslateTo"/>
|
<widget class="QComboBox" name="deeplTranslateTo"/>
|
||||||
</item>
|
</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>
|
<item>
|
||||||
<widget class="QPushButton" name="autoTranslateButton">
|
<widget class="QPushButton" name="autoTranslateButton">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
@@ -181,23 +198,23 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
</layout>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_8">
|
||||||
<item>
|
<item>
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout_8">
|
<widget class="QToolButton" name="searchButton">
|
||||||
<item>
|
<property name="text">
|
||||||
<widget class="QToolButton" name="searchButton">
|
<string>Search</string>
|
||||||
<property name="text">
|
</property>
|
||||||
<string>Search</string>
|
</widget>
|
||||||
</property>
|
</item>
|
||||||
</widget>
|
<item>
|
||||||
</item>
|
<widget class="QToolButton" name="searchNextButton">
|
||||||
<item>
|
<property name="text">
|
||||||
<widget class="QToolButton" name="searchNextButton">
|
<string>Next</string>
|
||||||
<property name="text">
|
</property>
|
||||||
<string>Next</string>
|
</widget>
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
</layout>
|
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
|
|||||||
Reference in New Issue
Block a user