QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsproperty.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsproperty.h
3 -------------
4 Date : January 2017
5 Copyright : (C) 2017 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15#ifndef QGSPROPERTY_H
16#define QGSPROPERTY_H
17
18#include "qgis_core.h"
19#include "qgis_sip.h"
21
22#include <QColor>
23#include <QDateTime>
24#include <QDomDocument>
25#include <QDomElement>
26#include <QHash>
27#include <QString>
28#include <QStringList>
29#include <QVariant>
30
32class QgsPropertyPrivate;
33
44class CORE_EXPORT QgsPropertyDefinition
45{
46 public:
47
80
106
111
120 QgsPropertyDefinition( const QString &name, const QString &description, StandardPropertyTemplate type, const QString &origin = QString(), const QString &comment = QString() );
121
132 QgsPropertyDefinition( const QString &name, DataType dataType, const QString &description, const QString &helpText, const QString &origin = QString(), const QString &comment = QString() );
133
137 QString name() const { return mName; }
138
142 void setName( const QString &name ) { mName = name; }
143
149 QString origin() const { return mOrigin; }
150
156 void setOrigin( const QString &origin ) { mOrigin = origin; }
157
161 QString description() const { return mDescription; }
162
166 QString comment() const { return mComment; }
167
171 void setComment( const QString &comment ) { mComment = comment; }
172
176 QString helpText() const { return mHelpText; }
177
181 void setDataType( DataType type ) { mTypes = type; }
182
186 DataType dataType() const { return mTypes; }
187
192 StandardPropertyTemplate standardTemplate() const { return mStandardType; }
193
198 bool supportsAssistant() const;
199
200 private:
201
202 QString mName;
203 QString mDescription;
204 DataType mTypes = DataTypeString;
205 QString mHelpText;
206 StandardPropertyTemplate mStandardType = Custom;
207 QString mOrigin;
208 QString mComment;
209
210 static QString trString();
211};
212
213
226
227class CORE_EXPORT QgsProperty
228{
229 public:
230
236 static QVariantMap propertyMapToVariantMap( const QMap<QString, QgsProperty> &propertyMap );
237
244 static QMap<QString, QgsProperty> variantMapToPropertyMap( const QVariantMap &variantMap );
245
249 QgsProperty();
250
252
256 static QgsProperty fromExpression( const QString &expression, bool isActive = true );
257
261 static QgsProperty fromField( const QString &fieldName, bool isActive = true );
262
266 static QgsProperty fromValue( const QVariant &value, bool isActive = true );
267
268 QgsProperty( const QgsProperty &other );
269 QgsProperty &operator=( const QgsProperty &other );
270
274 explicit operator bool() const SIP_SKIP;
275
276 bool operator==( const QgsProperty &other ) const;
277 bool operator!=( const QgsProperty &other ) const;
278
282 Qgis::PropertyType propertyType() const;
283
288 bool isActive() const;
289
304 bool isStaticValueInContext( const QgsExpressionContext &context, QVariant &staticValue SIP_OUT ) const;
305
310 void setActive( bool active );
311
317 void setStaticValue( const QVariant &value );
318
324 QVariant staticValue() const;
325
331 void setField( const QString &field );
332
338 QString field() const;
339
345 void setExpressionString( const QString &expression );
346
352 QString expressionString() const;
353
358 QString asExpression() const;
359
365 bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const;
366
374 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const;
375
381 bool isProjectColor() const;
382
397 QVariant value( const QgsExpressionContext &context, const QVariant &defaultValue = QVariant(), bool *ok SIP_OUT = nullptr ) const;
398
413 QDateTime valueAsDateTime( const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok SIP_OUT = nullptr ) const;
414
428 QString valueAsString( const QgsExpressionContext &context, const QString &defaultString = QString(), bool *ok SIP_OUT = nullptr ) const;
429
443 QColor valueAsColor( const QgsExpressionContext &context, const QColor &defaultColor = QColor(), bool *ok SIP_OUT = nullptr ) const;
444
458 double valueAsDouble( const QgsExpressionContext &context, double defaultValue = 0.0, bool *ok SIP_OUT = nullptr ) const;
459
473 int valueAsInt( const QgsExpressionContext &context, int defaultValue = 0, bool *ok SIP_OUT = nullptr ) const;
474
488 bool valueAsBool( const QgsExpressionContext &context, bool defaultValue = false, bool *ok SIP_OUT = nullptr ) const;
489
496 QVariant toVariant() const;
497
504 bool loadVariant( const QVariant &property );
505
513
518 const QgsPropertyTransformer *transformer() const;
519
527
529 operator QVariant() const
530 {
531 return QVariant::fromValue( *this );
532 }
533
534
535#ifdef SIP_RUN
536 SIP_PYOBJECT __repr__();
537 % MethodCode
538 QString typeString;
539 QString definitionString;
540 switch ( sipCpp->propertyType() )
541 {
543 typeString = QStringLiteral( "static" );
544 definitionString = sipCpp->staticValue().toString();
545 break;
546
548 typeString = QStringLiteral( "field" );
549 definitionString = sipCpp->field();
550 break;
551
553 typeString = QStringLiteral( "expression" );
554 definitionString = sipCpp->expressionString();
555 break;
556
558 typeString = QStringLiteral( "invalid" );
559 break;
560 }
561
562 QString str = QStringLiteral( "<QgsProperty: %1%2%3>" ).arg( !sipCpp->isActive() && sipCpp->propertyType() != Qgis::PropertyType::Invalid ? QStringLiteral( "INACTIVE " ) : QString(),
563 typeString,
564 definitionString.isEmpty() ? QString() : QStringLiteral( " (%1)" ).arg( definitionString ) );
565 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
566 % End
567#endif
568
569 private:
570
571 mutable QExplicitlySharedDataPointer<QgsPropertyPrivate> d;
572
577 QVariant propertyValue( const QgsExpressionContext &context, const QVariant &defaultValue = QVariant(), bool *ok = nullptr ) const;
578
579};
580
582
583#endif // QGSPROPERTY_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
@ Invalid
Invalid (not set) property.
Definition qgis.h:683
@ Field
Field based property.
Definition qgis.h:685
@ Static
Static property.
Definition qgis.h:684
@ Expression
Expression based property.
Definition qgis.h:686
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
StandardPropertyTemplate standardTemplate() const
Returns the property's standard template, if applicable.
QString helpText() const
Helper text for using the property, including a description of the valid values for the property.
QString comment() const
Returns the comment of the property.
DataType dataType() const
Returns the allowable field/value data type for the property.
void setOrigin(const QString &origin)
Sets the origin of the property.
QString description() const
Descriptive name of the property.
StandardPropertyTemplate
Predefined standard property templates.
Definition qgsproperty.h:50
@ HorizontalAnchor
Horizontal anchor point.
Definition qgsproperty.h:73
@ Double
Double value (including negative values).
Definition qgsproperty.h:55
@ VerticalAnchor
Vertical anchor point.
Definition qgsproperty.h:74
@ Double0To1
Double value between 0-1 (inclusive).
Definition qgsproperty.h:57
@ FillStyle
Fill style (eg solid, lines).
Definition qgsproperty.h:71
@ StrokeWidth
Line stroke width.
Definition qgsproperty.h:70
@ LineStyle
Line style (eg solid/dashed).
Definition qgsproperty.h:69
@ Integer
Integer value (including negative values).
Definition qgsproperty.h:52
@ String
Any string value.
Definition qgsproperty.h:59
@ DateTime
DateTime value.
Definition qgsproperty.h:77
@ BlendMode
Blend mode.
Definition qgsproperty.h:65
@ Boolean
Boolean value.
Definition qgsproperty.h:51
@ RenderUnits
Render units (eg mm/pixels/map units).
Definition qgsproperty.h:61
@ PenJoinStyle
Pen join style.
Definition qgsproperty.h:64
@ SvgPath
Path to an SVG file.
Definition qgsproperty.h:75
@ IntegerPositiveGreaterZero
Non-zero positive integer values.
Definition qgsproperty.h:54
@ IntegerPositive
Positive integer values (including 0).
Definition qgsproperty.h:53
@ Opacity
Opacity (0-100).
Definition qgsproperty.h:60
@ CapStyle
Line cap style (eg round).
Definition qgsproperty.h:72
@ ColorNoAlpha
Color with no alpha channel.
Definition qgsproperty.h:63
@ Rotation
Rotation (value between 0-360 degrees).
Definition qgsproperty.h:58
@ Custom
Custom property types.
Definition qgsproperty.h:78
@ Size
1D size (eg marker radius, or square marker height/width)
Definition qgsproperty.h:67
@ ColorWithAlpha
Color with alpha channel.
Definition qgsproperty.h:62
@ DoublePositive
Positive double value (including 0).
Definition qgsproperty.h:56
@ Size2D
2D size (width/height different)
Definition qgsproperty.h:68
QString name() const
Returns the name of the property.
QgsPropertyDefinition()=default
Constructs an empty property.
void setDataType(DataType type)
Sets the data type.
void setName(const QString &name)
Sets the name of the property.
QString origin() const
Returns the origin of the property.
void setComment(const QString &comment)
Sets comment of the property.
DataType
Valid data types required by property.
Definition qgsproperty.h:83
@ DataTypeString
Property requires a string value.
Definition qgsproperty.h:90
@ DataTypeBoolean
Property requires a boolean value.
@ DataTypeNumeric
Property requires a numeric value.
Definition qgsproperty.h:97
Abstract base class for objects which transform the calculated value of a property.
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.
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...
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.
Qgis::PropertyType propertyType() const
Returns the property type.
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.
bool isStaticValueInContext(const QgsExpressionContext &context, QVariant &staticValue) const
Returns true if the property is effectively a static value in the specified context.
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.
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)