File indexing completed on 2024-12-22 04:40:17

0001 /*
0002     SPDX-FileCopyrightText: 2023 Mladen Milinkovic <max@smoothware.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "translatedialog.h"
0008 
0009 #include <QFile>
0010 #include <QLabel>
0011 #include <QComboBox>
0012 #include <QGroupBox>
0013 #include <QGridLayout>
0014 #include <QIcon>
0015 
0016 #include "appglobal.h"
0017 #include "application.h"
0018 #include "core/richtext/richdocument.h"
0019 #include "core/subtitleiterator.h"
0020 #include "scconfig.h"
0021 #include "translate/deeplengine.h"
0022 #include "translate/googlecloudengine.h"
0023 #include "translate/translateengine.h"
0024 
0025 using namespace SubtitleComposer;
0026 
0027 TranslateDialog::TranslateDialog(QWidget *parent)
0028     : ActionWithTargetDialog(i18n("Translate"), parent),
0029       m_engines({ new GoogleCloudEngine(parent), new DeepLEngine(parent) }),
0030       m_settings(nullptr)
0031 {
0032     QComboBox *engineCombo = new QComboBox(m_mainWidget);
0033     engineCombo->setEditable(false);
0034     for(const TranslateEngine *e: qAsConst(m_engines))
0035         engineCombo->addItem(e->name());
0036     m_mainLayout->addWidget(engineCombo);
0037     engineCombo->setCurrentText(SCConfig::translateEngine());
0038     connect(engineCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &TranslateDialog::updateEngineUI);
0039     updateEngineUI(engineCombo->currentIndex());
0040 
0041     createTargetsGroupBox(i18n("Translate texts in"));
0042     createLineTargetsButtonGroup();
0043     createTextTargetsButtonGroup();
0044     setTextsTargetEnabled(Both, false);
0045 }
0046 
0047 void
0048 TranslateDialog::updateEngineUI(int index)
0049 {
0050     m_engine = m_engines.at(index);
0051 
0052     m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
0053     connect(m_engine, &TranslateEngine::engineReady, this, [&](bool status){
0054         m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
0055     });
0056 
0057     if(m_settings)
0058         m_settings->deleteLater();
0059     m_settings = new QWidget(m_mainWidget);
0060     m_engine->settings(m_settings);
0061     m_settings->layout()->setContentsMargins(0, 0, 0, 0);
0062     m_mainLayout->insertWidget(1, m_settings);
0063 }
0064 
0065 void
0066 TranslateDialog::performTranslation()
0067 {
0068     static TranslateDialog *dlg = new TranslateDialog(app()->mainWindow());
0069     if(dlg->exec() != QDialog::Accepted)
0070         return;
0071 
0072     SubtitleCompositeActionExecutor executor(appSubtitle(), i18n("Translate texts"));
0073 
0074     RangeList ranges = app()->linesWidget()->targetRanges(dlg->selectedLinesTarget());
0075     const bool primary = dlg->selectedTextsTarget() == Primary;
0076 
0077     QVector<QString> *texts = new QVector<QString>;
0078     for(SubtitleIterator it(*appSubtitle(), ranges); it.current(); ++it)
0079         texts->push_back(it.current()->doc(primary)->toHtml());
0080 
0081     SCConfig::setTranslateEngine(dlg->m_engine->name());
0082     dlg->m_engine->translate(*texts);
0083     SCConfig::self()->save();
0084 
0085     connect(dlg->m_engine, &TranslateEngine::translated, dlg, [=](){
0086         int i = 0;
0087         for(SubtitleIterator it(*appSubtitle(), ranges); it.current(); ++it)
0088             it.current()->doc(primary)->setHtml(texts->at(i++));
0089         delete texts;
0090     });
0091 }