BibleTime
btbookshelftreemodel.h
Go to the documentation of this file.
1/*********
2*
3* In the name of the Father, and of the Son, and of the Holy Spirit.
4*
5* This file is part of BibleTime's source code, https://bibletime.info/
6*
7* Copyright 1999-2025 by the BibleTime developers.
8* The BibleTime source code is licensed under the GNU General Public License
9* version 2.0.
10*
11**********/
12
13#pragma once
14
15#include <QAbstractItemModel>
16
17#include <initializer_list>
18#include <memory>
19#include <QList>
20#include <QObject>
21#include <QMap>
22#include <QModelIndex>
23#include <QPersistentModelIndex>
24#include <QString>
25#include <QtCore>
26#include <QVariant>
27#include "../drivers/btconstmoduleset.h"
28#include "../drivers/btmoduleset.h"
29#include "btbookshelfmodel.h"
30#include "item.h"
31
32
33namespace BookshelfModel { class ModuleItem; }
34class BtConfigCore;
36
42private: // types:
43
46
47public: // types:
48
53
59
61 CHECKED, /**< Check all added modules by default. */
62 UNCHECKED, /**< Uncheck all added modules by default. */
63 MODULE_HIDDEN, /**< By default, check only added modules that are not hidden. */
64 MODULE_INDEXED /**< By default, check only added modules that are indexed. */
65 };
66
67 class Grouping {
68
69 public: // methods:
70
71 Grouping() = default;
72 Grouping(Grouping && copy) = default;
73 Grouping(Grouping const & copy) = default;
74 Grouping(std::initializer_list<Group> values) : m_list(values) {}
75
76 ~Grouping() = default;
77
78 Grouping & operator=(Grouping && copy) = default;
79 Grouping & operator=(Grouping const & copy) = default;
80
81 auto const & list() const noexcept { return m_list; }
82
83 bool loadFrom(BtConfigCore const & config, QString const & key);
84 void saveTo(BtConfigCore & config, QString const & key) const;
85
86 friend bool operator==(Grouping const & lhs, Grouping const & rhs)
87 { return lhs.list() == rhs.list(); }
88
89 friend QDataStream &
91
92 public: // fields:
93
94 static Grouping const NONE;
95 static Grouping const CAT;
96 static Grouping const CAT_LANG;
97 static Grouping const LANG;
98 static Grouping const LANG_CAT;
99 static Grouping const DEFAULT;
100
101 private: // fields:
102
103 QList<Group> m_list;
104
105 };
106
107public: // methods:
108
110 BtBookshelfTreeModel(BtConfigCore const & config,
111 QString const & configKey,
112 QObject * const parent = nullptr);
113 BtBookshelfTreeModel(Grouping const & grouping, QObject * parent = nullptr);
115
116 int rowCount(QModelIndex const & parent = QModelIndex()) const override;
117 int columnCount(QModelIndex const & parent = QModelIndex()) const override;
118 bool hasChildren(QModelIndex const & parent = QModelIndex()) const override;
119 QModelIndex index(int row,
120 int column,
121 QModelIndex const & parent = QModelIndex())
122 const override;
123 QModelIndex parent(QModelIndex const & index) const override;
124 QVariant data(QModelIndex const & index, int role = Qt::DisplayRole)
125 const override;
126 QVariant data(CSwordModuleInfo & module, int role = Qt::DisplayRole) const;
127 Qt::ItemFlags flags(QModelIndex const & index) const override;
129 Qt::Orientation orientation,
130 int role = Qt::DisplayRole) const override;
131 bool setData(QModelIndex const & index,
132 QVariant const & value,
133 int role) override;
134
135 CSwordModuleInfo * module(QModelIndex const & index) const {
136 return static_cast<CSwordModuleInfo *>(
137 data(index,
138 BtBookshelfModel::ModulePointerRole).value<void *>());
139 }
140 std::shared_ptr<QAbstractItemModel> sourceModel() const noexcept
141 { return m_sourceModel; }
142
143 Grouping const & groupingOrder() const { return m_groupingOrder; }
144 bool checkable() const { return m_checkable; }
146 QList<CSwordModuleInfo *> modules() const { return m_modules.keys(); }
147 BtModuleSet const & checkedModules() const {
149 }
150
151public Q_SLOTS:
152
153 void setSourceModel(std::shared_ptr<QAbstractItemModel> sourceModel);
155 bool emitSignal = true);
156 void setCheckable(bool checkable);
159
161
164
165protected: // methods:
166
167 void resetData();
168
169protected Q_SLOTS:
170
171 void moduleDataChanged(QModelIndex const & topLeft,
172 QModelIndex const & bottomRight);
173 void moduleInserted(QModelIndex const & parent, int start, int end);
174 void moduleRemoved(QModelIndex const & parent, int start, int end);
175
176private: // methods:
177
178 void addModule(CSwordModuleInfo & module, bool checked, bool inReset);
180
181 BookshelfModel::Item & getItem(QModelIndex const & index) const;
182 QModelIndex getIndex(BookshelfModel::Item const & item);
183 void resetParentCheckStates(QModelIndex parentIndex, bool inReset);
184
185 template <class T>
186 QModelIndex getGroup(CSwordModuleInfo const & module,
187 QModelIndex const parentIndex,
188 bool & beginInsert)
189 {
191 int groupIndex;
192 T * groupItem = parentItem.getGroupItem<T>(module, groupIndex);
193
194 if (!groupItem) {
195 groupItem = new T(module);
196 groupIndex = parentItem.indexFor(*groupItem);
197 if (beginInsert) {
199 beginInsert = false;
200 }
201 parentItem.insertChild(groupIndex, groupItem);
202 }
203 return index(groupIndex, 0, parentIndex);
204 }
205
206private: // fields:
207
208 std::shared_ptr<QAbstractItemModel> m_sourceModel;
209 std::unique_ptr<BookshelfModel::Item> m_rootItem;
215
217
218};
219
220QDataStream & operator <<(QDataStream & os, BtBookshelfTreeModel::Grouping const & o);
221QDataStream & operator >>(QDataStream & is, BtBookshelfTreeModel::Grouping & o);
222
223Q_DECLARE_METATYPE(BtBookshelfTreeModel::Grouping)
QDataStream & operator>>(QDataStream &is, BtBookshelfTreeModel::Grouping &o)
QDataStream & operator<<(QDataStream &os, BtBookshelfTreeModel::Grouping const &o)
friend QDataStream & operator>>(QDataStream &is, BtBookshelfTreeModel::Grouping &o)
auto const & list() const noexcept
friend bool operator==(Grouping const &lhs, Grouping const &rhs)
void saveTo(BtConfigCore &config, QString const &key) const
Grouping(Grouping const &copy)=default
Grouping & operator=(Grouping const &copy)=default
Grouping(std::initializer_list< Group > values)
Grouping(Grouping &&copy)=default
Grouping & operator=(Grouping &&copy)=default
bool loadFrom(BtConfigCore const &config, QString const &key)
QVariant data(QModelIndex const &index, int role=Qt::DisplayRole) const override
CheckedBehavior m_defaultChecked
QModelIndex index(int row, int column, QModelIndex const &parent=QModelIndex()) const override
std::unique_ptr< BookshelfModel::Item > m_rootItem
void groupingOrderChanged(BtBookshelfTreeModel::Grouping newGrouping)
void removeModule(CSwordModuleInfo &module)
void setDefaultChecked(CheckedBehavior const b)
QModelIndex parent(QModelIndex const &index) const override
std::shared_ptr< QAbstractItemModel > m_sourceModel
BookshelfModel::Item & getItem(QModelIndex const &index) const
BtModuleSet const & checkedModules() const
QModelIndex getIndex(BookshelfModel::Item const &item)
void setCheckable(bool checkable)
void setCheckedModules(BtConstModuleSet const &modules)
void setGroupingOrder(BtBookshelfTreeModel::Grouping const &groupingOrder, bool emitSignal=true)
CSwordModuleInfo * module(QModelIndex const &index) const
bool setData(QModelIndex const &index, QVariant const &value, int role) override
QMap< CSwordModuleInfo *, QPersistentModelIndex > SourceIndexMap
QList< CSwordModuleInfo * > modules() const
int columnCount(QModelIndex const &parent=QModelIndex()) const override
~BtBookshelfTreeModel() override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
CheckedBehavior defaultChecked() const
void resetParentCheckStates(QModelIndex parentIndex, bool inReset)
QModelIndex getGroup(CSwordModuleInfo const &module, QModelIndex const parentIndex, bool &beginInsert)
Qt::ItemFlags flags(QModelIndex const &index) const override
Grouping const & groupingOrder() const
bool hasChildren(QModelIndex const &parent=QModelIndex()) const override
std::shared_ptr< QAbstractItemModel > sourceModel() const noexcept
void setSourceModel(std::shared_ptr< QAbstractItemModel > sourceModel)
int rowCount(QModelIndex const &parent=QModelIndex()) const override
void moduleDataChanged(QModelIndex const &topLeft, QModelIndex const &bottomRight)
QMap< CSwordModuleInfo *, BookshelfModel::ModuleItem * > ModuleItemMap
void addModule(CSwordModuleInfo &module, bool checked, bool inReset)
void moduleRemoved(QModelIndex const &parent, int start, int end)
void moduleInserted(QModelIndex const &parent, int start, int end)
void moduleChecked(CSwordModuleInfo *module, bool checked)
#define T(f)