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 AbstractTreeItem *AbstractTreeItem::child(int row) const {
135 if(childCount() <= row)
138 return _childItems[row];
141 AbstractTreeItem *AbstractTreeItem::childById(const quint64 &id) const {
142 const int numChilds = childCount();
143 for(int i = 0; i < numChilds; i++) {
144 if(_childItems[i]->id() == id)
145 return _childItems[i];
150 int AbstractTreeItem::childCount(int column) const {
154 return _childItems.count();
157 int AbstractTreeItem::row() const {
161 return parent()->_childItems.indexOf(const_cast<AbstractTreeItem *>(this));
164 AbstractTreeItem *AbstractTreeItem::parent() const {
165 return qobject_cast<AbstractTreeItem *>(QObject::parent());
168 Qt::ItemFlags AbstractTreeItem::flags() const {
172 void AbstractTreeItem::setFlags(Qt::ItemFlags flags) {
176 void AbstractTreeItem::dumpChildList() {
177 qDebug() << "==== Childlist for Item:" << this << id() << "====";
178 if(childCount() > 0) {
179 AbstractTreeItem *child;
180 QList<AbstractTreeItem *>::const_iterator childIter = _childItems.constBegin();
181 while(childIter != _childItems.constEnd()) {
183 qDebug() << "Row:" << child->row() << child << child->id() << child->data(0, Qt::DisplayRole);
187 qDebug() << "==== End Of Childlist ====";
190 /*****************************************
192 *****************************************/
193 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
194 : AbstractTreeItem(parent),
199 SimpleTreeItem::~SimpleTreeItem() {
202 QVariant SimpleTreeItem::data(int column, int role) const {
203 if(column >= columnCount() || role != Qt::DisplayRole)
206 return _itemData[column];
209 bool SimpleTreeItem::setData(int column, const QVariant &value, int role) {
210 if(column > columnCount() || role != Qt::DisplayRole)
213 if(column == columnCount())
214 _itemData.append(value);
216 _itemData[column] = value;
218 emit dataChanged(column);
222 int SimpleTreeItem::columnCount() const {
223 return _itemData.count();
226 /*****************************************
228 *****************************************/
229 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
230 : AbstractTreeItem(parent),
231 _propertyOrder(propertyOrder)
235 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
236 : AbstractTreeItem(parent),
237 _propertyOrder(QStringList())
242 PropertyMapItem::~PropertyMapItem() {
245 QVariant PropertyMapItem::data(int column, int role) const {
246 if(column >= columnCount())
250 case Qt::ToolTipRole:
251 return toolTip(column);
252 case Qt::DisplayRole:
253 case TreeModel::SortRole: // fallthrough, since SortRole should default to DisplayRole
254 return property(_propertyOrder[column].toAscii());
261 bool PropertyMapItem::setData(int column, const QVariant &value, int role) {
262 if(column >= columnCount() || role != Qt::DisplayRole)
265 emit dataChanged(column);
266 return setProperty(_propertyOrder[column].toAscii(), value);
269 int PropertyMapItem::columnCount() const {
270 return _propertyOrder.count();
273 void PropertyMapItem::appendProperty(const QString &property) {
274 _propertyOrder << property;
279 /*****************************************
281 *****************************************/
282 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
283 : QAbstractItemModel(parent),
284 _childStatus(QModelIndex(), 0, 0, 0),
285 _aboutToRemoveOrInsert(false)
287 rootItem = new SimpleTreeItem(data, 0);
288 connectItem(rootItem);
290 if(QCoreApplication::instance()->arguments().contains("--debugmodel")) {
291 connect(this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
292 this, SLOT(debug_rowsAboutToBeInserted(const QModelIndex &, int, int)));
293 connect(this, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
294 this, SLOT(debug_rowsAboutToBeRemoved(const QModelIndex &, int, int)));
295 connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
296 this, SLOT(debug_rowsInserted(const QModelIndex &, int, int)));
297 connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
298 this, SLOT(debug_rowsRemoved(const QModelIndex &, int, int)));
299 connect(this, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
300 this, SLOT(debug_dataChanged(const QModelIndex &, const QModelIndex &)));
304 TreeModel::~TreeModel() {
308 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
309 if(!hasIndex(row, column, parent))
310 return QModelIndex();
312 AbstractTreeItem *parentItem;
314 if(!parent.isValid())
315 parentItem = rootItem;
317 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
319 AbstractTreeItem *childItem = parentItem->child(row);
322 return createIndex(row, column, childItem);
324 return QModelIndex();
327 QModelIndex TreeModel::indexById(quint64 id, const QModelIndex &parent) const {
328 AbstractTreeItem *parentItem;
330 if(!parent.isValid())
331 parentItem = rootItem;
333 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
335 AbstractTreeItem *childItem = parentItem->childById(id);
338 return createIndex(childItem->row(), 0, 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 qWarning() << "TreeModel::parent(): has been asked for the rootItems Parent!";
358 return QModelIndex();
361 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
362 AbstractTreeItem *parentItem = childItem->parent();
364 Q_ASSERT(parentItem);
365 if(parentItem == rootItem)
366 return QModelIndex();
368 return createIndex(parentItem->row(), 0, parentItem);
371 int TreeModel::rowCount(const QModelIndex &parent) const {
372 AbstractTreeItem *parentItem;
373 if(!parent.isValid())
374 parentItem = rootItem;
376 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
378 return parentItem->childCount(parent.column());
381 int TreeModel::columnCount(const QModelIndex &parent) const {
383 return rootItem->columnCount();
384 // since there the Qt Views don't draw more columns than the header has columns
385 // we can be lazy and simply return the count of header columns
386 // actually this gives us more freedom cause we don't have to ensure that a rows parent
387 // has equal or more columns than that row
389 // AbstractTreeItem *parentItem;
390 // if(!parent.isValid())
391 // parentItem = rootItem;
393 // parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
394 // return parentItem->columnCount();
397 QVariant TreeModel::data(const QModelIndex &index, int role) const {
401 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
402 if(role == Qt::DisplayRole && !item->data(index.column(), role).isValid()) {
403 qDebug() << item->data(0, role) << item->columnCount();
405 return item->data(index.column(), role);
408 bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
412 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
413 return item->setData(index.column(), value, role);
416 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
417 if(!index.isValid()) {
418 return rootItem->flags() & Qt::ItemIsDropEnabled;
420 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
421 return item->flags();
425 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
426 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
427 return rootItem->data(section, role);
432 void TreeModel::itemDataChanged(int column) {
433 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
434 QModelIndex leftIndex, rightIndex;
440 leftIndex = createIndex(item->row(), 0, item);
441 rightIndex = createIndex(item->row(), item->columnCount() - 1, item);
443 leftIndex = createIndex(item->row(), column, item);
444 rightIndex = leftIndex;
447 emit dataChanged(leftIndex, rightIndex);
450 void TreeModel::connectItem(AbstractTreeItem *item) {
451 connect(item, SIGNAL(dataChanged(int)),
452 this, SLOT(itemDataChanged(int)));
454 connect(item, SIGNAL(beginAppendChilds(int, int)),
455 this, SLOT(beginAppendChilds(int, int)));
456 connect(item, SIGNAL(endAppendChilds()),
457 this, SLOT(endAppendChilds()));
459 connect(item, SIGNAL(beginRemoveChilds(int, int)),
460 this, SLOT(beginRemoveChilds(int, int)));
461 connect(item, SIGNAL(endRemoveChilds()),
462 this, SLOT(endRemoveChilds()));
465 void TreeModel::beginAppendChilds(int firstRow, int lastRow) {
466 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
468 qWarning() << "TreeModel::beginAppendChilds(): cannot append Childs to unknown parent";
472 QModelIndex parent = indexByItem(parentItem);
473 Q_ASSERT(!_aboutToRemoveOrInsert);
475 _aboutToRemoveOrInsert = true;
476 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
477 beginInsertRows(parent, firstRow, lastRow);
480 void TreeModel::endAppendChilds() {
481 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
483 qWarning() << "TreeModel::endAppendChilds(): cannot append Childs to unknown parent";
486 Q_ASSERT(_aboutToRemoveOrInsert);
487 ChildStatus cs = _childStatus;
488 QModelIndex parent = indexByItem(parentItem);
489 Q_ASSERT(cs.parent == parent);
490 Q_ASSERT(rowCount(parent) == cs.childCount + cs.end - cs.start + 1);
492 _aboutToRemoveOrInsert = false;
493 for(int i = cs.start; i <= cs.end; i++) {
494 connectItem(parentItem->child(i));
499 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
500 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
502 qWarning() << "TreeModel::beginRemoveChilds(): cannot append Childs to unknown parent";
506 for(int i = firstRow; i <= lastRow; i++) {
507 disconnect(parentItem->child(i), 0, this, 0);
511 QModelIndex parent = indexByItem(parentItem);
512 Q_ASSERT(firstRow <= lastRow);
513 Q_ASSERT(parentItem->childCount() > lastRow);
514 Q_ASSERT(!_aboutToRemoveOrInsert);
515 _aboutToRemoveOrInsert = true;
516 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
518 beginRemoveRows(parent, firstRow, lastRow);
521 void TreeModel::endRemoveChilds() {
522 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
524 qWarning() << "TreeModel::endRemoveChilds(): cannot remove Childs from unknown parent";
528 // concistency checks
529 Q_ASSERT(_aboutToRemoveOrInsert);
530 ChildStatus cs = _childStatus;
531 QModelIndex parent = indexByItem(parentItem);
532 Q_ASSERT(cs.parent == parent);
533 Q_ASSERT(rowCount(parent) == cs.childCount - cs.end + cs.start - 1);
534 _aboutToRemoveOrInsert = false;
539 void TreeModel::clear() {
540 rootItem->removeAllChilds();
543 void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
544 qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
547 void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
548 AbstractTreeItem *parentItem;
549 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
551 parentItem = rootItem;
552 qDebug() << "debug_rowsAboutToBeRemoved" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
555 AbstractTreeItem *childItem;
556 for(int i = end; i >= start; i--) {
557 child = parent.child(i, 0);
558 childItem = parentItem->child(i);
560 qDebug() << ">>>" << i << child << childItem->id() << child.data().toString();
564 void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end) {
565 AbstractTreeItem *parentItem;
566 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
568 parentItem = rootItem;
569 qDebug() << "debug_rowsInserted:" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
572 AbstractTreeItem *childItem;
573 for(int i = start; i <= end; i++) {
574 child = parent.child(i, 0);
575 childItem = parentItem->child(i);
577 qDebug() << "<<<" << i << child << childItem->id() << child.data().toString();
581 void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
582 qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
585 void TreeModel::debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
586 qDebug() << "debug_dataChanged" << topLeft << bottomRight;
587 QStringList displayData;
588 for(int row = topLeft.row(); row <= bottomRight.row(); row++) {
589 displayData = QStringList();
590 for(int column = topLeft.column(); column <= bottomRight.column(); column++) {
591 displayData << data(topLeft.sibling(row, column), Qt::DisplayRole).toString();
593 qDebug() << " row:" << row << displayData;