HAL
search_proxy_model.cpp
Go to the documentation of this file.
2 #include <QDebug>
3 
4 namespace hal {
6  {
7  mSearchOptions = SearchOptions(8); // ? search only first column ?
8  }
9 
10  void SearchProxyModel::startSearch(QString text, int options)
11  {
12  mSearchString = text;
13  mSearchOptions = SearchOptions(options);
14  }
15 
16  bool SearchProxyModel::isMatching(const QString searchString, const QString stringToCheck) const
17  {
19  //check if stringToCheck contains the searchString
20  return stringToCheck.contains(searchString, mSearchOptions.isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive);
21  }
22  else if(mSearchOptions.isExactMatch()){
23  //check if the stringToCheck is the same as the searchString - also checks CaseSensitivity
24 
25  return 0 == QString::compare(searchString, stringToCheck, mSearchOptions.isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive);
26  }
28  //checks if the stringToCheck matches the regEx given by searchString
30  return regEx.match(stringToCheck).hasMatch();
31  }
32  return false;
33  }
34 
36  {
38  for(int column = 0; column < this->columnCount(); column++){
39  QVariant header = this->headerData(column, Qt::Horizontal, Qt::DisplayRole);
40  if(header.isValid() && header.canConvert<QString>()){
41  list.append(header.toString());
42  }
43  }
44  return list;
45  }
46 
47  bool SearchProxyModel::checkRowRecursion(int sourceRow, const QModelIndex &sourceParent,
48  int startIndex, int endIndex, int offset) const
49  {
50  if (checkRow(sourceRow, sourceParent, startIndex, endIndex, offset))
51  return true;
52  QModelIndex currentIndex = sourceModel()->index(sourceRow, 0, sourceParent);
53  int nrows = sourceModel()->rowCount(currentIndex);
54  for (int irow = 0; irow < nrows; irow++)
55  {
56  if (checkRowRecursion(irow, currentIndex, startIndex, endIndex, offset))
57  return true;
58  }
59  return false;
60  }
61 
62  bool SearchProxyModel::checkRow(int sourceRow, const QModelIndex& sourceParent, int startIndex, int endIndex, int offset) const
63  {
65  if(columns.empty()){
66  //iterate over each column
67  for(int index = startIndex; index <= endIndex; index++){
68  QString entry = sourceModel()->index(sourceRow, index, sourceParent).data().toString();
69  if(isMatching(mSearchString, entry))
70  {
71  return true;
72  }
73  }
74  return false;
75  }
76  else
77  {
78  for(int index : columns)
79  {
80  QString entry = sourceModel()->index(sourceRow, index + offset, sourceParent).data().toString();
81  if(isMatching(mSearchString, entry))
82  return true;
83  }
84  return false;
85  }
86 
87  }
88 
89 }
const QList< int > & getColumns() const
bool isExactMatch() const
bool isCaseSensitive() const
bool isRegularExpression() const
SearchOptions mSearchOptions
virtual void startSearch(QString text, int options)=0
bool checkRow(int sourceRow, const QModelIndex &sourceParent, int startIndex, int endIndex, int offset=0) const
Should be called inside filterAcceptsRow function and returns true if the source_row,...
virtual bool checkRowRecursion(int sourceRow, const QModelIndex &sourceParent, int startIndex, int endIndex, int offset=0) const
SearchProxyModel(QObject *parent=nullptr)
bool isMatching(const QString searchString, const QString stringToCheck) const
Check if a string matches the SearchOptions.
virtual QList< QString > getColumnNames()
void append(const T &value)
bool empty() const const
virtual int columnCount(const QModelIndex &parent) const const override
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
int compare(const QString &other, Qt::CaseSensitivity cs) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
CaseSensitive
DisplayRole
Horizontal
bool canConvert(int targetTypeId) const const
bool isValid() const const
QString toString() const const