BibleTime
btmoduletextmodel.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 <optional>
16 #include <QAbstractListModel>
17 #include <QColor>
18 #include <QStringList>
19 #include "../btglobal.h"
20 #include "../drivers/btmodulelist.h"
21 #include "../keys/cswordversekey.h"
22 #include "../keys/cswordtreekey.h"
23 #include "../rendering/cdisplayrendering.h"
24 
25 
26 class CSwordModuleInfo;
27 
28 /** For the BtFindWidget buttons (previous, next) */
29 struct FindState {
30  // Model row for item with highlight
31  int index;
32 
33  // 1st, 2nd, etc. word highlighted within the item
34  int subIndex;
35 };
36 
37 struct ModuleEntry {
38  enum TextRoles {
39  ReferenceRole = Qt::UserRole + 1,
40  TextRole = Qt::UserRole + 2,
41 
42  Text0Role = Qt::UserRole + 10,
43  Text1Role = Qt::UserRole + 11,
44  Text2Role = Qt::UserRole + 12,
45  Text3Role = Qt::UserRole + 13,
46  Text4Role = Qt::UserRole + 14,
47  Text5Role = Qt::UserRole + 15,
48  Text6Role = Qt::UserRole + 16,
49  Text7Role = Qt::UserRole + 17,
50  Text8Role = Qt::UserRole + 18,
51  Text9Role = Qt::UserRole + 19,
52 
53  Title0Role = Qt::UserRole + 20,
54  Title1Role = Qt::UserRole + 21,
55  Title2Role = Qt::UserRole + 22,
56  Title3Role = Qt::UserRole + 23,
57  Title4Role = Qt::UserRole + 24,
58  Title5Role = Qt::UserRole + 25,
59  Title6Role = Qt::UserRole + 26,
60  Title7Role = Qt::UserRole + 27,
61  Title8Role = Qt::UserRole + 28,
62  Title9Role = Qt::UserRole + 29
63  };
64 };
65 
67 
68 public:
69  virtual ~BtModuleTextFilter() = 0;
70  virtual QString processText(const QString& text) = 0;
71 };
72 
73 
74 /**
75  * @page modelviewmodel Details about the module text model
76  * <p>
77  * The BtModuleTextModel represents the text of a sword module. It uses
78  * an integer index to address each entry (verse, paragraph, etc).
79  * The html text is obtained using backend rendering functions.
80  * It is then post processed to fix a few things so that the QML
81  * ListView works correctly. The ListView supports a "rich text"
82  * that is a subset of html. The post processing is done in
83  * the class BtTextFilter which is installed into BtModuleTextModel
84  * by the frontend.
85  * </p>
86  */
87 
88 
89 /**
90  \brief Model that represents the entire text of a given module
91 
92  It is used by a QML ListView to view a small portion of the
93  available module text. Only the portions of the module that are
94  currently displayed and some just above or below this, are
95  instantiated.
96 
97  \note Four parallel modules are supported.
98  */
99 
100 
102 
103  Q_OBJECT
104 
105 public:
106 
107  BtModuleTextModel(QObject *parent = nullptr);
108 
109  /** Convert index(row) into CSwordVerseKey. */
110  CSwordVerseKey indexToVerseKey(int index) const;
111 
112  /** Convert index(row) into verse. */
113  int indexToVerse(int index) const;
114 
115  /** Convert index(row) into CSwordVerseKey. */
117  CSwordModuleInfo const & module) const;
118 
119  /** Convert index(row) into CSwordVerseKey. */
120  CSwordKey* indexToKey(int index, int moduleNum) const;
121 
122  /** Convert CSwordKey into index. */
123  int keyToIndex(CSwordKey const & key) const;
124 
125  /** Convert CSwordVerseKey into index(row). */
126  int verseKeyToIndex(const CSwordVerseKey& key) const;
127 
128  /** Get reference name from index(row). */
129  QString indexToKeyName(int index) const;
130 
131  /** Get index offset of first entry of module */
132  int firstEntryIndex() const noexcept { return m_firstEntry; }
133 
134  /** Reimplemented from QAbstractItemModel. */
135  int columnCount(const QModelIndex & parent = QModelIndex()) const override;
136 
137  /** Reimplemented from QAbstractItemModel. */
138  QVariant data(const QModelIndex & index,
139  int role = Qt::DisplayRole) const override;
140 
141  /** Reimplemented from QAbstractItemModel. */
142  int rowCount(const QModelIndex & parent = QModelIndex()) const override;
143 
144  /** Reimplemented from QAbstractItemModel. */
145  virtual bool setData(const QModelIndex &index,
146  const QVariant &value, int role = Qt::EditRole) override;
147 
148  void setDisplayOptions(const DisplayOptions & displayOptions);
149 
150  /** Set the state of the currently found word functionality */
151  void setFindState(std::optional<FindState> findState);
152 
153  /** Set the color of word that are highlighted */
154  void setHighlightWords(const QString& highlightWords, bool caseSensitive);
155 
156  /** Used by model to get the roleNames and corresponding role numbers. */
157  QHash<int, QByteArray> roleNames() const override;
158 
159  /** Set the filter options used for rendering module text. */
160  void setFilterOptions(FilterOptions filterOptions);
161 
162  /** Specifies one or more module names for use by the model */
163  void setModules(const QStringList& modules);
164 
165  /** Load module pointers from module names */
166  void reloadModules();
167 
168  /** Set the text options used for rendering module text. */
169  void setTextFilter(BtModuleTextFilter * textFilter);
170 
171 private:
172 
173  CSwordTreeKey indexToBookKey(int index) const;
174 
175  bool isBible() const;
176  bool isBook() const;
177  bool isCommentary() const;
178  bool isLexicon() const;
179  bool isSelected(int index) const;
180 
181  /** returns text string for each model index */
182  QString bookData(const QModelIndex & index, int role = Qt::DisplayRole) const;
183  QString verseData(const QModelIndex & index, int role = Qt::DisplayRole) const;
184  QString lexiconData(const QModelIndex & index, int role = Qt::DisplayRole) const;
185 
187  QStringList m_modules;
189 
194  std::optional<FindState> m_findState;
195 };
QList< CSwordModuleInfo const * > BtConstModuleList
Definition: btmodulelist.h:21
virtual QString processText(const QString &text)=0
virtual ~BtModuleTextFilter()=0
Model that represents the entire text of a given module.
QString bookData(const QModelIndex &index, int role=Qt::DisplayRole) const
CSwordKey * indexToKey(int index, int moduleNum) const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void setDisplayOptions(const DisplayOptions &displayOptions)
int firstEntryIndex() const noexcept
std::optional< FindState > m_findState
int verseKeyToIndex(const CSwordVerseKey &key) const
int columnCount(const QModelIndex &parent=QModelIndex()) const override
CSwordTreeKey indexToBookKey(int index) const
BtConstModuleList m_moduleInfoList
BtModuleTextModel(QObject *parent=nullptr)
QString lexiconData(const QModelIndex &index, int role=Qt::DisplayRole) const
void setModules(const QStringList &modules)
void setHighlightWords(const QString &highlightWords, bool caseSensitive)
int keyToIndex(CSwordKey const &key) const
bool isSelected(int index) const
int indexToVerse(int index) const
void setFilterOptions(FilterOptions filterOptions)
CSwordVerseKey indexToVerseKey(int index) const
QString verseData(const QModelIndex &index, int role=Qt::DisplayRole) const
QString indexToKeyName(int index) const
void setTextFilter(BtModuleTextFilter *textFilter)
QHash< int, QByteArray > roleNames() const override
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
BtModuleTextFilter * m_textFilter
Rendering::CDisplayRendering m_displayRendering
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
void setFindState(std::optional< FindState > findState)
CSwordKey implementation for Sword's TreeKey.
Definition: cswordtreekey.h:43
CSwordKey implementation for Sword's VerseKey.
Rendering for the html display widget.