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

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

sync with github

File size: 3.1 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 "qjsonitem.h"
27
28QJsonTreeItem::QJsonTreeItem(QJsonTreeItem *parent)
29    : mParent( parent )
30    , mType( QJsonValue::Type::Null )
31{
32}
33
34QJsonTreeItem::~QJsonTreeItem()
35{
36    qDeleteAll(mChilds);
37
38}
39
40void QJsonTreeItem::appendChild(QJsonTreeItem *item)
41{
42    mChilds.append(item);
43}
44
45QJsonTreeItem *QJsonTreeItem::child(int row)
46{
47    return mChilds.value(row);
48}
49
50QJsonTreeItem *QJsonTreeItem::parent()
51{
52    return mParent;
53}
54
55int QJsonTreeItem::childCount() const
56{
57    return mChilds.count();
58}
59
60int QJsonTreeItem::row() const
61{
62    if (mParent)
63        return mParent->mChilds.indexOf(const_cast<QJsonTreeItem*>(this));
64
65    return 0;
66}
67
68void QJsonTreeItem::setKey(const QString &key)
69{
70    mKey = key;
71}
72
73void QJsonTreeItem::setValue(const QString &value)
74{
75    mValue = value;
76}
77
78void QJsonTreeItem::setType(const QJsonValue::Type &type)
79{
80    mType = type;
81}
82
83QString QJsonTreeItem::key() const
84{
85    return mKey;
86}
87
88QString QJsonTreeItem::value() const
89{
90    return mValue;
91}
92
93QJsonValue::Type QJsonTreeItem::type() const
94{
95    return mType;
96}
97
98QJsonTreeItem* QJsonTreeItem::load(const QJsonValue& value, QJsonTreeItem* parent)
99{
100
101
102    QJsonTreeItem * rootItem = new QJsonTreeItem(parent);
103    rootItem->setKey("root");
104
105    if ( value.isObject())
106    {
107
108        //Get all QJsonValue childs
109        foreach (QString key , value.toObject().keys()){
110            QJsonValue v = value.toObject().value(key);
111            QJsonTreeItem * child = load(v,rootItem);
112            child->setKey(key);
113            child->setType(v.type());
114            rootItem->appendChild(child);
115
116        }
117
118    }
119
120    else if ( value.isArray())
121    {
122        //Get all QJsonValue childs
123        int index = 0;
124        foreach (QJsonValue v , value.toArray()){
125
126            QJsonTreeItem * child = load(v,rootItem);
127            child->setKey(QString::number(index));
128            child->setType(v.type());
129            rootItem->appendChild(child);
130            ++index;
131        }
132    }
133    else
134    {
135        rootItem->setValue(value.toVariant().toString());
136        rootItem->setType(value.type());
137    }
138
139    return rootItem;
140}
141
Note: See TracBrowser for help on using the repository browser.