1 #include <Gui/TreeModel/TreeModel.hpp>
3 #include <Core/Utils/Log.hpp>
6 #include <QApplication>
11 using namespace Ra::Core::Utils;
15 if ( parent.column() > 0 ) {
return 0; }
16 TreeItem* parentItem = getItem( parent );
21 if ( index.isValid() ) {
23 CORE_ASSERT( item,
"Null item found" );
26 CORE_ASSERT( findInTree( item ),
"Item is not in tree" );
29 return m_rootItem.get();
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 );
44 bool 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 ) {
47 setAllItemsChecked(
false );
48 setItemChecked( index,
true );
50 else { setItemChecked( index, value.toBool() ); }
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(); }
68 TreeItem* parentItem = getItem( parent );
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() ) {
77 TreeItem* childItem = getItem( child );
78 TreeItem* parentItem = childItem->m_parent;
81 if ( parentItem && parentItem != m_rootItem.get() ) {
90 return index.isValid() && getItem( index )->isValid() && getItem( index )->isSelectable()
91 ? QAbstractItemModel::flags( index ) | Qt::ItemIsUserCheckable
105 std::stack<const TreeItem*> stack;
106 stack.push( m_rootItem.get() );
107 while ( !stack.empty() ) {
108 const TreeItem* current = stack.top();
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";
121 std::stack<const TreeItem*> stack;
122 stack.push( m_rootItem.get() );
123 while ( !stack.empty() ) {
124 const TreeItem* current = stack.top();
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 );
137 setItemChecked( index, checked );
142 if ( index.isValid() ) {
145 item->setChecked( checked );
146 emit dataChanged( index, index, { Qt::CheckStateRole } );
149 for (
int i = 0; i < rowCount( index ); ++i ) {
150 const QModelIndex child = this->index( i, 0, index );
151 setItemChecked( child, checked );
int getIndexInParent() const
std::vector< std::unique_ptr< TreeItem > > m_children
Children of item in the tree.
virtual bool isSelectable() const =0
Return true if the represented object can be selected.
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.
QModelIndex parent(const QModelIndex &child) const override
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.