Skip to content

Commit 8e1c437

Browse files
committed
Allow expression contexts to specify a list of highlighted functions
as well as just variable names In some cases contexts may provide specific functions of use to that context, or more generally there may be functions we want to highlight for a particular expression builder (e.g. highlighting to_dms in the grid annotation builder)
1 parent 5cf0433 commit 8e1c437

File tree

5 files changed

+97
-7
lines changed

5 files changed

+97
-7
lines changed

python/core/auto_generated/qgsexpressioncontext.sip.in

+33-3
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,13 @@ contained by the context.
424424

425425
bool isHighlightedVariable( const QString &name ) const;
426426
%Docstring
427-
Returns true if the specified variable name is intended to be highlighted to the
427+
Returns true if the specified variable ``name`` is intended to be highlighted to the
428428
user. This is used by the expression builder to more prominently display the
429429
variable.
430430

431-
:param name: variable name
432-
433431
.. seealso:: :py:func:`setHighlightedVariables`
432+
433+
.. seealso:: :py:func:`isHighlightedFunction`
434434
%End
435435

436436
void setHighlightedVariables( const QStringList &variableNames );
@@ -441,6 +441,36 @@ is used by the expression builder to more prominently display these variables.
441441
:param variableNames: variable names to highlight
442442

443443
.. seealso:: :py:func:`isHighlightedVariable`
444+
445+
.. seealso:: :py:func:`setHighlightedFunctions`
446+
%End
447+
448+
bool isHighlightedFunction( const QString &name ) const;
449+
%Docstring
450+
Returns true if the specified function ``name`` is intended to be highlighted to the
451+
user. This is used by the expression builder to more prominently display the
452+
function.
453+
454+
.. seealso:: :py:func:`setHighlightedFunctions`
455+
456+
.. seealso:: :py:func:`isHighlightedVariable`
457+
458+
.. versionadded:: 3.4
459+
%End
460+
461+
void setHighlightedFunctions( const QStringList &names );
462+
%Docstring
463+
Sets the list of function ``names`` intended to be highlighted to the user. This
464+
is used by the expression builder to more prominently display these functions.
465+
466+
Note that these function names may include standard functions which are not functions
467+
specific to this context, and these standard functions will also be highlighted to users.
468+
469+
.. seealso:: :py:func:`isHighlightedFunction`
470+
471+
.. seealso:: :py:func:`setHighlightedVariables`
472+
473+
.. versionadded:: 3.4
444474
%End
445475

446476
QgsExpressionContextScope *activeScopeForVariable( const QString &name );

src/core/qgsexpressioncontext.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ QgsExpressionContext::QgsExpressionContext( const QgsExpressionContext &other )
237237
mStack << new QgsExpressionContextScope( *scope );
238238
}
239239
mHighlightedVariables = other.mHighlightedVariables;
240+
mHighlightedFunctions = other.mHighlightedFunctions;
240241
mCachedValues = other.mCachedValues;
241242
}
242243

@@ -250,6 +251,7 @@ QgsExpressionContext &QgsExpressionContext::operator=( QgsExpressionContext &&ot
250251
other.mStack.clear();
251252

252253
mHighlightedVariables = other.mHighlightedVariables;
254+
mHighlightedFunctions = other.mHighlightedFunctions;
253255
mCachedValues = other.mCachedValues;
254256
}
255257
return *this;
@@ -264,6 +266,7 @@ QgsExpressionContext &QgsExpressionContext::operator=( const QgsExpressionContex
264266
mStack << new QgsExpressionContextScope( *scope );
265267
}
266268
mHighlightedVariables = other.mHighlightedVariables;
269+
mHighlightedFunctions = other.mHighlightedFunctions;
267270
mCachedValues = other.mCachedValues;
268271
return *this;
269272
}
@@ -311,6 +314,16 @@ void QgsExpressionContext::setHighlightedVariables( const QStringList &variableN
311314
mHighlightedVariables = variableNames;
312315
}
313316

317+
bool QgsExpressionContext::isHighlightedFunction( const QString &name ) const
318+
{
319+
return mHighlightedFunctions.contains( name );
320+
}
321+
322+
void QgsExpressionContext::setHighlightedFunctions( const QStringList &names )
323+
{
324+
mHighlightedFunctions = names;
325+
}
326+
314327
const QgsExpressionContextScope *QgsExpressionContext::activeScopeForVariable( const QString &name ) const
315328
{
316329
//iterate through stack backwards, so that higher priority variables take precedence

src/core/qgsexpressioncontext.h

+27-2
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,11 @@ class CORE_EXPORT QgsExpressionContext
425425
QVariantMap variablesToMap() const;
426426

427427
/**
428-
* Returns true if the specified variable name is intended to be highlighted to the
428+
* Returns true if the specified variable \a name is intended to be highlighted to the
429429
* user. This is used by the expression builder to more prominently display the
430430
* variable.
431-
* \param name variable name
432431
* \see setHighlightedVariables()
432+
* \see isHighlightedFunction()
433433
*/
434434
bool isHighlightedVariable( const QString &name ) const;
435435

@@ -438,9 +438,33 @@ class CORE_EXPORT QgsExpressionContext
438438
* is used by the expression builder to more prominently display these variables.
439439
* \param variableNames variable names to highlight
440440
* \see isHighlightedVariable()
441+
* \see setHighlightedFunctions()
441442
*/
442443
void setHighlightedVariables( const QStringList &variableNames );
443444

445+
/**
446+
* Returns true if the specified function \a name is intended to be highlighted to the
447+
* user. This is used by the expression builder to more prominently display the
448+
* function.
449+
* \see setHighlightedFunctions()
450+
* \see isHighlightedVariable()
451+
* \since QGIS 3.4
452+
*/
453+
bool isHighlightedFunction( const QString &name ) const;
454+
455+
/**
456+
* Sets the list of function \a names intended to be highlighted to the user. This
457+
* is used by the expression builder to more prominently display these functions.
458+
*
459+
* Note that these function names may include standard functions which are not functions
460+
* specific to this context, and these standard functions will also be highlighted to users.
461+
*
462+
* \see isHighlightedFunction()
463+
* \see setHighlightedVariables()
464+
* \since QGIS 3.4
465+
*/
466+
void setHighlightedFunctions( const QStringList &names );
467+
444468
/**
445469
* Returns the currently active scope from the context for a specified variable name.
446470
* As scopes later in the stack override earlier contexts, this will be the last matching
@@ -714,6 +738,7 @@ class CORE_EXPORT QgsExpressionContext
714738

715739
QList< QgsExpressionContextScope * > mStack;
716740
QStringList mHighlightedVariables;
741+
QStringList mHighlightedFunctions;
717742

718743
// Cache is mutable because we want to be able to add cached values to const contexts
719744
mutable QMap< QString, QVariant > mCachedValues;

src/gui/qgsexpressionbuilderwidget.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ void QgsExpressionBuilderWidget::updateFunctionTree()
577577
name += '(';
578578
else if ( !name.startsWith( '$' ) )
579579
name += QLatin1String( "()" );
580-
registerItemForAllGroups( func->groups(), func->name(), ' ' + name + ' ', func->helpText() );
580+
registerItemForAllGroups( func->groups(), func->name(), ' ' + name + ' ', func->helpText(), QgsExpressionItem::ExpressionNode, mExpressionContext.isHighlightedFunction( func->name() ) );
581581
}
582582

583583
// load relation names
@@ -721,7 +721,7 @@ void QgsExpressionBuilderWidget::loadExpressionContext()
721721
continue;
722722
if ( func->params() != 0 )
723723
name += '(';
724-
registerItemForAllGroups( func->groups(), func->name(), ' ' + name + ' ', func->helpText() );
724+
registerItemForAllGroups( func->groups(), func->name(), ' ' + name + ' ', func->helpText(), QgsExpressionItem::ExpressionNode, mExpressionContext.isHighlightedFunction( func->name() ) );
725725
}
726726
}
727727

tests/src/core/testqgsexpressioncontext.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class TestQgsExpressionContext : public QObject
4444
void setFeature();
4545
void setFields();
4646
void takeScopes();
47+
void highlighted();
4748

4849
void globalScope();
4950
void projectScope();
@@ -542,6 +543,27 @@ void TestQgsExpressionContext::takeScopes()
542543
QVERIFY( !context.variable( "test_project" ).isValid() );
543544
}
544545

546+
void TestQgsExpressionContext::highlighted()
547+
{
548+
QgsExpressionContext context;
549+
QVERIFY( !context.isHighlightedFunction( QStringLiteral( "x" ) ) );
550+
QVERIFY( !context.isHighlightedVariable( QStringLiteral( "x" ) ) );
551+
context.setHighlightedFunctions( QStringList() << QStringLiteral( "x" ) << QStringLiteral( "y" ) );
552+
QVERIFY( context.isHighlightedFunction( QStringLiteral( "x" ) ) );
553+
QVERIFY( context.isHighlightedFunction( QStringLiteral( "y" ) ) );
554+
QVERIFY( !context.isHighlightedFunction( QStringLiteral( "z" ) ) );
555+
QVERIFY( !context.isHighlightedVariable( QStringLiteral( "x" ) ) );
556+
context.setHighlightedVariables( QStringList() << QStringLiteral( "a" ) << QStringLiteral( "b" ) );
557+
QVERIFY( context.isHighlightedVariable( QStringLiteral( "a" ) ) );
558+
QVERIFY( context.isHighlightedVariable( QStringLiteral( "b" ) ) );
559+
QVERIFY( !context.isHighlightedVariable( QStringLiteral( "c" ) ) );
560+
QVERIFY( !context.isHighlightedFunction( QStringLiteral( "a" ) ) );
561+
context.setHighlightedFunctions( QStringList() );
562+
context.setHighlightedVariables( QStringList() );
563+
QVERIFY( !context.isHighlightedFunction( QStringLiteral( "x" ) ) );
564+
QVERIFY( !context.isHighlightedVariable( QStringLiteral( "a" ) ) );
565+
}
566+
545567
void TestQgsExpressionContext::globalScope()
546568
{
547569
QgsExpressionContextUtils::setGlobalVariable( QStringLiteral( "test" ), "testval" );

0 commit comments

Comments
 (0)