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