1 /***************************************************************************
2 * Copyright (C) 2005-09 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 // since we are called asynchronously we have to recheck if the item in question still has no childs
131 if(removeEvent->child()->childCount())
134 removeChild(childRow);
137 bool AbstractTreeItem::reParent(AbstractTreeItem *newParent) {
138 // currently we support only re parenting if the child that's about to be
139 // adopted does not have any children itself.
140 if(childCount() != 0) {
141 qDebug() << "AbstractTreeItem::reParent(): cannot reparent" << this << "with children.";
149 emit parent()->beginRemoveChilds(oldRow, oldRow);
150 parent()->_childItems.removeAt(oldRow);
151 emit parent()->endRemoveChilds();
153 AbstractTreeItem *oldParent = parent();
154 setParent(newParent);
156 bool success = newParent->newChild(this);
158 qWarning() << "AbstractTreeItem::reParent(): failed to attach to new parent after removing from old parent! this:" << this << "new parent:" << newParent;
161 oldParent->checkForDeletion();
166 AbstractTreeItem *AbstractTreeItem::child(int row) const {
167 if(childCount() <= row)
170 return _childItems[row];
173 int AbstractTreeItem::childCount(int column) const {
177 return _childItems.count();
180 int AbstractTreeItem::row() const {
182 qWarning() << "AbstractTreeItem::row():" << this << "has no parent AbstractTreeItem as it's parent! parent is" << QObject::parent();
186 int row_ = parent()->_childItems.indexOf(const_cast<AbstractTreeItem *>(this));
188 qWarning() << "AbstractTreeItem::row():" << this << "is not in the child list of" << QObject::parent();
192 void AbstractTreeItem::dumpChildList() {
193 qDebug() << "==== Childlist for Item:" << this << "====";
194 if(childCount() > 0) {
195 AbstractTreeItem *child;
196 QList<AbstractTreeItem *>::const_iterator childIter = _childItems.constBegin();
197 while(childIter != _childItems.constEnd()) {
199 qDebug() << "Row:" << child->row() << child << child->data(0, Qt::DisplayRole);
203 qDebug() << "==== End Of Childlist ====";
206 /*****************************************
208 *****************************************/
209 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
210 : AbstractTreeItem(parent),
215 SimpleTreeItem::~SimpleTreeItem() {
218 QVariant SimpleTreeItem::data(int column, int role) const {
219 if(column >= columnCount() || role != Qt::DisplayRole)
222 return _itemData[column];
225 bool SimpleTreeItem::setData(int column, const QVariant &value, int role) {
226 if(column > columnCount() || role != Qt::DisplayRole)
229 if(column == columnCount())
230 _itemData.append(value);
232 _itemData[column] = value;
234 emit dataChanged(column);
238 int SimpleTreeItem::columnCount() const {
239 return _itemData.count();
242 /*****************************************
244 *****************************************/
245 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
246 : AbstractTreeItem(parent),
247 _propertyOrder(propertyOrder)
251 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
252 : AbstractTreeItem(parent),
253 _propertyOrder(QStringList())
258 PropertyMapItem::~PropertyMapItem() {
261 QVariant PropertyMapItem::data(int column, int role) const {
262 if(column >= columnCount())
266 case Qt::ToolTipRole:
267 return toolTip(column);
268 case Qt::DisplayRole:
269 case TreeModel::SortRole: // fallthrough, since SortRole should default to DisplayRole
270 return property(_propertyOrder[column].toAscii());
277 bool PropertyMapItem::setData(int column, const QVariant &value, int role) {
278 if(column >= columnCount() || role != Qt::DisplayRole)
281 emit dataChanged(column);
282 return setProperty(_propertyOrder[column].toAscii(), value);
285 int PropertyMapItem::columnCount() const {
286 return _propertyOrder.count();
289 void PropertyMapItem::appendProperty(const QString &property) {
290 _propertyOrder << property;
295 /*****************************************
297 *****************************************/
298 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
299 : QAbstractItemModel(parent),
300 _childStatus(QModelIndex(), 0, 0, 0),
301 _aboutToRemoveOrInsert(false)
303 rootItem = new SimpleTreeItem(data, 0);
304 connectItem(rootItem);
306 if(Quassel::isOptionSet("debugmodel")) {
307 connect(this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
308 this, SLOT(debug_rowsAboutToBeInserted(const QModelIndex &, int, int)));
309 connect(this, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
310 this, SLOT(debug_rowsAboutToBeRemoved(const QModelIndex &, int, int)));
311 connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
312 this, SLOT(debug_rowsInserted(const QModelIndex &, int, int)));
313 connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
314 this, SLOT(debug_rowsRemoved(const QModelIndex &, int, int)));
315 connect(this, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
316 this, SLOT(debug_dataChanged(const QModelIndex &, const QModelIndex &)));
320 TreeModel::~TreeModel() {
324 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
325 if(row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent))
326 return QModelIndex();
328 AbstractTreeItem *parentItem;
330 if(!parent.isValid())
331 parentItem = rootItem;
333 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
335 AbstractTreeItem *childItem = parentItem->child(row);
338 return createIndex(row, column, childItem);
340 return QModelIndex();
343 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
345 qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
346 return QModelIndex();
350 return QModelIndex();
352 return createIndex(item->row(), 0, item);
355 QModelIndex TreeModel::parent(const QModelIndex &index) const {
356 if(!index.isValid()) {
357 // ModelTest does this
358 // qWarning() << "TreeModel::parent(): has been asked for the rootItems Parent!";
359 return QModelIndex();
362 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
363 AbstractTreeItem *parentItem = childItem->parent();
365 Q_ASSERT(parentItem);
366 if(parentItem == rootItem)
367 return QModelIndex();
369 return createIndex(parentItem->row(), 0, parentItem);
372 int TreeModel::rowCount(const QModelIndex &parent) const {
373 AbstractTreeItem *parentItem;
374 if(!parent.isValid())
375 parentItem = rootItem;
377 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
379 return parentItem->childCount(parent.column());
382 int TreeModel::columnCount(const QModelIndex &parent) const {
384 return rootItem->columnCount();
385 // since there the Qt Views don't draw more columns than the header has columns
386 // we can be lazy and simply return the count of header columns
387 // actually this gives us more freedom cause we don't have to ensure that a rows parent
388 // has equal or more columns than that row
390 // AbstractTreeItem *parentItem;
391 // if(!parent.isValid())
392 // parentItem = rootItem;
394 // parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
395 // return parentItem->columnCount();
398 QVariant TreeModel::data(const QModelIndex &index, int role) const {
402 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
403 return item->data(index.column(), role);
406 bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
410 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
411 return item->setData(index.column(), value, role);
414 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
415 if(!index.isValid()) {
416 return rootItem->flags() & Qt::ItemIsDropEnabled;
418 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
419 return item->flags();
423 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
424 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
425 return rootItem->data(section, role);
430 void TreeModel::itemDataChanged(int column) {
431 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
432 QModelIndex leftIndex, rightIndex;
438 leftIndex = createIndex(item->row(), 0, item);
439 rightIndex = createIndex(item->row(), item->columnCount() - 1, item);
441 leftIndex = createIndex(item->row(), column, item);
442 rightIndex = leftIndex;
445 emit dataChanged(leftIndex, rightIndex);
448 void TreeModel::connectItem(AbstractTreeItem *item) {
449 connect(item, SIGNAL(dataChanged(int)),
450 this, SLOT(itemDataChanged(int)));
452 connect(item, SIGNAL(beginAppendChilds(int, int)),
453 this, SLOT(beginAppendChilds(int, int)));
454 connect(item, SIGNAL(endAppendChilds()),
455 this, SLOT(endAppendChilds()));
457 connect(item, SIGNAL(beginRemoveChilds(int, int)),
458 this, SLOT(beginRemoveChilds(int, int)));
459 connect(item, SIGNAL(endRemoveChilds()),
460 this, SLOT(endRemoveChilds()));
463 void TreeModel::beginAppendChilds(int firstRow, int lastRow) {
464 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
466 qWarning() << "TreeModel::beginAppendChilds(): cannot append Children to unknown parent";
470 QModelIndex parent = indexByItem(parentItem);
471 Q_ASSERT(!_aboutToRemoveOrInsert);
473 _aboutToRemoveOrInsert = true;
474 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
475 beginInsertRows(parent, firstRow, lastRow);
478 void TreeModel::endAppendChilds() {
479 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
481 qWarning() << "TreeModel::endAppendChilds(): cannot append Children to unknown parent";
484 Q_ASSERT(_aboutToRemoveOrInsert);
485 ChildStatus cs = _childStatus;
486 QModelIndex parent = indexByItem(parentItem);
487 Q_ASSERT(cs.parent == parent);
488 Q_ASSERT(rowCount(parent) == cs.childCount + cs.end - cs.start + 1);
490 _aboutToRemoveOrInsert = false;
491 for(int i = cs.start; i <= cs.end; i++) {
492 connectItem(parentItem->child(i));
497 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
498 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
500 qWarning() << "TreeModel::beginRemoveChilds(): cannot append Children to unknown parent";
504 for(int i = firstRow; i <= lastRow; i++) {
505 disconnect(parentItem->child(i), 0, this, 0);
509 QModelIndex parent = indexByItem(parentItem);
510 Q_ASSERT(firstRow <= lastRow);
511 Q_ASSERT(parentItem->childCount() > lastRow);
512 Q_ASSERT(!_aboutToRemoveOrInsert);
513 _aboutToRemoveOrInsert = true;
514 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
516 beginRemoveRows(parent, firstRow, lastRow);
519 void TreeModel::endRemoveChilds() {
520 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
522 qWarning() << "TreeModel::endRemoveChilds(): cannot remove Children from unknown parent";
526 // concistency checks
527 Q_ASSERT(_aboutToRemoveOrInsert);
528 ChildStatus cs = _childStatus;
529 QModelIndex parent = indexByItem(parentItem);
530 Q_ASSERT(cs.parent == parent);
531 Q_ASSERT(rowCount(parent) == cs.childCount - cs.end + cs.start - 1);
532 _aboutToRemoveOrInsert = false;
537 void TreeModel::clear() {
538 rootItem->removeAllChilds();
541 void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
542 qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
545 void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
546 AbstractTreeItem *parentItem;
547 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
549 parentItem = rootItem;
550 qDebug() << "debug_rowsAboutToBeRemoved" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
553 AbstractTreeItem *childItem;
554 for(int i = end; i >= start; i--) {
555 child = parent.child(i, 0);
556 childItem = parentItem->child(i);
558 qDebug() << ">>>" << i << child << child.data().toString();
562 void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end) {
563 AbstractTreeItem *parentItem;
564 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
566 parentItem = rootItem;
567 qDebug() << "debug_rowsInserted:" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
570 AbstractTreeItem *childItem;
571 for(int i = start; i <= end; i++) {
572 child = parent.child(i, 0);
573 childItem = parentItem->child(i);
575 qDebug() << "<<<" << i << child << child.data().toString();
579 void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
580 qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
583 void TreeModel::debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
584 qDebug() << "debug_dataChanged" << topLeft << bottomRight;
585 QStringList displayData;
586 for(int row = topLeft.row(); row <= bottomRight.row(); row++) {
587 displayData = QStringList();
588 for(int column = topLeft.column(); column <= bottomRight.column(); column++) {
589 displayData << data(topLeft.sibling(row, column), Qt::DisplayRole).toString();
591 qDebug() << " row:" << row << displayData;