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];
98 Qt::ItemFlags TreeItem::flags() const {
100 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
103 /*****************************************
105 *****************************************/
106 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
107 : QAbstractItemModel(parent)
109 rootItem = new TreeItem(data, 0);
112 TreeModel::~TreeModel() {
116 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
117 if(!hasIndex(row, column, parent))
118 return QModelIndex();
120 TreeItem *parentItem;
122 if(!parent.isValid())
123 parentItem = rootItem;
125 parentItem = static_cast<TreeItem*>(parent.internalPointer());
127 TreeItem *childItem = parentItem->child(row);
129 return createIndex(row, column, childItem);
131 return QModelIndex();
134 QModelIndex TreeModel::indexById(uint id, const QModelIndex &parent) const {
135 TreeItem *parentItem;
137 if(!parent.isValid())
138 parentItem = rootItem;
140 parentItem = static_cast<TreeItem *>(parent.internalPointer());
142 TreeItem *childItem = parentItem->childById(id);
144 return createIndex(childItem->row(), 0, childItem);
146 return QModelIndex();
149 QModelIndex TreeModel::parent(const QModelIndex &index) const {
151 return QModelIndex();
153 TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
154 TreeItem *parentItem = childItem->parent();
156 if(parentItem == rootItem)
157 return QModelIndex();
159 return createIndex(parentItem->row(), 0, parentItem);
162 int TreeModel::rowCount(const QModelIndex &parent) const {
163 TreeItem *parentItem;
164 if(parent.column() > 0)
167 if(!parent.isValid())
168 parentItem = rootItem;
170 parentItem = static_cast<TreeItem*>(parent.internalPointer());
172 return parentItem->childCount();
175 int TreeModel::columnCount(const QModelIndex &parent) const {
177 return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
179 return rootItem->columnCount();
182 QVariant TreeModel::data(const QModelIndex &index, int role) const {
186 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
187 return item->data(index.column(), role);
190 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
195 item = static_cast<TreeItem *>(index.internalPointer());
196 return item->flags();
199 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
200 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
201 return rootItem->data(section, role);
206 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
207 if(row > rowCount(parent))
211 if(!parent.isValid())
214 item = static_cast<TreeItem*>(parent.internalPointer());
216 beginRemoveRows(parent, row, row);
217 item->removeChild(row);
222 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
223 // check if there is work to be done
227 // out of range check
228 if(row + count - 1 > rowCount(parent) || row < 0 || count < 0)
232 if(!parent.isValid())
235 item = static_cast<TreeItem*>(parent.internalPointer());
238 beginRemoveRows(parent, row, row + count - 1);
240 for(int i = row + count - 1; i >= 0; i--) {
241 item->removeChild(i);
247 void TreeModel::clear() {
248 removeRows(0, rowCount());