1 /***************************************************************************
2 * Copyright (C) 2005-2020 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"
25 #include <QAbstractItemModel>
26 #include <QLinkedList> // needed for debug
28 #include <QStringList>
31 /*****************************************
32 * general item used in the Tree Model
33 *****************************************/
34 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)
97 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;
120 /*****************************************
122 *****************************************/
123 class CLIENT_EXPORT PropertyMapItem : public AbstractTreeItem
128 PropertyMapItem(AbstractTreeItem* parent = nullptr);
130 virtual QStringList propertyOrder() const = 0;
132 QVariant data(int column, int role) const override;
133 bool setData(int column, const QVariant& value, int role) override;
135 virtual QString toolTip(int column) const { Q_UNUSED(column) return QString(); }
136 int columnCount() const override;
139 /*****************************************
141 *****************************************/
142 class CLIENT_EXPORT TreeModel : public QAbstractItemModel
149 SortRole = Qt::UserRole,
153 TreeModel(const QList<QVariant>&, QObject* parent = nullptr);
154 ~TreeModel() override;
156 AbstractTreeItem* root() const;
158 QVariant data(const QModelIndex& index, int role) const override;
159 bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
161 Qt::ItemFlags flags(const QModelIndex& index) const override;
162 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
164 QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
165 QModelIndex indexByItem(AbstractTreeItem* item) const;
167 QModelIndex parent(const QModelIndex& index) const override;
169 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
170 int columnCount(const QModelIndex& parent = QModelIndex()) const override;
172 virtual void clear();
175 void itemDataChanged(int column = -1);
177 void beginAppendChilds(int firstRow, int lastRow);
178 void endAppendChilds();
180 void beginRemoveChilds(int firstRow, int lastRow);
181 void endRemoveChilds();
184 AbstractTreeItem* rootItem;
187 void connectItem(AbstractTreeItem* item);
195 inline ChildStatus(QModelIndex parent_, int cc_, int s_, int 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);