File indexing completed on 2024-11-24 04:16:56

0001 /*
0002   SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "libretranslateengineconfigurewidget.h"
0008 #include <KLocalizedString>
0009 #include <QCheckBox>
0010 #include <QCompleter>
0011 #include <QFormLayout>
0012 #include <QLineEdit>
0013 #if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
0014 #include <TextAddonsWidgets/LineEditCatchReturnKey>
0015 #else
0016 #include <KLineEditEventHandler>
0017 #endif
0018 
0019 LibreTranslateEngineConfigureWidget::LibreTranslateEngineConfigureWidget(QWidget *parent)
0020     : QWidget{parent}
0021     , mServerUrl(new QLineEdit(this))
0022     , mApiKey(new QLineEdit(this))
0023     , mRequiredApiKey(new QCheckBox(i18n("Server required Api Key"), this))
0024 {
0025     auto mainLayout = new QFormLayout(this);
0026     mainLayout->setContentsMargins({});
0027     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0028 
0029     mServerUrl->setObjectName(QStringLiteral("mServerUrl"));
0030     mServerUrl->setClearButtonEnabled(true);
0031     mainLayout->addRow(i18n("Server Url:"), mServerUrl);
0032 
0033     mRequiredApiKey->setObjectName(QStringLiteral("mRequiredApiKey"));
0034     mainLayout->addWidget(mRequiredApiKey);
0035 
0036     mApiKey->setObjectName(QStringLiteral("mApiKey"));
0037     mApiKey->setClearButtonEnabled(true);
0038     mainLayout->addRow(i18n("Api Key:"), mApiKey);
0039     connect(mRequiredApiKey, &QCheckBox::clicked, this, &LibreTranslateEngineConfigureWidget::updateApiKeyState);
0040 
0041     const QStringList listServer{
0042         QStringLiteral("https://libretranslate.com"),
0043         QStringLiteral("https://libretranslate.de"),
0044         QStringLiteral("https://translate.argosopentech.com"),
0045         QStringLiteral("https://translate.api.skitzen.com"),
0046         QStringLiteral("https://translate.fortytwo-it.com"),
0047         QStringLiteral("https://translate.terraprint.co"),
0048         QStringLiteral("https://lt.vern.cc"),
0049     };
0050 
0051 #if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
0052     new TextAddonsWidgets::LineEditCatchReturnKey(mApiKey, this);
0053     new TextAddonsWidgets::LineEditCatchReturnKey(mServerUrl, this);
0054 #else
0055     KLineEditEventHandler::catchReturnKey(mApiKey);
0056     KLineEditEventHandler::catchReturnKey(mServerUrl);
0057 #endif
0058 
0059     auto completer = new QCompleter(listServer, this);
0060     completer->setCaseSensitivity(Qt::CaseInsensitive);
0061     mServerUrl->setCompleter(completer);
0062 }
0063 
0064 LibreTranslateEngineConfigureWidget::~LibreTranslateEngineConfigureWidget() = default;
0065 
0066 void LibreTranslateEngineConfigureWidget::updateApiKeyState(bool state)
0067 {
0068     mApiKey->setEnabled(state);
0069 }
0070 
0071 QString LibreTranslateEngineConfigureWidget::serverUrl() const
0072 {
0073     return mServerUrl->text();
0074 }
0075 
0076 void LibreTranslateEngineConfigureWidget::setServerUrl(const QString &serverUrl)
0077 {
0078     mServerUrl->setText(serverUrl);
0079 }
0080 
0081 QString LibreTranslateEngineConfigureWidget::apiKey() const
0082 {
0083     return mApiKey->text();
0084 }
0085 
0086 void LibreTranslateEngineConfigureWidget::setApiKey(const QString &key)
0087 {
0088     mApiKey->setText(key);
0089 }
0090 
0091 bool LibreTranslateEngineConfigureWidget::serverRequiredApiKey() const
0092 {
0093     return mRequiredApiKey->isChecked();
0094 }
0095 
0096 void LibreTranslateEngineConfigureWidget::setServerRequiredApiKey(bool state)
0097 {
0098     mRequiredApiKey->setChecked(state);
0099     updateApiKeyState(state);
0100 }
0101 
0102 #include "moc_libretranslateengineconfigurewidget.cpp"