KanoopGuiQt 1.3.0
Qt GUI utility library
Loading...
Searching...
No Matches
abstractmodelitem.h
1/******************************************************************************************
2**
3** abstractmodelitem.h
4**
5** Provides a base class for items which use the AbstractItemModel class in this library
6** and the EntityMetadata class in KanoopCommonQt.
7**
8** Moved from my Tesseract Engineering repo to open-source
9**
10** Author: Stephen Punak
11** Created: Sun Oct 22 17:41:53 2023
12**
13******************************************************************************************/
14#ifndef ABSTRACTMODELITEM_H
15#define ABSTRACTMODELITEM_H
16#include <QAbstractAnimation>
17#include <QIcon>
18#include <QUuid>
19#include <Kanoop/entitymetadata.h>
20#include <Kanoop/gui/libkanoopgui.h>
21
23
24/**
25 * @brief Base class for items managed by AbstractItemModel.
26 *
27 * Each item holds an EntityMetadata descriptor, a UUID, an optional icon, and
28 * references to its parent model, parent item, and child items. Subclass this
29 * to attach domain-specific data and override data() to supply display values.
30 */
31class LIBKANOOPGUI_EXPORT AbstractModelItem
32{
33public:
34 /** @brief Default constructor — creates an unowned, untyped item. */
36
37 /**
38 * @brief Construct an item owned by a model with no metadata.
39 * @param model Model that owns this item
40 */
42
43 /**
44 * @brief Construct an item with metadata, a model, and an optional UUID.
45 * @param entityMetadata Metadata describing the entity
46 * @param model Model that owns this item
47 * @param uuid Optional UUID for this item
48 */
49 AbstractModelItem(const EntityMetadata& entityMetadata, AbstractItemModel* model, const QUuid& uuid = QUuid());
50
51 /**
52 * @brief Construct an item with metadata, a UUID, and a model.
53 * @param entityMetadata Metadata describing the entity
54 * @param uuid UUID for this item
55 * @param model Model that owns this item
56 */
57 AbstractModelItem(const EntityMetadata& entityMetadata, const QUuid& uuid, AbstractItemModel* model);
58
59 /** @brief Destructor — deletes all child items. */
61 qDeleteAll(_children);
62 }
63
64 // Overridable Properties
65 /** @brief Return the entity metadata for this item. */
66 virtual EntityMetadata entityMetadata() const { return _entityMetadata; }
67 /** @brief Return a mutable reference to the entity metadata. */
68 virtual EntityMetadata& entityMetadataRef() { return _entityMetadata; }
69 /**
70 * @brief Set the entity metadata for this item.
71 * @param metadata New metadata
72 */
73 virtual void setEntityMetadata(const EntityMetadata& metadata) { _entityMetadata = metadata; }
74 /** @brief Return the entity type integer from this item's metadata. */
75 virtual int entityType() const { return _entityMetadata.type(); }
76 /** @brief Return the UUID for this item. */
77 virtual QUuid uuid() const { return _uuid; }
78 /** @brief Return the icon for this item. */
79 virtual QIcon icon() const { return _icon; }
80 /**
81 * @brief Return display or decoration data for a model index.
82 * @param index Model index being queried
83 * @param role Qt item data role
84 * @return Data for the given role
85 */
86 virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
87 /**
88 * @brief Update this item's data from new entity metadata.
89 * @param metadata New metadata to apply
90 */
91 virtual void updateFromMetadata(const EntityMetadata& metadata);
92 /**
93 * @brief Update a single cell from a raw variant value (no-op by default).
94 * @param headerType Column header type
95 * @param value New cell value
96 */
97 virtual void updateFromVariant(int headerType, const QVariant& value) { Q_UNUSED(headerType) Q_UNUSED(value) }
98
99 /**
100 * @brief A list of AbstractModelItem pointers with UUID and entity-type search helpers.
101 */
102 class List : public QList<AbstractModelItem*>
103 {
104 public:
105 /**
106 * @brief Find the first item with the given UUID.
107 * @param uuid UUID to search for
108 * @return Matching item pointer, or nullptr if not found
109 */
110 AbstractModelItem* findByUuid(const QUuid& uuid) const
111 {
112 AbstractModelItem* result = nullptr;
113 auto it = std::find_if(constBegin(), constEnd(), [uuid](AbstractModelItem* a) { return a->uuid() == uuid; } );
114 if(it != constEnd()) {
115 result = *it;
116 }
117 return result;
118 }
119
120 /**
121 * @brief Return the list index of the first item with the given UUID.
122 * @param uuid UUID to search for
123 * @return Index of the matching item, or -1 if not found
124 */
125 int indexOfUuid(const QUuid& uuid) const
126 {
127 int result = -1;
128 auto it = std::find_if(constBegin(), constEnd(), [uuid](AbstractModelItem* a) { return a->uuid() == uuid; } );
129 if(it != constEnd()) {
130 result = std::distance(constBegin(), it);
131 }
132 return result;
133 }
134
135 /**
136 * @brief Return the list index of the first item with the given entity type.
137 * @param entityType Entity type to search for
138 * @return Index of the matching item, or -1 if not found
139 */
140 int firstIndexOfEntityType(int entityType) const
141 {
142 int result = -1;
143 for(int i = 0;i < count();i++) {
144 AbstractModelItem* item = this->at(i);
145 if(item->entityType() == entityType) {
146 result = i;
147 break;
148 }
149 }
150 return result;
151 }
152
153 /**
154 * @brief Return the list index of the last item with the given entity type.
155 * @param entityType Entity type to search for
156 * @return Index of the matching item, or -1 if not found
157 */
158 int lastIndexOfEntityType(int entityType) const
159 {
160 int result = -1;
161 for(int i = 0;i < count();i++) {
162 AbstractModelItem* item = this->at(i);
163 if(item->entityType() == entityType) {
164 result = i;
165 }
166 }
167 return result;
168 }
169
170 /**
171 * @brief Find all child items castable to type T, optionally recursing.
172 * @tparam T Pointer type to find
173 * @param recursive Whether to recurse into children (default true)
174 * @return List of matching items cast to T
175 */
176 template <typename T>
177 QList<T> findChildItems(bool recursive = true) const
178 {
179 QList<T> result;
180 for(AbstractModelItem* item : *this) {
181 T candidate = dynamic_cast<T>(item);
182 if(candidate != nullptr) {
183 result.append(candidate);
184 }
185
186 if(recursive) {
187 result.append(item->findChildren<T>(recursive));
188 }
189 }
190 return result;
191 }
192 };
193
194 // Model Properties and Methods
195 /**
196 * @brief Return the row index of this item within its parent's children.
197 * @return Row index, or 0 if this item has no parent
198 */
199 int row() const;
200
201 /** @brief Return this item's parent item, or nullptr if it is a root item. */
202 AbstractModelItem* parent() const { return _parent; }
203
204 /**
205 * @brief Return the child item at the given row.
206 * @param row Row index
207 * @return Child item pointer, or nullptr if row is out of range
208 */
209 AbstractModelItem* child(int row) const;
210
211 /** @brief Return the list of child items. */
212 List children() const { return _children; }
213 /** @brief Return a mutable reference to the child items list. */
214 List& childrenRef() { return _children; }
215 /** @brief Return the list of sibling items (all children of this item's parent). */
216 List siblings() const;
217
218 /**
219 * @brief Return the number of direct children, optionally filtered by entity type.
220 * @param entityType Entity type filter (0 = count all types)
221 * @return Count of matching children
222 */
223 int childCount(int entityType = 0) const;
224
225 /**
226 * @brief Return the total number of descendants, optionally filtered by entity type.
227 * @param entityType Entity type filter (0 = count all types)
228 * @return Total count of matching descendants
229 */
230 int childCountRecursive(int entityType = 0) const;
231
232 /**
233 * @brief Insert a child item at the given index.
234 * @param index Position to insert at
235 * @param child Child item to insert
236 * @return Pointer to the inserted child
237 */
239
240 /**
241 * @brief Append a child item.
242 * @param child Child item to append
243 * @return Pointer to the appended child
244 */
246
247 /**
248 * @brief Delete a specific child item.
249 * @param child Child item to remove and delete
250 */
252
253 /** @brief Delete and remove all child items. */
255
256 /**
257 * @brief Find all direct (or recursive) children castable to type T.
258 * @tparam T Pointer type to find
259 * @param recursive Whether to recurse into grandchildren (default false)
260 * @return List of matching children cast to T
261 */
262 template <typename T>
263 QList<T> findChildren(bool recursive = false) const
264 {
265 QList<T> result = _children.findChildItems<T>(recursive);
266 return result;
267 }
268
269 /**
270 * @brief Return the first direct child castable to type T.
271 * @tparam T Pointer type to find
272 * @return First matching child cast to T, or nullptr if none
273 */
274 template <typename T>
275 T firstChild() const
276 {
277 T result = nullptr;
278 for(AbstractModelItem* item : _children) {
279 T candidate = dynamic_cast<T>(item);
280 if(candidate != nullptr) {
281 result = candidate;
282 break;
283 }
284 }
285 return result;
286 }
287
288 /** @brief Return the model that owns this item. */
289 AbstractItemModel* model() const { return _model; }
290
291protected:
292 /**
293 * @brief Set the icon for this item.
294 * @param value Icon to assign
295 */
296 void setIcon(const QIcon& value) { _icon = value; }
297
298private:
299 EntityMetadata _entityMetadata;
300 AbstractItemModel* _model = nullptr;
301 QUuid _uuid;
302 AbstractModelItem* _parent = nullptr;
303 List _children;
304
305 QIcon _icon;
306};
307
308#endif // ABSTRACTMODELITEM_H
Extended QAbstractItemModel providing EntityMetadata-based item lookup and header management.
A list of AbstractModelItem pointers with UUID and entity-type search helpers.
int indexOfUuid(const QUuid &uuid) const
Return the list index of the first item with the given UUID.
QList< T > findChildItems(bool recursive=true) const
Find all child items castable to type T, optionally recursing.
int firstIndexOfEntityType(int entityType) const
Return the list index of the first item with the given entity type.
int lastIndexOfEntityType(int entityType) const
Return the list index of the last item with the given entity type.
AbstractModelItem * findByUuid(const QUuid &uuid) const
Find the first item with the given UUID.
Base class for items managed by AbstractItemModel.
int childCount(int entityType=0) const
Return the number of direct children, optionally filtered by entity type.
virtual QUuid uuid() const
Return the UUID for this item.
List & childrenRef()
Return a mutable reference to the child items list.
AbstractModelItem * insertChild(int index, AbstractModelItem *child)
Insert a child item at the given index.
void setIcon(const QIcon &value)
Set the icon for this item.
AbstractItemModel * model() const
Return the model that owns this item.
virtual EntityMetadata entityMetadata() const
Return the entity metadata for this item.
AbstractModelItem * parent() const
Return this item's parent item, or nullptr if it is a root item.
AbstractModelItem * child(int row) const
Return the child item at the given row.
AbstractModelItem(const EntityMetadata &entityMetadata, AbstractItemModel *model, const QUuid &uuid=QUuid())
Construct an item with metadata, a model, and an optional UUID.
AbstractModelItem * appendChild(AbstractModelItem *child)
Append a child item.
int childCountRecursive(int entityType=0) const
Return the total number of descendants, optionally filtered by entity type.
void deleteAllChildren()
Delete and remove all child items.
void deleteChild(AbstractModelItem *child)
Delete a specific child item.
virtual int entityType() const
Return the entity type integer from this item's metadata.
virtual ~AbstractModelItem()
Destructor — deletes all child items.
virtual void updateFromVariant(int headerType, const QVariant &value)
Update a single cell from a raw variant value (no-op by default).
int row() const
Return the row index of this item within its parent's children.
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Return display or decoration data for a model index.
virtual EntityMetadata & entityMetadataRef()
Return a mutable reference to the entity metadata.
List children() const
Return the list of child items.
AbstractModelItem(AbstractItemModel *model)
Construct an item owned by a model with no metadata.
virtual void updateFromMetadata(const EntityMetadata &metadata)
Update this item's data from new entity metadata.
T firstChild() const
Return the first direct child castable to type T.
List siblings() const
Return the list of sibling items (all children of this item's parent).
QList< T > findChildren(bool recursive=false) const
Find all direct (or recursive) children castable to type T.
virtual QIcon icon() const
Return the icon for this item.
virtual void setEntityMetadata(const EntityMetadata &metadata)
Set the entity metadata for this item.
AbstractModelItem(const EntityMetadata &entityMetadata, const QUuid &uuid, AbstractItemModel *model)
Construct an item with metadata, a UUID, and a model.
AbstractModelItem()
Default constructor — creates an unowned, untyped item.