Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
Highlight search results in form view, closes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
joshirio committed Nov 4, 2017
1 parent aaa86c3 commit eb5ebdf
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 0 deletions.
19 changes: 19 additions & 0 deletions views/formview/formview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1373,12 +1373,31 @@ void FormView::populateFields()
while (m->canFetchMore(index))
m->fetchMore(index);

//check if any filter aka search is active
QString activeSearchString = "";
QAbstractItemModel *imodel = this->model();
if (imodel) {
QSqlTableModel *sqlModel = static_cast<QSqlTableModel*> (imodel);
if (sqlModel) {
QString filter = sqlModel->filter();
if (!filter.isEmpty()) {
QString beforeOr = filter.split("OR").at(0);
QStringList wilds = beforeOr.split("%");
activeSearchString = wilds.at(1);
} else {
activeSearchString = "__no_active_filter_symphytum"; //avoid empty searches
}
}
}

FormWidget *fw;
int column = 1; //0 is ID, so start with 1
foreach (fw, m_formWidgetList) {
index = m->index(m_currentRow, column);
if (index.isValid()) {
fw->setData(index.data());
//highlight fields with results, if found
fw->showHighlightSearchResults(activeSearchString);
}
column++;
}
Expand Down
6 changes: 6 additions & 0 deletions widgets/form_widgets/abstractformwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ bool AbstractFormWidget::operator != (const AbstractFormWidget& other) const
{
return !(*this == other);
}

bool AbstractFormWidget::showHighlightSearchResults(const QString &searchString)
{
Q_UNUSED(searchString);
return false;
}
8 changes: 8 additions & 0 deletions widgets/form_widgets/abstractformwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ class AbstractFormWidget : public QWidget
*/
bool operator!= (const AbstractFormWidget& other) const;

/** Highlight the form widget or its contents if the searched string is matched.
* Default implementation returns false, implement if applicable.
* If no match is found, make sure to clear any highlighted state.
* @param searchString - the string to match against the data
* @return bool - if the data was found (matched) or not
*/
virtual bool showHighlightSearchResults(const QString& searchString);

signals:
/** Emitted when content data has been edited */
void dataEdited();
Expand Down
17 changes: 17 additions & 0 deletions widgets/form_widgets/emailformwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ QVariant EmailFormWidget::getData() const
return m_lineEdit->text();
}

bool EmailFormWidget::showHighlightSearchResults(const QString &searchString)
{
bool r = m_lineEdit->text().contains(searchString, Qt::CaseInsensitive);
QString highLightCSS = "QLabel {"
"background: yellow; }"
"QLineEdit { color: red; }";
QString currentStyleSheet = this->styleSheet();
if (r) {
if (!currentStyleSheet.contains(highLightCSS))
this->setStyleSheet(currentStyleSheet.append(highLightCSS));
} else {
this->setStyleSheet(currentStyleSheet.remove(highLightCSS));
}

return r;
}

void EmailFormWidget::loadMetadataDisplayProperties(const QString &metadata)
{
MetadataPropertiesParser parser(metadata);
Expand Down
1 change: 1 addition & 0 deletions widgets/form_widgets/emailformwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class EmailFormWidget : public AbstractFormWidget
void clearData();
void setData(const QVariant &data);
QVariant getData() const;
bool showHighlightSearchResults(const QString &searchString);

/**
* Supported display properties are:
Expand Down
17 changes: 17 additions & 0 deletions widgets/form_widgets/numberformwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ QVariant NumberFormWidget::getData() const
return ""; //empty value
}

bool NumberFormWidget::showHighlightSearchResults(const QString &searchString)
{
bool r = m_lineEdit->text().contains(searchString, Qt::CaseInsensitive);
QString highLightCSS = "QLabel {"
"background: yellow; }"
"QLineEdit { color: red; }";
QString currentStyleSheet = this->styleSheet();
if (r) {
if (!currentStyleSheet.contains(highLightCSS))
this->setStyleSheet(currentStyleSheet.append(highLightCSS));
} else {
this->setStyleSheet(currentStyleSheet.remove(highLightCSS));
}

return r;
}

void NumberFormWidget::loadMetadataDisplayProperties(const QString &metadata)
{
MetadataPropertiesParser parser(metadata);
Expand Down
1 change: 1 addition & 0 deletions widgets/form_widgets/numberformwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class NumberFormWidget : public AbstractFormWidget
void clearData();
void setData(const QVariant &data);
QVariant getData() const;
bool showHighlightSearchResults(const QString &searchString);

/**
* Supported display properties are:
Expand Down
40 changes: 40 additions & 0 deletions widgets/form_widgets/textformwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,46 @@ QVariant TextFormWidget::getData() const
return m_lineEdit->text();
}

bool TextFormWidget::showHighlightSearchResults(const QString &searchString)
{
bool r = getData().toString().contains(searchString, Qt::CaseInsensitive);
QString highLightCSS = "QLabel {"
"background: yellow; }"
"QLineEdit { color: red; }";
QString currentStyleSheet = this->styleSheet();
if (r) {
if (!currentStyleSheet.contains(highLightCSS))
this->setStyleSheet(currentStyleSheet.append(highLightCSS));

//highlight text area
QTextDocument *document = m_textArea->document();
QTextCursor highlightCursor(document);
QTextCursor cursor(document);

cursor.beginEditBlock();

QTextCharFormat plainFormat(highlightCursor.charFormat());
QTextCharFormat colorFormat = plainFormat;
colorFormat.setForeground(Qt::red);

while (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
highlightCursor = document->find(searchString, highlightCursor);

if (!highlightCursor.isNull()) {
highlightCursor.movePosition(QTextCursor::WordRight,
QTextCursor::KeepAnchor);
highlightCursor.mergeCharFormat(colorFormat);
}
}

cursor.endEditBlock();
} else {
this->setStyleSheet(currentStyleSheet.remove(highLightCSS));
}

return r;
}

void TextFormWidget::loadMetadataDisplayProperties(const QString &metadata)
{
MetadataPropertiesParser parser(metadata);
Expand Down
1 change: 1 addition & 0 deletions widgets/form_widgets/textformwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TextFormWidget : public AbstractFormWidget
void clearData();
void setData(const QVariant &data);
QVariant getData() const;
bool showHighlightSearchResults(const QString &searchString);

/**
* Supported display properties are:
Expand Down
17 changes: 17 additions & 0 deletions widgets/form_widgets/urlformwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ QVariant URLFormWidget::getData() const
return m_lineEdit->text();
}

bool URLFormWidget::showHighlightSearchResults(const QString &searchString)
{
bool r = m_lineEdit->text().contains(searchString, Qt::CaseInsensitive);
QString highLightCSS = "QLabel {"
"background: yellow; }"
"QLineEdit { color: red; }";
QString currentStyleSheet = this->styleSheet();
if (r) {
if (!currentStyleSheet.contains(highLightCSS))
this->setStyleSheet(currentStyleSheet.append(highLightCSS));
} else {
this->setStyleSheet(currentStyleSheet.remove(highLightCSS));
}

return r;
}

void URLFormWidget::loadMetadataDisplayProperties(const QString &metadata)
{
MetadataPropertiesParser parser(metadata);
Expand Down
1 change: 1 addition & 0 deletions widgets/form_widgets/urlformwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class URLFormWidget : public AbstractFormWidget
void clearData();
void setData(const QVariant &data);
QVariant getData() const;
bool showHighlightSearchResults(const QString &searchString);

/**
* Supported display properties are:
Expand Down

0 comments on commit eb5ebdf

Please sign in to comment.