Skip to content
Permalink
Browse files

Merge pull request #33197 from signedav/searchtags_functions

Search tags on expression functions
  • Loading branch information
signedav committed Dec 17, 2019
2 parents c945334 + 929670b commit 4cbd9a1ee36339390180a15f0f6aefaf274ae531
@@ -535,6 +535,15 @@ Returns the help text for a specified function.
.. seealso:: :py:func:`variableHelpText`

.. seealso:: :py:func:`formatVariableHelp`
%End

static QStringList tags( const QString &name );
%Docstring
Returns a string list of search tags for a specified function.

:param name: function name

.. versionadded:: 3.12
%End

static QString variableHelpText( const QString &variableName );
@@ -63,6 +63,7 @@ Gets the type of expression item, e.g., header, field, ExpressionNode.

static const int CUSTOM_SORT_ROLE;
static const int ITEM_TYPE_ROLE;
static const int SEARCH_TAGS_ROLE;

};

@@ -199,7 +200,8 @@ preview result and for populating the list of available functions and variables.
const QString &helpText = QString(),
QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
bool highlightedItem = false, int sortOrder = 1,
QIcon icon = QIcon() );
QIcon icon = QIcon(),
const QStringList &tags = QStringList() );
%Docstring
Registers a node item for the expression builder.

@@ -211,6 +213,7 @@ Registers a node item for the expression builder.
:param highlightedItem: set to ``True`` to make the item highlighted, which inserts a bold copy of the item at the top level
:param sortOrder: sort ranking for item
:param icon: custom icon to show for item
:param tags: tags to find function
%End

bool isExpressionValid();
@@ -7,6 +7,6 @@
{"arg":"delimiter","optional":true,"default":"','","description":"the string delimiter used to split the input string"},
{"arg":"empty_value","optional":true,"default":"''","description":"the optional string to use as replacement for empty (zero length) matches"}],
"examples": [ { "expression":"string_to_array('1,2,3',',')", "returns":"[ '1', '2', '3' ]"},
{ "expression":"string_to_array('1,,3',',','0')", "returns":"[ '1', '0', '3' ]"}
]
{ "expression":"string_to_array('1,,3',',','0')", "returns":"[ '1', '0', '3' ]"}],
"tags": [ "split", "convert", "separate" ]
}
@@ -111,7 +111,15 @@ def quote(v):
)

if 'notes' in v:
cpp.write(",\n tr( \"{0}\" )".format(v['notes']))
cpp.write(",\n tr( \"{0}\" )".format(v['notes']))
else:
cpp.write(",\n QString()")

if 'tags' in v:
cpp.write(",\n QStringList()")

for t in v['tags']:
cpp.write("\n << QStringLiteral( \"{0}\" ) << tr( \"{0}\" )".format(t))

cpp.write("\n )")

@@ -652,6 +652,25 @@ QString QgsExpression::helpText( QString name )
return helpContents;
}

QStringList QgsExpression::tags( const QString &name )
{
QStringList tags = QStringList();

QgsExpression::initFunctionHelp();

if ( sFunctionHelpTexts()->contains( name ) )
{
const Help &f = ( *sFunctionHelpTexts() )[ name ];

for ( const HelpVariant &v : qgis::as_const( f.mVariants ) )
{
tags << v.mTags;
}
}

return tags;
}

void QgsExpression::initVariableHelp()
{
if ( !sVariableHelpTexts()->isEmpty() )
@@ -567,6 +567,13 @@ class CORE_EXPORT QgsExpression
*/
static QString helpText( QString name );

/**
* Returns a string list of search tags for a specified function.
* \param name function name
* \since QGIS 3.12
*/
static QStringList tags( const QString &name );

/**
* Returns the help text for a specified variable.
* \param variableName name of variable
@@ -116,13 +116,15 @@ struct HelpVariant
const QList<HelpArg> &arguments = QList<HelpArg>(),
bool variableLenArguments = false,
const QList<HelpExample> &examples = QList<HelpExample>(),
const QString &notes = QString() )
const QString &notes = QString(),
const QStringList &tags = QStringList() )
: mName( name )
, mDescription( description )
, mArguments( arguments )
, mVariableLenArguments( variableLenArguments )
, mExamples( examples )
, mNotes( notes )
, mTags( tags )
{}

QString mName;
@@ -131,6 +133,7 @@ struct HelpVariant
bool mVariableLenArguments;
QList<HelpExample> mExamples;
QString mNotes;
QStringList mTags;
};


@@ -161,7 +161,7 @@ QgsStaticExpressionFunction::QgsStaticExpressionFunction( const QString &fnname,
bool lazyEval,
const QStringList &aliases,
bool handlesNull )
: QgsExpressionFunction( fnname, params, group, helpText, lazyEval, handlesNull )
: QgsExpressionFunction( fnname, params, group, helpText, lazyEval, handlesNull, false )
, mFnc( fcn )
, mAliases( aliases )
, mUsesGeometry( false )
@@ -413,7 +413,6 @@ class QgsStaticExpressionFunction : public QgsExpressionFunction
const QStringList &aliases = QStringList(),
bool handlesNull = false );


/**
* Static function for evaluation against a QgsExpressionContext, using a named list of parameter values and list
* of groups.
@@ -506,11 +506,12 @@ void QgsExpressionBuilderWidget::registerItem( const QString &group,
const QString &label,
const QString &expressionText,
const QString &helpText,
QgsExpressionItem::ItemType type, bool highlightedItem, int sortOrder, QIcon icon )
QgsExpressionItem::ItemType type, bool highlightedItem, int sortOrder, QIcon icon, const QStringList &tags )
{
QgsExpressionItem *item = new QgsExpressionItem( label, expressionText, helpText, type );
item->setData( label, Qt::UserRole );
item->setData( sortOrder, QgsExpressionItem::CUSTOM_SORT_ROLE );
item->setData( tags, QgsExpressionItem::SEARCH_TAGS_ROLE );
item->setIcon( icon );

// Look up the group and insert the new function.
@@ -671,7 +672,7 @@ void QgsExpressionBuilderWidget::updateFunctionTree()
name += '(';
else if ( !name.startsWith( '$' ) )
name += QLatin1String( "()" );
registerItemForAllGroups( func->groups(), func->name(), ' ' + name + ' ', func->helpText(), QgsExpressionItem::ExpressionNode, mExpressionContext.isHighlightedFunction( func->name() ) );
registerItemForAllGroups( func->groups(), func->name(), ' ' + name + ' ', func->helpText(), QgsExpressionItem::ExpressionNode, mExpressionContext.isHighlightedFunction( func->name() ), 1, QgsExpression::tags( func->name() ) );
}

// load relation names
@@ -817,16 +818,16 @@ void QgsExpressionBuilderWidget::loadExpressionContext()
continue;
if ( func->params() != 0 )
name += '(';
registerItemForAllGroups( func->groups(), func->name(), ' ' + name + ' ', func->helpText(), QgsExpressionItem::ExpressionNode, mExpressionContext.isHighlightedFunction( func->name() ) );
registerItemForAllGroups( func->groups(), func->name(), ' ' + name + ' ', func->helpText(), QgsExpressionItem::ExpressionNode, mExpressionContext.isHighlightedFunction( func->name() ), 1, QgsExpression::tags( func->name() ) );
}
}

void QgsExpressionBuilderWidget::registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText, const QString &helpText, QgsExpressionItem::ItemType type, bool highlightedItem, int sortOrder )
void QgsExpressionBuilderWidget::registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText, const QString &helpText, QgsExpressionItem::ItemType type, bool highlightedItem, int sortOrder, const QStringList &tags )
{
const auto constGroups = groups;
for ( const QString &group : constGroups )
{
registerItem( group, label, expressionText, helpText, type, highlightedItem, sortOrder );
registerItem( group, label, expressionText, helpText, type, highlightedItem, sortOrder, QIcon(), tags );
}
}

@@ -1231,7 +1232,21 @@ bool QgsExpressionItemSearchProxy::filterAcceptsRow( int source_row, const QMode
if ( itemType == QgsExpressionItem::Header )
return false;

return QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent );
// check match of item label or tags
if ( QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent ) )
{
return true;
}
else
{
const QStringList tags = sourceModel()->data( index, QgsExpressionItem::SEARCH_TAGS_ROLE ).toStringList();
for ( const QString &tag : tags )
{
if ( tag.contains( filterRegExp() ) )
return true;
}
}
return false;
}

bool QgsExpressionItemSearchProxy::lessThan( const QModelIndex &left, const QModelIndex &right ) const
@@ -96,6 +96,8 @@ class GUI_EXPORT QgsExpressionItem : public QStandardItem
static const int CUSTOM_SORT_ROLE = Qt::UserRole + 1;
//! Item type role
static const int ITEM_TYPE_ROLE = Qt::UserRole + 2;
//! Search tags role
static const int SEARCH_TAGS_ROLE = Qt::UserRole + 3;

private:
QString mExpressionText;
@@ -215,12 +217,14 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
* \param highlightedItem set to TRUE to make the item highlighted, which inserts a bold copy of the item at the top level
* \param sortOrder sort ranking for item
* \param icon custom icon to show for item
* \param tags tags to find function
*/
void registerItem( const QString &group, const QString &label, const QString &expressionText,
const QString &helpText = QString(),
QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
bool highlightedItem = false, int sortOrder = 1,
QIcon icon = QIcon() );
QIcon icon = QIcon(),
const QStringList &tags = QStringList() );

bool isExpressionValid();

@@ -399,11 +403,12 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
* \param type The type of the expression item.
* \param highlightedItem set to TRUE to make the item highlighted, which inserts a bold copy of the item at the top level
* \param sortOrder sort ranking for item
* \param tags tags to find function
*/
void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
const QString &helpText = QString(),
QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
bool highlightedItem = false, int sortOrder = 1 );
bool highlightedItem = false, int sortOrder = 1, const QStringList &tags = QStringList() );

/**
* Returns a HTML formatted string for use as a \a relation item help.

0 comments on commit 4cbd9a1

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