Reenabled Activity levels
[quassel.git] / src / client / treemodel.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
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) version 3.                                           *
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 #ifndef _TREEMODEL_H_
22 #define _TREEMODEL_H_
23
24 #include <QList>
25 #include <QStringList>
26 #include <QVariant>
27 #include <QHash>
28 #include <QAbstractItemModel>
29
30 #include <QLinkedList> // needed for debug
31
32 /*****************************************
33  *  general item used in the Tree Model
34  *****************************************/
35 class AbstractTreeItem : public QObject {
36   Q_OBJECT
37   Q_PROPERTY(quint64 id READ id)
38
39 public:
40   AbstractTreeItem(AbstractTreeItem *parent = 0);
41   virtual ~AbstractTreeItem();
42
43   bool newChild(int column, AbstractTreeItem *child);
44   bool newChild(AbstractTreeItem *child);
45   
46   bool removeChild(int column, int row);
47   bool removeChild(int row);
48
49   bool removeChildById(int column, const quint64 &id);
50   bool removeChildById(const quint64 &id);
51   
52   void removeAllChilds();
53
54   virtual quint64 id() const;
55
56   AbstractTreeItem *child(int column, int row) const;
57   AbstractTreeItem *child(int row) const;
58   
59   AbstractTreeItem *childById(int column, const quint64 &id) const;
60   AbstractTreeItem *childById(const quint64 &id) const;
61
62   int childCount(int column) const;
63   int childCount() const;
64
65   virtual int columnCount() const = 0;
66
67   virtual QVariant data(int column, int role) const = 0;
68   virtual bool setData(int column, const QVariant &value, int role) = 0;
69
70   virtual Qt::ItemFlags flags() const;
71   virtual void setFlags(Qt::ItemFlags);
72
73   int column() const;
74   int row() const;
75   AbstractTreeItem *parent() const;
76
77 signals:
78   void dataChanged(int column = -1);
79
80   void beginAppendChilds(int column, int firstRow, int lastRow);
81   void endAppendChilds();
82   
83   void beginRemoveChilds(int column, int firstRow, int lastRow);
84   void endRemoveChilds();
85                                        
86 private:
87   QHash<int, QList<AbstractTreeItem *> > _childItems;
88   Qt::ItemFlags _flags;
89
90   int defaultColumn() const;
91 };
92
93
94 /*****************************************
95  * SimpleTreeItem
96  *****************************************/
97 class SimpleTreeItem : public AbstractTreeItem {
98   Q_OBJECT
99
100 public:
101   SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent = 0);
102   virtual ~SimpleTreeItem();
103
104   virtual QVariant data(int column, int role) const;
105   virtual bool setData(int column, const QVariant &value, int role);
106
107   virtual int columnCount() const;
108
109 private:
110   QList<QVariant> _itemData;
111 };
112
113 /*****************************************
114  * PropertyMapItem
115  *****************************************/
116 class PropertyMapItem : public AbstractTreeItem {
117   Q_OBJECT
118
119 public:
120   PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent = 0);
121   PropertyMapItem(AbstractTreeItem *parent = 0);
122
123   virtual ~PropertyMapItem();
124   
125   virtual QVariant data(int column, int role) const;
126   virtual bool setData(int column, const QVariant &value, int role);
127
128   virtual QString toolTip(int column) const { Q_UNUSED(column) return QString(); }
129   virtual int columnCount() const;
130   
131   void appendProperty(const QString &property);
132
133 private:
134   QStringList _propertyOrder;
135 };
136
137
138 /*****************************************
139  * TreeModel
140  *****************************************/
141 class TreeModel : public QAbstractItemModel {
142   Q_OBJECT
143
144 public:
145   TreeModel(const QList<QVariant> &, QObject *parent = 0);
146   virtual ~TreeModel();
147
148   virtual QVariant data(const QModelIndex &index, int role) const;
149   virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
150
151   virtual Qt::ItemFlags flags(const QModelIndex &index) const;
152   QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
153   
154   QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
155   QModelIndex indexById(quint64 id, const QModelIndex &parent = QModelIndex()) const;
156   QModelIndex indexByItem(AbstractTreeItem *item, int column = 0) const;
157
158   QModelIndex parent(const QModelIndex &index) const;
159
160   int rowCount(const QModelIndex &parent = QModelIndex()) const;
161   int columnCount(const QModelIndex &parent = QModelIndex()) const;
162
163   virtual void clear();
164
165 private slots:
166   void itemDataChanged(int column = -1);
167   
168   void beginAppendChilds(int column, int firstRow, int lastRow);
169   void endAppendChilds();
170   
171   void beginRemoveChilds(int column, int firstRow, int lastRow);
172   void endRemoveChilds();
173   
174 protected:
175   AbstractTreeItem *rootItem;
176
177 private:
178   void connectItem(AbstractTreeItem *item);
179   
180   struct ChildStatus {
181     QModelIndex parent;
182     int childCount;
183     int start;
184     int end;
185     inline ChildStatus(QModelIndex parent_, int cc_, int s_, int e_) : parent(parent_), childCount(cc_), start(s_), end(e_) {};
186   };
187   ChildStatus _childStatus;
188   int _aboutToRemoveOrInsert;
189   // QLinkedList<ChildStatus> _childStatus;
190
191 };
192
193 #endif