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 ***************************************************************************/
21 #include "treemodel.h"
23 /*****************************************
24 * Buffer Items stored in the Tree Model
25 *****************************************/
26 TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
30 _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
34 TreeItem::TreeItem(TreeItem *parent)
36 itemData(QList<QVariant>()),
38 _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
42 TreeItem::~TreeItem() {
43 qDeleteAll(_childItems);
46 uint TreeItem::id() const {
50 void TreeItem::appendChild(TreeItem *item) {
51 _childItems.append(item);
52 _childHash[item->id()] = item;
53 connect(item, SIGNAL(destroyed()),
54 this, SLOT(childDestroyed()));
57 void TreeItem::removeChild(int row) {
58 if(row >= _childItems.size())
60 TreeItem *treeitem = _childItems.value(row);
61 _childItems.removeAt(row);
62 _childHash.remove(_childHash.key(treeitem));
65 TreeItem *TreeItem::child(int row) const {
66 if(row < _childItems.size())
67 return _childItems.value(row);
72 TreeItem *TreeItem::childById(const uint &id) const {
73 if(_childHash.contains(id))
74 return _childHash.value(id);
79 int TreeItem::childCount() const {
80 return _childItems.count();
83 int TreeItem::row() const {
85 return _parentItem->_childItems.indexOf(const_cast<TreeItem*>(this));
90 TreeItem *TreeItem::parent() {
94 int TreeItem::columnCount() const {
95 return itemData.count();
98 QVariant TreeItem::data(int column, int role) const {
99 if(role == Qt::DisplayRole && column < itemData.count())
100 return itemData[column];
105 Qt::ItemFlags TreeItem::flags() const {
109 void TreeItem::setFlags(Qt::ItemFlags flags) {
113 void TreeItem::childDestroyed() {
114 TreeItem *item = static_cast<TreeItem*>(sender());
115 removeChild(item->row());
120 /*****************************************
122 *****************************************/
123 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
124 : QAbstractItemModel(parent)
126 rootItem = new TreeItem(data, 0);
129 TreeModel::~TreeModel() {
133 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
134 if(!hasIndex(row, column, parent))
135 return QModelIndex();
137 TreeItem *parentItem;
139 if(!parent.isValid())
140 parentItem = rootItem;
142 parentItem = static_cast<TreeItem*>(parent.internalPointer());
144 TreeItem *childItem = parentItem->child(row);
146 return createIndex(row, column, childItem);
148 return QModelIndex();
151 QModelIndex TreeModel::indexById(uint id, const QModelIndex &parent) const {
152 TreeItem *parentItem;
154 if(!parent.isValid())
155 parentItem = rootItem;
157 parentItem = static_cast<TreeItem *>(parent.internalPointer());
159 TreeItem *childItem = parentItem->childById(id);
161 return createIndex(childItem->row(), 0, childItem);
163 return QModelIndex();
166 QModelIndex TreeModel::parent(const QModelIndex &index) const {
168 return QModelIndex();
170 TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
171 TreeItem *parentItem = static_cast<TreeItem*>(childItem->parent());
173 if(parentItem == rootItem)
174 return QModelIndex();
176 return createIndex(parentItem->row(), 0, parentItem);
179 int TreeModel::rowCount(const QModelIndex &parent) const {
180 TreeItem *parentItem;
181 if(!parent.isValid())
182 parentItem = rootItem;
184 parentItem = static_cast<TreeItem*>(parent.internalPointer());
186 return parentItem->childCount();
189 int TreeModel::columnCount(const QModelIndex &parent) const {
191 return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
193 return rootItem->columnCount();
196 QVariant TreeModel::data(const QModelIndex &index, int role) const {
200 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
201 return item->data(index.column(), role);
204 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
209 item = static_cast<TreeItem *>(index.internalPointer());
210 return item->flags();
213 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
214 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
215 return rootItem->data(section, role);
220 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
221 if(row > rowCount(parent))
225 if(!parent.isValid())
228 item = static_cast<TreeItem*>(parent.internalPointer());
230 beginRemoveRows(parent, row, row);
231 item->removeChild(row);
236 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
237 // check if there is work to be done
241 // out of range check
242 if(row + count - 1 > rowCount(parent) || row < 0 || count < 0)
246 if(!parent.isValid())
249 item = static_cast<TreeItem*>(parent.internalPointer());
252 beginRemoveRows(parent, row, row + count - 1);
254 for(int i = row + count - 1; i >= 0; i--) {
255 item->removeChild(i);
261 void TreeModel::clear() {
262 removeRows(0, rowCount());