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-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 #include "btbookshelffiltermodel.h"
14 
15 #include <QAbstractItemModel>
16 #include <QVariant>
17 #include "../../util/btassert.h"
18 #include "../drivers/cswordmoduleinfo.h"
19 #include "btbookshelfmodel.h"
20 
21 
23  : QSortFilterProxyModel(parent)
24  , m_enabled(true)
25  , m_nameFilterRole(BtBookshelfModel::ModuleNameRole)
26  , m_nameFilterColumn(0)
27  , m_nameFilterCase(Qt::CaseInsensitive)
28  , m_hiddenFilterRole(BtBookshelfModel::ModuleHiddenRole)
29  , m_hiddenFilterColumn(0)
30  , m_showHidden(false)
31  , m_showShown(true)
32 { setDynamicSortFilter(true); }
33 
34 void BtBookshelfFilterModel::setEnabled(bool const enable) {
35  if (enable == m_enabled)
36  return;
37  m_enabled = enable;
38  invalidateFilter();
39 }
40 
41 // Name filter:
42 
44  if (m_nameFilterRole == role)
45  return;
46  m_nameFilterRole = role;
47  invalidateFilter();
48 }
49 
51  if (m_nameFilterColumn == column)
52  return;
53  m_nameFilterColumn = column;
54  invalidateFilter();
55 }
56 
58  if (m_nameFilter == filter)
59  return;
61  invalidateFilter();
62 }
63 
64 void BtBookshelfFilterModel::setNameFilterCase(Qt::CaseSensitivity const value){
65  if (m_nameFilterCase == value)
66  return;
67  m_nameFilterCase = value;
68  invalidateFilter();
69 }
70 
71 // Hidden filter:
72 
74  if (m_hiddenFilterRole == role)
75  return;
76  m_hiddenFilterRole = role;
77  invalidateFilter();
78 }
79 
81  if (m_hiddenFilterColumn == column)
82  return;
83  m_hiddenFilterColumn = column;
84  invalidateFilter();
85 }
86 
88  if (m_showHidden == show)
89  return;
90  m_showHidden = show;
91  invalidateFilter();
92 }
93 
94 void BtBookshelfFilterModel::setShowShown(bool const show) {
95  if (m_showShown == show)
96  return;
97  m_showShown = show;
98  invalidateFilter();
99 }
100 
101 // Filtering:
102 
104  QModelIndex const & parent) const
105 {
106  if (!m_enabled)
107  return true;
108 
109  if (!hiddenFilterAcceptsRow(row, parent))
110  return false;
111  if (!nameFilterAcceptsRow(row, parent))
112  return false;
113  return true;
114 }
115 
117  int row,
118  QModelIndex const & parent) const
119 {
120  if (m_nameFilter.isEmpty())
121  return true;
122 
123  auto const * const m = sourceModel();
124  BT_ASSERT(m);
125 
126  auto const itemIndex = m->index(row, m_nameFilterColumn, parent);
127  auto const numChildren = m->rowCount(itemIndex);
128  if (numChildren == 0) {
129  auto const data = m->data(itemIndex, m_nameFilterRole);
130  return data.toString().contains(m_nameFilter, m_nameFilterCase);
131  }
132 
133  for (int i = 0; i < numChildren; ++i)
134  if (filterAcceptsRow(i, itemIndex))
135  return true;
136  return false;
137 }
138 
140  int row,
141  QModelIndex const & parent) const
142 {
143  if (m_showHidden && m_showShown)
144  return true;
145 
146  auto const * const m = sourceModel();
147  BT_ASSERT(m);
148 
149  auto const itemIndex = m->index(row, m_hiddenFilterColumn, parent);
150  auto const numChildren = m->rowCount(itemIndex);
151  if (numChildren == 0) {
152  if (static_cast<Qt::CheckState>(m->data(itemIndex,
153  m_hiddenFilterRole).toBool()))
154  return m_showHidden;
155  return m_showShown;
156  }
157 
158  for (int i = 0; i < numChildren; ++i)
159  if (filterAcceptsRow(i, itemIndex))
160  return true;
161  return false;
162 }
#define BT_ASSERT(...)
Definition: btassert.h:17
void setNameFilterKeyColumn(int column)
bool nameFilterAcceptsRow(int row, QModelIndex const &parent) const
void setHiddenFilterKeyColumn(int column)
BtBookshelfFilterModel(QObject *parent=nullptr)
bool filterAcceptsRow(int row, QModelIndex const &parent) const override
bool hiddenFilterAcceptsRow(int row, QModelIndex const &parent) const
void setNameFilterCase(Qt::CaseSensitivity value)
Qt::CaseSensitivity m_nameFilterCase
void setNameFilterFixedString(QString const &nameFilter)
bool filter(WizardTaskType const taskType, QStringList const &languages, CSwordModuleInfo const *const mInfo)