QGIS API Documentation  3.11.0-Master (b7980f009c)
Public Slots | Signals | Public Member Functions | Protected Member Functions | List of all members
QgsExpressionBuilderWidget Class Reference

A reusable widget that can be used to build a expression string. More...

#include <qgsexpressionbuilderwidget.h>

Inheritance diagram for QgsExpressionBuilderWidget:
Inheritance graph
[legend]

Public Slots

void autosave ()
 Auto save the current Python function code. More...
 
const QList< QgsExpressionItem * > findExpressions (const QString &label)
 Returns the list of expression items matching a label. More...
 
void loadAllUsedValues ()
 Load all unique values from the set layer into the sample area. More...
 
void loadAllValues ()
 Load all unique values from the set layer into the sample area. More...
 
void loadSampleUsedValues ()
 Load used sample values into the sample value area. More...
 
void loadSampleValues ()
 Load sample values into the sample value area. More...
 
void removeSelectedUserExpression ()
 Removes the selected expression from the stored user expressions, the selected expression must be a user stored expression. More...
 
void setAutoSave (bool enabled)
 Enabled or disable auto saving. More...
 
void storeCurrentUserExpression ()
 Adds the current expressions to the stored user expressions. More...
 

Signals

void evalErrorChanged ()
 Will be set to true if the current expression text reported an eval error with the context. More...
 
void expressionParsed (bool isValid)
 Emitted when the user changes the expression in the widget. More...
 
void parserErrorChanged ()
 Will be set to true if the current expression text reported a parser error with the context. More...
 

Public Member Functions

 QgsExpressionBuilderWidget (QWidget *parent=nullptr)
 Create a new expression builder widget with an optional parent. More...
 
 ~QgsExpressionBuilderWidget () override
 
bool evalError () const
 Will be set to true if the current expression text reported an eval error with the context. More...
 
QString expectedOutputFormat ()
 The set expected format string. More...
 
QgsExpressionContext expressionContext () const
 Returns the expression context for the widget. More...
 
QString expressionText ()
 Gets the expression string that has been set in the expression area. More...
 
bool isExpressionValid ()
 
void loadCodeFromFile (QString path)
 Loads code from the given file into the function editor. More...
 
void loadFieldNames ()
 Loads all the field names from the layer. More...
 
void loadFieldNames (const QgsFields &fields)
 
void loadFieldsAndValues (const QMap< QString, QStringList > &fieldValues)
 Loads field names and values from the specified map. More...
 
void loadFunctionCode (const QString &code)
 Loads code into the function editor. More...
 
void loadRecent (const QString &collection=QStringLiteral("generic"))
 Loads the recent expressions from the given collection. More...
 
void loadUserExpressions ()
 Loads the user expressions. More...
 
QStandardItemModel * model ()
 Returns a pointer to the dialog's function item model. More...
 
void newFunctionFile (const QString &fileName="scratch")
 Creates a new file in the function editor. More...
 
bool parserError () const
 Will be set to true if the current expression text reports a parser error with the context. More...
 
QgsProjectproject ()
 Returns the project currently associated with the widget. More...
 
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(), const QStringList &tags=QStringList())
 Registers a node item for the expression builder. More...
 
void removeFromUserExpressions (const QString &label)
 Removes the expression label from the user stored expressions. More...
 
void saveFunctionFile (QString fileName)
 Saves the current function editor text to the given file. More...
 
void saveToRecent (const QString &collection="generic")
 Adds the current expression to the given collection. More...
 
void saveToUserExpressions (const QString &label, const QString expression, const QString &helpText)
 Stores the user expression with given label and helpText. More...
 
void setExpectedOutputFormat (const QString &expected)
 The set expected format string. More...
 
void setExpressionContext (const QgsExpressionContext &context)
 Sets the expression context for the widget. More...
 
void setExpressionText (const QString &expression)
 Sets the expression string for the widget. More...
 
void setGeomCalculator (const QgsDistanceArea &da)
 Sets geometry calculator used in distance/area calculations. More...
 
void setLayer (QgsVectorLayer *layer)
 Sets layer in order to get the fields and values. More...
 
void setProject (QgsProject *project)
 Sets the project currently associated with the widget. More...
 
void updateFunctionFileList (const QString &path)
 Updates the list of function files found at the given path. More...
 

Protected Member Functions

void showEvent (QShowEvent *e) override
 

Detailed Description

A reusable widget that can be used to build a expression string.

See QgsExpressionBuilderDialog for example of usage.

Definition at line 135 of file qgsexpressionbuilderwidget.h.

Constructor & Destructor Documentation

◆ QgsExpressionBuilderWidget()

QgsExpressionBuilderWidget::QgsExpressionBuilderWidget ( QWidget *  parent = nullptr)

Create a new expression builder widget with an optional parent.

Definition at line 47 of file qgsexpressionbuilderwidget.cpp.

◆ ~QgsExpressionBuilderWidget()

QgsExpressionBuilderWidget::~QgsExpressionBuilderWidget ( )
override

Definition at line 225 of file qgsexpressionbuilderwidget.cpp.

Member Function Documentation

◆ autosave

void QgsExpressionBuilderWidget::autosave ( )
slot

Auto save the current Python function code.

Definition at line 1265 of file qgsexpressionbuilderwidget.cpp.

◆ evalError()

bool QgsExpressionBuilderWidget::evalError ( ) const

Will be set to true if the current expression text reported an eval error with the context.

Since
QGIS 3.0

Definition at line 976 of file qgsexpressionbuilderwidget.cpp.

◆ evalErrorChanged

void QgsExpressionBuilderWidget::evalErrorChanged ( )
signal

Will be set to true if the current expression text reported an eval error with the context.

Since
QGIS 3.0

◆ expectedOutputFormat()

QString QgsExpressionBuilderWidget::expectedOutputFormat ( )

The set expected format string.

This is pure text format and no expression validation is done against it.

Returns
The expected value format.

Definition at line 800 of file qgsexpressionbuilderwidget.cpp.

◆ expressionContext()

QgsExpressionContext QgsExpressionBuilderWidget::expressionContext ( ) const
inline

Returns the expression context for the widget.

The context is used for the expression preview result and for populating the list of available functions and variables.

See also
setExpressionContext
Since
QGIS 2.12

Definition at line 199 of file qgsexpressionbuilderwidget.h.

◆ expressionParsed

void QgsExpressionBuilderWidget::expressionParsed ( bool  isValid)
signal

Emitted when the user changes the expression in the widget.

Users of this widget should connect to this signal to decide if to let the user continue.

Parameters
isValidIs true if the expression the user has typed is valid.

◆ expressionText()

QString QgsExpressionBuilderWidget::expressionText ( )

Gets the expression string that has been set in the expression area.

Returns
The expression as a string.

Definition at line 790 of file qgsexpressionbuilderwidget.cpp.

◆ findExpressions

const QList< QgsExpressionItem * > QgsExpressionBuilderWidget::findExpressions ( const QString &  label)
slot

Returns the list of expression items matching a label.

Since
QGIS 3.12

Definition at line 1322 of file qgsexpressionbuilderwidget.cpp.

◆ isExpressionValid()

bool QgsExpressionBuilderWidget::isExpressionValid ( )

Definition at line 596 of file qgsexpressionbuilderwidget.cpp.

◆ loadAllUsedValues

void QgsExpressionBuilderWidget::loadAllUsedValues ( )
slot

Load all unique values from the set layer into the sample area.

Only the used ones. Without available values, even if the formatter can provide them (eg. RelationReference).

Since
QGIS 3.12

Definition at line 1243 of file qgsexpressionbuilderwidget.cpp.

◆ loadAllValues

void QgsExpressionBuilderWidget::loadAllValues ( )
slot

Load all unique values from the set layer into the sample area.

Including all available values, in case the formatter can provide them (eg. RelationReference).

Definition at line 1217 of file qgsexpressionbuilderwidget.cpp.

◆ loadCodeFromFile()

void QgsExpressionBuilderWidget::loadCodeFromFile ( QString  path)

Loads code from the given file into the function editor.

Definition at line 389 of file qgsexpressionbuilderwidget.cpp.

◆ loadFieldNames() [1/2]

void QgsExpressionBuilderWidget::loadFieldNames ( )

Loads all the field names from the layer.

Remarks
Should this really be public couldn't we just do this for the user?

Definition at line 418 of file qgsexpressionbuilderwidget.cpp.

◆ loadFieldNames() [2/2]

void QgsExpressionBuilderWidget::loadFieldNames ( const QgsFields fields)

Definition at line 429 of file qgsexpressionbuilderwidget.cpp.

◆ loadFieldsAndValues()

void QgsExpressionBuilderWidget::loadFieldsAndValues ( const QMap< QString, QStringList > &  fieldValues)

Loads field names and values from the specified map.

Note
The field values must be quoted appropriately if they are strings.
Since
QGIS 2.12

Definition at line 445 of file qgsexpressionbuilderwidget.cpp.

◆ loadFunctionCode()

void QgsExpressionBuilderWidget::loadFunctionCode ( const QString &  code)

Loads code into the function editor.

Definition at line 397 of file qgsexpressionbuilderwidget.cpp.

◆ loadRecent()

void QgsExpressionBuilderWidget::loadRecent ( const QString &  collection = QStringLiteral( "generic" ))

Loads the recent expressions from the given collection.

By default it is loaded from the collection "generic".

Definition at line 619 of file qgsexpressionbuilderwidget.cpp.

◆ loadSampleUsedValues

void QgsExpressionBuilderWidget::loadSampleUsedValues ( )
slot

Load used sample values into the sample value area.

Only the used ones. Without available values, even if the formatter can provide them (eg. RelationReference).

Since
QGIS 3.12

Definition at line 1230 of file qgsexpressionbuilderwidget.cpp.

◆ loadSampleValues

void QgsExpressionBuilderWidget::loadSampleValues ( )
slot

Load sample values into the sample value area.

Including available values, in case the formatter can provide them (eg. RelationReference).

Definition at line 1204 of file qgsexpressionbuilderwidget.cpp.

◆ loadUserExpressions()

void QgsExpressionBuilderWidget::loadUserExpressions ( )

Loads the user expressions.

Since
QGIS 3.12

Definition at line 640 of file qgsexpressionbuilderwidget.cpp.

◆ model()

QStandardItemModel * QgsExpressionBuilderWidget::model ( )

Returns a pointer to the dialog's function item model.

This method is exposed for testing purposes only - it should not be used to modify the model.

Since
QGIS 3.0

Definition at line 990 of file qgsexpressionbuilderwidget.cpp.

◆ newFunctionFile()

void QgsExpressionBuilderWidget::newFunctionFile ( const QString &  fileName = "scratch")

Creates a new file in the function editor.

Definition at line 350 of file qgsexpressionbuilderwidget.cpp.

◆ parserError()

bool QgsExpressionBuilderWidget::parserError ( ) const

Will be set to true if the current expression text reports a parser error with the context.

Since
QGIS 3.0

Definition at line 951 of file qgsexpressionbuilderwidget.cpp.

◆ parserErrorChanged

void QgsExpressionBuilderWidget::parserErrorChanged ( )
signal

Will be set to true if the current expression text reported a parser error with the context.

Since
QGIS 3.0

◆ project()

QgsProject * QgsExpressionBuilderWidget::project ( )

Returns the project currently associated with the widget.

See also
setProject()
Since
QGIS 3.0

Definition at line 995 of file qgsexpressionbuilderwidget.cpp.

◆ registerItem()

void QgsExpressionBuilderWidget::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(),
const QStringList &  tags = QStringList() 
)

Registers a node item for the expression builder.

Parameters
groupThe group the item will be show in the tree view. If the group doesn't exist it will be created.
labelThe label that is show to the user for the item in the tree.
expressionTextThe text that is inserted into the expression area when the user double clicks on the item.
helpTextThe help text that the user will see when item is selected.
typeThe type of the expression item.
highlightedItemset to true to make the item highlighted, which inserts a bold copy of the item at the top level
sortOrdersort ranking for item
iconcustom icon to show for item
tagstags to find function

Definition at line 551 of file qgsexpressionbuilderwidget.cpp.

◆ removeFromUserExpressions()

void QgsExpressionBuilderWidget::removeFromUserExpressions ( const QString &  label)

Removes the expression label from the user stored expressions.

Since
QGIS 3.12

Definition at line 686 of file qgsexpressionbuilderwidget.cpp.

◆ removeSelectedUserExpression

void QgsExpressionBuilderWidget::removeSelectedUserExpression ( )
slot

Removes the selected expression from the stored user expressions, the selected expression must be a user stored expression.

Since
QGIS 3.12

Definition at line 1298 of file qgsexpressionbuilderwidget.cpp.

◆ saveFunctionFile()

void QgsExpressionBuilderWidget::saveFunctionFile ( QString  fileName)

Saves the current function editor text to the given file.

Definition at line 297 of file qgsexpressionbuilderwidget.cpp.

◆ saveToRecent()

void QgsExpressionBuilderWidget::saveToRecent ( const QString &  collection = "generic")

Adds the current expression to the given collection.

By default it is saved to the collection "generic".

Definition at line 601 of file qgsexpressionbuilderwidget.cpp.

◆ saveToUserExpressions()

void QgsExpressionBuilderWidget::saveToUserExpressions ( const QString &  label,
const QString  expression,
const QString &  helpText 
)

Stores the user expression with given label and helpText.

Since
QGIS 3.12

Definition at line 667 of file qgsexpressionbuilderwidget.cpp.

◆ setAutoSave

void QgsExpressionBuilderWidget::setAutoSave ( bool  enabled)
inlineslot

Enabled or disable auto saving.

When enabled Python scripts will be auto saved when text changes.

Parameters
enabledtrue to enable auto saving.

Definition at line 368 of file qgsexpressionbuilderwidget.h.

◆ setExpectedOutputFormat()

void QgsExpressionBuilderWidget::setExpectedOutputFormat ( const QString &  expected)

The set expected format string.

This is pure text format and no expression validation is done against it.

Parameters
expectedThe expected value format for the expression.
Note
Only a UI hint and not used for expression validation.

Definition at line 805 of file qgsexpressionbuilderwidget.cpp.

◆ setExpressionContext()

void QgsExpressionBuilderWidget::setExpressionContext ( const QgsExpressionContext context)

Sets the expression context for the widget.

The context is used for the expression preview result and for populating the list of available functions and variables.

Parameters
contextexpression context
See also
expressionContext
Since
QGIS 2.12

Definition at line 811 of file qgsexpressionbuilderwidget.cpp.

◆ setExpressionText()

void QgsExpressionBuilderWidget::setExpressionText ( const QString &  expression)

Sets the expression string for the widget.

Definition at line 795 of file qgsexpressionbuilderwidget.cpp.

◆ setGeomCalculator()

void QgsExpressionBuilderWidget::setGeomCalculator ( const QgsDistanceArea da)

Sets geometry calculator used in distance/area calculations.

Definition at line 785 of file qgsexpressionbuilderwidget.cpp.

◆ setLayer()

void QgsExpressionBuilderWidget::setLayer ( QgsVectorLayer layer)

Sets layer in order to get the fields and values.

Note
this needs to be called before calling loadFieldNames().

Definition at line 233 of file qgsexpressionbuilderwidget.cpp.

◆ setProject()

void QgsExpressionBuilderWidget::setProject ( QgsProject project)

Sets the project currently associated with the widget.

This controls which layers and relations and other project-specific items are shown in the widget.

See also
project()
Since
QGIS 3.0

Definition at line 1000 of file qgsexpressionbuilderwidget.cpp.

◆ showEvent()

void QgsExpressionBuilderWidget::showEvent ( QShowEvent *  e)
overrideprotected

Definition at line 1006 of file qgsexpressionbuilderwidget.cpp.

◆ storeCurrentUserExpression

void QgsExpressionBuilderWidget::storeCurrentUserExpression ( )
slot

Adds the current expressions to the stored user expressions.

Since
QGIS 3.12

Definition at line 1288 of file qgsexpressionbuilderwidget.cpp.

◆ updateFunctionFileList()

void QgsExpressionBuilderWidget::updateFunctionFileList ( const QString &  path)

Updates the list of function files found at the given path.

Definition at line 320 of file qgsexpressionbuilderwidget.cpp.


The documentation for this class was generated from the following files: