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