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) : QObject(parent) {
32 TreeItem::TreeItem(TreeItem *parent) {
33 itemData = QList<QVariant>();
37 TreeItem::~TreeItem() {
38 qDeleteAll(childItems);
41 uint TreeItem::id() const {
45 void TreeItem::appendChild(TreeItem *item) {
46 childItems.append(item);
47 childHash[item->id()] = item;
50 void TreeItem::removeChild(int row) {
51 if(row >= childItems.size())
53 TreeItem *treeitem = childItems.value(row);
54 childItems.removeAt(row);
55 childHash.remove(childHash.key(treeitem));
58 TreeItem *TreeItem::child(int row) const {
59 if(row < childItems.size())
60 return childItems.value(row);
65 TreeItem *TreeItem::childById(const uint &id) const {
66 if(childHash.contains(id))
67 return childHash.value(id);
72 int TreeItem::childCount() const {
73 return childItems.count();
76 int TreeItem::row() const {
78 return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
83 TreeItem *TreeItem::parent() {
87 int TreeItem::columnCount() const {
88 return itemData.count();
91 QVariant TreeItem::data(int column, int role) const {
92 if(role == Qt::DisplayRole and column < itemData.count())
93 return itemData[column];
99 /*****************************************
101 *****************************************/
102 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent) : QAbstractItemModel(parent) {
103 rootItem = new TreeItem(data, 0);
106 TreeModel::~TreeModel() {
110 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
111 if(!hasIndex(row, column, parent))
112 return QModelIndex();
114 TreeItem *parentItem;
116 if(!parent.isValid())
117 parentItem = rootItem;
119 parentItem = static_cast<TreeItem*>(parent.internalPointer());
121 TreeItem *childItem = parentItem->child(row);
123 return createIndex(row, column, childItem);
125 return QModelIndex();
128 QModelIndex TreeModel::indexById(uint id, const QModelIndex &parent) const {
129 TreeItem *parentItem;
131 if(!parent.isValid())
132 parentItem = rootItem;
134 parentItem = static_cast<TreeItem *>(parent.internalPointer());
136 TreeItem *childItem = parentItem->childById(id);
138 return createIndex(childItem->row(), 0, childItem);
140 return QModelIndex();
143 QModelIndex TreeModel::parent(const QModelIndex &index) const {
145 return QModelIndex();
147 TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
148 TreeItem *parentItem = childItem->parent();
150 if(parentItem == rootItem)
151 return QModelIndex();
153 return createIndex(parentItem->row(), 0, parentItem);
156 int TreeModel::rowCount(const QModelIndex &parent) const {
157 TreeItem *parentItem;
158 if(parent.column() > 0)
161 if(!parent.isValid())
162 parentItem = rootItem;
164 parentItem = static_cast<TreeItem*>(parent.internalPointer());
166 return parentItem->childCount();
169 int TreeModel::columnCount(const QModelIndex &parent) const {
171 return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
173 return rootItem->columnCount();
176 QVariant TreeModel::data(const QModelIndex &index, int role) const {
180 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
181 return item->data(index.column(), role);
184 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
188 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
191 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
192 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
193 return rootItem->data(section, role);
198 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
199 if(row > rowCount(parent))
203 if(!parent.isValid())
206 item = static_cast<TreeItem*>(parent.internalPointer());
208 beginRemoveRows(parent, row, row);
209 item->removeChild(row);
214 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
215 // check if there is work to be done
219 // out of range check
220 if(row + count - 1 > rowCount(parent) || row < 0 || count < 0)
224 if(!parent.isValid())
227 item = static_cast<TreeItem*>(parent.internalPointer());
230 beginRemoveRows(parent, row, row + count - 1);
232 for(int i = row + count - 1; i >= 0; i--) {
233 item->removeChild(i);
239 void TreeModel::clear() {
240 removeRows(0, rowCount());