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)
36 AbstractTreeItem::~AbstractTreeItem() {
39 bool AbstractTreeItem::newChild(AbstractTreeItem *item) {
40 int newRow = childCount();
41 emit beginAppendChilds(newRow, newRow);
42 _childItems.append(item);
43 emit endAppendChilds();
47 bool AbstractTreeItem::newChilds(const QList<AbstractTreeItem *> &items) {
51 int nextRow = childCount();
52 int lastRow = nextRow + items.count() - 1;
54 emit beginAppendChilds(nextRow, lastRow);
56 emit endAppendChilds();
61 bool AbstractTreeItem::removeChild(int row) {
62 if(childCount() <= row)
65 child(row)->removeAllChilds();
66 emit beginRemoveChilds(row, row);
67 AbstractTreeItem *treeitem = _childItems.takeAt(row);
68 treeitem->deleteLater();
69 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->removeAllChilds();
91 emit beginRemoveChilds(0, numChilds - 1);
92 childIter = _childItems.begin();
93 while(childIter != _childItems.end()) {
95 childIter = _childItems.erase(childIter);
98 emit endRemoveChilds();
101 bool AbstractTreeItem::reParent(AbstractTreeItem *newParent) {
102 // currently we support only re parenting if the child that's about to be
103 // adopted does not have any children itself.
104 if(childCount() != 0) {
105 qDebug() << "AbstractTreeItem::reParent(): cannot reparent" << this << "with children.";
113 emit parent()->beginRemoveChilds(oldRow, oldRow);
114 parent()->_childItems.removeAt(oldRow);
115 emit parent()->endRemoveChilds();
117 setParent(newParent);
119 bool success = newParent->newChild(this);
121 qWarning() << "AbstractTreeItem::reParent(): failed to attach to new parent after removing from old parent! this:" << this << "new parent:" << newParent;
126 AbstractTreeItem *AbstractTreeItem::child(int row) const {
127 if(childCount() <= row)
130 return _childItems[row];
133 int AbstractTreeItem::childCount(int column) const {
137 return _childItems.count();
140 int AbstractTreeItem::row() const {
142 qWarning() << "AbstractTreeItem::row():" << this << "has no parent AbstractTreeItem as it's parent! parent is" << QObject::parent();
146 int row_ = parent()->_childItems.indexOf(const_cast<AbstractTreeItem *>(this));
148 qWarning() << "AbstractTreeItem::row():" << this << "is not in the child list of" << QObject::parent();
152 void AbstractTreeItem::dumpChildList() {
153 qDebug() << "==== Childlist for Item:" << this << "====";
154 if(childCount() > 0) {
155 AbstractTreeItem *child;
156 QList<AbstractTreeItem *>::const_iterator childIter = _childItems.constBegin();
157 while(childIter != _childItems.constEnd()) {
159 qDebug() << "Row:" << child->row() << child << child->data(0, Qt::DisplayRole);
163 qDebug() << "==== End Of Childlist ====";
166 /*****************************************
168 *****************************************/
169 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
170 : AbstractTreeItem(parent),
175 SimpleTreeItem::~SimpleTreeItem() {
178 QVariant SimpleTreeItem::data(int column, int role) const {
179 if(column >= columnCount() || role != Qt::DisplayRole)
182 return _itemData[column];
185 bool SimpleTreeItem::setData(int column, const QVariant &value, int role) {
186 if(column > columnCount() || role != Qt::DisplayRole)
189 if(column == columnCount())
190 _itemData.append(value);
192 _itemData[column] = value;
194 emit dataChanged(column);
198 int SimpleTreeItem::columnCount() const {
199 return _itemData.count();
202 /*****************************************
204 *****************************************/
205 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
206 : AbstractTreeItem(parent),
207 _propertyOrder(propertyOrder)
211 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
212 : AbstractTreeItem(parent),
213 _propertyOrder(QStringList())
218 PropertyMapItem::~PropertyMapItem() {
221 QVariant PropertyMapItem::data(int column, int role) const {
222 if(column >= columnCount())
226 case Qt::ToolTipRole:
227 return toolTip(column);
228 case Qt::DisplayRole:
229 case TreeModel::SortRole: // fallthrough, since SortRole should default to DisplayRole
230 return property(_propertyOrder[column].toAscii());
237 bool PropertyMapItem::setData(int column, const QVariant &value, int role) {
238 if(column >= columnCount() || role != Qt::DisplayRole)
241 emit dataChanged(column);
242 return setProperty(_propertyOrder[column].toAscii(), value);
245 int PropertyMapItem::columnCount() const {
246 return _propertyOrder.count();
249 void PropertyMapItem::appendProperty(const QString &property) {
250 _propertyOrder << property;
255 /*****************************************
257 *****************************************/
258 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
259 : QAbstractItemModel(parent),
260 _childStatus(QModelIndex(), 0, 0, 0),
261 _aboutToRemoveOrInsert(false)
263 rootItem = new SimpleTreeItem(data, 0);
264 connectItem(rootItem);
266 if(Global::parser.isSet("debugmodel")) {
267 connect(this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
268 this, SLOT(debug_rowsAboutToBeInserted(const QModelIndex &, int, int)));
269 connect(this, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
270 this, SLOT(debug_rowsAboutToBeRemoved(const QModelIndex &, int, int)));
271 connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
272 this, SLOT(debug_rowsInserted(const QModelIndex &, int, int)));
273 connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
274 this, SLOT(debug_rowsRemoved(const QModelIndex &, int, int)));
275 connect(this, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
276 this, SLOT(debug_dataChanged(const QModelIndex &, const QModelIndex &)));
280 TreeModel::~TreeModel() {
284 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
285 if(!hasIndex(row, column, parent))
286 return QModelIndex();
288 AbstractTreeItem *parentItem;
290 if(!parent.isValid())
291 parentItem = rootItem;
293 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
295 AbstractTreeItem *childItem = parentItem->child(row);
298 return createIndex(row, column, childItem);
300 return QModelIndex();
303 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
305 qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
306 return QModelIndex();
310 return QModelIndex();
312 return createIndex(item->row(), 0, item);
315 QModelIndex TreeModel::parent(const QModelIndex &index) const {
316 if(!index.isValid()) {
317 qWarning() << "TreeModel::parent(): has been asked for the rootItems Parent!";
318 return QModelIndex();
321 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
322 AbstractTreeItem *parentItem = childItem->parent();
324 Q_ASSERT(parentItem);
325 if(parentItem == rootItem)
326 return QModelIndex();
328 return createIndex(parentItem->row(), 0, parentItem);
331 int TreeModel::rowCount(const QModelIndex &parent) const {
332 AbstractTreeItem *parentItem;
333 if(!parent.isValid())
334 parentItem = rootItem;
336 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
338 return parentItem->childCount(parent.column());
341 int TreeModel::columnCount(const QModelIndex &parent) const {
343 return rootItem->columnCount();
344 // since there the Qt Views don't draw more columns than the header has columns
345 // we can be lazy and simply return the count of header columns
346 // actually this gives us more freedom cause we don't have to ensure that a rows parent
347 // has equal or more columns than that row
349 // AbstractTreeItem *parentItem;
350 // if(!parent.isValid())
351 // parentItem = rootItem;
353 // parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
354 // return parentItem->columnCount();
357 QVariant TreeModel::data(const QModelIndex &index, int role) const {
361 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
362 return item->data(index.column(), role);
365 bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
369 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
370 return item->setData(index.column(), value, role);
373 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
374 if(!index.isValid()) {
375 return rootItem->flags() & Qt::ItemIsDropEnabled;
377 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
378 return item->flags();
382 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
383 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
384 return rootItem->data(section, role);
389 void TreeModel::itemDataChanged(int column) {
390 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
391 QModelIndex leftIndex, rightIndex;
397 leftIndex = createIndex(item->row(), 0, item);
398 rightIndex = createIndex(item->row(), item->columnCount() - 1, item);
400 leftIndex = createIndex(item->row(), column, item);
401 rightIndex = leftIndex;
404 emit dataChanged(leftIndex, rightIndex);
407 void TreeModel::connectItem(AbstractTreeItem *item) {
408 connect(item, SIGNAL(dataChanged(int)),
409 this, SLOT(itemDataChanged(int)));
411 connect(item, SIGNAL(beginAppendChilds(int, int)),
412 this, SLOT(beginAppendChilds(int, int)));
413 connect(item, SIGNAL(endAppendChilds()),
414 this, SLOT(endAppendChilds()));
416 connect(item, SIGNAL(beginRemoveChilds(int, int)),
417 this, SLOT(beginRemoveChilds(int, int)));
418 connect(item, SIGNAL(endRemoveChilds()),
419 this, SLOT(endRemoveChilds()));
422 void TreeModel::beginAppendChilds(int firstRow, int lastRow) {
423 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
425 qWarning() << "TreeModel::beginAppendChilds(): cannot append Childs to unknown parent";
429 QModelIndex parent = indexByItem(parentItem);
430 Q_ASSERT(!_aboutToRemoveOrInsert);
432 _aboutToRemoveOrInsert = true;
433 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
434 beginInsertRows(parent, firstRow, lastRow);
437 void TreeModel::endAppendChilds() {
438 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
440 qWarning() << "TreeModel::endAppendChilds(): cannot append Childs to unknown parent";
443 Q_ASSERT(_aboutToRemoveOrInsert);
444 ChildStatus cs = _childStatus;
445 QModelIndex parent = indexByItem(parentItem);
446 Q_ASSERT(cs.parent == parent);
447 Q_ASSERT(rowCount(parent) == cs.childCount + cs.end - cs.start + 1);
449 _aboutToRemoveOrInsert = false;
450 for(int i = cs.start; i <= cs.end; i++) {
451 connectItem(parentItem->child(i));
456 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
457 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
459 qWarning() << "TreeModel::beginRemoveChilds(): cannot append Childs to unknown parent";
463 for(int i = firstRow; i <= lastRow; i++) {
464 disconnect(parentItem->child(i), 0, this, 0);
468 QModelIndex parent = indexByItem(parentItem);
469 Q_ASSERT(firstRow <= lastRow);
470 Q_ASSERT(parentItem->childCount() > lastRow);
471 Q_ASSERT(!_aboutToRemoveOrInsert);
472 _aboutToRemoveOrInsert = true;
473 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
475 beginRemoveRows(parent, firstRow, lastRow);
478 void TreeModel::endRemoveChilds() {
479 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
481 qWarning() << "TreeModel::endRemoveChilds(): cannot remove Childs from unknown parent";
485 // concistency checks
486 Q_ASSERT(_aboutToRemoveOrInsert);
487 ChildStatus cs = _childStatus;
488 QModelIndex parent = indexByItem(parentItem);
489 Q_ASSERT(cs.parent == parent);
490 Q_ASSERT(rowCount(parent) == cs.childCount - cs.end + cs.start - 1);
491 _aboutToRemoveOrInsert = false;
496 void TreeModel::clear() {
497 rootItem->removeAllChilds();
500 void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
501 qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
504 void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
505 AbstractTreeItem *parentItem;
506 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
508 parentItem = rootItem;
509 qDebug() << "debug_rowsAboutToBeRemoved" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
512 AbstractTreeItem *childItem;
513 for(int i = end; i >= start; i--) {
514 child = parent.child(i, 0);
515 childItem = parentItem->child(i);
517 qDebug() << ">>>" << i << child << child.data().toString();
521 void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end) {
522 AbstractTreeItem *parentItem;
523 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
525 parentItem = rootItem;
526 qDebug() << "debug_rowsInserted:" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
529 AbstractTreeItem *childItem;
530 for(int i = start; i <= end; i++) {
531 child = parent.child(i, 0);
532 childItem = parentItem->child(i);
534 qDebug() << "<<<" << i << child << child.data().toString();
538 void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
539 qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
542 void TreeModel::debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
543 qDebug() << "debug_dataChanged" << topLeft << bottomRight;
544 QStringList displayData;
545 for(int row = topLeft.row(); row <= bottomRight.row(); row++) {
546 displayData = QStringList();
547 for(int column = topLeft.column(); column <= bottomRight.column(); column++) {
548 displayData << data(topLeft.sibling(row, column), Qt::DisplayRole).toString();
550 qDebug() << " row:" << row << displayData;