3ae2bc3aafd9407bcaa2d25754d7ecdd6c660e85
[quassel.git] / src / client / treemodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC 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) version 3.                                           *
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 "treemodel.h"
22
23 #include <QDebug>
24
25 /*****************************************
26  *  Abstract Items of a TreeModel
27  *****************************************/
28 AbstractTreeItem::AbstractTreeItem(AbstractTreeItem *parent)
29   : QObject(parent),
30     _parentItem(parent),
31     _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
32 {
33 }
34
35 AbstractTreeItem::~AbstractTreeItem() {
36   foreach(int key, _childItems.keys()) {
37     qDeleteAll(_childItems[key]);
38   }
39 }
40
41 quint64 AbstractTreeItem::id() const {
42   return (quint64)this;
43 }
44
45 int AbstractTreeItem::defaultColumn() const {
46   // invalid QModelIndexes aka rootNodes get their Childs stuffed into column -1
47   // all others to 0
48   if(_parentItem == 0)
49     return -1;
50   else
51     return 0;
52 }
53
54 void AbstractTreeItem::appendChild(int column, AbstractTreeItem *item) {
55   if(!_childItems.contains(column)) {
56     _childItems[column] = QList<AbstractTreeItem *>();
57     _childHash[column] = QHash<quint64, AbstractTreeItem *>();
58   }
59   
60   _childItems[column].append(item);
61   _childHash[column][item->id()] = item;
62
63   connect(item, SIGNAL(destroyed()), this, SLOT(childDestroyed()));
64 }
65
66 void AbstractTreeItem::appendChild(AbstractTreeItem *child) {
67   appendChild(defaultColumn(), child);
68 }
69
70 void AbstractTreeItem::removeChild(int column, int row) {
71   if(!_childItems.contains(column)
72      || _childItems[column].size() <= row)
73     return;
74   
75   AbstractTreeItem *treeitem = _childItems[column].value(row);
76   _childItems[column].removeAt(row);
77   _childHash[column].remove(_childHash[column].key(treeitem));
78   treeitem->deleteLater();
79 }
80
81 void AbstractTreeItem::removeChild(int row) {
82   removeChild(defaultColumn(), row);
83 }
84
85 AbstractTreeItem *AbstractTreeItem::child(int column, int row) const {
86   if(!_childItems.contains(column)
87      || _childItems[column].size() <= row)
88     return 0;
89   else
90     return _childItems[column].value(row);
91 }
92
93 AbstractTreeItem *AbstractTreeItem::child(int row) const {
94   return child(defaultColumn(), row);
95 }
96
97 AbstractTreeItem *AbstractTreeItem::childById(int column, const uint &id) const {
98   if(!_childHash.contains(column)
99      || !_childHash[column].contains(id))
100     return 0;
101   else
102     return _childHash[column].value(id);
103 }
104
105 AbstractTreeItem *AbstractTreeItem::childById(const uint &id) const {
106   return childById(defaultColumn(), id);
107 }
108
109 int AbstractTreeItem::childCount(int column) const {
110   if(!_childItems.contains(column))
111     return 0;
112   else
113     return _childItems[column].count();
114 }
115
116 int AbstractTreeItem::childCount() const {
117   return childCount(defaultColumn());
118 }
119
120 int AbstractTreeItem::column() const {
121   if(!_parentItem)
122     return -1;
123
124   QHash<int, QList<AbstractTreeItem*> >::const_iterator iter = _parentItem->_childItems.constBegin();
125   int pos;
126   while(iter != _parentItem->_childItems.constEnd()) {
127     pos = iter.value().indexOf(const_cast<AbstractTreeItem *>(this));
128     if(pos != -1)
129       return iter.key();
130     iter++;
131   }
132
133   // unable to find us o_O
134   return _parentItem->defaultColumn();
135 }
136
137 int AbstractTreeItem::row() const {
138   if(!_parentItem)
139     return -1;
140   else
141     return _parentItem->_childItems[column()].indexOf(const_cast<AbstractTreeItem*>(this));
142 }
143
144 AbstractTreeItem *AbstractTreeItem::parent() {
145   return _parentItem;
146 }
147
148 Qt::ItemFlags AbstractTreeItem::flags() const {
149   return _flags;
150 }
151
152 void AbstractTreeItem::setFlags(Qt::ItemFlags flags) {
153   _flags = flags;
154 }
155
156 void AbstractTreeItem::childDestroyed() {
157   AbstractTreeItem *item = static_cast<AbstractTreeItem*>(sender());
158   removeChild(item->column(), item->row());
159 }
160   
161 /*****************************************
162  * SimpleTreeItem
163  *****************************************/
164 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
165   : AbstractTreeItem(parent),
166     _itemData(data)
167 {
168 }
169
170 SimpleTreeItem::~SimpleTreeItem() {
171 }
172
173 QVariant SimpleTreeItem::data(int column, int role) const {
174   if(role == Qt::DisplayRole && column < _itemData.count())
175     return _itemData[column];
176   else
177     return QVariant();
178 }
179
180 int SimpleTreeItem::columnCount() const {
181   return _itemData.count();
182 }
183
184 /*****************************************
185  * PropertyMapItem
186  *****************************************/
187 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
188   : AbstractTreeItem(parent),
189     _propertyOrder(propertyOrder)
190 {
191 }
192
193 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
194   : AbstractTreeItem(parent),
195     _propertyOrder(QStringList())
196 {
197 }
198
199
200 PropertyMapItem::~PropertyMapItem() {
201 }
202   
203 QVariant PropertyMapItem::data(int column, int role) const {
204   if(column >= columnCount())
205     return QVariant();
206
207   if(role != Qt::DisplayRole)
208     return QVariant();
209
210   return property(_propertyOrder[column].toAscii());
211 }
212
213 int PropertyMapItem::columnCount() const {
214   return _propertyOrder.count();
215 }
216   
217 void PropertyMapItem::appendProperty(const QString &property) {
218   _propertyOrder << property;
219 }
220
221
222
223 /*****************************************
224  * TreeModel
225  *****************************************/
226 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
227   : QAbstractItemModel(parent)
228 {
229   rootItem = new SimpleTreeItem(data, 0);
230 }
231
232 TreeModel::~TreeModel() {
233   delete rootItem;
234 }
235
236 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
237   if(!hasIndex(row, column, parent))
238     return QModelIndex();
239   
240   AbstractTreeItem *parentItem;
241   
242   if(!parent.isValid())
243     parentItem = rootItem;
244   else
245     parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
246   
247   AbstractTreeItem *childItem = parentItem->child(parent.column(), row);
248
249   if(childItem)
250     return createIndex(row, column, childItem);
251   else
252     return QModelIndex();
253 }
254
255 QModelIndex TreeModel::indexById(uint id, const QModelIndex &parent) const {
256   AbstractTreeItem *parentItem; 
257   
258   if(!parent.isValid())
259     parentItem = rootItem;
260   else
261     parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
262   
263   AbstractTreeItem *childItem = parentItem->childById(parent.column(), id);
264   
265   if(childItem)
266     return createIndex(childItem->row(), 0, childItem);
267   else
268     return QModelIndex();
269 }
270
271 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
272   if(item == 0) {
273     qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
274     return QModelIndex();
275   }
276   
277   if(item == rootItem)
278     return QModelIndex();
279   else
280     return createIndex(item->row(), 0, item);
281
282 }
283
284 QModelIndex TreeModel::parent(const QModelIndex &index) const {
285   if(!index.isValid())
286     return QModelIndex();
287   
288   AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
289   AbstractTreeItem *parentItem = static_cast<AbstractTreeItem *>(childItem->parent());
290   
291   if(parentItem == rootItem)
292     return QModelIndex();
293   
294   return createIndex(parentItem->row(), 0, parentItem);
295 }
296
297 int TreeModel::rowCount(const QModelIndex &parent) const {
298   AbstractTreeItem *parentItem;
299   if(!parent.isValid())
300     parentItem = rootItem;
301   else
302     parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
303
304   return parentItem->childCount(parent.column());
305 }
306
307 int TreeModel::columnCount(const QModelIndex &parent) const {
308   if(parent.isValid())
309     return static_cast<AbstractTreeItem*>(parent.internalPointer())->columnCount();
310   else
311     return rootItem->columnCount();
312 }
313
314 QVariant TreeModel::data(const QModelIndex &index, int role) const {
315   if(!index.isValid())
316     return QVariant();
317
318   AbstractTreeItem *item = static_cast<AbstractTreeItem*>(index.internalPointer());
319   return item->data(index.column(), role);
320 }
321
322 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
323   AbstractTreeItem *item;
324   if(!index.isValid())
325     item = rootItem;
326   else
327     item = static_cast<AbstractTreeItem *>(index.internalPointer());
328   return item->flags();
329 }
330
331 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
332   if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
333     return rootItem->data(section, role);
334   else
335     return QVariant();
336 }
337
338 void TreeModel::itemDataChanged(int column) {
339   AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
340   QModelIndex itemIndex;
341
342   if(item == rootItem) 
343     itemIndex = QModelIndex();
344   else
345     itemIndex = createIndex(item->row(), column, item);
346
347   emit dataChanged(itemIndex, itemIndex);
348 }
349
350 void TreeModel::appendChild(AbstractTreeItem *parent, AbstractTreeItem *child) {
351   if(parent == 0 or child == 0) {
352     qWarning() << "TreeModel::apendChild(parent, child) parent and child have to be valid pointers!" << parent << child;
353     return;
354   }
355
356   int nextRow = parent->childCount();
357   beginInsertRows(indexByItem(parent), nextRow, nextRow);
358   parent->appendChild(child);
359   endInsertRows();
360
361   connect(child, SIGNAL(dataChanged(int)),
362           this, SLOT(itemDataChanged(int)));
363 }
364
365
366 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
367   if(row > rowCount(parent))
368     return false;
369   
370   AbstractTreeItem *item;
371   if(!parent.isValid())
372     item = rootItem;
373   else
374     item = static_cast<AbstractTreeItem*>(parent.internalPointer());
375   
376   beginRemoveRows(parent, row, row);
377   item->removeChild(parent.column(), row);
378   endRemoveRows();
379   return true;
380 }
381
382 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
383   // check if there is work to be done
384   if(count == 0)
385     return true;
386
387   // out of range check
388   if(row + count - 1 > rowCount(parent) || row < 0 || count < 0) 
389     return false;
390   
391   AbstractTreeItem *item;
392   if(!parent.isValid())
393     item = rootItem;
394   else
395     item = static_cast<AbstractTreeItem *>(parent.internalPointer());
396   
397   
398   beginRemoveRows(parent, row, row + count - 1);
399
400   for(int i = row + count - 1; i >= 0; i--) {
401     item->removeChild(parent.column(), i);
402   }
403   endRemoveRows();
404   return true;
405 }
406
407 void TreeModel::clear() {
408   removeRows(0, rowCount());
409   reset();
410 }