KTextAddons

grammalecteresultwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "grammalecteresultwidget.h"
8#include "common/grammarresulttextedit.h"
9#include "grammalectemanager.h"
10#include "grammalecteparser.h"
11#include "textgrammarcheck_debug.h"
12#include <KLocalizedString>
13#include <KMessageBox>
14#include <QJsonDocument>
15using namespace TextGrammarCheck;
16GrammalecteResultWidget::GrammalecteResultWidget(QWidget *parent)
17 : GrammarResultWidget(parent)
18{
19}
20
21GrammalecteResultWidget::~GrammalecteResultWidget() = default;
22
23void GrammalecteResultWidget::checkGrammar()
24{
25 auto job = new GrammalecteResultJob(this);
26 job->setPythonPath(GrammalecteManager::self()->pythonPath());
27 job->setGrammarlecteCliPath(GrammalecteManager::self()->grammalectePath());
28 job->setArguments(GrammalecteManager::self()->options());
29 job->setText(mResult->toPlainText());
30 connect(job, &GrammalecteResultJob::finished, this, &GrammalecteResultWidget::slotCheckGrammarFinished);
31 connect(job, &GrammalecteResultJob::error, this, &GrammalecteResultWidget::slotError);
32 job->start();
33}
34
35void GrammalecteResultWidget::slotCheckGrammarFinished(const QString &result)
36{
37 GrammalecteParser parser;
38 const QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8());
39 const QJsonObject fields = doc.object();
40 applyGrammarResult(parser.parseResult(fields));
41}
42
43void GrammalecteResultWidget::slotError(GrammalecteResultJob::ErrorType error)
44{
45 QString str;
46 switch (error) {
47 case GrammalecteResultJob::ErrorType::NoError:
48 break;
49 case GrammalecteResultJob::ErrorType::TextIsEmpty:
50 qCWarning(TEXTGRAMMARCHECK_LOG) << "An error found during executing GrammalecteResultJob: text is empty";
51 break;
52 case GrammalecteResultJob::ErrorType::PythonPathMissing:
53 qCWarning(TEXTGRAMMARCHECK_LOG) << "An error found during executing GrammalecteResultJob: missing python path";
54 str = i18n("Python path is missing.");
55 break;
56 case GrammalecteResultJob::ErrorType::GrammalecteMissing:
57 qCWarning(TEXTGRAMMARCHECK_LOG) << "An error found during executing GrammalecteResultJob: missing grammalectepath";
58 str = i18n("Grammalecte path not found.");
59 break;
60 case GrammalecteResultJob::ErrorType::Unknown:
61 qCWarning(TEXTGRAMMARCHECK_LOG) << "An error found during executing GrammalecteResultJob: unknown error";
62 break;
63 case GrammalecteResultJob::ErrorType::PythonPathNotExist:
64 qCWarning(TEXTGRAMMARCHECK_LOG) << "An error found during executing GrammalecteResultJob: python exec doesn't exist";
65 str = i18n("Grammalecte program file not found.");
66 break;
67 case GrammalecteResultJob::ErrorType::GrammarlectCliNotExist:
68 qCWarning(TEXTGRAMMARCHECK_LOG) << "An error found during executing GrammalecteResultJob: grammalecte cli not found.";
69 str = i18n("Grammalecte cli file not found.");
70 break;
71 }
72 if (!str.isEmpty()) {
73 KMessageBox::error(this, str, i18nc("@title:window", "Grammalecte error"));
74 }
75}
76
77#include "moc_grammalecteresultwidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
QByteArray toUtf8() const const
QString toPlainText() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.