14#ifndef ABSTRACTMODELITEM_H
15#define ABSTRACTMODELITEM_H
16#include <QAbstractAnimation>
19#include <Kanoop/entitymetadata.h>
20#include <Kanoop/gui/libkanoopgui.h>
61 qDeleteAll(_children);
73 virtual void setEntityMetadata(
const EntityMetadata& metadata) { _entityMetadata = metadata; }
75 virtual int entityType()
const {
return _entityMetadata.type(); }
77 virtual QUuid
uuid()
const {
return _uuid; }
79 virtual QIcon
icon()
const {
return _icon; }
86 virtual QVariant
data(
const QModelIndex& index,
int role = Qt::DisplayRole)
const;
97 virtual void updateFromVariant(
int headerType,
const QVariant& value) { Q_UNUSED(headerType) Q_UNUSED(value) }
102 class List :
public QList<AbstractModelItem*>
113 auto it = std::find_if(constBegin(), constEnd(), [uuid](
AbstractModelItem* a) {
return a->
uuid() == uuid; } );
114 if(it != constEnd()) {
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);
143 for(
int i = 0;i < count();i++) {
161 for(
int i = 0;i < count();i++) {
176 template <
typename T>
181 T candidate =
dynamic_cast<T
>(item);
182 if(candidate !=
nullptr) {
183 result.append(candidate);
187 result.append(item->findChildren<T>(recursive));
262 template <
typename T>
265 QList<T> result = _children.findChildItems<T>(recursive);
274 template <
typename T>
279 T candidate =
dynamic_cast<T
>(item);
280 if(candidate !=
nullptr) {
296 void setIcon(
const QIcon& value) { _icon = value; }
299 EntityMetadata _entityMetadata;
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.