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