Started to reorganize the Buffer{Model|View|Filter}. Mostly cleanup at the moment.
[quassel.git] / src / client / treemodel.h
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel 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) any later version.                                   *
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 <QVariant>
26 #include <QHash>
27 #include <QAbstractItemModel>
28
29 /*****************************************
30  *  general item used in the Tree Model
31  *****************************************/
32 class TreeItem : public QObject {
33   Q_OBJECT
34   Q_PROPERTY(uint id READ id)
35   
36 public:
37   TreeItem(const QList<QVariant> &data, TreeItem *parent = 0);
38   TreeItem(TreeItem *parent = 0);
39   virtual ~TreeItem();
40   
41   void appendChild(TreeItem *child);
42   void removeChild(int row);
43                    
44   virtual uint id() const;
45   TreeItem *child(int row) const;
46   TreeItem *childById(const uint &) const;
47   int childCount() const;
48   int columnCount() const;
49   virtual QVariant data(int column, int role) const;
50   virtual Qt::ItemFlags flags() const;
51   int row() const;
52   TreeItem *parent();
53     
54 protected:
55   QList<TreeItem *> childItems;
56   QHash<uint, TreeItem *> childHash; // uint to be compatible to qHash functions
57   TreeItem *parentItem;
58   QList<QVariant> itemData;
59 };
60
61
62 /*****************************************
63  * TreeModel
64  *****************************************/
65 class TreeModel : public QAbstractItemModel {
66   Q_OBJECT
67   
68 public:
69   TreeModel(const QList<QVariant> &, QObject *parent = 0);
70   virtual ~TreeModel();
71   
72   QVariant data(const QModelIndex &index, int role) const;
73   virtual Qt::ItemFlags flags(const QModelIndex &index) const;
74   QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
75   QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
76   QModelIndex indexById(uint id, const QModelIndex &parent = QModelIndex()) const;
77   QModelIndex parent(const QModelIndex &index) const;
78   int rowCount(const QModelIndex &parent = QModelIndex()) const;
79   int columnCount(const QModelIndex &parent = QModelIndex()) const;
80
81   virtual void clear();
82
83 protected:
84   bool removeRow(int row, const QModelIndex &parent = QModelIndex());
85   bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
86   
87   TreeItem *rootItem;
88 };
89
90 #endif