File indexing completed on 2024-05-12 16:16:01

0001 /*
0002    SPDX-FileCopyrightText: 2019-2023 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "grammalectegrammarerror.h"
0008 #include "textgrammarcheck_debug.h"
0009 
0010 #include <QJsonArray>
0011 
0012 using namespace TextGrammarCheck;
0013 
0014 GrammalecteGrammarError::GrammalecteGrammarError() = default;
0015 
0016 GrammalecteGrammarError::~GrammalecteGrammarError() = default;
0017 
0018 QStringList GrammalecteGrammarError::parseSuggestion(const QJsonObject &obj)
0019 {
0020     QStringList lst;
0021     const QJsonArray array = obj[QStringLiteral("aSuggestions")].toArray();
0022     const QVariantList list = array.toVariantList();
0023     lst.reserve(list.count());
0024     for (const QVariant &v : list) {
0025         // qDebug() << " v" << v.toString();
0026         lst.append(v.toString());
0027     }
0028     return lst;
0029 }
0030 
0031 QColor GrammalecteGrammarError::parseColor(const QJsonObject &obj)
0032 {
0033     QColor col;
0034     const QJsonArray array = obj[QStringLiteral("aColor")].toArray();
0035     if (array.isEmpty()) {
0036         return col;
0037     }
0038     if (array.count() == 3) {
0039         // const QVariantList list = array.toVariantList();
0040         //        for (const QVariant &v : list) {
0041         //            qDebug() << " v" << v.toInt();
0042         //        }
0043         col = QColor(array.at(0).toInt(), array.at(1).toInt(), array.at(2).toInt());
0044     } else {
0045         qCWarning(TEXTGRAMMARCHECK_LOG) << "Parsing color: Array is not correct:" << array;
0046     }
0047     return col;
0048 }
0049 
0050 void GrammalecteGrammarError::parse(const QJsonObject &obj, int blockindex)
0051 {
0052     const int end = obj[QStringLiteral("nEnd")].toInt(-1);
0053     mStart = obj[QStringLiteral("nStart")].toInt(-1);
0054     if (end != -1) {
0055         mLength = end - mStart;
0056     }
0057     mError = obj[QStringLiteral("sMessage")].toString();
0058     if (mLength != -1) {
0059         mBlockId = blockindex;
0060         mColor = parseColor(obj);
0061         mSuggestions = parseSuggestion(obj);
0062     }
0063     mRule = obj[QStringLiteral("sRuleId")].toString();
0064     mOption = obj[QStringLiteral("sType")].toString();
0065     mUrl = obj[QStringLiteral("URL")].toString();
0066 }