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 #include <QLinkedList> // needed for debug
32 /*****************************************
33 * general item used in the Tree Model
34 *****************************************/
35 class AbstractTreeItem : public QObject {
37 Q_PROPERTY(quint64 id READ id)
40 AbstractTreeItem(AbstractTreeItem *parent = 0);
41 virtual ~AbstractTreeItem();
43 bool newChild(int column, AbstractTreeItem *child);
44 bool newChild(AbstractTreeItem *child);
46 bool removeChild(int column, int row);
47 bool removeChild(int row);
49 bool removeChildById(int column, const quint64 &id);
50 bool removeChildById(const quint64 &id);
52 void removeAllChilds();
54 virtual quint64 id() const;
56 AbstractTreeItem *child(int column, int row) const;
57 AbstractTreeItem *child(int row) const;
59 AbstractTreeItem *childById(int column, const quint64 &id) const;
60 AbstractTreeItem *childById(const quint64 &id) const;
62 int childCount(int column) const;
63 int childCount() const;
65 virtual int columnCount() const = 0;
67 virtual QVariant data(int column, int role) const = 0;
68 virtual bool setData(int column, const QVariant &value, int role) = 0;
70 virtual Qt::ItemFlags flags() const;
71 virtual void setFlags(Qt::ItemFlags);
75 AbstractTreeItem *parent() const;
80 void dataChanged(int column = -1);
82 void beginAppendChilds(int column, int firstRow, int lastRow);
83 void endAppendChilds();
85 void beginRemoveChilds(int column, int firstRow, int lastRow);
86 void endRemoveChilds();
89 QHash<int, QList<AbstractTreeItem *> > _childItems;
92 int defaultColumn() const;
96 /*****************************************
98 *****************************************/
99 class SimpleTreeItem : public AbstractTreeItem {
103 SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent = 0);
104 virtual ~SimpleTreeItem();
106 virtual QVariant data(int column, int role) const;
107 virtual bool setData(int column, const QVariant &value, int role);
109 virtual int columnCount() const;
112 QList<QVariant> _itemData;
115 /*****************************************
117 *****************************************/
118 class PropertyMapItem : public AbstractTreeItem {
122 PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent = 0);
123 PropertyMapItem(AbstractTreeItem *parent = 0);
125 virtual ~PropertyMapItem();
127 virtual QVariant data(int column, int role) const;
128 virtual bool setData(int column, const QVariant &value, int role);
130 virtual QString toolTip(int column) const { Q_UNUSED(column) return QString(); }
131 virtual int columnCount() const;
133 void appendProperty(const QString &property);
136 QStringList _propertyOrder;
140 /*****************************************
142 *****************************************/
143 class TreeModel : public QAbstractItemModel {
147 TreeModel(const QList<QVariant> &, QObject *parent = 0);
148 virtual ~TreeModel();
150 virtual QVariant data(const QModelIndex &index, int role) const;
151 virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
153 virtual Qt::ItemFlags flags(const QModelIndex &index) const;
154 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
156 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
157 QModelIndex indexById(quint64 id, const QModelIndex &parent = QModelIndex()) const;
158 QModelIndex indexByItem(AbstractTreeItem *item, int column = 0) const;
160 QModelIndex parent(const QModelIndex &index) const;
162 int rowCount(const QModelIndex &parent = QModelIndex()) const;
163 int columnCount(const QModelIndex &parent = QModelIndex()) const;
165 virtual void clear();
168 void itemDataChanged(int column = -1);
170 void beginAppendChilds(int column, int firstRow, int lastRow);
171 void endAppendChilds();
173 void beginRemoveChilds(int column, int firstRow, int lastRow);
174 void endRemoveChilds();
177 AbstractTreeItem *rootItem;
180 void connectItem(AbstractTreeItem *item);
187 inline ChildStatus(QModelIndex parent_, int cc_, int s_, int e_) : parent(parent_), childCount(cc_), start(s_), end(e_) {};
189 ChildStatus _childStatus;
190 int _aboutToRemoveOrInsert;
193 void debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
194 void debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
195 void debug_rowsInserted(const QModelIndex &parent, int start, int end);
196 void debug_rowsRemoved(const QModelIndex &parent, int start, int end);