made the signalproxy compile with MSVC
[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
67   virtual Qt::ItemFlags flags() const;
68   virtual void setFlags(Qt::ItemFlags);
69
70   int column() const;
71   int row() const;
72   AbstractTreeItem *parent() const;
73
74 signals:
75   void dataChanged(int column = -1);
76   void newChild(AbstractTreeItem *);
77
78   void beginRemoveChilds(int firstRow, int lastRow);
79   void endRemoveChilds();
80                                        
81 private slots:
82   void childDestroyed();
83
84 private:
85   QHash<int, QList<AbstractTreeItem *> > _childItems;
86   QHash<int, QHash<quint64, AbstractTreeItem *> > _childHash; // uint to be compatible to qHash functions FIXME test this
87   Qt::ItemFlags _flags;
88
89   int defaultColumn() const;
90 };
91
92
93 /*****************************************
94  * SimpleTreeItem
95  *****************************************/
96 class SimpleTreeItem : public AbstractTreeItem {
97   Q_OBJECT
98
99 public:
100   SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent = 0);
101   virtual ~SimpleTreeItem();
102   virtual QVariant data(int column, int role) const;
103   virtual int columnCount() const;
104
105 private:
106   QList<QVariant> _itemData;
107 };
108
109 /*****************************************
110  * PropertyMapItem
111  *****************************************/
112 class PropertyMapItem : public AbstractTreeItem {
113   Q_OBJECT
114
115 public:
116   PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent = 0);
117   PropertyMapItem(AbstractTreeItem *parent = 0);
118
119   virtual ~PropertyMapItem();
120   
121   virtual QVariant data(int column, int role) const;
122   virtual int columnCount() const;
123   
124   void appendProperty(const QString &property);
125
126 private:
127   QStringList _propertyOrder;
128 };
129
130
131 /*****************************************
132  * TreeModel
133  *****************************************/
134 class TreeModel : public QAbstractItemModel {
135   Q_OBJECT
136
137 public:
138   TreeModel(const QList<QVariant> &, QObject *parent = 0);
139   virtual ~TreeModel();
140
141   QVariant data(const QModelIndex &index, int role) const;
142   virtual Qt::ItemFlags flags(const QModelIndex &index) const;
143   QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
144   
145   QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
146   QModelIndex indexById(quint64 id, const QModelIndex &parent = QModelIndex()) const;
147   QModelIndex indexByItem(AbstractTreeItem *item) const;
148
149   QModelIndex parent(const QModelIndex &index) const;
150
151   int rowCount(const QModelIndex &parent = QModelIndex()) const;
152   int columnCount(const QModelIndex &parent = QModelIndex()) const;
153
154   virtual void clear();
155
156 private slots:
157   void itemDataChanged(int column = -1);
158   void newChild(AbstractTreeItem *child);
159
160   void beginRemoveChilds(int firstRow, int lastRow);
161   void endRemoveChilds();
162   
163 protected:
164   void appendChild(AbstractTreeItem *parent, AbstractTreeItem *child);
165
166   bool removeRow(int row, const QModelIndex &parent = QModelIndex());
167   bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
168   
169   AbstractTreeItem *rootItem;
170 };
171
172 #endif