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 
27 #include <QColor>
28 #include <QStringList>
29 
31 
32 TreeModel::TreeModel(QObject* parent) : QAbstractItemModel(parent)
33 {
34 }
35 
37 {
38  if (rootItem != nullptr)
39  {
40  delete rootItem;
41  }
42 }
43 
44 int
45 TreeModel::columnCount(const QModelIndex& parent) const
46 {
47  return rootItem->columnCount();
48 }
49 
50 QVariant
51 TreeModel::data(const QModelIndex& index, int role) const
52 {
53  if (!index.isValid())
54  {
55  return QVariant();
56  }
57 
58  if (role != Qt::DisplayRole)
59  {
60  return QVariant();
61  }
62 
63  TreeItem* item = getItem(index);
64 
65  return item->data(index.column());
66 }
67 
68 Qt::ItemFlags
69 TreeModel::flags(const QModelIndex& index) const
70 {
71  if (!index.isValid())
72  {
73  return 0;
74  }
75 
76  TreeItem* item = getItem(index);
77  if (!item->isEnabled())
78  {
79  return QAbstractItemModel::flags(index) & (~Qt::ItemIsEnabled);
80  }
81  else
82  {
83  return QAbstractItemModel::flags(index) | (Qt::ItemIsEnabled);
84  }
85 }
86 
87 QVariant
88 TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
89 {
90  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
91  {
92  return rootItem->data(section);
93  }
94 
95  return QVariant();
96 }
97 
98 QModelIndex
99 TreeModel::index(int row, int column, const QModelIndex& parent) const
100 {
101  if (!hasIndex(row, column, parent))
102  {
103  return QModelIndex();
104  }
105 
106  TreeItem* parentItem = getItem(parent);
107 
108  TreeItem* childItem = parentItem->child(row);
109  if (childItem)
110  {
111  QModelIndex result = createIndex(row, column, childItem);
112  return result;
113  }
114  else
115  {
116  return QModelIndex();
117  }
118 }
119 
120 QModelIndex
121 TreeModel::parent(const QModelIndex& index) const
122 {
123  if (!index.isValid())
124  {
125  return QModelIndex();
126  }
127 
128  TreeItem* childItem = getItem(index);
129  TreeItem* parentItem = childItem->parent();
130 
131  if (parentItem == rootItem || parentItem == nullptr)
132  {
133  return QModelIndex();
134  }
135 
136  return createIndex(parentItem->row(), 0, parentItem);
137 }
138 
139 int
140 TreeModel::rowCount(const QModelIndex& parent) const
141 {
142  TreeItem* parentItem = getItem(parent);
143 
144  return parentItem->childCount();
145 }
146 
147 //TreeItem* TreeModel::getRootItem()
148 //{
149 // return rootItem;
150 //}
151 
152 
153 TreeItem*
154 TreeModel::getItem(const QModelIndex& index) const
155 {
156  if (index.isValid())
157  {
158  TreeItem* item = static_cast<TreeItem*>(index.internalPointer());
159  if (item)
160  {
161  return item;
162  }
163  }
164 
165  return rootItem;
166 }
167 
168 bool
169 TreeModel::insertColumn(int position, QVariant data, const QModelIndex& parent)
170 {
171  bool success;
172 
173  beginInsertColumns(parent, position, position);
174  success = rootItem->insertColumn(position, data);
175  endInsertColumns();
176 
177  return success;
178 }
179 
180 bool
181 TreeModel::removeColumn(int position, const QModelIndex& parent)
182 {
183  bool success;
184 
185  beginRemoveColumns(parent, position, position);
186  success = rootItem->removeColumn(position);
187  endRemoveColumns();
188 
189  if (rootItem->columnCount() == 0)
190  {
191  reset();
192  }
193 
194  return success;
195 }
196 
197 bool
198 TreeModel::insertRow(int position, TreeItem* item, const QModelIndex& parent)
199 {
200  TreeItem* parentItem = getItem(parent);
201  bool success = true;
202 
203  beginInsertRows(parent, position, position);
204  success = parentItem->insertChild(position, item);
205  endInsertRows();
206 
207  return success;
208 }
209 
210 bool
211 TreeModel::removeRow(int position, const QModelIndex& parent)
212 {
213  TreeItem* parentItem = getItem(parent);
214  bool success = true;
215 
216  beginRemoveRows(parent, position, position);
217  success = parentItem->removeChild(position);
218  endRemoveRows();
219 
220  return success;
221 }
TreeItem::columnCount
int columnCount() const
Definition: treeitem.cpp:63
TreeModel::~TreeModel
~TreeModel() override
Definition: treemodel.cpp:36
TreeModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: treemodel.cpp:99
GfxTL::Orientation
ScalarT Orientation(const VectorXD< 2, ScalarT > &p1, const VectorXD< 2, ScalarT > &p2, const VectorXD< 2, ScalarT > &c)
Definition: Orientation.h:10
TreeItem
Definition: treeitem.h:47
index
uint8_t index
Definition: EtherCATFrame.h:59
TreeModel::insertRow
bool insertRow(int position, TreeItem *item, const QModelIndex &parent=QModelIndex())
Definition: treemodel.cpp:198
TreeItem::removeColumn
bool removeColumn(int position)
Definition: treeitem.cpp:161
TreeItem::data
virtual QVariant data(int column) const
Definition: treeitem.cpp:69
TreeItem::parent
TreeItem * parent()
Definition: treeitem.cpp:75
TreeItem::insertChild
bool insertChild(int position, TreeItem *child)
Definition: treeitem.cpp:116
TreeModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: treemodel.cpp:69
TreeModel::parent
QModelIndex parent(const QModelIndex &index) const override
Definition: treemodel.cpp:121
TreeModel::removeColumn
bool removeColumn(int position, const QModelIndex &parent=QModelIndex())
Definition: treemodel.cpp:181
TreeItem::childCount
int childCount() const
Definition: treeitem.cpp:57
TreeItem::child
TreeItem * child(int row)
Definition: treeitem.cpp:51
TreeModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Definition: treemodel.cpp:88
TreeItem::isEnabled
bool isEnabled()
Definition: treeitem.cpp:98
TreeModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: treemodel.cpp:51
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
TreeModel::getItem
TreeItem * getItem(const QModelIndex &index) const
Definition: treemodel.cpp:154
TreeModel::TreeModel
TreeModel(QObject *parent=0)
Definition: treemodel.cpp:32
TreeModel::removeRow
bool removeRow(int position, const QModelIndex &parent=QModelIndex())
Definition: treemodel.cpp:211
TreeModel::insertColumn
bool insertColumn(int position, QVariant data, const QModelIndex &parent=QModelIndex())
Definition: treemodel.cpp:169
Logging.h
TreeItem::row
int row() const
Definition: treeitem.cpp:81
TreeModel::rootItem
TreeItem * rootItem
Definition: treemodel.h:69
TreeItem::removeChild
bool removeChild(int position)
Definition: treeitem.cpp:130
TreeItem::insertColumn
bool insertColumn(int position, QVariant data)
Definition: treeitem.cpp:143
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:140