8#include "translatorwidget.h"
11#include "texttranslator_debug.h"
12#include "translator/misc/translatorutil.h"
13#include "translator/networkmanager.h"
14#include "translator/translatorengineclient.h"
15#include "translator/translatorengineloader.h"
16#include "translator/translatorengineplugin.h"
17#include "translatorconfiguredialog.h"
18#include "translatordebugdialog.h"
19#include <KBusyIndicatorWidget>
21#include <KConfigGroup>
22#include <KLocalizedString>
27#include <KSharedConfig>
40using namespace TextTranslator;
43static const char myTranslatorWidgetConfigGroupName[] =
"TranslatorWidget";
48 TranslatorWidgetPrivate() =
default;
50 ~TranslatorWidgetPrivate()
52 delete translatorPlugin;
56 void fillToCombobox(
const QString &lang);
65 QLabel *engineNameLabel =
nullptr;
66 TextTranslator::TranslatorEngineClient *translatorClient =
nullptr;
67 TextTranslator::TranslatorEnginePlugin *translatorPlugin =
nullptr;
72 bool languageSettingsChanged =
false;
73 bool standalone =
true;
76void TranslatorWidget::TranslatorWidgetPrivate::fillToCombobox(
const QString &lang)
80 TranslatorUtil translatorUtil;
85 const QString languageCode = TranslatorUtil::languageCode(i.key());
86 if ((i.key() != TranslatorUtil::automatic) && languageCode != lang) {
87 translatorUtil.addItemToFromComboBox(toCombobox, languageCode, i.value());
92void TranslatorWidget::TranslatorWidgetPrivate::initLanguage()
94 if (!translatorClient) {
98 fromCombobox->clear();
102 TranslatorUtil translatorUtil;
103 while (i.hasNext()) {
105 const QString languageCode = TranslatorUtil::languageCode(i.key());
106 translatorUtil.addItemToFromComboBox(fromCombobox, languageCode, i.value());
115void TranslatorTextEdit::dropEvent(
QDropEvent *event)
117 if (
event->source() !=
this) {
118 if (
event->mimeData()->hasText()) {
132TranslatorWidget::TranslatorWidget(
QWidget *parent)
134 , d(new TranslatorWidgetPrivate)
139TranslatorWidget::TranslatorWidget(
const QString &text,
QWidget *parent)
141 , d(new TranslatorWidgetPrivate)
144 d->inputText->setPlainText(text);
147TranslatorWidget::~TranslatorWidget()
150 disconnect(d->inputText, &TranslatorTextEdit::translateText,
this, &TranslatorWidget::slotTranslate);
154void TranslatorWidget::writeConfig()
156 if (d->languageSettingsChanged) {
158 myGroup.writeEntry(QStringLiteral(
"FromLanguage"), d->fromCombobox->itemData(d->fromCombobox->currentIndex()).toString());
159 myGroup.writeEntry(
"ToLanguage", d->toCombobox->itemData(d->toCombobox->currentIndex()).toString());
163 myGroupUi.writeEntry(
"mainSplitter", d->splitter->sizes());
167void TranslatorWidget::readConfig()
171 d->splitter->setSizes(myGroupUi.readEntry(
"mainSplitter",
size));
174 const QString from = myGroup.readEntry(QStringLiteral(
"FromLanguage"));
178 const int indexFrom = d->fromCombobox->findData(from);
179 if (indexFrom != -1) {
180 d->fromCombobox->setCurrentIndex(indexFrom);
182 d->translatorClient->generateToListFromCurrentToLanguage(from);
184 d->toCombobox->blockSignals(
true);
185 d->fillToCombobox(from);
186 d->toCombobox->blockSignals(
false);
188 const QString to = myGroup.readEntry(QStringLiteral(
"ToLanguage"));
189 const int indexTo = d->toCombobox->findData(to);
191 d->toCombobox->setCurrentIndex(indexTo);
193 d->invert->setEnabled(from !=
"auto"_L1);
196void TranslatorWidget::loadEngineSettings()
198 d->engineName = TranslatorUtil::loadEngine();
203void TranslatorWidget::init()
216 closeBtn->setObjectName(QStringLiteral(
"close-button"));
218 closeBtn->setIconSize(
QSize(16, 16));
219 closeBtn->setToolTip(
i18nc(
"@info:tooltip",
"Close"));
221#ifndef QT_NO_ACCESSIBILITY
222 closeBtn->setAccessibleName(
i18n(
"Close"));
224 closeBtn->setAutoRaise(
true);
225 hboxLayout->addWidget(closeBtn);
229 hboxLayout->addWidget(label);
231 d->fromCombobox->setMinimumWidth(50);
232 d->fromCombobox->setObjectName(QStringLiteral(
"from"));
233 hboxLayout->addWidget(d->fromCombobox);
236 hboxLayout->addWidget(label);
238 d->toCombobox->setMinimumWidth(50);
239 d->toCombobox->setObjectName(QStringLiteral(
"to"));
241 hboxLayout->addWidget(d->toCombobox);
245 hboxLayout->addWidget(separator);
247 d->invert =
new QPushButton(
i18nc(
"Invert language choices so that from becomes to and to becomes from",
"Invert"),
this);
248 d->invert->setObjectName(QStringLiteral(
"invert-button"));
250 hboxLayout->addWidget(d->invert);
253 d->clear->setObjectName(QStringLiteral(
"clear-button"));
254#ifndef QT_NO_ACCESSIBILITY
255 d->clear->setAccessibleName(
i18n(
"Clear"));
258 hboxLayout->addWidget(d->clear);
261 d->translate->setObjectName(QStringLiteral(
"translate-button"));
262#ifndef QT_NO_ACCESSIBILITY
263 d->translate->setAccessibleName(
i18n(
"Translate"));
266 hboxLayout->addWidget(d->translate);
269 if (!qEnvironmentVariableIsEmpty(
"TRANSLATING_DEBUGGING")) {
271 hboxLayout->addWidget(debugButton);
276 hboxLayout->addWidget(d->progressIndicator);
277 d->progressIndicator->setFixedHeight(d->toCombobox->height());
279 hboxLayout->addStretch();
281 d->engineNameLabel =
new QLabel(
this);
282 hboxLayout->addWidget(d->engineNameLabel);
285 configureButton->setObjectName(QStringLiteral(
"configure_button"));
287 configureButton->setIconSize(
QSize(16, 16));
288 configureButton->setToolTip(
i18nc(
"@info:tooltip",
"Configure"));
292 loadEngineSettings();
295 hboxLayout->addWidget(configureButton);
297 layout->addLayout(hboxLayout);
304 d->splitter->setChildrenCollapsible(
false);
306 d->inputText->setObjectName(QStringLiteral(
"inputtext"));
309 connect(d->inputText, &TranslatorTextEdit::translateText,
this, &TranslatorWidget::slotTranslate);
311 d->splitter->addWidget(d->inputText);
313 d->translatorResultTextEdit->setObjectName(QStringLiteral(
"translatedtext"));
314 d->translatorResultTextEdit->setReadOnly(
true);
315 d->splitter->addWidget(d->translatorResultTextEdit);
319 d->fromCombobox->setCurrentIndex(0);
320 loadEngineSettings();
322 slotFromLanguageChanged(0,
true);
326 slotFromLanguageChanged(val, false);
336 d->languageSettingsChanged =
false;
339void TranslatorWidget::switchEngine()
341 if (d->translatorPlugin) {
343 delete d->translatorPlugin;
344 d->translatorPlugin =
nullptr;
346 d->translatorClient = TextTranslator::TranslatorEngineLoader::self()->createTranslatorClient(d->engineName);
347 if (!d->translatorClient) {
348 const QString fallBackEngineName = TextTranslator::TranslatorEngineLoader::self()->fallbackFirstEngine();
349 if (!fallBackEngineName.
isEmpty()) {
350 d->translatorClient = TextTranslator::TranslatorEngineLoader::self()->createTranslatorClient(fallBackEngineName);
353 if (d->translatorClient) {
354 d->translatorPlugin = d->translatorClient->createTranslator();
355 connect(d->translatorPlugin, &TextTranslator::TranslatorEnginePlugin::translateDone,
this, &TranslatorWidget::slotTranslateDone);
356 connect(d->translatorPlugin, &TextTranslator::TranslatorEnginePlugin::translateFailed,
this, &TranslatorWidget::slotTranslateFailed);
358 d->engineNameLabel->setText(QStringLiteral(
"[%1]").arg(d->translatorClient->translatedName()));
359 d->invert->setVisible(d->translatorClient->hasInvertSupport());
364void TranslatorWidget::updatePlaceHolder()
366 if (d->translatorClient->engineType() == TextTranslator::TranslatorEngineClient::Network) {
367 d->inputText->setPlaceholderText(
368 i18nc(
"@info:placeholder",
"Drag text that you want to translate. (Be careful text will be send to external Server)."));
370 d->inputText->setPlaceholderText(
i18nc(
"@info:placeholder",
"Drag text that you want to translate."));
374void TranslatorWidget::slotConfigChanged()
376 d->languageSettingsChanged =
true;
379void TranslatorWidget::slotTextChanged()
381 d->translate->setEnabled(!d->inputText->document()->isEmpty());
382 d->clear->setEnabled(!d->inputText->document()->isEmpty());
385void TranslatorWidget::slotFromLanguageChanged(
int index,
bool initialize)
387 const QString lang = d->fromCombobox->itemData(index).toString();
388 d->invert->setEnabled(lang !=
"auto"_L1);
389 const QString to = d->toCombobox->itemData(d->toCombobox->currentIndex()).toString();
393 d->translatorClient->generateToListFromCurrentToLanguage(lang);
394 d->toCombobox->blockSignals(
true);
395 d->fillToCombobox(lang);
396 d->toCombobox->blockSignals(
false);
397 const int indexTo = d->toCombobox->findData(to);
399 d->toCombobox->setCurrentIndex(indexTo);
406void TranslatorWidget::setTextToTranslate(
const QString &text)
408 d->inputText->setPlainText(text);
412void TranslatorWidget::slotTranslate()
414 if (!d->translatorPlugin) {
415 qCWarning(TEXTTRANSLATOR_LOG) <<
" Translator plugin invalid";
418 if (!TextTranslator::NetworkManager::self()->isOnline()) {
422 const QString textToTranslate = d->inputText->toPlainText();
427 d->translatorResultTextEdit->clear();
429 const QString from = d->fromCombobox->itemData(d->fromCombobox->currentIndex()).toString();
430 const QString to = d->toCombobox->itemData(d->toCombobox->currentIndex()).toString();
431 d->translate->setEnabled(
false);
432 d->progressIndicator->show();
434 const QString inputText{d->inputText->toPlainText()};
436 d->translatorPlugin->setFrom(from);
437 d->translatorPlugin->setTo(to);
438 d->translatorPlugin->setInputText(inputText);
439 d->translatorPlugin->translate();
443void TranslatorWidget::slotTranslateDone()
445 d->translate->setEnabled(
true);
446 d->progressIndicator->hide();
447 d->translatorResultTextEdit->setPlainText(d->translatorPlugin->resultTranslate());
450void TranslatorWidget::slotTranslateFailed(
const QString &message)
452 d->translate->setEnabled(
true);
453 d->progressIndicator->hide();
454 d->translatorResultTextEdit->clear();
460void TranslatorWidget::slotInvertLanguage()
462 const QString fromLanguage = d->fromCombobox->itemData(d->fromCombobox->currentIndex()).toString();
464 if (fromLanguage ==
"auto"_L1) {
468 const QString toLanguage = d->toCombobox->itemData(d->toCombobox->currentIndex()).toString();
469 const int indexFrom = d->fromCombobox->findData(toLanguage);
470 if (indexFrom != -1) {
471 d->fromCombobox->setCurrentIndex(indexFrom);
473 const int indexTo = d->toCombobox->findData(fromLanguage);
475 d->toCombobox->setCurrentIndex(indexTo);
480void TranslatorWidget::setStandalone(
bool b)
485void TranslatorWidget::slotCloseWidget()
490 d->inputText->clear();
491 d->translatorResultTextEdit->clear();
492 d->progressIndicator->hide();
499bool TranslatorWidget::event(
QEvent *e)
516void TranslatorWidget::slotClear()
518 d->inputText->clear();
519 d->translatorResultTextEdit->clear();
520 d->translate->setEnabled(
false);
521 if (d->translatorPlugin) {
522 d->translatorPlugin->clear();
526void TranslatorWidget::slotDebug()
528 if (d->translatorPlugin) {
529 TranslatorDebugDialog dlg(
this);
530 dlg.setDebug(d->translatorPlugin->jsonDebug());
533 qCWarning(TEXTTRANSLATOR_LOG) <<
" Translator plugin invalid";
537#include "moc_translatorwidget.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
The TranslatorConfigureDialog class.
The TranslatorTextEdit class.
The TranslatorWidget class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
void information(QWidget *parent, const QString &text, const QString &title=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QString label(StandardShortcut id)
virtual void setSpacing(int spacing) override
void currentIndexChanged(int index)
QIcon fromTheme(const QString &name)
void setContentsMargins(const QMargins &margins)
virtual void setSpacing(int)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
QObject * parent() const const
virtual void dropEvent(QDropEvent *e) override
QTextCursor textCursor() const const
bool isEmpty() const const
QString trimmed() const const
PM_LayoutHorizontalSpacing