libdrmconf 0.14.1
A library to program DMR radios.
Loading...
Searching...
No Matches
configobject.hh
1#ifndef CONFIGOBJECT_HH
2#define CONFIGOBJECT_HH
3
4#include <QObject>
5#include <QString>
6#include <QHash>
7#include <QVector>
8#include <QMetaProperty>
9
10#include <yaml-cpp/yaml.h>
11
12#include "errorstack.hh"
13
14// Forward declaration
15class Config;
16class ConfigObject;
17class ConfigExtension;
18
20template <class T>
21bool propIsInstance(const QMetaProperty &prop) {
22 if (QMetaType::UnknownType == prop.typeId())
23 return false;
24 QMetaType type = prop.metaType();
25 if (! (QMetaType::PointerToQObject & type.flags()))
26 return false;
27 return type.metaObject()->inherits(&T::staticMetaObject);
28}
29
30
34class ConfigItem : public QObject
35{
36 Q_OBJECT
37
38public:
42 class Context
43 {
44 public:
46 Context();
48 virtual ~Context();
49
51 const QString &version() const;
53 void setVersion(const QString &ver);
54
56 virtual bool contains(ConfigObject *obj) const;
58 virtual bool contains(const QString &id) const;
59
61 virtual QString getId(ConfigObject *obj) const;
63 virtual ConfigObject *getObj(const QString &id) const;
64
66 virtual bool add(const QString &id, ConfigObject *);
67
69 static bool tagIsSet(const QString &className, const QString &property, const QString &tag);
71 static bool hasTag(const QString &className, const QString &property, QVariant value);
73 static QVariant tagGetValue(const QString &className, const QString &property, const QString &tag);
75 static QString getTag(const QString &className, const QString &property, QVariant value);
77 static void setTag(const QString &className, const QString &property, const QString &tag, QVariant value);
78
79 protected:
81 QString _version;
83 QHash<QString, ConfigObject *> _objects;
85 QHash<ConfigObject*, QString> _ids;
87 static QHash<QString, QList<QPair<QString, QVariant>>> _tags;
88 };
89
90protected:
93 explicit ConfigItem(QObject *parent = nullptr);
94
95public:
99 virtual bool copy(const ConfigItem &other);
100
102 virtual ConfigItem *clone() const = 0;
103
110 virtual int compare(const ConfigItem &other) const;
111
112public:
115 virtual bool label(Context &context, const ErrorStack &err=ErrorStack());
118 virtual YAML::Node serialize(const Context &context, const ErrorStack &err=ErrorStack());
119
123 virtual ConfigItem *allocateChild(QMetaProperty &prop, const YAML::Node &node,
124 const Context &ctx, const ErrorStack &err=ErrorStack());
126 virtual bool parse(const YAML::Node &node, Context &ctx, const ErrorStack &err=ErrorStack());
128 virtual bool link(const YAML::Node &node, const Context &ctx, const ErrorStack &err=ErrorStack());
129
131 virtual void clear();
132
134 virtual const Config *config() const;
136 virtual void findItemsOfTypes(const QStringList &typeNames, QSet<ConfigItem*> &items) const;
137
139 template <class Object>
140 bool is() const {
141 return nullptr != qobject_cast<const Object*>(this);
142 }
143
145 template <class Object>
146 const Object *as() const {
147 return qobject_cast<const Object*>(this);
148 }
149
151 template <class Object>
152 Object *as() {
153 return qobject_cast<Object *>(this);
154 }
155
157 bool hasDescription() const;
159 bool hasLongDescription() const;
161 bool hasDescription(const QMetaProperty &prop) const;
163 bool hasLongDescription(const QMetaProperty &prop) const;
165 QString description() const;
167 QString longDescription() const;
169 QString description(const QMetaProperty &prop) const;
171 QString longDescription(const QMetaProperty &prop) const;
172
173protected:
176 virtual bool populate(YAML::Node &node, const Context &context, const ErrorStack &err=ErrorStack());
177
178signals:
185 void endClear();
186};
187
188
192{
193 Q_OBJECT
194
196 Q_PROPERTY(QString name READ name WRITE setName)
197
198
199 Q_CLASSINFO("IdPrefix", "obj")
200
201protected:
204 ConfigObject(QObject *parent = nullptr);
205
209 ConfigObject(const QString &name, QObject *parent = nullptr);
210
211public:
213 virtual const QString &name() const;
215 virtual void setName(const QString &name);
216
217public:
219 QString idPrefix() const;
220 bool label(Context &context, const ErrorStack &err=ErrorStack());
221 bool parse(const YAML::Node &node, Context &ctx, const ErrorStack &err=ErrorStack());
222
223protected:
224 virtual bool populate(YAML::Node &node, const Context &context, const ErrorStack &err=ErrorStack());
225
227 static QString findIdPrefix(const QMetaObject* meta);
228
229protected:
231 QString _name;
232};
233
234
239{
240 Q_OBJECT
241
242protected:
244 explicit ConfigExtension(QObject *parent=nullptr);
245};
246
247
250class AbstractConfigObjectList: public QObject
251{
252 Q_OBJECT
253
254protected:
256 explicit AbstractConfigObjectList(const QMetaObject &elementTypes=ConfigObject::staticMetaObject, QObject *parent = nullptr);
258 AbstractConfigObjectList(const std::initializer_list<QMetaObject> &elementTypes, QObject *parent=nullptr);
259
260public:
262 virtual bool copy(const AbstractConfigObjectList &other);
263
265 virtual bool label(ConfigItem::Context &context, const ErrorStack &err=ErrorStack()) = 0;
268 virtual YAML::Node serialize(const ConfigItem::Context &context, const ErrorStack &err=ErrorStack()) = 0;
269
271 virtual int count() const;
273 virtual int indexOf(ConfigObject *obj) const;
275 virtual void clear();
276
278 virtual const Config *config() const;
280 virtual void findItemsOfTypes(const QStringList &typeNames, QSet<ConfigItem*> &items) const;
282 virtual QList<ConfigObject *> findItemsByName(const QString name) const;
283
285 virtual bool has(ConfigObject *obj) const;
287 virtual ConfigObject *get(int idx) const;
289 virtual int add(ConfigObject *obj, int row=-1, bool unique=true);
291 virtual int replace(ConfigObject *obj, int row, bool unique=true);
293 virtual bool take(ConfigObject *obj);
295 virtual bool del(ConfigObject *obj);
296
298 virtual bool moveUp(int idx);
300 virtual bool moveUp(int first, int last);
302 virtual bool moveDown(int idx);
304 virtual bool moveDown(int first, int last);
308 virtual bool move(int source, int count, int destination);
309
311 const QList<QMetaObject> &elementTypes() const;
313 QStringList classNames() const;
314
315signals:
317 void elementAdded(int idx);
319 void elementModified(int idx);
321 void elementRemoved(int idx);
322
323private slots:
325 void onElementModified(ConfigItem *obj);
327 void onElementDeleted(QObject *obj);
328
329protected:
331 QList<QMetaObject> _elementTypes;
333 QVector<ConfigObject *> _items;
334};
335
336
342{
343 Q_OBJECT
344
345protected:
347 explicit ConfigObjectList(const QMetaObject &elementTypes=ConfigItem::staticMetaObject, QObject *parent = nullptr);
349 ConfigObjectList(const std::initializer_list<QMetaObject> &elementTypes, QObject *parent=nullptr);
350
351public:
352 int add(ConfigObject *obj, int row=-1, bool unique=true);
353 bool take(ConfigObject *obj);
354 bool del(ConfigObject *obj);
355 void clear();
356 bool copy(const AbstractConfigObjectList &other);
357
364 virtual int compare(const ConfigObjectList &other) const;
365
367 virtual ConfigItem *allocateChild(const YAML::Node &node, ConfigItem::Context &ctx, const ErrorStack &err=ErrorStack()) = 0;
369 virtual bool parse(const YAML::Node &node, ConfigItem::Context &ctx, const ErrorStack &err=ErrorStack());
371 virtual bool link(const YAML::Node &node, const ConfigItem::Context &ctx, const ErrorStack &err=ErrorStack());
372
373 bool label(ConfigItem::Context &context, const ErrorStack &err=ErrorStack());
374 YAML::Node serialize(const ConfigItem::Context &context, const ErrorStack &err=ErrorStack());
375};
376
377
383{
384 Q_OBJECT
385
386protected:
388 explicit ConfigObjectRefList(const QMetaObject &elementTypes=ConfigObject::staticMetaObject, QObject *parent = nullptr);
390 ConfigObjectRefList(const std::initializer_list<QMetaObject> &elementTypes, QObject *parent=nullptr);
391
392public:
393 bool label(ConfigItem::Context &context, const ErrorStack &err=ErrorStack());
394 YAML::Node serialize(const ConfigItem::Context &context, const ErrorStack &err=ErrorStack());
395
402 virtual int compare(const ConfigObjectRefList &other) const;
403};
404
405
406#endif // CONFIGOBJECT_HH
virtual int indexOf(ConfigObject *obj) const
Returns the index of the given object within the list.
Definition configobject.cc:1271
QStringList classNames() const
Returns a list of all class names.
Definition configobject.cc:1481
virtual bool move(int source, int count, int destination)
Moves the given source range to the destination index.
Definition configobject.cc:1451
QVector< ConfigObject * > _items
Holds the list items.
Definition configobject.hh:333
void elementAdded(int idx)
Gets emitted if an element was added to the list.
virtual YAML::Node serialize(const ConfigItem::Context &context, const ErrorStack &err=ErrorStack())=0
Recursively serializes the configuration to YAML nodes.
QList< QMetaObject > _elementTypes
Holds the static QMetaObject of the element type.
Definition configobject.hh:331
virtual void clear()
Clears the list.
Definition configobject.cc:1276
virtual int add(ConfigObject *obj, int row=-1, bool unique=true)
Adds an element to the list.
Definition configobject.cc:1322
virtual const Config * config() const
Returns the config object, this list belongs to.
Definition configobject.cc:1284
void elementRemoved(int idx)
Gets emitted if one of the lists elements gets deleted.
virtual bool has(ConfigObject *obj) const
Returns true, if the list contains the given object.
Definition configobject.cc:1312
virtual bool label(ConfigItem::Context &context, const ErrorStack &err=ErrorStack())=0
Recursively labels the config object.
virtual bool moveUp(int idx)
Moves an object at index idx one step up.
Definition configobject.cc:1416
virtual QList< ConfigObject * > findItemsByName(const QString name) const
Searches the list for objects with the given name.
Definition configobject.cc:1302
virtual bool take(ConfigObject *obj)
Removes an element from the list.
Definition configobject.cc:1396
virtual int count() const
Returns the number of elements in the list.
Definition configobject.cc:1266
virtual ConfigObject * get(int idx) const
Returns the list element at the given index or nullptr if out of bounds.
Definition configobject.cc:1317
virtual bool copy(const AbstractConfigObjectList &other)
Copies all elements from other to this list.
Definition configobject.cc:1257
virtual void findItemsOfTypes(const QStringList &typeNames, QSet< ConfigItem * > &items) const
Searches the config tree to find all instances of the given type names.
Definition configobject.cc:1293
void elementModified(int idx)
Gets emitted if one of the lists elements gets modified.
virtual bool moveDown(int idx)
Moves an object at index idx one step down.
Definition configobject.cc:1433
AbstractConfigObjectList(const QMetaObject &elementTypes=ConfigObject::staticMetaObject, QObject *parent=nullptr)
Hidden constructor.
Definition configobject.cc:1244
virtual int replace(ConfigObject *obj, int row, bool unique=true)
Replaces an element in the list.
Definition configobject.cc:1353
virtual bool del(ConfigObject *obj)
Removes an element from the list (and deletes it if owned).
Definition configobject.cc:1411
const QList< QMetaObject > & elementTypes() const
Returns the element type for this list.
Definition configobject.cc:1476
Base class of all device/vendor specific configuration extensions.
Definition configobject.hh:239
ConfigExtension(QObject *parent=nullptr)
Hidden constructor.
Definition configobject.cc:1234
Parse context for config objects.
Definition configobject.hh:43
static QHash< QString, QList< QPair< QString, QVariant > > > _tags
Set of tag-value pairs for all properties.
Definition configobject.hh:87
static void setTag(const QString &className, const QString &property, const QString &tag, QVariant value)
Associates the given object with the tag for the property of the given class.
Definition configobject.cc:136
static bool tagIsSet(const QString &className, const QString &property, const QString &tag)
Returns true if the property of the class has the specified tag associated.
Definition configobject.cc:88
static bool hasTag(const QString &className, const QString &property, QVariant value)
Returns true if the property of the class has the specified object as a tag associated.
Definition configobject.cc:100
QString _version
The version string.
Definition configobject.hh:81
const QString & version() const
Returns the read version string.
Definition configobject.cc:50
virtual bool add(const QString &id, ConfigObject *)
Associates the given object with the given ID.
Definition configobject.cc:79
void setVersion(const QString &ver)
Sets the version string.
Definition configobject.cc:54
QHash< QString, ConfigObject * > _objects
ID->OBJ look-up table.
Definition configobject.hh:83
Context()
Empty constructor.
Definition configobject.cc:39
virtual ~Context()
Destructor.
Definition configobject.cc:45
static QString getTag(const QString &className, const QString &property, QVariant value)
Returns the tag associated with the object for the property of the class.
Definition configobject.cc:124
virtual QString getId(ConfigObject *obj) const
Returns ID of the given object.
Definition configobject.cc:69
virtual ConfigObject * getObj(const QString &id) const
Returns the object for the given ID.
Definition configobject.cc:74
QHash< ConfigObject *, QString > _ids
OBJ->ID look-up table.
Definition configobject.hh:85
static QVariant tagGetValue(const QString &className, const QString &property, const QString &tag)
Returns the object associated with the tag for the property of the class.
Definition configobject.cc:112
virtual bool contains(ConfigObject *obj) const
Returns true, if the context contains the given object.
Definition configobject.cc:59
Base class for all configuration objects (channels, zones, contacts, etc).
Definition configobject.hh:35
virtual bool link(const YAML::Node &node, const Context &ctx, const ErrorStack &err=ErrorStack())
Links the given object to the rest of the codeplug using the given context.
Definition configobject.cc:887
virtual int compare(const ConfigItem &other) const
Compares the items.
Definition configobject.cc:267
bool hasLongDescription() const
Returns true if there is a class info "longDescription" for this instance.
Definition configobject.cc:1080
virtual bool copy(const ConfigItem &other)
Copies the given item into this one.
Definition configobject.cc:165
Object * as()
Casts this object to the given type.
Definition configobject.hh:152
virtual bool populate(YAML::Node &node, const Context &context, const ErrorStack &err=ErrorStack())
Recursively serializes the configuration to YAML nodes.
Definition configobject.cc:433
QString longDescription() const
Returns the long description of this instance if set by a class info.
Definition configobject.cc:1112
virtual ConfigItem * clone() const =0
Clones this item.
bool hasDescription() const
Returns true if there is a class info "description" for this instance.
Definition configobject.cc:1074
virtual void findItemsOfTypes(const QStringList &typeNames, QSet< ConfigItem * > &items) const
Searches the config tree to find all instances of the given type names.
Definition configobject.cc:1051
virtual ConfigItem * allocateChild(QMetaProperty &prop, const YAML::Node &node, const Context &ctx, const ErrorStack &err=ErrorStack())
Allocates an instance for the given property on the given YAML node.
Definition configobject.cc:542
bool is() const
Returns true if this object is of class Object.
Definition configobject.hh:140
virtual bool label(Context &context, const ErrorStack &err=ErrorStack())
Recursively labels the config object.
Definition configobject.cc:381
virtual const Config * config() const
Returns the config, the item belongs to or nullptr if not part of a config.
Definition configobject.cc:1040
virtual YAML::Node serialize(const Context &context, const ErrorStack &err=ErrorStack())
Recursively serializes the configuration to YAML nodes.
Definition configobject.cc:405
virtual void clear()
Clears the config object.
Definition configobject.cc:413
const Object * as() const
Casts this object to the given type.
Definition configobject.hh:146
virtual bool parse(const YAML::Node &node, Context &ctx, const ErrorStack &err=ErrorStack())
Parses the given YAML node, updates the given object and updates the given context (IDs).
Definition configobject.cc:570
QString description() const
Returns the description of this instance if set by a class info.
Definition configobject.cc:1104
void endClear()
Gets emitted after clearing the item.
ConfigItem(QObject *parent=nullptr)
Hidden constructor.
Definition configobject.cc:158
void modified(ConfigItem *obj)
Gets emitted once the config object is modified.
void beginClear()
Gets emitted before clearing the item.
bool take(ConfigObject *obj)
Removes an element from the list.
Definition configobject.cc:1615
int add(ConfigObject *obj, int row=-1, bool unique=true)
Adds an element to the list.
Definition configobject.cc:1608
void clear()
Clears the list.
Definition configobject.cc:1629
ConfigObjectList(const QMetaObject &elementTypes=ConfigItem::staticMetaObject, QObject *parent=nullptr)
Hidden constructor.
Definition configobject.cc:1511
virtual int compare(const ConfigObjectList &other) const
Compares the object lists.
Definition configobject.cc:1646
virtual bool parse(const YAML::Node &node, ConfigItem::Context &ctx, const ErrorStack &err=ErrorStack())
Parses the list from the YAML node.
Definition configobject.cc:1545
bool copy(const AbstractConfigObjectList &other)
Copies all elements from other to this list.
Definition configobject.cc:1637
virtual bool link(const YAML::Node &node, const ConfigItem::Context &ctx, const ErrorStack &err=ErrorStack())
Links the list from the given YAML node.
Definition configobject.cc:1579
YAML::Node serialize(const ConfigItem::Context &context, const ErrorStack &err=ErrorStack())
Recursively serializes the configuration to YAML nodes.
Definition configobject.cc:1533
bool del(ConfigObject *obj)
Removes an element from the list (and deletes it if owned).
Definition configobject.cc:1622
virtual ConfigItem * allocateChild(const YAML::Node &node, ConfigItem::Context &ctx, const ErrorStack &err=ErrorStack())=0
Allocates a member objects for the given YAML node.
bool label(ConfigItem::Context &context, const ErrorStack &err=ErrorStack())
Recursively labels the config object.
Definition configobject.cc:1524
bool label(ConfigItem::Context &context, const ErrorStack &err=ErrorStack())
Recursively labels the config object.
Definition configobject.cc:1675
YAML::Node serialize(const ConfigItem::Context &context, const ErrorStack &err=ErrorStack())
Recursively serializes the configuration to YAML nodes.
Definition configobject.cc:1682
ConfigObjectRefList(const QMetaObject &elementTypes=ConfigObject::staticMetaObject, QObject *parent=nullptr)
Hidden constructor.
Definition configobject.cc:1662
virtual int compare(const ConfigObjectRefList &other) const
Compares the object ref lists.
Definition configobject.cc:1695
Base class of all labeled and named objects.
Definition configobject.hh:192
virtual bool populate(YAML::Node &node, const Context &context, const ErrorStack &err=ErrorStack())
Recursively serializes the configuration to YAML nodes.
Definition configobject.cc:1212
bool parse(const YAML::Node &node, Context &ctx, const ErrorStack &err=ErrorStack())
Parses the given YAML node, updates the given object and updates the given context (IDs).
Definition configobject.cc:1194
ConfigObject(QObject *parent=nullptr)
Specifies the prefix for every ID assigned to every object during serialization.
Definition configobject.cc:1141
bool label(Context &context, const ErrorStack &err=ErrorStack())
Recursively labels the config object.
Definition configobject.cc:1172
virtual void setName(const QString &name)
Sets the name of the object.
Definition configobject.cc:1159
QString _name
Holds the name of the object.
Definition configobject.hh:231
QString name
The name of the object.
Definition configobject.hh:196
QString idPrefix() const
Returns the ID prefix for this object.
Definition configobject.cc:1167
static QString findIdPrefix(const QMetaObject *meta)
Helper to find the IdPrefix class info in the class hierarchy.
Definition configobject.cc:1219
The config class, representing the codeplug configuration.
Definition config.hh:70
Implements a stack of error messages to provide a pretty formatted error traceback.
Definition errorstack.hh:43