1 /***************************************************************************
2 * Copyright (C) 2005-2012 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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
40 NoTreeItemFlag = 0x00,
41 DeleteOnLastChildRemoved = 0x01
43 Q_DECLARE_FLAGS(TreeItemFlags, TreeItemFlag)
45 AbstractTreeItem(AbstractTreeItem *parent = 0);
47 bool newChild(AbstractTreeItem *child);
48 bool newChilds(const QList<AbstractTreeItem *> &items);
50 bool removeChild(int row);
51 inline bool removeChild(AbstractTreeItem *child) { return removeChild(child->row()); }
52 void removeAllChilds();
54 bool reParent(AbstractTreeItem *newParent);
56 AbstractTreeItem *child(int row) const;
58 int childCount(int column = 0) const;
60 virtual int columnCount() const = 0;
62 virtual QVariant data(int column, int role) const = 0;
63 virtual bool setData(int column, const QVariant &value, int role) = 0;
65 virtual inline Qt::ItemFlags flags() const { return _flags; }
66 virtual inline void setFlags(Qt::ItemFlags flags) { _flags = flags; }
68 inline AbstractTreeItem::TreeItemFlags treeItemFlags() const { return _treeItemFlags; }
69 inline void setTreeItemFlags(AbstractTreeItem::TreeItemFlags flags) { _treeItemFlags = flags; }
71 inline AbstractTreeItem *parent() const { return qobject_cast<AbstractTreeItem *>(QObject::parent()); }
76 void dataChanged(int column = -1);
78 void beginAppendChilds(int firstRow, int lastRow);
79 void endAppendChilds();
81 void beginRemoveChilds(int firstRow, int lastRow);
82 void endRemoveChilds();
85 void customEvent(QEvent *event);
88 QList<AbstractTreeItem *> _childItems;
90 TreeItemFlags _treeItemFlags;
92 void removeChildLater(AbstractTreeItem *child);
93 inline void checkForDeletion()
95 if (treeItemFlags() & DeleteOnLastChildRemoved && childCount() == 0) parent()->removeChildLater(this);
100 /*****************************************
102 *****************************************/
103 class SimpleTreeItem : public AbstractTreeItem
108 SimpleTreeItem(const QList<QVariant> &data, AbstractTreeItem *parent = 0);
109 virtual ~SimpleTreeItem();
111 virtual QVariant data(int column, int role) const;
112 virtual bool setData(int column, const QVariant &value, int role);
114 virtual int columnCount() const;
117 QList<QVariant> _itemData;
121 /*****************************************
123 *****************************************/
124 class PropertyMapItem : public AbstractTreeItem
129 PropertyMapItem(const QStringList &propertyOrder, AbstractTreeItem *parent = 0);
130 PropertyMapItem(AbstractTreeItem *parent = 0);
132 virtual ~PropertyMapItem();
134 virtual QVariant data(int column, int role) const;
135 virtual bool setData(int column, const QVariant &value, int role);
137 virtual QString toolTip(int column) const { Q_UNUSED(column) return QString(); }
138 virtual int columnCount() const;
140 void appendProperty(const QString &property);
143 QStringList _propertyOrder;
147 /*****************************************
149 *****************************************/
150 class TreeModel : public QAbstractItemModel
156 SortRole = Qt::UserRole,
160 TreeModel(const QList<QVariant> &, QObject *parent = 0);
161 virtual ~TreeModel();
163 virtual QVariant data(const QModelIndex &index, int role) const;
164 virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
166 virtual Qt::ItemFlags flags(const QModelIndex &index) const;
167 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
169 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
170 QModelIndex indexByItem(AbstractTreeItem *item) const;
172 QModelIndex parent(const QModelIndex &index) const;
174 int rowCount(const QModelIndex &parent = QModelIndex()) const;
175 int columnCount(const QModelIndex &parent = QModelIndex()) const;
177 virtual void clear();
180 void itemDataChanged(int column = -1);
182 void beginAppendChilds(int firstRow, int lastRow);
183 void endAppendChilds();
185 void beginRemoveChilds(int firstRow, int lastRow);
186 void endRemoveChilds();
189 AbstractTreeItem *rootItem;
192 void connectItem(AbstractTreeItem *item);
199 inline ChildStatus(QModelIndex parent_, int cc_, int s_, int e_) : parent(parent_), childCount(cc_), start(s_), end(e_) {};
201 ChildStatus _childStatus;
202 int _aboutToRemoveOrInsert;
205 void debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
206 void debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
207 void debug_rowsInserted(const QModelIndex &parent, int start, int end);
208 void debug_rowsRemoved(const QModelIndex &parent, int start, int end);
209 void debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);