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() const {
151 return _childItems.count();
154 int AbstractTreeItem::row() const {
158 return parent()->_childItems.indexOf(const_cast<AbstractTreeItem *>(this));
161 AbstractTreeItem *AbstractTreeItem::parent() const {
162 return qobject_cast<AbstractTreeItem *>(QObject::parent());
165 Qt::ItemFlags AbstractTreeItem::flags() const {
169 void AbstractTreeItem::setFlags(Qt::ItemFlags flags) {
173 void AbstractTreeItem::dumpChildList() {
174 qDebug() << "==== Childlist for Item:" << this << id() << "====";
175 if(childCount() > 0) {
176 AbstractTreeItem *child;
177 QList<AbstractTreeItem *>::const_iterator childIter = _childItems.constBegin();
178 while(childIter != _childItems.constEnd()) {
180 qDebug() << "Row:" << child->row() << child << child->id() << child->data(0, Qt::DisplayRole);
184 qDebug() << "==== End Of Childlist ====";
187 /*****************************************
189 *****************************************/
190 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
191 : AbstractTreeItem(parent),
196 SimpleTreeItem::~SimpleTreeItem() {
199 QVariant SimpleTreeItem::data(int column, int role) const {
200 if(column >= columnCount() || role != Qt::DisplayRole)
203 return _itemData[column];
206 bool SimpleTreeItem::setData(int column, const QVariant &value, int role) {
207 if(column > columnCount() || role != Qt::DisplayRole)
210 if(column == columnCount())
211 _itemData.append(value);
213 _itemData[column] = value;
215 emit dataChanged(column);
219 int SimpleTreeItem::columnCount() const {
220 return _itemData.count();
223 /*****************************************
225 *****************************************/
226 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
227 : AbstractTreeItem(parent),
228 _propertyOrder(propertyOrder)
232 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
233 : AbstractTreeItem(parent),
234 _propertyOrder(QStringList())
239 PropertyMapItem::~PropertyMapItem() {
242 QVariant PropertyMapItem::data(int column, int role) const {
243 if(column >= columnCount())
247 case Qt::ToolTipRole:
248 return toolTip(column);
249 case Qt::DisplayRole:
250 case TreeModel::SortRole: // fallthrough, since SortRole should default to DisplayRole
251 return property(_propertyOrder[column].toAscii());
258 bool PropertyMapItem::setData(int column, const QVariant &value, int role) {
259 if(column >= columnCount() || role != Qt::DisplayRole)
262 emit dataChanged(column);
263 return setProperty(_propertyOrder[column].toAscii(), value);
266 int PropertyMapItem::columnCount() const {
267 return _propertyOrder.count();
270 void PropertyMapItem::appendProperty(const QString &property) {
271 _propertyOrder << property;
276 /*****************************************
278 *****************************************/
279 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
280 : QAbstractItemModel(parent),
281 _childStatus(QModelIndex(), 0, 0, 0),
282 _aboutToRemoveOrInsert(false)
284 rootItem = new SimpleTreeItem(data, 0);
285 connectItem(rootItem);
287 if(QCoreApplication::instance()->arguments().contains("--debugmodel")) {
288 connect(this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
289 this, SLOT(debug_rowsAboutToBeInserted(const QModelIndex &, int, int)));
290 connect(this, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
291 this, SLOT(debug_rowsAboutToBeRemoved(const QModelIndex &, int, int)));
292 connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
293 this, SLOT(debug_rowsInserted(const QModelIndex &, int, int)));
294 connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
295 this, SLOT(debug_rowsRemoved(const QModelIndex &, int, int)));
299 TreeModel::~TreeModel() {
303 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
304 if(!hasIndex(row, column, parent))
305 return QModelIndex();
307 AbstractTreeItem *parentItem;
309 if(!parent.isValid())
310 parentItem = rootItem;
312 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
314 AbstractTreeItem *childItem = parentItem->child(row);
317 return createIndex(row, column, childItem);
319 return QModelIndex();
322 QModelIndex TreeModel::indexById(quint64 id, const QModelIndex &parent) const {
323 AbstractTreeItem *parentItem;
325 if(!parent.isValid())
326 parentItem = rootItem;
328 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
330 AbstractTreeItem *childItem = parentItem->childById(id);
333 return createIndex(childItem->row(), 0, 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();
376 int TreeModel::columnCount(const QModelIndex &parent) const {
378 // since there the Qt Views don't draw more columns than the header has columns
379 // we can be lazy and simply return the count of header columns
380 // actually this gives us more freedom cause we don't have to ensure that a rows parent
381 // has equal or more columns than that row
383 // if(parent.isValid()) {
384 // AbstractTreeItem *child;
385 // if(child = static_cast<AbstractTreeItem *>(parent.internalPointer())->child(parent.column(), parent.row()))
386 // return child->columnCount();
388 // return static_cast<AbstractTreeItem*>(parent.internalPointer())->columnCount();
390 // return rootItem->columnCount();
393 return rootItem->columnCount();
396 QVariant TreeModel::data(const QModelIndex &index, int role) const {
400 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
401 return item->data(index.column(), role);
404 bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
408 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
409 return item->setData(index.column(), value, role);
412 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
413 AbstractTreeItem *item;
417 item = static_cast<AbstractTreeItem *>(index.internalPointer());
418 return item->flags();
421 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
422 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
423 return rootItem->data(section, role);
428 void TreeModel::itemDataChanged(int column) {
429 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
430 QModelIndex leftIndex, rightIndex;
436 leftIndex = createIndex(item->row(), 0, item);
437 rightIndex = createIndex(item->row(), item->columnCount() - 1, item);
439 leftIndex = createIndex(item->row(), column, item);
440 rightIndex = leftIndex;
443 emit dataChanged(leftIndex, rightIndex);
446 void TreeModel::connectItem(AbstractTreeItem *item) {
447 connect(item, SIGNAL(dataChanged(int)),
448 this, SLOT(itemDataChanged(int)));
450 connect(item, SIGNAL(beginAppendChilds(int, int)),
451 this, SLOT(beginAppendChilds(int, int)));
452 connect(item, SIGNAL(endAppendChilds()),
453 this, SLOT(endAppendChilds()));
455 connect(item, SIGNAL(beginRemoveChilds(int, int)),
456 this, SLOT(beginRemoveChilds(int, int)));
457 connect(item, SIGNAL(endRemoveChilds()),
458 this, SLOT(endRemoveChilds()));
461 void TreeModel::beginAppendChilds(int firstRow, int lastRow) {
462 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
464 qWarning() << "TreeModel::beginAppendChilds(): cannot append Childs to unknown parent";
468 QModelIndex parent = indexByItem(parentItem);
469 Q_ASSERT(!_aboutToRemoveOrInsert);
471 _aboutToRemoveOrInsert = true;
472 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
473 beginInsertRows(parent, firstRow, lastRow);
476 void TreeModel::endAppendChilds() {
477 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
479 qWarning() << "TreeModel::endAppendChilds(): cannot append Childs to unknown parent";
482 Q_ASSERT(_aboutToRemoveOrInsert);
483 ChildStatus cs = _childStatus;
484 QModelIndex parent = indexByItem(parentItem);
485 Q_ASSERT(cs.parent == parent);
486 Q_ASSERT(rowCount(parent) == cs.childCount + cs.end - cs.start + 1);
488 _aboutToRemoveOrInsert = false;
489 for(int i = cs.start; i <= cs.end; i++) {
490 connectItem(parentItem->child(i));
495 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
496 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
498 qWarning() << "TreeModel::beginRemoveChilds(): cannot append Childs to unknown parent";
501 QModelIndex parent = indexByItem(parentItem);
502 Q_ASSERT(firstRow <= lastRow);
503 Q_ASSERT(parentItem->childCount() > lastRow);
504 Q_ASSERT(!_aboutToRemoveOrInsert);
506 _aboutToRemoveOrInsert = true;
507 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
508 beginRemoveRows(parent, firstRow, lastRow);
511 void TreeModel::endRemoveChilds() {
512 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
514 qWarning() << "TreeModel::endRemoveChilds(): cannot append Childs to unknown parent";
517 Q_ASSERT(_aboutToRemoveOrInsert);
518 ChildStatus cs = _childStatus;
519 QModelIndex parent = indexByItem(parentItem);
520 Q_ASSERT(cs.parent == parent);
521 Q_ASSERT(rowCount(parent) == cs.childCount - cs.end + cs.start - 1);
523 _aboutToRemoveOrInsert = false;
527 void TreeModel::clear() {
528 rootItem->removeAllChilds();
531 void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
532 qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
535 void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
536 AbstractTreeItem *parentItem;
537 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
539 parentItem = rootItem;
540 qDebug() << "#" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
543 AbstractTreeItem *childItem;
544 for(int i = end; i >= start; i--) {
545 child = parent.child(i, 0);
546 childItem = parentItem->child(i);
548 qDebug() << ">>>" << i << child << childItem->id() << child.data().toString();
552 void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end) {
553 AbstractTreeItem *parentItem;
554 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
556 parentItem = rootItem;
557 qDebug() << "#" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
560 AbstractTreeItem *childItem;
561 for(int i = start; i <= end; i++) {
562 child = parent.child(i, 0);
563 childItem = parentItem->child(i);
565 qDebug() << "<<<" << i << child << childItem->id() << child.data().toString();
569 void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
570 qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;