BibleTime
btbookshelfmodel.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-2021 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 <QAbstractListModel>
16 
17 #include <memory>
18 #include <QList>
19 #include <QModelIndex>
20 #include <QObject>
21 #include <QString>
22 #include <Qt>
23 #include <QVariant>
24 #include "../drivers/btconstmoduleset.h"
25 
26 
27 class CSwordModuleInfo;
28 
29 /**
30  Implements a simple list model projecting CSwordModuleInfo instances. This model is mostly
31  implemented to provide an interface the the underlying data and to provide notifications
32  when modules are added, removed or changed. If you want to use a model for widgets, the
33  BtBookshelfTreeModel might be a better choice, since it also provides sorting and grouping.
34 */
36 
37  Q_OBJECT
38 
39 public: // types:
40 
41  enum ModuleRole {
42  ModuleNameRole = Qt::UserRole,
52  UserRole
53  };
54 
55 private: // types:
56 
57  /// Used to restrict construction to newInstance() only.
58  struct ConstructInPrivate {};
59 
60 public: // methods:
61 
65 
66  static std::shared_ptr<BtBookshelfModel> newInstance();
67 
68  ~BtBookshelfModel() noexcept override;
69 
70  BtBookshelfModel & operator=(BtBookshelfModel &&) = delete;
71  BtBookshelfModel & operator=(BtBookshelfModel const &) = delete;
72 
73 
74  // Virtual methods implemented from QAbstractListModel:
75  int rowCount(const QModelIndex & parent = QModelIndex()) const override;
76  QVariant data(CSwordModuleInfo * module, int role) const;
77  QVariant data(const QModelIndex & index, int role) const override;
78  QVariant headerData(int section,
79  Qt::Orientation orientation,
80  int role = Qt::DisplayRole) const override;
81  bool setData(const QModelIndex & index,
82  const QVariant & value,
83  int role = ModuleHiddenRole) override;
84 
85  /**
86  Given an index of this model, this method returns a pointer to the underlying
87  CSwordModuleInfo instance corresponding to the given index.
88  \param[in] index An index to this model.
89  */
90  CSwordModuleInfo * module(QModelIndex const & index) const {
91  return static_cast<CSwordModuleInfo *>(
92  data(index,
93  BtBookshelfModel::ModulePointerRole).value<void *>());
94  }
95 
96  /**
97  Clears the data of the whole model by removing all items.
98  \param[in] destroy If true, all CSwordModuleInfo instances in this model are also
99  destroyed.
100  */
101  void clear(bool destroy = false);
102 
103  /**
104  Appends the given module to this model.
105  \param[in] module Module to add.
106  */
107  void addModule(CSwordModuleInfo * const module);
108 
109  /**
110  Removes the given module from this model and optionally destroys it.
111  \param[in] module The module to remove from this model.
112  \param[in] destroy If true, the given CSwordModuleInfo instance is destroyed.
113  */
114  void removeModule(CSwordModuleInfo * const module,
115  bool destroy = false);
116 
117  /**
118  Removes all modules from the given set from this model and optionally destroys
119  them.
120  \param[in] modules The set of modules to remove from this model.
121  \param[in] destroy If true, the given CSwordModuleInfo instances are destroyed.
122  */
123  void removeModules(BtConstModuleSet const & modules,
124  bool destroy = false);
125 
126  /**
127  Removes all modules from the given list from this model and optionally destroys
128  them.
129  \param[in] modules The list of modules to remove from this model.
130  \param[in] destroy If true, the given CSwordModuleInfo instances are destroyed.
131  */
132  void removeModules(const QList<CSwordModuleInfo *> & modules,
133  bool destroy = false);
134 
135  /**
136  Returns the first module found with the given name.
137  \param[in] name Name of the module to find.
138  */
139  CSwordModuleInfo* getModule(const QString & name) const;
140 
141  /**
142  Returns the list of handled modules as a list of CSwordModuleInfo* pointers.
143  */
144  QList<CSwordModuleInfo *> const & moduleList() const { return m_data; }
145 
146 protected Q_SLOTS:
147 
148  /**
149  Slot DIRECTLY called by CSwordModuleInfo when the hidden status of the respective
150  module changes.
151  \param[in] hidden True, if the module was hidden; false, if the module was shown.
152  */
153  void moduleHidden(bool hidden);
154 
155  /**
156  Slot DIRECTLY called by CSwordModuleInfo when the indexed status of the respective
157  module changes.
158  \param[in] indexed True, if the module was indexed; false if the index was deleted.
159  */
160  void moduleIndexed(bool indexed);
161 
162  /**
163  Slot DIRECTLY called by CSwordModuleInfo when the locked status of the respective
164  module changes.
165  \param[in] unlocked True, if the module was unlocked; false if the module was
166  locked.
167  */
168  void moduleUnlocked(bool unlocked);
169 
170 private: // methods:
171 
172  /**
173  Called internally when module data changes. This method emits any neccessary
174  signals for this model.
175  \pre The givem module is handled by this model.
176  \param[in] module The module that changed status.
177  */
179 
180 private: // fields:
181 
182  /**
183  The underlying data as a list of pointers to the respective CSwordModuleInfo
184  instances.
185  */
187 
188 };
static std::shared_ptr< BtBookshelfModel > newInstance()
void removeModule(CSwordModuleInfo *const module, bool destroy=false)
void addModule(CSwordModuleInfo *const module)
void moduleIndexed(bool indexed)
CSwordModuleInfo * module(QModelIndex const &index) const
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
bool setData(const QModelIndex &index, const QVariant &value, int role=ModuleHiddenRole) override
~BtBookshelfModel() noexcept override
QList< CSwordModuleInfo * > m_data
QVariant data(CSwordModuleInfo *module, int role) const
BtBookshelfModel(BtBookshelfModel const &)=delete
void moduleHidden(bool hidden)
CSwordModuleInfo * getModule(const QString &name) const
QList< CSwordModuleInfo * > const & moduleList() const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void moduleUnlocked(bool unlocked)
void moduleDataChanged(CSwordModuleInfo *module)
BtBookshelfModel(BtBookshelfModel &&)=delete
void removeModules(BtConstModuleSet const &modules, bool destroy=false)
void clear(bool destroy=false)
Used to restrict construction to newInstance() only.