KTextAddons

languagetoolresultwidget.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 "languagetoolresultwidget.h"
8#include "common/grammarresulttextedit.h"
9#include "languagetoolcombobox.h"
10#include "languagetoolmanager.h"
11#include "languagetoolparser.h"
12#include "languagetoolresultjob.h"
13#include "languagetoolupdatecombobox.h"
14
15#include <KLocalizedString>
16#include <KMessageBox>
17
18#include <QHBoxLayout>
19#include <QJsonDocument>
20#include <QToolButton>
21using namespace TextGrammarCheck;
22LanguageToolResultWidget::LanguageToolResultWidget(QWidget *parent)
23 : GrammarResultWidget(parent)
24 , mLanguageToolUpdateCombobox(new LanguageToolUpdateComboBox(this))
25{
26 addExtraWidget();
27 mLanguageToolUpdateCombobox->setLanguageToolCombobox(mLanguageToolComboBox);
28 mLanguageToolUpdateCombobox->setParentWidget(this);
29 if (LanguageToolManager::self()->allowToGetListOfLanguages()) {
30 mLanguageToolUpdateCombobox->firstRefreshListOfLanguages();
31 } else {
32 mLanguageToolComboBox->setLanguage(LanguageToolManager::self()->language());
33 }
34}
35
36LanguageToolResultWidget::~LanguageToolResultWidget() = default;
37
38void LanguageToolResultWidget::checkGrammar()
39{
40 auto job = new LanguageToolResultJob(this);
41 job->setUrl(LanguageToolManager::self()->languageToolCheckPath());
42 job->setNetworkAccessManager(LanguageToolManager::self()->networkAccessManager());
43 job->setText(mResult->toPlainText());
44 job->setLanguage(/*LanguageToolManager::self()->language()*/ mLanguageToolComboBox->language());
45 connect(job, &LanguageToolResultJob::finished, this, &LanguageToolResultWidget::slotCheckGrammarFinished);
46 connect(job, &LanguageToolResultJob::error, this, &LanguageToolResultWidget::slotError);
47 job->start();
48}
49
50void LanguageToolResultWidget::slotCheckGrammarFinished(const QString &result)
51{
52 LanguageToolParser parser;
53 const QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8());
54 const QJsonObject fields = doc.object();
55 applyGrammarResult(parser.parseResult(fields));
56}
57
58void LanguageToolResultWidget::slotError(const QString &str)
59{
60 KMessageBox::error(this, i18n("An error was reported: %1", str), i18nc("@title:window", "Failed to check grammar."));
61}
62
63void LanguageToolResultWidget::addExtraWidget()
64{
65 auto extraWidgetLayout = new QHBoxLayout;
66 extraWidgetLayout->setObjectName(QStringLiteral("extraWidgetLayout"));
67 extraWidgetLayout->setContentsMargins({});
68 mLanguageToolComboBox = new LanguageToolComboBox(this);
69 mLanguageToolComboBox->setObjectName(QStringLiteral("languagetoolcombobox"));
70 extraWidgetLayout->addWidget(mLanguageToolComboBox);
71
72 auto recheckBtn = new QToolButton(this);
73 recheckBtn->setObjectName(QStringLiteral("recheck-button"));
74 recheckBtn->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
75 recheckBtn->setIconSize(QSize(16, 16));
76 recheckBtn->setToolTip(i18nc("@info:tooltip", "Check"));
77 recheckBtn->setAutoRaise(true);
78 extraWidgetLayout->addWidget(recheckBtn);
79
80 mExtraWidgetLayout->addLayout(extraWidgetLayout);
81 connect(recheckBtn, &QToolButton::clicked, this, &LanguageToolResultWidget::checkAgain);
82}
83
84#include "moc_languagetoolresultwidget.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)
void clicked(bool checked)
void addLayout(QLayout *layout, int stretch)
QIcon fromTheme(const QString &name)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setObjectName(QAnyStringView name)
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 Mon Nov 18 2024 12:19:49 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.