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::removeChild(int row) {
55 if(childCount() <= row)
58 child(row)->removeAllChilds();
59 emit beginRemoveChilds(row, row);
60 AbstractTreeItem *treeitem = _childItems.takeAt(row);
61 treeitem->deleteLater();
62 emit endRemoveChilds();
67 bool AbstractTreeItem::removeChildById(const quint64 &id) {
68 const int numChilds = childCount();
70 for(int i = 0; i < numChilds; i++) {
71 if(_childItems[i]->id() == id)
72 return removeChild(i);
78 void AbstractTreeItem::removeAllChilds() {
79 const int numChilds = childCount();
84 AbstractTreeItem *child;
86 QList<AbstractTreeItem *>::iterator childIter = _childItems.begin();
87 while(childIter != _childItems.end()) {
89 child->removeAllChilds();
92 emit beginRemoveChilds(0, numChilds - 1);
93 while(childIter != _childItems.end()) {
95 childIter = _childItems.erase(childIter);
98 emit endRemoveChilds();
101 AbstractTreeItem *AbstractTreeItem::child(int row) const {
102 if(childCount() <= row)
105 return _childItems[row];
108 AbstractTreeItem *AbstractTreeItem::childById(const quint64 &id) const {
109 const int numChilds = childCount();
110 for(int i = 0; i < numChilds; i++) {
111 if(_childItems[i]->id() == id)
112 return _childItems[i];
117 int AbstractTreeItem::childCount() const {
118 return _childItems.count();
121 int AbstractTreeItem::row() const {
125 return parent()->_childItems.indexOf(const_cast<AbstractTreeItem *>(this));
128 AbstractTreeItem *AbstractTreeItem::parent() const {
129 return qobject_cast<AbstractTreeItem *>(QObject::parent());
132 Qt::ItemFlags AbstractTreeItem::flags() const {
136 void AbstractTreeItem::setFlags(Qt::ItemFlags flags) {
140 void AbstractTreeItem::dumpChildList() {
141 qDebug() << "==== Childlist for Item:" << this << id() << "====";
142 if(childCount() > 0) {
143 AbstractTreeItem *child;
144 QList<AbstractTreeItem *>::const_iterator childIter = _childItems.constBegin();
145 while(childIter != _childItems.constEnd()) {
147 qDebug() << "Row:" << child->row() << child << child->id() << child->data(0, Qt::DisplayRole);
151 qDebug() << "==== End Of Childlist ====";
154 /*****************************************
156 *****************************************/
157 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
158 : AbstractTreeItem(parent),
163 SimpleTreeItem::~SimpleTreeItem() {
166 QVariant SimpleTreeItem::data(int column, int role) const {
167 if(column >= columnCount() || role != Qt::DisplayRole)
170 return _itemData[column];
173 bool SimpleTreeItem::setData(int column, const QVariant &value, int role) {
174 if(column > columnCount() || role != Qt::DisplayRole)
177 if(column == columnCount())
178 _itemData.append(value);
180 _itemData[column] = value;
182 emit dataChanged(column);
186 int SimpleTreeItem::columnCount() const {
187 return _itemData.count();
190 /*****************************************
192 *****************************************/
193 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
194 : AbstractTreeItem(parent),
195 _propertyOrder(propertyOrder)
199 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
200 : AbstractTreeItem(parent),
201 _propertyOrder(QStringList())
206 PropertyMapItem::~PropertyMapItem() {
209 QVariant PropertyMapItem::data(int column, int role) const {
210 if(column >= columnCount())
214 case Qt::ToolTipRole:
215 return toolTip(column);
216 case Qt::DisplayRole:
217 return property(_propertyOrder[column].toAscii());
224 bool PropertyMapItem::setData(int column, const QVariant &value, int role) {
225 if(column >= columnCount() || role != Qt::DisplayRole)
228 emit dataChanged(column);
229 return setProperty(_propertyOrder[column].toAscii(), value);
232 int PropertyMapItem::columnCount() const {
233 return _propertyOrder.count();
236 void PropertyMapItem::appendProperty(const QString &property) {
237 _propertyOrder << property;
242 /*****************************************
244 *****************************************/
245 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
246 : QAbstractItemModel(parent),
247 _childStatus(QModelIndex(), 0, 0, 0),
248 _aboutToRemoveOrInsert(false)
250 rootItem = new SimpleTreeItem(data, 0);
251 connectItem(rootItem);
253 if(QCoreApplication::instance()->arguments().contains("--debugmodel")) {
254 connect(this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
255 this, SLOT(debug_rowsAboutToBeInserted(const QModelIndex &, int, int)));
256 connect(this, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
257 this, SLOT(debug_rowsAboutToBeRemoved(const QModelIndex &, int, int)));
258 connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
259 this, SLOT(debug_rowsInserted(const QModelIndex &, int, int)));
260 connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
261 this, SLOT(debug_rowsRemoved(const QModelIndex &, int, int)));
265 TreeModel::~TreeModel() {
269 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
270 if(!hasIndex(row, column, parent))
271 return QModelIndex();
273 AbstractTreeItem *parentItem;
275 if(!parent.isValid())
276 parentItem = rootItem;
278 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
280 AbstractTreeItem *childItem = parentItem->child(row);
283 return createIndex(row, column, childItem);
285 return QModelIndex();
288 QModelIndex TreeModel::indexById(quint64 id, const QModelIndex &parent) const {
289 AbstractTreeItem *parentItem;
291 if(!parent.isValid())
292 parentItem = rootItem;
294 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
296 AbstractTreeItem *childItem = parentItem->childById(id);
299 return createIndex(childItem->row(), 0, childItem);
301 return QModelIndex();
304 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
306 qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
307 return QModelIndex();
311 return QModelIndex();
313 return createIndex(item->row(), 0, item);
316 QModelIndex TreeModel::parent(const QModelIndex &index) const {
317 if(!index.isValid()) {
318 qWarning() << "TreeModel::parent(): has been asked for the rootItems Parent!";
319 return QModelIndex();
322 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
323 AbstractTreeItem *parentItem = childItem->parent();
325 Q_ASSERT(parentItem);
326 if(parentItem == rootItem)
327 return QModelIndex();
329 return createIndex(parentItem->row(), 0, parentItem);
332 int TreeModel::rowCount(const QModelIndex &parent) const {
333 AbstractTreeItem *parentItem;
334 if(!parent.isValid())
335 parentItem = rootItem;
337 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
339 return parentItem->childCount();
342 int TreeModel::columnCount(const QModelIndex &parent) const {
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 // if(parent.isValid()) {
350 // AbstractTreeItem *child;
351 // if(child = static_cast<AbstractTreeItem *>(parent.internalPointer())->child(parent.column(), parent.row()))
352 // return child->columnCount();
354 // return static_cast<AbstractTreeItem*>(parent.internalPointer())->columnCount();
356 // return rootItem->columnCount();
359 return rootItem->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 AbstractTreeItem *item;
383 item = static_cast<AbstractTreeItem *>(index.internalPointer());
384 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";
467 QModelIndex parent = indexByItem(parentItem);
468 Q_ASSERT(firstRow <= lastRow);
469 Q_ASSERT(parentItem->childCount() > lastRow);
470 Q_ASSERT(!_aboutToRemoveOrInsert);
472 _aboutToRemoveOrInsert = true;
473 _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
474 beginRemoveRows(parent, firstRow, lastRow);
477 void TreeModel::endRemoveChilds() {
478 AbstractTreeItem *parentItem = qobject_cast<AbstractTreeItem *>(sender());
480 qWarning() << "TreeModel::endRemoveChilds(): 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;
493 void TreeModel::clear() {
494 rootItem->removeAllChilds();
497 void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) {
498 // qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
501 void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
502 AbstractTreeItem *parentItem;
503 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
505 parentItem = rootItem;
506 qDebug() << "#" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
509 AbstractTreeItem *childItem;
510 for(int i = end; i >= start; i--) {
511 child = parent.child(i, 0);
512 childItem = parentItem->child(i);
514 qDebug() << ">>>" << i << child << childItem->id() << child.data().toString();
518 void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end) {
519 AbstractTreeItem *parentItem;
520 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
522 parentItem = rootItem;
523 qDebug() << "#" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
526 AbstractTreeItem *childItem;
527 for(int i = start; i <= end; i++) {
528 child = parent.child(i, 0);
529 childItem = parentItem->child(i);
531 qDebug() << "<<<" << i << child << childItem->id() << child.data().toString();
535 void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end) {
536 // qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;