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 parent()->checkForDeletion();
122 setParent(newParent);
124 bool success = newParent->newChild(this);
126 qWarning() << "AbstractTreeItem::reParent(): failed to attach to new parent after removing from old parent! this:" << this << "new parent:" << newParent;
131 AbstractTreeItem *AbstractTreeItem::child(int row) const {
132 if(childCount() <= row)
135 return _childItems[row];
138 int AbstractTreeItem::childCount(int column) const {
142 return _childItems.count();
145 int AbstractTreeItem::row() const {
147 qWarning() << "AbstractTreeItem::row():" << this << "has no parent AbstractTreeItem as it's parent! parent is" << QObject::parent();
151 int row_ = parent()->_childItems.indexOf(const_cast<AbstractTreeItem *>(this));
153 qWarning() << "AbstractTreeItem::row():" << this << "is not in the child list of" << QObject::parent();
157 void AbstractTreeItem::dumpChildList() {
158 qDebug() << "==== Childlist for Item:" << this << "====";
159 if(childCount() > 0) {
160 AbstractTreeItem *child;
161 QList<AbstractTreeItem *>::const_iterator childIter = _childItems.constBegin();
162 while(childIter != _childItems.constEnd()) {
164 qDebug() << "Row:" << child->row() << child << child->data(0, Qt::DisplayRole);
168 qDebug() << "==== End Of Childlist ====";
171 /*****************************************
173 *****************************************/
174 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
175 : AbstractTreeItem(parent),
180 SimpleTreeItem::~SimpleTreeItem() {
183 QVariant SimpleTreeItem::data(int column, int role) const {
184 if(column >= columnCount() || role != Qt::DisplayRole)
187 return _itemData[column];
190 bool SimpleTreeItem::setData(int column, const QVariant &value, int role) {
191 if(column > columnCount() || role != Qt::DisplayRole)
194 if(column == columnCount())
195 _itemData.append(value);
197 _itemData[column] = value;
199 emit dataChanged(column);
203 int SimpleTreeItem::columnCount() const {
204 return _itemData.count();
207 /*****************************************
209 *****************************************/
210 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
211 : AbstractTreeItem(parent),
212 _propertyOrder(propertyOrder)
216 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
217 : AbstractTreeItem(parent),
218 _propertyOrder(QStringList())
223 PropertyMapItem::~PropertyMapItem() {
226 QVariant PropertyMapItem::data(int column, int role) const {
227 if(column >= columnCount())
231 case Qt::ToolTipRole:
232 return toolTip(column);
233 case Qt::DisplayRole:
234 case TreeModel::SortRole: // fallthrough, since SortRole should default to DisplayRole
235 return property(_propertyOrder[column].toAscii());
242 bool PropertyMapItem::setData(int column, const QVariant &value, int role) {
243 if(column >= columnCount() || role != Qt::DisplayRole)
246 emit dataChanged(column);
247 return setProperty(_propertyOrder[column].toAscii(), value);
250 int PropertyMapItem::columnCount() const {
251 return _propertyOrder.count();
254 void PropertyMapItem::appendProperty(const QString &property) {
255 _propertyOrder << property;
260 /*****************************************
262 *****************************************/
263 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
264 : QAbstractItemModel(parent),
265 _childStatus(QModelIndex(), 0, 0, 0),
266 _aboutToRemoveOrInsert(false)
268 rootItem = new SimpleTreeItem(data, 0);
269 connectItem(rootItem);
271 if(Global::parser.isSet("debugmodel")) {
272 connect(this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
273 this, SLOT(debug_rowsAboutToBeInserted(const QModelIndex &, int, int)));
274 connect(this, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
275 this, SLOT(debug_rowsAboutToBeRemoved(const QModelIndex &, int, int)));
276 connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
277 this, SLOT(debug_rowsInserted(const QModelIndex &, int, int)));
278 connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
279 this, SLOT(debug_rowsRemoved(const QModelIndex &, int, int)));
280 connect(this, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
281 this, SLOT(debug_dataChanged(const QModelIndex &, const QModelIndex &)));
285 TreeModel::~TreeModel() {
289 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
290 if(!hasIndex(row, column, parent))
291 return QModelIndex();
293 AbstractTreeItem *parentItem;
295 if(!parent.isValid())
296 parentItem = rootItem;
298 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
300 AbstractTreeItem *childItem = parentItem->child(row);
303 return createIndex(row, column, childItem);
305 return QModelIndex();
308 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
310 qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
311 return QModelIndex();
315 return QModelIndex();
317 return createIndex(item->row(), 0, item);
320 QModelIndex TreeModel::parent(const QModelIndex &index) const {
321 if(!index.isValid()) {
322 qWarning() << "TreeModel::parent(): has been asked for the rootItems Parent!";
323 return QModelIndex();
326 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
327 AbstractTreeItem *parentItem = childItem->parent();
329 Q_ASSERT(parentItem);
330 if(parentItem == rootItem)
331 return QModelIndex();
333 return createIndex(parentItem->row(), 0, parentItem);
336 int TreeModel::rowCount(const QModelIndex &parent) const {
337 AbstractTreeItem *parentItem;
338 if(!parent.isValid())
339 parentItem = rootItem;
341 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
343 return parentItem->childCount(parent.column());
346 int TreeModel::columnCount(const QModelIndex &parent) const {
348 return rootItem->columnCount();
349 // since there the Qt Views don't draw more columns than the header has columns
350 // we can be lazy and simply return the count of header columns
351 // actually this gives us more freedom cause we don't have to ensure that a rows parent
352 // has equal or more columns than that row
354 // AbstractTreeItem *parentItem;
355 // if(!parent.isValid())
356 // parentItem = rootItem;
358 // parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
359 // return parentItem->columnCount();
362 QVariant TreeModel::data(const QModelIndex &index, int role) const {
366 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
367 return item->data(index.column(), role);
370 bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
374 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
375 return item->setData(index.column(), value, role);
378 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
379 if(!index.isValid()) {
380 return rootItem->flags() & Qt::ItemIsDropEnabled;
382 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
383 return item->flags();
387 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
388 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
389 return rootItem->data(section, role);
394 void TreeModel::itemDataChanged(int column) {
395 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
396 QModelIndex leftIndex, rightIndex;
402 leftIndex = createIndex(item->row(), 0, item);
403 rightIndex = createIndex(item->row(), item->columnCount() - 1, item);
405 leftIndex = createIndex(item->row(), column, item);
406 rightIndex = leftIndex;
409 emit dataChanged(leftIndex, rightIndex);
412 void TreeModel::connectItem(AbstractTreeItem *item) {
413 connect(item, SIGNAL(dataChanged(int)),
414 this, SLOT(itemDataChanged(int)));
416 connect(item, SIGNAL(beginAppendChilds(int, int)),
417 this, SLOT(beginAppendChilds(int, int)));
418 connect(item, SIGNAL(endAppendChilds()),
419 this, SLOT(endAppendChilds()));
421 connect(item, SIGNAL(beginRemoveChilds(int, int)),
422 this, SLOT(beginRemoveChilds(int, int)));
423 connect(item, SIGNAL(endRemoveChilds()),
424 this, SLOT(endRemoveChilds()));
427 void TreeModel::beginAppendChilds(int firstRow, int lastRow) {
428 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
430 qWarning() << "TreeModel::beginAppendChilds(): cannot append Childs to unknown parent";
434 QModelIndex parent = indexByItem(parentItem);
435 Q_ASSERT(!_aboutToRemoveOrInsert);
437 _aboutToRemoveOrInsert = true;
438 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
439 beginInsertRows(parent, firstRow, lastRow);
442 void TreeModel::endAppendChilds() {
443 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
445 qWarning() << "TreeModel::endAppendChilds(): cannot append Childs to unknown parent";
448 Q_ASSERT(_aboutToRemoveOrInsert);
449 ChildStatus cs = _childStatus;
450 QModelIndex parent = indexByItem(parentItem);
451 Q_ASSERT(cs.parent == parent);
452 Q_ASSERT(rowCount(parent) == cs.childCount + cs.end - cs.start + 1);
454 _aboutToRemoveOrInsert = false;
455 for(int i = cs.start; i <= cs.end; i++) {
456 connectItem(parentItem->child(i));
461 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
462 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
464 qWarning() << "TreeModel::beginRemoveChilds(): cannot append Childs to unknown parent";
468 for(int i = firstRow; i <= lastRow; i++) {
469 disconnect(parentItem->child(i), 0, this, 0);
473 QModelIndex parent = indexByItem(parentItem);
474 Q_ASSERT(firstRow <= lastRow);
475 Q_ASSERT(parentItem->childCount() > lastRow);
476 Q_ASSERT(!_aboutToRemoveOrInsert);
477 _aboutToRemoveOrInsert = true;
478 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
480 beginRemoveRows(parent, firstRow, lastRow);
483 void TreeModel::endRemoveChilds() {
484 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
486 qWarning() << "TreeModel::endRemoveChilds(): cannot remove Childs from unknown parent";
490 // concistency checks
491 Q_ASSERT(_aboutToRemoveOrInsert);
492 ChildStatus cs = _childStatus;
493 QModelIndex parent = indexByItem(parentItem);
494 Q_ASSERT(cs.parent == parent);
495 Q_ASSERT(rowCount(parent) == cs.childCount - cs.end + cs.start - 1);
496 _aboutToRemoveOrInsert = false;
501 void TreeModel::clear() {
502 rootItem->removeAllChilds();
505 void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
506 qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
509 void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
510 AbstractTreeItem *parentItem;
511 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
513 parentItem = rootItem;
514 qDebug() << "debug_rowsAboutToBeRemoved" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
517 AbstractTreeItem *childItem;
518 for(int i = end; i >= start; i--) {
519 child = parent.child(i, 0);
520 childItem = parentItem->child(i);
522 qDebug() << ">>>" << i << child << child.data().toString();
526 void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end) {
527 AbstractTreeItem *parentItem;
528 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
530 parentItem = rootItem;
531 qDebug() << "debug_rowsInserted:" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
534 AbstractTreeItem *childItem;
535 for(int i = start; i <= end; i++) {
536 child = parent.child(i, 0);
537 childItem = parentItem->child(i);
539 qDebug() << "<<<" << i << child << child.data().toString();
543 void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
544 qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
547 void TreeModel::debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
548 qDebug() << "debug_dataChanged" << topLeft << bottomRight;
549 QStringList displayData;
550 for(int row = topLeft.row(); row <= bottomRight.row(); row++) {
551 displayData = QStringList();
552 for(int column = topLeft.column(); column <= bottomRight.column(); column++) {
553 displayData << data(topLeft.sibling(row, column), Qt::DisplayRole).toString();
555 qDebug() << " row:" << row << displayData;