Refactor Datei-Speicherlogik und füge Fehlerbehandlung hinzu
This commit is contained in:
103
mainwindow.cpp
103
mainwindow.cpp
@@ -20,11 +20,13 @@
|
||||
#include <QTimer>
|
||||
#include <QDialog>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QFrame>
|
||||
#include <QLabel>
|
||||
#include <QMenu>
|
||||
#include <QPlainTextEdit>
|
||||
#include <QScrollArea>
|
||||
#include <QSet>
|
||||
#include <QScrollBar>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
namespace {
|
||||
@@ -407,26 +409,36 @@ void MainWindow::on_saveButton_clicked()
|
||||
}
|
||||
|
||||
void MainWindow::saveFiles(QStringList &failedFiles) {
|
||||
auto projectDir = ui->projectDir->text();
|
||||
for (int i = 0; i < ui->treeWidget->topLevelItemCount(); ++i) {
|
||||
QTreeWidgetItem *fileItem = ui->treeWidget->topLevelItem(i);
|
||||
if (fileItem->data(0, Qt::UserRole).toBool()) {
|
||||
QString fileName = fileItem->text(0);
|
||||
QString filePath = QString("%1/game/tl/%2/%3").arg(projectDir, ui->languageCombo->currentText(), fileName);
|
||||
QString backupFileName = QString("%1/game/tl/%2/%3.bak.%4")
|
||||
.arg(projectDir, ui->languageCombo->currentText(), fileName, QDateTime::currentDateTime().toString("yyyyMMddHHmmss"));
|
||||
if (!createBackup(filePath, backupFileName)) {
|
||||
failedFiles << filePath + ": Failed to create backup";
|
||||
continue;
|
||||
QString errorMessage;
|
||||
if (!saveFile(fileItem, errorMessage)) {
|
||||
failedFiles << errorMessage;
|
||||
}
|
||||
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) {
|
||||
if (!QFile::rename(filePath, backupFileName)) {
|
||||
qDebug() << "Failed to create backup file: " << backupFileName;
|
||||
@@ -798,18 +810,41 @@ void MainWindow::showBulkTranslationDialog()
|
||||
QWidget *content = new QWidget(scrollArea);
|
||||
QVBoxLayout *contentLayout = new QVBoxLayout(content);
|
||||
QVector<OriginalWordReplacementEdit *> edits;
|
||||
QVector<QPair<QWidget *, QTreeWidgetItem *>> fileSections;
|
||||
edits.reserve(bulkTranslations.size());
|
||||
QTreeWidgetItem *currentFile = nullptr;
|
||||
QVBoxLayout *fileLayout = nullptr;
|
||||
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);
|
||||
originalLabel->setWordWrap(true);
|
||||
originalLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
|
||||
originalLabel->setStyleSheet(QStringLiteral("font-weight: 600; padding-top: 10px;"));
|
||||
contentLayout->addWidget(originalLabel);
|
||||
originalLabel->setStyleSheet(QStringLiteral("font-weight: 600; padding-top: 6px;"));
|
||||
fileLayout->addWidget(originalLabel);
|
||||
|
||||
auto *translationEdit = new OriginalWordReplacementEdit(item.originalText, content);
|
||||
translationEdit->setPlainText(item.translatedText);
|
||||
translationEdit->setPlaceholderText(tr("Übersetzung"));
|
||||
contentLayout->addWidget(translationEdit);
|
||||
fileLayout->addWidget(translationEdit);
|
||||
edits.append(translationEdit);
|
||||
}
|
||||
contentLayout->addStretch();
|
||||
@@ -827,6 +862,44 @@ void MainWindow::showBulkTranslationDialog()
|
||||
countAndShowUntranslated();
|
||||
};
|
||||
|
||||
int currentFileSection = 0;
|
||||
QSet<QTreeWidgetItem *> promptedFiles;
|
||||
connect(scrollArea->verticalScrollBar(), &QScrollBar::valueChanged, &dialog,
|
||||
[this, &fileSections, ¤tFileSection, &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);
|
||||
buttons->button(QDialogButtonBox::Save)->setText(tr("Zwischenspeichern"));
|
||||
buttons->button(QDialogButtonBox::Apply)->setText(tr("Übernehmen und schließen"));
|
||||
|
||||
@@ -75,6 +75,7 @@ private:
|
||||
void crawlProject();
|
||||
void populateTreeWidgetFromMap();
|
||||
void saveFiles(QStringList &failedFiles);
|
||||
bool saveFile(QTreeWidgetItem *fileItem, QString &errorMessage);
|
||||
bool createBackup(const QString &filePath, const QString &backupFileName);
|
||||
bool editAndSaveFile(QTreeWidgetItem *fileItem, const QString &backupFileName, const QString &fileName);
|
||||
bool saveToFile(const QString &backupFileName, const QStringList &lines);
|
||||
|
||||
Reference in New Issue
Block a user