1 /***************************************************************************
2 * Copyright (C) 2005-07 by The Quassel 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) any later version. *
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 ***************************************************************************/
22 #include "treemodel.h"
24 /*****************************************
25 * Buffer Items stored in the Tree Model
26 *****************************************/
27 TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
31 _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
35 TreeItem::TreeItem(TreeItem *parent)
37 itemData(QList<QVariant>()),
39 _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
43 TreeItem::~TreeItem() {
44 qDeleteAll(_childItems);
47 uint TreeItem::id() const {
51 void TreeItem::appendChild(TreeItem *item) {
52 _childItems.append(item);
53 _childHash[item->id()] = item;
54 connect(item, SIGNAL(destroyed()),
55 this, SLOT(childDestroyed()));
58 void TreeItem::removeChild(int row) {
59 if(row >= _childItems.size())
61 TreeItem *treeitem = _childItems.value(row);
62 _childItems.removeAt(row);
63 _childHash.remove(_childHash.key(treeitem));
66 TreeItem *TreeItem::child(int row) const {
67 if(row < _childItems.size())
68 return _childItems.value(row);
73 TreeItem *TreeItem::childById(const uint &id) const {
74 if(_childHash.contains(id))
75 return _childHash.value(id);
80 int TreeItem::childCount() const {
81 return _childItems.count();
84 int TreeItem::row() const {
86 return _parentItem->_childItems.indexOf(const_cast<TreeItem*>(this));
91 TreeItem *TreeItem::parent() {
95 int TreeItem::columnCount() const {
96 return itemData.count();
99 QVariant TreeItem::data(int column, int role) const {
100 if(role == Qt::DisplayRole && column < itemData.count())
101 return itemData[column];
106 Qt::ItemFlags TreeItem::flags() const {
110 void TreeItem::setFlags(Qt::ItemFlags flags) {
114 void TreeItem::childDestroyed() {
115 TreeItem *item = static_cast<TreeItem*>(sender());
116 removeChild(item->row());
121 /*****************************************
123 *****************************************/
124 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
125 : QAbstractItemModel(parent)
127 rootItem = new TreeItem(data, 0);
130 TreeModel::~TreeModel() {
134 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
135 if(!hasIndex(row, column, parent))
136 return QModelIndex();
138 TreeItem *parentItem;
140 if(!parent.isValid())
141 parentItem = rootItem;
143 parentItem = static_cast<TreeItem*>(parent.internalPointer());
145 TreeItem *childItem = parentItem->child(row);
147 return createIndex(row, column, childItem);
149 return QModelIndex();
152 QModelIndex TreeModel::indexById(uint id, const QModelIndex &parent) const {
153 TreeItem *parentItem;
155 if(!parent.isValid())
156 parentItem = rootItem;
158 parentItem = static_cast<TreeItem *>(parent.internalPointer());
160 TreeItem *childItem = parentItem->childById(id);
162 return createIndex(childItem->row(), 0, childItem);
164 return QModelIndex();
167 QModelIndex TreeModel::parent(const QModelIndex &index) const {
169 return QModelIndex();
171 TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
172 TreeItem *parentItem = static_cast<TreeItem*>(childItem->parent());
174 if(parentItem == rootItem)
175 return QModelIndex();
177 return createIndex(parentItem->row(), 0, parentItem);
180 int TreeModel::rowCount(const QModelIndex &parent) const {
181 TreeItem *parentItem;
182 if(!parent.isValid())
183 parentItem = rootItem;
185 parentItem = static_cast<TreeItem*>(parent.internalPointer());
187 return parentItem->childCount();
190 int TreeModel::columnCount(const QModelIndex &parent) const {
192 return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
194 return rootItem->columnCount();
197 QVariant TreeModel::data(const QModelIndex &index, int role) const {
201 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
202 return item->data(index.column(), role);
205 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
210 item = static_cast<TreeItem *>(index.internalPointer());
211 return item->flags();
214 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
215 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
216 return rootItem->data(section, role);
221 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
222 if(row > rowCount(parent))
226 if(!parent.isValid())
229 item = static_cast<TreeItem*>(parent.internalPointer());
231 beginRemoveRows(parent, row, row);
232 item->removeChild(row);
237 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
238 // check if there is work to be done
242 // out of range check
243 if(row + count - 1 > rowCount(parent) || row < 0 || count < 0)
247 if(!parent.isValid())
250 item = static_cast<TreeItem*>(parent.internalPointer());
253 beginRemoveRows(parent, row, row + count - 1);
255 for(int i = row + count - 1; i >= 0; i--) {
256 item->removeChild(i);
262 void TreeModel::clear() {
263 removeRows(0, rowCount());