KanoopGuiQt 1.3.0
Qt GUI utility library
Loading...
Searching...
No Matches
TreeViewBase Class Reference

QTreeView subclass integrating AbstractItemModel with rich navigation helpers. More...

#include <treeviewbase.h>

+ Inheritance diagram for TreeViewBase:
+ Collaboration diagram for TreeViewBase:

Public Slots

virtual void refreshVisibleColumns (const QList< int > &columns)
 Refresh the display of a set of columns.
 
virtual void refreshVisibleIndexes (const QModelIndexList &indexes)
 Refresh the display of a set of source model indexes.
 
virtual void refreshIndex (const QModelIndex &sourceIndex)
 Refresh a single source model index.
 
virtual void clear ()
 Remove all items from the view model.
 

Signals

void itemProgramaticallySelected (const QModelIndex &index)
 Emitted when an item is selected programmatically.
 
void headerChanged ()
 Emitted when the header is resized or reordered.
 
void currentSelectionChanged ()
 Emitted when the current selection changes.
 
void currentIndexChanged (const QModelIndex &index)
 Emitted when the current index changes.
 
void entityAdded (const EntityMetadata &metadata)
 Emitted when an entity is added.
 
void entityDeleted (const EntityMetadata &metadata)
 Emitted when an entity is deleted.
 
void entityUpdated (const EntityMetadata &metadata)
 Emitted when an entity is updated.
 

Public Member Functions

 TreeViewBase (QWidget *parent=nullptr)
 Construct with an optional parent widget.
 
int entityTypeAtPos (const QPoint &pos)
 Return the entity type of the item at a view position.
 
virtual EntityMetadata currentMetadata () const
 Return the EntityMetadata of the currently selected item.
 
virtual EntityMetadata metadataAtPos (const QPoint &pos) const
 Return the EntityMetadata of the item at a view position.
 
virtual QModelIndex firstIndexOfEntityUuid (const QUuid &uuid) const
 Return the first index whose item UUID matches.
 
virtual QModelIndexList indexesOfUuid (const QUuid &uuid) const
 Return all indexes whose item UUID matches.
 
virtual void setCurrentUuid (const QUuid &uuid, ScrollHint scrollHint=EnsureVisible)
 Select the item with the given UUID and optionally scroll to it.
 
virtual void setCurrentIndex (const QModelIndex &index, ScrollHint scrollHint=EnsureVisible)
 Set the current index and optionally scroll to it.
 
virtual QModelIndex findNextMatch (const QString &text, const QModelIndex &fromIndex) const
 Find the next index whose display text matches, searching forward.
 
virtual QModelIndex findPreviousMatch (const QString &text, const QModelIndex &fromIndex) const
 Find the previous index whose display text matches, searching backward.
 
virtual QModelIndex finalChildIndex (const QModelIndex &from) const
 Return the deepest last child of the given index.
 
virtual QModelIndex nextIndex (const QModelIndex &from) const
 Return the next index in display order after from.
 
virtual QModelIndex previousIndex (const QModelIndex &from) const
 Return the previous index in display order before from.
 
virtual QModelIndexList matchBackwards (const QModelIndex &start, int role, const QVariant &value, int hits=1, Qt::MatchFlags flags=Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const
 Search backward from start for indexes matching a role value.
 
QModelIndex currentSourceIndex () const
 Return the current source model index (mapped through the proxy).
 
QByteArray saveState () const
 Save the header and expansion state to a byte array.
 
void restoreState (const QByteArray &state)
 Restore header and expansion state from a byte array.
 
void restoreHeaderStates ()
 Restore horizontal header state (column widths/order) from settings.
 
virtual void setModel (QAbstractItemModel *model) override
 Set the model, wrapping it in an internal sort/filter proxy.
 
virtual void setSelectionModel (QItemSelectionModel *selectionModel) override
 Set the selection model.
 
AbstractItemModelsourceModel () const
 Return the underlying AbstractItemModel (without the proxy).
 
QSortFilterProxyModel * proxyModel () const
 Return the internal sort/filter proxy model.
 
void collapseRecursively (const QModelIndex &index, int depth=-1)
 Recursively collapse index and all its descendants up to a depth limit.
 
bool isLeafExpanded (const QModelIndex &index, bool recursive=true) const
 Return whether the deepest expanded leaf under index is expanded.
 
bool isIndexVisible (const QModelIndex &index) const
 Return whether the given index is visible in the current viewport.
 
void setColumnDelegate (int type, QStyledItemDelegate *delegate)
 Assign a custom item delegate to the column of the given header type.
 
int columnForHeaderType (int headerType) const
 Return the column index for a given header type.
 
void setColumnTypesVisible (const QList< int > &headerTypes, bool visible, bool exclusive=false)
 Show or hide columns identified by header types.
 
void setColumnsVisible (const QList< int > &columns, bool visible, bool exclusive=false)
 Show or hide columns by column index.
 

Protected Member Functions

EntityMetadata findCurrentParent (int entityMetadataType) const
 Walk ancestors of the current index to find one with the given entity type.
 
EntityMetadata findFirstParent (const QModelIndex &index, int entityMetadataType) const
 Walk ancestors of index to find one with the given entity type.
 
QModelIndexList findParents (const QModelIndex &index) const
 Return all ancestor indexes of index.
 
QModelIndexList mapToSource (const QModelIndexList &indexes) const
 Map a list of view indexes to their source model equivalents.
 
virtual void addHeaderContextMenuItems (QMenu *menu, const QPoint &globalPos)
 Override to add application-specific items to the header context menu.
 

Static Protected Member Functions

static void logIndex (const char *file, int lineNumber, Log::LogLevel level, const QModelIndex &index, const QString &text)
 Log a model index at the given log level.
 
static bool testMatch (const QModelIndex &index, int role, const QVariant &value, Qt::MatchFlags flags, QModelIndexList &foundIndexes)
 Test whether index matches value under role using flags, appending to foundIndexes.
 

Detailed Description

QTreeView subclass integrating AbstractItemModel with rich navigation helpers.

TreeViewBase wraps its source model in a QSortFilterProxyModel and provides helpers for UUID-based navigation, text-based forward/backward search, recursive collapse, column visibility management, header state persistence, and delegate assignment.

Definition at line 31 of file treeviewbase.h.

Constructor & Destructor Documentation

◆ TreeViewBase()

TreeViewBase::TreeViewBase ( QWidget *  parent = nullptr)

Construct with an optional parent widget.

Parameters
parentOptional QWidget parent

Member Function Documentation

◆ addHeaderContextMenuItems()

virtual void TreeViewBase::addHeaderContextMenuItems ( QMenu *  menu,
const QPoint &  globalPos 
)
inlineprotectedvirtual

Override to add application-specific items to the header context menu.

Parameters
menuMenu to populate
globalPosGlobal cursor position when the menu was requested

Definition at line 294 of file treeviewbase.h.

◆ collapseRecursively()

void TreeViewBase::collapseRecursively ( const QModelIndex &  index,
int  depth = -1 
)

Recursively collapse index and all its descendants up to a depth limit.

Parameters
indexRoot index to collapse from
depthMaximum depth to collapse (-1 for unlimited)

◆ columnForHeaderType()

int TreeViewBase::columnForHeaderType ( int  headerType) const

Return the column index for a given header type.

Parameters
headerTypeHeader type identifier
Returns
Column index, or -1 if not found

◆ currentIndexChanged

void TreeViewBase::currentIndexChanged ( const QModelIndex &  index)
signal

Emitted when the current index changes.

Parameters
indexThe newly current index

◆ currentMetadata()

virtual EntityMetadata TreeViewBase::currentMetadata ( ) const
virtual

Return the EntityMetadata of the currently selected item.

Returns
EntityMetadata for the current item

◆ currentSourceIndex()

QModelIndex TreeViewBase::currentSourceIndex ( ) const

Return the current source model index (mapped through the proxy).

Returns
Source model index corresponding to the current view index

◆ entityTypeAtPos()

int TreeViewBase::entityTypeAtPos ( const QPoint &  pos)

Return the entity type of the item at a view position.

Parameters
posView-local position
Returns
Entity type integer, or -1 if no item at that position

◆ finalChildIndex()

virtual QModelIndex TreeViewBase::finalChildIndex ( const QModelIndex &  from) const
virtual

Return the deepest last child of the given index.

Parameters
fromStarting index
Returns
Last descendant index

◆ findCurrentParent()

EntityMetadata TreeViewBase::findCurrentParent ( int  entityMetadataType) const
protected

Walk ancestors of the current index to find one with the given entity type.

Parameters
entityMetadataTypeEntity type to search for
Returns
EntityMetadata of the first matching ancestor

◆ findFirstParent()

EntityMetadata TreeViewBase::findFirstParent ( const QModelIndex &  index,
int  entityMetadataType 
) const
protected

Walk ancestors of index to find one with the given entity type.

Parameters
indexStarting index
entityMetadataTypeEntity type to search for
Returns
EntityMetadata of the first matching ancestor

◆ findNextMatch()

virtual QModelIndex TreeViewBase::findNextMatch ( const QString &  text,
const QModelIndex &  fromIndex 
) const
virtual

Find the next index whose display text matches, searching forward.

Parameters
textText to search for
fromIndexStarting index for the search
Returns
Next matching index, or invalid index if not found

◆ findParents()

QModelIndexList TreeViewBase::findParents ( const QModelIndex &  index) const
protected

Return all ancestor indexes of index.

Parameters
indexStarting index
Returns
List of ancestor indexes from nearest to root

◆ findPreviousMatch()

virtual QModelIndex TreeViewBase::findPreviousMatch ( const QString &  text,
const QModelIndex &  fromIndex 
) const
virtual

Find the previous index whose display text matches, searching backward.

Parameters
textText to search for
fromIndexStarting index for the backward search
Returns
Previous matching index, or invalid index if not found

◆ firstIndexOfEntityUuid()

virtual QModelIndex TreeViewBase::firstIndexOfEntityUuid ( const QUuid &  uuid) const
virtual

Return the first index whose item UUID matches.

Parameters
uuidUUID to search for
Returns
First matching index, or invalid index if not found

◆ indexesOfUuid()

virtual QModelIndexList TreeViewBase::indexesOfUuid ( const QUuid &  uuid) const
virtual

Return all indexes whose item UUID matches.

Parameters
uuidUUID to search for
Returns
List of matching indexes

◆ isIndexVisible()

bool TreeViewBase::isIndexVisible ( const QModelIndex &  index) const

Return whether the given index is visible in the current viewport.

Parameters
indexIndex to check
Returns
true if the index row is within the visible area

◆ isLeafExpanded()

bool TreeViewBase::isLeafExpanded ( const QModelIndex &  index,
bool  recursive = true 
) const

Return whether the deepest expanded leaf under index is expanded.

Parameters
indexRoot index to check
recursiveWhether to check recursively
Returns
true if the leaf is expanded

◆ mapToSource()

QModelIndexList TreeViewBase::mapToSource ( const QModelIndexList &  indexes) const
protected

Map a list of view indexes to their source model equivalents.

Parameters
indexesProxy model indexes to map
Returns
Corresponding source model indexes

◆ matchBackwards()

virtual QModelIndexList TreeViewBase::matchBackwards ( const QModelIndex &  start,
int  role,
const QVariant &  value,
int  hits = 1,
Qt::MatchFlags  flags = Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap) 
) const
virtual

Search backward from start for indexes matching a role value.

Parameters
startIndex to begin searching from
roleModel role to compare
valueValue to match
hitsMaximum number of matches to return
flagsMatch flags
Returns
List of matching indexes in reverse order

◆ metadataAtPos()

virtual EntityMetadata TreeViewBase::metadataAtPos ( const QPoint &  pos) const
virtual

Return the EntityMetadata of the item at a view position.

Parameters
posView-local position
Returns
EntityMetadata at that position

◆ nextIndex()

virtual QModelIndex TreeViewBase::nextIndex ( const QModelIndex &  from) const
virtual

Return the next index in display order after from.

Parameters
fromReference index
Returns
Next index, or invalid if at the end

◆ previousIndex()

virtual QModelIndex TreeViewBase::previousIndex ( const QModelIndex &  from) const
virtual

Return the previous index in display order before from.

Parameters
fromReference index
Returns
Previous index, or invalid if at the beginning

◆ proxyModel()

QSortFilterProxyModel * TreeViewBase::proxyModel ( ) const
inline

Return the internal sort/filter proxy model.

Returns
Pointer to the proxy model

Definition at line 183 of file treeviewbase.h.

◆ refreshIndex

virtual void TreeViewBase::refreshIndex ( const QModelIndex &  sourceIndex)
virtualslot

Refresh a single source model index.

Parameters
sourceIndexSource model index to refresh

◆ refreshVisibleColumns

virtual void TreeViewBase::refreshVisibleColumns ( const QList< int > &  columns)
virtualslot

Refresh the display of a set of columns.

Parameters
columnsColumn indexes to refresh

◆ refreshVisibleIndexes

virtual void TreeViewBase::refreshVisibleIndexes ( const QModelIndexList &  indexes)
virtualslot

Refresh the display of a set of source model indexes.

Parameters
indexesSource model indexes to refresh

◆ restoreState()

void TreeViewBase::restoreState ( const QByteArray &  state)

Restore header and expansion state from a byte array.

Parameters
statePreviously saved state

◆ saveState()

QByteArray TreeViewBase::saveState ( ) const

Save the header and expansion state to a byte array.

Returns
Serialized state

◆ setColumnDelegate()

void TreeViewBase::setColumnDelegate ( int  type,
QStyledItemDelegate *  delegate 
)

Assign a custom item delegate to the column of the given header type.

Parameters
typeColumn header type identifier
delegateDelegate to install

◆ setColumnsVisible()

void TreeViewBase::setColumnsVisible ( const QList< int > &  columns,
bool  visible,
bool  exclusive = false 
)

Show or hide columns by column index.

Parameters
columnsList of column indexes
visibletrue to show, false to hide
exclusiveIf true, hide all other columns

◆ setColumnTypesVisible()

void TreeViewBase::setColumnTypesVisible ( const QList< int > &  headerTypes,
bool  visible,
bool  exclusive = false 
)

Show or hide columns identified by header types.

Parameters
headerTypesList of header type identifiers
visibletrue to show, false to hide
exclusiveIf true, hide all other columns

◆ setCurrentIndex()

virtual void TreeViewBase::setCurrentIndex ( const QModelIndex &  index,
ScrollHint  scrollHint = EnsureVisible 
)
virtual

Set the current index and optionally scroll to it.

Parameters
indexIndex to make current
scrollHintHow to scroll to make the item visible

◆ setCurrentUuid()

virtual void TreeViewBase::setCurrentUuid ( const QUuid &  uuid,
ScrollHint  scrollHint = EnsureVisible 
)
virtual

Select the item with the given UUID and optionally scroll to it.

Parameters
uuidUUID of the item to select
scrollHintHow to scroll to make the item visible

◆ setModel()

virtual void TreeViewBase::setModel ( QAbstractItemModel *  model)
overridevirtual

Set the model, wrapping it in an internal sort/filter proxy.

Parameters
modelModel to display (should derive from AbstractItemModel)

◆ setSelectionModel()

virtual void TreeViewBase::setSelectionModel ( QItemSelectionModel *  selectionModel)
overridevirtual

Set the selection model.

Parameters
selectionModelSelection model to use

◆ sourceModel()

AbstractItemModel * TreeViewBase::sourceModel ( ) const
inline

Return the underlying AbstractItemModel (without the proxy).

Returns
Pointer to the source model

Definition at line 177 of file treeviewbase.h.


The documentation for this class was generated from the following file: