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
This commit is contained in:
Wren Turkal 2019-01-06 01:25:12 -08:00
parent 42b00c5c58
commit 71f7f40875
2 changed files with 54 additions and 26 deletions

View File

@ -29,6 +29,7 @@
#include "vtablesearch.h" #include "vtablesearch.h"
#include <QPalette> #include <QPalette>
#include <QStringBuilder>
#include <QTableWidget> #include <QTableWidget>
#include <QTableWidgetItem> #include <QTableWidgetItem>
#include <Qt> #include <Qt>
@ -92,6 +93,40 @@ void VTableSearch::ShowNext(int newIndex)
} }
} }
QList<QTableWidgetItem *> VTableSearch::FindTableItems(QString term)
{
if (term.isEmpty())
{
return QList<QTableWidgetItem *>();
}
if (term.startsWith("/"))
{
QRegularExpression qre("^/(?<searchType>[^/]+)/(?<searchString>.+)$");
QScopedPointer<QRegularExpressionMatch> match(new QRegularExpressionMatch());
if (!term.contains(qre, match.data()))
{
return QList<QTableWidgetItem *>();
}
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<QTableWidgetItem *>();
}
}
else
{
return table->findItems(term, Qt::MatchContains);
}
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VTableSearch::Find(const QString &term) void VTableSearch::Find(const QString &term)
{ {
@ -99,24 +134,21 @@ void VTableSearch::Find(const QString &term)
Clear(); Clear();
if (not term.isEmpty()) searchList = FindTableItems(term);
if (not searchList.isEmpty())
{ {
searchList = table->findItems(term, Qt::MatchContains); for (auto item : qAsConst(searchList))
if (not searchList.isEmpty())
{ {
for (auto item : qAsConst(searchList)) item->setBackground(Qt::yellow);
{
item->setBackground(Qt::yellow);
}
searchIndex = 0;
QTableWidgetItem *item = searchList.at(searchIndex);
item->setBackground(Qt::red);
table->scrollToItem(item);
emit HasResult(true);
} }
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) SCASSERT(table != nullptr)
if (term.isEmpty()) searchList = FindTableItems(term);
{
return;
}
searchList = table->findItems(term, Qt::MatchContains);
for (auto item : qAsConst(searchList))
{
item->setBackground(Qt::yellow);
}
if (not searchList.isEmpty()) if (not searchList.isEmpty())
{ {
for (auto item : qAsConst(searchList))
{
item->setBackground(Qt::yellow);
}
if (searchIndex < 0) if (searchIndex < 0)
{ {
searchIndex = searchList.size() - 1; searchIndex = searchList.size() - 1;

View File

@ -60,6 +60,7 @@ private:
void Clear(); void Clear();
void ShowNext(int newIndex); void ShowNext(int newIndex);
QList<QTableWidgetItem *> FindTableItems(QString term);
}; };
#endif // VTABLESEARCH_H #endif // VTABLESEARCH_H