File indexing completed on 2025-02-16 04:49:31

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "languagetoolinterface.h"
0008 #include "languagetoolplugin_debug.h"
0009 #include <KActionCollection>
0010 #include <KLocalizedString>
0011 #include <KMessageBox>
0012 #include <KPIMTextEdit/RichTextComposer>
0013 #include <KToggleAction>
0014 #include <TextGrammarCheck/GrammarAction>
0015 #include <TextGrammarCheck/LanguageToolManager>
0016 #include <TextGrammarCheck/LanguageToolResultWidget>
0017 
0018 #include <QHBoxLayout>
0019 #include <QTextBlock>
0020 
0021 LanguageToolInterface::LanguageToolInterface(KActionCollection *ac, QWidget *parent)
0022     : MessageComposer::PluginEditorGrammarCustomToolsViewInterface(parent)
0023     , mGrammarResultWidget(new TextGrammarCheck::LanguageToolResultWidget(this))
0024 {
0025     auto layout = new QHBoxLayout(this);
0026     layout->setContentsMargins({});
0027     connect(mGrammarResultWidget, &TextGrammarCheck::LanguageToolResultWidget::replaceText, this, &LanguageToolInterface::slotReplaceText);
0028     connect(mGrammarResultWidget, &TextGrammarCheck::LanguageToolResultWidget::checkAgain, this, &LanguageToolInterface::checkAgain);
0029     connect(mGrammarResultWidget, &TextGrammarCheck::LanguageToolResultWidget::closeChecker, this, &LanguageToolInterface::closeChecker);
0030     connect(mGrammarResultWidget, &TextGrammarCheck::LanguageToolResultWidget::configure, this, [this]() {
0031         Q_EMIT configure(parentWidget());
0032     });
0033 
0034     layout->addWidget(mGrammarResultWidget);
0035     createAction(ac);
0036 }
0037 
0038 LanguageToolInterface::~LanguageToolInterface() = default;
0039 
0040 void LanguageToolInterface::slotReplaceText(const TextGrammarCheck::GrammarAction &act)
0041 {
0042     if (richTextEditor()) {
0043         QTextBlock block = richTextEditor()->document()->findBlockByNumber(act.blockId() - 1);
0044         if (block.isValid()) {
0045             QTextCursor cur(block);
0046             const int position = cur.position() + act.start();
0047             cur.setPosition(position);
0048             cur.setPosition(position + act.length(), QTextCursor::KeepAnchor);
0049             cur.insertText(act.replacement());
0050         }
0051     }
0052 }
0053 
0054 void LanguageToolInterface::closeChecker()
0055 {
0056     mGrammarResultWidget->hide();
0057     Q_EMIT activateView(nullptr);
0058 }
0059 
0060 void LanguageToolInterface::slotActivateGrammalecte(bool state)
0061 {
0062     if (state) {
0063         if (!checkAgain()) {
0064             mAction->setChecked(false);
0065             return;
0066         }
0067         mGrammarResultWidget->show();
0068         Q_EMIT activateView(this);
0069     } else {
0070         closeChecker();
0071     }
0072 }
0073 
0074 KToggleAction *LanguageToolInterface::action() const
0075 {
0076     return mAction;
0077 }
0078 
0079 void LanguageToolInterface::createAction(KActionCollection *ac)
0080 {
0081     mAction = new KToggleAction(i18n("&Check Grammar (LanguageTool)"), this);
0082     connect(mAction, &KToggleAction::triggered, this, &LanguageToolInterface::slotActivateGrammalecte);
0083     if (ac) {
0084         ac->addAction(QStringLiteral("checkgrammar-languagetool"), mAction);
0085     }
0086     mAction->setChecked(false);
0087 }
0088 
0089 bool LanguageToolInterface::checkAgain()
0090 {
0091     if (richTextEditor()) {
0092         if (!TextGrammarCheck::LanguageToolManager::self()->useLocalInstance()) {
0093             if (KMessageBox::warningTwoActions(parentWidget(),
0094                                                i18n("You do not use local instance.\nYour text will be sent to an external web site "
0095                                                     "(https://languagetool.org/).\nDo you want to continue?"),
0096                                                i18n("Check Grammar with LanguageTool"),
0097                                                KStandardGuiItem::cont(),
0098                                                KStandardGuiItem::cancel(),
0099                                                QStringLiteral("send_data_on_languagetool"))
0100                 == KMessageBox::ButtonCode::SecondaryAction) {
0101                 return false;
0102             }
0103         }
0104         mGrammarResultWidget->setText(richTextEditor()->toPlainText());
0105         mGrammarResultWidget->checkGrammar();
0106     } else {
0107         qCWarning(KMAIL_EDITOR_LANGUAGETOOL_PLUGIN_LOG) << "richtexteditor not set, it's a bug";
0108     }
0109     return true;
0110 }
0111 
0112 #include "moc_languagetoolinterface.cpp"