KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
entitymetadata.h
1/**
2 * EntityMetadata
3 *
4 * This is a container class for trivial (copyable) objects.
5 *
6 * The primary idea is the EntityMetadata containing the target object can be passed
7 * around in place of the object itself. The EntityMetadata object itself can contain
8 * whatever additional data is needed by using the data() and setData() methods.
9 *
10 * User defined entity types should be defined in an enumeration and registered at
11 * initialization with registerMetadata().
12 *
13 * Icons ID's can be associated with specific entity types using registerIcon()
14 * when used in conjunction with KanoopCommonGuiQt
15 *
16 * It is primarily intended for use by the AbstractItemView / AbstractItemModel
17 * paradigm, but has found use in several other applications.
18 *
19 * Stephen Punak, September 23 2024
20 */
21#ifndef ENTITYMETADATA_H
22#define ENTITYMETADATA_H
23
24#include <QUuid>
25#include <QVariant>
26#include <Kanoop/kanoopcommon.h>
27
28class EntityMetadataInfo;
29
30/**
31 * @brief A typed, role-keyed data container for passing objects through model/view layers.
32 *
33 * EntityMetadata wraps an integer entity type together with arbitrary role-keyed QVariant data
34 * and an optional UUID. Use registerMetadata() at startup to associate type integers with
35 * human-readable names and optional icon IDs.
36 */
37class KANOOP_EXPORT EntityMetadata
38{
39public:
40 /** @brief Default constructor — creates an invalid (type 0) metadata object. */
42 _type(0), _iconId(0) {}
43
44 /**
45 * @brief Construct metadata with only a type identifier.
46 * @param type Application-defined entity type integer
47 */
48 EntityMetadata(int type);
49
50 /**
51 * @brief Construct metadata with a type, data value, and optional role.
52 * @param type Application-defined entity type integer
53 * @param data Payload stored under the given role
54 * @param role Model role key (default KANOOP::DataRole)
55 */
56 EntityMetadata(int type, const QVariant& data, KANOOP::ModelRole role = KANOOP::DataRole);
57
58 /**
59 * @brief Construct metadata with a type, data value, UUID, and optional role.
60 * @param type Application-defined entity type integer
61 * @param data Payload stored under the given role
62 * @param uuid UUID to associate with this entity
63 * @param role Model role key (default KANOOP::DataRole)
64 */
65 EntityMetadata(int type, const QVariant& data, const QUuid& uuid, KANOOP::ModelRole role = KANOOP::DataRole);
66
67 /** @brief Equality comparison by type and UUID.
68 * @param other EntityMetadata to compare.
69 * @return True if type and UUID match. */
70 bool operator==(const EntityMetadata& other) const;
71
72 /**
73 * @brief Return the entity type integer.
74 * @return Type value as registered with registerMetadata()
75 */
76 int type() const { return _type; }
77
78 /**
79 * @brief Return the human-readable name for this entity's type.
80 * @return Registered type name string, or empty string if unknown
81 */
82 QString typeString() const;
83
84 /**
85 * @brief Return the icon ID associated with this entity's type.
86 * @return Icon ID, or 0 if none was registered
87 */
88 int iconId() const { return _iconId; }
89
90 /**
91 * @brief Return the data stored under the given role.
92 * @param role Model role key (default KANOOP::DataRole)
93 * @return Stored QVariant, or invalid QVariant if no data for role
94 */
95 QVariant data(int role = KANOOP::DataRole) const { return _data.value(role); }
96
97 /**
98 * @brief Store a value under the given role.
99 * @param value Value to store
100 * @param role Model role key (default KANOOP::DataRole)
101 */
102 void setData(const QVariant& value, int role = KANOOP::DataRole) { _data.insert(role, value); }
103
104 /**
105 * @brief Test whether data has been stored for a given role.
106 * @param role Model role key to check
107 * @return true if data exists for role
108 */
109 bool hasData(int role) const { return _data.contains(role); }
110
111 /**
112 * @brief Return the UUID stored under KANOOP::UUidRole.
113 * @return Stored UUID, or null UUID if not set
114 */
115 QUuid uuid() const { return data(KANOOP::UUidRole).toUuid(); }
116
117 /**
118 * @brief Store a UUID under KANOOP::UUidRole.
119 * @param value UUID to associate with this entity
120 */
121 void setUuid(const QUuid& value) { setData(value, KANOOP::UUidRole); }
122
123 /**
124 * @brief Wrap this object in a QVariant.
125 * @return QVariant containing a copy of this EntityMetadata
126 */
127 QVariant toVariant() const { return QVariant::fromValue<EntityMetadata>(*this); }
128
129 /**
130 * @brief Unwrap an EntityMetadata from a QVariant.
131 * @param value Variant previously created by toVariant()
132 * @return Extracted EntityMetadata
133 */
134 static EntityMetadata fromVariant(const QVariant& value) { return value.value<EntityMetadata>(); }
135
136 /**
137 * @brief Test whether this metadata has a non-zero type.
138 * @return true if type != 0
139 */
140 bool isValid() const { return _type != 0; }
141
142 /**
143 * @brief Register a name (and optional icon ID) for an entity type integer.
144 * @param type Application-defined entity type integer
145 * @param name Human-readable name for this type
146 * @param iconId Optional icon identifier to associate with this type
147 */
148 static void registerMetadata(int type, const QString& name, int iconId = 0);
149
150 /**
151 * @brief Associate an icon ID with an already-registered entity type.
152 * @param type Registered entity type integer
153 * @param iconId Icon identifier to associate
154 */
155 static void registerIcon(int type, int iconId);
156
157 /**
158 * @brief Look up the registered name for an entity type integer.
159 * @param type Entity type integer
160 * @return Registered name string, or empty string if not found
161 */
162 static QString getTypeString(int type);
163
164 /**
165 * @brief Look up the entity type integer for a registered name string.
166 * @param value Registered name string
167 * @return Matching type integer, or 0 if not found
168 */
169 static int getTypeFromString(const QString& value);
170
171 /**
172 * @brief Return a MIME type string for the given entity type.
173 * @param type Entity type integer
174 * @return MIME type string of the form "application-epcpower/<name>"
175 */
176 static QString getMimeTypeString(int type) { return QString("application-epcpower/%1").arg(getTypeString(type)); }
177
178private:
179 /** @brief Resolve and cache the icon ID from the registered type registry. */
180 void resolveIconId();
181
182 int _type = 0;
183 QMap<int, QVariant> _data;
184 int _iconId = 0;
185
186 static QMap<int, EntityMetadataInfo*> _registeredTypes;
187 static QMap<QString, int> _nameToTypeMap;
188};
189
190/**
191 * @brief A list of EntityMetadata objects with type-filter support.
192 */
193class KANOOP_EXPORT EntityMetadataList : public QList<EntityMetadata>
194{
195public:
196 /**
197 * @brief Return all entries whose type matches the given value.
198 * @param type Entity type integer to filter by
199 * @return Sub-list containing only matching entries
200 */
202 {
203 EntityMetadataList result;
204 for(const EntityMetadata& metadata : *this) {
205 if(metadata.type() == type) {
206 result.append(metadata);
207 }
208 }
209 return result;
210 }
211
212 /**
213 * @brief Wrap this list in a QVariant.
214 * @return QVariant containing a copy of this list
215 */
216 QVariant toVariant() const { return QVariant::fromValue<EntityMetadataList>(*this); }
217
218 /**
219 * @brief Unwrap an EntityMetadataList from a QVariant.
220 * @param value Variant previously created by toVariant()
221 * @return Extracted EntityMetadataList
222 */
223 static EntityMetadataList fromVariant(const QVariant& value) { return value.value<EntityMetadataList>(); }
224};
225
226Q_DECLARE_METATYPE(EntityMetadata)
227Q_DECLARE_METATYPE(EntityMetadataList)
228
229#endif // ENTITYMETADATA_H
A list of EntityMetadata objects with type-filter support.
static EntityMetadataList fromVariant(const QVariant &value)
Unwrap an EntityMetadataList from a QVariant.
QVariant toVariant() const
Wrap this list in a QVariant.
EntityMetadataList findByType(int type) const
Return all entries whose type matches the given value.
A typed, role-keyed data container for passing objects through model/view layers.
static void registerMetadata(int type, const QString &name, int iconId=0)
Register a name (and optional icon ID) for an entity type integer.
static QString getMimeTypeString(int type)
Return a MIME type string for the given entity type.
QVariant toVariant() const
Wrap this object in a QVariant.
EntityMetadata()
Default constructor — creates an invalid (type 0) metadata object.
EntityMetadata(int type, const QVariant &data, KANOOP::ModelRole role=KANOOP::DataRole)
Construct metadata with a type, data value, and optional role.
int iconId() const
Return the icon ID associated with this entity's type.
void setData(const QVariant &value, int role=KANOOP::DataRole)
Store a value under the given role.
static void registerIcon(int type, int iconId)
Associate an icon ID with an already-registered entity type.
static int getTypeFromString(const QString &value)
Look up the entity type integer for a registered name string.
int type() const
Return the entity type integer.
bool hasData(int role) const
Test whether data has been stored for a given role.
void setUuid(const QUuid &value)
Store a UUID under KANOOP::UUidRole.
EntityMetadata(int type, const QVariant &data, const QUuid &uuid, KANOOP::ModelRole role=KANOOP::DataRole)
Construct metadata with a type, data value, UUID, and optional role.
EntityMetadata(int type)
Construct metadata with only a type identifier.
static QString getTypeString(int type)
Look up the registered name for an entity type integer.
QVariant data(int role=KANOOP::DataRole) const
Return the data stored under the given role.
QUuid uuid() const
Return the UUID stored under KANOOP::UUidRole.
static EntityMetadata fromVariant(const QVariant &value)
Unwrap an EntityMetadata from a QVariant.
bool operator==(const EntityMetadata &other) const
Equality comparison by type and UUID.
QString typeString() const
Return the human-readable name for this entity's type.
bool isValid() const
Test whether this metadata has a non-zero type.
ModelRole
Model roles extending Qt::UserRole for use in AbstractItemModel subclasses.
@ DataRole
Primary data payload role.
@ UUidRole
Role carrying a QUuid.