forked from lemirep/Models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONListItemBinder.cpp
125 lines (107 loc) · 4.75 KB
/
JSONListItemBinder.cpp
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
/****************************************************************************
**
** Copyright (C) Paul Lemire, Tepee3DTeam and/or its subsidiary(-ies).
** Contact: [email protected]
** Contact: [email protected]
**
** This file is part of the Tepee3D project
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
****************************************************************************/
#include "JSONListItemBinder.h"
#include <QDebug>
QHash<QJsonValue::Type,
void (Models::JSONListItemBinder::*)
(const QString &keyName,
const QJsonValue &jsonValue)> Models::JSONListItemBinder::jsonToValue = Models::JSONListItemBinder::initJsonToValueHash();
QHash<QJsonValue::Type,
void (Models::JSONListItemBinder::*)
(const QString &keyName,
const QJsonValue &jsonValue)> Models::JSONListItemBinder::initJsonToValueHash()
{
QHash<QJsonValue::Type,
void (Models::JSONListItemBinder::*)
(const QString &keyName,
const QJsonValue &jsonValue)> hash;
hash[QJsonValue::Null] = &Models::JSONListItemBinder::valueFromNullOrUndefined;
hash[QJsonValue::Undefined] = &Models::JSONListItemBinder::valueFromNullOrUndefined;
hash[QJsonValue::Object] = &Models::JSONListItemBinder::valueFromQJsonObject;
hash[QJsonValue::Array] = &Models::JSONListItemBinder::valueFromQJsonArray;
hash[QJsonValue::Bool] = &Models::JSONListItemBinder::valueFromScalar;
hash[QJsonValue::String] = &Models::JSONListItemBinder::valueFromScalar;
hash[QJsonValue::Double] = &Models::JSONListItemBinder::valueFromScalar;
return hash;
}
Models::JSONListItemBinder::JSONListItemBinder(Models::ListItem *item)
: item(item)
{
}
Models::JSONListItemBinder::~JSONListItemBinder()
{
}
void Models::JSONListItemBinder::fromQJsonValue(const QJsonValue &jsonValue, Models::ListItem *item)
{
if (item == NULL)
return ;
Models::JSONListItemBinder instance(item);
instance.nameToRoles = item->roleTypesFromName();
(instance.*jsonToValue[jsonValue.type()])("", jsonValue);
}
void Models::JSONListItemBinder::valueFromQJsonObject(const QString &keyName, const QJsonValue &jsonValue)
{
QJsonObject obj = jsonValue.toObject();
QStringList keys = obj.keys();
foreach (const QString &keyName, keys)
{
QJsonValue val = obj.value(keyName);
(this->*jsonToValue[val.type()])(keyName, val);
}
}
void Models::JSONListItemBinder::valueFromQJsonArray(const QString &keyName, const QJsonValue &jsonValue)
{
// IF KEYNAME VALUE IS OF QOBJECT* TYPE AND IS A LISTMODEL
// FOR EACH VALUE IN ARRAY CREATE NEW OBJECT OF MODEL->PROTOTYPE->GETNEWITEMINSTANCE
QJsonArray array = jsonValue.toArray();
Models::ListModel *tmpModel = NULL;
if (this->item->data(this->nameToRoles[keyName.toLocal8Bit()]).type() == QMetaType::QObjectStar &&
qobject_cast<Models::SubListedListItem *>(this->item) != NULL &&
(tmpModel = qobject_cast<Models::ListModel*>(
qvariant_cast<QObject *>(
this->item->data(this->nameToRoles[keyName.toLocal8Bit()])))) != NULL)
{
foreach (const QJsonValue &val, array)
{
Models::ListItem *subItem = tmpModel->getPrototype()->getNewItemInstance(this->item);
JSONListItemBinder::fromQJsonValue(val, subItem);
qobject_cast<Models::SubListedListItem *>(this->item)->submodel()->appendRow(subItem);
}
}
else
foreach (const QJsonValue &val, array)
(this->*jsonToValue[val.type()])(keyName, val);
}
void Models::JSONListItemBinder::valueFromScalar(const QString &keyName, const QJsonValue &jsonValue)
{
if (this->nameToRoles.contains(keyName.toLocal8Bit()))
this->item->setData(this->nameToRoles[keyName.toLocal8Bit()], jsonValue.toVariant());
}
void Models::JSONListItemBinder::valueFromNullOrUndefined(const QString &keyName, const QJsonValue &jsonValue)
{
qDebug() << "Value is null or undefined for " << keyName;
}