libdrmconf 0.13.3
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{
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 hasTag(const QString &className, const QString &property, const QString &tag);
71 static bool hasTag(const QString &className, const QString &property, ConfigObject *obj);
73 static ConfigObject *getTag(const QString &className, const QString &property, const QString &tag);
75 static QString getTag(const QString &className, const QString &property, ConfigObject *obj);
77 static void setTag(const QString &className, const QString &property, const QString &tag, ConfigObject *obj);
78
79 protected:
90 };
91
92protected:
95 explicit ConfigItem(QObject *parent = nullptr);
96
97public:
101 virtual bool copy(const ConfigItem &other);
102
104 virtual ConfigItem *clone() const = 0;
105
112 virtual int compare(const ConfigItem &other) const;
113
114public:
117 virtual bool label(Context &context, const ErrorStack &err=ErrorStack());
120 virtual YAML::Node serialize(const Context &context, const ErrorStack &err=ErrorStack());
121
125 virtual ConfigItem *allocateChild(QMetaProperty &prop, const YAML::Node &node,
126 const Context &ctx, const ErrorStack &err=ErrorStack());
128 virtual bool parse(const YAML::Node &node, Context &ctx, const ErrorStack &err=ErrorStack());
130 virtual bool link(const YAML::Node &node, const Context &ctx, const ErrorStack &err=ErrorStack());
131
133 virtual void clear();
134
136 virtual const Config *config() const;
138 virtual void findItemsOfTypes(const QStringList &typeNames, QSet<ConfigItem*> &items) const;
139
141 template <class Object>
142 bool is() const {
143 return nullptr != qobject_cast<const Object*>(this);
144 }
145
147 template <class Object>
148 const Object *as() const {
149 return qobject_cast<const Object*>(this);
150 }
151
153 template <class Object>
155 return qobject_cast<Object *>(this);
156 }
157
159 bool hasDescription() const;
161 bool hasLongDescription() const;
163 bool hasDescription(const QMetaProperty &prop) const;
165 bool hasLongDescription(const QMetaProperty &prop) const;
167 QString description() const;
169 QString longDescription() const;
174
175protected:
178 virtual bool populate(YAML::Node &node, const Context &context, const ErrorStack &err=ErrorStack());
179
180signals:
187 void endClear();
188};
189
190
194{
196
199
200
201 Q_CLASSINFO("IdPrefix", "obj")
202
206 ConfigObject(QObject *parent = nullptr);
207
211 ConfigObject(const QString &name, QObject *parent = nullptr);
212
213public:
218
219public:
223 bool parse(const YAML::Node &node, Context &ctx, const ErrorStack &err=ErrorStack());
224
227
230
234};
235
236
241{
243
244protected:
246 explicit ConfigExtension(QObject *parent=nullptr);
247};
248
249
252class AbstractConfigObjectList: public QObject
253{
254 Q_OBJECT
255
256protected:
258 explicit AbstractConfigObjectList(const QMetaObject &elementTypes=ConfigObject::staticMetaObject, QObject *parent = nullptr);
260 AbstractConfigObjectList(const std::initializer_list<QMetaObject> &elementTypes, QObject *parent=nullptr);
261
262public:
264 virtual bool copy(const AbstractConfigObjectList &other);
265
267 virtual bool label(ConfigItem::Context &context, const ErrorStack &err=ErrorStack()) = 0;
270 virtual YAML::Node serialize(const ConfigItem::Context &context, const ErrorStack &err=ErrorStack()) = 0;
271
273 virtual int count() const;
275 virtual int indexOf(ConfigObject *obj) const;
277 virtual void clear();
278
280 virtual const Config *config() const;
282 virtual void findItemsOfTypes(const QStringList &typeNames, QSet<ConfigItem*> &items) const;
284 virtual QList<ConfigObject *> findItemsByName(const QString name) const;
285
287 virtual bool has(ConfigObject *obj) const;
289 virtual ConfigObject *get(int idx) const;
291 virtual int add(ConfigObject *obj, int row=-1, bool unique=true);
293 virtual int replace(ConfigObject *obj, int row, bool unique=true);
295 virtual bool take(ConfigObject *obj);
297 virtual bool del(ConfigObject *obj);
298
300 virtual bool moveUp(int idx);
302 virtual bool moveUp(int first, int last);
304 virtual bool moveDown(int idx);
306 virtual bool moveDown(int first, int last);
310 virtual bool move(int source, int count, int destination);
311
313 const QList<QMetaObject> &elementTypes() const;
315 QStringList classNames() const;
316
317signals:
319 void elementAdded(int idx);
321 void elementModified(int idx);
323 void elementRemoved(int idx);
324
325private slots:
327 void onElementModified(ConfigItem *obj);
329 void onElementDeleted(QObject *obj);
330
331protected:
333 QList<QMetaObject> _elementTypes;
335 QVector<ConfigObject *> _items;
336};
337
338
344{
345 Q_OBJECT
346
347protected:
349 explicit ConfigObjectList(const QMetaObject &elementTypes=ConfigItem::staticMetaObject, QObject *parent = nullptr);
351 ConfigObjectList(const std::initializer_list<QMetaObject> &elementTypes, QObject *parent=nullptr);
352
353public:
354 int add(ConfigObject *obj, int row=-1, bool unique=true);
355 bool take(ConfigObject *obj);
356 bool del(ConfigObject *obj);
357 void clear();
358 bool copy(const AbstractConfigObjectList &other);
359
366 virtual int compare(const ConfigObjectList &other) const;
367
369 virtual ConfigItem *allocateChild(const YAML::Node &node, ConfigItem::Context &ctx, const ErrorStack &err=ErrorStack()) = 0;
371 virtual bool parse(const YAML::Node &node, ConfigItem::Context &ctx, const ErrorStack &err=ErrorStack());
373 virtual bool link(const YAML::Node &node, const ConfigItem::Context &ctx, const ErrorStack &err=ErrorStack());
374
375 bool label(ConfigItem::Context &context, const ErrorStack &err=ErrorStack());
376 YAML::Node serialize(const ConfigItem::Context &context, const ErrorStack &err=ErrorStack());
377};
378
379
385{
386 Q_OBJECT
387
388protected:
390 explicit ConfigObjectRefList(const QMetaObject &elementTypes=ConfigObject::staticMetaObject, QObject *parent = nullptr);
392 ConfigObjectRefList(const std::initializer_list<QMetaObject> &elementTypes, QObject *parent=nullptr);
393
394public:
395 bool label(ConfigItem::Context &context, const ErrorStack &err=ErrorStack());
396 YAML::Node serialize(const ConfigItem::Context &context, const ErrorStack &err=ErrorStack());
397
404 virtual int compare(const ConfigObjectRefList &other) const;
405};
406
407
408#endif // CONFIGOBJECT_HH
Generic list class for config objects.
Definition configobject.hh:253
QVector< ConfigObject * > _items
Holds the list items.
Definition configobject.hh:335
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:333
void elementRemoved(int idx)
Gets emitted if one of the lists elements gets deleted.
virtual bool label(ConfigItem::Context &context, const ErrorStack &err=ErrorStack())=0
Recursively labels the config object.
void elementModified(int idx)
Gets emitted if one of the lists elements gets modified.
Base class of all device/vendor specific configuration extensions.
Definition configobject.hh:241
Parse context for config objects.
Definition configobject.hh:43
QString _version
The version string.
Definition configobject.hh:81
const QString & version() const
Returns the read version string.
Definition configobject.cc:49
virtual bool add(const QString &id, ConfigObject *)
Associates the given object with the given ID.
Definition configobject.cc:78
void setVersion(const QString &ver)
Sets the version string.
Definition configobject.cc:53
QHash< QString, ConfigObject * > _objects
ID->OBJ look-up table.
Definition configobject.hh:83
Context()
Empty constructor.
Definition configobject.cc:38
virtual ~Context()
Destructor.
Definition configobject.cc:44
virtual QString getId(ConfigObject *obj) const
Returns ID of the given object.
Definition configobject.cc:68
static ConfigObject * getTag(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:99
static QHash< QString, QHash< ConfigObject *, QString > > _tagNames
Maps singleton objects to tags.
Definition configobject.hh:89
virtual ConfigObject * getObj(const QString &id) const
Returns the object for the given ID.
Definition configobject.cc:73
static void setTag(const QString &className, const QString &property, const QString &tag, ConfigObject *obj)
Associates the given object with the tag for the property of the given class.
Definition configobject.cc:117
static QHash< QString, QHash< QString, ConfigObject * > > _tagObjects
Maps tags to singleton objects.
Definition configobject.hh:87
QHash< ConfigObject *, QString > _ids
OBJ->ID look-up table.
Definition configobject.hh:85
virtual bool contains(ConfigObject *obj) const
Returns true, if the context contains the given object.
Definition configobject.cc:58
static bool hasTag(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:87
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:772
virtual int compare(const ConfigItem &other) const
Compares the items.
Definition configobject.cc:238
bool hasLongDescription() const
Returns true if there is a class info "longDescription" for this instance.
Definition configobject.cc:954
virtual bool copy(const ConfigItem &other)
Copies the given item into this one.
Definition configobject.cc:139
Object * as()
Casts this object to the given type.
Definition configobject.hh:154
virtual bool populate(YAML::Node &node, const Context &context, const ErrorStack &err=ErrorStack())
Recursively serializes the configuration to YAML nodes.
Definition configobject.cc:393
QString longDescription() const
Returns the long description of this instance if set by a class info.
Definition configobject.cc:986
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:948
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:925
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:487
bool is() const
Returns true if this object is of class Object.
Definition configobject.hh:142
virtual bool label(Context &context, const ErrorStack &err=ErrorStack())
Recursively labels the config object.
Definition configobject.cc:341
virtual const Config * config() const
Returns the config, the item belongs to or nullptr if not part of a config.
Definition configobject.cc:914
virtual YAML::Node serialize(const Context &context, const ErrorStack &err=ErrorStack())
Recursively serializes the configuration to YAML nodes.
Definition configobject.cc:365
virtual void clear()
Clears the config object.
Definition configobject.cc:373
const Object * as() const
Casts this object to the given type.
Definition configobject.hh:148
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:515
QString description() const
Returns the description of this instance if set by a class info.
Definition configobject.cc:978
void endClear()
Gets emitted after clearing the item.
void modified(ConfigItem *obj)
Gets emitted once the config object is modified.
void beginClear()
Gets emitted before clearing the item.
List class for config objects.
Definition configobject.hh:344
virtual ConfigItem * allocateChild(const YAML::Node &node, ConfigItem::Context &ctx, const ErrorStack &err=ErrorStack())=0
Allocates a member objects for the given YAML node.
List class for config objects.
Definition configobject.hh:385
Base class of all labeled and named objects.
Definition configobject.hh:194
virtual bool populate(YAML::Node &node, const Context &context, const ErrorStack &err=ErrorStack())
Recursively serializes the configuration to YAML nodes.
Definition configobject.cc:1086
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:1068
bool label(Context &context, const ErrorStack &err=ErrorStack())
Recursively labels the config object.
Definition configobject.cc:1046
virtual void setName(const QString &name)
Sets the name of the object.
Definition configobject.cc:1033
QString _name
Holds the name of the object.
Definition configobject.hh:233
QString name
The name of the object.
Definition configobject.hh:198
QString idPrefix() const
Returns the ID prefix for this object.
Definition configobject.cc:1041
static QString findIdPrefix(const QMetaObject *meta)
Helper to find the IdPrefix class info in the class hierarchy.
Definition configobject.cc:1093
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