25 #include <QRegularExpression>
29 , mDescription( description )
30 , mStandardType( type )
34 switch ( mStandardType )
38 mHelpText = QObject::tr(
"bool [<b>1</b>=True|<b>0</b>=False]" );
43 mHelpText = QObject::tr(
"int [≤ 0 ≥]" );
48 mHelpText = QObject::tr(
"int [≥ 0]" );
53 mHelpText = QObject::tr(
"int [≥ 1]" );
58 mHelpText = QObject::tr(
"double [≤ 0.0 ≥]" );
63 mHelpText = QObject::tr(
"double [≥ 0.0]" );
68 mHelpText = QObject::tr(
"double [0.0-1.0]" );
73 mHelpText = QObject::tr(
"double [0.0-360.0]" );
78 mHelpText = QObject::tr(
"string of variable length" );
83 mHelpText = QObject::tr(
"int [0-100]" );
88 mHelpText = trString() + QStringLiteral(
"[<b>MM</b>|<b>MapUnit</b>|<b>Pixel</b>|<b>Point</b>]" );
93 mHelpText = QObject::tr(
"string [<b>r,g,b,a</b>] as int 0-255 or #<b>AARRGGBB</b> as hex or <b>color</b> as color's name" );
98 mHelpText = QObject::tr(
"string [<b>r,g,b</b>] as int 0-255 or #<b>RRGGBB</b> as hex or <b>color</b> as color's name" );
103 mHelpText = trString() + QStringLiteral(
"[<b>bevel</b>|<b>miter</b>|<b>round</b>]" );
108 mHelpText = trString() + QStringLiteral(
"[<b>Normal</b>|<b>Lighten</b>|<b>Screen</b>|<b>Dodge</b>|<br>"
109 "<b>Addition</b>|<b>Darken</b>|<b>Multiply</b>|<b>Burn</b>|<b>Overlay</b>|<br>"
110 "<b>SoftLight</b>|<b>HardLight</b>|<b>Difference</b>|<b>Subtract</b>]" );
115 mHelpText = QObject::tr(
"double coord [<b>X,Y</b>]" );
120 mHelpText = QObject::tr(
"double [≥ 0.0]" );
125 mHelpText = QObject::tr(
"string of doubles '<b>width,height</b>' or array of doubles <b>[width, height]</b>" );
130 mHelpText = trString() + QStringLiteral(
"[<b>no</b>|<b>solid</b>|<b>dash</b>|<b>dot</b>|<b>dash dot</b>|<b>dash dot dot</b>]" );
135 mHelpText = QObject::tr(
"double [≥ 0.0]" );
140 mHelpText = trString() + QStringLiteral(
"[<b>solid</b>|<b>horizontal</b>|<b>vertical</b>|<b>cross</b>|<b>b_diagonal</b>|<b>f_diagonal"
141 "</b>|<b>diagonal_x</b>|<b>dense1</b>|<b>dense2</b>|<b>dense3</b>|<b>dense4</b>|<b>dense5"
142 "</b>|<b>dense6</b>|<b>dense7</b>|<b>no]" );
147 mHelpText = trString() + QStringLiteral(
"[<b>square</b>|<b>flat</b>|<b>round</b>]" );
152 mHelpText = trString() + QStringLiteral(
"[<b>left</b>|<b>center</b>|<b>right</b>]" );
157 mHelpText = trString() + QStringLiteral(
"[<b>top</b>|<b>center</b>|<b>bottom</b>]" );
162 mHelpText = trString() + QStringLiteral(
"[<b>filepath</b>] as<br>"
163 "<b>''</b>=empty|absolute|search-paths-relative|<br>"
164 "project-relative|URL" );
169 mHelpText = QObject::tr(
"string of doubles '<b>x,y</b>' or array of doubles <b>[x, y]</b>" );
174 mHelpText = QObject::tr(
"DateTime or string representation of a DateTime" );
184 , mDescription( description )
186 , mHelpText( helpText )
188 , mComment( comment )
197 QString QgsPropertyDefinition::trString()
200 return QObject::tr(
"string " );
209 QVariantMap variantMap;
210 QMap<QString, QgsProperty>::const_iterator it = propertyMap.constBegin();
211 for ( ; it != propertyMap.constEnd(); ++it )
212 variantMap.insert( it.key(), it.value().toVariant() );
218 QMap<QString, QgsProperty> propertyMap;
219 QVariantMap::const_iterator it = variantMap.constBegin();
220 for ( ; it != variantMap.constEnd(); ++it )
224 propertyMap.insert( it.key(), property );
231 d =
new QgsPropertyPrivate();
272 return d->active == other.d->active
273 && d->type == other.d->type
274 && ( d->type !=
StaticProperty || d->staticValue == other.d->staticValue )
277 && ( ( !d->transformer && !other.d->transformer ) || ( d->transformer && other.d->transformer && d->transformer->toExpression( QString() ) == other.d->transformer->toExpression( QString() ) ) );
282 return ( !( ( *
this ) == other ) );
287 return static_cast< Type >( d->type );
305 d->staticValue =
value;
313 return d->staticValue;
320 d->fieldName =
field;
321 d->cachedFieldIdx = -1;
332 QgsProperty::operator bool()
const
334 return d->type != InvalidProperty;
341 d->expressionString = expression;
343 d->expressionPrepared =
false;
344 d->expressionIsInvalid =
false;
352 return d->expressionString;
370 exp = d->expressionString;
377 return d->transformer ? d->transformer->toExpression( exp ) : exp;
402 if ( !d->expression.prepare( &context ) )
404 d->expressionReferencedCols.clear();
405 d->expressionPrepared =
false;
406 d->expressionIsInvalid =
true;
410 d->expressionPrepared =
true;
411 d->expressionIsInvalid =
false;
412 d->expressionReferencedCols = d->expression.referencedColumns();
427 return QSet<QString>();
433 return QSet<QString>();
437 QSet< QString > fields;
438 if ( !d->fieldName.isEmpty() )
439 fields.insert( d->fieldName );
447 return d->expression.referencedColumns();
450 if ( d->expressionIsInvalid )
451 return QSet< QString >();
454 if ( !d->expressionPrepared && !
prepare( context ) )
456 d->expressionIsInvalid =
true;
457 return QSet< QString >();
460 return d->expressionReferencedCols;
464 return QSet<QString>();
469 const QRegularExpression rx( QStringLiteral(
"^project_color\\('.*'\\)$" ) );
471 && rx.match( d->expressionString ).hasMatch();
474 QVariant QgsProperty::propertyValue(
const QgsExpressionContext &context,
const QVariant &defaultValue,
bool *ok )
const
488 return d->staticValue;
498 if ( d->cachedFieldIdx >= 0 )
505 if ( d->cachedFieldIdx < 0 )
515 if ( d->expressionIsInvalid )
518 if ( !d->expressionPrepared && !
prepare( context ) )
521 QVariant result = d->expression.evaluate( &context );
522 if ( !result.isNull() )
549 QVariant val = propertyValue( context, defaultValue, &valOk );
550 if ( !d->transformer && !valOk )
553 if ( d->transformer )
557 val = d->transformer->transform( context, val );
569 const QVariant val =
value( context, defaultDateTime, &valOk );
571 if ( !valOk || val.isNull() )
575 return defaultDateTime;
579 if ( val.type() == QVariant::DateTime )
581 dateTime = val.value<QDateTime>();
585 dateTime = val.toDateTime();
588 if ( !dateTime.isValid() )
589 return defaultDateTime;
601 const QVariant val =
value( context, defaultString, &valOk );
603 if ( !valOk || val.isNull() )
607 return defaultString;
613 return val.toString();
623 const QVariant val =
value( context, defaultColor, &valOk );
625 if ( !valOk || val.isNull() )
629 if ( val.type() == QVariant::Color )
631 color = val.value<QColor>();
638 if ( !color.isValid() )
654 const QVariant val =
value( context, defaultValue, &valOk );
656 if ( !valOk || val.isNull() )
659 bool convertOk =
false;
660 const double dbl = val.toDouble( &convertOk );
677 const QVariant val =
value( context, defaultValue, &valOk );
679 if ( !valOk || val.isNull() )
682 bool convertOk =
false;
683 const int integer = val.toInt( &convertOk );
687 const double dbl = val.toDouble( &convertOk );
692 return std::round( dbl );
713 const QVariant val =
value( context, defaultValue, &valOk );
715 if ( !valOk || val.isNull() )
725 QVariantMap propertyMap;
727 propertyMap.insert( QStringLiteral(
"active" ), d->active );
728 propertyMap.insert( QStringLiteral(
"type" ), d->type );
734 propertyMap.insert( QStringLiteral(
"val" ), d->staticValue.toString() );
738 propertyMap.insert( QStringLiteral(
"field" ), d->fieldName );
742 propertyMap.insert( QStringLiteral(
"expression" ), d->expressionString );
749 if ( d->transformer )
752 transformer.insert( QStringLiteral(
"t" ), d->transformer->transformerType() );
753 transformer.insert( QStringLiteral(
"d" ), d->transformer->toVariant() );
755 propertyMap.insert( QStringLiteral(
"transformer" ),
transformer );
763 const QVariantMap propertyMap =
property.toMap();
766 d->active = propertyMap.value( QStringLiteral(
"active" ) ).toBool();
767 d->type =
static_cast< Type >( propertyMap.value( QStringLiteral(
"type" ),
InvalidProperty ).toInt() );
772 d->staticValue = propertyMap.value( QStringLiteral(
"val" ) );
777 d->fieldName = propertyMap.value( QStringLiteral(
"field" ) ).toString();
778 if ( d->fieldName.isEmpty() )
783 d->expressionString = propertyMap.value( QStringLiteral(
"expression" ) ).toString();
784 if ( d->expressionString.isEmpty() )
788 d->expressionPrepared =
false;
789 d->expressionIsInvalid =
false;
790 d->expressionReferencedCols.clear();
799 delete d->transformer;
800 d->transformer =
nullptr;
803 const QVariant transform = propertyMap.value( QStringLiteral(
"transformer" ) );
805 if ( transform.isValid() )
807 const QVariantMap transformerMap = transform.toMap();
831 return d->transformer;
839 if ( d->transformer )
842 QString baseExpression;
850 if ( !fieldName.isEmpty() )
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsFeature feature() const
Convenience function for retrieving the feature for the context, if set.
QgsFields fields() const
Convenience function for retrieving the fields for the context, if set.
Class for parsing and evaluation of expressions (formerly called "search strings").
static QString quotedValue(const QVariant &value)
Returns a string representation of a literal value, including appropriate quotations where required.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool isValid() const
Returns the validity of this feature.
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Container of fields for a vector layer.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
StandardPropertyTemplate
Predefined standard property templates.
@ HorizontalAnchor
Horizontal anchor point.
@ Double
Double value (including negative values)
@ VerticalAnchor
Vertical anchor point.
@ Double0To1
Double value between 0-1 (inclusive)
@ FillStyle
Fill style (eg solid, lines)
@ StrokeWidth
Line stroke width.
@ LineStyle
Line style (eg solid/dashed)
@ Integer
Integer value (including negative values)
@ String
Any string value.
@ DateTime
DateTime value.
@ RenderUnits
Render units (eg mm/pixels/map units)
@ PenJoinStyle
Pen join style.
@ SvgPath
Path to an SVG file.
@ IntegerPositiveGreaterZero
Non-zero positive integer values.
@ IntegerPositive
Positive integer values (including 0)
@ CapStyle
Line cap style (eg round)
@ ColorNoAlpha
Color with no alpha channel.
@ Rotation
Rotation (value between 0-360 degrees)
@ Custom
Custom property types.
@ Size
1D size (eg marker radius, or square marker height/width)
@ ColorWithAlpha
Color with alpha channel.
@ DoublePositive
Positive double value (including 0)
@ Size2D
2D size (width/height different)
bool supportsAssistant() const
Returns true if the property is of a type which is compatible with property override assistants.
QgsPropertyDefinition()=default
Constructs an empty property.
DataType
Valid data types required by property.
@ DataTypeString
Property requires a string value.
@ DataTypeBoolean
Property requires a boolean value.
@ DataTypeNumeric
Property requires a numeric value.
A store for object properties.
QDateTime valueAsDateTime(const QgsExpressionContext &context, const QDateTime &defaultDateTime=QDateTime(), bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a datetime.
@ ExpressionBasedProperty
Expression based property (QgsExpressionBasedProperty)
@ StaticProperty
Static property (QgsStaticProperty)
@ FieldBasedProperty
Field based property (QgsFieldBasedProperty)
@ InvalidProperty
Invalid (not set) property.
QColor valueAsColor(const QgsExpressionContext &context, const QColor &defaultColor=QColor(), bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a color.
bool isProjectColor() const
Returns true if the property is set to a linked project color.
QSet< QString > referencedFields(const QgsExpressionContext &context=QgsExpressionContext(), bool ignoreContext=false) const
Returns the set of any fields referenced by the property for a specified expression context.
QString asExpression() const
Returns an expression string representing the state of the property, or an empty string if the proper...
bool operator==(const QgsProperty &other) const
static QVariantMap propertyMapToVariantMap(const QMap< QString, QgsProperty > &propertyMap)
Convert a map of QgsProperty to a map of QVariant This is useful to save a map of properties.
QString expressionString() const
Returns the expression used for the property value.
bool convertToTransformer()
Attempts to convert an existing expression based property to a base expression with corresponding tra...
void setTransformer(QgsPropertyTransformer *transformer)
Sets an optional transformer to use for manipulating the calculated values for the property.
void setStaticValue(const QVariant &value)
Sets the static value for the property.
QString valueAsString(const QgsExpressionContext &context, const QString &defaultString=QString(), bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a string.
QString field() const
Returns the current field name the property references.
const QgsPropertyTransformer * transformer() const
Returns the existing transformer used for manipulating the calculated values for the property,...
QVariant value(const QgsExpressionContext &context, const QVariant &defaultValue=QVariant(), bool *ok=nullptr) const
Calculates the current value of the property, including any transforms which are set for the property...
static QgsProperty fromExpression(const QString &expression, bool isActive=true)
Returns a new ExpressionBasedProperty created from the specified expression.
QVariant toVariant() const
Saves this property to a QVariantMap, wrapped in a QVariant.
bool isActive() const
Returns whether the property is currently active.
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
static QMap< QString, QgsProperty > variantMapToPropertyMap(const QVariantMap &variantMap)
Convert a map of QVariant to a map of QgsProperty This is useful to restore a map of properties.
void setField(const QString &field)
Sets the field name the property references.
int valueAsInt(const QgsExpressionContext &context, int defaultValue=0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as an integer.
bool valueAsBool(const QgsExpressionContext &context, bool defaultValue=false, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as an boolean.
bool prepare(const QgsExpressionContext &context=QgsExpressionContext()) const
Prepares the property against a specified expression context.
bool loadVariant(const QVariant &property)
Loads this property from a QVariantMap, wrapped in a QVariant.
QVariant staticValue() const
Returns the current static value for the property.
Type propertyType() const
Returns the property type.
bool operator!=(const QgsProperty &other) const
void setExpressionString(const QString &expression)
Sets the expression to use for the property value.
QgsProperty & operator=(const QgsProperty &other)
static QgsProperty fromField(const QString &fieldName, bool isActive=true)
Returns a new FieldBasedProperty created from the specified field name.
QgsProperty()
Constructor for a QgsProperty.
static QgsProperty fromValue(const QVariant &value, bool isActive=true)
Returns a new StaticProperty created from the specified value.
void setActive(bool active)
Sets whether the property is currently active.
static QColor decodeColor(const QString &str)