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