I CAN HAZ A STYLE ENGINES!?
[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 && column < itemData.count())
93     return itemData[column];
94   else
95     return QVariant();
96 }
97
98 Qt::ItemFlags TreeItem::flags() const {
99   // some sane defaults
100   return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
101 }
102
103 /*****************************************
104  * TreeModel
105  *****************************************/
106 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent)
107   : QAbstractItemModel(parent)
108 {
109   rootItem = new TreeItem(data, 0);
110 }
111
112 TreeModel::~TreeModel() {
113   delete rootItem;
114 }
115
116 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
117   if(!hasIndex(row, column, parent))
118     return QModelIndex();
119   
120   TreeItem *parentItem;
121   
122   if(!parent.isValid())
123     parentItem = rootItem;
124   else
125     parentItem = static_cast<TreeItem*>(parent.internalPointer());
126   
127   TreeItem *childItem = parentItem->child(row);
128   if(childItem)
129     return createIndex(row, column, childItem);
130   else
131     return QModelIndex();
132 }
133
134 QModelIndex TreeModel::indexById(uint id, const QModelIndex &parent) const {
135   TreeItem *parentItem; 
136   
137   if(!parent.isValid())
138     parentItem = rootItem;
139   else
140     parentItem = static_cast<TreeItem *>(parent.internalPointer());
141   
142   TreeItem *childItem = parentItem->childById(id);
143   if(childItem)
144     return createIndex(childItem->row(), 0, childItem);
145   else
146     return QModelIndex();
147 }
148
149 QModelIndex TreeModel::parent(const QModelIndex &index) const {
150   if(!index.isValid())
151     return QModelIndex();
152   
153   TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
154   TreeItem *parentItem = childItem->parent();
155   
156   if(parentItem == rootItem)
157     return QModelIndex();
158   
159   return createIndex(parentItem->row(), 0, parentItem);
160 }
161
162 int TreeModel::rowCount(const QModelIndex &parent) const {
163   TreeItem *parentItem;
164   if(parent.column() > 0)
165     return 0;
166   
167   if(!parent.isValid())
168     parentItem = rootItem;
169   else
170     parentItem = static_cast<TreeItem*>(parent.internalPointer());
171   
172   return parentItem->childCount();
173 }
174
175 int TreeModel::columnCount(const QModelIndex &parent) const {
176   if(parent.isValid())
177     return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
178   else
179     return rootItem->columnCount();
180 }
181
182 QVariant TreeModel::data(const QModelIndex &index, int role) const {
183   if(!index.isValid())
184     return QVariant();
185
186   TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
187   return item->data(index.column(), role);
188 }
189
190 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
191   TreeItem *item;
192   if(!index.isValid())
193     item = rootItem;
194   else
195     item = static_cast<TreeItem *>(index.internalPointer());
196   return item->flags();
197 }
198
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);
202   else
203     return QVariant();
204 }
205
206 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
207   if(row > rowCount(parent))
208     return false;
209   
210   TreeItem *item;
211   if(!parent.isValid())
212     item = rootItem;
213   else
214     item = static_cast<TreeItem*>(parent.internalPointer());
215   
216   beginRemoveRows(parent, row, row);
217   item->removeChild(row);
218   endRemoveRows();
219   return true;
220 }
221
222 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
223   // check if there is work to be done
224   if(count == 0)
225     return true;
226
227   // out of range check
228   if(row + count - 1 > rowCount(parent) || row < 0 || count < 0) 
229     return false;
230   
231   TreeItem *item;
232   if(!parent.isValid())
233     item = rootItem;
234   else
235     item = static_cast<TreeItem*>(parent.internalPointer());
236   
237   
238   beginRemoveRows(parent, row, row + count - 1);
239
240   for(int i = row + count - 1; i >= 0; i--) {
241     item->removeChild(i);
242   }
243   endRemoveRows();
244   return true;
245 }
246
247 void TreeModel::clear() {
248   removeRows(0, rowCount());
249 }