28#include <QVersionNumber>
30#include "moc_qgsexpressiontreeview.cpp"
35 QString text = QStringLiteral(
"<h3>%1</h3>\n<div class=\"description\"><p>%2</p></div>" )
36 .arg( QCoreApplication::translate(
"relation_help",
"relation %1" ).arg( relation.
name() ), QObject::tr(
"Inserts the relation ID for the relation named '%1'." ).arg( relation.
name() ) );
38 text += QStringLiteral(
"<h4>%1</h4><div class=\"description\"><pre>%2</pre></div>" )
39 .arg( QObject::tr(
"Current value" ), relation.
id() );
48 QString text = QStringLiteral(
"<h3>%1</h3>\n<div class=\"description\"><p>%2</p></div>" )
49 .arg( QCoreApplication::translate(
"layer_help",
"map layer %1" ).arg( layer->
name() ), QObject::tr(
"Inserts the layer ID for the layer named '%1'." ).arg( layer->
name() ) );
51 text += QStringLiteral(
"<h4>%1</h4><div class=\"description\"><pre>%2</pre></div>" )
52 .arg( QObject::tr(
"Current value" ), layer->
id() );
60 QString text = QStringLiteral(
"<h3>%1</h3>\n<div class=\"description\"><p>%2</p></div>" )
61 .arg( QCoreApplication::translate(
"recent_expression_help",
"expression %1" ).arg( label ), QCoreApplication::translate(
"recent_expression_help",
"Recently used expression." ) );
63 text += QStringLiteral(
"<h4>%1</h4><div class=\"description\"><pre>%2</pre></div>" )
64 .arg( QObject::tr(
"Expression" ), expression );
72 QString text = QStringLiteral(
"<h3>%1</h3>\n<div class=\"description\"><p>%2</p></div>" )
73 .arg( QCoreApplication::translate(
"user_expression_help",
"expression %1" ).arg( label ), description );
75 text += QStringLiteral(
"<h4>%1</h4><div class=\"description\"><pre>%2</pre></div>" )
76 .arg( QObject::tr(
"Expression" ), expression );
82QString
formatVariableHelp(
const QString &variable,
const QString &description,
bool showValue,
const QVariant &value )
84 QString text = QStringLiteral(
"<h3>%1</h3>\n<div class=\"description\"><p>%2</p></div>" )
85 .arg( QCoreApplication::translate(
"variable_help",
"variable %1" ).arg( variable ), description );
89 QString valueString = !value.isValid()
90 ? QCoreApplication::translate(
"variable_help",
"not set" )
93 text += QStringLiteral(
"<h4>%1</h4><div class=\"description\"><p>%2</p></div>" )
94 .arg( QObject::tr(
"Current value" ), valueString );
108 : QTreeView( parent )
111 connect(
this, &QTreeView::doubleClicked,
this, &QgsExpressionTreeView::onDoubleClicked );
113 mModel = std::make_unique<QStandardItemModel>();
114 mProxyModel = std::make_unique<QgsExpressionItemSearchProxy>();
115 mProxyModel->setDynamicSortFilter(
true );
116 mProxyModel->setSourceModel( mModel.get() );
117 setModel( mProxyModel.get() );
118 setSortingEnabled(
true );
119 sortByColumn( 0, Qt::AscendingOrder );
121 setSelectionMode( QAbstractItemView::SelectionMode::SingleSelection );
123 setContextMenuPolicy( Qt::CustomContextMenu );
124 connect(
this, &QWidget::customContextMenuRequested,
this, &QgsExpressionTreeView::showContextMenu );
125 connect( selectionModel(), &QItemSelectionModel::currentChanged,
this, &QgsExpressionTreeView::currentItemChanged );
127 updateFunctionTree();
132 QModelIndex firstItem = mProxyModel->index( 0, 0, QModelIndex() );
133 setCurrentIndex( firstItem );
150 mExpressionContext = context;
151 updateFunctionTree();
159 mMenuProvider = provider;
164 updateFunctionTree();
172 QModelIndex idx = mProxyModel->mapToSource( currentIndex() );
190 updateFunctionTree();
196 mProxyModel->setFilterString( text );
197 if ( text.isEmpty() )
204 QModelIndex index = mProxyModel->index( 0, 0 );
205 if ( mProxyModel->hasChildren( index ) )
207 QModelIndex child = mProxyModel->index( 0, 0, index );
208 selectionModel()->setCurrentIndex( child, QItemSelectionModel::ClearAndSelect );
213void QgsExpressionTreeView::onDoubleClicked(
const QModelIndex &index )
215 QModelIndex idx = mProxyModel->mapToSource( index );
227void QgsExpressionTreeView::showContextMenu( QPoint pt )
229 QModelIndex idx = indexAt( pt );
230 idx = mProxyModel->mapToSource( idx );
231 QgsExpressionItem *item =
static_cast<QgsExpressionItem *
>( mModel->itemFromIndex( idx ) );
235 if ( !mMenuProvider )
238 QMenu *menu = mMenuProvider->createContextMenu( item );
241 menu->popup( mapToGlobal( pt ) );
244void QgsExpressionTreeView::currentItemChanged(
const QModelIndex &index,
const QModelIndex & )
247 QModelIndex idx = mProxyModel->mapToSource( index );
248 QgsExpressionItem *item =
static_cast<QgsExpressionItem *
>( mModel->itemFromIndex( idx ) );
255void QgsExpressionTreeView::updateFunctionTree()
258 mExpressionGroups.clear();
261 static const QList<QPair<QString, QString>> operators = QList<QPair<QString, QString>>()
262 << QPair<QString, QString>( QStringLiteral(
"+" ), QStringLiteral(
" + " ) )
263 << QPair<QString, QString>( QStringLiteral(
"-" ), QStringLiteral(
" - " ) )
264 << QPair<QString, QString>( QStringLiteral(
"*" ), QStringLiteral(
" * " ) )
265 << QPair<QString, QString>( QStringLiteral(
"/" ), QStringLiteral(
" / " ) )
266 << QPair<QString, QString>( QStringLiteral(
"//" ), QStringLiteral(
" // " ) )
267 << QPair<QString, QString>( QStringLiteral(
"%" ), QStringLiteral(
" % " ) )
268 << QPair<QString, QString>( QStringLiteral(
"^" ), QStringLiteral(
" ^ " ) )
269 << QPair<QString, QString>( QStringLiteral(
"=" ), QStringLiteral(
" = " ) )
270 << QPair<QString, QString>( QStringLiteral(
"~" ), QStringLiteral(
" ~ " ) )
271 << QPair<QString, QString>( QStringLiteral(
">" ), QStringLiteral(
" > " ) )
272 << QPair<QString, QString>( QStringLiteral(
"<" ), QStringLiteral(
" < " ) )
273 << QPair<QString, QString>( QStringLiteral(
"<>" ), QStringLiteral(
" <> " ) )
274 << QPair<QString, QString>( QStringLiteral(
"<=" ), QStringLiteral(
" <= " ) )
275 << QPair<QString, QString>( QStringLiteral(
">=" ), QStringLiteral(
" >= " ) )
276 << QPair<QString, QString>( QStringLiteral(
"[]" ), QStringLiteral(
"[]" ) )
277 << QPair<QString, QString>( QStringLiteral(
"||" ), QStringLiteral(
" || " ) )
278 << QPair<QString, QString>( QStringLiteral(
"BETWEEN" ), QStringLiteral(
" BETWEEN " ) )
279 << QPair<QString, QString>( QStringLiteral(
"NOT BETWEEN" ), QStringLiteral(
" NOT BETWEEN " ) )
280 << QPair<QString, QString>( QStringLiteral(
"IN" ), QStringLiteral(
" IN " ) )
281 << QPair<QString, QString>( QStringLiteral(
"LIKE" ), QStringLiteral(
" LIKE " ) )
282 << QPair<QString, QString>( QStringLiteral(
"ILIKE" ), QStringLiteral(
" ILIKE " ) )
283 << QPair<QString, QString>( QStringLiteral(
"IS" ), QStringLiteral(
" IS " ) )
284 << QPair<QString, QString>( QStringLiteral(
"IS NOT" ), QStringLiteral(
" IS NOT " ) )
285 << QPair<QString, QString>( QStringLiteral(
"OR" ), QStringLiteral(
" OR " ) )
286 << QPair<QString, QString>( QStringLiteral(
"AND" ), QStringLiteral(
" AND " ) )
287 << QPair<QString, QString>( QStringLiteral(
"NOT" ), QStringLiteral(
" NOT " ) );
288 for (
const auto &name : operators )
293 QString casestring = QStringLiteral(
"CASE WHEN condition THEN result END" );
297 registerItem( QStringLiteral(
"Fields and Values" ), QStringLiteral(
"NULL" ), QStringLiteral(
"NULL" ), QString(),
QgsExpressionItem::ExpressionNode,
false, -1 );
301 for (
int i = 0; i < count; i++ )
304 QString name = func->
name();
305 if ( name.startsWith(
'_' ) )
315 if ( func->
params() != 0 )
317 else if ( !name.startsWith(
'$' ) )
318 name += QLatin1String(
"()" );
329 loadExpressionContext();
332QgsExpressionItem *QgsExpressionTreeView::registerItem(
const QString &group,
const QString &label,
const QString &expressionText,
const QString &helpText,
QgsExpressionItem::ItemType type,
bool highlightedItem,
int sortOrder,
const QIcon &icon,
const QStringList &tags,
const QString &name )
334 QgsExpressionItem *item =
new QgsExpressionItem( label, expressionText, helpText, type );
335 item->setData( label, Qt::UserRole );
339 item->setIcon( icon );
342 if ( mExpressionGroups.contains( group ) )
344 QgsExpressionItem *groupNode = mExpressionGroups.value( group );
345 groupNode->appendRow( item );
351 newgroupNode->setData( group, Qt::UserRole );
354 newgroupNode->appendRow( item );
355 newgroupNode->setBackground( QBrush( QColor( 150, 150, 150, 150 ) ) );
356 mModel->appendRow( newgroupNode );
357 mExpressionGroups.insert( group, newgroupNode );
360 if ( highlightedItem )
363 QgsExpressionItem *topLevelItem =
new QgsExpressionItem( label, expressionText, helpText, type );
364 topLevelItem->setData( label, Qt::UserRole );
366 QFont font = topLevelItem->font();
367 font.setBold(
true );
368 topLevelItem->setFont( font );
369 mModel->appendRow( topLevelItem );
374void QgsExpressionTreeView::registerItemForAllGroups(
const QStringList &groups,
const QString &label,
const QString &expressionText,
const QString &helpText,
QgsExpressionItem::ItemType type,
bool highlightedItem,
int sortOrder,
const QStringList &tags )
376 const auto constGroups = groups;
377 for (
const QString &group : constGroups )
379 registerItem( group, label, expressionText, helpText, type, highlightedItem, sortOrder, QIcon(), tags );
383void QgsExpressionTreeView::loadExpressionContext()
385 QStringList variableNames = mExpressionContext.filteredVariableNames();
386 const auto constVariableNames = variableNames;
387 for (
const QString &variable : constVariableNames )
389 registerItem( QStringLiteral(
"Variables" ), variable,
" @" + variable +
' ',
formatVariableHelp( variable, mExpressionContext.description( variable ),
true, mExpressionContext.variable( variable ) ),
QgsExpressionItem::ExpressionNode, mExpressionContext.isHighlightedVariable( variable ) );
393 QStringList contextFunctions = mExpressionContext.functionNames();
394 const auto constContextFunctions = contextFunctions;
395 for (
const QString &functionName : constContextFunctions )
397 QgsExpressionFunction *func = mExpressionContext.function( functionName );
398 QString name = func->
name();
399 if ( name.startsWith(
'_' ) )
401 if ( func->
params() != 0 )
407void QgsExpressionTreeView::loadLayers()
412 QMap<QString, QgsMapLayer *> layers = mProject->mapLayers();
413 QMap<QString, QgsMapLayer *>::const_iterator layerIt = layers.constBegin();
414 for ( ; layerIt != layers.constEnd(); ++layerIt )
417 QgsExpressionItem *parentItem = registerItem( QStringLiteral(
"Map Layers" ), layerIt.value()->name(), QStringLiteral(
"'%1'" ).arg( layerIt.key() ),
formatLayerHelp( layerIt.value() ),
QgsExpressionItem::ExpressionNode,
false, 99, icon );
418 loadLayerFields( qobject_cast<QgsVectorLayer *>( layerIt.value() ), parentItem );
427 const QgsFields fields = layer->
fields();
428 for (
int fieldIdx = 0; fieldIdx < fields.
count(); ++fieldIdx )
430 const QgsField field = fields.
at( fieldIdx );
434 item->setData( label, Qt::UserRole );
439 item->setIcon( icon );
440 parentItem->appendRow( item );
446 for (
int i = 0; i < fields.
count(); ++i )
457 if ( mExpressionGroups.contains( QStringLiteral(
"Fields and Values" ) ) )
459 QgsExpressionItem *node = mExpressionGroups.value( QStringLiteral(
"Fields and Values" ) );
460 node->removeRows( 0, node->rowCount() );
463 registerItem( QStringLiteral(
"Fields and Values" ), QStringLiteral(
"NULL" ), QStringLiteral(
"NULL" ), QString(),
QgsExpressionItem::ExpressionNode,
false, -1 );
474 registerItem( QStringLiteral(
"Fields and Values" ), QStringLiteral(
"feature" ), QStringLiteral(
"@feature" ), currentFeatureHelp,
QgsExpressionItem::ExpressionNode,
false, -1 );
475 registerItem( QStringLiteral(
"Fields and Values" ), QStringLiteral(
"id" ), QStringLiteral(
"@id" ), currentFeatureIdHelp,
QgsExpressionItem::ExpressionNode,
false, -1 );
476 registerItem( QStringLiteral(
"Fields and Values" ), QStringLiteral(
"geometry" ), QStringLiteral(
"@geometry" ), currentGeometryHelp,
QgsExpressionItem::ExpressionNode,
false, -1 );
480 registerItem( QStringLiteral(
"Variables" ), QStringLiteral(
"geometry" ), QStringLiteral(
"@geometry" ), currentGeometryHelp,
QgsExpressionItem::ExpressionNode,
false );
482 registerItem( QStringLiteral(
"Record and Attributes" ), QStringLiteral(
"feature" ), QStringLiteral(
"@feature" ), currentFeatureHelp,
QgsExpressionItem::ExpressionNode,
true, -1 );
483 registerItem( QStringLiteral(
"Record and Attributes" ), QStringLiteral(
"id" ), QStringLiteral(
"@id" ), currentFeatureIdHelp,
QgsExpressionItem::ExpressionNode,
true, -1 );
484 registerItem( QStringLiteral(
"Record and Attributes" ), QStringLiteral(
"geometry" ), QStringLiteral(
"@geometry" ), currentGeometryHelp,
QgsExpressionItem::ExpressionNode,
true, -1 );
491 const QgsFields &fields = mLayer->fields();
496void QgsExpressionTreeView::loadRelations()
501 QMap<QString, QgsRelation> relations = mProject->relationManager()->relations();
502 QMap<QString, QgsRelation>::const_iterator relIt = relations.constBegin();
503 for ( ; relIt != relations.constEnd(); ++relIt )
505 registerItemForAllGroups( QStringList() << tr(
"Relations" ), relIt->name(), QStringLiteral(
"'%1'" ).arg( relIt->id() ),
formatRelationHelp( relIt.value() ) );
511 mRecentKey = collection;
512 QString name = tr(
"Recent (%1)" ).arg( collection );
513 if ( mExpressionGroups.contains( name ) )
516 node->removeRows( 0, node->rowCount() );
520 const QString location = QStringLiteral(
"/expressions/recent/%1" ).arg( collection );
521 const QStringList expressions = settings.
value( location ).toStringList();
523 for (
const QString &expression : expressions )
526 QString label = expression;
527 label.replace(
'\n',
' ' );
536 QString location = QStringLiteral(
"/expressions/recent/%1" ).arg( collection );
537 QStringList expressions = settings.
value( location ).toStringList();
538 expressions.removeAll( expressionText );
540 expressions.prepend( expressionText );
542 while ( expressions.count() > 20 )
544 expressions.pop_back();
547 settings.
setValue( location, expressions );
554 const QString location = QStringLiteral(
"user" );
557 settings.
setValue( QStringLiteral(
"expression" ), expression );
558 settings.
setValue( QStringLiteral(
"helpText" ), helpText );
561 const QModelIndexList idxs { mModel->match( mModel->index( 0, 0 ), Qt::DisplayRole, label, 1, Qt::MatchFlag::MatchRecursive ) };
562 if ( !idxs.isEmpty() )
564 scrollTo( idxs.first() );
579 if ( mExpressionGroups.contains( QStringLiteral(
"UserGroup" ) ) )
581 QgsExpressionItem *node = mExpressionGroups.value( QStringLiteral(
"UserGroup" ) );
582 node->removeRows( 0, node->rowCount() );
586 const QString location = QStringLiteral(
"user" );
592 for (
const auto &label : std::as_const( mUserExpressionLabels ) )
595 expression = settings.
value( QStringLiteral(
"expression" ) ).toString();
604 return mUserExpressionLabels;
609 const QString group = QStringLiteral(
"user" );
611 QJsonArray exportList;
612 QJsonObject exportObject {
614 {
"exported_at", QDateTime::currentDateTime().toString( Qt::ISODate ) },
616 {
"expressions", exportList }
623 for (
const QString &label : std::as_const( mUserExpressionLabels ) )
627 const QString expression = settings.
value( QStringLiteral(
"expression" ) ).toString();
628 const QString helpText = settings.
value( QStringLiteral(
"helpText" ) ).toString();
629 const QJsonObject expressionObject {
631 {
"type",
"expression" },
632 {
"expression", expression },
634 {
"description", helpText }
636 exportList.push_back( expressionObject );
641 exportObject[QStringLiteral(
"expressions" )] = exportList;
642 QJsonDocument exportJson = QJsonDocument( exportObject );
650 if ( !expressionsDocument.isObject() )
653 QJsonObject expressionsObject = expressionsDocument.object();
656 if ( !expressionsObject[QStringLiteral(
"qgis_version" )].isString()
657 || !expressionsObject[QStringLiteral(
"exported_at" )].isString()
658 || !expressionsObject[QStringLiteral(
"author" )].isString()
659 || !expressionsObject[QStringLiteral(
"expressions" )].isArray() )
663 QVersionNumber qgisJsonVersion = QVersionNumber::fromString( expressionsObject[QStringLiteral(
"qgis_version" )].toString() );
664 QVersionNumber qgisVersion = QVersionNumber::fromString(
Qgis::version() );
668 if ( qgisJsonVersion > qgisVersion )
670 QMessageBox::StandardButtons buttons = QMessageBox::Yes | QMessageBox::No;
671 switch ( QMessageBox::question(
this, tr(
"QGIS Version Mismatch" ), tr(
"The imported expressions are from newer version of QGIS (%1) "
672 "and some of the expression might not work the current version (%2). "
673 "Are you sure you want to continue?" )
674 .arg( qgisJsonVersion.toString(), qgisVersion.toString() ),
677 case QMessageBox::No:
680 case QMessageBox::Yes:
689 QStringList skippedExpressionLabels;
690 bool isApplyToAll =
false;
691 bool isOkToOverwrite =
false;
697 const QJsonArray expressions = expressionsObject[QStringLiteral(
"expressions" )].toArray();
698 for (
const QJsonValue &&expressionValue : expressions )
701 if ( !expressionValue.isObject() )
704 skippedExpressionLabels.append( expressionValue.toString() );
708 QJsonObject expressionObj = expressionValue.toObject();
711 if ( !expressionObj[QStringLiteral(
"name" )].isString()
712 || !expressionObj[QStringLiteral(
"type" )].isString()
713 || !expressionObj[QStringLiteral(
"expression" )].isString()
714 || !expressionObj[QStringLiteral(
"group" )].isString()
715 || !expressionObj[QStringLiteral(
"description" )].isString() )
718 if ( !expressionObj[QStringLiteral(
"name" )].toString().isEmpty() )
719 skippedExpressionLabels.append( expressionObj[QStringLiteral(
"name" )].toString() );
721 skippedExpressionLabels.append( expressionObj[QStringLiteral(
"expression" )].toString() );
727 if ( expressionObj[QStringLiteral(
"type" )].toString() != QLatin1String(
"expression" ) )
729 skippedExpressionLabels.append( expressionObj[QStringLiteral(
"name" )].toString() );
734 if ( expressionObj[QStringLiteral(
"group" )].toString() != QLatin1String(
"user" ) )
736 skippedExpressionLabels.append( expressionObj[QStringLiteral(
"name" )].toString() );
740 const QString label = expressionObj[QStringLiteral(
"name" )].toString();
741 const QString expression = expressionObj[QStringLiteral(
"expression" )].toString();
742 const QString helpText = expressionObj[QStringLiteral(
"description" )].toString();
745 if ( label.contains( QLatin1String(
"\\" ) ) || label.contains(
'/' ) )
747 skippedExpressionLabels.append( expressionObj[QStringLiteral(
"name" )].toString() );
752 const QString oldExpression = settings.
value( QStringLiteral(
"expression" ) ).toString();
756 if ( mUserExpressionLabels.contains( label ) && expression != oldExpression )
759 showMessageBoxConfirmExpressionOverwrite( isApplyToAll, isOkToOverwrite, label, oldExpression, expression );
761 if ( isOkToOverwrite )
765 skippedExpressionLabels.append( label );
777 if ( !skippedExpressionLabels.isEmpty() )
779 QStringList skippedExpressionLabelsQuoted;
780 skippedExpressionLabelsQuoted.reserve( skippedExpressionLabels.size() );
781 for (
const QString &skippedExpressionLabel : skippedExpressionLabels )
782 skippedExpressionLabelsQuoted.append( QStringLiteral(
"'%1'" ).arg( skippedExpressionLabel ) );
784 QMessageBox::information(
this, tr(
"Skipped Expression Imports" ), QStringLiteral(
"%1\n%2" ).arg( tr(
"The following expressions have been skipped:" ), skippedExpressionLabelsQuoted.join( QLatin1String(
", " ) ) ) );
790 QList<QgsExpressionItem *> result;
791 const QList<QStandardItem *> found { mModel->findItems( label, Qt::MatchFlag::MatchRecursive ) };
792 result.reserve( found.size() );
793 std::transform( found.begin(), found.end(), std::back_inserter( result ), []( QStandardItem *item ) ->
QgsExpressionItem * { return static_cast<QgsExpressionItem *>( item ); } );
797void QgsExpressionTreeView::showMessageBoxConfirmExpressionOverwrite(
799 bool &isOkToOverwrite,
800 const QString &label,
801 const QString &oldExpression,
802 const QString &newExpression
805 QMessageBox::StandardButtons buttons = QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll;
806 switch ( QMessageBox::question(
this, tr(
"Expression Overwrite" ), tr(
"The expression with label '%1' was already defined."
807 "The old expression \"%2\" will be overwritten by \"%3\"."
808 "Are you sure you want to overwrite the expression?" )
809 .arg( label, oldExpression, newExpression ),
812 case QMessageBox::NoToAll:
814 isOkToOverwrite =
false;
817 case QMessageBox::No:
818 isApplyToAll =
false;
819 isOkToOverwrite =
false;
822 case QMessageBox::YesToAll:
824 isOkToOverwrite =
true;
827 case QMessageBox::Yes:
828 isApplyToAll =
false;
829 isOkToOverwrite =
true;
846 setFilterCaseSensitivity( Qt::CaseInsensitive );
851 QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
857 int count = sourceModel()->rowCount( index );
858 bool matchchild =
false;
859 for (
int i = 0; i < count; ++i )
871 const QString name = sourceModel()->data( index, Qt::DisplayRole ).toString();
872 if ( name.contains( mFilterString, Qt::CaseInsensitive ) )
878 return std::any_of( tags.begin(), tags.end(), [
this](
const QString &tag ) {
879 return tag.contains( mFilterString, Qt::CaseInsensitive );
885 mFilterString = string;
893 if ( leftSort != rightSort )
894 return leftSort < rightSort;
896 QString leftString = sourceModel()->data( left, Qt::DisplayRole ).toString();
897 QString rightString = sourceModel()->data( right, Qt::DisplayRole ).toString();
900 if ( leftString.startsWith(
'$' ) )
901 leftString = leftString.mid( 1 );
902 if ( rightString.startsWith(
'$' ) )
903 rightString = rightString.mid( 1 );
905 return QString::localeAwareCompare( leftString, rightString ) < 0;
static QString version()
Version string.
static QString userFullName()
Returns the user's operating system login account full display name.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
bool isContextual() const
Returns whether the function is only available if provided by a QgsExpressionContext object.
int params() const
The number of parameters this function takes.
QStringList groups() const
Returns a list of the groups the function belongs to.
virtual bool isDeprecated() const
Returns true if the function is deprecated and should not be presented as a valid option to users in ...
QString name() const
The name of the function.
const QString helpText() const
The help text for the function.
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
void setFilterString(const QString &string)
Sets the search filter string.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
QgsExpressionItemSearchProxy()
An expression item that can be used in the QgsExpressionBuilderWidget tree.
static const int LAYER_ID_ROLE
Layer ID role.
QString getExpressionText() const
static const int SEARCH_TAGS_ROLE
Search tags role.
static const int ITEM_TYPE_ROLE
Item type role.
static const int CUSTOM_SORT_ROLE
Custom sort order role.
QgsExpressionItem::ItemType getItemType() const
Gets the type of expression item, e.g., header, field, ExpressionNode.
static const int ITEM_NAME_ROLE
Item name role.
QgsProject * project()
Returns the project currently associated with the widget.
void refresh()
Refreshes the content of the tree.
void setProject(QgsProject *project)
Sets the project currently associated with the widget.
void saveToUserExpressions(const QString &label, const QString &expression, const QString &helpText)
Stores the user expression with given label and helpText.
QgsExpressionItem * currentItem() const
Returns the current item or a nullptr.
void setLayer(QgsVectorLayer *layer)
Sets layer in order to get the fields and values.
void setMenuProvider(MenuProvider *provider)
Sets the menu provider.
QStringList userExpressionLabels() const
Returns the user expression labels.
void expressionItemDoubleClicked(const QString &text)
Emitted when a expression item is double clicked.
void currentExpressionItemChanged(QgsExpressionItem *item)
Emitter when the current expression item changed.
QJsonDocument exportUserExpressions()
Create the expressions JSON document storing all the user expressions to be exported.
QgsExpressionTreeView(QWidget *parent=nullptr)
Constructor.
void loadExpressionsFromJson(const QJsonDocument &expressionsDocument)
Load and permanently store the expressions from the expressions JSON document.
void saveToRecent(const QString &expressionText, const QString &collection="generic")
Adds the current expression to the given collection.
void setSearchText(const QString &text)
Sets the text to filter the expression tree.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context for the tree view.
const QList< QgsExpressionItem * > findExpressions(const QString &label)
Returns the list of expression items matching a label.
void removeFromUserExpressions(const QString &label)
Removes the expression label from the user stored expressions.
void loadRecent(const QString &collection=QStringLiteral("generic"))
Loads the recent expressions from the given collection.
void loadUserExpressions()
Loads the user expressions.
Q_DECL_DEPRECATED QStandardItemModel * model()
Returns a pointer to the dialog's function item model.
void loadFieldNames(const QgsFields &fields)
This allows loading fields without specifying a layer.
static const QList< QgsExpressionFunction * > & Functions()
static int functionCount()
Returns the number of functions defined in the parser.
static QString variableHelpText(const QString &variableName)
Returns the help text for a specified variable.
static QString formatPreviewString(const QVariant &value, bool htmlOutput=true, int maximumPreviewLength=60)
Formats an expression result for friendly display to the user.
static QStringList tags(const QString &name)
Returns a string list of search tags for a specified function.
static QString group(const QString &group)
Returns the translated name for a function group.
Encapsulate a field in an attribute table or data source.
QString displayNameWithAlias() const
Returns the name to use when displaying this field and adds the alias in parenthesis if it is defined...
Container of fields for a vector layer.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
QIcon iconForField(int fieldIdx, bool considerOrigin=false) const
Returns an icon corresponding to a field index, based on the field's type and source.
static QIcon iconForLayer(const QgsMapLayer *layer)
Returns the icon corresponding to a specified map layer.
Base class for all map layer types.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Represents a relationship between two vector layers.
Stores settings for use within QGIS.
QStringList childGroups(Qgis::SettingsOrigin origin=Qgis::SettingsOrigin::Any) const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
void endGroup()
Resets the group to what it was before the corresponding beginGroup() call.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Represents a vector layer which manages a vector based dataset.
QString formatLayerHelp(const QgsMapLayer *layer)
Returns a HTML formatted string for use as a layer item help.
QString formatUserExpressionHelp(const QString &label, const QString &expression, const QString &description)
Returns a HTML formatted string for use as a user expression item help.
QString formatVariableHelp(const QString &variable, const QString &description, bool showValue, const QVariant &value)
Returns a HTML formatted string for use as a variable item help.
QString formatRecentExpressionHelp(const QString &label, const QString &expression)
Returns a HTML formatted string for use as a recent expression item help.
QString formatRelationHelp(const QgsRelation &relation)
Returns a HTML formatted string for use as a relation item help.