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(AbstractTreeItem *child);
44 bool newChilds(const QList<AbstractTreeItem *> &items);
46 bool removeChild(int row);
47 bool removeChildById(const quint64 &id);
48 void removeAllChilds();
50 virtual quint64 id() const;
52 bool reParent(AbstractTreeItem *newParent);
54 AbstractTreeItem *child(int row) const;
55 AbstractTreeItem *childById(const quint64 &id) const;
57 int childCount(int column = 0) const;
59 virtual int columnCount() const = 0;
61 virtual QVariant data(int column, int role) const = 0;
62 virtual bool setData(int column, const QVariant &value, int role) = 0;
64 virtual Qt::ItemFlags flags() const;
65 virtual void setFlags(Qt::ItemFlags);
68 AbstractTreeItem *parent() const;
73 void dataChanged(int column = -1);
75 void beginAppendChilds(int firstRow, int lastRow);
76 void endAppendChilds();
78 void beginRemoveChilds(int firstRow, int lastRow);
79 void endRemoveChilds();
82 QList<AbstractTreeItem *> _childItems;
87 /*****************************************
89 *****************************************/
90 class SimpleTreeItem : public AbstractTreeItem {
94 SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent = 0);
95 virtual ~SimpleTreeItem();
97 virtual QVariant data(int column, int role) const;
98 virtual bool setData(int column, const QVariant &value, int role);
100 virtual int columnCount() const;
103 QList<QVariant> _itemData;
106 /*****************************************
108 *****************************************/
109 class PropertyMapItem : public AbstractTreeItem {
113 PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent = 0);
114 PropertyMapItem(AbstractTreeItem *parent = 0);
116 virtual ~PropertyMapItem();
118 virtual QVariant data(int column, int role) const;
119 virtual bool setData(int column, const QVariant &value, int role);
121 virtual QString toolTip(int column) const { Q_UNUSED(column) return QString(); }
122 virtual int columnCount() const;
124 void appendProperty(const QString &property);
127 QStringList _propertyOrder;
131 /*****************************************
133 *****************************************/
134 class TreeModel : public QAbstractItemModel {
139 SortRole = Qt::UserRole,
143 TreeModel(const QList<QVariant> &, QObject *parent = 0);
144 virtual ~TreeModel();
146 virtual QVariant data(const QModelIndex &index, int role) const;
147 virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
149 virtual Qt::ItemFlags flags(const QModelIndex &index) const;
150 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
152 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
153 QModelIndex indexById(quint64 id, const QModelIndex &parent = QModelIndex()) const;
154 QModelIndex indexByItem(AbstractTreeItem *item) const;
156 QModelIndex parent(const QModelIndex &index) const;
158 int rowCount(const QModelIndex &parent = QModelIndex()) const;
159 int columnCount(const QModelIndex &parent = QModelIndex()) const;
161 virtual void clear();
164 void itemDataChanged(int column = -1);
166 void beginAppendChilds(int firstRow, int lastRow);
167 void endAppendChilds();
169 void beginRemoveChilds(int firstRow, int lastRow);
170 void endRemoveChilds();
173 AbstractTreeItem *rootItem;
176 void connectItem(AbstractTreeItem *item);
183 inline ChildStatus(QModelIndex parent_, int cc_, int s_, int e_) : parent(parent_), childCount(cc_), start(s_), end(e_) {};
185 ChildStatus _childStatus;
186 int _aboutToRemoveOrInsert;
189 void debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
190 void debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
191 void debug_rowsInserted(const QModelIndex &parent, int start, int end);
192 void debug_rowsRemoved(const QModelIndex &parent, int start, int end);
193 void debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);