Fixing Issues with the NetworkModel. Though the performance still
[quassel.git] / src / client / treemodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
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     _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
31 {
32 }
33
34 AbstractTreeItem::~AbstractTreeItem() {
35   removeAllChilds();
36 }
37
38 quint64 AbstractTreeItem::id() const {
39   return (quint64)this;
40 }
41
42 int AbstractTreeItem::defaultColumn() const {
43   // invalid QModelIndexes aka rootNodes get their Childs stuffed into column -1
44   // all others to 0
45   if(parent() == 0)
46     return -1;
47   else
48     return 0;
49 }
50
51 void AbstractTreeItem::appendChild(int column, AbstractTreeItem *item) {
52   if(!_childItems.contains(column)) {
53     _childItems[column] = QList<AbstractTreeItem *>();
54     _childHash[column] = QHash<quint64, AbstractTreeItem *>();
55   }
56   
57   _childItems[column].append(item);
58   _childHash[column][item->id()] = item;
59
60   connect(item, SIGNAL(destroyed()), this, SLOT(childDestroyed()));
61 }
62
63 void AbstractTreeItem::appendChild(AbstractTreeItem *child) {
64   appendChild(defaultColumn(), child);
65 }
66
67 void AbstractTreeItem::removeChild(int column, int row) {
68   if(!_childItems.contains(column)
69      || _childItems[column].size() <= row)
70     return;
71
72   if(column == defaultColumn())
73     emit beginRemoveChilds(row, row);
74   
75   AbstractTreeItem *treeitem = _childItems[column].value(row);
76   _childItems[column].removeAt(row);
77   _childHash[column].remove(_childHash[column].key(treeitem));
78   disconnect(treeitem, 0, this, 0);
79   treeitem->deleteLater();
80
81   if(column == defaultColumn())
82     emit endRemoveChilds();
83 }
84
85 void AbstractTreeItem::removeChild(int row) {
86   removeChild(defaultColumn(), row);
87 }
88
89 void AbstractTreeItem::removeAllChilds() {
90   emit beginRemoveChilds(0, childCount() - 1);
91
92   AbstractTreeItem *child;
93   foreach(int key, _childItems.keys()) {
94     QList<AbstractTreeItem *>::iterator iter = _childItems[key].begin();
95     while(iter != _childItems[key].end()) {
96       child = *iter;
97       iter = _childItems[key].erase(iter);
98       disconnect(child, 0, this, 0);
99       child->removeAllChilds();
100       child->deleteLater();
101     }
102   }
103   emit endRemoveChilds();
104 }
105
106 AbstractTreeItem *AbstractTreeItem::child(int column, int row) const {
107   if(!_childItems.contains(column)
108      || _childItems[column].size() <= row)
109     return 0;
110   else
111     return _childItems[column].value(row);
112 }
113
114 AbstractTreeItem *AbstractTreeItem::child(int row) const {
115   return child(defaultColumn(), row);
116 }
117
118 AbstractTreeItem *AbstractTreeItem::childById(int column, const quint64 &id) const {
119   if(!_childHash.contains(column)
120      || !_childHash[column].contains(id))
121     return 0;
122   else
123     return _childHash[column].value(id);
124 }
125
126 AbstractTreeItem *AbstractTreeItem::childById(const quint64 &id) const {
127   return childById(defaultColumn(), id);
128 }
129
130 int AbstractTreeItem::childCount(int column) const {
131   if(!_childItems.contains(column))
132     return 0;
133   else
134     return _childItems[column].count();
135 }
136
137 int AbstractTreeItem::childCount() const {
138   return childCount(defaultColumn());
139 }
140
141 int AbstractTreeItem::column() const {
142   if(!parent())
143     return -1;
144
145   QHash<int, QList<AbstractTreeItem*> >::const_iterator iter = parent()->_childItems.constBegin();
146   while(iter != parent()->_childItems.constEnd()) {
147     if(iter.value().contains(const_cast<AbstractTreeItem *>(this)))
148       return iter.key();
149     iter++;
150   }
151
152   // unable to find us o_O
153   return parent()->defaultColumn();
154 }
155
156 int AbstractTreeItem::row() const {
157   if(!parent())
158     return -1;
159   else
160     return parent()->_childItems[column()].indexOf(const_cast<AbstractTreeItem*>(this));
161 }
162
163 AbstractTreeItem *AbstractTreeItem::parent() const {
164   return qobject_cast<AbstractTreeItem *>(QObject::parent());
165 }
166
167 Qt::ItemFlags AbstractTreeItem::flags() const {
168   return _flags;
169 }
170
171 void AbstractTreeItem::setFlags(Qt::ItemFlags flags) {
172   _flags = flags;
173 }
174
175 void AbstractTreeItem::childDestroyed() {
176   AbstractTreeItem *item = static_cast<AbstractTreeItem*>(sender());
177
178   if(!item) {
179     qWarning() << "AbstractTreeItem::childDestroyed() received null pointer!";
180     return;
181   }
182
183   QHash<int, QList<AbstractTreeItem*> >::const_iterator iter = _childItems.constBegin();
184   int column, row = -1;
185   while(iter != _childItems.constEnd()) {
186     row = iter.value().indexOf(item);
187     if(row != -1) {
188       column = iter.key();
189       break;
190     }
191     iter++;
192   }
193
194   if(row == -1) {
195     qWarning() << "AbstractTreeItem::childDestroyed(): unknown Child died:" << item << "parent:" << this;
196     return;
197   }
198   
199   _childItems[column].removeAt(row);
200   _childHash[column].remove(_childHash[column].key(item));
201   emit beginRemoveChilds(row, row);
202   emit endRemoveChilds();
203 }
204   
205 /*****************************************
206  * SimpleTreeItem
207  *****************************************/
208 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
209   : AbstractTreeItem(parent),
210     _itemData(data)
211 {
212 }
213
214 SimpleTreeItem::~SimpleTreeItem() {
215 }
216
217 QVariant SimpleTreeItem::data(int column, int role) const {
218   if(role == Qt::DisplayRole && column < _itemData.count())
219     return _itemData[column];
220   else
221     return QVariant();
222 }
223
224 int SimpleTreeItem::columnCount() const {
225   return _itemData.count();
226 }
227
228 /*****************************************
229  * PropertyMapItem
230  *****************************************/
231 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
232   : AbstractTreeItem(parent),
233     _propertyOrder(propertyOrder)
234 {
235 }
236
237 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
238   : AbstractTreeItem(parent),
239     _propertyOrder(QStringList())
240 {
241 }
242
243
244 PropertyMapItem::~PropertyMapItem() {
245 }
246   
247 QVariant PropertyMapItem::data(int column, int role) const {
248   if(column >= columnCount())
249     return QVariant();
250
251   if(role != Qt::DisplayRole)
252     return QVariant();
253
254   return property(_propertyOrder[column].toAscii());
255 }
256
257 int PropertyMapItem::columnCount() const {
258   return _propertyOrder.count();
259 }
260   
261 void PropertyMapItem::appendProperty(const QString &property) {
262   _propertyOrder << property;
263 }
264
265
266
267 /*****************************************
268  * TreeModel
269  *****************************************/
270 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
271   : QAbstractItemModel(parent)
272 {
273   rootItem = new SimpleTreeItem(data, 0);
274 }
275
276 TreeModel::~TreeModel() {
277   delete rootItem;
278 }
279
280 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
281   if(!hasIndex(row, column, parent))
282     return QModelIndex();
283   
284   AbstractTreeItem *parentItem;
285   
286   if(!parent.isValid())
287     parentItem = rootItem;
288   else
289     parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
290   
291   AbstractTreeItem *childItem = parentItem->child(parent.column(), row);
292
293   if(childItem)
294     return createIndex(row, column, childItem);
295   else
296     return QModelIndex();
297 }
298
299 QModelIndex TreeModel::indexById(quint64 id, const QModelIndex &parent) const {
300   AbstractTreeItem *parentItem; 
301   
302   if(!parent.isValid())
303     parentItem = rootItem;
304   else
305     parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
306   
307   AbstractTreeItem *childItem = parentItem->childById(parent.column(), id);
308   
309   if(childItem)
310     return createIndex(childItem->row(), 0, childItem);
311   else
312     return QModelIndex();
313 }
314
315 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
316   if(item == 0) {
317     qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
318     return QModelIndex();
319   }
320   
321   if(item == rootItem)
322     return QModelIndex();
323   else
324     return createIndex(item->row(), 0, item);
325
326 }
327
328 QModelIndex TreeModel::parent(const QModelIndex &index) const {
329   if(!index.isValid())
330     return QModelIndex();
331   
332   AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
333   AbstractTreeItem *parentItem = static_cast<AbstractTreeItem *>(childItem->parent());
334   
335   if(parentItem == rootItem)
336     return QModelIndex();
337   
338   return createIndex(parentItem->row(), 0, parentItem);
339 }
340
341 int TreeModel::rowCount(const QModelIndex &parent) const {
342   AbstractTreeItem *parentItem;
343   if(!parent.isValid())
344     parentItem = rootItem;
345   else
346     parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
347
348   return parentItem->childCount(parent.column());
349 }
350
351 int TreeModel::columnCount(const QModelIndex &parent) const {
352   Q_UNUSED(parent)
353   // since there the Qt Views don't draw more columns than the header has columns
354   // we can be lazy and simply return the count of header columns
355   // actually this gives us more freedom cause we don't have to ensure that a rows parent
356   // has equal or more columns than that row
357   
358 //   if(parent.isValid()) {
359 //     AbstractTreeItem *child;
360 //     if(child = static_cast<AbstractTreeItem *>(parent.internalPointer())->child(parent.column(), parent.row()))
361 //       return child->columnCount();
362 //     else
363 //       return static_cast<AbstractTreeItem*>(parent.internalPointer())->columnCount();
364 //   } else {
365 //     return rootItem->columnCount();
366 //   }
367
368   return rootItem->columnCount();
369 }
370
371 QVariant TreeModel::data(const QModelIndex &index, int role) const {
372   if(!index.isValid())
373     return QVariant();
374
375   AbstractTreeItem *item = static_cast<AbstractTreeItem*>(index.internalPointer());
376   return item->data(index.column(), role);
377 }
378
379 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
380   AbstractTreeItem *item;
381   if(!index.isValid())
382     item = rootItem;
383   else
384     item = static_cast<AbstractTreeItem *>(index.internalPointer());
385   return item->flags();
386 }
387
388 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
389   if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
390     return rootItem->data(section, role);
391   else
392     return QVariant();
393 }
394
395 void TreeModel::itemDataChanged(int column) {
396   AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
397   QModelIndex leftIndex, rightIndex;
398
399   if(item == rootItem)
400     return;
401
402   if(column == -1) {
403     leftIndex = createIndex(item->row(), 0, item);
404     rightIndex = createIndex(item->row(), item->columnCount()-1, item);
405   } else {
406     leftIndex = createIndex(item->row(), column, item);
407     rightIndex = leftIndex;
408   }
409
410   emit dataChanged(leftIndex, rightIndex);
411 }
412
413 void TreeModel::appendChild(AbstractTreeItem *parent, AbstractTreeItem *child) {
414   if(parent == 0 or child == 0) {
415     qWarning() << "TreeModel::appendChild(parent, child) parent and child have to be valid pointers!" << parent << child;
416     return;
417   }
418
419   int nextRow = parent->childCount();
420   beginInsertRows(indexByItem(parent), nextRow, nextRow);
421   parent->appendChild(child);
422   endInsertRows();
423
424   connect(child, SIGNAL(dataChanged(int)),
425           this, SLOT(itemDataChanged(int)));
426   
427   connect(child, SIGNAL(newChild(AbstractTreeItem *)),
428           this, SLOT(newChild(AbstractTreeItem *)));
429
430 //   connect(child, SIGNAL(childRemoved(int)),
431 //        this, SLOT(childRemoved(int)));
432
433   connect(child, SIGNAL(beginRemoveChilds(int, int)),
434           this, SLOT(beginRemoveChilds(int, int)));
435   
436   connect(child, SIGNAL(endRemoveChilds()),
437           this, SLOT(endRemoveChilds()));
438 }
439
440 void TreeModel::newChild(AbstractTreeItem *child) {
441   appendChild(static_cast<AbstractTreeItem *>(sender()), child);
442 }
443
444 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
445   QModelIndex parent = indexByItem(static_cast<AbstractTreeItem *>(sender()));
446   beginRemoveRows(parent, firstRow, lastRow);
447 }
448
449 void TreeModel::endRemoveChilds() {
450   endRemoveRows();
451 }
452
453 void TreeModel::childRemoved(int row) {
454   QModelIndex parent = indexByItem(static_cast<AbstractTreeItem *>(sender()));
455   beginRemoveRows(parent, row, row);
456   endRemoveRows();
457 }
458
459 void TreeModel::childsRemoved(int firstRow, int lastRow) {
460   QModelIndex parent = indexByItem(static_cast<AbstractTreeItem *>(sender()));
461   beginRemoveRows(parent, firstRow, lastRow);
462   endRemoveRows();
463   
464 }
465
466 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
467   if(row > rowCount(parent))
468     return false;
469   
470   AbstractTreeItem *item;
471   if(!parent.isValid())
472     item = rootItem;
473   else
474     item = static_cast<AbstractTreeItem*>(parent.internalPointer());
475   
476   beginRemoveRows(parent, row, row);
477   item->removeChild(parent.column(), row);
478   endRemoveRows();
479   return true;
480 }
481
482 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
483   // check if there is work to be done
484   if(count == 0)
485     return true;
486
487   // out of range check
488   if(row + count - 1 > rowCount(parent) || row < 0 || count < 0) 
489     return false;
490   
491   AbstractTreeItem *item;
492   if(!parent.isValid())
493     item = rootItem;
494   else
495     item = static_cast<AbstractTreeItem *>(parent.internalPointer());
496   
497   
498   beginRemoveRows(parent, row, row + count - 1);
499
500   for(int i = row + count - 1; i >= 0; i--) {
501     item->removeChild(parent.column(), i);
502   }
503   endRemoveRows();
504   return true;
505 }
506
507 void TreeModel::clear() {
508   removeRows(0, rowCount());
509   reset();
510 }