1 /***************************************************************************
2 * Copyright (C) 2005-07 by the Quassel IRC Team *
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)
31 _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
35 AbstractTreeItem::~AbstractTreeItem() {
36 foreach(int key, _childItems.keys()) {
37 qDeleteAll(_childItems[key]);
41 quint64 AbstractTreeItem::id() const {
45 int AbstractTreeItem::defaultColumn() const {
46 // invalid QModelIndexes aka rootNodes get their Childs stuffed into column -1
54 void AbstractTreeItem::appendChild(int column, AbstractTreeItem *item) {
55 if(!_childItems.contains(column)) {
56 _childItems[column] = QList<AbstractTreeItem *>();
57 _childHash[column] = QHash<quint64, AbstractTreeItem *>();
60 _childItems[column].append(item);
61 _childHash[column][item->id()] = item;
63 connect(item, SIGNAL(destroyed()), this, SLOT(childDestroyed()));
66 void AbstractTreeItem::appendChild(AbstractTreeItem *child) {
67 appendChild(defaultColumn(), child);
70 void AbstractTreeItem::removeChild(int column, int row) {
71 if(!_childItems.contains(column)
72 || _childItems[column].size() <= row)
75 AbstractTreeItem *treeitem = _childItems[column].value(row);
76 _childItems[column].removeAt(row);
77 _childHash[column].remove(_childHash[column].key(treeitem));
78 treeitem->deleteLater();
81 void AbstractTreeItem::removeChild(int row) {
82 removeChild(defaultColumn(), row);
85 AbstractTreeItem *AbstractTreeItem::child(int column, int row) const {
86 if(!_childItems.contains(column)
87 || _childItems[column].size() <= row)
90 return _childItems[column].value(row);
93 AbstractTreeItem *AbstractTreeItem::child(int row) const {
94 return child(defaultColumn(), row);
97 AbstractTreeItem *AbstractTreeItem::childById(int column, const uint &id) const {
98 if(!_childHash.contains(column)
99 || !_childHash[column].contains(id))
102 return _childHash[column].value(id);
105 AbstractTreeItem *AbstractTreeItem::childById(const uint &id) const {
106 return childById(defaultColumn(), id);
109 int AbstractTreeItem::childCount(int column) const {
110 if(!_childItems.contains(column))
113 return _childItems[column].count();
116 int AbstractTreeItem::childCount() const {
117 return childCount(defaultColumn());
120 int AbstractTreeItem::column() const {
124 QHash<int, QList<AbstractTreeItem*> >::const_iterator iter = _parentItem->_childItems.constBegin();
126 while(iter != _parentItem->_childItems.constEnd()) {
127 pos = iter.value().indexOf(const_cast<AbstractTreeItem *>(this));
133 // unable to find us o_O
134 return _parentItem->defaultColumn();
137 int AbstractTreeItem::row() const {
141 return _parentItem->_childItems[column()].indexOf(const_cast<AbstractTreeItem*>(this));
144 AbstractTreeItem *AbstractTreeItem::parent() {
148 Qt::ItemFlags AbstractTreeItem::flags() const {
152 void AbstractTreeItem::setFlags(Qt::ItemFlags flags) {
156 void AbstractTreeItem::childDestroyed() {
157 AbstractTreeItem *item = static_cast<AbstractTreeItem*>(sender());
160 qWarning() << "AbstractTreeItem::childDestroyed() received null pointer!";
163 _childItems[item->column()].removeAt(item->row());
164 _childHash[item->column()].remove(_childHash[item->column()].key(item));
167 /*****************************************
169 *****************************************/
170 SimpleTreeItem::SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent)
171 : AbstractTreeItem(parent),
176 SimpleTreeItem::~SimpleTreeItem() {
179 QVariant SimpleTreeItem::data(int column, int role) const {
180 if(role == Qt::DisplayRole && column < _itemData.count())
181 return _itemData[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())
213 if(role != Qt::DisplayRole)
216 return property(_propertyOrder[column].toAscii());
219 int PropertyMapItem::columnCount() const {
220 return _propertyOrder.count();
223 void PropertyMapItem::appendProperty(const QString &property) {
224 _propertyOrder << property;
229 /*****************************************
231 *****************************************/
232 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
233 : QAbstractItemModel(parent)
235 rootItem = new SimpleTreeItem(data, 0);
238 TreeModel::~TreeModel() {
242 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
243 if(!hasIndex(row, column, parent))
244 return QModelIndex();
246 AbstractTreeItem *parentItem;
248 if(!parent.isValid())
249 parentItem = rootItem;
251 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
253 AbstractTreeItem *childItem = parentItem->child(parent.column(), row);
256 return createIndex(row, column, childItem);
258 return QModelIndex();
261 QModelIndex TreeModel::indexById(uint id, const QModelIndex &parent) const {
262 AbstractTreeItem *parentItem;
264 if(!parent.isValid())
265 parentItem = rootItem;
267 parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
269 AbstractTreeItem *childItem = parentItem->childById(parent.column(), id);
272 return createIndex(childItem->row(), 0, childItem);
274 return QModelIndex();
277 QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
279 qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
280 return QModelIndex();
284 return QModelIndex();
286 return createIndex(item->row(), 0, item);
290 QModelIndex TreeModel::parent(const QModelIndex &index) const {
292 return QModelIndex();
294 AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
295 AbstractTreeItem *parentItem = static_cast<AbstractTreeItem *>(childItem->parent());
297 if(parentItem == rootItem)
298 return QModelIndex();
300 return createIndex(parentItem->row(), 0, parentItem);
303 int TreeModel::rowCount(const QModelIndex &parent) const {
304 AbstractTreeItem *parentItem;
305 if(!parent.isValid())
306 parentItem = rootItem;
308 parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
310 return parentItem->childCount(parent.column());
313 int TreeModel::columnCount(const QModelIndex &parent) const {
315 return static_cast<AbstractTreeItem*>(parent.internalPointer())->columnCount();
317 return rootItem->columnCount();
320 QVariant TreeModel::data(const QModelIndex &index, int role) const {
324 AbstractTreeItem *item = static_cast<AbstractTreeItem*>(index.internalPointer());
325 return item->data(index.column(), role);
328 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
329 AbstractTreeItem *item;
333 item = static_cast<AbstractTreeItem *>(index.internalPointer());
334 return item->flags();
337 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
338 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
339 return rootItem->data(section, role);
344 void TreeModel::itemDataChanged(int column) {
345 AbstractTreeItem *item = qobject_cast<AbstractTreeItem *>(sender());
346 QModelIndex itemIndex;
349 itemIndex = QModelIndex();
351 itemIndex = createIndex(item->row(), column, item);
353 emit dataChanged(itemIndex, itemIndex);
356 void TreeModel::appendChild(AbstractTreeItem *parent, AbstractTreeItem *child) {
357 if(parent == 0 or child == 0) {
358 qWarning() << "TreeModel::apendChild(parent, child) parent and child have to be valid pointers!" << parent << child;
362 int nextRow = parent->childCount();
363 beginInsertRows(indexByItem(parent), nextRow, nextRow);
364 parent->appendChild(child);
367 connect(child, SIGNAL(dataChanged(int)),
368 this, SLOT(itemDataChanged(int)));
372 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
373 if(row > rowCount(parent))
376 AbstractTreeItem *item;
377 if(!parent.isValid())
380 item = static_cast<AbstractTreeItem*>(parent.internalPointer());
382 beginRemoveRows(parent, row, row);
383 item->removeChild(parent.column(), row);
388 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
389 // check if there is work to be done
393 // out of range check
394 if(row + count - 1 > rowCount(parent) || row < 0 || count < 0)
397 AbstractTreeItem *item;
398 if(!parent.isValid())
401 item = static_cast<AbstractTreeItem *>(parent.internalPointer());
404 beginRemoveRows(parent, row, row + count - 1);
406 for(int i = row + count - 1; i >= 0; i--) {
407 item->removeChild(parent.column(), i);
413 void TreeModel::clear() {
414 removeRows(0, rowCount());