aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/annotationeditor/defaultannotations.cpp
blob: 81441ba6a8b3214d5533b4f59e6f947411364725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "defaultannotations.h"

#include <QColor>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QRegularExpression>

namespace QmlDesigner {
DefaultAnnotationsModel::DefaultAnnotationsModel(QObject *parent)
    : QAbstractListModel(parent)
{
    qRegisterMetaType<RichTextProxy>();
}

DefaultAnnotationsModel::~DefaultAnnotationsModel() {}

int DefaultAnnotationsModel::rowCount(const QModelIndex &) const
{
    return static_cast<int>(m_defaults.size());
}

QVariant DefaultAnnotationsModel::data(const QModelIndex &index, int role) const
{
    const auto row = static_cast<size_t>(index.row());
    if (!index.isValid() || m_defaults.size() < row)
        return {};

    auto &item = m_defaults[row];

    switch (role) {
    case Qt::DisplayRole:
    case Qt::EditRole:
    case Name:
        return item.first;
    case Type:
        return item.second.typeName();
    case Default:
        return item.second;
    }

    return {};
}

QVariantMap DefaultAnnotationsModel::fetchData() const
{
    return m_defaultMap;
}

bool DefaultAnnotationsModel::hasDefault(const Comment &comment) const
{
    return m_defaultMap.count(comment.title().toLower());
}

QMetaType::Type DefaultAnnotationsModel::defaultType(const Comment &comment) const
{
    return hasDefault(comment) ? QMetaType::Type(m_defaultMap[comment.title().toLower()].userType())
                               : QMetaType::UnknownType;
}

QVariant DefaultAnnotationsModel::defaultValue(const Comment &comment) const
{
    return hasDefault(comment) ? m_defaultMap.value(comment.title().toLower()) : QVariant();
}

bool DefaultAnnotationsModel::isRichText(const Comment &comment) const
{
    const auto type = defaultType(comment);
    return type == QMetaType::UnknownType || type == qMetaTypeId<RichTextProxy>();
}

void DefaultAnnotationsModel::loadFromFile(QString const &filename)
{
    QFile file(filename);
    if (file.open(QFile::ReadOnly)) {
        loadFromFile(&file);
    }
}

void DefaultAnnotationsModel::loadFromFile(QIODevice *io)
{
    QJsonParseError error;
    auto doc = QJsonDocument::fromJson(io->readAll(), &error);

    if (error.error == QJsonParseError::NoError)
        loadFromJson(doc);
    else {
    } // TODO: Error handling
}

void DefaultAnnotationsModel::loadFromJson(const QJsonDocument &doc)
{
    beginResetModel();
    m_defaultMap = asVariantMapFromJson(doc);
    m_defaults.clear();
    m_defaults.reserve(m_defaultMap.size());

    for (auto &key : m_defaultMap.keys())
        m_defaults.emplace_back(key, m_defaultMap.value(key));

    endResetModel();
}

QVariantMap DefaultAnnotationsModel::asVariantMapFromJson(const QJsonDocument &doc)
{
    QVariantMap map;
    QJsonObject obj = doc.object();
    for (auto key : obj.keys()) {
        key = key.toLower();
        auto val = obj[key];

        switch (val.type()) {
        case QJsonValue::Double:
            map[key] = double{0.0};
            break;
        case QJsonValue::Bool:
            map[key] = false;
            break;
        case QJsonValue::Object: {
            auto o = val.toObject();
            auto type = o["type"].toString().toLower();
            auto val = o["value"].toVariant();

            if (type == QStringLiteral("richtext"))
                map[key] = QVariant::fromValue(RichTextProxy{val.toString()});
            else if (type == QStringLiteral("string"))
                map[key] = QVariant::fromValue(val.toString());
            else if (type == QStringLiteral("bool"))
                map[key] = QVariant::fromValue(val.toBool());
            else if (type == QStringLiteral("double"))
                map[key] = QVariant::fromValue(val.toDouble());
            else if (type == QStringLiteral("color"))
                map[key] = QVariant::fromValue(QColor(val.toString()));
            break;
        }
        case QJsonValue::String:
            map[key] = QString{};
            break;
        default:
            break;
        }
    }

    return map;
}

QString DefaultAnnotationsModel::defaultJsonFilePath()
{
    return QStringLiteral(":/annotationeditor/defaultannotations.json");
}

QString RichTextProxy::plainText() const
{
    QString plainText(text);
    plainText.remove(QRegularExpression("<.*?>"));
    return plainText.mid(plainText.indexOf("}") + 1);
}

} // namespace QmlDesigner