Just added some safetychecks
[quassel.git] / src / client / treemodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but 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, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "global.h"
22 #include "treemodel.h"
23
24 /*****************************************
25  *  Buffer Items stored in the Tree Model
26  *****************************************/
27 TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
28   : QObject(parent),
29     itemData(data),
30     _parentItem(parent),
31     _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
32 {
33 }
34
35 TreeItem::TreeItem(TreeItem *parent)
36   : QObject(parent),
37     itemData(QList<QVariant>()),
38     _parentItem(parent),
39     _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
40 {
41 }
42
43 TreeItem::~TreeItem() {
44   qDeleteAll(_childItems);
45 }
46
47 uint TreeItem::id() const {
48   return (uint)this;
49 }
50
51 void TreeItem::appendChild(TreeItem *item) {
52   _childItems.append(item);
53   _childHash[item->id()] = item;
54   connect(item, SIGNAL(destroyed()),
55           this, SLOT(childDestroyed()));
56 }
57
58 void TreeItem::removeChild(int row) {
59   if(row >= _childItems.size())
60     return;
61   TreeItem *treeitem = _childItems.value(row);
62   _childItems.removeAt(row);
63   _childHash.remove(_childHash.key(treeitem));
64 }
65
66 TreeItem *TreeItem::child(int row) const {
67   if(row < _childItems.size())
68     return _childItems.value(row);
69   else
70     return 0;
71 }
72
73 TreeItem *TreeItem::childById(const uint &id) const {
74   if(_childHash.contains(id))
75     return _childHash.value(id);
76   else
77     return 0;
78 }
79
80 int TreeItem::childCount() const {
81   return _childItems.count();
82 }
83
84 int TreeItem::row() const {
85   if(_parentItem)
86     return _parentItem->_childItems.indexOf(const_cast<TreeItem*>(this));
87   else
88     return 0;
89 }
90
91 TreeItem *TreeItem::parent() {
92   return _parentItem;
93 }
94
95 int TreeItem::columnCount() const {
96   return itemData.count();
97 }
98
99 QVariant TreeItem::data(int column, int role) const {
100   if(role == Qt::DisplayRole && column < itemData.count())
101     return itemData[column];
102   else
103     return QVariant();
104 }
105
106 Qt::ItemFlags TreeItem::flags() const {
107   return _flags;
108 }
109
110 void TreeItem::setFlags(Qt::ItemFlags flags) {
111   _flags = flags;
112 }
113
114 void TreeItem::childDestroyed() {
115   TreeItem *item = static_cast<TreeItem*>(sender());
116   removeChild(item->row());
117 }
118   
119
120
121 /*****************************************
122  * TreeModel
123  *****************************************/
124 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
125   : QAbstractItemModel(parent)
126 {
127   rootItem = new TreeItem(data, 0);
128 }
129
130 TreeModel::~TreeModel() {
131   delete rootItem;
132 }
133
134 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
135   if(!hasIndex(row, column, parent))
136     return QModelIndex();
137   
138   TreeItem *parentItem;
139   
140   if(!parent.isValid())
141     parentItem = rootItem;
142   else
143     parentItem = static_cast<TreeItem*>(parent.internalPointer());
144   
145   TreeItem *childItem = parentItem->child(row);
146   if(childItem)
147     return createIndex(row, column, childItem);
148   else
149     return QModelIndex();
150 }
151
152 QModelIndex TreeModel::indexById(uint id, const QModelIndex &parent) const {
153   TreeItem *parentItem; 
154   
155   if(!parent.isValid())
156     parentItem = rootItem;
157   else
158     parentItem = static_cast<TreeItem *>(parent.internalPointer());
159   
160   TreeItem *childItem = parentItem->childById(id);
161   if(childItem)
162     return createIndex(childItem->row(), 0, childItem);
163   else
164     return QModelIndex();
165 }
166
167 QModelIndex TreeModel::parent(const QModelIndex &index) const {
168   if(!index.isValid())
169     return QModelIndex();
170   
171   TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
172   TreeItem *parentItem = static_cast<TreeItem*>(childItem->parent());
173   
174   if(parentItem == rootItem)
175     return QModelIndex();
176   
177   return createIndex(parentItem->row(), 0, parentItem);
178 }
179
180 int TreeModel::rowCount(const QModelIndex &parent) const {
181   TreeItem *parentItem;
182   if(!parent.isValid())
183     parentItem = rootItem;
184   else
185     parentItem = static_cast<TreeItem*>(parent.internalPointer());
186   
187   return parentItem->childCount();
188 }
189
190 int TreeModel::columnCount(const QModelIndex &parent) const {
191   if(parent.isValid())
192     return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
193   else
194     return rootItem->columnCount();
195 }
196
197 QVariant TreeModel::data(const QModelIndex &index, int role) const {
198   if(!index.isValid())
199     return QVariant();
200
201   TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
202   return item->data(index.column(), role);
203 }
204
205 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
206   TreeItem *item;
207   if(!index.isValid())
208     item = rootItem;
209   else
210     item = static_cast<TreeItem *>(index.internalPointer());
211   return item->flags();
212 }
213
214 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
215   if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
216     return rootItem->data(section, role);
217   else
218     return QVariant();
219 }
220
221 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
222   if(row > rowCount(parent))
223     return false;
224   
225   TreeItem *item;
226   if(!parent.isValid())
227     item = rootItem;
228   else
229     item = static_cast<TreeItem*>(parent.internalPointer());
230   
231   beginRemoveRows(parent, row, row);
232   item->removeChild(row);
233   endRemoveRows();
234   return true;
235 }
236
237 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
238   // check if there is work to be done
239   if(count == 0)
240     return true;
241
242   // out of range check
243   if(row + count - 1 > rowCount(parent) || row < 0 || count < 0) 
244     return false;
245   
246   TreeItem *item;
247   if(!parent.isValid())
248     item = rootItem;
249   else
250     item = static_cast<TreeItem*>(parent.internalPointer());
251   
252   
253   beginRemoveRows(parent, row, row + count - 1);
254
255   for(int i = row + count - 1; i >= 0; i--) {
256     item->removeChild(i);
257   }
258   endRemoveRows();
259   return true;
260 }
261
262 void TreeModel::clear() {
263   removeRows(0, rowCount());
264 }