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