19#ifndef ABSTRACTITEMMODEL_H
20#define ABSTRACTITEMMODEL_H
21#include <QAbstractItemModel>
22#include <Kanoop/gui/abstractmodelitem.h>
23#include <Kanoop/gui/tableheader.h>
24#include <Kanoop/utility/loggingbaseclass.h>
33 public LoggingBaseClass
49 qDeleteAll(_rootItems);
67 virtual QModelIndexList
indexesOfEntity(
int type,
const QVariant &data,
int role = Qt::DisplayRole)
const;
90 virtual QModelIndex
firstIndexOfEntity(
int type,
const QVariant &data,
int role = Qt::DisplayRole)
const;
125 virtual QModelIndex
firstMatch(
const QModelIndex& startSearchIndex,
int role,
const QVariant& value, Qt::MatchFlags flags = Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap))
const;
134 virtual QModelIndex
firstMatch(
int role,
const QVariant& value, Qt::MatchFlags flags = Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap))
const;
143 virtual QModelIndexList
childIndexes(
const QModelIndex& parent,
int type = -1,
bool recursive =
true)
const;
183 virtual void refresh(
const QModelIndex& topLeft,
const QModelIndex& bottomRight);
187 virtual QModelIndex
index(
int row,
int column,
const QModelIndex &parent = QModelIndex())
const override;
189 virtual QModelIndex sibling(
int row,
int column,
const QModelIndex& idx)
const override;
192 virtual QModelIndex
parent(
const QModelIndex &child)
const override;
194 virtual int rowCount(
const QModelIndex &parent = QModelIndex())
const override;
196 virtual int columnCount(
const QModelIndex &parent = QModelIndex())
const override;
198 virtual QVariant
data(
const QModelIndex &index,
int role)
const override;
200 virtual QVariant
headerData(
int section, Qt::Orientation orientation,
int role)
const override;
202 virtual bool removeRows(
int row,
int count,
const QModelIndex& parentIndex)
override;
204 virtual bool hasChildren(
const QModelIndex& parent)
const override;
206 virtual bool setHeaderData(
int section, Qt::Orientation orientation,
const QVariant& value,
int role)
override;
221 static QString
indexToString(
const QModelIndex& index,
bool includeText =
false);
373 template <
typename T>
378 T candidate =
dynamic_cast<T
>(item);
379 if(candidate !=
nullptr) {
380 result.append(candidate);
382 result.append(item->findChildren<T>(
true));
414 static QString
toString(
const QModelIndex& index,
bool includeText =
false);
439 virtual void addEntity(
const EntityMetadata& metadata) { Q_UNUSED(metadata) }
441 virtual void deleteEntity(
const EntityMetadata& metadata) { Q_UNUSED(metadata) }
443 virtual void updateEntity(
const EntityMetadata& metadata) { Q_UNUSED(metadata) }
Extended QAbstractItemModel providing EntityMetadata-based item lookup and header management.
TableHeader columnHeader(int section) const
Return the column header for a given section.
const AbstractModelItem::List rootItemsConst() const
Return a const copy of the root items list.
virtual QModelIndex firstIndexOfChildEntityType(const QModelIndex &parent, int type, bool recursive=true) const
Return the first child index under parent with the given entity type.
virtual QVariant data(const QModelIndex &index, int role) const override
Return the data for the given index and role.
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Return the model index for the item at row/column under parent.
void appendAdHocColumnHeader(int type, const QString &value)
Append a column header (ad-hoc alias for appendColumnHeader).
void refreshAll()
Emit dataChanged for the entire model.
QModelIndexList getPersistentIndexes() const
Return all persistent model indexes.
EntityMetadata columnEntityMetadata(int type) const
Retrieve the EntityMetadata for a column header type.
void updateItemAtIndex(const QModelIndex &itemIndex, const EntityMetadata &metadata)
Update the item at itemIndex with new metadata and emit dataChanged.
void appendRowHeader(int type, const QString &value=QString())
Append a row header with optional display text.
TableHeader::IntMap columnHeadersIntMap() const
Return the column headers as an int-keyed map.
void deleteRootItems(const EntityMetadata &metadata)
Delete all root items matching the given EntityMetadata.
void appendColumnHeader(int type, const QString &text)
Append a column header with the given type and display text.
void deleteColumnHeader(int section)
Delete the column header at the given section.
AbstractModelItem::List & rootItemsRef()
Return a mutable reference to the root items list.
TableHeader rowHeader(int row) const
Return the row header for a given row.
virtual void deleteEntity(const EntityMetadata &metadata)
Handle an entity-deleted event (no-op by default).
void setColumnHeaderText(int section, const QString &text)
Change the display text of a column header.
virtual QModelIndex firstIndexOfEntity(int type, const QVariant &data, int role=Qt::DisplayRole) const
Return the first index matching entity type and a role value.
QModelIndex findFirstDirectChild(const QModelIndex &parentIndex, const QVariant &value, int role) const
Find the first direct child of parentIndex matching a role/value pair.
AbstractItemModel(QObject *parent=nullptr)
Construct with an optional parent.
virtual QModelIndex firstMatch(const QModelIndex &startSearchIndex, int role, const QVariant &value, Qt::MatchFlags flags=Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const
Return the first match starting from startSearchIndex.
void setColumnTextColor(int type, const QColor &color)
Set the text color for all cells in the column of the given type.
virtual ~AbstractItemModel()
Destructor — deletes all root items.
virtual QModelIndex firstIndexOfEntityUuid(const QUuid &uuid) const
Return the first index whose item UUID matches.
void setColumnHeaderVisible(int type, bool visible)
Show or hide the column whose header has the given type.
virtual QModelIndex firstIndexOfChildEntityUuid(const QModelIndex &parent, const QUuid &uuid, bool recursive=true) const
Return the first child index under parent whose UUID matches.
static QString toString(const QModelIndex &index, bool includeText=false)
Format a QModelIndex as a debug string (static alias).
AbstractModelItem::List rootItems() const
Return the list of root items.
virtual QModelIndex parent(const QModelIndex &child) const override
Return the parent index of a child index.
void appendColumnHeader(int type, const QColor &columnTextColor, const QString &text)
Append a column header with a custom text color.
void emitRowChanged(const QModelIndex &rowIndex)
Emit dataChanged for all columns of the given row index.
virtual bool hasChildren(const QModelIndex &parent) const override
Return true if parent has child items.
void entityAdded(const EntityMetadata &metadata)
Emitted after an entity is added to the model.
virtual void refresh(const QModelIndex &topLeft, const QModelIndex &bottomRight)
Emit dataChanged for the rectangular region from topLeft to bottomRight.
AbstractModelItem * insertRootItem(int row, AbstractModelItem *item)
Insert an item at the given row among root items.
virtual void addEntity(const EntityMetadata &metadata)
Handle an entity-added event (no-op by default).
int rootItemCount() const
Return the number of root items.
AbstractModelItem * appendRootItem(AbstractModelItem *item)
Append an item to the root items list.
void deleteItem(const QUuid &uuid)
Delete any item (at any level) with the given UUID.
void updateItemsAtIndexes(const QModelIndexList &indexes, const EntityMetadata &metadata)
Update items at multiple indexes with new metadata.
virtual QModelIndex firstMatch(int role, const QVariant &value, Qt::MatchFlags flags=Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const
Return the first match anywhere in the model.
virtual QModelIndex firstIndexOfEntityType(int type) const
Return the first index whose item has the given entity type.
virtual bool removeRows(int row, int count, const QModelIndex &parentIndex) override
Remove count rows starting at row under parentIndex.
void appendRootItems(QList< AbstractModelItem * > items)
Append multiple items to the root items list.
AbstractItemModel(const QString &loggingCategory, QObject *parent=nullptr)
Construct with a logging category and optional parent.
virtual QModelIndexList childIndexes(const QModelIndex &parent, int type=-1, bool recursive=true) const
Return child indexes under parent, optionally filtered by entity type.
TableHeader::IntMap rowHeadersIntMap() const
Return the row headers as an int-keyed map.
int columnForHeader(int type) const
Return the column index for a header type.
void entityUpdated(const EntityMetadata &metadata)
Emitted after an entity in the model is updated.
void entityDeleted(const EntityMetadata &metadata)
Emitted after an entity is deleted from the model.
virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) override
Set header data for the given section, orientation, and role.
void setColumnHeaderEntityMetadata(int type, const EntityMetadata &metadata)
Associate an EntityMetadata with a column header type.
static QString indexToString(const QModelIndex &index, bool includeText=false)
Format a QModelIndex as a debug string.
TableHeader::List columnHeaders() const
Return all column headers as a sorted list.
QList< T > findItems() const
Find all items of type T in the model (root and their children).
void deleteRootItems(const QUuid &uuid)
Delete all root items with the given UUID.
virtual QModelIndexList indexesOfEntityUuid(const QUuid &uuid) const
Return all indexes whose item UUID matches.
virtual QModelIndexList indexesOfEntity(int type, const QVariant &data, int role=Qt::DisplayRole) const
Return all indexes matching entity type and a role value.
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Return header data for the given section, orientation, and role.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const override
Return the number of rows under parent.
virtual void clear()
Remove all root items from the model.
void deleteRootItem(AbstractModelItem *item)
Delete a root item from the model.
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const override
Return the number of columns under parent.
void insertColumnHeader(int type, int index, const QString &text)
Insert a column header at the given index.
void insertAdHocColumnHeader(int type, int index, const QString &value)
Insert a column header at the given index (ad-hoc alias for insertColumnHeader).
void appendAdHocRowHeader(int type, const QString &value)
Append a row header (ad-hoc alias for appendRowHeader).
virtual void updateEntity(const EntityMetadata &metadata)
Handle an entity-updated event (no-op by default).
virtual QModelIndexList indexesOfEntityType(int type) const
Return all indexes whose item has the given entity type.
A list of AbstractModelItem pointers with UUID and entity-type search helpers.
Base class for items managed by AbstractItemModel.