Debian Codegraph @

utils/fgqcanvas/localprop.cpp

raw symbols SCIP
1 //
2 // Copyright (C) 2017 James Turner [email protected]
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 #include "localprop.h"
19
20 #include <QJsonValue>
21 #include <QDebug>
22
23 QDataStream& operator<<(QDataStream& stream, const NameIndexTuple& nameIndex)
24 {
25 stream << nameIndex.name << nameIndex.index;
26 return stream;
27 }
28
29 QDataStream& operator>>(QDataStream& stream, NameIndexTuple& nameIndex)
30 {
31 stream >> nameIndex.name >> nameIndex.index;
32 return stream;
33 }
34
35 LocalProp *LocalProp::getOrCreateWithPath(const QByteArray &path, QVariant defaultValue)
36 {
37 if (path.isEmpty()) {
38 return this;
39 }
40
41 QList<QByteArray> segments = path.split('/');
42 LocalProp* result = this;
43 while (segments.size() > 1) {
44 QByteArray nameIndex = segments.front();
45 result = result->getOrCreateChildWithNameAndIndex(nameIndex);
46 segments.pop_front();
47 }
48
49 // for the final segment, pass the default value
50 if (!segments.empty()) {
51 result = result->getOrCreateChildWithNameAndIndex(segments.front(), defaultValue);
52 }
53
54 return result;
55 }
56
57 LocalProp *LocalProp::getWithPath(const QByteArray &path) const
58 {
59 if (path.isEmpty()) {
60 return const_cast<LocalProp*>(this);
61 }
62
63 QList<QByteArray> segments = path.split('/');
64 LocalProp* result = const_cast<LocalProp*>(this);
65 while (!segments.empty()) {
66 QByteArray nameIndex = segments.front();
67 result = result->childWithNameAndIndex(nameIndex);
68 segments.pop_front();
69 if (!result) {
70 return nullptr;
71 }
72 }
73
74 return result;
75 }
76
77 static bool lessThanPropNameIndex(const LocalProp* prop, const NameIndexTuple& ni)
78 {
79 return prop->id() < ni;
80 }
81
82
83 LocalProp::LocalProp(LocalProp *pr, const NameIndexTuple& ni) :
84 QObject(pr),
85 _id(ni),
86 _parent(pr)
87 {
88 }
89
90 LocalProp::~LocalProp()
91 {
92 for (auto c : _children) {
93 delete c;
94 }
95 }
96
97 void LocalProp::processChange(QJsonValue json)
98 {
99 QVariant newValue = json.toVariant();
100 if (newValue != _value) {
101 _value = newValue;
102 emit valueChanged(_value);
103 }
104 }
105
106 const NameIndexTuple &LocalProp::id() const
107 {
108 return _id;
109 }
110
111 QByteArray LocalProp::path() const
112 {
113 if (_parent) {
114 return _parent->path() + '/' + _id.toString();
115 }
116
117 return _id.toString();
118 }
119
120 LocalProp *LocalProp::childWithNameAndIndex(const NameIndexTuple& ni) const
121 {
122 auto it = std::lower_bound(_children.begin(), _children.end(), ni, lessThanPropNameIndex);
123 if ((it != _children.end()) && ((*it)->id() == ni)) {
124 return *it;
125 }
126
127 return nullptr;
128 }
129
130 bool LocalProp::hasChild(const char* name) const
131 {
132 return childWithNameAndIndex(QByteArray::fromRawData(name, strlen(name))) != nullptr;
133 }
134
135 void LocalProp::changeValue(const char *path, QVariant value)
136 {
137 LocalProp* p = getOrCreateWithPath(path);
138 p->_value = value;
139 p->valueChanged(value);
140 }
141
142 void LocalProp::saveToStream(QDataStream &stream) const
143 {
144 stream << _id << _position << _value;
145 stream << static_cast<int>(_children.size());
146 for (auto child : _children) {
147 child->saveToStream(stream);
148 }
149 }
150
151 LocalProp* LocalProp::restoreFromStream(QDataStream &stream, LocalProp* parent)
152 {
153 NameIndexTuple id;
154 stream >> id;
155 LocalProp* prop = new LocalProp(parent, id);
156 stream >> prop->_position >> prop->_value;
157 int childCount;
158 stream >> childCount;
159 for (int c=0; c< childCount; ++c) {
160 prop->_children.push_back(restoreFromStream(stream, prop));
161 }
162
163 return prop;
164 }
165
166 void LocalProp::recursiveNotifyRestored()
167 {
168 emit valueChanged(_value);
169 for (auto child : _children) {
170 emit childAdded(child);
171 }
172
173 for (auto cc : _children) {
174 cc->recursiveNotifyRestored();
175 }
176 }
177
178 LocalProp *LocalProp::getOrCreateChildWithNameAndIndex(const NameIndexTuple& ni,
179 QVariant defaultValue)
180 {
181 auto it = std::lower_bound(_children.begin(), _children.end(), ni, lessThanPropNameIndex);
182 if ((it != _children.end()) && ((*it)->id() == ni)) {
183 return *it;
184 }
185
186 LocalProp* newChild = new LocalProp(this, ni);
187 newChild->_value = defaultValue;
188 _children.insert(it, newChild);
189 emit childAdded(newChild);
190 return newChild;
191 }
192
193 LocalProp *LocalProp::getOrCreateWithPath(const char *name)
194 {
195 return getOrCreateWithPath(QByteArray::fromRawData(name, strlen(name)));
196 }
197
198 LocalProp *LocalProp::getWithPath(const char *name) const
199 {
200 return getWithPath(QByteArray::fromRawData(name, strlen(name)));
201 }
202
203 QByteArray LocalProp::name() const
204 {
205 return _id.name;
206 }
207
208 unsigned int LocalProp::index() const
209 {
210 return _id.index;
211 }
212
213 void LocalProp::setPosition(unsigned int pos)
214 {
215 _position = pos;
216 }
217
218 LocalProp *LocalProp::parent() const
219 {
220 return const_cast<LocalProp*>(_parent);
221 }
222
223 std::vector<QVariant> LocalProp::valuesOfChildren(const char *name) const
224 {
225 std::vector<QVariant> result;
226
227 for (LocalProp* c : childrenWithName(name)) {
228 result.push_back(c->value());
229 }
230
231 return result;
232 }
233
234 std::vector<LocalProp *> LocalProp::childrenWithName(const char *name) const
235 {
236 std::vector<LocalProp *> result;
237 for (LocalProp* child : _children) {
238 if (child->_id.name == name)
239 result.push_back(child);
240 }
241 return result;
242 }
243
244 QVariant LocalProp::value() const
245 {
246 return _value;
247 }
248
249 QVariant LocalProp::value(const char *path, QVariant defaultValue) const
250 {
251 LocalProp* n = getWithPath(path);
252 if (!n || n->value().isNull()) {
253 return defaultValue;
254 }
255
256 return n->value();
257 }
258
259 void LocalProp::removeChild(LocalProp *prop)
260 {
261 Q_ASSERT(prop->parent() == this);
262 auto it = std::find(_children.begin(), _children.end(), prop);
263 Q_ASSERT(it != _children.end());
264 _children.erase(it);
265 emit childRemoved(prop);
266 delete prop;
267 }