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"
24 #include <QCoreApplication>
26 /*****************************************
27 * Abstract Items of a TreeModel
28 *****************************************/
29 AbstractTreeItem::AbstractTreeItem(AbstractTreeItem *parent)
31 _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
35 AbstractTreeItem::~AbstractTreeItem() {
38 quint64 AbstractTreeItem::id() const {
42 bool AbstractTreeItem::newChild(AbstractTreeItem *item) {
43 // check if a child with that ID is already known
44 Q_ASSERT(childById(item->id()) == 0);
46 int newRow = childCount();
47 emit beginAppendChilds(newRow, newRow);
48 _childItems.append(item);
49 emit endAppendChilds();
54 bool AbstractTreeItem::newChilds(const QList<AbstractTreeItem *> &items) {
58 QList<AbstractTreeItem *>::const_iterator itemIter = items.constBegin();
59 AbstractTreeItem *item;
60 while(itemIter != items.constEnd()) {
62 if(childById(item->id()) != 0) {
63 qWarning() << "AbstractTreeItem::newChilds(): received child that is already attached" << item << item->id();
69 int nextRow = childCount();
70 int lastRow = nextRow + items.count() - 1;
72 emit beginAppendChilds(nextRow, lastRow);
73 itemIter = items.constBegin();
74 while(itemIter != items.constEnd()) {
75 _childItems.append(*itemIter);
78 emit endAppendChilds();
83 bool AbstractTreeItem::removeChild(int row) {
84 if(childCount() <= row)
87 child(row)->removeAllChilds();
88 emit beginRemoveChilds(row, row);
89 AbstractTreeItem *treeitem = _childItems.takeAt(row);
90 treeitem->deleteLater();
91 emit endRemoveChilds();
96 bool AbstractTreeItem::removeChildById(const quint64 &id) {
97 const int numChilds = childCount();
99 for(int i = 0; i < numChilds; i++) {
100 if(_childItems[i]->id() == id)
101 return removeChild(i);
107 void AbstractTreeItem::removeAllChilds() {
108 const int numChilds = childCount();
113 AbstractTreeItem *child;
115 QList<AbstractTreeItem *>::iterator childIter;
117 childIter = _childItems.begin();
118 while(childIter != _childItems.end()) {
120 child->removeAllChilds();
124 emit beginRemoveChilds(0, numChilds - 1);
125 childIter = _childItems.begin();
126 while(childIter != _childItems.end()) {
128 childIter = _childItems.erase(childIter);
129 child->deleteLater();
131 emit endRemoveChilds();
134 bool AbstractTreeItem::reParent(AbstractTreeItem *newParent) {
135 // currently we support only re parenting if the child that's about to be
136 // adopted does not have any children itself.
137 if(childCount() != 0) {
138 qDebug() << "AbstractTreeItem::reParent(): cannot reparent" << this << "with children.";
146 emit parent()->beginRemoveChilds(oldRow, oldRow);
147 parent()->_childItems.removeAt(oldRow);
148 emit parent()->endRemoveChilds();
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;
159 AbstractTreeItem *AbstractTreeItem::child(int row) const {
160 if(childCount() <= row)
163 return _childItems[row];
166 AbstractTreeItem *AbstractTreeItem::childById(const quint64 &id) const {
167 const int numChilds = childCount();
168 for(int i = 0; i < numChilds; i++) {
169 if(_childItems[i]->id() == id)
170 return _childItems[i];
175 int AbstractTreeItem::childCount(int column) const {
179 return _childItems.count();
182 int AbstractTreeItem::row() const {
184 qWarning() << "AbstractTreeItem::row():" << this << "has no parent AbstractTreeItem as it's parent! parent is" << QObject::parent();
188 int row_ = parent()->_childItems.indexOf(const_cast<AbstractTreeItem *>(this));
190 qWarning() << "AbstractTreeItem::row():" << this << "is not in the child list of" << QObject::parent();
194 AbstractTreeItem *AbstractTreeItem::parent() const {
195 return qobject_cast<AbstractTreeItem *>(QObject::parent());
198 Qt::ItemFlags AbstractTreeItem::flags() const {
202 void AbstractTreeItem::setFlags(Qt::ItemFlags flags) {
206 void AbstractTreeItem::dumpChildList() {
207 qDebug() << "==== Childlist for Item:" << this << id() << "====";
208 if(childCount() > 0) {
209 AbstractTreeItem *child;
210 QList<AbstractTreeItem *>::const_iterator childIter = _childItems.constBegin();
211 while(childIter != _childItems.constEnd()) {
213 qDebug() << "Row:" << child->row() << child << child->id() << child->data(0, Qt::DisplayRole);
217 qDebug() << "==== End Of Childlist ====";
220 /*****************************************
222 *****************************************/
223 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
224 : AbstractTreeItem(parent),
229 SimpleTreeItem::~SimpleTreeItem() {
232 QVariant SimpleTreeItem::data(int column, int role) const {
233 if(column >= columnCount() || role != Qt::DisplayRole)
236 return _itemData[column];
239 bool SimpleTreeItem::setData(int column, const QVariant &value, int role) {
240 if(column > columnCount() || role != Qt::DisplayRole)
243 if(column == columnCount())
244 _itemData.append(value);
246 _itemData[column] = value;
248 emit dataChanged(column);
252 int SimpleTreeItem::columnCount() const {
253 return _itemData.count();
256 /*****************************************
258 *****************************************/
259 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
260 : AbstractTreeItem(parent),
261 _propertyOrder(propertyOrder)
265 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
266 : AbstractTreeItem(parent),
267 _propertyOrder(QStringList())
272 PropertyMapItem::~PropertyMapItem() {
275 QVariant PropertyMapItem::data(int column, int role) const {
276 if(column >= columnCount())
280 case Qt::ToolTipRole:
281 return toolTip(column);
282 case Qt::DisplayRole:
283 case TreeModel::SortRole: // fallthrough, since SortRole should default to DisplayRole
284 return property(_propertyOrder[column].toAscii());
291 bool PropertyMapItem::setData(int column, const QVariant &value, int role) {
292 if(column >= columnCount() || role != Qt::DisplayRole)
295 emit dataChanged(column);
296 return setProperty(_propertyOrder[column].toAscii(), value);
299 int PropertyMapItem::columnCount() const {
300 return _propertyOrder.count();
303 void PropertyMapItem::appendProperty(const QString &property) {
304 _propertyOrder << property;
309 /*****************************************
311 *****************************************/
312 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
313 : QAbstractItemModel(parent),
314 _childStatus(QModelIndex(), 0, 0, 0),
315 _aboutToRemoveOrInsert(false)
317 rootItem = new SimpleTreeItem(data, 0);
318 connectItem(rootItem);
320 if(QCoreApplication::instance()->arguments().contains("--debugmodel")) {
321 connect(this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
322 this, SLOT(debug_rowsAboutToBeInserted(const QModelIndex &, int, int)));
323 connect(this, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
324 this, SLOT(debug_rowsAboutToBeRemoved(const QModelIndex &, int, int)));
325 connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
326 this, SLOT(debug_rowsInserted(const QModelIndex &, int, int)));
327 connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
328 this, SLOT(debug_rowsRemoved(const QModelIndex &, int, int)));
329 connect(this, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
330 this, SLOT(debug_dataChanged(const QModelIndex &, const QModelIndex &)));
334 TreeModel::~TreeModel() {
338 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
339 if(!hasIndex(row, column, parent))
340 return QModelIndex();
342 AbstractTreeItem *parentItem;
344 if(!parent.isValid())
345 parentItem = rootItem;
347 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
349 AbstractTreeItem *childItem = parentItem->child(row);
352 return createIndex(row, column, childItem);
354 return QModelIndex();
357 QModelIndex TreeModel::indexById(quint64 id, const QModelIndex &parent) const {
358 AbstractTreeItem *parentItem;
360 if(!parent.isValid())
361 parentItem = rootItem;
363 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
365 AbstractTreeItem *childItem = parentItem->childById(id);
368 return createIndex(childItem->row(), 0, childItem);
370 return QModelIndex();
373 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
375 qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
376 return QModelIndex();
380 return QModelIndex();
382 return createIndex(item->row(), 0, item);
385 QModelIndex TreeModel::parent(const QModelIndex &index) const {
386 if(!index.isValid()) {
387 qWarning() << "TreeModel::parent(): has been asked for the rootItems Parent!";
388 return QModelIndex();
391 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
392 AbstractTreeItem *parentItem = childItem->parent();
394 Q_ASSERT(parentItem);
395 if(parentItem == rootItem)
396 return QModelIndex();
398 return createIndex(parentItem->row(), 0, parentItem);
401 int TreeModel::rowCount(const QModelIndex &parent) const {
402 AbstractTreeItem *parentItem;
403 if(!parent.isValid())
404 parentItem = rootItem;
406 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
408 return parentItem->childCount(parent.column());
411 int TreeModel::columnCount(const QModelIndex &parent) const {
413 return rootItem->columnCount();
414 // since there the Qt Views don't draw more columns than the header has columns
415 // we can be lazy and simply return the count of header columns
416 // actually this gives us more freedom cause we don't have to ensure that a rows parent
417 // has equal or more columns than that row
419 // AbstractTreeItem *parentItem;
420 // if(!parent.isValid())
421 // parentItem = rootItem;
423 // parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
424 // return parentItem->columnCount();
427 QVariant TreeModel::data(const QModelIndex &index, int role) const {
431 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
432 return item->data(index.column(), role);
435 bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
439 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
440 return item->setData(index.column(), value, role);
443 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
444 if(!index.isValid()) {
445 return rootItem->flags() & Qt::ItemIsDropEnabled;
447 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
448 return item->flags();
452 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
453 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
454 return rootItem->data(section, role);
459 void TreeModel::itemDataChanged(int column) {
460 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
461 QModelIndex leftIndex, rightIndex;
467 leftIndex = createIndex(item->row(), 0, item);
468 rightIndex = createIndex(item->row(), item->columnCount() - 1, item);
470 leftIndex = createIndex(item->row(), column, item);
471 rightIndex = leftIndex;
474 emit dataChanged(leftIndex, rightIndex);
477 void TreeModel::connectItem(AbstractTreeItem *item) {
478 connect(item, SIGNAL(dataChanged(int)),
479 this, SLOT(itemDataChanged(int)));
481 connect(item, SIGNAL(beginAppendChilds(int, int)),
482 this, SLOT(beginAppendChilds(int, int)));
483 connect(item, SIGNAL(endAppendChilds()),
484 this, SLOT(endAppendChilds()));
486 connect(item, SIGNAL(beginRemoveChilds(int, int)),
487 this, SLOT(beginRemoveChilds(int, int)));
488 connect(item, SIGNAL(endRemoveChilds()),
489 this, SLOT(endRemoveChilds()));
492 void TreeModel::beginAppendChilds(int firstRow, int lastRow) {
493 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
495 qWarning() << "TreeModel::beginAppendChilds(): cannot append Childs to unknown parent";
499 QModelIndex parent = indexByItem(parentItem);
500 Q_ASSERT(!_aboutToRemoveOrInsert);
502 _aboutToRemoveOrInsert = true;
503 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
504 beginInsertRows(parent, firstRow, lastRow);
507 void TreeModel::endAppendChilds() {
508 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
510 qWarning() << "TreeModel::endAppendChilds(): cannot append Childs to unknown parent";
513 Q_ASSERT(_aboutToRemoveOrInsert);
514 ChildStatus cs = _childStatus;
515 QModelIndex parent = indexByItem(parentItem);
516 Q_ASSERT(cs.parent == parent);
517 Q_ASSERT(rowCount(parent) == cs.childCount + cs.end - cs.start + 1);
519 _aboutToRemoveOrInsert = false;
520 for(int i = cs.start; i <= cs.end; i++) {
521 connectItem(parentItem->child(i));
526 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
527 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
529 qWarning() << "TreeModel::beginRemoveChilds(): cannot append Childs to unknown parent";
533 for(int i = firstRow; i <= lastRow; i++) {
534 disconnect(parentItem->child(i), 0, this, 0);
538 QModelIndex parent = indexByItem(parentItem);
539 Q_ASSERT(firstRow <= lastRow);
540 Q_ASSERT(parentItem->childCount() > lastRow);
541 Q_ASSERT(!_aboutToRemoveOrInsert);
542 _aboutToRemoveOrInsert = true;
543 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
545 beginRemoveRows(parent, firstRow, lastRow);
548 void TreeModel::endRemoveChilds() {
549 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
551 qWarning() << "TreeModel::endRemoveChilds(): cannot remove Childs from unknown parent";
555 // concistency checks
556 Q_ASSERT(_aboutToRemoveOrInsert);
557 ChildStatus cs = _childStatus;
558 QModelIndex parent = indexByItem(parentItem);
559 Q_ASSERT(cs.parent == parent);
560 Q_ASSERT(rowCount(parent) == cs.childCount - cs.end + cs.start - 1);
561 _aboutToRemoveOrInsert = false;
566 void TreeModel::clear() {
567 rootItem->removeAllChilds();
570 void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
571 qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
574 void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
575 AbstractTreeItem *parentItem;
576 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
578 parentItem = rootItem;
579 qDebug() << "debug_rowsAboutToBeRemoved" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
582 AbstractTreeItem *childItem;
583 for(int i = end; i >= start; i--) {
584 child = parent.child(i, 0);
585 childItem = parentItem->child(i);
587 qDebug() << ">>>" << i << child << childItem->id() << child.data().toString();
591 void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end) {
592 AbstractTreeItem *parentItem;
593 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
595 parentItem = rootItem;
596 qDebug() << "debug_rowsInserted:" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
599 AbstractTreeItem *childItem;
600 for(int i = start; i <= end; i++) {
601 child = parent.child(i, 0);
602 childItem = parentItem->child(i);
604 qDebug() << "<<<" << i << child << childItem->id() << child.data().toString();
608 void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
609 qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
612 void TreeModel::debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
613 qDebug() << "debug_dataChanged" << topLeft << bottomRight;
614 QStringList displayData;
615 for(int row = topLeft.row(); row <= bottomRight.row(); row++) {
616 displayData = QStringList();
617 for(int column = topLeft.column(); column <= bottomRight.column(); column++) {
618 displayData << data(topLeft.sibling(row, column), Qt::DisplayRole).toString();
620 qDebug() << " row:" << row << displayData;