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"
25 #include <QCoreApplication>
27 class RemoveChildLaterEvent : public QEvent {
29 RemoveChildLaterEvent(AbstractTreeItem *child) : QEvent(QEvent::User), _child(child) {};
30 inline AbstractTreeItem *child() { return _child; }
32 AbstractTreeItem *_child;
36 /*****************************************
37 * Abstract Items of a TreeModel
38 *****************************************/
39 AbstractTreeItem::AbstractTreeItem(AbstractTreeItem *parent)
41 _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled),
46 bool AbstractTreeItem::newChild(AbstractTreeItem *item) {
47 int newRow = childCount();
48 emit beginAppendChilds(newRow, newRow);
49 _childItems.append(item);
50 emit endAppendChilds();
54 bool AbstractTreeItem::newChilds(const QList<AbstractTreeItem *> &items) {
58 int nextRow = childCount();
59 int lastRow = nextRow + items.count() - 1;
61 emit beginAppendChilds(nextRow, lastRow);
63 emit endAppendChilds();
68 bool AbstractTreeItem::removeChild(int row) {
69 if(row < 0 || childCount() <= row)
72 child(row)->removeAllChilds();
73 emit beginRemoveChilds(row, row);
74 AbstractTreeItem *treeitem = _childItems.takeAt(row);
76 emit endRemoveChilds();
83 void AbstractTreeItem::removeAllChilds() {
84 const int numChilds = childCount();
89 AbstractTreeItem *child;
91 QList<AbstractTreeItem *>::iterator childIter;
93 childIter = _childItems.begin();
94 while(childIter != _childItems.end()) {
96 child->setTreeItemFlags(0); // disable self deletion, as this would only fuck up consitency and the child gets deleted anyways
97 child->removeAllChilds();
101 emit beginRemoveChilds(0, numChilds - 1);
102 childIter = _childItems.begin();
103 while(childIter != _childItems.end()) {
105 childIter = _childItems.erase(childIter);
108 emit endRemoveChilds();
113 void AbstractTreeItem::removeChildLater(AbstractTreeItem *child) {
115 QCoreApplication::postEvent(this, new RemoveChildLaterEvent(child));
118 void AbstractTreeItem::customEvent(QEvent *event) {
119 if(event->type() != QEvent::User)
124 RemoveChildLaterEvent *removeEvent = static_cast<RemoveChildLaterEvent *>(event);
125 int childRow = _childItems.indexOf(removeEvent->child());
129 removeChild(childRow);
132 bool AbstractTreeItem::reParent(AbstractTreeItem *newParent) {
133 // currently we support only re parenting if the child that's about to be
134 // adopted does not have any children itself.
135 if(childCount() != 0) {
136 qDebug() << "AbstractTreeItem::reParent(): cannot reparent" << this << "with children.";
144 emit parent()->beginRemoveChilds(oldRow, oldRow);
145 parent()->_childItems.removeAt(oldRow);
146 emit parent()->endRemoveChilds();
148 AbstractTreeItem *oldParent = parent();
149 setParent(newParent);
151 bool success = newParent->newChild(this);
153 qWarning() << "AbstractTreeItem::reParent(): failed to attach to new parent after removing from old parent! this:" << this << "new parent:" << newParent;
156 oldParent->checkForDeletion();
161 AbstractTreeItem *AbstractTreeItem::child(int row) const {
162 if(childCount() <= row)
165 return _childItems[row];
168 int AbstractTreeItem::childCount(int column) const {
172 return _childItems.count();
175 int AbstractTreeItem::row() const {
177 qWarning() << "AbstractTreeItem::row():" << this << "has no parent AbstractTreeItem as it's parent! parent is" << QObject::parent();
181 int row_ = parent()->_childItems.indexOf(const_cast<AbstractTreeItem *>(this));
183 qWarning() << "AbstractTreeItem::row():" << this << "is not in the child list of" << QObject::parent();
187 void AbstractTreeItem::dumpChildList() {
188 qDebug() << "==== Childlist for Item:" << this << "====";
189 if(childCount() > 0) {
190 AbstractTreeItem *child;
191 QList<AbstractTreeItem *>::const_iterator childIter = _childItems.constBegin();
192 while(childIter != _childItems.constEnd()) {
194 qDebug() << "Row:" << child->row() << child << child->data(0, Qt::DisplayRole);
198 qDebug() << "==== End Of Childlist ====";
201 /*****************************************
203 *****************************************/
204 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
205 : AbstractTreeItem(parent),
210 SimpleTreeItem::~SimpleTreeItem() {
213 QVariant SimpleTreeItem::data(int column, int role) const {
214 if(column >= columnCount() || role != Qt::DisplayRole)
217 return _itemData[column];
220 bool SimpleTreeItem::setData(int column, const QVariant &value, int role) {
221 if(column > columnCount() || role != Qt::DisplayRole)
224 if(column == columnCount())
225 _itemData.append(value);
227 _itemData[column] = value;
229 emit dataChanged(column);
233 int SimpleTreeItem::columnCount() const {
234 return _itemData.count();
237 /*****************************************
239 *****************************************/
240 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
241 : AbstractTreeItem(parent),
242 _propertyOrder(propertyOrder)
246 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
247 : AbstractTreeItem(parent),
248 _propertyOrder(QStringList())
253 PropertyMapItem::~PropertyMapItem() {
256 QVariant PropertyMapItem::data(int column, int role) const {
257 if(column >= columnCount())
261 case Qt::ToolTipRole:
262 return toolTip(column);
263 case Qt::DisplayRole:
264 case TreeModel::SortRole: // fallthrough, since SortRole should default to DisplayRole
265 return property(_propertyOrder[column].toAscii());
272 bool PropertyMapItem::setData(int column, const QVariant &value, int role) {
273 if(column >= columnCount() || role != Qt::DisplayRole)
276 emit dataChanged(column);
277 return setProperty(_propertyOrder[column].toAscii(), value);
280 int PropertyMapItem::columnCount() const {
281 return _propertyOrder.count();
284 void PropertyMapItem::appendProperty(const QString &property) {
285 _propertyOrder << property;
290 /*****************************************
292 *****************************************/
293 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
294 : QAbstractItemModel(parent),
295 _childStatus(QModelIndex(), 0, 0, 0),
296 _aboutToRemoveOrInsert(false)
298 rootItem = new SimpleTreeItem(data, 0);
299 connectItem(rootItem);
301 if(Global::parser.isSet("debugmodel")) {
302 connect(this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
303 this, SLOT(debug_rowsAboutToBeInserted(const QModelIndex &, int, int)));
304 connect(this, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
305 this, SLOT(debug_rowsAboutToBeRemoved(const QModelIndex &, int, int)));
306 connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
307 this, SLOT(debug_rowsInserted(const QModelIndex &, int, int)));
308 connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
309 this, SLOT(debug_rowsRemoved(const QModelIndex &, int, int)));
310 connect(this, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
311 this, SLOT(debug_dataChanged(const QModelIndex &, const QModelIndex &)));
315 TreeModel::~TreeModel() {
319 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
320 if(!hasIndex(row, column, parent))
321 return QModelIndex();
323 AbstractTreeItem *parentItem;
325 if(!parent.isValid())
326 parentItem = rootItem;
328 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
330 AbstractTreeItem *childItem = parentItem->child(row);
333 return createIndex(row, column, childItem);
335 return QModelIndex();
338 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
340 qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
341 return QModelIndex();
345 return QModelIndex();
347 return createIndex(item->row(), 0, item);
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();
356 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
357 AbstractTreeItem *parentItem = childItem->parent();
359 Q_ASSERT(parentItem);
360 if(parentItem == rootItem)
361 return QModelIndex();
363 return createIndex(parentItem->row(), 0, parentItem);
366 int TreeModel::rowCount(const QModelIndex &parent) const {
367 AbstractTreeItem *parentItem;
368 if(!parent.isValid())
369 parentItem = rootItem;
371 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
373 return parentItem->childCount(parent.column());
376 int TreeModel::columnCount(const QModelIndex &parent) const {
378 return rootItem->columnCount();
379 // since there the Qt Views don't draw more columns than the header has columns
380 // we can be lazy and simply return the count of header columns
381 // actually this gives us more freedom cause we don't have to ensure that a rows parent
382 // has equal or more columns than that row
384 // AbstractTreeItem *parentItem;
385 // if(!parent.isValid())
386 // parentItem = rootItem;
388 // parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
389 // return parentItem->columnCount();
392 QVariant TreeModel::data(const QModelIndex &index, int role) const {
396 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
397 return item->data(index.column(), role);
400 bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
404 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
405 return item->setData(index.column(), value, role);
408 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
409 if(!index.isValid()) {
410 return rootItem->flags() & Qt::ItemIsDropEnabled;
412 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
413 return item->flags();
417 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
418 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
419 return rootItem->data(section, role);
424 void TreeModel::itemDataChanged(int column) {
425 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
426 QModelIndex leftIndex, rightIndex;
432 leftIndex = createIndex(item->row(), 0, item);
433 rightIndex = createIndex(item->row(), item->columnCount() - 1, item);
435 leftIndex = createIndex(item->row(), column, item);
436 rightIndex = leftIndex;
439 emit dataChanged(leftIndex, rightIndex);
442 void TreeModel::connectItem(AbstractTreeItem *item) {
443 connect(item, SIGNAL(dataChanged(int)),
444 this, SLOT(itemDataChanged(int)));
446 connect(item, SIGNAL(beginAppendChilds(int, int)),
447 this, SLOT(beginAppendChilds(int, int)));
448 connect(item, SIGNAL(endAppendChilds()),
449 this, SLOT(endAppendChilds()));
451 connect(item, SIGNAL(beginRemoveChilds(int, int)),
452 this, SLOT(beginRemoveChilds(int, int)));
453 connect(item, SIGNAL(endRemoveChilds()),
454 this, SLOT(endRemoveChilds()));
457 void TreeModel::beginAppendChilds(int firstRow, int lastRow) {
458 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
460 qWarning() << "TreeModel::beginAppendChilds(): cannot append Childs to unknown parent";
464 QModelIndex parent = indexByItem(parentItem);
465 Q_ASSERT(!_aboutToRemoveOrInsert);
467 _aboutToRemoveOrInsert = true;
468 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
469 beginInsertRows(parent, firstRow, lastRow);
472 void TreeModel::endAppendChilds() {
473 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
475 qWarning() << "TreeModel::endAppendChilds(): cannot append Childs to unknown parent";
478 Q_ASSERT(_aboutToRemoveOrInsert);
479 ChildStatus cs = _childStatus;
480 QModelIndex parent = indexByItem(parentItem);
481 Q_ASSERT(cs.parent == parent);
482 Q_ASSERT(rowCount(parent) == cs.childCount + cs.end - cs.start + 1);
484 _aboutToRemoveOrInsert = false;
485 for(int i = cs.start; i <= cs.end; i++) {
486 connectItem(parentItem->child(i));
491 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
492 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
494 qWarning() << "TreeModel::beginRemoveChilds(): cannot append Childs to unknown parent";
498 for(int i = firstRow; i <= lastRow; i++) {
499 disconnect(parentItem->child(i), 0, this, 0);
503 QModelIndex parent = indexByItem(parentItem);
504 Q_ASSERT(firstRow <= lastRow);
505 Q_ASSERT(parentItem->childCount() > lastRow);
506 Q_ASSERT(!_aboutToRemoveOrInsert);
507 _aboutToRemoveOrInsert = true;
508 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
510 beginRemoveRows(parent, firstRow, lastRow);
513 void TreeModel::endRemoveChilds() {
514 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
516 qWarning() << "TreeModel::endRemoveChilds(): cannot remove Childs from unknown parent";
520 // concistency checks
521 Q_ASSERT(_aboutToRemoveOrInsert);
522 ChildStatus cs = _childStatus;
523 QModelIndex parent = indexByItem(parentItem);
524 Q_ASSERT(cs.parent == parent);
525 Q_ASSERT(rowCount(parent) == cs.childCount - cs.end + cs.start - 1);
526 _aboutToRemoveOrInsert = false;
531 void TreeModel::clear() {
532 rootItem->removeAllChilds();
535 void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
536 qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
539 void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
540 AbstractTreeItem *parentItem;
541 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
543 parentItem = rootItem;
544 qDebug() << "debug_rowsAboutToBeRemoved" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
547 AbstractTreeItem *childItem;
548 for(int i = end; i >= start; i--) {
549 child = parent.child(i, 0);
550 childItem = parentItem->child(i);
552 qDebug() << ">>>" << i << child << child.data().toString();
556 void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end) {
557 AbstractTreeItem *parentItem;
558 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
560 parentItem = rootItem;
561 qDebug() << "debug_rowsInserted:" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
564 AbstractTreeItem *childItem;
565 for(int i = start; i <= end; i++) {
566 child = parent.child(i, 0);
567 childItem = parentItem->child(i);
569 qDebug() << "<<<" << i << child << child.data().toString();
573 void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
574 qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
577 void TreeModel::debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
578 qDebug() << "debug_dataChanged" << topLeft << bottomRight;
579 QStringList displayData;
580 for(int row = topLeft.row(); row <= bottomRight.row(); row++) {
581 displayData = QStringList();
582 for(int column = topLeft.column(); column <= bottomRight.column(); column++) {
583 displayData << data(topLeft.sibling(row, column), Qt::DisplayRole).toString();
585 qDebug() << " row:" << row << displayData;