From 71f7f40875e00ff09c21c2569096a32edb8e8b1a Mon Sep 17 00:00:00 2001 From: Wren Turkal Date: Sun, 6 Jan 2019 01:25:12 -0800 Subject: [PATCH] Add ability to search measurements by regex. You do this by prepending "/r/" to the front of the search string. This makes it much easier to find measurements when one has a lot of measurements for a given individual. I also refactored that common table search code to consolidate this functionality and make it easier to add other search methods in the future. --HG-- branch : develop --- src/libs/vmisc/vtablesearch.cpp | 79 ++++++++++++++++++++++----------- src/libs/vmisc/vtablesearch.h | 1 + 2 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/libs/vmisc/vtablesearch.cpp b/src/libs/vmisc/vtablesearch.cpp index f683a2b8b..82df0700e 100644 --- a/src/libs/vmisc/vtablesearch.cpp +++ b/src/libs/vmisc/vtablesearch.cpp @@ -29,6 +29,7 @@ #include "vtablesearch.h" #include +#include #include #include #include @@ -92,6 +93,40 @@ void VTableSearch::ShowNext(int newIndex) } } +QList VTableSearch::FindTableItems(QString term) +{ + if (term.isEmpty()) + { + return QList(); + } + + if (term.startsWith("/")) + { + QRegularExpression qre("^/(?[^/]+)/(?.+)$"); + QScopedPointer match(new QRegularExpressionMatch()); + if (!term.contains(qre, match.data())) + { + return QList(); + } + + auto searchType = match->capturedRef("searchType"); + auto searchString = match->capturedRef("searchString"); + if (searchType == "r") + { + QString reSearchString = ".*" % searchString % ".*"; + return table->findItems(reSearchString, Qt::MatchRegExp); + } + else + { + return QList(); + } + } + else + { + return table->findItems(term, Qt::MatchContains); + } +} + //--------------------------------------------------------------------------------------------------------------------- void VTableSearch::Find(const QString &term) { @@ -99,24 +134,21 @@ void VTableSearch::Find(const QString &term) Clear(); - if (not term.isEmpty()) + searchList = FindTableItems(term); + + if (not searchList.isEmpty()) { - searchList = table->findItems(term, Qt::MatchContains); - - if (not searchList.isEmpty()) + for (auto item : qAsConst(searchList)) { - for (auto item : qAsConst(searchList)) - { - item->setBackground(Qt::yellow); - } - - searchIndex = 0; - QTableWidgetItem *item = searchList.at(searchIndex); - item->setBackground(Qt::red); - table->scrollToItem(item); - - emit HasResult(true); + item->setBackground(Qt::yellow); } + + searchIndex = 0; + QTableWidgetItem *item = searchList.at(searchIndex); + item->setBackground(Qt::red); + table->scrollToItem(item); + + emit HasResult(true); } } @@ -195,20 +227,15 @@ void VTableSearch::RefreshList(const QString &term) { SCASSERT(table != nullptr) - if (term.isEmpty()) - { - return; - } - - searchList = table->findItems(term, Qt::MatchContains); - - for (auto item : qAsConst(searchList)) - { - item->setBackground(Qt::yellow); - } + searchList = FindTableItems(term); if (not searchList.isEmpty()) { + for (auto item : qAsConst(searchList)) + { + item->setBackground(Qt::yellow); + } + if (searchIndex < 0) { searchIndex = searchList.size() - 1; diff --git a/src/libs/vmisc/vtablesearch.h b/src/libs/vmisc/vtablesearch.h index 243f4a44d..18c1eba6d 100644 --- a/src/libs/vmisc/vtablesearch.h +++ b/src/libs/vmisc/vtablesearch.h @@ -60,6 +60,7 @@ private: void Clear(); void ShowNext(int newIndex); + QList FindTableItems(QString term); }; #endif // VTABLESEARCH_H