46 #include "TreeModel.hh" 100 if (!_index.isValid())
107 std::cerr <<
"Root" << std::endl;
111 if ( _role == Qt::BackgroundRole ) {
113 return QVariant (QBrush (QColor (192, 192, 192)));
117 if (_role == Qt::DisplayRole)
119 switch (_index.column ())
122 return QVariant(item->
id());
124 return QVariant(item->
name());
143 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) 144 if (!_index.isValid())
147 Qt::ItemFlags
flags = 0;
149 if (!_index.isValid())
150 return Qt::ItemFlags();
152 Qt::ItemFlags flags = Qt::ItemFlags();
156 if ( _index.column() == 0 || _index.column() == 1 )
157 flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
159 flags = Qt::ItemIsEnabled;
177 if (_orientation == Qt::Horizontal && _role == Qt::DisplayRole) {
180 return QVariant(
"ID");
181 else if (_section == 1)
182 return QVariant(
"Name");
204 if (!_parent.isValid())
207 parentItem =
static_cast<TreeItem*
>(_parent.internalPointer());
211 return createIndex(_row, _column, childItem);
213 return QModelIndex();
226 if (!_index.isValid())
227 return QModelIndex();
233 return QModelIndex();
235 return createIndex(parentItem->
row(), 0, parentItem);
249 if (_parent.column() > 0)
252 if (!_parent.isValid())
255 parentItem =
static_cast<TreeItem*
>(_parent.internalPointer());
277 if (obj != 0 && item != 0){
284 if ( index.isValid() )
285 emit dataChanged( index, index);
296 if ( index0.isValid() && index1.isValid() ){
298 emit dataChanged( index0, index1);
375 if ( item != 0 && !
isRoot(item) ){
378 QModelIndex parentIndex = itemIndex.parent();
380 beginRemoveRows( parentIndex, itemIndex.row(), itemIndex.row() );
401 QModelIndex oldParentIndex = itemIndex.parent();
405 beginRemoveRows( oldParentIndex, itemIndex.row(), itemIndex.row() );
417 emit layoutChanged();
429 if (_index.isValid()) {
431 if (item)
return item;
446 if (_index.isValid()) {
463 if (_index.isValid()) {
485 return QModelIndex();
487 QModelIndex
index = createIndex(_object->
row(), _column, _object);
509 return QModelIndex();
523 if (
isRoot(_item) || (!_item->isGroup()) )
533 emit dataChanged( index0, index1);
538 emit dataChanged(index, index);
557 bool changed =
false;
576 emit dataChanged(index, index);
579 if ( current->isGroup() )
589 emit dataChangedInside(
itemId(_index), _index.column(), _value );
int itemId(const QModelIndex &_index) const
Get the id of a TreeItem corresponding to a given ModelIndex.
QString itemName(const QModelIndex &_index) const
Get the name of a TreeItem corresponding to a given ModelIndex.
int row() const
get the row of this item from the parent
void deleteSubtree()
delete the whole subtree below this item ( The item itself is not touched )
void moveItem(TreeItem *_item, TreeItem *_parent)
move the item to a new parent
TreeItem * child(int row)
return a child
TreeModel(QObject *_parent=0)
Constructor.
TreeItem * childExists(int _objectId)
Check if the element exists in the subtree of this element.
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
BaseObject *& objectRoot()
Get the root of the object structure.
int columnCount(const QModelIndex &_parent=QModelIndex()) const
Return the number of columns.
void objectDeleted(int id_)
The object with the given id has been deleted. delete it from the internal tree.
void setParent(TreeItem *_parent)
Set the parent pointer.
bool setData(const QModelIndex &_index, const QVariant &_value, int _role)
Set Data at 'index' to 'value'.
QVariant headerData(int _section, Qt::Orientation _orientation, int _role=Qt::DisplayRole) const
return the header data of the model
TreeItem * parent()
Get the parent item ( 0 if root item )
void appendChild(TreeItem *child)
add a child to this node
bool dataType(DataType _type) const
QModelIndex parent(const QModelIndex &_index) const
Get the parent ModelIndex.
BaseObject * parent()
Get the parent item ( 0 if rootitem )
void objectAdded(BaseObject *_object)
The object with the given id has been added. add it to the internal tree.
QString name() const
return the name of the object. The name defaults to NONAME if unset.
void objectChanged(int id_)
The object with the given id has been changed. Check if model also has to be changed.
QModelIndex index(int _row, int _column, const QModelIndex &_parent=QModelIndex()) const
Get the ModelIndex at given row,column.
bool isGroup() const
Check if object is a group.
QVariant data(const QModelIndex &_index, int _role) const
Get the data of the corresponding entry.
void propagateUpwards(TreeItem *_obj, int _column, bool _value)
Recursively update a column up to the root of the tree.
Qt::ItemFlags flags(const QModelIndex &_index) const
return the types of the corresponding entry
void propagateDownwards(TreeItem *_obj, int _column)
Recursively update a column up to the root of the tree.
bool isRoot(TreeItem *_item)
Check if the given item is the root item.
virtual bool visible()
return if object is visible
TreeItem * getItem(const QModelIndex &_index) const
Get the TreeItem corresponding to a given ModelIndex.
int rowCount(const QModelIndex &_parent=QModelIndex()) const
get the number of rows
QModelIndex getModelIndex(TreeItem *_object, int _column)
Return the ModelIndex corresponding to a given TreeItem and Column.
TreeItem * rootItem_
Rootitem of the tree.
void removeChild(TreeItem *_item)
Remove a child from this object.
int childCount() const
get the number of children
const DataType DATA_UNKNOWN(0)
None of the other Objects.