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"
25 #include <QCoreApplication>
27 /*****************************************
28 * Abstract Items of a TreeModel
29 *****************************************/
30 AbstractTreeItem::AbstractTreeItem(AbstractTreeItem *parent)
32 _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled),
37 bool AbstractTreeItem::newChild(AbstractTreeItem *item) {
38 int newRow = childCount();
39 emit beginAppendChilds(newRow, newRow);
40 _childItems.append(item);
41 emit endAppendChilds();
45 bool AbstractTreeItem::newChilds(const QList<AbstractTreeItem *> &items) {
49 int nextRow = childCount();
50 int lastRow = nextRow + items.count() - 1;
52 emit beginAppendChilds(nextRow, lastRow);
54 emit endAppendChilds();
59 bool AbstractTreeItem::removeChild(int row) {
60 if(row < 0 || childCount() <= row)
63 child(row)->removeAllChilds();
64 emit beginRemoveChilds(row, row);
65 AbstractTreeItem *treeitem = _childItems.takeAt(row);
67 emit endRemoveChilds();
74 void AbstractTreeItem::removeAllChilds() {
75 const int numChilds = childCount();
80 AbstractTreeItem *child;
82 QList<AbstractTreeItem *>::iterator childIter;
84 childIter = _childItems.begin();
85 while(childIter != _childItems.end()) {
87 child->setTreeItemFlags(0); // disable self deletion, as this would only fuck up consitency and the child gets deleted anyways
88 child->removeAllChilds();
92 emit beginRemoveChilds(0, numChilds - 1);
93 childIter = _childItems.begin();
94 while(childIter != _childItems.end()) {
96 childIter = _childItems.erase(childIter);
99 emit endRemoveChilds();
104 bool AbstractTreeItem::reParent(AbstractTreeItem *newParent) {
105 // currently we support only re parenting if the child that's about to be
106 // adopted does not have any children itself.
107 if(childCount() != 0) {
108 qDebug() << "AbstractTreeItem::reParent(): cannot reparent" << this << "with children.";
116 emit parent()->beginRemoveChilds(oldRow, oldRow);
117 parent()->_childItems.removeAt(oldRow);
118 emit parent()->endRemoveChilds();
120 AbstractTreeItem *oldParent = parent();
121 setParent(newParent);
123 bool success = newParent->newChild(this);
125 qWarning() << "AbstractTreeItem::reParent(): failed to attach to new parent after removing from old parent! this:" << this << "new parent:" << newParent;
128 oldParent->checkForDeletion();
133 AbstractTreeItem *AbstractTreeItem::child(int row) const {
134 if(childCount() <= row)
137 return _childItems[row];
140 int AbstractTreeItem::childCount(int column) const {
144 return _childItems.count();
147 int AbstractTreeItem::row() const {
149 qWarning() << "AbstractTreeItem::row():" << this << "has no parent AbstractTreeItem as it's parent! parent is" << QObject::parent();
153 int row_ = parent()->_childItems.indexOf(const_cast<AbstractTreeItem *>(this));
155 qWarning() << "AbstractTreeItem::row():" << this << "is not in the child list of" << QObject::parent();
159 void AbstractTreeItem::dumpChildList() {
160 qDebug() << "==== Childlist for Item:" << this << "====";
161 if(childCount() > 0) {
162 AbstractTreeItem *child;
163 QList<AbstractTreeItem *>::const_iterator childIter = _childItems.constBegin();
164 while(childIter != _childItems.constEnd()) {
166 qDebug() << "Row:" << child->row() << child << child->data(0, Qt::DisplayRole);
170 qDebug() << "==== End Of Childlist ====";
173 /*****************************************
175 *****************************************/
176 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
177 : AbstractTreeItem(parent),
182 SimpleTreeItem::~SimpleTreeItem() {
185 QVariant SimpleTreeItem::data(int column, int role) const {
186 if(column >= columnCount() || role != Qt::DisplayRole)
189 return _itemData[column];
192 bool SimpleTreeItem::setData(int column, const QVariant &value, int role) {
193 if(column > columnCount() || role != Qt::DisplayRole)
196 if(column == columnCount())
197 _itemData.append(value);
199 _itemData[column] = value;
201 emit dataChanged(column);
205 int SimpleTreeItem::columnCount() const {
206 return _itemData.count();
209 /*****************************************
211 *****************************************/
212 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
213 : AbstractTreeItem(parent),
214 _propertyOrder(propertyOrder)
218 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
219 : AbstractTreeItem(parent),
220 _propertyOrder(QStringList())
225 PropertyMapItem::~PropertyMapItem() {
228 QVariant PropertyMapItem::data(int column, int role) const {
229 if(column >= columnCount())
233 case Qt::ToolTipRole:
234 return toolTip(column);
235 case Qt::DisplayRole:
236 case TreeModel::SortRole: // fallthrough, since SortRole should default to DisplayRole
237 return property(_propertyOrder[column].toAscii());
244 bool PropertyMapItem::setData(int column, const QVariant &value, int role) {
245 if(column >= columnCount() || role != Qt::DisplayRole)
248 emit dataChanged(column);
249 return setProperty(_propertyOrder[column].toAscii(), value);
252 int PropertyMapItem::columnCount() const {
253 return _propertyOrder.count();
256 void PropertyMapItem::appendProperty(const QString &property) {
257 _propertyOrder << property;
262 /*****************************************
264 *****************************************/
265 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
266 : QAbstractItemModel(parent),
267 _childStatus(QModelIndex(), 0, 0, 0),
268 _aboutToRemoveOrInsert(false)
270 rootItem = new SimpleTreeItem(data, 0);
271 connectItem(rootItem);
273 if(Global::parser.isSet("debugmodel")) {
274 connect(this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
275 this, SLOT(debug_rowsAboutToBeInserted(const QModelIndex &, int, int)));
276 connect(this, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
277 this, SLOT(debug_rowsAboutToBeRemoved(const QModelIndex &, int, int)));
278 connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
279 this, SLOT(debug_rowsInserted(const QModelIndex &, int, int)));
280 connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
281 this, SLOT(debug_rowsRemoved(const QModelIndex &, int, int)));
282 connect(this, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
283 this, SLOT(debug_dataChanged(const QModelIndex &, const QModelIndex &)));
287 TreeModel::~TreeModel() {
291 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
292 if(!hasIndex(row, column, parent))
293 return QModelIndex();
295 AbstractTreeItem *parentItem;
297 if(!parent.isValid())
298 parentItem = rootItem;
300 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
302 AbstractTreeItem *childItem = parentItem->child(row);
305 return createIndex(row, column, childItem);
307 return QModelIndex();
310 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
312 qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
313 return QModelIndex();
317 return QModelIndex();
319 return createIndex(item->row(), 0, item);
322 QModelIndex TreeModel::parent(const QModelIndex &index) const {
323 if(!index.isValid()) {
324 qWarning() << "TreeModel::parent(): has been asked for the rootItems Parent!";
325 return QModelIndex();
328 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
329 AbstractTreeItem *parentItem = childItem->parent();
331 Q_ASSERT(parentItem);
332 if(parentItem == rootItem)
333 return QModelIndex();
335 return createIndex(parentItem->row(), 0, parentItem);
338 int TreeModel::rowCount(const QModelIndex &parent) const {
339 AbstractTreeItem *parentItem;
340 if(!parent.isValid())
341 parentItem = rootItem;
343 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
345 return parentItem->childCount(parent.column());
348 int TreeModel::columnCount(const QModelIndex &parent) const {
350 return rootItem->columnCount();
351 // since there the Qt Views don't draw more columns than the header has columns
352 // we can be lazy and simply return the count of header columns
353 // actually this gives us more freedom cause we don't have to ensure that a rows parent
354 // has equal or more columns than that row
356 // AbstractTreeItem *parentItem;
357 // if(!parent.isValid())
358 // parentItem = rootItem;
360 // parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
361 // return parentItem->columnCount();
364 QVariant TreeModel::data(const QModelIndex &index, int role) const {
368 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
369 return item->data(index.column(), role);
372 bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
376 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
377 return item->setData(index.column(), value, role);
380 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
381 if(!index.isValid()) {
382 return rootItem->flags() & Qt::ItemIsDropEnabled;
384 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
385 return item->flags();
389 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
390 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
391 return rootItem->data(section, role);
396 void TreeModel::itemDataChanged(int column) {
397 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
398 QModelIndex leftIndex, rightIndex;
404 leftIndex = createIndex(item->row(), 0, item);
405 rightIndex = createIndex(item->row(), item->columnCount() - 1, item);
407 leftIndex = createIndex(item->row(), column, item);
408 rightIndex = leftIndex;
411 emit dataChanged(leftIndex, rightIndex);
414 void TreeModel::connectItem(AbstractTreeItem *item) {
415 connect(item, SIGNAL(dataChanged(int)),
416 this, SLOT(itemDataChanged(int)));
418 connect(item, SIGNAL(beginAppendChilds(int, int)),
419 this, SLOT(beginAppendChilds(int, int)));
420 connect(item, SIGNAL(endAppendChilds()),
421 this, SLOT(endAppendChilds()));
423 connect(item, SIGNAL(beginRemoveChilds(int, int)),
424 this, SLOT(beginRemoveChilds(int, int)));
425 connect(item, SIGNAL(endRemoveChilds()),
426 this, SLOT(endRemoveChilds()));
429 void TreeModel::beginAppendChilds(int firstRow, int lastRow) {
430 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
432 qWarning() << "TreeModel::beginAppendChilds(): cannot append Childs to unknown parent";
436 QModelIndex parent = indexByItem(parentItem);
437 Q_ASSERT(!_aboutToRemoveOrInsert);
439 _aboutToRemoveOrInsert = true;
440 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
441 beginInsertRows(parent, firstRow, lastRow);
444 void TreeModel::endAppendChilds() {
445 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
447 qWarning() << "TreeModel::endAppendChilds(): cannot append Childs to unknown parent";
450 Q_ASSERT(_aboutToRemoveOrInsert);
451 ChildStatus cs = _childStatus;
452 QModelIndex parent = indexByItem(parentItem);
453 Q_ASSERT(cs.parent == parent);
454 Q_ASSERT(rowCount(parent) == cs.childCount + cs.end - cs.start + 1);
456 _aboutToRemoveOrInsert = false;
457 for(int i = cs.start; i <= cs.end; i++) {
458 connectItem(parentItem->child(i));
463 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
464 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
466 qWarning() << "TreeModel::beginRemoveChilds(): cannot append Childs to unknown parent";
470 for(int i = firstRow; i <= lastRow; i++) {
471 disconnect(parentItem->child(i), 0, this, 0);
475 QModelIndex parent = indexByItem(parentItem);
476 Q_ASSERT(firstRow <= lastRow);
477 Q_ASSERT(parentItem->childCount() > lastRow);
478 Q_ASSERT(!_aboutToRemoveOrInsert);
479 _aboutToRemoveOrInsert = true;
480 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
482 beginRemoveRows(parent, firstRow, lastRow);
485 void TreeModel::endRemoveChilds() {
486 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
488 qWarning() << "TreeModel::endRemoveChilds(): cannot remove Childs from unknown parent";
492 // concistency checks
493 Q_ASSERT(_aboutToRemoveOrInsert);
494 ChildStatus cs = _childStatus;
495 QModelIndex parent = indexByItem(parentItem);
496 Q_ASSERT(cs.parent == parent);
497 Q_ASSERT(rowCount(parent) == cs.childCount - cs.end + cs.start - 1);
498 _aboutToRemoveOrInsert = false;
503 void TreeModel::clear() {
504 rootItem->removeAllChilds();
507 void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
508 qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
511 void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
512 AbstractTreeItem *parentItem;
513 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
515 parentItem = rootItem;
516 qDebug() << "debug_rowsAboutToBeRemoved" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
519 AbstractTreeItem *childItem;
520 for(int i = end; i >= start; i--) {
521 child = parent.child(i, 0);
522 childItem = parentItem->child(i);
524 qDebug() << ">>>" << i << child << child.data().toString();
528 void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end) {
529 AbstractTreeItem *parentItem;
530 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
532 parentItem = rootItem;
533 qDebug() << "debug_rowsInserted:" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
536 AbstractTreeItem *childItem;
537 for(int i = start; i <= end; i++) {
538 child = parent.child(i, 0);
539 childItem = parentItem->child(i);
541 qDebug() << "<<<" << i << child << child.data().toString();
545 void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
546 qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
549 void TreeModel::debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
550 qDebug() << "debug_dataChanged" << topLeft << bottomRight;
551 QStringList displayData;
552 for(int row = topLeft.row(); row <= bottomRight.row(); row++) {
553 displayData = QStringList();
554 for(int column = topLeft.column(); column <= bottomRight.column(); column++) {
555 displayData << data(topLeft.sibling(row, column), Qt::DisplayRole).toString();
557 qDebug() << " row:" << row << displayData;