File indexing completed on 2025-01-19 04:46:43

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 "grammalecteinterface.h"
0008 #include "grammalecteplugin_debug.h"
0009 #include <KPIMTextEdit/RichTextComposer>
0010 
0011 #include <TextGrammarCheck/GrammalecteResultWidget>
0012 #include <TextGrammarCheck/GrammarAction>
0013 
0014 #include <KActionCollection>
0015 #include <KLocalizedString>
0016 #include <KToggleAction>
0017 
0018 #include <QHBoxLayout>
0019 #include <QTextBlock>
0020 
0021 GrammalecteInterface::GrammalecteInterface(KActionCollection *ac, QWidget *parent)
0022     : MessageComposer::PluginEditorGrammarCustomToolsViewInterface(parent)
0023     , mGrammarResultWidget(new TextGrammarCheck::GrammalecteResultWidget(this))
0024 {
0025     auto layout = new QHBoxLayout(this);
0026     layout->setContentsMargins({});
0027     connect(mGrammarResultWidget, &TextGrammarCheck::GrammalecteResultWidget::replaceText, this, &GrammalecteInterface::slotReplaceText);
0028     connect(mGrammarResultWidget, &TextGrammarCheck::GrammalecteResultWidget::checkAgain, this, &GrammalecteInterface::checkAgain);
0029     connect(mGrammarResultWidget, &TextGrammarCheck::GrammalecteResultWidget::closeChecker, this, &GrammalecteInterface::closeChecker);
0030     connect(mGrammarResultWidget, &TextGrammarCheck::GrammalecteResultWidget::configure, this, [this]() {
0031         Q_EMIT configure(parentWidget());
0032     });
0033 
0034     layout->addWidget(mGrammarResultWidget);
0035     createAction(ac);
0036 }
0037 
0038 GrammalecteInterface::~GrammalecteInterface() = default;
0039 
0040 void GrammalecteInterface::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 GrammalecteInterface::slotActivateGrammalecte(bool state)
0055 {
0056     if (state) {
0057         mGrammarResultWidget->show();
0058         checkAgain();
0059         Q_EMIT activateView(this);
0060     } else {
0061         closeChecker();
0062     }
0063 }
0064 
0065 void GrammalecteInterface::closeChecker()
0066 {
0067     mGrammarResultWidget->hide();
0068     Q_EMIT activateView(nullptr);
0069 }
0070 
0071 KToggleAction *GrammalecteInterface::action() const
0072 {
0073     return mAction;
0074 }
0075 
0076 void GrammalecteInterface::createAction(KActionCollection *ac)
0077 {
0078     mAction = new KToggleAction(i18n("&Check Grammar (Grammalecte)"), this);
0079     connect(mAction, &KToggleAction::triggered, this, &GrammalecteInterface::slotActivateGrammalecte);
0080     if (ac) {
0081         ac->addAction(QStringLiteral("checkgrammar-grammalecte"), mAction);
0082     }
0083     mAction->setChecked(false);
0084 }
0085 
0086 void GrammalecteInterface::checkAgain()
0087 {
0088     if (richTextEditor()) {
0089         mGrammarResultWidget->setText(richTextEditor()->toPlainText());
0090         mGrammarResultWidget->checkGrammar();
0091     } else {
0092         qCWarning(KMAIL_EDITOR_GRAMMALECTE_PLUGIN_LOG) << "richtexteditor not set, it's a bug";
0093     }
0094 }
0095 
0096 #include "moc_grammalecteinterface.cpp"