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