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);
108 AbstractTreeItem *child;
109 foreach(int column, _childItems.keys()) {
110 QList<AbstractTreeItem *>::iterator iter = _childItems[column].begin();
111 while(iter != _childItems[column].end()) {
113 _childHash[column].remove(_childHash[column].key(child));
114 iter = _childItems[column].erase(iter);
115 disconnect(child, 0, this, 0);
116 child->removeAllChilds();
117 child->deleteLater();
120 emit endRemoveChilds();
123 AbstractTreeItem *AbstractTreeItem::child(int column, int row) const {
124 if(!_childItems.contains(column)
125 || _childItems[column].size() <= row)
128 return _childItems[column].value(row);
131 AbstractTreeItem *AbstractTreeItem::child(int row) const {
132 return child(defaultColumn(), row);
135 AbstractTreeItem *AbstractTreeItem::childById(int column, const quint64 &id) const {
136 if(!_childHash.contains(column)
137 || !_childHash[column].contains(id))
140 return _childHash[column].value(id);
143 AbstractTreeItem *AbstractTreeItem::childById(const quint64 &id) const {
144 return childById(defaultColumn(), id);
147 int AbstractTreeItem::childCount(int column) const {
148 if(!_childItems.contains(column))
151 return _childItems[column].count();
154 int AbstractTreeItem::childCount() const {
155 return childCount(defaultColumn());
158 int AbstractTreeItem::column() const {
162 QHash<int, QList<AbstractTreeItem*> >::const_iterator iter = parent()->_childItems.constBegin();
163 while(iter != parent()->_childItems.constEnd()) {
164 if(iter.value().contains(const_cast<AbstractTreeItem *>(this)))
169 // unable to find us o_O
170 return parent()->defaultColumn();
173 int AbstractTreeItem::row() const {
177 return parent()->_childItems[column()].indexOf(const_cast<AbstractTreeItem*>(this));
180 AbstractTreeItem *AbstractTreeItem::parent() const {
181 return qobject_cast<AbstractTreeItem *>(QObject::parent());
184 Qt::ItemFlags AbstractTreeItem::flags() const {
188 void AbstractTreeItem::setFlags(Qt::ItemFlags flags) {
192 void AbstractTreeItem::childDestroyed() {
193 AbstractTreeItem *item = static_cast<AbstractTreeItem*>(sender());
196 qWarning() << "AbstractTreeItem::childDestroyed() received null pointer!";
200 QHash<int, QList<AbstractTreeItem*> >::const_iterator iter = _childItems.constBegin();
201 int column, row = -1;
202 while(iter != _childItems.constEnd()) {
203 row = iter.value().indexOf(item);
212 qWarning() << "AbstractTreeItem::childDestroyed(): unknown Child died:" << item << "parent:" << this;
216 _childItems[column].removeAt(row);
217 _childHash[column].remove(_childHash[column].key(item));
218 emit beginRemoveChilds(row, row);
219 emit endRemoveChilds();
222 /*****************************************
224 *****************************************/
225 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
226 : AbstractTreeItem(parent),
231 SimpleTreeItem::~SimpleTreeItem() {
234 QVariant SimpleTreeItem::data(int column, int role) const {
235 if(column >= columnCount() || role != Qt::DisplayRole)
238 return _itemData[column];
241 bool SimpleTreeItem::setData(int column, const QVariant &value, int role) {
242 if(column > columnCount() || role != Qt::DisplayRole)
245 if(column == columnCount())
246 _itemData.append(value);
248 _itemData[column] = value;
253 int SimpleTreeItem::columnCount() const {
254 return _itemData.count();
257 /*****************************************
259 *****************************************/
260 PropertyMapItem::PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent)
261 : AbstractTreeItem(parent),
262 _propertyOrder(propertyOrder)
266 PropertyMapItem::PropertyMapItem(AbstractTreeItem *parent)
267 : AbstractTreeItem(parent),
268 _propertyOrder(QStringList())
273 PropertyMapItem::~PropertyMapItem() {
276 QVariant PropertyMapItem::data(int column, int role) const {
277 if(column >= columnCount() || role != Qt::DisplayRole)
280 return property(_propertyOrder[column].toAscii());
283 bool PropertyMapItem::setData(int column, const QVariant &value, int role) {
284 if(column >= columnCount() || role != Qt::DisplayRole)
287 return setProperty(_propertyOrder[column].toAscii(), value);
290 int PropertyMapItem::columnCount() const {
291 return _propertyOrder.count();
294 void PropertyMapItem::appendProperty(const QString &property) {
295 _propertyOrder << property;
300 /*****************************************
302 *****************************************/
303 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
304 : QAbstractItemModel(parent)
306 rootItem = new SimpleTreeItem(data, 0);
308 connect(rootItem, SIGNAL(dataChanged(int)),
309 this, SLOT(itemDataChanged(int)));
311 connect(rootItem, SIGNAL(newChild(AbstractTreeItem *)),
312 this, SLOT(newChild(AbstractTreeItem *)));
314 connect(rootItem, SIGNAL(beginRemoveChilds(int, int)),
315 this, SLOT(beginRemoveChilds(int, int)));
317 connect(rootItem, SIGNAL(endRemoveChilds()),
318 this, SLOT(endRemoveChilds()));
322 TreeModel::~TreeModel() {
326 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
327 if(!hasIndex(row, column, parent))
328 return QModelIndex();
330 AbstractTreeItem *parentItem;
332 if(!parent.isValid())
333 parentItem = rootItem;
335 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
337 AbstractTreeItem *childItem = parentItem->child(parent.column(), row);
340 return createIndex(row, column, childItem);
342 return QModelIndex();
345 QModelIndex TreeModel::indexById(quint64 id, const QModelIndex &parent) const {
346 AbstractTreeItem *parentItem;
348 if(!parent.isValid())
349 parentItem = rootItem;
351 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
353 AbstractTreeItem *childItem = parentItem->childById(parent.column(), id);
356 return createIndex(childItem->row(), 0, childItem);
358 return QModelIndex();
361 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
363 qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
364 return QModelIndex();
368 return QModelIndex();
370 return createIndex(item->row(), 0, item);
374 QModelIndex TreeModel::parent(const QModelIndex &index) const {
376 return QModelIndex();
378 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
379 AbstractTreeItem *parentItem = static_cast<AbstractTreeItem *>(childItem->parent());
381 if(parentItem == rootItem)
382 return QModelIndex();
384 return createIndex(parentItem->row(), 0, parentItem);
387 int TreeModel::rowCount(const QModelIndex &parent) const {
388 AbstractTreeItem *parentItem;
389 if(!parent.isValid())
390 parentItem = rootItem;
392 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
394 return parentItem->childCount(parent.column());
397 int TreeModel::columnCount(const QModelIndex &parent) const {
399 // since there the Qt Views don't draw more columns than the header has columns
400 // we can be lazy and simply return the count of header columns
401 // actually this gives us more freedom cause we don't have to ensure that a rows parent
402 // has equal or more columns than that row
404 // if(parent.isValid()) {
405 // AbstractTreeItem *child;
406 // if(child = static_cast<AbstractTreeItem *>(parent.internalPointer())->child(parent.column(), parent.row()))
407 // return child->columnCount();
409 // return static_cast<AbstractTreeItem*>(parent.internalPointer())->columnCount();
411 // return rootItem->columnCount();
414 return rootItem->columnCount();
417 QVariant TreeModel::data(const QModelIndex &index, int role) const {
421 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
422 return item->data(index.column(), role);
425 bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
429 AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
430 return item->setData(index.column(), value, role);
433 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
434 AbstractTreeItem *item;
438 item = static_cast<AbstractTreeItem *>(index.internalPointer());
439 return item->flags();
442 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
443 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
444 return rootItem->data(section, role);
449 void TreeModel::itemDataChanged(int column) {
450 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
451 QModelIndex leftIndex, rightIndex;
457 leftIndex = createIndex(item->row(), 0, item);
458 rightIndex = createIndex(item->row(), item->columnCount()-1, item);
460 leftIndex = createIndex(item->row(), column, item);
461 rightIndex = leftIndex;
464 emit dataChanged(leftIndex, rightIndex);
467 void TreeModel::appendChild(AbstractTreeItem *parent, AbstractTreeItem *child) {
468 if(parent == 0 || child == 0) {
469 qWarning() << "TreeModel::appendChild(parent, child) parent and child have to be valid pointers!" << parent << child;
473 int nextRow = parent->childCount();
474 beginInsertRows(indexByItem(parent), nextRow, nextRow);
475 parent->appendChild(child);
478 connect(child, SIGNAL(dataChanged(int)),
479 this, SLOT(itemDataChanged(int)));
481 connect(child, SIGNAL(newChild(AbstractTreeItem *)),
482 this, SLOT(newChild(AbstractTreeItem *)));
484 connect(child, SIGNAL(beginRemoveChilds(int, int)),
485 this, SLOT(beginRemoveChilds(int, int)));
487 connect(child, SIGNAL(endRemoveChilds()),
488 this, SLOT(endRemoveChilds()));
491 void TreeModel::newChild(AbstractTreeItem *child) {
492 appendChild(static_cast<AbstractTreeItem *>(sender()), child);
495 void TreeModel::beginRemoveChilds(int firstRow, int lastRow) {
496 QModelIndex parent = indexByItem(static_cast<AbstractTreeItem *>(sender()));
497 beginRemoveRows(parent, firstRow, lastRow);
500 void TreeModel::endRemoveChilds() {
504 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
505 if(row > rowCount(parent))
508 AbstractTreeItem *item;
509 if(!parent.isValid())
512 item = static_cast<AbstractTreeItem*>(parent.internalPointer());
514 beginRemoveRows(parent, row, row);
515 item->removeChild(parent.column(), row);
520 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
521 // check if there is work to be done
525 // out of range check
526 if(row + count - 1 > rowCount(parent) || row < 0 || count < 0)
529 AbstractTreeItem *item;
530 if(!parent.isValid())
533 item = static_cast<AbstractTreeItem *>(parent.internalPointer());
536 beginRemoveRows(parent, row, row + count - 1);
538 for(int i = row + count - 1; i >= 0; i--) {
539 item->removeChild(parent.column(), i);
545 void TreeModel::clear() {
546 rootItem->removeAllChilds();