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 /*****************************************
26 * Abstract Items of a TreeModel
27 *****************************************/
28 AbstractTreeItem::AbstractTreeItem(AbstractTreeItem *parent)
30 _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
34 AbstractTreeItem::~AbstractTreeItem() {
38 quint64 AbstractTreeItem::id() const {
42 int AbstractTreeItem::defaultColumn() const {
43 // invalid QModelIndexes aka rootNodes get their Childs stuffed into column -1
51 void AbstractTreeItem::appendChild(int column, AbstractTreeItem *item) {
52 if(!_childItems.contains(column)) {
53 _childItems[column] = QList<AbstractTreeItem *>();
54 _childHash[column] = QHash<quint64, AbstractTreeItem *>();
57 _childItems[column].append(item);
58 _childHash[column][item->id()] = item;
60 connect(item, SIGNAL(destroyed()), this, SLOT(childDestroyed()));
63 void AbstractTreeItem::appendChild(AbstractTreeItem *child) {
64 appendChild(defaultColumn(), child);
67 void AbstractTreeItem::removeChild(int column, int row) {
68 if(!_childItems.contains(column)
69 || _childItems[column].size() <= row)
72 if(column == defaultColumn())
73 emit beginRemoveChilds(row, row);
75 AbstractTreeItem *treeitem = _childItems[column].value(row);
76 _childItems[column].removeAt(row);
77 _childHash[column].remove(_childHash[column].key(treeitem));
78 disconnect(treeitem, 0, this, 0);
79 treeitem->deleteLater();
81 if(column == defaultColumn())
82 emit endRemoveChilds();
85 void AbstractTreeItem::removeChild(int row) {
86 removeChild(defaultColumn(), row);
89 void AbstractTreeItem::removeChildById(int column, const quint64 &id) {
90 if(!_childHash[column].contains(id))
93 AbstractTreeItem *treeItem = _childHash[column][id];
94 int row = _childItems[column].indexOf(treeItem);
96 removeChild(column, row);
99 void AbstractTreeItem::removeChildById(const quint64 &id) {
100 removeChildById(defaultColumn(), id);
103 void AbstractTreeItem::removeAllChilds() {
104 if(childCount() == 0)
107 emit beginRemoveChilds(0, childCount() - 1);
109 AbstractTreeItem *child;
110 foreach(int column, _childItems.keys()) {
111 QList<AbstractTreeItem *>::iterator iter = _childItems[column].begin();
112 while(iter != _childItems[column].end()) {
114 _childHash[column].remove(_childHash[column].key(child));
115 iter = _childItems[column].erase(iter);
116 disconnect(child, 0, this, 0);
117 child->removeAllChilds();
118 child->deleteLater();
121 emit endRemoveChilds();
124 AbstractTreeItem *AbstractTreeItem::child(int column, int row) const {
125 if(!_childItems.contains(column)
126 || _childItems[column].size() <= row)
129 return _childItems[column].value(row);
132 AbstractTreeItem *AbstractTreeItem::child(int row) const {
133 return child(defaultColumn(), row);
136 AbstractTreeItem *AbstractTreeItem::childById(int column, const quint64 &id) const {
137 if(!_childHash.contains(column)
138 || !_childHash[column].contains(id))
141 return _childHash[column].value(id);
144 AbstractTreeItem *AbstractTreeItem::childById(const quint64 &id) const {
145 return childById(defaultColumn(), id);
148 int AbstractTreeItem::childCount(int column) const {
149 if(!_childItems.contains(column))
152 return _childItems[column].count();
155 int AbstractTreeItem::childCount() const {
156 return childCount(defaultColumn());
159 int AbstractTreeItem::column() const {
163 QHash<int, QList<AbstractTreeItem*> >::const_iterator iter = parent()->_childItems.constBegin();
164 while(iter != parent()->_childItems.constEnd()) {
165 if(iter.value().contains(const_cast<AbstractTreeItem *>(this)))
170 // unable to find us o_O
171 return parent()->defaultColumn();
174 int AbstractTreeItem::row() const {
178 return parent()->_childItems[column()].indexOf(const_cast<AbstractTreeItem*>(this));
181 AbstractTreeItem *AbstractTreeItem::parent() const {
182 return qobject_cast<AbstractTreeItem *>(QObject::parent());
185 Qt::ItemFlags AbstractTreeItem::flags() const {
189 void AbstractTreeItem::setFlags(Qt::ItemFlags flags) {
193 void AbstractTreeItem::childDestroyed() {
194 AbstractTreeItem *item = static_cast<AbstractTreeItem*>(sender());
197 qWarning() << "AbstractTreeItem::childDestroyed() received null pointer!";
201 QHash<int, QList<AbstractTreeItem*> >::const_iterator iter = _childItems.constBegin();
202 int column, row = -1;
203 while(iter != _childItems.constEnd()) {
204 row = iter.value().indexOf(item);
213 qWarning() << "AbstractTreeItem::childDestroyed(): unknown Child died:" << item << "parent:" << this;
217 _childItems[column].removeAt(row);
218 _childHash[column].remove(_childHash[column].key(item));
219 emit beginRemoveChilds(row, row);
220 emit endRemoveChilds();
223 /*****************************************
225 *****************************************/
226 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
227 : AbstractTreeItem(parent),
232 SimpleTreeItem::~SimpleTreeItem() {
235 QVariant SimpleTreeItem::data(int column, int role) const {
236 if(role == Qt::DisplayRole && column < _itemData.count())
237 return _itemData[column];
242 int SimpleTreeItem::columnCount() const {
243 return _itemData.count();
246 /*****************************************
248 *****************************************/
249 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
250 : AbstractTreeItem(parent),
251 _propertyOrder(propertyOrder)
255 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
256 : AbstractTreeItem(parent),
257 _propertyOrder(QStringList())
262 PropertyMapItem::~PropertyMapItem() {
265 QVariant PropertyMapItem::data(int column, int role) const {
266 if(column >= columnCount())
269 if(role != Qt::DisplayRole)
272 return property(_propertyOrder[column].toAscii());
275 int PropertyMapItem::columnCount() const {
276 return _propertyOrder.count();
279 void PropertyMapItem::appendProperty(const QString &property) {
280 _propertyOrder << property;
285 /*****************************************
287 *****************************************/
288 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
289 : QAbstractItemModel(parent)
291 rootItem = new SimpleTreeItem(data, 0);
294 TreeModel::~TreeModel() {
298 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
299 if(!hasIndex(row, column, parent))
300 return QModelIndex();
302 AbstractTreeItem *parentItem;
304 if(!parent.isValid())
305 parentItem = rootItem;
307 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
309 AbstractTreeItem *childItem = parentItem->child(parent.column(), row);
312 return createIndex(row, column, childItem);
314 return QModelIndex();
317 QModelIndex TreeModel::indexById(quint64 id, const QModelIndex &parent) const {
318 AbstractTreeItem *parentItem;
320 if(!parent.isValid())
321 parentItem = rootItem;
323 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
325 AbstractTreeItem *childItem = parentItem->childById(parent.column(), id);
328 return createIndex(childItem->row(), 0, childItem);
330 return QModelIndex();
333 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
335 qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
336 return QModelIndex();
340 return QModelIndex();
342 return createIndex(item->row(), 0, item);
346 QModelIndex TreeModel::parent(const QModelIndex &index) const {
348 return QModelIndex();
350 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
351 AbstractTreeItem *parentItem = static_cast<AbstractTreeItem *>(childItem->parent());
353 if(parentItem == rootItem)
354 return QModelIndex();
356 return createIndex(parentItem->row(), 0, parentItem);
359 int TreeModel::rowCount(const QModelIndex &parent) const {
360 AbstractTreeItem *parentItem;
361 if(!parent.isValid())
362 parentItem = rootItem;
364 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
366 return parentItem->childCount(parent.column());
369 int TreeModel::columnCount(const QModelIndex &parent) const {
371 // since there the Qt Views don't draw more columns than the header has columns
372 // we can be lazy and simply return the count of header columns
373 // actually this gives us more freedom cause we don't have to ensure that a rows parent
374 // has equal or more columns than that row
376 // if(parent.isValid()) {
377 // AbstractTreeItem *child;
378 // if(child = static_cast<AbstractTreeItem *>(parent.internalPointer())->child(parent.column(), parent.row()))
379 // return child->columnCount();
381 // return static_cast<AbstractTreeItem*>(parent.internalPointer())->columnCount();
383 // return rootItem->columnCount();
386 return rootItem->columnCount();
389 QVariant TreeModel::data(const QModelIndex &index, int role) const {
393 AbstractTreeItem *item = static_cast<AbstractTreeItem*>(index.internalPointer());
394 return item->data(index.column(), role);
397 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
398 AbstractTreeItem *item;
402 item = static_cast<AbstractTreeItem *>(index.internalPointer());
403 return item->flags();
406 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
407 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
408 return rootItem->data(section, role);
413 void TreeModel::itemDataChanged(int column) {
414 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
415 QModelIndex leftIndex, rightIndex;
421 leftIndex = createIndex(item->row(), 0, item);
422 rightIndex = createIndex(item->row(), item->columnCount()-1, item);
424 leftIndex = createIndex(item->row(), column, item);
425 rightIndex = leftIndex;
428 emit dataChanged(leftIndex, rightIndex);
431 void TreeModel::appendChild(AbstractTreeItem *parent, AbstractTreeItem *child) {
432 if(parent == 0 || child == 0) {
433 qWarning() << "TreeModel::appendChild(parent, child) parent and child have to be valid pointers!" << parent << child;
437 int nextRow = parent->childCount();
438 beginInsertRows(indexByItem(parent), nextRow, nextRow);
439 parent->appendChild(child);
442 connect(child, SIGNAL(dataChanged(int)),
443 this, SLOT(itemDataChanged(int)));
445 connect(child, SIGNAL(newChild(AbstractTreeItem *)),
446 this, SLOT(newChild(AbstractTreeItem *)));
448 connect(child, SIGNAL(beginRemoveChilds(int, int)),
449 this, SLOT(beginRemoveChilds(int, int)));
451 connect(child, SIGNAL(endRemoveChilds()),
452 this, SLOT(endRemoveChilds()));
455 void TreeModel::newChild(AbstractTreeItem *child) {
456 appendChild(static_cast<AbstractTreeItem *>(sender()), child);
459 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
460 QModelIndex parent = indexByItem(static_cast<AbstractTreeItem *>(sender()));
461 beginRemoveRows(parent, firstRow, lastRow);
464 void TreeModel::endRemoveChilds() {
468 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
469 if(row > rowCount(parent))
472 AbstractTreeItem *item;
473 if(!parent.isValid())
476 item = static_cast<AbstractTreeItem*>(parent.internalPointer());
478 beginRemoveRows(parent, row, row);
479 item->removeChild(parent.column(), row);
484 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
485 // check if there is work to be done
489 // out of range check
490 if(row + count - 1 > rowCount(parent) || row < 0 || count < 0)
493 AbstractTreeItem *item;
494 if(!parent.isValid())
497 item = static_cast<AbstractTreeItem *>(parent.internalPointer());
500 beginRemoveRows(parent, row, row + count - 1);
502 for(int i = row + count - 1; i >= 0; i--) {
503 item->removeChild(parent.column(), i);
509 void TreeModel::clear() {
510 removeRows(0, rowCount());