Skip to content
Permalink
Browse files

[FEATURE] Add "starts with" and "ends with" to multi attribute search

  • Loading branch information
m-kuhn committed Sep 5, 2017
1 parent 655f121 commit 8efa9fa815e1437432a44ed00ea51f063da27014
@@ -90,6 +90,8 @@ class QgsSearchWidgetWrapper : QgsWidgetWrapper
IsNull,
IsNotBetween,
IsNotNull,
StartsWith,
EndsWith,
};
typedef QFlags<QgsSearchWidgetWrapper::FilterFlag> FilterFlags;

@@ -34,7 +34,9 @@ QList<QgsSearchWidgetWrapper::FilterFlag> QgsSearchWidgetWrapper::exclusiveFilte
<< Contains
<< DoesNotContain
<< IsNull
<< IsNotNull;
<< IsNotNull
<< StartsWith
<< EndsWith;
}

QList<QgsSearchWidgetWrapper::FilterFlag> QgsSearchWidgetWrapper::nonExclusiveFilterFlags()
@@ -73,7 +75,10 @@ QString QgsSearchWidgetWrapper::toString( QgsSearchWidgetWrapper::FilterFlag fla
return QObject::tr( "Is missing (null)" );
case IsNotNull:
return QObject::tr( "Is not missing (not null)" );

case StartsWith:
return QObject::tr( "Starts with" );
case EndsWith:
return QObject::tr( "Ends with" );
}
return QString();
}
@@ -109,6 +109,8 @@ class GUI_EXPORT QgsSearchWidgetWrapper : public QgsWidgetWrapper
IsNull = 1 << 11, //!< Supports searching for null values
IsNotBetween = 1 << 12, //!< Supports searching for values outside of a set range
IsNotNull = 1 << 13, //!< Supports searching for non-null values
StartsWith = 1 << 14, //!< Supports searching for strings that start with
EndsWith = 1 << 15, //!< Supports searching for strings that end with
};
Q_DECLARE_FLAGS( FilterFlags, FilterFlag )

@@ -109,7 +109,7 @@ QgsSearchWidgetWrapper::FilterFlags QgsDefaultSearchWidgetWrapper::supportedFlag
break;

case QVariant::String:
flags |= Contains | DoesNotContain;
flags |= Contains | DoesNotContain | StartsWith | EndsWith;
break;

default:
@@ -213,15 +213,21 @@ QString QgsDefaultSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper:
+ ( flags & EqualTo ? "=" : "<>" ) +
QStringLiteral( "lower(%1)" ).arg( QgsExpression::quotedString( mLineEdit->text() ) );
}
else if ( flags & Contains || flags & DoesNotContain )
else if ( flags & Contains || flags & DoesNotContain || flags & StartsWith || flags & EndsWith )
{
QString exp = fieldName + ( mCheckbox && mCheckbox->isChecked() ? " LIKE " : " ILIKE " );
QString value = QgsExpression::quotedString( mLineEdit->text() );
value.chop( 1 );
value = value.remove( 0, 1 );
exp += "'%" + value + "%'";
exp += '\'';
if ( !flags.testFlag( StartsWith ) )
exp += '%';
exp += value;
if ( !flags.testFlag( EndsWith ) )
exp += '%';
exp += '\'';
if ( flags & DoesNotContain )
exp.prepend( "NOT (" ).append( ")" );
exp.prepend( "NOT (" ).append( ')' );
return exp;
}

@@ -87,9 +87,13 @@ def testCreateExpression(self):
case_sensitive.setChecked(False)
self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.Contains), '"fldtxt" ILIKE \'%test%\'')
self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.DoesNotContain), 'NOT ("fldtxt" ILIKE \'%test%\')')
self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.StartsWith), '"fldtxt" ILIKE \'test%\'')
self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.EndsWith), '"fldtxt" ILIKE \'%test\'')
case_sensitive.setChecked(True)
self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.Contains), '"fldtxt" LIKE \'%test%\'')
self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.DoesNotContain), 'NOT ("fldtxt" LIKE \'%test%\')')
self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.StartsWith), '"fldtxt" LIKE \'test%\'')
self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.EndsWith), '"fldtxt" LIKE \'%test\'')
case_sensitive.setChecked(False)

# numeric field

0 comments on commit 8efa9fa

Please sign in to comment.
You can’t perform that action at this time.