1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
25 #include <QStringList>
28 #include <QAbstractItemModel>
30 /*****************************************
31 * general item used in the Tree Model
32 *****************************************/
33 class AbstractTreeItem : public QObject {
35 Q_PROPERTY(uint id READ id)
38 AbstractTreeItem(AbstractTreeItem *parent = 0);
39 virtual ~AbstractTreeItem();
41 void appendChild(int column, AbstractTreeItem *child);
42 void appendChild(AbstractTreeItem *child);
44 void removeChild(int column, int row);
45 void removeChild(int row);
47 virtual quint64 id() const;
49 AbstractTreeItem *child(int column, int row) const;
50 AbstractTreeItem *child(int row) const;
52 AbstractTreeItem *childById(int column, const uint &id) const;
53 AbstractTreeItem *childById(const uint &id) const;
55 int childCount(int column) const;
56 int childCount() const;
58 virtual int columnCount() const = 0;
60 virtual QVariant data(int column, int role) const = 0;
62 virtual Qt::ItemFlags flags() const;
63 virtual void setFlags(Qt::ItemFlags);
67 AbstractTreeItem *parent();
70 void dataChanged(int column = -1);
71 void newChild(AbstractTreeItem *);
72 void childDestroyed(int row);
75 void childDestroyed();
78 QHash<int, QList<AbstractTreeItem *> > _childItems;
79 QHash<int, QHash<quint64, AbstractTreeItem *> > _childHash; // uint to be compatible to qHash functions
80 AbstractTreeItem *_parentItem;
83 int defaultColumn() const;
87 /*****************************************
89 *****************************************/
90 class SimpleTreeItem : public AbstractTreeItem {
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;
100 QList<QVariant> _itemData;
103 /*****************************************
105 *****************************************/
106 class PropertyMapItem : public AbstractTreeItem {
110 PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent = 0);
111 PropertyMapItem(AbstractTreeItem *parent = 0);
113 virtual ~PropertyMapItem();
115 virtual QVariant data(int column, int role) const;
116 virtual int columnCount() const;
118 void appendProperty(const QString &property);
121 QStringList _propertyOrder;
125 /*****************************************
127 *****************************************/
128 class TreeModel : public QAbstractItemModel {
132 TreeModel(const QList<QVariant> &, QObject *parent = 0);
133 virtual ~TreeModel();
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;
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;
143 QModelIndex parent(const QModelIndex &index) const;
145 int rowCount(const QModelIndex &parent = QModelIndex()) const;
146 int columnCount(const QModelIndex &parent = QModelIndex()) const;
148 virtual void clear();
151 void itemDataChanged(int column = -1);
152 void newChild(AbstractTreeItem *child);
153 void childDestroyed(int row);
156 void appendChild(AbstractTreeItem *parent, AbstractTreeItem *child);
158 bool removeRow(int row, const QModelIndex &parent = QModelIndex());
159 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
161 AbstractTreeItem *rootItem;