BufferModell::currentChanged() is history. If you need it, file a friendly complaint...
[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 /*****************************************
31  *  general item used in the Tree Model
32  *****************************************/
33 class AbstractTreeItem : public QObject {
34   Q_OBJECT
35   Q_PROPERTY(quint64 id READ id)
36
37 public:
38   AbstractTreeItem(AbstractTreeItem *parent = 0);
39   virtual ~AbstractTreeItem();
40
41   void appendChild(int column, AbstractTreeItem *child);
42   void appendChild(AbstractTreeItem *child);
43   
44   void removeChild(int column, int row);
45   void removeChild(int row);
46
47   void removeChildById(int column, const quint64 &id);
48   void removeChildById(const quint64 &id);
49   
50   void removeAllChilds();
51
52   virtual quint64 id() const;
53
54   AbstractTreeItem *child(int column, int row) const;
55   AbstractTreeItem *child(int row) const;
56   
57   AbstractTreeItem *childById(int column, const quint64 &id) const;
58   AbstractTreeItem *childById(const quint64 &id) const;
59
60   int childCount(int column) const;
61   int childCount() const;
62
63   virtual int columnCount() const = 0;
64
65   virtual QVariant data(int column, int role) const = 0;
66   virtual bool setData(int column, const QVariant &value, int role) = 0;
67
68   virtual Qt::ItemFlags flags() const;
69   virtual void setFlags(Qt::ItemFlags);
70
71   int column() const;
72   int row() const;
73   AbstractTreeItem *parent() const;
74
75 signals:
76   void dataChanged(int column = -1);
77   void newChild(AbstractTreeItem *);
78
79   void beginRemoveChilds(int firstRow, int lastRow);
80   void endRemoveChilds();
81                                        
82 private slots:
83   void childDestroyed();
84
85 private:
86   QHash<int, QList<AbstractTreeItem *> > _childItems;
87   QHash<int, QHash<quint64, AbstractTreeItem *> > _childHash; // uint to be compatible to qHash functions FIXME test this
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 int columnCount() const;
129   
130   void appendProperty(const QString &property);
131
132 private:
133   QStringList _propertyOrder;
134 };
135
136
137 /*****************************************
138  * TreeModel
139  *****************************************/
140 class TreeModel : public QAbstractItemModel {
141   Q_OBJECT
142
143 public:
144   TreeModel(const QList<QVariant> &, QObject *parent = 0);
145   virtual ~TreeModel();
146
147   virtual QVariant data(const QModelIndex &index, int role) const;
148   virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
149
150   virtual Qt::ItemFlags flags(const QModelIndex &index) const;
151   QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
152   
153   QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
154   QModelIndex indexById(quint64 id, const QModelIndex &parent = QModelIndex()) const;
155   QModelIndex indexByItem(AbstractTreeItem *item) const;
156
157   QModelIndex parent(const QModelIndex &index) const;
158
159   int rowCount(const QModelIndex &parent = QModelIndex()) const;
160   int columnCount(const QModelIndex &parent = QModelIndex()) const;
161
162   virtual void clear();
163
164 private slots:
165   void itemDataChanged(int column = -1);
166   void newChild(AbstractTreeItem *child);
167
168   void beginRemoveChilds(int firstRow, int lastRow);
169   void endRemoveChilds();
170   
171 protected:
172   void appendChild(AbstractTreeItem *parent, AbstractTreeItem *child);
173
174   bool removeRow(int row, const QModelIndex &parent = QModelIndex());
175   bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
176   
177   AbstractTreeItem *rootItem;
178 };
179
180 #endif