65310a6244c22d81a459369051e5a5a1cc8dc014
[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 #include <QCoreApplication>
25
26 /*****************************************
27  *  Abstract Items of a TreeModel
28  *****************************************/
29 AbstractTreeItem::AbstractTreeItem(AbstractTreeItem *parent)
30   : QObject(parent),
31     _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
32 {
33 }
34
35 AbstractTreeItem::~AbstractTreeItem() {
36 }
37
38 quint64 AbstractTreeItem::id() const {
39   return qHash(this);
40 }
41
42 bool AbstractTreeItem::newChild(AbstractTreeItem *item) {
43   // check if a child with that ID is already known
44   Q_ASSERT(childById(item->id()) == 0);
45     
46   int newRow = childCount();
47   emit beginAppendChilds(newRow, newRow);
48   _childItems.append(item);
49   emit endAppendChilds();
50   
51   return true;
52 }
53
54 bool AbstractTreeItem::newChilds(const QList<AbstractTreeItem *> &items) {
55   if(items.isEmpty())
56     return false;
57   
58   QList<AbstractTreeItem *>::const_iterator itemIter = items.constBegin();
59   AbstractTreeItem *item;
60   while(itemIter != items.constEnd()) {
61     item = *itemIter;
62     if(childById(item->id()) != 0) {
63       qWarning() << "AbstractTreeItem::newChilds(): received child that is already attached" << item << item->id();
64       return false;
65     }
66     itemIter++;
67   }
68
69   int nextRow = childCount();
70   int lastRow = nextRow + items.count() - 1;
71
72   emit beginAppendChilds(nextRow, lastRow);
73   itemIter = items.constBegin();
74   while(itemIter != items.constEnd()) {
75     _childItems.append(*itemIter);
76     itemIter++;
77   }
78   emit endAppendChilds();
79
80   return true;
81 }
82
83 bool AbstractTreeItem::removeChild(int row) {
84   if(childCount() <= row)
85     return false;
86
87   child(row)->removeAllChilds();
88   emit beginRemoveChilds(row, row);
89   AbstractTreeItem *treeitem = _childItems.takeAt(row);
90   treeitem->deleteLater();
91   emit endRemoveChilds();
92
93   return true;
94 }
95
96 bool AbstractTreeItem::removeChildById(const quint64 &id) {
97   const int numChilds = childCount();
98   
99   for(int i = 0; i < numChilds; i++) {
100     if(_childItems[i]->id() == id)
101       return removeChild(i);
102   }
103   
104   return false;
105 }
106
107 void AbstractTreeItem::removeAllChilds() {
108   const int numChilds = childCount();
109   
110   if(numChilds == 0)
111     return;
112   
113   AbstractTreeItem *child;
114
115   QList<AbstractTreeItem *>::iterator childIter;
116
117   childIter = _childItems.begin();
118   while(childIter != _childItems.end()) {
119     child = *childIter;
120     child->removeAllChilds();
121     childIter++;
122   }
123
124   emit beginRemoveChilds(0, numChilds - 1);
125   childIter = _childItems.begin();
126   while(childIter != _childItems.end()) {
127     child = *childIter;
128     childIter = _childItems.erase(childIter);
129     child->deleteLater();
130   }
131   emit endRemoveChilds();
132 }
133
134 AbstractTreeItem *AbstractTreeItem::child(int row) const {
135   if(childCount() <= row)
136     return 0;
137   else
138     return _childItems[row];
139 }
140
141 AbstractTreeItem *AbstractTreeItem::childById(const quint64 &id) const {
142   const int numChilds = childCount();
143   for(int i = 0; i < numChilds; i++) {
144     if(_childItems[i]->id() == id)
145       return _childItems[i];
146   }
147   return 0;
148 }
149
150 int AbstractTreeItem::childCount() const {
151   return _childItems.count();
152 }
153
154 int AbstractTreeItem::row() const {
155   if(!parent())
156     return -1;
157   else
158     return parent()->_childItems.indexOf(const_cast<AbstractTreeItem *>(this));
159 }
160
161 AbstractTreeItem *AbstractTreeItem::parent() const {
162   return qobject_cast<AbstractTreeItem *>(QObject::parent());
163 }
164
165 Qt::ItemFlags AbstractTreeItem::flags() const {
166   return _flags;
167 }
168
169 void AbstractTreeItem::setFlags(Qt::ItemFlags flags) {
170   _flags = flags;
171 }
172
173 void AbstractTreeItem::dumpChildList() {
174   qDebug() << "==== Childlist for Item:" << this << id() << "====";
175   if(childCount() > 0) {
176     AbstractTreeItem *child;
177     QList<AbstractTreeItem *>::const_iterator childIter = _childItems.constBegin();
178     while(childIter != _childItems.constEnd()) {
179       child = *childIter;
180       qDebug() << "Row:" << child->row() << child << child->id() << child->data(0, Qt::DisplayRole);
181       childIter++;
182     }
183   }
184   qDebug() << "==== End Of Childlist ====";  
185 }
186
187 /*****************************************
188  * SimpleTreeItem
189  *****************************************/
190 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
191   : AbstractTreeItem(parent),
192     _itemData(data)
193 {
194 }
195
196 SimpleTreeItem::~SimpleTreeItem() {
197 }
198
199 QVariant SimpleTreeItem::data(int column, int role) const {
200   if(column >= columnCount() || role != Qt::DisplayRole)
201     return QVariant();
202   else
203     return _itemData[column];
204 }
205
206 bool SimpleTreeItem::setData(int column, const QVariant &value, int role) {
207   if(column > columnCount() || role != Qt::DisplayRole)
208     return false;
209
210   if(column == columnCount())
211     _itemData.append(value);
212   else
213     _itemData[column] = value;
214
215   emit dataChanged(column);
216   return true;
217 }
218
219 int SimpleTreeItem::columnCount() const {
220   return _itemData.count();
221 }
222
223 /*****************************************
224  * PropertyMapItem
225  *****************************************/
226 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
227   : AbstractTreeItem(parent),
228     _propertyOrder(propertyOrder)
229 {
230 }
231
232 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
233   : AbstractTreeItem(parent),
234     _propertyOrder(QStringList())
235 {
236 }
237
238
239 PropertyMapItem::~PropertyMapItem() {
240 }
241   
242 QVariant PropertyMapItem::data(int column, int role) const {
243   if(column >= columnCount())
244     return QVariant();
245
246   switch(role) {
247   case Qt::ToolTipRole:
248     return toolTip(column);
249   case Qt::DisplayRole:
250   case TreeModel::SortRole:  // fallthrough, since SortRole should default to DisplayRole
251     return property(_propertyOrder[column].toAscii());
252   default:
253     return QVariant();
254   }
255   
256 }
257
258 bool PropertyMapItem::setData(int column, const QVariant &value, int role) {
259   if(column >= columnCount() || role != Qt::DisplayRole)
260     return false;
261
262   emit dataChanged(column);
263   return setProperty(_propertyOrder[column].toAscii(), value);
264 }
265
266 int PropertyMapItem::columnCount() const {
267   return _propertyOrder.count();
268 }
269   
270 void PropertyMapItem::appendProperty(const QString &property) {
271   _propertyOrder << property;
272 }
273
274
275
276 /*****************************************
277  * TreeModel
278  *****************************************/
279 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
280   : QAbstractItemModel(parent),
281     _childStatus(QModelIndex(), 0, 0, 0),
282     _aboutToRemoveOrInsert(false)
283 {
284   rootItem = new SimpleTreeItem(data, 0);
285   connectItem(rootItem);
286
287   if(QCoreApplication::instance()->arguments().contains("--debugmodel")) {
288     connect(this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
289             this, SLOT(debug_rowsAboutToBeInserted(const QModelIndex &, int, int)));
290     connect(this, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
291             this, SLOT(debug_rowsAboutToBeRemoved(const QModelIndex &, int, int)));
292     connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
293             this, SLOT(debug_rowsInserted(const QModelIndex &, int, int)));
294     connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
295             this, SLOT(debug_rowsRemoved(const QModelIndex &, int, int)));
296   }
297 }
298
299 TreeModel::~TreeModel() {
300   delete rootItem;
301 }
302
303 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
304   if(!hasIndex(row, column, parent))
305     return QModelIndex();
306   
307   AbstractTreeItem *parentItem;
308   
309   if(!parent.isValid())
310     parentItem = rootItem;
311   else
312     parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
313   
314   AbstractTreeItem *childItem = parentItem->child(row);
315
316   if(childItem)
317     return createIndex(row, column, childItem);
318   else
319     return QModelIndex();
320 }
321
322 QModelIndex TreeModel::indexById(quint64 id, const QModelIndex &parent) const {
323   AbstractTreeItem *parentItem; 
324   
325   if(!parent.isValid())
326     parentItem = rootItem;
327   else
328     parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
329   
330   AbstractTreeItem *childItem = parentItem->childById(id);
331   
332   if(childItem)
333     return createIndex(childItem->row(), 0, childItem);
334   else
335     return QModelIndex();
336 }
337
338 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
339   if(item == 0) {
340     qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
341     return QModelIndex();
342   }
343   
344   if(item == rootItem)
345     return QModelIndex();
346   else
347     return createIndex(item->row(), 0, item);
348 }
349
350 QModelIndex TreeModel::parent(const QModelIndex &index) const {
351   if(!index.isValid()) {
352     qWarning() << "TreeModel::parent(): has been asked for the rootItems Parent!";
353     return QModelIndex();
354   }
355   
356   AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
357   AbstractTreeItem *parentItem = childItem->parent();
358
359   Q_ASSERT(parentItem);
360   if(parentItem == rootItem)
361     return QModelIndex();
362   
363   return createIndex(parentItem->row(), 0, parentItem);
364 }
365
366 int TreeModel::rowCount(const QModelIndex &parent) const {
367   AbstractTreeItem *parentItem;
368   if(!parent.isValid())
369     parentItem = rootItem;
370   else
371     parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
372
373   return parentItem->childCount();
374 }
375
376 int TreeModel::columnCount(const QModelIndex &parent) const {
377   Q_UNUSED(parent)
378   // since there the Qt Views don't draw more columns than the header has columns
379   // we can be lazy and simply return the count of header columns
380   // actually this gives us more freedom cause we don't have to ensure that a rows parent
381   // has equal or more columns than that row
382   
383 //   if(parent.isValid()) {
384 //     AbstractTreeItem *child;
385 //     if(child = static_cast<AbstractTreeItem *>(parent.internalPointer())->child(parent.column(), parent.row()))
386 //       return child->columnCount();
387 //     else
388 //       return static_cast<AbstractTreeItem*>(parent.internalPointer())->columnCount();
389 //   } else {
390 //     return rootItem->columnCount();
391 //   }
392
393   return rootItem->columnCount();
394 }
395
396 QVariant TreeModel::data(const QModelIndex &index, int role) const {
397   if(!index.isValid())
398     return QVariant();
399
400   AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
401   return item->data(index.column(), role);
402 }
403
404 bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
405   if(!index.isValid())
406     return false;
407
408   AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
409   return item->setData(index.column(), value, role);
410 }
411
412 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
413   AbstractTreeItem *item;
414   if(!index.isValid())
415     item = rootItem;
416   else
417     item = static_cast<AbstractTreeItem *>(index.internalPointer());
418   return item->flags();
419 }
420
421 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
422   if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
423     return rootItem->data(section, role);
424   else
425     return QVariant();
426 }
427
428 void TreeModel::itemDataChanged(int column) {
429   AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
430   QModelIndex leftIndex, rightIndex;
431
432   if(item == rootItem)
433     return;
434
435   if(column == -1) {
436     leftIndex = createIndex(item->row(), 0, item);
437     rightIndex = createIndex(item->row(), item->columnCount() - 1, item);
438   } else {
439     leftIndex = createIndex(item->row(), column, item);
440     rightIndex = leftIndex;
441   }
442
443   emit dataChanged(leftIndex, rightIndex);
444 }
445
446 void TreeModel::connectItem(AbstractTreeItem *item) {
447   connect(item, SIGNAL(dataChanged(int)),
448           this, SLOT(itemDataChanged(int)));
449   
450   connect(item, SIGNAL(beginAppendChilds(int, int)),
451           this, SLOT(beginAppendChilds(int, int)));
452   connect(item, SIGNAL(endAppendChilds()),
453           this, SLOT(endAppendChilds()));
454   
455   connect(item, SIGNAL(beginRemoveChilds(int, int)),
456           this, SLOT(beginRemoveChilds(int, int)));
457   connect(item, SIGNAL(endRemoveChilds()),
458           this, SLOT(endRemoveChilds()));
459 }
460
461 void TreeModel::beginAppendChilds(int firstRow, int lastRow) {
462   AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
463   if(!parentItem) {
464     qWarning() << "TreeModel::beginAppendChilds(): cannot append Childs to unknown parent";
465     return;
466   }
467
468   QModelIndex parent = indexByItem(parentItem);
469   Q_ASSERT(!_aboutToRemoveOrInsert);
470   
471   _aboutToRemoveOrInsert = true;
472   _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
473   beginInsertRows(parent, firstRow, lastRow);
474 }
475
476 void TreeModel::endAppendChilds() {
477   AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
478   if(!parentItem) {
479     qWarning() << "TreeModel::endAppendChilds(): cannot append Childs to unknown parent";
480     return;
481   }
482   Q_ASSERT(_aboutToRemoveOrInsert);
483   ChildStatus cs = _childStatus;
484   QModelIndex parent = indexByItem(parentItem);
485   Q_ASSERT(cs.parent == parent);
486   Q_ASSERT(rowCount(parent) == cs.childCount + cs.end - cs.start + 1);
487
488   _aboutToRemoveOrInsert = false;
489   for(int i = cs.start; i <= cs.end; i++) {
490     connectItem(parentItem->child(i));
491   }
492   endInsertRows();
493 }
494
495 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
496   AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
497   if(!parentItem) {
498     qWarning() << "TreeModel::beginRemoveChilds(): cannot append Childs to unknown parent";
499     return;
500   }
501   QModelIndex parent = indexByItem(parentItem);
502   Q_ASSERT(firstRow <= lastRow);
503   Q_ASSERT(parentItem->childCount() > lastRow);
504   Q_ASSERT(!_aboutToRemoveOrInsert);
505   
506   _aboutToRemoveOrInsert = true;
507   _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
508   beginRemoveRows(parent, firstRow, lastRow);
509 }
510
511 void TreeModel::endRemoveChilds() {
512   AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
513   if(!parentItem) {
514     qWarning() << "TreeModel::endRemoveChilds(): cannot append Childs to unknown parent";
515     return;
516   }
517   Q_ASSERT(_aboutToRemoveOrInsert);
518   ChildStatus cs = _childStatus;
519   QModelIndex parent = indexByItem(parentItem);
520   Q_ASSERT(cs.parent == parent);
521   Q_ASSERT(rowCount(parent) == cs.childCount - cs.end + cs.start - 1);
522   
523   _aboutToRemoveOrInsert = false;
524   endRemoveRows();
525 }
526
527 void TreeModel::clear() {
528   rootItem->removeAllChilds();
529 }
530
531 void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
532   qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
533 }
534
535 void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
536   AbstractTreeItem *parentItem;
537   parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
538   if(!parentItem)
539     parentItem = rootItem;
540   qDebug() << "#" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
541
542   QModelIndex child;
543   AbstractTreeItem *childItem;
544   for(int i = end; i >= start; i--) {
545     child = parent.child(i, 0);
546     childItem = parentItem->child(i);
547     Q_ASSERT(childItem);
548     qDebug() << ">>>" << i << child << childItem->id() << child.data().toString();
549   }
550 }
551
552 void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end) {
553   AbstractTreeItem *parentItem;
554   parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
555   if(!parentItem)
556     parentItem = rootItem;
557   qDebug() << "#" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
558
559   QModelIndex child;
560   AbstractTreeItem *childItem;
561   for(int i = start; i <= end; i++) {
562     child = parent.child(i, 0);
563     childItem = parentItem->child(i);
564     Q_ASSERT(childItem);
565     qDebug() << "<<<" << i << child << childItem->id() << child.data().toString();
566   }
567 }
568
569 void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
570   qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
571 }