26 #include <nlohmann/json.hpp> 43 return QStringLiteral(
"ValueRelation" );
50 if ( cache.isValid() )
59 if ( config.value( QStringLiteral(
"AllowMulti" ) ).toBool() )
63 if ( layer->
fields().
at( fieldIndex ).
type() == QVariant::Map )
66 keyList = value.toStringList();
73 QStringList valueList;
77 if ( keyList.contains( item.key.toString() ) )
79 valueList << item.value;
83 return valueList.join( QStringLiteral(
", " ) ).prepend(
'{' ).append(
'}' );
94 if ( item.key == value )
101 return QStringLiteral(
"(%1)" ).arg( value.toString() );
112 Q_UNUSED( fieldIndex )
113 return QVariant::fromValue<ValueRelationCache>(
createCache( config ) );
127 int ki = fields.
indexOf( config.value( QStringLiteral(
"Key" ) ).toString() );
128 int vi = fields.
indexOf( config.value( QStringLiteral(
"Value" ) ).toString() );
135 const QString expression = config.value( QStringLiteral(
"FilterExpression" ) ).toString();
157 if ( config.value( QStringLiteral(
"OrderByValue" ) ).toBool() )
171 QStringList checkList;
172 if ( value.type() == QVariant::StringList )
174 checkList = value.toStringList();
176 else if ( value.type() == QVariant::String )
179 auto newVal { value };
180 if ( newVal.toString().trimmed().startsWith(
'{' ) )
182 newVal = QVariant( newVal.toString().trimmed().mid( 1 ).mid( 0, newVal.toString().length() - 2 ).prepend(
'[' ).append(
']' ) );
184 if ( newVal.toString().trimmed().startsWith(
'[' ) )
188 for (
auto &element :
json::parse( newVal.toString().toStdString() ) )
190 if ( element.is_number_integer() )
192 checkList << QString::number( element.get<
int>() );
194 else if ( element.is_number_unsigned() )
196 checkList << QString::number( element.get<
unsigned>() );
198 else if ( element.is_string() )
200 checkList << QString::fromStdString( element.get<std::string>() );
204 catch ( json::parse_error &ex )
206 qDebug() << QString::fromStdString( ex.what() );
210 else if ( value.type() == QVariant::List )
212 QVariantList valuesList( value.toList( ) );
213 checkList.reserve( valuesList.size() );
214 for (
const QVariant &listItem : qgis::as_const( valuesList ) )
216 QString v( listItem.toString( ) );
218 checkList.append( v );
228 QSet< QString > formVariables = scope->variableNames().toSet();
230 formVariables.intersect( usedVariables );
231 return formVariables;
241 QSet<QString> attributes;
245 const QSet<QString> formFunctions( scope->functionNames()
250 for (
const auto &f : expFunctions )
253 if ( formFunctions.contains( fd->
name( ) ) )
255 for (
const auto ¶m : f->args( )->list() )
257 attributes.insert( param->eval( &exp, &context ).toString() );
267 for (
auto it = attrs.constBegin() ; it != attrs.constEnd(); it++ )
269 if ( ! feature.
attribute( *it ).isValid() )
280 config.value( QStringLiteral(
"LayerName" ) ).toString(),
281 config.value( QStringLiteral(
"LayerSource" ) ).toString(),
282 config.value( QStringLiteral(
"LayerProviderName" ) ).toString() };
bool isValid() const
Returns the validity of this feature.
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
TYPE * resolveByIdOrNameOnly(const QgsProject *project)
Resolves the map layer by attempting to find a matching layer in a project using a weak match...
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Container of fields for a vector layer.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
static QgsExpressionContextScope * formScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current attribute form/table feat...
QSet< QString > referencedVariables() const
Returns a list of all variables which are used in this expression.
bool qgsVariantLessThan(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether the first is less than the second.
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Reads and writes project states.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
An expression node for expression functions.
QString name() const
The name of the function.
A abstract base class for defining QgsExpression functions.
int indexOf(const QString &fieldName) const
Gets the field index from the field name.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QList< int > QgsAttributeList
bool nextFeature(QgsFeature &f)
Geometry is not required. It may still be returned if e.g. required for a filter condition.
QgsSQLStatement::Node * parse(const QString &str, QString &parserErrorMsg)
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
QList< const T * > findNodes() const
Returns a list of all nodes of the given class which are used in this expression. ...
QSet< QString > referencedFunctions() const
Returns a list of the names of all functions which are used in this expression.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.