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 void TreeItem::appendChild(TreeItem *item) {
42 childItems.append(item);
45 void TreeItem::removeChild(int row) {
46 childItems.removeAt(row);
49 TreeItem *TreeItem::child(int row) {
50 return childItems.value(row);
53 int TreeItem::childCount() const {
54 return childItems.count();
57 int TreeItem::row() const {
59 return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
64 TreeItem *TreeItem::parent() {
68 int TreeItem::columnCount() const {
69 return itemData.count();
72 QVariant TreeItem::data(int column, int role) const {
73 if(role == Qt::DisplayRole and column < itemData.count())
74 return itemData[column];
80 /*****************************************
82 *****************************************/
83 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent) : QAbstractItemModel(parent) {
84 rootItem = new TreeItem(data, 0);
87 TreeModel::~TreeModel() {
91 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
92 if(!hasIndex(row, column, parent))
98 parentItem = rootItem;
100 parentItem = static_cast<TreeItem*>(parent.internalPointer());
102 TreeItem *childItem = parentItem->child(row);
104 return createIndex(row, column, childItem);
106 return QModelIndex();
109 QModelIndex TreeModel::parent(const QModelIndex &index) const {
111 return QModelIndex();
113 TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
114 TreeItem *parentItem = childItem->parent();
116 if(parentItem == rootItem)
117 return QModelIndex();
119 return createIndex(parentItem->row(), 0, parentItem);
122 int TreeModel::rowCount(const QModelIndex &parent) const {
123 TreeItem *parentItem;
124 if(parent.column() > 0)
127 if(!parent.isValid())
128 parentItem = rootItem;
130 parentItem = static_cast<TreeItem*>(parent.internalPointer());
132 return parentItem->childCount();
135 int TreeModel::columnCount(const QModelIndex &parent) const {
137 return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
139 return rootItem->columnCount();
142 QVariant TreeModel::data(const QModelIndex &index, int role) const {
146 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
147 return item->data(index.column(), role);
150 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
154 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
157 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
158 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
159 return rootItem->data(section, role);
164 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
165 if(row > rowCount(parent))
169 if(!parent.isValid())
172 item = static_cast<TreeItem*>(parent.internalPointer());
174 beginRemoveRows(parent, row, row);
175 item->removeChild(row);
180 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
181 // check if there is work to be done
185 // out of range check
186 if(row + count - 1 > rowCount(parent) || row < 0 || count < 0)
190 if(!parent.isValid())
193 item = static_cast<TreeItem*>(parent.internalPointer());
196 beginRemoveRows(parent, row, row + count - 1);
197 for(int i = row + count - 1; i >= 0; i--) {
198 item->removeChild(i);
204 void TreeModel::clear() {
205 removeRows(0, rowCount());