| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#ifndef LOCALPROP_H |
| 19 |
#define LOCALPROP_H |
| 20 |
|
| 21 |
#include <QByteArray> |
| 22 |
#include <QVariant> |
| 23 |
#include <QVector> |
| 24 |
#include <QObject> |
| 25 |
#include <QDataStream> |
| 26 |
|
| 27 |
struct NameIndexTuple |
| 28 |
{ |
| 29 |
QByteArray name; |
| 30 |
unsigned int index = 0; |
| 31 |
|
| 32 |
NameIndexTuple() |
| 33 |
{} |
| 34 |
|
| 35 |
NameIndexTuple(const char* nm, unsigned int idx) : |
| 36 |
name(nm), |
| 37 |
index(idx) |
| 38 |
{} |
| 39 |
|
| 40 |
NameIndexTuple(const QByteArray& bytes) : |
| 41 |
name(bytes) |
| 42 |
{ |
| 43 |
Q_ASSERT(bytes.indexOf('/') == -1); |
| 44 |
if (bytes.endsWith(']')) { |
| 45 |
int leftBracket = bytes.indexOf('['); |
| 46 |
index = bytes.mid(leftBracket + 1, bytes.length() - (leftBracket + 2)).toInt(); |
| 47 |
name = bytes.left(leftBracket); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
QByteArray toString() const |
| 52 |
{ |
| 53 |
QByteArray p = name; |
| 54 |
if (index > 0) { |
| 55 |
p += '[' + QByteArray::number(index) + ']'; |
| 56 |
} |
| 57 |
return p; |
| 58 |
} |
| 59 |
|
| 60 |
bool operator==(const NameIndexTuple& other) const |
| 61 |
{ |
| 62 |
return (name == other.name) && (index == other.index); |
| 63 |
} |
| 64 |
|
| 65 |
bool operator<(const NameIndexTuple& other) const |
| 66 |
{ |
| 67 |
if (name == other.name) { |
| 68 |
return index < other.index; |
| 69 |
} |
| 70 |
|
| 71 |
return name < other.name; |
| 72 |
} |
| 73 |
|
| 74 |
}; |
| 75 |
|
| 76 |
QDataStream& operator<<(QDataStream& stream, const NameIndexTuple& nameIndex); |
| 77 |
QDataStream& operator>>(QDataStream& stream, NameIndexTuple& nameIndex); |
| 78 |
|
| 79 |
class LocalProp : public QObject |
| 80 |
{ |
| 81 |
Q_OBJECT |
| 82 |
public: |
| 83 |
LocalProp(LocalProp* parent, const NameIndexTuple& ni); |
| 84 |
|
| 85 |
virtual ~LocalProp(); |
| 86 |
|
| 87 |
void processChange(QJsonValue newValue); |
| 88 |
|
| 89 |
const NameIndexTuple& id() const; |
| 90 |
|
| 91 |
QByteArray path() const; |
| 92 |
|
| 93 |
LocalProp* getOrCreateWithPath(const QByteArray& path, QVariant defaultValue = {}); |
| 94 |
|
| 95 |
LocalProp* childWithNameAndIndex(const NameIndexTuple& ni) const; |
| 96 |
|
| 97 |
LocalProp* getOrCreateChildWithNameAndIndex(const NameIndexTuple& ni, QVariant defaultValue = {}); |
| 98 |
|
| 99 |
LocalProp* getOrCreateWithPath(const char* name); |
| 100 |
|
| 101 |
LocalProp* getWithPath(const QByteArray& path) const; |
| 102 |
|
| 103 |
LocalProp* getWithPath(const char* name) const; |
| 104 |
|
| 105 |
QByteArray name() const; |
| 106 |
|
| 107 |
unsigned int index() const; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
unsigned int position() const |
| 113 |
{ |
| 114 |
return _position; |
| 115 |
} |
| 116 |
|
| 117 |
void setPosition(unsigned int pos); |
| 118 |
|
| 119 |
LocalProp* parent() const; |
| 120 |
|
| 121 |
std::vector<LocalProp*> children() const |
| 122 |
{ return _children; } |
| 123 |
|
| 124 |
std::vector<QVariant> valuesOfChildren(const char* name) const; |
| 125 |
|
| 126 |
std::vector<LocalProp*> childrenWithName(const char* name) const; |
| 127 |
|
| 128 |
QVariant value() const; |
| 129 |
|
| 130 |
QVariant value(const char* path, QVariant defaultValue) const; |
| 131 |
|
| 132 |
void removeChild(LocalProp* prop); |
| 133 |
|
| 134 |
bool hasChild(const char* name) const; |
| 135 |
|
| 136 |
void changeValue(const char* path, QVariant value); |
| 137 |
|
| 138 |
void saveToStream(QDataStream& stream) const; |
| 139 |
|
| 140 |
static LocalProp* restoreFromStream(QDataStream& stream, LocalProp *parent); |
| 141 |
|
| 142 |
void recursiveNotifyRestored(); |
| 143 |
signals: |
| 144 |
void valueChanged(QVariant val); |
| 145 |
|
| 146 |
void childAdded(LocalProp* child); |
| 147 |
void childRemoved(LocalProp* child); |
| 148 |
|
| 149 |
private: |
| 150 |
const NameIndexTuple _id; |
| 151 |
const LocalProp* _parent; |
| 152 |
std::vector<LocalProp*> _children; |
| 153 |
QVariant _value; |
| 154 |
unsigned int _position = 0; |
| 155 |
}; |
| 156 |
|
| 157 |
#endif |