Skip to content

Commit

Permalink
Changed grouping_proxy_model to use SearchProxyModel
Browse files Browse the repository at this point in the history
Regex search in search_proxy_model::isMatching now works
  • Loading branch information
HerrKermet committed Oct 10, 2023
1 parent 5dc1a60 commit 477f500
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
4 changes: 3 additions & 1 deletion plugins/gui/include/gui/grouping/grouping_proxy_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "gui/gui_utils/sort.h"

#include <QSortFilterProxyModel>
#include <gui/searchbar/search_proxy_model.h>

namespace hal
{
Expand All @@ -39,7 +40,7 @@ namespace hal
* GroupingTableModel (source model) to support data filtering.
* It is used to provide a search bar (<i>CTRL+F</i>) for groupings.
*/
class GroupingProxyModel : public QSortFilterProxyModel
class GroupingProxyModel : public SearchProxyModel
{
Q_OBJECT

Expand All @@ -64,6 +65,7 @@ namespace hal
* @param sortMechanism - the new sorting mechanism
*/
void setSortMechanism(gui_utility::mSortMechanism sortMechanism);
void startSearch(QString text, int options) override;

protected:
/**
Expand Down
1 change: 1 addition & 0 deletions plugins/gui/include/gui/searchbar/search_proxy_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace hal

protected:
SearchOptions mSearchOptions;
QString mSearchString;
};
}

3 changes: 2 additions & 1 deletion plugins/gui/src/grouping/grouping_manager_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ namespace hal
mContentLayout->addWidget(mSearchbar);

mSearchbar->hide();
mSearchbar->setColumnNames(mProxyModel->getColumnNames());

connect(mSearchbar, &Searchbar::textEdited, this, &GroupingManagerWidget::filter);
connect(mSearchbar, &Searchbar::triggerNewSearch, mProxyModel, &GroupingProxyModel::startSearch);
connect(mSearchbar, &Searchbar::textEdited, this, &GroupingManagerWidget::updateSearchIcon);

connect(mNewGroupingAction, &QAction::triggered, this, &GroupingManagerWidget::handleCreateGroupingClicked);
Expand Down
37 changes: 27 additions & 10 deletions plugins/gui/src/grouping/grouping_proxy_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,36 @@

namespace hal
{
GroupingProxyModel::GroupingProxyModel(QObject* parent) : QSortFilterProxyModel(parent), mSortMechanism(gui_utility::mSortMechanism::natural)
GroupingProxyModel::GroupingProxyModel(QObject* parent) : SearchProxyModel(parent), mSortMechanism(gui_utility::mSortMechanism::natural)
{

}

bool GroupingProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
{
if(filterRegularExpression().pattern().isEmpty())
return true;

QModelIndex source_index = sourceModel()->index(sourceRow, filterKeyColumn(), sourceParent);
if(source_index.isValid())
QList<int> columns = mSearchOptions.getColumns();
if(columns.empty()){
//iterate over each column
for(int index = 0; index < 3; index++){
QString entry = sourceModel()->index(sourceRow, index, sourceParent).data().toString();
if(isMatching(mSearchString, entry))
return true;

}
return false;
}else
{
if (sourceModel()->data(source_index, filterRole()).toString().contains(filterRegularExpression()))
return true;
else
return false;
for(int index : columns)
{
QString entry = sourceModel()->index(sourceRow, index, sourceParent).data().toString();
qInfo() << "Entry: " << entry;
if(isMatching(mSearchString, entry))
return true;
}
return false;
}

return true;
}

bool GroupingProxyModel::lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const
Expand Down Expand Up @@ -57,4 +68,10 @@ namespace hal
mSortMechanism = sortMechanism;
invalidate();
}
void GroupingProxyModel::startSearch(QString text, int options)
{
mSearchString = text;
mSearchOptions = SearchOptions(options);
invalidateFilter();
}
}
2 changes: 2 additions & 0 deletions plugins/gui/src/grouping/grouping_table_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ namespace hal {
return gte.name();
case 1:
return gte.id();
case 2:
return gte.color().name();
}
default:
break;
Expand Down
5 changes: 1 addition & 4 deletions plugins/gui/src/searchbar/search_proxy_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace hal {
}
bool SearchProxyModel::isMatching(const QString searchString, const QString stringToCheck) const
{
if(!mSearchOptions.isExactMatch()){
if(!mSearchOptions.isExactMatch() && !mSearchOptions.isRegularExpression()){
//check if stringToCheck contains the searchString
return stringToCheck.contains(searchString, mSearchOptions.isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive);
}
Expand All @@ -24,9 +24,6 @@ namespace hal {
}
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();
}
Expand Down

0 comments on commit 477f500

Please sign in to comment.