a8c8a9940bd1e3497974023fa4b547f16f18078b
[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   void removeAllChilds();
47
48   virtual quint64 id() const;
49
50   AbstractTreeItem *child(int column, int row) const;
51   AbstractTreeItem *child(int row) const;
52   
53   AbstractTreeItem *childById(int column, const quint64 &id) const;
54   AbstractTreeItem *childById(const quint64 &id) const;
55
56   int childCount(int column) const;
57   int childCount() const;
58
59   virtual int columnCount() const = 0;
60
61   virtual QVariant data(int column, int role) const = 0;
62
63   virtual Qt::ItemFlags flags() const;
64   virtual void setFlags(Qt::ItemFlags);
65
66   int column() const;
67   int row() const;
68   AbstractTreeItem *parent() const;
69
70 signals:
71   void dataChanged(int column = -1);
72   void newChild(AbstractTreeItem *);
73   void childRemoved(int row);
74   void childsRemoved(int firstRow, int lastRow);
75
76   void beginRemoveChilds(int firstRow, int lastRow);
77   void endRemoveChilds();
78                                        
79 private slots:
80   void childDestroyed();
81
82 private:
83   QHash<int, QList<AbstractTreeItem *> > _childItems;
84   QHash<int, QHash<quint64, AbstractTreeItem *> > _childHash; // uint to be compatible to qHash functions FIXME test this
85   Qt::ItemFlags _flags;
86
87   int defaultColumn() const;
88 };
89
90
91 /*****************************************
92  * SimpleTreeItem
93  *****************************************/
94 class SimpleTreeItem : public AbstractTreeItem {
95   Q_OBJECT
96
97 public:
98   SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent = 0);
99   virtual ~SimpleTreeItem();
100   virtual QVariant data(int column, int role) const;
101   virtual int columnCount() const;
102
103 private:
104   QList<QVariant> _itemData;
105 };
106
107 /*****************************************
108  * PropertyMapItem
109  *****************************************/
110 class PropertyMapItem : public AbstractTreeItem {
111   Q_OBJECT
112
113 public:
114   PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent = 0);
115   PropertyMapItem(AbstractTreeItem *parent = 0);
116
117   virtual ~PropertyMapItem();
118   
119   virtual QVariant data(int column, int role) const;
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   TreeModel(const QList<QVariant> &, QObject *parent = 0);
137   virtual ~TreeModel();
138
139   QVariant data(const QModelIndex &index, int role) const;
140   virtual Qt::ItemFlags flags(const QModelIndex &index) const;
141   QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
142   
143   QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
144   QModelIndex indexById(quint64 id, const QModelIndex &parent = QModelIndex()) const;
145   QModelIndex indexByItem(AbstractTreeItem *item) const;
146
147   QModelIndex parent(const QModelIndex &index) const;
148
149   int rowCount(const QModelIndex &parent = QModelIndex()) const;
150   int columnCount(const QModelIndex &parent = QModelIndex()) const;
151
152   virtual void clear();
153
154 private slots:
155   void itemDataChanged(int column = -1);
156   void newChild(AbstractTreeItem *child);
157
158   void beginRemoveChilds(int firstRow, int lastRow);
159   void endRemoveChilds();
160   
161   void childRemoved(int row);
162   void childsRemoved(int firstRow, int lastRow);
163
164 protected:
165   void appendChild(AbstractTreeItem *parent, AbstractTreeItem *child);
166
167   bool removeRow(int row, const QModelIndex &parent = QModelIndex());
168   bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
169   
170   AbstractTreeItem *rootItem;
171 };
172
173 #endif