KTextAddons

languagetoolgrammarerror.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 "languagetoolgrammarerror.h"
8using namespace Qt::Literals::StringLiterals;
9
10#include "languagetoolmanager.h"
11#include "textgrammarcheck_debug.h"
12
13#include <QJsonArray>
14using namespace TextGrammarCheck;
15LanguageToolGrammarError::LanguageToolGrammarError() = default;
16
17LanguageToolGrammarError::~LanguageToolGrammarError() = default;
18
19void LanguageToolGrammarError::parse(const QJsonObject &obj, int blockindex)
20{
21 // We use block id index based on 1 in API
22 mBlockId = blockindex;
23 mError = obj["message"_L1].toString();
24 mStart = obj["offset"_L1].toInt(-1);
25 mLength = obj["length"_L1].toInt(-1);
26 mSuggestions = parseSuggestion(obj);
27 const QJsonObject rulesObj = obj["rule"_L1].toObject();
28 if (!rulesObj.isEmpty()) {
29 mRule = rulesObj["id"_L1].toString();
30 const QJsonArray urlArray = rulesObj["urls"_L1].toArray();
31 if (!urlArray.isEmpty()) {
32 if (urlArray.count() > 1) {
33 qCWarning(TEXTGRAMMARCHECK_LOG) << "LanguageToolGrammarError::parse : more than 1 url found. Perhaps need to adapt api ";
34 }
35 mUrl = urlArray.at(0)["value"_L1].toString();
36 // qDebug() << " mUrl" << mUrl;
37 }
38 }
39 if (!mRule.isEmpty() && !mTesting) {
40 mColor = LanguageToolManager::self()->grammarColorForError(mRule);
41 } else {
42 mColor = QColor(Qt::red);
43 }
44}
45
46void LanguageToolGrammarError::setTesting(bool b)
47{
48 mTesting = b;
49}
50
51QStringList LanguageToolGrammarError::parseSuggestion(const QJsonObject &obj)
52{
53 QStringList lst;
54 const QJsonArray array = obj["replacements"_L1].toArray();
55 for (const QJsonValue &current : array) {
56 if (current.type() == QJsonValue::Object) {
57 const QJsonObject suggestionObject = current.toObject();
58 lst.append(suggestionObject["value"_L1].toString());
59 }
60 }
61 // qDebug() << " lst : " << lst;
62 return lst;
63}
char * toString(const EngineQuery &query)
QJsonValue at(qsizetype i) const const
qsizetype count() const const
bool isEmpty() const const
bool isEmpty() const const
QString toString() const const
void append(QList< T > &&value)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.