QGIS API Documentation 3.99.0-Master (d270888f95f)
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
31using namespace Qt::StringLiterals;
32
34class QgsPropertyPrivate;
35
46class CORE_EXPORT QgsPropertyDefinition
47{
48 public:
49
82
108
113
122 QgsPropertyDefinition( const QString &name, const QString &description, StandardPropertyTemplate type, const QString &origin = QString(), const QString &comment = QString() );
123
134 QgsPropertyDefinition( const QString &name, DataType dataType, const QString &description, const QString &helpText, const QString &origin = QString(), const QString &comment = QString() );
135
139 QString name() const { return mName; }
140
144 void setName( const QString &name ) { mName = name; }
145
151 QString origin() const { return mOrigin; }
152
158 void setOrigin( const QString &origin ) { mOrigin = origin; }
159
163 QString description() const { return mDescription; }
164
168 QString comment() const { return mComment; }
169
173 void setComment( const QString &comment ) { mComment = comment; }
174
178 QString helpText() const { return mHelpText; }
179
183 void setDataType( DataType type ) { mTypes = type; }
184
188 DataType dataType() const { return mTypes; }
189
194 StandardPropertyTemplate standardTemplate() const { return mStandardType; }
195
200 bool supportsAssistant() const;
201
202 private:
203
204 QString mName;
205 QString mDescription;
206 DataType mTypes = DataTypeString;
207 QString mHelpText;
208 StandardPropertyTemplate mStandardType = Custom;
209 QString mOrigin;
210 QString mComment;
211
212 static QString trString();
213};
214
215
228
229class CORE_EXPORT QgsProperty
230{
231 public:
232
238 static QVariantMap propertyMapToVariantMap( const QMap<QString, QgsProperty> &propertyMap );
239
246 static QMap<QString, QgsProperty> variantMapToPropertyMap( const QVariantMap &variantMap );
247
251 QgsProperty();
252
254
258 static QgsProperty fromExpression( const QString &expression, bool isActive = true );
259
263 static QgsProperty fromField( const QString &fieldName, bool isActive = true );
264
268 static QgsProperty fromValue( const QVariant &value, bool isActive = true );
269
270 QgsProperty( const QgsProperty &other );
271 QgsProperty &operator=( const QgsProperty &other );
272
276 explicit operator bool() const SIP_SKIP;
277
278 bool operator==( const QgsProperty &other ) const;
279 bool operator!=( const QgsProperty &other ) const;
280
284 Qgis::PropertyType propertyType() const;
285
290 bool isActive() const;
291
306 bool isStaticValueInContext( const QgsExpressionContext &context, QVariant &staticValue SIP_OUT ) const;
307
312 void setActive( bool active );
313
319 void setStaticValue( const QVariant &value );
320
326 QVariant staticValue() const;
327
333 void setField( const QString &field );
334
340 QString field() const;
341
347 void setExpressionString( const QString &expression );
348
354 QString expressionString() const;
355
360 QString asExpression() const;
361
367 bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const;
368
376 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const;
377
383 bool isProjectColor() const;
384
399 QVariant value( const QgsExpressionContext &context, const QVariant &defaultValue = QVariant(), bool *ok SIP_OUT = nullptr ) const;
400
415 QDateTime valueAsDateTime( const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok SIP_OUT = nullptr ) const;
416
430 QString valueAsString( const QgsExpressionContext &context, const QString &defaultString = QString(), bool *ok SIP_OUT = nullptr ) const;
431
445 QColor valueAsColor( const QgsExpressionContext &context, const QColor &defaultColor = QColor(), bool *ok SIP_OUT = nullptr ) const;
446
460 double valueAsDouble( const QgsExpressionContext &context, double defaultValue = 0.0, bool *ok SIP_OUT = nullptr ) const;
461
475 int valueAsInt( const QgsExpressionContext &context, int defaultValue = 0, bool *ok SIP_OUT = nullptr ) const;
476
490 bool valueAsBool( const QgsExpressionContext &context, bool defaultValue = false, bool *ok SIP_OUT = nullptr ) const;
491
498 QVariant toVariant() const;
499
506 bool loadVariant( const QVariant &property );
507
515
520 const QgsPropertyTransformer *transformer() const;
521
529
531 operator QVariant() const
532 {
533 return QVariant::fromValue( *this );
534 }
535
536
537#ifdef SIP_RUN
538 SIP_PYOBJECT __repr__();
539 % MethodCode
540 QString typeString;
541 QString definitionString;
542 switch ( sipCpp->propertyType() )
543 {
545 typeString = u"static"_s;
546 definitionString = sipCpp->staticValue().toString();
547 break;
548
550 typeString = u"field"_s;
551 definitionString = sipCpp->field();
552 break;
553
555 typeString = u"expression"_s;
556 definitionString = sipCpp->expressionString();
557 break;
558
560 typeString = u"invalid"_s;
561 break;
562 }
563
564 QString str = u"<QgsProperty: %1%2%3>"_s.arg( !sipCpp->isActive() && sipCpp->propertyType() != Qgis::PropertyType::Invalid ? u"INACTIVE "_s : QString(),
565 typeString,
566 definitionString.isEmpty() ? QString() : u" (%1)"_s.arg( definitionString ) );
567 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
568 % End
569#endif
570
571 private:
572
573 mutable QExplicitlySharedDataPointer<QgsPropertyPrivate> d;
574
579 QVariant propertyValue( const QgsExpressionContext &context, const QVariant &defaultValue = QVariant(), bool *ok = nullptr ) const;
580
581};
582
584
585#endif // QGSPROPERTY_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:59
@ Invalid
Invalid (not set) property.
Definition qgis.h:702
@ Field
Field based property.
Definition qgis.h:704
@ Static
Static property.
Definition qgis.h:703
@ Expression
Expression based property.
Definition qgis.h:705
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:52
@ HorizontalAnchor
Horizontal anchor point.
Definition qgsproperty.h:75
@ Double
Double value (including negative values).
Definition qgsproperty.h:57
@ VerticalAnchor
Vertical anchor point.
Definition qgsproperty.h:76
@ Double0To1
Double value between 0-1 (inclusive).
Definition qgsproperty.h:59
@ FillStyle
Fill style (eg solid, lines).
Definition qgsproperty.h:73
@ StrokeWidth
Line stroke width.
Definition qgsproperty.h:72
@ LineStyle
Line style (eg solid/dashed).
Definition qgsproperty.h:71
@ Integer
Integer value (including negative values).
Definition qgsproperty.h:54
@ String
Any string value.
Definition qgsproperty.h:61
@ DateTime
DateTime value.
Definition qgsproperty.h:79
@ BlendMode
Blend mode.
Definition qgsproperty.h:67
@ Boolean
Boolean value.
Definition qgsproperty.h:53
@ RenderUnits
Render units (eg mm/pixels/map units).
Definition qgsproperty.h:63
@ PenJoinStyle
Pen join style.
Definition qgsproperty.h:66
@ SvgPath
Path to an SVG file.
Definition qgsproperty.h:77
@ IntegerPositiveGreaterZero
Non-zero positive integer values.
Definition qgsproperty.h:56
@ IntegerPositive
Positive integer values (including 0).
Definition qgsproperty.h:55
@ Opacity
Opacity (0-100).
Definition qgsproperty.h:62
@ CapStyle
Line cap style (eg round).
Definition qgsproperty.h:74
@ ColorNoAlpha
Color with no alpha channel.
Definition qgsproperty.h:65
@ Rotation
Rotation (value between 0-360 degrees).
Definition qgsproperty.h:60
@ Custom
Custom property types.
Definition qgsproperty.h:80
@ Size
1D size (eg marker radius, or square marker height/width)
Definition qgsproperty.h:69
@ ColorWithAlpha
Color with alpha channel.
Definition qgsproperty.h:64
@ DoublePositive
Positive double value (including 0).
Definition qgsproperty.h:58
@ Size2D
2D size (width/height different)
Definition qgsproperty.h:70
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:85
@ DataTypeString
Property requires a string value.
Definition qgsproperty.h:92
@ DataTypeBoolean
Property requires a boolean value.
@ DataTypeNumeric
Property requires a numeric value.
Definition qgsproperty.h:99
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)