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) {
36 itemData = QList<QVariant>();
40 TreeItem::~TreeItem() {
41 qDeleteAll(childItems);
44 uint TreeItem::id() const {
48 void TreeItem::appendChild(TreeItem *item) {
49 childItems.append(item);
50 childHash[item->id()] = item;
53 void TreeItem::removeChild(int row) {
54 if(row >= childItems.size())
56 TreeItem *treeitem = childItems.value(row);
57 childItems.removeAt(row);
58 childHash.remove(childHash.key(treeitem));
61 TreeItem *TreeItem::child(int row) const {
62 if(row < childItems.size())
63 return childItems.value(row);
68 TreeItem *TreeItem::childById(const uint &id) const {
69 if(childHash.contains(id))
70 return childHash.value(id);
75 int TreeItem::childCount() const {
76 return childItems.count();
79 int TreeItem::row() const {
81 return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
86 TreeItem *TreeItem::parent() {
90 int TreeItem::columnCount() const {
91 return itemData.count();
94 QVariant TreeItem::data(int column, int role) const {
95 if(role == Qt::DisplayRole && column < itemData.count())
96 return itemData[column];
101 Qt::ItemFlags TreeItem::flags() const {
102 // some sane defaults
106 void TreeItem::setFlags(Qt::ItemFlags flags) {
111 /*****************************************
113 *****************************************/
114 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
115 : QAbstractItemModel(parent)
117 rootItem = new TreeItem(data, 0);
120 TreeModel::~TreeModel() {
124 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
125 if(!hasIndex(row, column, parent))
126 return QModelIndex();
128 TreeItem *parentItem;
130 if(!parent.isValid())
131 parentItem = rootItem;
133 parentItem = static_cast<TreeItem*>(parent.internalPointer());
135 TreeItem *childItem = parentItem->child(row);
137 return createIndex(row, column, childItem);
139 return QModelIndex();
142 QModelIndex TreeModel::indexById(uint id, const QModelIndex &parent) const {
143 TreeItem *parentItem;
145 if(!parent.isValid())
146 parentItem = rootItem;
148 parentItem = static_cast<TreeItem *>(parent.internalPointer());
150 TreeItem *childItem = parentItem->childById(id);
152 return createIndex(childItem->row(), 0, childItem);
154 return QModelIndex();
157 QModelIndex TreeModel::parent(const QModelIndex &index) const {
159 return QModelIndex();
161 TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
162 TreeItem *parentItem = childItem->parent();
164 if(parentItem == rootItem)
165 return QModelIndex();
167 return createIndex(parentItem->row(), 0, parentItem);
170 int TreeModel::rowCount(const QModelIndex &parent) const {
171 TreeItem *parentItem;
172 if(parent.column() > 0)
175 if(!parent.isValid())
176 parentItem = rootItem;
178 parentItem = static_cast<TreeItem*>(parent.internalPointer());
180 return parentItem->childCount();
183 int TreeModel::columnCount(const QModelIndex &parent) const {
185 return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
187 return rootItem->columnCount();
190 QVariant TreeModel::data(const QModelIndex &index, int role) const {
194 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
195 return item->data(index.column(), role);
198 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
203 item = static_cast<TreeItem *>(index.internalPointer());
204 return item->flags();
207 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
208 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
209 return rootItem->data(section, role);
214 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
215 if(row > rowCount(parent))
219 if(!parent.isValid())
222 item = static_cast<TreeItem*>(parent.internalPointer());
224 beginRemoveRows(parent, row, row);
225 item->removeChild(row);
230 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
231 // check if there is work to be done
235 // out of range check
236 if(row + count - 1 > rowCount(parent) || row < 0 || count < 0)
240 if(!parent.isValid())
243 item = static_cast<TreeItem*>(parent.internalPointer());
246 beginRemoveRows(parent, row, row + count - 1);
248 for(int i = row + count - 1; i >= 0; i--) {
249 item->removeChild(i);
255 void TreeModel::clear() {
256 removeRows(0, rowCount());