treemodel.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXCore::gui
19  * @author Cedric Seehausen (usdnr at kit dot edu)
20  * @date 2016
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "treemodel.h"
26 
28 
29 #include <QColor>
30 #include <QStringList>
31 
32 TreeModel::TreeModel(QObject* parent)
33  : QAbstractItemModel(parent)
34 {
35 }
36 
38 {
39  if (rootItem != nullptr)
40  {
41  delete rootItem;
42  }
43 }
44 
45 int TreeModel::columnCount(const QModelIndex& parent) const
46 {
47  return rootItem->columnCount();
48 }
49 
50 QVariant TreeModel::data(const QModelIndex& index, int role) const
51 {
52  if (!index.isValid())
53  {
54  return QVariant();
55  }
56 
57  if (role != Qt::DisplayRole)
58  {
59  return QVariant();
60  }
61 
62  TreeItem* item = getItem(index);
63 
64  return item->data(index.column());
65 }
66 
67 Qt::ItemFlags TreeModel::flags(const QModelIndex& index) const
68 {
69  if (!index.isValid())
70  {
71  return 0;
72  }
73 
74  TreeItem* item = getItem(index);
75  if (!item->isEnabled())
76  {
77  return QAbstractItemModel::flags(index) & (~Qt::ItemIsEnabled);
78  }
79  else
80  {
81  return QAbstractItemModel::flags(index) | (Qt::ItemIsEnabled);
82  }
83 }
84 
85 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
86  int role) const
87 {
88  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
89  {
90  return rootItem->data(section);
91  }
92 
93  return QVariant();
94 }
95 
96 QModelIndex TreeModel::index(int row, int column, const QModelIndex& parent)
97 const
98 {
99  if (!hasIndex(row, column, parent))
100  {
101  return QModelIndex();
102  }
103 
104  TreeItem* parentItem = getItem(parent);
105 
106  TreeItem* childItem = parentItem->child(row);
107  if (childItem)
108  {
109  QModelIndex result = createIndex(row, column, childItem);
110  return result;
111  }
112  else
113  {
114  return QModelIndex();
115  }
116 }
117 
118 QModelIndex TreeModel::parent(const QModelIndex& index) const
119 {
120  if (!index.isValid())
121  {
122  return QModelIndex();
123  }
124 
125  TreeItem* childItem = getItem(index);
126  TreeItem* parentItem = childItem->parent();
127 
128  if (parentItem == rootItem || parentItem == nullptr)
129  {
130  return QModelIndex();
131  }
132 
133  return createIndex(parentItem->row(), 0, parentItem);
134 }
135 
136 int TreeModel::rowCount(const QModelIndex& parent) const
137 {
138  TreeItem* parentItem = getItem(parent);
139 
140  return parentItem->childCount();
141 }
142 
143 
144 //TreeItem* TreeModel::getRootItem()
145 //{
146 // return rootItem;
147 //}
148 
149 
150 TreeItem* TreeModel::getItem(const QModelIndex& index) const
151 {
152  if (index.isValid())
153  {
154  TreeItem* item = static_cast<TreeItem*>(index.internalPointer());
155  if (item)
156  {
157  return item;
158  }
159  }
160 
161  return rootItem;
162 }
163 
164 
165 bool TreeModel::insertColumn(int position, QVariant data, const QModelIndex& parent)
166 {
167  bool success;
168 
169  beginInsertColumns(parent, position, position);
170  success = rootItem->insertColumn(position, data);
171  endInsertColumns();
172 
173  return success;
174 }
175 
176 bool TreeModel::removeColumn(int position, const QModelIndex& parent)
177 {
178  bool success;
179 
180  beginRemoveColumns(parent, position, position);
181  success = rootItem->removeColumn(position);
182  endRemoveColumns();
183 
184  if (rootItem->columnCount() == 0)
185  {
186  reset();
187  }
188 
189  return success;
190 }
191 
192 bool TreeModel::insertRow(int position, TreeItem* item, const QModelIndex& parent)
193 {
194  TreeItem* parentItem = getItem(parent);
195  bool success = true;
196 
197  beginInsertRows(parent, position, position);
198  success = parentItem->insertChild(position, item);
199  endInsertRows();
200 
201  return success;
202 }
203 
204 bool TreeModel::removeRow(int position, const QModelIndex& parent)
205 {
206  TreeItem* parentItem = getItem(parent);
207  bool success = true;
208 
209  beginRemoveRows(parent, position, position);
210  success = parentItem->removeChild(position);
211  endRemoveRows();
212 
213  return success;
214 }
215 
216 
TreeItem::columnCount
int columnCount() const
Definition: treeitem.cpp:59
TreeModel::~TreeModel
~TreeModel() override
Definition: treemodel.cpp:37
TreeModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: treemodel.cpp:96
GfxTL::Orientation
ScalarT Orientation(const VectorXD< 2, ScalarT > &p1, const VectorXD< 2, ScalarT > &p2, const VectorXD< 2, ScalarT > &c)
Definition: Orientation.h:9
TreeItem
Definition: treeitem.h:46
index
uint8_t index
Definition: EtherCATFrame.h:59
TreeModel::insertRow
bool insertRow(int position, TreeItem *item, const QModelIndex &parent=QModelIndex())
Definition: treemodel.cpp:192
TreeItem::removeColumn
bool removeColumn(int position)
Definition: treeitem.cpp:147
TreeItem::data
virtual QVariant data(int column) const
Definition: treeitem.cpp:64
TreeItem::parent
TreeItem * parent()
Definition: treeitem.cpp:69
TreeItem::insertChild
bool insertChild(int position, TreeItem *child)
Definition: treeitem.cpp:105
TreeModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: treemodel.cpp:67
TreeModel::parent
QModelIndex parent(const QModelIndex &index) const override
Definition: treemodel.cpp:118
TreeModel::removeColumn
bool removeColumn(int position, const QModelIndex &parent=QModelIndex())
Definition: treemodel.cpp:176
TreeItem::childCount
int childCount() const
Definition: treeitem.cpp:54
TreeItem::child
TreeItem * child(int row)
Definition: treeitem.cpp:49
TreeModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Definition: treemodel.cpp:85
TreeItem::isEnabled
bool isEnabled()
Definition: treeitem.cpp:89
TreeModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: treemodel.cpp:50
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
TreeModel::getItem
TreeItem * getItem(const QModelIndex &index) const
Definition: treemodel.cpp:150
TreeModel::TreeModel
TreeModel(QObject *parent=0)
Definition: treemodel.cpp:32
TreeModel::removeRow
bool removeRow(int position, const QModelIndex &parent=QModelIndex())
Definition: treemodel.cpp:204
TreeModel::insertColumn
bool insertColumn(int position, QVariant data, const QModelIndex &parent=QModelIndex())
Definition: treemodel.cpp:165
Logging.h
TreeItem::row
int row() const
Definition: treeitem.cpp:74
TreeModel::rootItem
TreeItem * rootItem
Definition: treemodel.h:70
TreeItem::removeChild
bool removeChild(int position)
Definition: treeitem.cpp:118
TreeItem::insertColumn
bool insertColumn(int position, QVariant data)
Definition: treeitem.cpp:130
TreeModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition: treemodel.cpp:45
treemodel.h
TreeModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition: treemodel.cpp:136