1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "treemodel.h"
23 #include <QCoreApplication>
28 class RemoveChildLaterEvent : public QEvent {
30 RemoveChildLaterEvent(AbstractTreeItem *child) : QEvent(QEvent::User), _child(child) {};
31 inline AbstractTreeItem *child() { return _child; }
33 AbstractTreeItem *_child;
37 /*****************************************
38 * Abstract Items of a TreeModel
39 *****************************************/
40 AbstractTreeItem::AbstractTreeItem(AbstractTreeItem *parent)
42 _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled),
47 bool AbstractTreeItem::newChild(AbstractTreeItem *item) {
48 int newRow = childCount();
49 emit beginAppendChilds(newRow, newRow);
50 _childItems.append(item);
51 emit endAppendChilds();
55 bool AbstractTreeItem::newChilds(const QList<AbstractTreeItem *> &items) {
59 int nextRow = childCount();
60 int lastRow = nextRow + items.count() - 1;
62 emit beginAppendChilds(nextRow, lastRow);
64 emit endAppendChilds();
69 bool AbstractTreeItem::removeChild(int row) {
70 if(row < 0 || childCount() <= row)
73 child(row)->removeAllChilds();
74 emit beginRemoveChilds(row, row);
75 AbstractTreeItem *treeitem = _childItems.takeAt(row);
77 emit endRemoveChilds();
84 void AbstractTreeItem::removeAllChilds() {
85 const int numChilds = childCount();
90 AbstractTreeItem *child;
92 QList<AbstractTreeItem *>::iterator childIter;
94 childIter = _childItems.begin();
95 while(childIter != _childItems.end()) {
97 child->setTreeItemFlags(0); // disable self deletion, as this would only fuck up consitency and the child gets deleted anyways
98 child->removeAllChilds();
102 emit beginRemoveChilds(0, numChilds - 1);
103 childIter = _childItems.begin();
104 while(childIter != _childItems.end()) {
106 childIter = _childItems.erase(childIter);
109 emit endRemoveChilds();
114 void AbstractTreeItem::removeChildLater(AbstractTreeItem *child) {
116 QCoreApplication::postEvent(this, new RemoveChildLaterEvent(child));
119 void AbstractTreeItem::customEvent(QEvent *event) {
120 if(event->type() != QEvent::User)
125 RemoveChildLaterEvent *removeEvent = static_cast<RemoveChildLaterEvent *>(event);
126 int childRow = _childItems.indexOf(removeEvent->child());
130 removeChild(childRow);
133 bool AbstractTreeItem::reParent(AbstractTreeItem *newParent) {
134 // currently we support only re parenting if the child that's about to be
135 // adopted does not have any children itself.
136 if(childCount() != 0) {
137 qDebug() << "AbstractTreeItem::reParent(): cannot reparent" << this << "with children.";
145 emit parent()->beginRemoveChilds(oldRow, oldRow);
146 parent()->_childItems.removeAt(oldRow);
147 emit parent()->endRemoveChilds();
149 AbstractTreeItem *oldParent = parent();
150 setParent(newParent);
152 bool success = newParent->newChild(this);
154 qWarning() << "AbstractTreeItem::reParent(): failed to attach to new parent after removing from old parent! this:" << this << "new parent:" << newParent;
157 oldParent->checkForDeletion();
162 AbstractTreeItem *AbstractTreeItem::child(int row) const {
163 if(childCount() <= row)
166 return _childItems[row];
169 int AbstractTreeItem::childCount(int column) const {
173 return _childItems.count();
176 int AbstractTreeItem::row() const {
178 qWarning() << "AbstractTreeItem::row():" << this << "has no parent AbstractTreeItem as it's parent! parent is" << QObject::parent();
182 int row_ = parent()->_childItems.indexOf(const_cast<AbstractTreeItem *>(this));
184 qWarning() << "AbstractTreeItem::row():" << this << "is not in the child list of" << QObject::parent();
188 void AbstractTreeItem::dumpChildList() {
189 qDebug() << "==== Childlist for Item:" << this << "====";
190 if(childCount() > 0) {
191 AbstractTreeItem *child;
192 QList<AbstractTreeItem *>::const_iterator childIter = _childItems.constBegin();
193 while(childIter != _childItems.constEnd()) {
195 qDebug() << "Row:" << child->row() << child << child->data(0, Qt::DisplayRole);
199 qDebug() << "==== End Of Childlist ====";
202 /*****************************************
204 *****************************************/
205 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
206 : AbstractTreeItem(parent),
211 SimpleTreeItem::~SimpleTreeItem() {
214 QVariant SimpleTreeItem::data(int column, int role) const {
215 if(column >= columnCount() || role != Qt::DisplayRole)
218 return _itemData[column];
221 bool SimpleTreeItem::setData(int column, const QVariant &value, int role) {
222 if(column > columnCount() || role != Qt::DisplayRole)
225 if(column == columnCount())
226 _itemData.append(value);
228 _itemData[column] = value;
230 emit dataChanged(column);
234 int SimpleTreeItem::columnCount() const {
235 return _itemData.count();
238 /*****************************************
240 *****************************************/
241 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
242 : AbstractTreeItem(parent),
243 _propertyOrder(propertyOrder)
247 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
248 : AbstractTreeItem(parent),
249 _propertyOrder(QStringList())
254 PropertyMapItem::~PropertyMapItem() {
257 QVariant PropertyMapItem::data(int column, int role) const {
258 if(column >= columnCount())
262 case Qt::ToolTipRole:
263 return toolTip(column);
264 case Qt::DisplayRole:
265 case TreeModel::SortRole: // fallthrough, since SortRole should default to DisplayRole
266 return property(_propertyOrder[column].toAscii());
273 bool PropertyMapItem::setData(int column, const QVariant &value, int role) {
274 if(column >= columnCount() || role != Qt::DisplayRole)
277 emit dataChanged(column);
278 return setProperty(_propertyOrder[column].toAscii(), value);
281 int PropertyMapItem::columnCount() const {
282 return _propertyOrder.count();
285 void PropertyMapItem::appendProperty(const QString &property) {
286 _propertyOrder << property;
291 /*****************************************
293 *****************************************/
294 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
295 : QAbstractItemModel(parent),
296 _childStatus(QModelIndex(), 0, 0, 0),
297 _aboutToRemoveOrInsert(false)
299 rootItem = new SimpleTreeItem(data, 0);
300 connectItem(rootItem);
302 if(Quassel::isOptionSet("debugmodel")) {
303 connect(this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
304 this, SLOT(debug_rowsAboutToBeInserted(const QModelIndex &, int, int)));
305 connect(this, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
306 this, SLOT(debug_rowsAboutToBeRemoved(const QModelIndex &, int, int)));
307 connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
308 this, SLOT(debug_rowsInserted(const QModelIndex &, int, int)));
309 connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
310 this, SLOT(debug_rowsRemoved(const QModelIndex &, int, int)));
311 connect(this, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
312 this, SLOT(debug_dataChanged(const QModelIndex &, const QModelIndex &)));
316 TreeModel::~TreeModel() {
320 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
321 if(!hasIndex(row, column, parent))
322 return QModelIndex();
324 AbstractTreeItem *parentItem;
326 if(!parent.isValid())
327 parentItem = rootItem;
329 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
331 AbstractTreeItem *childItem = parentItem->child(row);
334 return createIndex(row, column, childItem);
336 return QModelIndex();
339 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
341 qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
342 return QModelIndex();
346 return QModelIndex();
348 return createIndex(item->row(), 0, item);
351 QModelIndex TreeModel::parent(const QModelIndex &index) const {
352 if(!index.isValid()) {
353 qWarning() << "TreeModel::parent(): has been asked for the rootItems Parent!";
354 return QModelIndex();
357 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
358 AbstractTreeItem *parentItem = childItem->parent();
360 Q_ASSERT(parentItem);
361 if(parentItem == rootItem)
362 return QModelIndex();
364 return createIndex(parentItem->row(), 0, parentItem);
367 int TreeModel::rowCount(const QModelIndex &parent) const {
368 AbstractTreeItem *parentItem;
369 if(!parent.isValid())
370 parentItem = rootItem;
372 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
374 return parentItem->childCount(parent.column());
377 int TreeModel::columnCount(const QModelIndex &parent) const {
379 return rootItem->columnCount();
380 // since there the Qt Views don't draw more columns than the header has columns
381 // we can be lazy and simply return the count of header columns
382 // actually this gives us more freedom cause we don't have to ensure that a rows parent
383 // has equal or more columns than that row
385 // AbstractTreeItem *parentItem;
386 // if(!parent.isValid())
387 // parentItem = rootItem;
389 // parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
390 // return parentItem->columnCount();
393 QVariant TreeModel::data(const QModelIndex &index, int role) const {
397 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
398 return item->data(index.column(), role);
401 bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
405 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
406 return item->setData(index.column(), value, role);
409 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
410 if(!index.isValid()) {
411 return rootItem->flags() & Qt::ItemIsDropEnabled;
413 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
414 return item->flags();
418 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
419 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
420 return rootItem->data(section, role);
425 void TreeModel::itemDataChanged(int column) {
426 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
427 QModelIndex leftIndex, rightIndex;
433 leftIndex = createIndex(item->row(), 0, item);
434 rightIndex = createIndex(item->row(), item->columnCount() - 1, item);
436 leftIndex = createIndex(item->row(), column, item);
437 rightIndex = leftIndex;
440 emit dataChanged(leftIndex, rightIndex);
443 void TreeModel::connectItem(AbstractTreeItem *item) {
444 connect(item, SIGNAL(dataChanged(int)),
445 this, SLOT(itemDataChanged(int)));
447 connect(item, SIGNAL(beginAppendChilds(int, int)),
448 this, SLOT(beginAppendChilds(int, int)));
449 connect(item, SIGNAL(endAppendChilds()),
450 this, SLOT(endAppendChilds()));
452 connect(item, SIGNAL(beginRemoveChilds(int, int)),
453 this, SLOT(beginRemoveChilds(int, int)));
454 connect(item, SIGNAL(endRemoveChilds()),
455 this, SLOT(endRemoveChilds()));
458 void TreeModel::beginAppendChilds(int firstRow, int lastRow) {
459 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
461 qWarning() << "TreeModel::beginAppendChilds(): cannot append Childs to unknown parent";
465 QModelIndex parent = indexByItem(parentItem);
466 Q_ASSERT(!_aboutToRemoveOrInsert);
468 _aboutToRemoveOrInsert = true;
469 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
470 beginInsertRows(parent, firstRow, lastRow);
473 void TreeModel::endAppendChilds() {
474 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
476 qWarning() << "TreeModel::endAppendChilds(): cannot append Childs to unknown parent";
479 Q_ASSERT(_aboutToRemoveOrInsert);
480 ChildStatus cs = _childStatus;
481 QModelIndex parent = indexByItem(parentItem);
482 Q_ASSERT(cs.parent == parent);
483 Q_ASSERT(rowCount(parent) == cs.childCount + cs.end - cs.start + 1);
485 _aboutToRemoveOrInsert = false;
486 for(int i = cs.start; i <= cs.end; i++) {
487 connectItem(parentItem->child(i));
492 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
493 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
495 qWarning() << "TreeModel::beginRemoveChilds(): cannot append Childs to unknown parent";
499 for(int i = firstRow; i <= lastRow; i++) {
500 disconnect(parentItem->child(i), 0, this, 0);
504 QModelIndex parent = indexByItem(parentItem);
505 Q_ASSERT(firstRow <= lastRow);
506 Q_ASSERT(parentItem->childCount() > lastRow);
507 Q_ASSERT(!_aboutToRemoveOrInsert);
508 _aboutToRemoveOrInsert = true;
509 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
511 beginRemoveRows(parent, firstRow, lastRow);
514 void TreeModel::endRemoveChilds() {
515 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
517 qWarning() << "TreeModel::endRemoveChilds(): cannot remove Childs from unknown parent";
521 // concistency checks
522 Q_ASSERT(_aboutToRemoveOrInsert);
523 ChildStatus cs = _childStatus;
524 QModelIndex parent = indexByItem(parentItem);
525 Q_ASSERT(cs.parent == parent);
526 Q_ASSERT(rowCount(parent) == cs.childCount - cs.end + cs.start - 1);
527 _aboutToRemoveOrInsert = false;
532 void TreeModel::clear() {
533 rootItem->removeAllChilds();
536 void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
537 qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
540 void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
541 AbstractTreeItem *parentItem;
542 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
544 parentItem = rootItem;
545 qDebug() << "debug_rowsAboutToBeRemoved" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
548 AbstractTreeItem *childItem;
549 for(int i = end; i >= start; i--) {
550 child = parent.child(i, 0);
551 childItem = parentItem->child(i);
553 qDebug() << ">>>" << i << child << child.data().toString();
557 void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end) {
558 AbstractTreeItem *parentItem;
559 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
561 parentItem = rootItem;
562 qDebug() << "debug_rowsInserted:" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
565 AbstractTreeItem *childItem;
566 for(int i = start; i <= end; i++) {
567 child = parent.child(i, 0);
568 childItem = parentItem->child(i);
570 qDebug() << "<<<" << i << child << child.data().toString();
574 void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
575 qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
578 void TreeModel::debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
579 qDebug() << "debug_dataChanged" << topLeft << bottomRight;
580 QStringList displayData;
581 for(int row = topLeft.row(); row <= bottomRight.row(); row++) {
582 displayData = QStringList();
583 for(int column = topLeft.column(); column <= bottomRight.column(); column++) {
584 displayData << data(topLeft.sibling(row, column), Qt::DisplayRole).toString();
586 qDebug() << " row:" << row << displayData;