BibleTime
btbookshelffiltermodel.cpp
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-2026 by the BibleTime developers.
8* The BibleTime source code is licensed under the GNU General Public License
9* version 2.0.
10*
11**********/
12
14
15#include <utility>
16#include "../../util/btassert.h"
17#include "btbookshelfmodel.h"
18
19
21 : QSortFilterProxyModel(parent)
22 , m_showHidden(false)
23{ setDynamicSortFilter(true); }
24
26BtBookshelfFilterModel::module(QModelIndex const & index) const noexcept {
27 return static_cast<CSwordModuleInfo *>(
28 data(index,
30}
31
32template <typename Field, typename Value>
33void BtBookshelfFilterModel::changeFilter(Field & field, Value && value) {
34 if (field == value)
35 return;
36 #if (QT_VERSION >= QT_VERSION_CHECK(6, 9, 0))
37 beginFilterChange();
38 #endif
39 field = std::forward<Value>(value);
40 #if (QT_VERSION >= QT_VERSION_CHECK(6, 10, 0))
41 endFilterChange(QSortFilterProxyModel::Direction::Rows);
42 #else
43 invalidateFilter(); // together with beginFilterChange() in Qt 6.9
44 #endif
45}
46
49
52
54 std::optional<CSwordModuleInfo::ModuleType> type)
56
58 QModelIndex const & parent) const
59{
60 auto const * const m = sourceModel();
61 BT_ASSERT(m);
62
63 auto const itemIndex = m->index(row, 0, parent);
64 auto const numChildren = m->rowCount(itemIndex);
65 if (numChildren == 0) {
66 if (!m_nameFilter.isEmpty()) {
67 auto const data =
68 m->data(itemIndex, BtBookshelfModel::ModuleNameRole);
69 if (!data.toString().contains(m_nameFilter, Qt::CaseInsensitive))
70 return false;
71 }
72 if (!m_showHidden) {
73 auto const isHidden =
74 static_cast<Qt::CheckState>(
75 m->data(itemIndex,
77 if (isHidden)
78 return false;
79 }
80 if (m_moduleChooserType.has_value()) {
81 static constexpr auto const MPR =
83 auto * const module =
84 static_cast<CSwordModuleInfo *>(
85 m->data(itemIndex, MPR).value<void *>());
87 auto const moduleType = module->type();
88 if ((moduleType != *m_moduleChooserType)
90 || (moduleType != CSwordModuleInfo::Commentary)))
91 return false;
92 }
93 return true;
94 }
95
96 for (int i = 0; i < numChildren; ++i)
97 if (filterAcceptsRow(i, itemIndex))
98 return true;
99 return false;
100}
#define BT_ASSERT(...)
Definition btassert.h:17
std::optional< CSwordModuleInfo::ModuleType > m_moduleChooserType
CSwordModuleInfo * module(QModelIndex const &index) const noexcept
void changeFilter(Field &field, Value &&value)
BtBookshelfFilterModel(QObject *parent=nullptr)
bool filterAcceptsRow(int row, QModelIndex const &parent) const override
void setNameFilterFixedString(QString const &nameFilter)
void setModuleChooserType(std::optional< CSwordModuleInfo::ModuleType > type)