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 return property(_propertyOrder[column].toAscii());
257 bool PropertyMapItem::setData(int column, const QVariant &value, int role) {
258 if(column >= columnCount() || role != Qt::DisplayRole)
261 emit dataChanged(column);
262 return setProperty(_propertyOrder[column].toAscii(), value);
265 int PropertyMapItem::columnCount() const {
266 return _propertyOrder.count();
269 void PropertyMapItem::appendProperty(const QString &property) {
270 _propertyOrder << property;
275 /*****************************************
277 *****************************************/
278 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
279 : QAbstractItemModel(parent),
280 _childStatus(QModelIndex(), 0, 0, 0),
281 _aboutToRemoveOrInsert(false)
283 rootItem = new SimpleTreeItem(data, 0);
284 connectItem(rootItem);
286 if(QCoreApplication::instance()->arguments().contains("--debugmodel")) {
287 connect(this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
288 this, SLOT(debug_rowsAboutToBeInserted(const QModelIndex &, int, int)));
289 connect(this, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
290 this, SLOT(debug_rowsAboutToBeRemoved(const QModelIndex &, int, int)));
291 connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
292 this, SLOT(debug_rowsInserted(const QModelIndex &, int, int)));
293 connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
294 this, SLOT(debug_rowsRemoved(const QModelIndex &, int, int)));
298 TreeModel::~TreeModel() {
302 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
303 if(!hasIndex(row, column, parent))
304 return QModelIndex();
306 AbstractTreeItem *parentItem;
308 if(!parent.isValid())
309 parentItem = rootItem;
311 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
313 AbstractTreeItem *childItem = parentItem->child(row);
316 return createIndex(row, column, childItem);
318 return QModelIndex();
321 QModelIndex TreeModel::indexById(quint64 id, const QModelIndex &parent) const {
322 AbstractTreeItem *parentItem;
324 if(!parent.isValid())
325 parentItem = rootItem;
327 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
329 AbstractTreeItem *childItem = parentItem->childById(id);
332 return createIndex(childItem->row(), 0, childItem);
334 return QModelIndex();
337 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
339 qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
340 return QModelIndex();
344 return QModelIndex();
346 return createIndex(item->row(), 0, item);
349 QModelIndex TreeModel::parent(const QModelIndex &index) const {
350 if(!index.isValid()) {
351 qWarning() << "TreeModel::parent(): has been asked for the rootItems Parent!";
352 return QModelIndex();
355 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
356 AbstractTreeItem *parentItem = childItem->parent();
358 Q_ASSERT(parentItem);
359 if(parentItem == rootItem)
360 return QModelIndex();
362 return createIndex(parentItem->row(), 0, parentItem);
365 int TreeModel::rowCount(const QModelIndex &parent) const {
366 AbstractTreeItem *parentItem;
367 if(!parent.isValid())
368 parentItem = rootItem;
370 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
372 return parentItem->childCount();
375 int TreeModel::columnCount(const QModelIndex &parent) const {
377 // since there the Qt Views don't draw more columns than the header has columns
378 // we can be lazy and simply return the count of header columns
379 // actually this gives us more freedom cause we don't have to ensure that a rows parent
380 // has equal or more columns than that row
382 // if(parent.isValid()) {
383 // AbstractTreeItem *child;
384 // if(child = static_cast<AbstractTreeItem *>(parent.internalPointer())->child(parent.column(), parent.row()))
385 // return child->columnCount();
387 // return static_cast<AbstractTreeItem*>(parent.internalPointer())->columnCount();
389 // return rootItem->columnCount();
392 return rootItem->columnCount();
395 QVariant TreeModel::data(const QModelIndex &index, int role) const {
399 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
400 return item->data(index.column(), role);
403 bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
407 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
408 return item->setData(index.column(), value, role);
411 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
412 AbstractTreeItem *item;
416 item = static_cast<AbstractTreeItem *>(index.internalPointer());
417 return item->flags();
420 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
421 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
422 return rootItem->data(section, role);
427 void TreeModel::itemDataChanged(int column) {
428 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
429 QModelIndex leftIndex, rightIndex;
435 leftIndex = createIndex(item->row(), 0, item);
436 rightIndex = createIndex(item->row(), item->columnCount() - 1, item);
438 leftIndex = createIndex(item->row(), column, item);
439 rightIndex = leftIndex;
442 emit dataChanged(leftIndex, rightIndex);
445 void TreeModel::connectItem(AbstractTreeItem *item) {
446 connect(item, SIGNAL(dataChanged(int)),
447 this, SLOT(itemDataChanged(int)));
449 connect(item, SIGNAL(beginAppendChilds(int, int)),
450 this, SLOT(beginAppendChilds(int, int)));
451 connect(item, SIGNAL(endAppendChilds()),
452 this, SLOT(endAppendChilds()));
454 connect(item, SIGNAL(beginRemoveChilds(int, int)),
455 this, SLOT(beginRemoveChilds(int, int)));
456 connect(item, SIGNAL(endRemoveChilds()),
457 this, SLOT(endRemoveChilds()));
460 void TreeModel::beginAppendChilds(int firstRow, int lastRow) {
461 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
463 qWarning() << "TreeModel::beginAppendChilds(): cannot append Childs to unknown parent";
467 QModelIndex parent = indexByItem(parentItem);
468 Q_ASSERT(!_aboutToRemoveOrInsert);
470 _aboutToRemoveOrInsert = true;
471 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
472 beginInsertRows(parent, firstRow, lastRow);
475 void TreeModel::endAppendChilds() {
476 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
478 qWarning() << "TreeModel::endAppendChilds(): cannot append Childs to unknown parent";
481 Q_ASSERT(_aboutToRemoveOrInsert);
482 ChildStatus cs = _childStatus;
483 QModelIndex parent = indexByItem(parentItem);
484 Q_ASSERT(cs.parent == parent);
485 Q_ASSERT(rowCount(parent) == cs.childCount + cs.end - cs.start + 1);
487 _aboutToRemoveOrInsert = false;
488 for(int i = cs.start; i <= cs.end; i++) {
489 connectItem(parentItem->child(i));
494 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
495 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
497 qWarning() << "TreeModel::beginRemoveChilds(): cannot append Childs to unknown parent";
500 QModelIndex parent = indexByItem(parentItem);
501 Q_ASSERT(firstRow <= lastRow);
502 Q_ASSERT(parentItem->childCount() > lastRow);
503 Q_ASSERT(!_aboutToRemoveOrInsert);
505 _aboutToRemoveOrInsert = true;
506 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
507 beginRemoveRows(parent, firstRow, lastRow);
510 void TreeModel::endRemoveChilds() {
511 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
513 qWarning() << "TreeModel::endRemoveChilds(): cannot append Childs to unknown parent";
516 Q_ASSERT(_aboutToRemoveOrInsert);
517 ChildStatus cs = _childStatus;
518 QModelIndex parent = indexByItem(parentItem);
519 Q_ASSERT(cs.parent == parent);
520 Q_ASSERT(rowCount(parent) == cs.childCount - cs.end + cs.start - 1);
522 _aboutToRemoveOrInsert = false;
526 void TreeModel::clear() {
527 rootItem->removeAllChilds();
530 void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
531 qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
534 void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
535 AbstractTreeItem *parentItem;
536 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
538 parentItem = rootItem;
539 qDebug() << "#" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
542 AbstractTreeItem *childItem;
543 for(int i = end; i >= start; i--) {
544 child = parent.child(i, 0);
545 childItem = parentItem->child(i);
547 qDebug() << ">>>" << i << child << childItem->id() << child.data().toString();
551 void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end) {
552 AbstractTreeItem *parentItem;
553 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
555 parentItem = rootItem;
556 qDebug() << "#" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
559 AbstractTreeItem *childItem;
560 for(int i = start; i <= end; i++) {
561 child = parent.child(i, 0);
562 childItem = parentItem->child(i);
564 qDebug() << "<<<" << i << child << childItem->id() << child.data().toString();
568 void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
569 qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;