52439afec80409d604007c56ee7231bb915fb731
[quassel.git] / src / client / treemodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "global.h"
22 #include "treemodel.h"
23
24 /*****************************************
25  *  Buffer Items stored in the Tree Model
26  *****************************************/
27 TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
28   : QObject(parent),
29     parentItem(parent),
30     itemData(data),
31   _flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
32 {
33 }
34
35 TreeItem::TreeItem(TreeItem *parent) {
36   itemData = QList<QVariant>();
37   parentItem = parent;
38 }
39
40 TreeItem::~TreeItem() {
41   qDeleteAll(childItems);
42 }
43
44 uint TreeItem::id() const {
45   return (uint)this;
46 }
47
48 void TreeItem::appendChild(TreeItem *item) {
49   childItems.append(item);
50   childHash[item->id()] = item;
51 }
52
53 void TreeItem::removeChild(int row) {
54   if(row >= childItems.size())
55     return;
56   TreeItem *treeitem = childItems.value(row);
57   childItems.removeAt(row);
58   childHash.remove(childHash.key(treeitem));
59 }
60
61 TreeItem *TreeItem::child(int row) const {
62   if(row < childItems.size())
63     return childItems.value(row);
64   else
65     return 0;
66 }
67
68 TreeItem *TreeItem::childById(const uint &id) const {
69   if(childHash.contains(id))
70     return childHash.value(id);
71   else
72     return 0;
73 }
74
75 int TreeItem::childCount() const {
76   return childItems.count();
77 }
78
79 int TreeItem::row() const {
80   if(parentItem)
81     return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
82   else
83     return 0;
84 }
85
86 TreeItem *TreeItem::parent() {
87   return parentItem;
88 }
89
90 int TreeItem::columnCount() const {
91   return itemData.count();
92 }
93
94 QVariant TreeItem::data(int column, int role) const {
95   if(role == Qt::DisplayRole && column < itemData.count())
96     return itemData[column];
97   else
98     return QVariant();
99 }
100
101 Qt::ItemFlags TreeItem::flags() const {
102   // some sane defaults
103   return _flags;
104 }
105
106 void TreeItem::setFlags(Qt::ItemFlags flags) {
107   _flags = flags;
108 }
109
110
111 /*****************************************
112  * TreeModel
113  *****************************************/
114 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
115   : QAbstractItemModel(parent)
116 {
117   rootItem = new TreeItem(data, 0);
118 }
119
120 TreeModel::~TreeModel() {
121   delete rootItem;
122 }
123
124 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
125   if(!hasIndex(row, column, parent))
126     return QModelIndex();
127   
128   TreeItem *parentItem;
129   
130   if(!parent.isValid())
131     parentItem = rootItem;
132   else
133     parentItem = static_cast<TreeItem*>(parent.internalPointer());
134   
135   TreeItem *childItem = parentItem->child(row);
136   if(childItem)
137     return createIndex(row, column, childItem);
138   else
139     return QModelIndex();
140 }
141
142 QModelIndex TreeModel::indexById(uint id, const QModelIndex &parent) const {
143   TreeItem *parentItem; 
144   
145   if(!parent.isValid())
146     parentItem = rootItem;
147   else
148     parentItem = static_cast<TreeItem *>(parent.internalPointer());
149   
150   TreeItem *childItem = parentItem->childById(id);
151   if(childItem)
152     return createIndex(childItem->row(), 0, childItem);
153   else
154     return QModelIndex();
155 }
156
157 QModelIndex TreeModel::parent(const QModelIndex &index) const {
158   if(!index.isValid())
159     return QModelIndex();
160   
161   TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
162   TreeItem *parentItem = childItem->parent();
163   
164   if(parentItem == rootItem)
165     return QModelIndex();
166   
167   return createIndex(parentItem->row(), 0, parentItem);
168 }
169
170 int TreeModel::rowCount(const QModelIndex &parent) const {
171   TreeItem *parentItem;
172   if(parent.column() > 0)
173     return 0;
174   
175   if(!parent.isValid())
176     parentItem = rootItem;
177   else
178     parentItem = static_cast<TreeItem*>(parent.internalPointer());
179   
180   return parentItem->childCount();
181 }
182
183 int TreeModel::columnCount(const QModelIndex &parent) const {
184   if(parent.isValid())
185     return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
186   else
187     return rootItem->columnCount();
188 }
189
190 QVariant TreeModel::data(const QModelIndex &index, int role) const {
191   if(!index.isValid())
192     return QVariant();
193
194   TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
195   return item->data(index.column(), role);
196 }
197
198 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
199   TreeItem *item;
200   if(!index.isValid())
201     item = rootItem;
202   else
203     item = static_cast<TreeItem *>(index.internalPointer());
204   return item->flags();
205 }
206
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);
210   else
211     return QVariant();
212 }
213
214 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
215   if(row > rowCount(parent))
216     return false;
217   
218   TreeItem *item;
219   if(!parent.isValid())
220     item = rootItem;
221   else
222     item = static_cast<TreeItem*>(parent.internalPointer());
223   
224   beginRemoveRows(parent, row, row);
225   item->removeChild(row);
226   endRemoveRows();
227   return true;
228 }
229
230 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
231   // check if there is work to be done
232   if(count == 0)
233     return true;
234
235   // out of range check
236   if(row + count - 1 > rowCount(parent) || row < 0 || count < 0) 
237     return false;
238   
239   TreeItem *item;
240   if(!parent.isValid())
241     item = rootItem;
242   else
243     item = static_cast<TreeItem*>(parent.internalPointer());
244   
245   
246   beginRemoveRows(parent, row, row + count - 1);
247
248   for(int i = row + count - 1; i >= 0; i--) {
249     item->removeChild(i);
250   }
251   endRemoveRows();
252   return true;
253 }
254
255 void TreeModel::clear() {
256   removeRows(0, rowCount());
257 }