3#include <Core/Utils/Log.hpp>
11using namespace Ra::Core::Utils;
15 if (
parent.column() > 0 ) {
return 0; }
21 if (
index.isValid() ) {
23 CORE_ASSERT( item,
"Null item found" );
26 CORE_ASSERT(
findInTree( item ),
"Item is not in tree" );
33 if (
index.isValid() &&
index.column() == 0 ) {
34 if ( role == Qt::DisplayRole ) {
35 return QVariant( QString::fromStdString(
getItem(
index )->getName() ) );
37 else if ( role == Qt::CheckStateRole ) {
38 return static_cast<int>(
getItem(
index )->isChecked() ? Qt::Checked : Qt::Unchecked );
44bool TreeModel::setData(
const QModelIndex& index,
const QVariant& value,
int role ) {
45 if (
index.isValid() &&
index.column() == 0 && role == Qt::CheckStateRole ) {
46 if ( QApplication::keyboardModifiers() == Qt::CTRL ) {
57 if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
58 return QVariant( QString::fromStdString( getHeaderString() ) );
60 else {
return QVariant(); }
64 if ( !hasIndex( row, column,
parent ) ) {
return QModelIndex(); }
66 if (
parent.isValid() &&
parent.column() != 0 ) {
return QModelIndex(); }
69 if ( parentItem &&
size_t( row ) < parentItem->
m_children.size() ) {
70 return createIndex( row, column, parentItem->
m_children[row].get() );
72 else {
return QModelIndex(); }
76 if ( child.isValid() ) {
78 TreeItem* parentItem = childItem->m_parent;
81 if ( parentItem && parentItem !=
m_rootItem.get() ) {
91 ? QAbstractItemModel::flags(
index ) | Qt::ItemIsUserCheckable
107 while ( !stack.
empty() ) {
110 if ( current == item ) {
return true; }
111 for (
const auto& child : current->
m_children ) {
112 stack.
push( child.get() );
119#if defined CORE_DEBUG
120 LOG( logDEBUG ) <<
" Printing tree model";
123 while ( !stack.
empty() ) {
126 LOG( logDEBUG ) << current->getName();
127 for (
const auto& child : current->
m_children ) {
128 stack.
push( child.get() );
135 for (
int row = 0; row <
rowCount(); ++row ) {
136 const QModelIndex
index = this->
index( row, 0 );
142 if (
index.isValid() ) {
145 item->setChecked( checked );
146 emit dataChanged(
index,
index, { Qt::CheckStateRole } );
150 const QModelIndex child = this->
index( i, 0,
index );
Base class for element of the tree representation.
int getIndexInParent() const
std::vector< std::unique_ptr< TreeItem > > m_children
Children of item in the tree.
virtual bool isValid() const =0
Return true if the represented object is valid.
virtual bool isSelectable() const =0
Return true if the represented object can be selected.
void modelRebuilt()
Emitted when the model has been rebuilt.
TreeItem * getItem(const QModelIndex &index) const
Get the tree item corresponding to the given index.
void setAllItemsChecked(bool checked=true)
Set the check state of all items.
void setItemChecked(const QModelIndex &index, bool checked=true)
Set the check state of one item and all of its children.
virtual void buildModel()=0
QModelIndex parent(const QModelIndex &child) const override
std::unique_ptr< TreeItem > m_rootItem
Root of the tree.
Qt::ItemFlags flags(const QModelIndex &index) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role) const override
void printModel() const
Prints elements in the model to stdout (debug only)
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
bool findInTree(const TreeItem *item) const
Internal functions to check if an item is in the tree.
hepler function to manage enum as underlying types in VariableSet