351438a16ad9c502dc70ccf7a4a6e20abea79d61
[quassel.git] / src / client / treemodel.h
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
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(uint 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   virtual quint64 id() const;
48
49   AbstractTreeItem *child(int column, int row) const;
50   AbstractTreeItem *child(int row) const;
51   
52   AbstractTreeItem *childById(int column, const uint &id) const;
53   AbstractTreeItem *childById(const uint &id) const;
54
55   int childCount(int column) const;
56   int childCount() const;
57
58   virtual int columnCount() const = 0;
59
60   virtual QVariant data(int column, int role) const = 0;
61
62   virtual Qt::ItemFlags flags() const;
63   virtual void setFlags(Qt::ItemFlags);
64
65   int column() const;
66   int row() const;
67   AbstractTreeItem *parent();
68
69 signals:
70   void dataChanged(int column);
71   void newChild(AbstractTreeItem *);
72   void childDestroyed(int row);
73                                        
74 private slots:
75   void childDestroyed();
76
77 private:
78   QHash<int, QList<AbstractTreeItem *> > _childItems;
79   QHash<int, QHash<quint64, AbstractTreeItem *> > _childHash; // uint to be compatible to qHash functions
80   AbstractTreeItem *_parentItem;
81   Qt::ItemFlags _flags;
82
83   int defaultColumn() const;
84 };
85
86
87 /*****************************************
88  * SimpleTreeItem
89  *****************************************/
90 class SimpleTreeItem : public AbstractTreeItem {
91   Q_OBJECT
92
93 public:
94   SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent = 0);
95   virtual ~SimpleTreeItem();
96   virtual QVariant data(int column, int role) const;
97   virtual int columnCount() const;
98
99 private:
100   QList<QVariant> _itemData;
101 };
102
103 /*****************************************
104  * PropertyMapItem
105  *****************************************/
106 class PropertyMapItem : public AbstractTreeItem {
107   Q_OBJECT
108
109 public:
110   PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent = 0);
111   PropertyMapItem(AbstractTreeItem *parent = 0);
112
113   virtual ~PropertyMapItem();
114   
115   virtual QVariant data(int column, int role) const;
116   virtual int columnCount() const;
117   
118   void appendProperty(const QString &property);
119
120 private:
121   QStringList _propertyOrder;
122 };
123
124
125 /*****************************************
126  * TreeModel
127  *****************************************/
128 class TreeModel : public QAbstractItemModel {
129   Q_OBJECT
130
131 public:
132   TreeModel(const QList<QVariant> &, QObject *parent = 0);
133   virtual ~TreeModel();
134
135   QVariant data(const QModelIndex &index, int role) const;
136   virtual Qt::ItemFlags flags(const QModelIndex &index) const;
137   QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
138   
139   QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
140   QModelIndex indexById(uint id, const QModelIndex &parent = QModelIndex()) const;
141   QModelIndex indexByItem(AbstractTreeItem *item) const;
142
143   QModelIndex parent(const QModelIndex &index) const;
144
145   int rowCount(const QModelIndex &parent = QModelIndex()) const;
146   int columnCount(const QModelIndex &parent = QModelIndex()) const;
147
148   virtual void clear();
149
150 private slots:
151   void itemDataChanged(int column);
152   void newChild(AbstractTreeItem *child);
153   void childDestroyed(int row);
154
155 protected:
156   void appendChild(AbstractTreeItem *parent, AbstractTreeItem *child);
157
158   bool removeRow(int row, const QModelIndex &parent = QModelIndex());
159   bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
160   
161   AbstractTreeItem *rootItem;
162 };
163
164 #endif