QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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 <QVariant>
23#include <QHash>
24#include <QString>
25#include <QStringList>
26#include <QDomElement>
27#include <QDomDocument>
28#include <QColor>
29#include <QDateTime>
30
32class QgsPropertyPrivate;
33
45class CORE_EXPORT QgsPropertyDefinition
46{
47 public:
48
51 {
52 Boolean = 0,
79 Custom = 3000,
80 };
81
84 {
85
91 DataTypeString = 0,
92
99
106 };
107
112
121 QgsPropertyDefinition( const QString &name, const QString &description, StandardPropertyTemplate type, const QString &origin = QString(), const QString &comment = QString() );
122
133 QgsPropertyDefinition( const QString &name, DataType dataType, const QString &description, const QString &helpText, const QString &origin = QString(), const QString &comment = QString() );
134
138 QString name() const { return mName; }
139
143 void setName( const QString &name ) { mName = name; }
144
150 QString origin() const { return mOrigin; }
151
157 void setOrigin( const QString &origin ) { mOrigin = origin; }
158
162 QString description() const { return mDescription; }
163
167 QString comment() const { return mComment; }
168
172 void setComment( const QString &comment ) { mComment = comment; }
173
177 QString helpText() const { return mHelpText; }
178
182 void setDataType( DataType type ) { mTypes = type; }
183
187 DataType dataType() const { return mTypes; }
188
193 StandardPropertyTemplate standardTemplate() const { return mStandardType; }
194
199 bool supportsAssistant() const;
200
201 private:
202
203 QString mName;
204 QString mDescription;
205 DataType mTypes = DataTypeString;
206 QString mHelpText;
207 StandardPropertyTemplate mStandardType = Custom;
208 QString mOrigin;
209 QString mComment;
210
211 static QString trString();
212};
213
214
229class CORE_EXPORT QgsProperty
230{
231 public:
232
234 enum Type
235 {
240 };
241
247 static QVariantMap propertyMapToVariantMap( const QMap<QString, QgsProperty> &propertyMap );
248
255 static QMap<QString, QgsProperty> variantMapToPropertyMap( const QVariantMap &variantMap );
256
260 QgsProperty();
261
263
267 static QgsProperty fromExpression( const QString &expression, bool isActive = true );
268
272 static QgsProperty fromField( const QString &fieldName, bool isActive = true );
273
277 static QgsProperty fromValue( const QVariant &value, bool isActive = true );
278
280 QgsProperty( const QgsProperty &other );
281
282 QgsProperty &operator=( const QgsProperty &other );
283
287 operator bool() const;
288
289 bool operator==( const QgsProperty &other ) const;
290 bool operator!=( const QgsProperty &other ) const;
291
295 Type propertyType() const;
296
301 bool isActive() const;
302
317 bool isStaticValueInContext( const QgsExpressionContext &context, QVariant &staticValue SIP_OUT ) const;
318
323 void setActive( bool active );
324
330 void setStaticValue( const QVariant &value );
331
337 QVariant staticValue() const;
338
344 void setField( const QString &field );
345
351 QString field() const;
352
358 void setExpressionString( const QString &expression );
359
365 QString expressionString() const;
366
371 QString asExpression() const;
372
378 bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const;
379
387 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const;
388
394 bool isProjectColor() const;
395
410 QVariant value( const QgsExpressionContext &context, const QVariant &defaultValue = QVariant(), bool *ok SIP_OUT = nullptr ) const;
411
426 QDateTime valueAsDateTime( const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok SIP_OUT = nullptr ) const;
427
441 QString valueAsString( const QgsExpressionContext &context, const QString &defaultString = QString(), bool *ok SIP_OUT = nullptr ) const;
442
456 QColor valueAsColor( const QgsExpressionContext &context, const QColor &defaultColor = QColor(), bool *ok SIP_OUT = nullptr ) const;
457
471 double valueAsDouble( const QgsExpressionContext &context, double defaultValue = 0.0, bool *ok SIP_OUT = nullptr ) const;
472
486 int valueAsInt( const QgsExpressionContext &context, int defaultValue = 0, bool *ok SIP_OUT = nullptr ) const;
487
501 bool valueAsBool( const QgsExpressionContext &context, bool defaultValue = false, bool *ok SIP_OUT = nullptr ) const;
502
509 QVariant toVariant() const;
510
517 bool loadVariant( const QVariant &property );
518
525 void setTransformer( QgsPropertyTransformer *transformer SIP_TRANSFER );
526
531 const QgsPropertyTransformer *transformer() const;
532
539 bool convertToTransformer();
540
542 operator QVariant() const
543 {
544 return QVariant::fromValue( *this );
545 }
546
547
548#ifdef SIP_RUN
549 SIP_PYOBJECT __repr__();
550 % MethodCode
551 QString typeString;
552 QString definitionString;
553 switch ( sipCpp->propertyType() )
554 {
556 typeString = QStringLiteral( "static" );
557 definitionString = sipCpp->staticValue().toString();
558 break;
559
561 typeString = QStringLiteral( "field" );
562 definitionString = sipCpp->field();
563 break;
564
566 typeString = QStringLiteral( "expression" );
567 definitionString = sipCpp->expressionString();
568 break;
569
571 typeString = QStringLiteral( "invalid" );
572 break;
573 }
574
575 QString str = QStringLiteral( "<QgsProperty: %1%2%3>" ).arg( !sipCpp->isActive() && sipCpp->propertyType() != QgsProperty::InvalidProperty ? QStringLiteral( "INACTIVE " ) : QString(),
576 typeString,
577 definitionString.isEmpty() ? QString() : QStringLiteral( " (%1)" ).arg( definitionString ) );
578 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
579 % End
580#endif
581
582 private:
583
584 mutable QExplicitlySharedDataPointer<QgsPropertyPrivate> d;
585
590 QVariant propertyValue( const QgsExpressionContext &context, const QVariant &defaultValue = QVariant(), bool *ok = nullptr ) const;
591
592};
593
595
596#endif // QGSPROPERTY_H
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition for a property.
Definition: qgsproperty.h:46
StandardPropertyTemplate standardTemplate() const
Returns the property's standard template, if applicable.
Definition: qgsproperty.h:193
QString helpText() const
Helper text for using the property, including a description of the valid values for the property.
Definition: qgsproperty.h:177
QString comment() const
Returns the comment of the property.
Definition: qgsproperty.h:167
DataType dataType() const
Returns the allowable field/value data type for the property.
Definition: qgsproperty.h:187
void setOrigin(const QString &origin)
Sets the origin of the property.
Definition: qgsproperty.h:157
QString description() const
Descriptive name of the property.
Definition: qgsproperty.h:162
StandardPropertyTemplate
Predefined standard property templates.
Definition: qgsproperty.h:51
@ HorizontalAnchor
Horizontal anchor point.
Definition: qgsproperty.h:74
@ Double
Double value (including negative values)
Definition: qgsproperty.h:56
@ VerticalAnchor
Vertical anchor point.
Definition: qgsproperty.h:75
@ Double0To1
Double value between 0-1 (inclusive)
Definition: qgsproperty.h:58
@ FillStyle
Fill style (eg solid, lines)
Definition: qgsproperty.h:72
@ StrokeWidth
Line stroke width.
Definition: qgsproperty.h:71
@ LineStyle
Line style (eg solid/dashed)
Definition: qgsproperty.h:70
@ Integer
Integer value (including negative values)
Definition: qgsproperty.h:53
@ String
Any string value.
Definition: qgsproperty.h:60
@ DateTime
DateTime value.
Definition: qgsproperty.h:78
@ BlendMode
Blend mode.
Definition: qgsproperty.h:66
@ RenderUnits
Render units (eg mm/pixels/map units)
Definition: qgsproperty.h:62
@ PenJoinStyle
Pen join style.
Definition: qgsproperty.h:65
@ SvgPath
Path to an SVG file.
Definition: qgsproperty.h:76
@ IntegerPositiveGreaterZero
Non-zero positive integer values.
Definition: qgsproperty.h:55
@ IntegerPositive
Positive integer values (including 0)
Definition: qgsproperty.h:54
@ Opacity
Opacity (0-100)
Definition: qgsproperty.h:61
@ CapStyle
Line cap style (eg round)
Definition: qgsproperty.h:73
@ ColorNoAlpha
Color with no alpha channel.
Definition: qgsproperty.h:64
@ Rotation
Rotation (value between 0-360 degrees)
Definition: qgsproperty.h:59
@ Size
1D size (eg marker radius, or square marker height/width)
Definition: qgsproperty.h:68
@ ColorWithAlpha
Color with alpha channel.
Definition: qgsproperty.h:63
@ DoublePositive
Positive double value (including 0)
Definition: qgsproperty.h:57
@ Size2D
2D size (width/height different)
Definition: qgsproperty.h:69
QString name() const
Returns the name of the property.
Definition: qgsproperty.h:138
QgsPropertyDefinition()=default
Constructs an empty property.
void setDataType(DataType type)
Sets the data type.
Definition: qgsproperty.h:182
void setName(const QString &name)
Sets the name of the property.
Definition: qgsproperty.h:143
QString origin() const
Returns the origin of the property.
Definition: qgsproperty.h:150
void setComment(const QString &comment)
Sets comment of the property.
Definition: qgsproperty.h:172
DataType
Valid data types required by property.
Definition: qgsproperty.h:84
@ DataTypeBoolean
Property requires a boolean value.
Definition: qgsproperty.h:105
@ DataTypeNumeric
Property requires a numeric value.
Definition: qgsproperty.h:98
Abstract base class for objects which transform the calculated value of a property.
A store for object properties.
Definition: qgsproperty.h:230
Type
Property types.
Definition: qgsproperty.h:235
@ ExpressionBasedProperty
Expression based property (QgsExpressionBasedProperty)
Definition: qgsproperty.h:239
@ StaticProperty
Static property (QgsStaticProperty)
Definition: qgsproperty.h:237
@ FieldBasedProperty
Field based property (QgsFieldBasedProperty)
Definition: qgsproperty.h:238
@ InvalidProperty
Invalid (not set) property.
Definition: qgsproperty.h:236
#define str(x)
Definition: qgis.cpp:38
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_OUT
Definition: qgis_sip.h:58
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
const QgsField & field
Definition: qgsfield.h:554