Fixed disconnect from Core. Dis- and reconnecting should now work as expected.
[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 void TreeItem::appendChild(TreeItem *item) {
42   childItems.append(item);
43 }
44
45 void TreeItem::removeChild(int row) {
46   childItems.removeAt(row);
47 }
48
49 TreeItem *TreeItem::child(int row) {
50   return childItems.value(row);
51 }
52
53 int TreeItem::childCount() const {
54   return childItems.count();
55 }
56
57 int TreeItem::row() const {
58   if(parentItem)
59     return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
60   else
61     return 0;
62 }
63
64 TreeItem *TreeItem::parent() {
65   return parentItem;
66 }
67
68 int TreeItem::columnCount() const {
69   return itemData.count();
70 }
71
72 QVariant TreeItem::data(int column, int role) const {
73   if(role == Qt::DisplayRole and column < itemData.count())
74     return itemData[column];
75   else
76     return QVariant();
77 }
78
79
80 /*****************************************
81  * TreeModel
82  *****************************************/
83 TreeModel::TreeModel(const QList<QVariant> &data, QObject *parent) : QAbstractItemModel(parent) {
84   rootItem = new TreeItem(data, 0);
85 }
86
87 TreeModel::~TreeModel() {
88   delete rootItem;
89 }
90
91 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const {
92   if(!hasIndex(row, column, parent))
93     return QModelIndex();
94   
95   TreeItem *parentItem;
96   
97   if(!parent.isValid())
98     parentItem = rootItem;
99   else
100     parentItem = static_cast<TreeItem*>(parent.internalPointer());
101   
102   TreeItem *childItem = parentItem->child(row);
103   if(childItem)
104     return createIndex(row, column, childItem);
105   else
106     return QModelIndex();
107 }
108
109 QModelIndex TreeModel::parent(const QModelIndex &index) const {
110   if(!index.isValid())
111     return QModelIndex();
112   
113   TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
114   TreeItem *parentItem = childItem->parent();
115   
116   if(parentItem == rootItem)
117     return QModelIndex();
118   
119   return createIndex(parentItem->row(), 0, parentItem);
120 }
121
122 int TreeModel::rowCount(const QModelIndex &parent) const {
123   TreeItem *parentItem;
124   if(parent.column() > 0)
125     return 0;
126   
127   if(!parent.isValid())
128     parentItem = rootItem;
129   else
130     parentItem = static_cast<TreeItem*>(parent.internalPointer());
131   
132   return parentItem->childCount();
133 }
134
135 int TreeModel::columnCount(const QModelIndex &parent) const {
136   if(parent.isValid())
137     return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
138   else
139     return rootItem->columnCount();
140 }
141
142 QVariant TreeModel::data(const QModelIndex &index, int role) const {
143   if(!index.isValid())
144     return QVariant();
145
146   TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
147   return item->data(index.column(), role);
148 }
149
150 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
151   if(!index.isValid())
152     return 0;
153   else
154     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
155 }
156
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);
160   else
161     return QVariant();
162 }
163
164 bool TreeModel::removeRow(int row, const QModelIndex &parent) {
165   if(row > rowCount(parent))
166     return false;
167   
168   TreeItem *item;
169   if(!parent.isValid())
170     item = rootItem;
171   else
172     item = static_cast<TreeItem*>(parent.internalPointer());
173   
174   beginRemoveRows(parent, row, row);
175   item->removeChild(row);
176   endRemoveRows();
177   return true;
178 }
179
180 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
181   // check if there is work to be done
182   if(count == 0)
183     return true;
184
185   // out of range check
186   if(row + count - 1 > rowCount(parent) || row < 0 || count < 0) 
187     return false;
188   
189   TreeItem *item;
190   if(!parent.isValid())
191     item = rootItem;
192   else
193     item = static_cast<TreeItem*>(parent.internalPointer());
194   
195   
196   beginRemoveRows(parent, row, row + count - 1);
197
198   for(int i = row + count - 1; i >= 0; i--) {
199     item->removeChild(i);
200   }
201   endRemoveRows();
202   return true;
203 }
204
205 void TreeModel::clear() {
206   removeRows(0, rowCount());
207 }