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