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