28 #include <nlohmann/json.hpp> 45 return QStringLiteral(
"ValueRelation" );
52 if ( cache.isValid() )
61 if ( config.value( QStringLiteral(
"AllowMulti" ) ).toBool() )
65 if ( layer->
fields().
at( fieldIndex ).
type() == QVariant::Map )
68 keyList = value.toStringList();
72 keyList = valueToStringList( value );
75 QStringList valueList;
79 if ( keyList.contains( item.key.toString() ) )
81 valueList << item.value;
85 return valueList.join( QStringLiteral(
", " ) ).prepend(
'{' ).append(
'}' );
96 if ( item.key == value )
103 return QStringLiteral(
"(%1)" ).arg( value.toString() );
108 return representValue( layer, fieldIndex, config, cache, value );
114 Q_UNUSED( fieldIndex )
115 return QVariant::fromValue<ValueRelationCache>( createCache( config ) );
129 int ki = fields.
indexOf( config.value( QStringLiteral(
"Key" ) ).toString() );
130 int vi = fields.
indexOf( config.value( QStringLiteral(
"Value" ) ).toString() );
137 const QString expression = config.value( QStringLiteral(
"FilterExpression" ) ).toString();
141 if ( ! expression.isEmpty() && ( ! expressionRequiresFormScope( expression )
142 || expressionIsUsable( expression, formFeature ) ) )
159 if ( config.value( QStringLiteral(
"OrderByValue" ) ).toBool() )
173 QStringList checkList;
174 if ( value.type() == QVariant::StringList )
176 checkList = value.toStringList();
180 QVariantList valuesList;
181 if ( value.type() == QVariant::String )
184 auto newVal { value };
185 if ( newVal.toString().trimmed().startsWith(
'{' ) )
190 else if ( newVal.toString().trimmed().startsWith(
'[' ) )
195 for (
auto &element :
json::parse( newVal.toString().toStdString() ) )
197 if ( element.is_number_integer() )
199 valuesList.push_back( element.get<
int>() );
201 else if ( element.is_number_unsigned() )
203 valuesList.push_back( element.get<
unsigned>() );
205 else if ( element.is_string() )
207 valuesList.push_back( QString::fromStdString( element.get<std::string>() ) );
211 catch ( json::parse_error &ex )
213 QgsMessageLog::logMessage( QObject::tr(
"Cannot parse JSON like string '%1' Error: %2" ).arg( newVal.toString(), ex.what() ) );
217 else if ( value.type() == QVariant::List )
219 valuesList = value.toList( );
222 checkList.reserve( valuesList.size() );
223 for (
const QVariant &listItem : qgis::as_const( valuesList ) )
225 QString v( listItem.toString( ) );
227 checkList.append( v );
237 QSet< QString > formVariables = scope->variableNames().toSet();
239 formVariables.intersect( usedVariables );
240 return formVariables;
245 return !( expressionFormAttributes( expression ).isEmpty() && expressionFormVariables( expression ).isEmpty() );
250 QSet<QString> attributes;
254 const QSet<QString> formFunctions( scope->functionNames()
259 for (
const auto &f : expFunctions )
262 if ( formFunctions.contains( fd->
name( ) ) )
264 for (
const auto ¶m : f->args( )->list() )
266 attributes.insert( param->eval( &exp, &context ).toString() );
275 const QSet<QString> attrs = expressionFormAttributes( expression );
276 for (
auto it = attrs.constBegin() ; it != attrs.constEnd(); it++ )
278 if ( ! feature.
attribute( *it ).isValid() )
281 if ( ! expressionFormVariables( expression ).isEmpty() && feature.
geometry().
isEmpty( ) )
289 config.value( QStringLiteral(
"LayerName" ) ).toString(),
290 config.value( QStringLiteral(
"LayerSource" ) ).toString(),
291 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.
static QVariantList parseArray(const QString &string)
Returns a QVariantList created out of a string containing an array in postgres array format {1...
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...
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
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.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
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.