Merging r732:766 from trunk to branches/0.3.
[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(AbstractTreeItem *child);
44   bool newChilds(const QList<AbstractTreeItem *> &items);
45
46   bool removeChild(int row);
47   bool removeChildById(const quint64 &id);
48   void removeAllChilds();
49
50   virtual quint64 id() const;
51
52   AbstractTreeItem *child(int row) const;
53   AbstractTreeItem *childById(const quint64 &id) const;
54
55   int childCount(int column = 0) const;
56
57   virtual int columnCount() const = 0;
58
59   virtual QVariant data(int column, int role) const = 0;
60   virtual bool setData(int column, const QVariant &value, int role) = 0;
61
62   virtual Qt::ItemFlags flags() const;
63   virtual void setFlags(Qt::ItemFlags);
64
65   int row() const;
66   AbstractTreeItem *parent() const;
67
68   void dumpChildList();
69
70 signals:
71   void dataChanged(int column = -1);
72
73   void beginAppendChilds(int firstRow, int lastRow);
74   void endAppendChilds();
75   
76   void beginRemoveChilds(int firstRow, int lastRow);
77   void endRemoveChilds();
78                                        
79 private:
80   QList<AbstractTreeItem *> _childItems;
81   Qt::ItemFlags _flags;
82 };
83
84
85 /*****************************************
86  * SimpleTreeItem
87  *****************************************/
88 class SimpleTreeItem : public AbstractTreeItem {
89   Q_OBJECT
90
91 public:
92   SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent = 0);
93   virtual ~SimpleTreeItem();
94
95   virtual QVariant data(int column, int role) const;
96   virtual bool setData(int column, const QVariant &value, int role);
97
98   virtual int columnCount() const;
99
100 private:
101   QList<QVariant> _itemData;
102 };
103
104 /*****************************************
105  * PropertyMapItem
106  *****************************************/
107 class PropertyMapItem : public AbstractTreeItem {
108   Q_OBJECT
109
110 public:
111   PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent = 0);
112   PropertyMapItem(AbstractTreeItem *parent = 0);
113
114   virtual ~PropertyMapItem();
115   
116   virtual QVariant data(int column, int role) const;
117   virtual bool setData(int column, const QVariant &value, int role);
118
119   virtual QString toolTip(int column) const { Q_UNUSED(column) return QString(); }
120   virtual int columnCount() const;
121   
122   void appendProperty(const QString &property);
123
124 private:
125   QStringList _propertyOrder;
126 };
127
128
129 /*****************************************
130  * TreeModel
131  *****************************************/
132 class TreeModel : public QAbstractItemModel {
133   Q_OBJECT
134
135 public:
136   enum myRoles {
137     SortRole = Qt::UserRole,
138     UserRole
139   };
140
141   TreeModel(const QList<QVariant> &, QObject *parent = 0);
142   virtual ~TreeModel();
143
144   virtual QVariant data(const QModelIndex &index, int role) const;
145   virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
146
147   virtual Qt::ItemFlags flags(const QModelIndex &index) const;
148   QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
149   
150   QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
151   QModelIndex indexById(quint64 id, const QModelIndex &parent = QModelIndex()) const;
152   QModelIndex indexByItem(AbstractTreeItem *item) const;
153
154   QModelIndex parent(const QModelIndex &index) const;
155
156   int rowCount(const QModelIndex &parent = QModelIndex()) const;
157   int columnCount(const QModelIndex &parent = QModelIndex()) const;
158
159   virtual void clear();
160
161 private slots:
162   void itemDataChanged(int column = -1);
163   
164   void beginAppendChilds(int firstRow, int lastRow);
165   void endAppendChilds();
166   
167   void beginRemoveChilds(int firstRow, int lastRow);
168   void endRemoveChilds();
169   
170 protected:
171   AbstractTreeItem *rootItem;
172
173 private:
174   void connectItem(AbstractTreeItem *item);
175   
176   struct ChildStatus {
177     QModelIndex parent;
178     int childCount;
179     int start;
180     int end;
181     inline ChildStatus(QModelIndex parent_, int cc_, int s_, int e_) : parent(parent_), childCount(cc_), start(s_), end(e_) {};
182   };
183   ChildStatus _childStatus;
184   int _aboutToRemoveOrInsert;
185
186 private slots:
187   void debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
188   void debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
189   void debug_rowsInserted(const QModelIndex &parent, int start, int end);
190   void debug_rowsRemoved(const QModelIndex &parent, int start, int end);
191   void debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
192 };
193
194 #endif