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() {
90 emit beginRemoveChilds(0, childCount() - 1);
92 AbstractTreeItem *child;
93 foreach(int key, _childItems.keys()) {
94 QList<AbstractTreeItem *>::iterator iter = _childItems[key].begin();
95 while(iter != _childItems[key].end()) {
97 iter = _childItems[key].erase(iter);
98 disconnect(child, 0, this, 0);
99 child->removeAllChilds();
100 child->deleteLater();
103 emit endRemoveChilds();
106 AbstractTreeItem *AbstractTreeItem::child(int column, int row) const {
107 if(!_childItems.contains(column)
108 || _childItems[column].size() <= row)
111 return _childItems[column].value(row);
114 AbstractTreeItem *AbstractTreeItem::child(int row) const {
115 return child(defaultColumn(), row);
118 AbstractTreeItem *AbstractTreeItem::childById(int column, const quint64 &id) const {
119 if(!_childHash.contains(column)
120 || !_childHash[column].contains(id))
123 return _childHash[column].value(id);
126 AbstractTreeItem *AbstractTreeItem::childById(const quint64 &id) const {
127 return childById(defaultColumn(), id);
130 int AbstractTreeItem::childCount(int column) const {
131 if(!_childItems.contains(column))
134 return _childItems[column].count();
137 int AbstractTreeItem::childCount() const {
138 return childCount(defaultColumn());
141 int AbstractTreeItem::column() const {
145 QHash<int, QList<AbstractTreeItem*> >::const_iterator iter = parent()->_childItems.constBegin();
146 while(iter != parent()->_childItems.constEnd()) {
147 if(iter.value().contains(const_cast<AbstractTreeItem *>(this)))
152 // unable to find us o_O
153 return parent()->defaultColumn();
156 int AbstractTreeItem::row() const {
160 return parent()->_childItems[column()].indexOf(const_cast<AbstractTreeItem*>(this));
163 AbstractTreeItem *AbstractTreeItem::parent() const {
164 return qobject_cast<AbstractTreeItem *>(QObject::parent());
167 Qt::ItemFlags AbstractTreeItem::flags() const {
171 void AbstractTreeItem::setFlags(Qt::ItemFlags flags) {
175 void AbstractTreeItem::childDestroyed() {
176 AbstractTreeItem *item = static_cast<AbstractTreeItem*>(sender());
179 qWarning() << "AbstractTreeItem::childDestroyed() received null pointer!";
183 QHash<int, QList<AbstractTreeItem*> >::const_iterator iter = _childItems.constBegin();
184 int column, row = -1;
185 while(iter != _childItems.constEnd()) {
186 row = iter.value().indexOf(item);
195 qWarning() << "AbstractTreeItem::childDestroyed(): unknown Child died:" << item << "parent:" << this;
199 _childItems[column].removeAt(row);
200 _childHash[column].remove(_childHash[column].key(item));
201 emit beginRemoveChilds(row, row);
202 emit endRemoveChilds();
205 /*****************************************
207 *****************************************/
208 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
209 : AbstractTreeItem(parent),
214 SimpleTreeItem::~SimpleTreeItem() {
217 QVariant SimpleTreeItem::data(int column, int role) const {
218 if(role == Qt::DisplayRole && column < _itemData.count())
219 return _itemData[column];
224 int SimpleTreeItem::columnCount() const {
225 return _itemData.count();
228 /*****************************************
230 *****************************************/
231 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
232 : AbstractTreeItem(parent),
233 _propertyOrder(propertyOrder)
237 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
238 : AbstractTreeItem(parent),
239 _propertyOrder(QStringList())
244 PropertyMapItem::~PropertyMapItem() {
247 QVariant PropertyMapItem::data(int column, int role) const {
248 if(column >= columnCount())
251 if(role != Qt::DisplayRole)
254 return property(_propertyOrder[column].toAscii());
257 int PropertyMapItem::columnCount() const {
258 return _propertyOrder.count();
261 void PropertyMapItem::appendProperty(const QString &property) {
262 _propertyOrder << property;
267 /*****************************************
269 *****************************************/
270 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
271 : QAbstractItemModel(parent)
273 rootItem = new SimpleTreeItem(data, 0);
276 TreeModel::~TreeModel() {
280 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
281 if(!hasIndex(row, column, parent))
282 return QModelIndex();
284 AbstractTreeItem *parentItem;
286 if(!parent.isValid())
287 parentItem = rootItem;
289 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
291 AbstractTreeItem *childItem = parentItem->child(parent.column(), row);
294 return createIndex(row, column, childItem);
296 return QModelIndex();
299 QModelIndex TreeModel::indexById(quint64 id, const QModelIndex &parent) const {
300 AbstractTreeItem *parentItem;
302 if(!parent.isValid())
303 parentItem = rootItem;
305 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
307 AbstractTreeItem *childItem = parentItem->childById(parent.column(), id);
310 return createIndex(childItem->row(), 0, childItem);
312 return QModelIndex();
315 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
317 qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
318 return QModelIndex();
322 return QModelIndex();
324 return createIndex(item->row(), 0, item);
328 QModelIndex TreeModel::parent(const QModelIndex &index) const {
330 return QModelIndex();
332 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
333 AbstractTreeItem *parentItem = static_cast<AbstractTreeItem *>(childItem->parent());
335 if(parentItem == rootItem)
336 return QModelIndex();
338 return createIndex(parentItem->row(), 0, parentItem);
341 int TreeModel::rowCount(const QModelIndex &parent) const {
342 AbstractTreeItem *parentItem;
343 if(!parent.isValid())
344 parentItem = rootItem;
346 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
348 return parentItem->childCount(parent.column());
351 int TreeModel::columnCount(const QModelIndex &parent) const {
353 // since there the Qt Views don't draw more columns than the header has columns
354 // we can be lazy and simply return the count of header columns
355 // actually this gives us more freedom cause we don't have to ensure that a rows parent
356 // has equal or more columns than that row
358 // if(parent.isValid()) {
359 // AbstractTreeItem *child;
360 // if(child = static_cast<AbstractTreeItem *>(parent.internalPointer())->child(parent.column(), parent.row()))
361 // return child->columnCount();
363 // return static_cast<AbstractTreeItem*>(parent.internalPointer())->columnCount();
365 // return rootItem->columnCount();
368 return rootItem->columnCount();
371 QVariant TreeModel::data(const QModelIndex &index, int role) const {
375 AbstractTreeItem *item = static_cast<AbstractTreeItem*>(index.internalPointer());
376 return item->data(index.column(), role);
379 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
380 AbstractTreeItem *item;
384 item = static_cast<AbstractTreeItem *>(index.internalPointer());
385 return item->flags();
388 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
389 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
390 return rootItem->data(section, role);
395 void TreeModel::itemDataChanged(int column) {
396 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
397 QModelIndex leftIndex, rightIndex;
403 leftIndex = createIndex(item->row(), 0, item);
404 rightIndex = createIndex(item->row(), item->columnCount()-1, item);
406 leftIndex = createIndex(item->row(), column, item);
407 rightIndex = leftIndex;
410 emit dataChanged(leftIndex, rightIndex);
413 void TreeModel::appendChild(AbstractTreeItem *parent, AbstractTreeItem *child) {
414 if(parent == 0 or child == 0) {
415 qWarning() << "TreeModel::appendChild(parent, child) parent and child have to be valid pointers!" << parent << child;
419 int nextRow = parent->childCount();
420 beginInsertRows(indexByItem(parent), nextRow, nextRow);
421 parent->appendChild(child);
424 connect(child, SIGNAL(dataChanged(int)),
425 this, SLOT(itemDataChanged(int)));
427 connect(child, SIGNAL(newChild(AbstractTreeItem *)),
428 this, SLOT(newChild(AbstractTreeItem *)));
430 // connect(child, SIGNAL(childRemoved(int)),
431 // this, SLOT(childRemoved(int)));
433 connect(child, SIGNAL(beginRemoveChilds(int, int)),
434 this, SLOT(beginRemoveChilds(int, int)));
436 connect(child, SIGNAL(endRemoveChilds()),
437 this, SLOT(endRemoveChilds()));
440 void TreeModel::newChild(AbstractTreeItem *child) {
441 appendChild(static_cast<AbstractTreeItem *>(sender()), child);
444 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
445 QModelIndex parent = indexByItem(static_cast<AbstractTreeItem *>(sender()));
446 beginRemoveRows(parent, firstRow, lastRow);
449 void TreeModel::endRemoveChilds() {
453 void TreeModel::childRemoved(int row) {
454 QModelIndex parent = indexByItem(static_cast<AbstractTreeItem *>(sender()));
455 beginRemoveRows(parent, row, row);
459 void TreeModel::childsRemoved(int firstRow, int lastRow) {
460 QModelIndex parent = indexByItem(static_cast<AbstractTreeItem *>(sender()));
461 beginRemoveRows(parent, firstRow, lastRow);
466 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
467 if(row > rowCount(parent))
470 AbstractTreeItem *item;
471 if(!parent.isValid())
474 item = static_cast<AbstractTreeItem*>(parent.internalPointer());
476 beginRemoveRows(parent, row, row);
477 item->removeChild(parent.column(), row);
482 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
483 // check if there is work to be done
487 // out of range check
488 if(row + count - 1 > rowCount(parent) || row < 0 || count < 0)
491 AbstractTreeItem *item;
492 if(!parent.isValid())
495 item = static_cast<AbstractTreeItem *>(parent.internalPointer());
498 beginRemoveRows(parent, row, row + count - 1);
500 for(int i = row + count - 1; i >= 0; i--) {
501 item->removeChild(parent.column(), i);
507 void TreeModel::clear() {
508 removeRows(0, rowCount());