source: calamares/trunk/fuentes/src/libcalamaresui/utils/qjsonmodel.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 4.4 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   SPDX-License-Identifier: GPL-3.0+
4 *   License-Filename: LICENSES/GPLv3+-QJsonModel
5 */
6
7/***********************************************
8    Copyright (C) 2014  Schutz Sacha
9    This file is part of QJsonModel (https://github.com/dridk/QJsonmodel).
10
11    QJsonModel is free software: you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation, either version 3 of the License, or
14    (at your option) any later version.
15
16    QJsonModel is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with QJsonModel.  If not, see <http://www.gnu.org/licenses/>.
23
24**********************************************/
25
26#include "qjsonmodel.h"
27#include <QFile>
28#include <QDebug>
29#include <QJsonDocument>
30#include <QJsonObject>
31#include <QIcon>
32#include <QFont>
33
34QJsonModel::QJsonModel(QObject *parent) :
35    QAbstractItemModel(parent)
36    , mRootItem( new QJsonTreeItem )
37{
38    mHeaders.append("key");
39    mHeaders.append("value");
40
41
42}
43
44QJsonModel::~QJsonModel()
45{
46    delete mRootItem;
47}
48
49bool QJsonModel::load(const QString &fileName)
50{
51    QFile file(fileName);
52    bool success = false;
53    if (file.open(QIODevice::ReadOnly)) {
54        success = load(&file);
55        file.close();
56    }
57    else success = false;
58
59    return success;
60}
61
62bool QJsonModel::load(QIODevice *device)
63{
64    return loadJson(device->readAll());
65}
66
67bool QJsonModel::loadJson(const QByteArray &json)
68{
69    mDocument = QJsonDocument::fromJson(json);
70
71    if (!mDocument.isNull())
72    {
73        beginResetModel();
74        delete mRootItem;
75        if (mDocument.isArray()) {
76            mRootItem = QJsonTreeItem::load(QJsonValue(mDocument.array()));
77        } else {
78            mRootItem = QJsonTreeItem::load(QJsonValue(mDocument.object()));
79        }
80        endResetModel();
81        return true;
82    }
83    return false;
84}
85
86
87QVariant QJsonModel::data(const QModelIndex &index, int role) const
88{
89
90    if (!index.isValid())
91        return QVariant();
92
93
94    QJsonTreeItem *item = static_cast<QJsonTreeItem*>(index.internalPointer());
95
96
97    if ((role == Qt::DecorationRole) && (index.column() == 0)){
98
99        return mTypeIcons.value(item->type());
100    }
101
102
103    if (role == Qt::DisplayRole) {
104
105        if (index.column() == 0)
106            return QString("%1").arg(item->key());
107
108        if (index.column() == 1)
109            return QString("%1").arg(item->value());
110    }
111
112
113
114    return QVariant();
115
116}
117
118QVariant QJsonModel::headerData(int section, Qt::Orientation orientation, int role) const
119{
120    if (role != Qt::DisplayRole)
121        return QVariant();
122
123    if (orientation == Qt::Horizontal) {
124
125        return mHeaders.value(section);
126    }
127    else
128        return QVariant();
129}
130
131QModelIndex QJsonModel::index(int row, int column, const QModelIndex &parent) const
132{
133    if (!hasIndex(row, column, parent))
134        return QModelIndex();
135
136    QJsonTreeItem *parentItem;
137
138    if (!parent.isValid())
139        parentItem = mRootItem;
140    else
141        parentItem = static_cast<QJsonTreeItem*>(parent.internalPointer());
142
143    QJsonTreeItem *childItem = parentItem->child(row);
144    if (childItem)
145        return createIndex(row, column, childItem);
146    else
147        return QModelIndex();
148}
149
150QModelIndex QJsonModel::parent(const QModelIndex &index) const
151{
152    if (!index.isValid())
153        return QModelIndex();
154
155    QJsonTreeItem *childItem = static_cast<QJsonTreeItem*>(index.internalPointer());
156    QJsonTreeItem *parentItem = childItem->parent();
157
158    if (parentItem == mRootItem)
159        return QModelIndex();
160
161    return createIndex(parentItem->row(), 0, parentItem);
162}
163
164int QJsonModel::rowCount(const QModelIndex &parent) const
165{
166    QJsonTreeItem *parentItem;
167    if (parent.column() > 0)
168        return 0;
169
170    if (!parent.isValid())
171        parentItem = mRootItem;
172    else
173        parentItem = static_cast<QJsonTreeItem*>(parent.internalPointer());
174
175    return parentItem->childCount();
176}
177
178int QJsonModel::columnCount(const QModelIndex &parent) const
179{
180    Q_UNUSED(parent)
181    return 2;
182}
183
184void QJsonModel::setIcon(const QJsonValue::Type &type, const QIcon &icon)
185{
186    mTypeIcons.insert(type,icon);
187}
Note: See TracBrowser for help on using the repository browser.