Skip to content

Commit

Permalink
Moved filtering logic from multiple filterAcceptsRow functions into a…
Browse files Browse the repository at this point in the history
… centralized checkRow method
  • Loading branch information
HerrKermet committed Nov 7, 2023
1 parent 9c29904 commit ba9d45d
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,5 @@ namespace hal
*/
bool lessThan(const QModelIndex& left, const QModelIndex& right) const override;

private:
QString mSearchString;
};
} // namespace hal
3 changes: 2 additions & 1 deletion plugins/gui/include/gui/module_dialog/module_select_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ namespace hal {
void setSortMechanism(gui_utility::mSortMechanism sortMechanism);
void searchTextChanged(const QString& txt);
void startSearch(QString text, int options) override;
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;

protected:
static bool lessThan(const QColor& a, const QColor& b);
bool lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const override;
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;


private:
gui_utility::mSortMechanism mSortMechanism;
Expand Down
30 changes: 22 additions & 8 deletions plugins/gui/include/gui/searchbar/search_proxy_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ namespace hal
public:
SearchProxyModel(QObject* parent = nullptr);

/**
* @return List of QString matching the corresponding column header.
*/
virtual QList<QString> getColumnNames();

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

protected:
/**
* @brief Check if a string matches the SearchOptions.
*
Expand All @@ -48,16 +57,21 @@ namespace hal
*/
bool isMatching(const QString searchString, const QString stringToCheck) const;


/**
* @return List of QString matching the corresponding column header.
* @brief Should be called inside filterAcceptsRow function and returns true if the source_row, source_parent matches given SearchOptions
*
* This function checks whether a given `source_row` should be shown
* based on given `search options` .
*
* @param sourceRow The row index in the source model to be checked.
* @param sourceParent The QModelIndex representing the parent of the source model.
* @param startIndex The index of the first column to start checking.
* @param endIndex The index of the last column to stop checking.
* @param offset The offset to apply to the column indices based on search options if specific columns are checked.
*
* @return True if the row contains a matching entry, otherwise false.
*/
virtual QList<QString> getColumnNames();

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

protected:
bool checkRow(int sourceRow, const QModelIndex& sourceParent, int startIndex, int endIndex, int offset = 0) const;
SearchOptions mSearchOptions;
QString mSearchString;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,7 @@ namespace hal
{

//TODO somehow the program crashes with segfault if this method ever returns a false at the initial start

QList<int> columns = mSearchOptions.getColumns();
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(isMatching(mSearchString, entry))
{
return true;
}
}
return false;
}else
{
for(int index : columns)
{
QString entry = sourceModel()->index(source_row, index, source_parent).data().toString();
if(isMatching(mSearchString, entry))
return true;
}
return false;
}

return checkRow(source_row, source_parent, 0, 1);
}

bool ContextTableProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
Expand Down
26 changes: 5 additions & 21 deletions plugins/gui/src/grouping/grouping_proxy_model.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@




#include "gui/grouping/grouping_proxy_model.h"

#include "gui/gui_globals.h"
Expand All @@ -11,27 +15,7 @@ namespace hal

bool GroupingProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
{
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
{
for(int index : columns)
{
QString entry = sourceModel()->index(sourceRow, index, sourceParent).data().toString();
if(isMatching(mSearchString, entry))
return true;
}
return false;
}

return checkRow(sourceRow, sourceParent, 0, 2);
}

bool GroupingProxyModel::lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const
Expand Down
1 change: 1 addition & 0 deletions plugins/gui/src/module_dialog/gate_dialog.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#include "gui/module_dialog/gate_dialog.h"
#include "gui/module_dialog/module_select_model.h"
#include "gui/gui_globals.h"
Expand Down
23 changes: 2 additions & 21 deletions plugins/gui/src/module_dialog/gate_select_model.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#include "gui/module_dialog/gate_select_model.h"

#include "gui/content_manager/content_manager.h"
Expand Down Expand Up @@ -165,27 +166,7 @@ namespace hal

bool GateSelectProxy::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
{
QList<int> columns = mSearchOptions.getColumns();
if(columns.empty()){
//iterate over each column
for(int index = 0; index < 3; index++){
QString entry = sourceModel()->index(source_row, index, source_parent).data().toString();
if(isMatching(mSearchString, entry))
{
return true;
}
}
return false;
}else
{
for(int index : columns)
{
QString entry = sourceModel()->index(source_row, index, source_parent).data().toString();
if(isMatching(mSearchString, entry))
return true;
}
return false;
}
return checkRow(source_row, source_parent, 0, 2);
}

//---------------- PICKER -----------------------------------------
Expand Down
25 changes: 2 additions & 23 deletions plugins/gui/src/module_dialog/module_select_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,36 +201,15 @@ namespace hal
setFilterRegularExpression(txt);
}
void ModuleSelectProxy::startSearch(QString text, int options)
{
{
mSearchString = text;
mSearchOptions = SearchOptions(options);
invalidateFilter();
}

bool ModuleSelectProxy::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
{
QList<int> columns = mSearchOptions.getColumns();
if(columns.empty()){
//iterate over each column
for(int index = 1; index < 4; index++){
QString entry = sourceModel()->index(source_row, index, source_parent).data().toString();
if(isMatching(mSearchString, entry))
{
return true;
}
}
return false;
}else
{
for(int index : columns)
{
//offset the index by one to account for missing color column
QString entry = sourceModel()->index(source_row, index + 1, source_parent).data().toString();
if(isMatching(mSearchString, entry))
return true;
}
return false;
}
return checkRow(source_row, source_parent, 1, 3, 1);
}

//---------------- EXCLUDE ----------------------------------------
Expand Down
26 changes: 26 additions & 0 deletions plugins/gui/src/searchbar/search_proxy_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,30 @@ namespace hal {
return list;
}

bool SearchProxyModel::checkRow(int sourceRow, const QModelIndex& sourceParent, int startIndex, int endIndex, int offset) const
{
QList<int> columns = mSearchOptions.getColumns();
if(columns.empty()){
//iterate over each column
for(int index = startIndex; index <= endIndex; index++){
QString entry = sourceModel()->index(sourceRow, index, sourceParent).data().toString();
if(isMatching(mSearchString, entry))
{
return true;
}
}
return false;
}else
{
for(int index : columns)
{
QString entry = sourceModel()->index(sourceRow, index + offset, sourceParent).data().toString();
if(isMatching(mSearchString, entry))
return true;
}
return false;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,7 @@ namespace hal

bool SelectionTreeProxyModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
{
QList<int> columns = mSearchOptions.getColumns();
if(columns.empty()){
//iterate over each column
for(int index = 0; index < 3; index++){
QString entry = sourceModel()->index(source_row, index, source_parent).data().toString();
if(isMatching(mSearchString, entry))
{
return true;
}
}
return false;
}else
{
for(int index : columns)
{
QString entry = sourceModel()->index(source_row, index, source_parent).data().toString();
if(isMatching(mSearchString, entry))
return true;
}
return false;
}
return checkRow(source_row, source_parent, 0, 2);
}

bool SelectionTreeProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
Expand Down

0 comments on commit ba9d45d

Please sign in to comment.