Fixing the Bug where a clear of the BufferTreeModel would not result in cleard hashlists.
[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) : QObject(parent) {
28   itemData = data;
29   parentItem = parent;
30 }
31
32 TreeItem::TreeItem(TreeItem *parent) {
33   itemData = QList<QVariant>();
34   parentItem = parent;
35 }
36
37 TreeItem::~TreeItem() {
38   qDeleteAll(childItems);
39 }
40
41 uint TreeItem::id() const {
42   return (uint)this;
43 }
44
45 void TreeItem::appendChild(TreeItem *item) {
46   childItems.append(item);
47   childHash[item->id()] = item;
48 }
49
50 void TreeItem::removeChild(int row) {
51   if(row >= childItems.size())
52     return;
53   TreeItem *treeitem = childItems.value(row);
54   childItems.removeAt(row);
55   childHash.remove(childHash.key(treeitem));
56 }
57
58 TreeItem *TreeItem::child(int row) const {
59   if(row < childItems.size())
60     return childItems.value(row);
61   else
62     return 0;
63 }
64
65 TreeItem *TreeItem::childById(const uint &id) const {
66   if(childHash.contains(id))
67     return childHash.value(id);
68   else
69     return 0;
70 }
71
72 int TreeItem::childCount() const {
73   return childItems.count();
74 }
75
76 int TreeItem::row() const {
77   if(parentItem)
78     return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
79   else
80     return 0;
81 }
82
83 TreeItem *TreeItem::parent() {
84   return parentItem;
85 }
86
87 int TreeItem::columnCount() const {
88   return itemData.count();
89 }
90
91 QVariant TreeItem::data(int column, int role) const {
92   if(role == Qt::DisplayRole and column < itemData.count())
93     return itemData[column];
94   else
95     return QVariant();
96 }
97
98
99 /*****************************************
100  * TreeModel
101  *****************************************/
102 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent) : QAbstractItemModel(parent) {
103   rootItem = new TreeItem(data, 0);
104 }
105
106 TreeModel::~TreeModel() {
107   delete rootItem;
108 }
109
110 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
111   if(!hasIndex(row, column, parent))
112     return QModelIndex();
113   
114   TreeItem *parentItem;
115   
116   if(!parent.isValid())
117     parentItem = rootItem;
118   else
119     parentItem = static_cast<TreeItem*>(parent.internalPointer());
120   
121   TreeItem *childItem = parentItem->child(row);
122   if(childItem)
123     return createIndex(row, column, childItem);
124   else
125     return QModelIndex();
126 }
127
128 QModelIndex TreeModel::indexById(uint id, const QModelIndex &parent) const {
129   TreeItem *parentItem; 
130   
131   if(!parent.isValid())
132     parentItem = rootItem;
133   else
134     parentItem = static_cast<TreeItem *>(parent.internalPointer());
135   
136   TreeItem *childItem = parentItem->childById(id);
137   if(childItem)
138     return createIndex(childItem->row(), 0, childItem);
139   else
140     return QModelIndex();
141 }
142
143 QModelIndex TreeModel::parent(const QModelIndex &index) const {
144   if(!index.isValid())
145     return QModelIndex();
146   
147   TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
148   TreeItem *parentItem = childItem->parent();
149   
150   if(parentItem == rootItem)
151     return QModelIndex();
152   
153   return createIndex(parentItem->row(), 0, parentItem);
154 }
155
156 int TreeModel::rowCount(const QModelIndex &parent) const {
157   TreeItem *parentItem;
158   if(parent.column() > 0)
159     return 0;
160   
161   if(!parent.isValid())
162     parentItem = rootItem;
163   else
164     parentItem = static_cast<TreeItem*>(parent.internalPointer());
165   
166   return parentItem->childCount();
167 }
168
169 int TreeModel::columnCount(const QModelIndex &parent) const {
170   if(parent.isValid())
171     return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
172   else
173     return rootItem->columnCount();
174 }
175
176 QVariant TreeModel::data(const QModelIndex &index, int role) const {
177   if(!index.isValid())
178     return QVariant();
179
180   TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
181   return item->data(index.column(), role);
182 }
183
184 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
185   if(!index.isValid())
186     return 0;
187   else
188     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
189 }
190
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);
194   else
195     return QVariant();
196 }
197
198 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
199   if(row > rowCount(parent))
200     return false;
201   
202   TreeItem *item;
203   if(!parent.isValid())
204     item = rootItem;
205   else
206     item = static_cast<TreeItem*>(parent.internalPointer());
207   
208   beginRemoveRows(parent, row, row);
209   item->removeChild(row);
210   endRemoveRows();
211   return true;
212 }
213
214 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
215   // check if there is work to be done
216   if(count == 0)
217     return true;
218
219   // out of range check
220   if(row + count - 1 > rowCount(parent) || row < 0 || count < 0) 
221     return false;
222   
223   TreeItem *item;
224   if(!parent.isValid())
225     item = rootItem;
226   else
227     item = static_cast<TreeItem*>(parent.internalPointer());
228   
229   
230   beginRemoveRows(parent, row, row + count - 1);
231
232   for(int i = row + count - 1; i >= 0; i--) {
233     item->removeChild(i);
234   }
235   endRemoveRows();
236   return true;
237 }
238
239 void TreeModel::clear() {
240   removeRows(0, rowCount());
241 }