Skip to content

Commit

Permalink
Changed filterAcceptsRow of context_table_proxy_model.cpp to work wit…
Browse files Browse the repository at this point in the history
…h SearchOptions

Implemented SearchProxyModel::isMatching() function to check if a string matches the search criteria specified by the SearchOptions
Removed the "m"-prefix from the getterMethods
  • Loading branch information
HerrKermet committed Sep 26, 2023
1 parent 1b91945 commit 1db3dd1
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ namespace hal
bool lessThan(const QModelIndex& left, const QModelIndex& right) const override;

private:
QRegularExpression mFilterExpression;
QString mSearchString;
};
} // namespace hal
14 changes: 13 additions & 1 deletion plugins/gui/include/gui/searchbar/search_proxy_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,23 @@ namespace hal
public:
SearchProxyModel(QObject* parent = nullptr);

/**
* @brief Check if a string matches the SearchOptions.
*
* This function checks whether a given `stringToCheck` matches the search criteria
* specified by `searchString` based on search options.
*
* @param searchString The search criteria to match against `stringToCheck`.
* @param stringToCheck The string to be checked for a match.
* @return True if `stringToCheck` matches the search criteria, otherwise false.
*/
bool isMatching(const QString searchString, const QString stringToCheck) const;

public Q_SLOTS:
virtual void startSearch(QString text, int options) = 0;

protected:
SearchOptions* mSearchOptions;
SearchOptions mSearchOptions;
};
}

8 changes: 4 additions & 4 deletions plugins/gui/include/gui/searchbar/searchoptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ namespace hal
int toInt() const;
static int toInt(bool exactMatch, bool caseSensitive, bool regEx, QList<int> columns);

bool isMExactMatch() const;
bool isMCaseSensitive() const;
bool isMRegularExpression() const;
const QList<int>& getMColumns() const;
bool isExactMatch() const;
bool isCaseSensitive() const;
bool isRegularExpression() const;
const QList<int>& getColumns() const;

private:
bool mExactMatch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,33 @@ namespace hal
{
ContextTableProxyModel::ContextTableProxyModel(QObject* parent) : SearchProxyModel(parent)
{
mFilterExpression.setPatternOptions(QRegularExpression::CaseInsensitiveOption);

}

bool ContextTableProxyModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
{
QList<int> columns = mSearchOptions->getMColumns();
QList<int> columns = mSearchOptions.getColumns();


//TODO get column count
if(columns.empty()){
//iterate over each column
for(int index = 0; index < 2; index++){
QString entry = sourceModel()->index(source_row, index, source_parent).data().toString();
if(mFilterExpression.match(entry).hasMatch())
qInfo() << "Checking " << entry;
if(isMatching(mSearchString, entry))
{
qInfo() << "true";
return true;
}
}
return false;
}else
{
for(int index : columns)
{
QString entry = sourceModel()->index(source_row, index, source_parent).data().toString();
if(mFilterExpression.match(entry).hasMatch())
if(SearchProxyModel::isMatching(mSearchString, entry));
return true;
}
return false;
Expand Down Expand Up @@ -64,7 +68,8 @@ namespace hal

void ContextTableProxyModel::startSearch(QString text, int options)
{
mFilterExpression.setPattern(text);
mSearchString = text;
mSearchOptions = SearchOptions(options);
invalidateFilter();
}
}
25 changes: 23 additions & 2 deletions plugins/gui/src/searchbar/search_proxy_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,34 @@
namespace hal {
SearchProxyModel::SearchProxyModel(QObject* parent): QSortFilterProxyModel(parent)
{
mSearchOptions = new SearchOptions();
mSearchOptions = SearchOptions();
}

void SearchProxyModel::startSearch(QString text, int options)
{
mSearchOptions = new SearchOptions(options);
mSearchOptions = SearchOptions(options);
qInfo() << "Proxy received searchOptions";
}
bool SearchProxyModel::isMatching(const QString searchString, const QString stringToCheck) const
{
if(!mSearchOptions.isExactMatch()){
//check if stringToCheck contains the searchString
return stringToCheck.contains(searchString, mSearchOptions.isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive);
}
else if(mSearchOptions.isExactMatch()){
//check if the stringToCheck is the same as the searchString - also checks CaseSensitivity

return 0 == QString::compare(searchString, stringToCheck, mSearchOptions.isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive);
}
else if(mSearchOptions.isRegularExpression()){
//checks if the stringToCheck matches the regEx given by searchString

//TODO regEx does not work

auto regEx = QRegularExpression(searchString, mSearchOptions.isCaseSensitive() ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption);
return regEx.match(stringToCheck).hasMatch();
}
return false;
}

}
2 changes: 1 addition & 1 deletion plugins/gui/src/searchbar/searchbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace hal

ensurePolished();

mCurrentOptions = new SearchOptions(9);//exact match and search in all columns is on
mCurrentOptions = new SearchOptions(8);//exact match and search in all columns is on
mIncrementalSearch = false;
mSearchIconLabel->setPixmap(gui_utility::getStyledSvgIcon(mSearchIconStyle, mSearchIcon).pixmap(QSize(16, 16)));
mSearchIconLabel->installEventFilter(this);
Expand Down
8 changes: 4 additions & 4 deletions plugins/gui/src/searchbar/searchoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,22 @@ namespace hal
mColumns = columns;
}

bool SearchOptions::isMExactMatch() const
bool SearchOptions::isExactMatch() const
{
return mExactMatch;
}

bool SearchOptions::isMCaseSensitive() const
bool SearchOptions::isCaseSensitive() const
{
return mCaseSensitive;
}

bool SearchOptions::isMRegularExpression() const
bool SearchOptions::isRegularExpression() const
{
return mRegularExpression;
}

const QList<int>& SearchOptions::getMColumns() const
const QList<int>& SearchOptions::getColumns() const
{
return mColumns;
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/gui/src/searchbar/searchoptions_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ namespace hal

void SearchOptionsDialog::setOptions(SearchOptions* opts, bool incSearch, int minIncSearch) const
{
mExactMatchBox->setChecked(opts->isMExactMatch());
mCaseSensitiveBox->setChecked(opts->isMCaseSensitive());
mRegExBox->setChecked(opts->isMRegularExpression());
mExactMatchBox->setChecked(opts->isExactMatch());
mCaseSensitiveBox->setChecked(opts->isCaseSensitive());
mRegExBox->setChecked(opts->isRegularExpression());
mIncrementalSearchBox->setChecked(incSearch);

mSpinBox->setValue(minIncSearch);
Expand Down

0 comments on commit 1db3dd1

Please sign in to comment.