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
32TreeModel::TreeModel(QObject* parent) : QAbstractItemModel(parent)
33{
34}
35
37{
38 if (rootItem != nullptr)
39 {
40 delete rootItem;
41 }
42}
43
44int
45TreeModel::columnCount(const QModelIndex& parent) const
46{
47 return rootItem->columnCount();
48}
49
50QVariant
51TreeModel::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
68Qt::ItemFlags
69TreeModel::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
87QVariant
88TreeModel::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
98QModelIndex
99TreeModel::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
120QModelIndex
121TreeModel::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
139int
140TreeModel::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
154TreeModel::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
168bool
169TreeModel::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
180bool
181TreeModel::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
197bool
198TreeModel::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
210bool
211TreeModel::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}
int childCount() const
Definition treeitem.cpp:57
virtual QVariant data(int column) const
Definition treeitem.cpp:69
int row() const
Definition treeitem.cpp:81
bool isEnabled()
Definition treeitem.cpp:98
bool removeChild(int position)
Definition treeitem.cpp:130
bool insertChild(int position, TreeItem *child)
Definition treeitem.cpp:116
TreeItem * parent()
Definition treeitem.cpp:75
TreeItem * child(int row)
Definition treeitem.cpp:51
bool insertColumn(int position, QVariant data, const QModelIndex &parent=QModelIndex())
TreeItem * rootItem
Definition treemodel.h:69
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Definition treemodel.cpp:88
bool insertRow(int position, TreeItem *item, const QModelIndex &parent=QModelIndex())
int rowCount(const QModelIndex &parent=QModelIndex()) const override
TreeModel(QObject *parent=0)
Definition treemodel.cpp:32
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition treemodel.cpp:69
bool removeRow(int position, const QModelIndex &parent=QModelIndex())
bool removeColumn(int position, const QModelIndex &parent=QModelIndex())
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition treemodel.cpp:99
~TreeModel() override
Definition treemodel.cpp:36
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition treemodel.cpp:45
QModelIndex parent(const QModelIndex &index) const override
TreeItem * getItem(const QModelIndex &index) const
QVariant data(const QModelIndex &index, int role) const override
Definition treemodel.cpp:51