Refactor Datei-Speicherlogik und füge Fehlerbehandlung hinzu

This commit is contained in:
Torsten Schulz (local)
2026-09-16 10:32:19 +02:00
parent 76fbbf0ae2
commit 701720f029
2 changed files with 89 additions and 15 deletions

View File

@@ -20,11 +20,13 @@
#include <QTimer> #include <QTimer>
#include <QDialog> #include <QDialog>
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QFrame>
#include <QLabel> #include <QLabel>
#include <QMenu> #include <QMenu>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <QScrollArea> #include <QScrollArea>
#include <QSet> #include <QSet>
#include <QScrollBar>
#include <QVBoxLayout> #include <QVBoxLayout>
namespace { namespace {
@@ -407,26 +409,36 @@ void MainWindow::on_saveButton_clicked()
} }
void MainWindow::saveFiles(QStringList &failedFiles) { void MainWindow::saveFiles(QStringList &failedFiles) {
auto projectDir = ui->projectDir->text();
for (int i = 0; i < ui->treeWidget->topLevelItemCount(); ++i) { for (int i = 0; i < ui->treeWidget->topLevelItemCount(); ++i) {
QTreeWidgetItem *fileItem = ui->treeWidget->topLevelItem(i); QTreeWidgetItem *fileItem = ui->treeWidget->topLevelItem(i);
if (fileItem->data(0, Qt::UserRole).toBool()) { if (fileItem->data(0, Qt::UserRole).toBool()) {
QString fileName = fileItem->text(0); QString errorMessage;
QString filePath = QString("%1/game/tl/%2/%3").arg(projectDir, ui->languageCombo->currentText(), fileName); if (!saveFile(fileItem, errorMessage)) {
QString backupFileName = QString("%1/game/tl/%2/%3.bak.%4") failedFiles << errorMessage;
.arg(projectDir, ui->languageCombo->currentText(), fileName, QDateTime::currentDateTime().toString("yyyyMMddHHmmss"));
if (!createBackup(filePath, backupFileName)) {
failedFiles << filePath + ": Failed to create backup";
continue;
} }
if (!editAndSaveFile(fileItem, backupFileName, filePath)) {
failedFiles << filePath + ": Failed to edit and save file";
}
fileItem->setData(0, Qt::UserRole, false);
} }
} }
} }
bool MainWindow::saveFile(QTreeWidgetItem *fileItem, QString &errorMessage)
{
const QString projectDir = ui->projectDir->text();
const QString fileName = fileItem->text(0);
const QString filePath = QString("%1/game/tl/%2/%3").arg(projectDir, ui->languageCombo->currentText(), fileName);
const QString backupFileName = QString("%1/game/tl/%2/%3.bak.%4")
.arg(projectDir, ui->languageCombo->currentText(), fileName, QDateTime::currentDateTime().toString("yyyyMMddHHmmss"));
if (!createBackup(filePath, backupFileName)) {
errorMessage = filePath + ": Failed to create backup";
return false;
}
if (!editAndSaveFile(fileItem, backupFileName, filePath)) {
errorMessage = filePath + ": Failed to edit and save file";
return false;
}
fileItem->setData(0, Qt::UserRole, false);
return true;
}
bool MainWindow::createBackup(const QString &filePath, const QString &backupFileName) { bool MainWindow::createBackup(const QString &filePath, const QString &backupFileName) {
if (!QFile::rename(filePath, backupFileName)) { if (!QFile::rename(filePath, backupFileName)) {
qDebug() << "Failed to create backup file: " << backupFileName; qDebug() << "Failed to create backup file: " << backupFileName;
@@ -798,18 +810,41 @@ void MainWindow::showBulkTranslationDialog()
QWidget *content = new QWidget(scrollArea); QWidget *content = new QWidget(scrollArea);
QVBoxLayout *contentLayout = new QVBoxLayout(content); QVBoxLayout *contentLayout = new QVBoxLayout(content);
QVector<OriginalWordReplacementEdit *> edits; QVector<OriginalWordReplacementEdit *> edits;
QVector<QPair<QWidget *, QTreeWidgetItem *>> fileSections;
edits.reserve(bulkTranslations.size()); edits.reserve(bulkTranslations.size());
QTreeWidgetItem *currentFile = nullptr;
QVBoxLayout *fileLayout = nullptr;
for (const BulkTranslationItem &item : std::as_const(bulkTranslations)) { for (const BulkTranslationItem &item : std::as_const(bulkTranslations)) {
QTreeWidgetItem *fileItem = item.item->parent();
if (fileItem != currentFile) {
if (currentFile) {
auto *separator = new QFrame(content);
separator->setFrameShape(QFrame::HLine);
separator->setFrameShadow(QFrame::Sunken);
contentLayout->addWidget(separator);
}
currentFile = fileItem;
auto *fileSection = new QWidget(content);
fileLayout = new QVBoxLayout(fileSection);
fileLayout->setContentsMargins(6, 6, 6, 6);
fileLayout->setSpacing(8);
auto *fileLabel = new QLabel(tr("Datei: %1").arg(fileItem->text(0)), fileSection);
fileLabel->setStyleSheet(QStringLiteral("font-size: 15px; font-weight: 700; color: palette(highlight);"));
fileLayout->addWidget(fileLabel);
contentLayout->addWidget(fileSection);
fileSections.append(qMakePair(fileSection, fileItem));
}
QLabel *originalLabel = new QLabel(item.originalText, content); QLabel *originalLabel = new QLabel(item.originalText, content);
originalLabel->setWordWrap(true); originalLabel->setWordWrap(true);
originalLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); originalLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
originalLabel->setStyleSheet(QStringLiteral("font-weight: 600; padding-top: 10px;")); originalLabel->setStyleSheet(QStringLiteral("font-weight: 600; padding-top: 6px;"));
contentLayout->addWidget(originalLabel); fileLayout->addWidget(originalLabel);
auto *translationEdit = new OriginalWordReplacementEdit(item.originalText, content); auto *translationEdit = new OriginalWordReplacementEdit(item.originalText, content);
translationEdit->setPlainText(item.translatedText); translationEdit->setPlainText(item.translatedText);
translationEdit->setPlaceholderText(tr("Übersetzung")); translationEdit->setPlaceholderText(tr("Übersetzung"));
contentLayout->addWidget(translationEdit); fileLayout->addWidget(translationEdit);
edits.append(translationEdit); edits.append(translationEdit);
} }
contentLayout->addStretch(); contentLayout->addStretch();
@@ -827,6 +862,44 @@ void MainWindow::showBulkTranslationDialog()
countAndShowUntranslated(); countAndShowUntranslated();
}; };
int currentFileSection = 0;
QSet<QTreeWidgetItem *> promptedFiles;
connect(scrollArea->verticalScrollBar(), &QScrollBar::valueChanged, &dialog,
[this, &fileSections, &currentFileSection, &promptedFiles, applyTranslations](int scrollPosition) {
int visibleFileSection = currentFileSection;
for (int index = 0; index < fileSections.size(); ++index) {
if (fileSections[index].first->y() <= scrollPosition + 12) {
visibleFileSection = index;
} else {
break;
}
}
if (visibleFileSection <= currentFileSection) {
return;
}
QTreeWidgetItem *completedFile = fileSections[currentFileSection].second;
currentFileSection = visibleFileSection;
if (promptedFiles.contains(completedFile)) {
return;
}
promptedFiles.insert(completedFile);
const QMessageBox::StandardButton choice = QMessageBox::question(
this,
tr("Datei speichern?"),
tr("Du bist bei einer neuen Datei angekommen. Soll die abgeschlossene Datei \"%1\" jetzt gespeichert werden?")
.arg(completedFile->text(0)),
QMessageBox::Save | QMessageBox::No,
QMessageBox::Save);
if (choice == QMessageBox::Save) {
applyTranslations();
QString errorMessage;
if (!saveFile(completedFile, errorMessage)) {
QMessageBox::critical(this, tr("Error"), errorMessage);
}
}
});
QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Apply | QDialogButtonBox::Cancel, &dialog); QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Apply | QDialogButtonBox::Cancel, &dialog);
buttons->button(QDialogButtonBox::Save)->setText(tr("Zwischenspeichern")); buttons->button(QDialogButtonBox::Save)->setText(tr("Zwischenspeichern"));
buttons->button(QDialogButtonBox::Apply)->setText(tr("Übernehmen und schließen")); buttons->button(QDialogButtonBox::Apply)->setText(tr("Übernehmen und schließen"));

View File

@@ -75,6 +75,7 @@ private:
void crawlProject(); void crawlProject();
void populateTreeWidgetFromMap(); void populateTreeWidgetFromMap();
void saveFiles(QStringList &failedFiles); void saveFiles(QStringList &failedFiles);
bool saveFile(QTreeWidgetItem *fileItem, QString &errorMessage);
bool createBackup(const QString &filePath, const QString &backupFileName); bool createBackup(const QString &filePath, const QString &backupFileName);
bool editAndSaveFile(QTreeWidgetItem *fileItem, const QString &backupFileName, const QString &fileName); bool editAndSaveFile(QTreeWidgetItem *fileItem, const QString &backupFileName, const QString &fileName);
bool saveToFile(const QString &backupFileName, const QStringList &lines); bool saveToFile(const QString &backupFileName, const QStringList &lines);