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::removeAllChilds() {
93 emit beginRemoveChilds(0, childCount() - 1);
95 AbstractTreeItem *child;
96 foreach(int key, _childItems.keys()) {
97 QList<AbstractTreeItem *>::iterator iter = _childItems[key].begin();
98 while(iter != _childItems[key].end()) {
100 iter = _childItems[key].erase(iter);
101 disconnect(child, 0, this, 0);
102 child->removeAllChilds();
103 child->deleteLater();
106 emit endRemoveChilds();
109 AbstractTreeItem *AbstractTreeItem::child(int column, int row) const {
110 if(!_childItems.contains(column)
111 || _childItems[column].size() <= row)
114 return _childItems[column].value(row);
117 AbstractTreeItem *AbstractTreeItem::child(int row) const {
118 return child(defaultColumn(), row);
121 AbstractTreeItem *AbstractTreeItem::childById(int column, const quint64 &id) const {
122 if(!_childHash.contains(column)
123 || !_childHash[column].contains(id))
126 return _childHash[column].value(id);
129 AbstractTreeItem *AbstractTreeItem::childById(const quint64 &id) const {
130 return childById(defaultColumn(), id);
133 int AbstractTreeItem::childCount(int column) const {
134 if(!_childItems.contains(column))
137 return _childItems[column].count();
140 int AbstractTreeItem::childCount() const {
141 return childCount(defaultColumn());
144 int AbstractTreeItem::column() const {
148 QHash<int, QList<AbstractTreeItem*> >::const_iterator iter = parent()->_childItems.constBegin();
149 while(iter != parent()->_childItems.constEnd()) {
150 if(iter.value().contains(const_cast<AbstractTreeItem *>(this)))
155 // unable to find us o_O
156 return parent()->defaultColumn();
159 int AbstractTreeItem::row() const {
163 return parent()->_childItems[column()].indexOf(const_cast<AbstractTreeItem*>(this));
166 AbstractTreeItem *AbstractTreeItem::parent() const {
167 return qobject_cast<AbstractTreeItem *>(QObject::parent());
170 Qt::ItemFlags AbstractTreeItem::flags() const {
174 void AbstractTreeItem::setFlags(Qt::ItemFlags flags) {
178 void AbstractTreeItem::childDestroyed() {
179 AbstractTreeItem *item = static_cast<AbstractTreeItem*>(sender());
182 qWarning() << "AbstractTreeItem::childDestroyed() received null pointer!";
186 QHash<int, QList<AbstractTreeItem*> >::const_iterator iter = _childItems.constBegin();
187 int column, row = -1;
188 while(iter != _childItems.constEnd()) {
189 row = iter.value().indexOf(item);
198 qWarning() << "AbstractTreeItem::childDestroyed(): unknown Child died:" << item << "parent:" << this;
202 _childItems[column].removeAt(row);
203 _childHash[column].remove(_childHash[column].key(item));
204 emit beginRemoveChilds(row, row);
205 emit endRemoveChilds();
208 /*****************************************
210 *****************************************/
211 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
212 : AbstractTreeItem(parent),
217 SimpleTreeItem::~SimpleTreeItem() {
220 QVariant SimpleTreeItem::data(int column, int role) const {
221 if(role == Qt::DisplayRole && column < _itemData.count())
222 return _itemData[column];
227 int SimpleTreeItem::columnCount() const {
228 return _itemData.count();
231 /*****************************************
233 *****************************************/
234 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
235 : AbstractTreeItem(parent),
236 _propertyOrder(propertyOrder)
240 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
241 : AbstractTreeItem(parent),
242 _propertyOrder(QStringList())
247 PropertyMapItem::~PropertyMapItem() {
250 QVariant PropertyMapItem::data(int column, int role) const {
251 if(column >= columnCount())
254 if(role != Qt::DisplayRole)
257 return property(_propertyOrder[column].toAscii());
260 int PropertyMapItem::columnCount() const {
261 return _propertyOrder.count();
264 void PropertyMapItem::appendProperty(const QString &property) {
265 _propertyOrder << property;
270 /*****************************************
272 *****************************************/
273 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
274 : QAbstractItemModel(parent)
276 rootItem = new SimpleTreeItem(data, 0);
279 TreeModel::~TreeModel() {
283 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
284 if(!hasIndex(row, column, parent))
285 return QModelIndex();
287 AbstractTreeItem *parentItem;
289 if(!parent.isValid())
290 parentItem = rootItem;
292 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
294 AbstractTreeItem *childItem = parentItem->child(parent.column(), row);
297 return createIndex(row, column, childItem);
299 return QModelIndex();
302 QModelIndex TreeModel::indexById(quint64 id, const QModelIndex &parent) const {
303 AbstractTreeItem *parentItem;
305 if(!parent.isValid())
306 parentItem = rootItem;
308 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
310 AbstractTreeItem *childItem = parentItem->childById(parent.column(), id);
313 return createIndex(childItem->row(), 0, childItem);
315 return QModelIndex();
318 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
320 qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
321 return QModelIndex();
325 return QModelIndex();
327 return createIndex(item->row(), 0, item);
331 QModelIndex TreeModel::parent(const QModelIndex &index) const {
333 return QModelIndex();
335 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
336 AbstractTreeItem *parentItem = static_cast<AbstractTreeItem *>(childItem->parent());
338 if(parentItem == rootItem)
339 return QModelIndex();
341 return createIndex(parentItem->row(), 0, parentItem);
344 int TreeModel::rowCount(const QModelIndex &parent) const {
345 AbstractTreeItem *parentItem;
346 if(!parent.isValid())
347 parentItem = rootItem;
349 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
351 return parentItem->childCount(parent.column());
354 int TreeModel::columnCount(const QModelIndex &parent) const {
356 // since there the Qt Views don't draw more columns than the header has columns
357 // we can be lazy and simply return the count of header columns
358 // actually this gives us more freedom cause we don't have to ensure that a rows parent
359 // has equal or more columns than that row
361 // if(parent.isValid()) {
362 // AbstractTreeItem *child;
363 // if(child = static_cast<AbstractTreeItem *>(parent.internalPointer())->child(parent.column(), parent.row()))
364 // return child->columnCount();
366 // return static_cast<AbstractTreeItem*>(parent.internalPointer())->columnCount();
368 // return rootItem->columnCount();
371 return rootItem->columnCount();
374 QVariant TreeModel::data(const QModelIndex &index, int role) const {
378 AbstractTreeItem *item = static_cast<AbstractTreeItem*>(index.internalPointer());
379 return item->data(index.column(), role);
382 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
383 AbstractTreeItem *item;
387 item = static_cast<AbstractTreeItem *>(index.internalPointer());
388 return item->flags();
391 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
392 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
393 return rootItem->data(section, role);
398 void TreeModel::itemDataChanged(int column) {
399 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
400 QModelIndex leftIndex, rightIndex;
406 leftIndex = createIndex(item->row(), 0, item);
407 rightIndex = createIndex(item->row(), item->columnCount()-1, item);
409 leftIndex = createIndex(item->row(), column, item);
410 rightIndex = leftIndex;
413 emit dataChanged(leftIndex, rightIndex);
416 void TreeModel::appendChild(AbstractTreeItem *parent, AbstractTreeItem *child) {
417 if(parent == 0 or child == 0) {
418 qWarning() << "TreeModel::appendChild(parent, child) parent and child have to be valid pointers!" << parent << child;
422 int nextRow = parent->childCount();
423 beginInsertRows(indexByItem(parent), nextRow, nextRow);
424 parent->appendChild(child);
427 connect(child, SIGNAL(dataChanged(int)),
428 this, SLOT(itemDataChanged(int)));
430 connect(child, SIGNAL(newChild(AbstractTreeItem *)),
431 this, SLOT(newChild(AbstractTreeItem *)));
433 // connect(child, SIGNAL(childRemoved(int)),
434 // this, SLOT(childRemoved(int)));
436 connect(child, SIGNAL(beginRemoveChilds(int, int)),
437 this, SLOT(beginRemoveChilds(int, int)));
439 connect(child, SIGNAL(endRemoveChilds()),
440 this, SLOT(endRemoveChilds()));
443 void TreeModel::newChild(AbstractTreeItem *child) {
444 appendChild(static_cast<AbstractTreeItem *>(sender()), child);
447 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
448 QModelIndex parent = indexByItem(static_cast<AbstractTreeItem *>(sender()));
449 beginRemoveRows(parent, firstRow, lastRow);
452 void TreeModel::endRemoveChilds() {
456 void TreeModel::childRemoved(int row) {
457 QModelIndex parent = indexByItem(static_cast<AbstractTreeItem *>(sender()));
458 beginRemoveRows(parent, row, row);
462 void TreeModel::childsRemoved(int firstRow, int lastRow) {
463 QModelIndex parent = indexByItem(static_cast<AbstractTreeItem *>(sender()));
464 beginRemoveRows(parent, firstRow, lastRow);
469 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
470 if(row > rowCount(parent))
473 AbstractTreeItem *item;
474 if(!parent.isValid())
477 item = static_cast<AbstractTreeItem*>(parent.internalPointer());
479 beginRemoveRows(parent, row, row);
480 item->removeChild(parent.column(), row);
485 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
486 // check if there is work to be done
490 // out of range check
491 if(row + count - 1 > rowCount(parent) || row < 0 || count < 0)
494 AbstractTreeItem *item;
495 if(!parent.isValid())
498 item = static_cast<AbstractTreeItem *>(parent.internalPointer());
501 beginRemoveRows(parent, row, row + count - 1);
503 for(int i = row + count - 1; i >= 0; i--) {
504 item->removeChild(parent.column(), i);
510 void TreeModel::clear() {
511 removeRows(0, rowCount());