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 return item->data(index.column(), role);
405 bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
409 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
410 return item->setData(index.column(), value, role);
413 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
414 if(!index.isValid()) {
415 return rootItem->flags() & Qt::ItemIsDropEnabled;
417 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
418 return item->flags();
422 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
423 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
424 return rootItem->data(section, role);
429 void TreeModel::itemDataChanged(int column) {
430 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
431 QModelIndex leftIndex, rightIndex;
437 leftIndex = createIndex(item->row(), 0, item);
438 rightIndex = createIndex(item->row(), item->columnCount() - 1, item);
440 leftIndex = createIndex(item->row(), column, item);
441 rightIndex = leftIndex;
444 emit dataChanged(leftIndex, rightIndex);
447 void TreeModel::connectItem(AbstractTreeItem *item) {
448 connect(item, SIGNAL(dataChanged(int)),
449 this, SLOT(itemDataChanged(int)));
451 connect(item, SIGNAL(beginAppendChilds(int, int)),
452 this, SLOT(beginAppendChilds(int, int)));
453 connect(item, SIGNAL(endAppendChilds()),
454 this, SLOT(endAppendChilds()));
456 connect(item, SIGNAL(beginRemoveChilds(int, int)),
457 this, SLOT(beginRemoveChilds(int, int)));
458 connect(item, SIGNAL(endRemoveChilds()),
459 this, SLOT(endRemoveChilds()));
462 void TreeModel::beginAppendChilds(int firstRow, int lastRow) {
463 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
465 qWarning() << "TreeModel::beginAppendChilds(): cannot append Childs to unknown parent";
469 QModelIndex parent = indexByItem(parentItem);
470 Q_ASSERT(!_aboutToRemoveOrInsert);
472 _aboutToRemoveOrInsert = true;
473 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
474 beginInsertRows(parent, firstRow, lastRow);
477 void TreeModel::endAppendChilds() {
478 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
480 qWarning() << "TreeModel::endAppendChilds(): cannot append Childs to unknown parent";
483 Q_ASSERT(_aboutToRemoveOrInsert);
484 ChildStatus cs = _childStatus;
485 QModelIndex parent = indexByItem(parentItem);
486 Q_ASSERT(cs.parent == parent);
487 Q_ASSERT(rowCount(parent) == cs.childCount + cs.end - cs.start + 1);
489 _aboutToRemoveOrInsert = false;
490 for(int i = cs.start; i <= cs.end; i++) {
491 connectItem(parentItem->child(i));
496 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
497 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
499 qWarning() << "TreeModel::beginRemoveChilds(): cannot append Childs to unknown parent";
503 for(int i = firstRow; i <= lastRow; i++) {
504 disconnect(parentItem->child(i), 0, this, 0);
508 QModelIndex parent = indexByItem(parentItem);
509 Q_ASSERT(firstRow <= lastRow);
510 Q_ASSERT(parentItem->childCount() > lastRow);
511 Q_ASSERT(!_aboutToRemoveOrInsert);
512 _aboutToRemoveOrInsert = true;
513 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
515 beginRemoveRows(parent, firstRow, lastRow);
518 void TreeModel::endRemoveChilds() {
519 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
521 qWarning() << "TreeModel::endRemoveChilds(): cannot remove Childs from unknown parent";
525 // concistency checks
526 Q_ASSERT(_aboutToRemoveOrInsert);
527 ChildStatus cs = _childStatus;
528 QModelIndex parent = indexByItem(parentItem);
529 Q_ASSERT(cs.parent == parent);
530 Q_ASSERT(rowCount(parent) == cs.childCount - cs.end + cs.start - 1);
531 _aboutToRemoveOrInsert = false;
536 void TreeModel::clear() {
537 rootItem->removeAllChilds();
540 void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
541 qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
544 void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
545 AbstractTreeItem *parentItem;
546 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
548 parentItem = rootItem;
549 qDebug() << "debug_rowsAboutToBeRemoved" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
552 AbstractTreeItem *childItem;
553 for(int i = end; i >= start; i--) {
554 child = parent.child(i, 0);
555 childItem = parentItem->child(i);
557 qDebug() << ">>>" << i << child << childItem->id() << child.data().toString();
561 void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end) {
562 AbstractTreeItem *parentItem;
563 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
565 parentItem = rootItem;
566 qDebug() << "debug_rowsInserted:" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
569 AbstractTreeItem *childItem;
570 for(int i = start; i <= end; i++) {
571 child = parent.child(i, 0);
572 childItem = parentItem->child(i);
574 qDebug() << "<<<" << i << child << childItem->id() << child.data().toString();
578 void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
579 qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
582 void TreeModel::debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
583 qDebug() << "debug_dataChanged" << topLeft << bottomRight;
584 QStringList displayData;
585 for(int row = topLeft.row(); row <= bottomRight.row(); row++) {
586 displayData = QStringList();
587 for(int column = topLeft.column(); column <= bottomRight.column(); column++) {
588 displayData << data(topLeft.sibling(row, column), Qt::DisplayRole).toString();
590 qDebug() << " row:" << row << displayData;