File indexing completed on 2024-12-08 06:36:17

0001 /*
0002     SPDX-FileCopyrightText: 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "browserwidget.h"
0007 
0008 #include <KEduVocExpression>
0009 
0010 #include <QDragEnterEvent>
0011 
0012 using namespace Editor;
0013 
0014 BrowserWidget::BrowserWidget(QWidget *parent)
0015     : QWidget(parent)
0016 {
0017     setupUi(this);
0018     m_htmlPart = new QWebEngineView(widget);
0019     m_currentTranslation = -1;
0020     m_currentProvider = 0; ///@todo kconfig?
0021 
0022     QHBoxLayout *layout = new QHBoxLayout(widget);
0023     widget->setLayout(layout);
0024     layout->addWidget(m_htmlPart);
0025 
0026     connect(showCurrentButton, &QPushButton::clicked, this, &BrowserWidget::showCurrentTranslation);
0027 
0028     setupProviders();
0029     connect(providerComboBox, static_cast<void (KComboBox::*)(int)>(&KComboBox::currentIndexChanged), this, &BrowserWidget::providerChanged);
0030 }
0031 
0032 void BrowserWidget::setupProviders()
0033 {
0034     providerComboBox->clear();
0035 
0036     DictionaryProvider provider;
0037     provider.name = QStringLiteral("De-Es Beolingus");
0038     provider.url = QStringLiteral("https://dict.tu-chemnitz.de/dings.cgi?service=dees&query=\\{@}");
0039     provider.languages << QStringLiteral("de") << QStringLiteral("es");
0040     m_providers.append(provider);
0041     providerComboBox->addItem(provider.name);
0042 
0043     provider.name = QStringLiteral("De-En Beolingus");
0044     provider.url = QStringLiteral("https://dict.tu-chemnitz.de/dings.cgi?service=deen?query=\\{@}");
0045     provider.languages << QStringLiteral("de") << QStringLiteral("en");
0046     m_providers.append(provider);
0047     providerComboBox->addItem(provider.name);
0048 
0049     provider.name = QStringLiteral("De-En Leo");
0050     provider.url = QStringLiteral("https://dict.leo.org/?search=\\{@}");
0051     provider.languages << QStringLiteral("de") << QStringLiteral("en");
0052     m_providers.append(provider);
0053     providerComboBox->addItem(provider.name);
0054 
0055     provider.name = QStringLiteral("De-Es Leo");
0056     provider.url = QStringLiteral("https://dict.leo.org/?lp=esde&search=\\{@}");
0057     provider.languages << QStringLiteral("de") << QStringLiteral("es");
0058     m_providers.append(provider);
0059     providerComboBox->addItem(provider.name);
0060 
0061     provider.name = QStringLiteral("De-Fr Leo");
0062     provider.url = QStringLiteral("https://dict.leo.org/?lp=frde&search=\\{@}");
0063     provider.languages << QStringLiteral("de") << QStringLiteral("fr");
0064     m_providers.append(provider);
0065     providerComboBox->addItem(provider.name);
0066 
0067     provider.name = QStringLiteral("Ru-En Multitran");
0068     provider.url = QStringLiteral("https://multitran.com/c/m.exe?SHL=1&CL=1&l1=1&s=\\{@}");
0069     provider.languages << QStringLiteral("ru") << QStringLiteral("en");
0070     m_providers.append(provider);
0071     providerComboBox->addItem(provider.name);
0072 
0073     provider.name = QStringLiteral("Ru-De Multitran");
0074     provider.url = QStringLiteral("https://multitran.com/c/m.exe?SHL=1&CL=1&l1=3&s=\\{@}");
0075     provider.languages << QStringLiteral("ru") << QStringLiteral("de");
0076     m_providers.append(provider);
0077     providerComboBox->addItem(provider.name);
0078 
0079     provider.name = QStringLiteral("Ru-Fr Multitran");
0080     provider.url = QStringLiteral("https://multitran.com/c/m.exe?SHL=1&CL=1&l1=4&s=\\{@}");
0081     provider.languages << QStringLiteral("ru") << QStringLiteral("fr");
0082     m_providers.append(provider);
0083     providerComboBox->addItem(provider.name);
0084 
0085     provider.name = QStringLiteral("Heinzelnisse");
0086     provider.url = QStringLiteral(
0087         "https://heinzelnisse.info/"
0088         "dict?setOptions=true&searchItem=\\{@}&dictDeNoSearch=on&dictNoDeSearch=on&dictExactSearch=on&dictPhoneticSearch=on&wikiSearch=on&dictNynorskSearch=on&"
0089         "dictBokmaalSearch=checked&forumKeywordSearch=on&suggestion=on");
0090     provider.languages << QStringLiteral("no") << QStringLiteral("de");
0091     m_providers.append(provider);
0092     providerComboBox->addItem(provider.name);
0093 }
0094 
0095 void BrowserWidget::setTranslation(KEduVocExpression *entry, int translation)
0096 {
0097     m_currentTranslation = translation;
0098     m_entry = entry;
0099 
0100     if (followTableCheckBox->isChecked()) {
0101         showCurrentTranslation();
0102     }
0103 }
0104 
0105 void BrowserWidget::showCurrentTranslation()
0106 {
0107     if (m_entry) {
0108         if (m_entry->translation(m_currentTranslation)) {
0109             QString text = m_entry->translation(m_currentTranslation)->text();
0110             if (!text.isEmpty()) {
0111                 m_htmlPart->load(QUrl(QString(m_providers.value(m_currentProvider).url.replace(QLatin1String("\\{@}"), text))));
0112                 m_htmlPart->show();
0113             }
0114         }
0115     }
0116 }
0117 
0118 void BrowserWidget::openUrl(const QUrl &targetUrl)
0119 {
0120     m_htmlPart->load(targetUrl);
0121     m_htmlPart->show();
0122 }
0123 
0124 void BrowserWidget::providerChanged(int provider)
0125 {
0126     m_currentProvider = provider;
0127     showCurrentTranslation();
0128 }
0129 
0130 #include "moc_browserwidget.cpp"