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>
27 #include <QAbstractItemModel>
29 #include <QLinkedList> // needed for debug
31 /*****************************************
32 * general item used in the Tree Model
33 *****************************************/
34 class AbstractTreeItem : public QObject {
39 NoTreeItemFlag = 0x00,
40 DeleteOnLastChildRemoved = 0x01
42 Q_DECLARE_FLAGS(TreeItemFlags, TreeItemFlag);
44 AbstractTreeItem(AbstractTreeItem *parent = 0);
46 bool newChild(AbstractTreeItem *child);
47 bool newChilds(const QList<AbstractTreeItem *> &items);
49 bool removeChild(int row);
50 inline bool removeChild(AbstractTreeItem *child) { return removeChild(child->row()); }
51 void removeAllChilds();
53 bool reParent(AbstractTreeItem *newParent);
55 AbstractTreeItem *child(int row) 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 inline Qt::ItemFlags flags() const { return _flags; }
65 virtual inline void setFlags(Qt::ItemFlags flags) { _flags = flags; }
67 inline AbstractTreeItem::TreeItemFlags treeItemFlags() const { return _treeItemFlags; }
68 inline void setTreeItemFlags(AbstractTreeItem::TreeItemFlags flags) { _treeItemFlags = flags; }
70 inline AbstractTreeItem *parent() const { return qobject_cast<AbstractTreeItem *>(QObject::parent()); }
75 void dataChanged(int column = -1);
77 void beginAppendChilds(int firstRow, int lastRow);
78 void endAppendChilds();
80 void beginRemoveChilds(int firstRow, int lastRow);
81 void endRemoveChilds();
84 QList<AbstractTreeItem *> _childItems;
86 TreeItemFlags _treeItemFlags;
88 inline void checkForDeletion() { if(treeItemFlags() & DeleteOnLastChildRemoved && childCount() == 0) parent()->removeChild(this); }
92 /*****************************************
94 *****************************************/
95 class SimpleTreeItem : public AbstractTreeItem {
99 SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent = 0);
100 virtual ~SimpleTreeItem();
102 virtual QVariant data(int column, int role) const;
103 virtual bool setData(int column, const QVariant &value, int role);
105 virtual int columnCount() const;
108 QList<QVariant> _itemData;
111 /*****************************************
113 *****************************************/
114 class PropertyMapItem : public AbstractTreeItem {
118 PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent = 0);
119 PropertyMapItem(AbstractTreeItem *parent = 0);
121 virtual ~PropertyMapItem();
123 virtual QVariant data(int column, int role) const;
124 virtual bool setData(int column, const QVariant &value, int role);
126 virtual QString toolTip(int column) const { Q_UNUSED(column) return QString(); }
127 virtual int columnCount() const;
129 void appendProperty(const QString &property);
132 QStringList _propertyOrder;
136 /*****************************************
138 *****************************************/
139 class TreeModel : public QAbstractItemModel {
144 SortRole = Qt::UserRole,
148 TreeModel(const QList<QVariant> &, QObject *parent = 0);
149 virtual ~TreeModel();
151 virtual QVariant data(const QModelIndex &index, int role) const;
152 virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
154 virtual Qt::ItemFlags flags(const QModelIndex &index) const;
155 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
157 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
158 QModelIndex indexByItem(AbstractTreeItem *item) 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 firstRow, int lastRow);
171 void endAppendChilds();
173 void beginRemoveChilds(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);
197 void debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);