HAL
base_tree_model.cpp
Go to the documentation of this file.
3 
4 namespace hal
5 {
6 
8  {
10  }
11 
12  QVariant BaseTreeModel::data(const QModelIndex &index, int role) const
13  {
14  if(!index.isValid())
15  return QVariant();
16 
18  if(!item)
19  return QVariant();
20 
21  //The standard model only displays the text
22  if(role == Qt::DisplayRole)
23  return item->getData(index.column());
24 
25  return QVariant();
26  }
27 
28  QVariant BaseTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
29  {
30  if(role == Qt::DisplayRole && orientation == Qt::Horizontal && section < mRootItem->getColumnCount() && section >= 0)
31  return mRootItem->getData(section);
32  else
33  return QVariant();
34  }
35 
36  QModelIndex BaseTreeModel::index(int row, int column, const QModelIndex &parent) const
37  {
38  if(!hasIndex(row, column, parent))
39  return QModelIndex();
40 
41  BaseTreeItem* parentItem = parent.isValid() ? getItemFromIndex(parent) : mRootItem;
42  BaseTreeItem* childItem = parentItem->getChild(row);
43  return (childItem) ? createIndex(row, column, childItem) : QModelIndex();
44  }
45 
47  {
48  if(!index.isValid())
49  return QModelIndex();
50 
51  BaseTreeItem* currentItem = getItemFromIndex(index);
52  if(!currentItem)
53  return QModelIndex();
54 
55  BaseTreeItem* parentItem = currentItem->getParent();
56  if(parentItem == mRootItem)
57  return QModelIndex();
58 
59  return getIndexFromItem(parentItem);
60  }
61 
63  {
65  }
66 
67  int BaseTreeModel::rowCount(const QModelIndex &parent) const
68  {
69  if(!parent.isValid()) // no valid parent = root item
70  return mRootItem->getChildCount();
71  else if (parent.column()) // only first column is allowed to have children
72  return 0;
73  else
75  }
76 
77  int BaseTreeModel::columnCount(const QModelIndex &parent) const
78  {
79  Q_UNUSED(parent)
80  return mRootItem->getColumnCount();
81  }
82 
84  {
85  for(auto item : firstLevelItems)
86  mRootItem->appendChild(item);
87  }
88 
90  {
92  //delete all children, not the root item
93  while(mRootItem->getChildCount() > 0)
94  {
96  delete tmp;
97  }
98  endResetModel();
99  }
100 
102  {
103  QStringList retval;
104  int ncol = columnCount();
105  for (int icol = 0; icol < ncol; icol++)
106  retval.append(headerData(icol,Qt::Horizontal,Qt::DisplayRole).toString());
107  return retval;
108  }
109 
111  {
112  if(!mRootItem)
113  mRootItem = new RootTreeItem(labels);
114  else
115  for (int i=0; i<labels.size(); i++) {
116  QVariant qv = QVariant(labels.at(i));
117  mRootItem->setDataAtIndex(i, qv);
118  }
119 
120 
121  }
122 
124  {
125  assert(item);
126 
127  BaseTreeItem* parentItem = item->getParent();
128 
129  // if the given item has no parent, it is the root item
130  if(!parentItem)
131  return QModelIndex();
132 
133  // get the row of the item and create the modelindex
134  int itemRow = parentItem->getRowForChild(item);
135  if(itemRow != -1)
136  return createIndex(itemRow, 0, item);
137 
138  return QModelIndex();
139  }
140 
142  {
143  return (index.isValid()) ? static_cast<BaseTreeItem*>(index.internalPointer()) : nullptr;
144  }
145 
147  {
148  return mRootItem;
149  }
150 
151  void BaseTreeModel::insertChildItem(BaseTreeItem* childItem, BaseTreeItem* parentItem, int row)
152  {
153  if (!parentItem) parentItem = mRootItem;
154  if (row < 0) row = parentItem->getChildCount();
155  QModelIndex index = getIndexFromItem(parentItem);
156  beginInsertRows(index, row, row);
157  parentItem->insertChild(row,childItem);
158  endInsertRows();
159  }
160 
161 }
(Future) Base class for all tree models related to the details widget.
virtual int getRowForChild(const BaseTreeItem *child) const
virtual BaseTreeItem * getParent() const
virtual int getChildCount() const
virtual void appendChild(BaseTreeItem *child)
virtual BaseTreeItem * removeChildAtPos(int row)
virtual void insertChild(int index, BaseTreeItem *child)
virtual QVariant getData(int column) const =0
virtual BaseTreeItem * getChild(int row) const
QModelIndex getIndexFromItem(BaseTreeItem *item) const
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const override
RootTreeItem * mRootItem
QStringList headerLabels() const
BaseTreeModel(QObject *parent=nullptr)
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
virtual void clear()
virtual Qt::ItemFlags flags(const QModelIndex &index) const override
void insertChildItem(BaseTreeItem *childItem, BaseTreeItem *parentItem=nullptr, int row=-1)
void setContent(QList< BaseTreeItem * > firstLevelItems)
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
BaseTreeItem * getRootItem() const
void setHeaderLabels(const QStringList &label)
BaseTreeItem * getItemFromIndex(QModelIndex index) const
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const override
int getColumnCount() const override
QVariant getData(int column) const override
void setDataAtIndex(int index, QVariant &data) override
void beginInsertRows(const QModelIndex &parent, int first, int last)
QModelIndex createIndex(int row, int column, void *ptr) const const
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
bool hasIndex(int row, int column, const QModelIndex &parent) const const
void append(const T &value)
const T & at(int i) const const
int size() const const
int column() const const
void * internalPointer() const const
bool isValid() const const
QObject * parent() const const
DisplayRole
typedef ItemFlags
Orientation