1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 ***************************************************************************/
23 #include "client-export.h"
26 #include <QStringList>
28 #include <QAbstractItemModel>
30 #include <QLinkedList> // needed for debug
32 /*****************************************
33 * general item used in the Tree Model
34 *****************************************/
35 class CLIENT_EXPORT AbstractTreeItem : public QObject
41 NoTreeItemFlag = 0x00,
42 DeleteOnLastChildRemoved = 0x01
44 Q_DECLARE_FLAGS(TreeItemFlags, TreeItemFlag)
46 AbstractTreeItem(AbstractTreeItem *parent = nullptr);
48 bool newChild(AbstractTreeItem *child);
49 bool newChilds(const QList<AbstractTreeItem *> &items);
51 bool removeChild(int row);
52 inline bool removeChild(AbstractTreeItem *child) { return removeChild(child->row()); }
53 void removeAllChilds();
55 bool reParent(AbstractTreeItem *newParent);
57 AbstractTreeItem *child(int row) const;
59 int childCount(int column = 0) const;
61 virtual int columnCount() const = 0;
63 virtual QVariant data(int column, int role) const = 0;
64 virtual bool setData(int column, const QVariant &value, int role) = 0;
66 virtual inline Qt::ItemFlags flags() const { return _flags; }
67 virtual inline void setFlags(Qt::ItemFlags flags) { _flags = flags; }
69 inline AbstractTreeItem::TreeItemFlags treeItemFlags() const { return _treeItemFlags; }
70 inline void setTreeItemFlags(AbstractTreeItem::TreeItemFlags flags) { _treeItemFlags = flags; }
72 inline AbstractTreeItem *parent() const { return qobject_cast<AbstractTreeItem *>(QObject::parent()); }
77 void dataChanged(int column = -1);
79 void beginAppendChilds(int firstRow, int lastRow);
80 void endAppendChilds();
82 void beginRemoveChilds(int firstRow, int lastRow);
83 void endRemoveChilds();
86 void customEvent(QEvent *event) override;
89 QList<AbstractTreeItem *> _childItems;
91 TreeItemFlags _treeItemFlags;
93 void removeChildLater(AbstractTreeItem *child);
94 inline void checkForDeletion()
96 if (treeItemFlags() & DeleteOnLastChildRemoved && childCount() == 0) parent()->removeChildLater(this);
101 /*****************************************
103 *****************************************/
104 class CLIENT_EXPORT SimpleTreeItem : public AbstractTreeItem
109 SimpleTreeItem(QList<QVariant> data, AbstractTreeItem *parent = nullptr);
111 QVariant data(int column, int role) const override;
112 bool setData(int column, const QVariant &value, int role) override;
114 int columnCount() const override;
117 QList<QVariant> _itemData;
121 /*****************************************
123 *****************************************/
124 class CLIENT_EXPORT PropertyMapItem : public AbstractTreeItem
129 PropertyMapItem(AbstractTreeItem *parent = nullptr);
131 virtual QStringList propertyOrder() const = 0;
133 QVariant data(int column, int role) const override;
134 bool setData(int column, const QVariant &value, int role) override;
136 virtual QString toolTip(int column) const { Q_UNUSED(column) return QString(); }
137 int columnCount() const override;
141 /*****************************************
143 *****************************************/
144 class CLIENT_EXPORT TreeModel : public QAbstractItemModel
150 SortRole = Qt::UserRole,
154 TreeModel(const QList<QVariant> &, QObject *parent = nullptr);
155 ~TreeModel() override;
157 AbstractTreeItem *root() const;
159 QVariant data(const QModelIndex &index, int role) const override;
160 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
162 Qt::ItemFlags flags(const QModelIndex &index) const override;
163 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
165 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
166 QModelIndex indexByItem(AbstractTreeItem *item) const;
168 QModelIndex parent(const QModelIndex &index) const override;
170 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
171 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
173 virtual void clear();
176 void itemDataChanged(int column = -1);
178 void beginAppendChilds(int firstRow, int lastRow);
179 void endAppendChilds();
181 void beginRemoveChilds(int firstRow, int lastRow);
182 void endRemoveChilds();
185 AbstractTreeItem *rootItem;
188 void connectItem(AbstractTreeItem *item);
195 inline ChildStatus(QModelIndex parent_, int cc_, int s_, int e_) : parent(parent_), childCount(cc_), start(s_), end(e_) {};
197 ChildStatus _childStatus;
198 int _aboutToRemoveOrInsert;
201 void debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
202 void debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
203 void debug_rowsInserted(const QModelIndex &parent, int start, int end);
204 void debug_rowsRemoved(const QModelIndex &parent, int start, int end);
205 void debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);