QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
44class CORE_EXPORT QgsPropertyDefinition
45{
46 public:
47
50 {
51 Boolean = 0,
78 Custom = 3000,
79 };
80
83 {
84
90 DataTypeString = 0,
91
98
105 };
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
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
269 QgsProperty( const QgsProperty &other );
270
271 QgsProperty &operator=( const QgsProperty &other );
272
276 operator bool() const;
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
514 void setTransformer( QgsPropertyTransformer *transformer SIP_TRANSFER );
515
520 const QgsPropertyTransformer *transformer() const;
521
528 bool convertToTransformer();
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 = QStringLiteral( "static" );
546 definitionString = sipCpp->staticValue().toString();
547 break;
548
550 typeString = QStringLiteral( "field" );
551 definitionString = sipCpp->field();
552 break;
553
555 typeString = QStringLiteral( "expression" );
556 definitionString = sipCpp->expressionString();
557 break;
558
560 typeString = QStringLiteral( "invalid" );
561 break;
562 }
563
564 QString str = QStringLiteral( "<QgsProperty: %1%2%3>" ).arg( !sipCpp->isActive() && sipCpp->propertyType() != Qgis::PropertyType::Invalid ? QStringLiteral( "INACTIVE " ) : QString(),
565 typeString,
566 definitionString.isEmpty() ? QString() : QStringLiteral( " (%1)" ).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
PropertyType
Property types.
Definition: qgis.h:473
@ Invalid
Invalid (not set) property.
@ Field
Field based property.
@ Static
Static property.
@ Expression
Expression based property.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition for a property.
Definition: qgsproperty.h:45
StandardPropertyTemplate standardTemplate() const
Returns the property's standard template, if applicable.
Definition: qgsproperty.h:192
QString helpText() const
Helper text for using the property, including a description of the valid values for the property.
Definition: qgsproperty.h:176
QString comment() const
Returns the comment of the property.
Definition: qgsproperty.h:166
DataType dataType() const
Returns the allowable field/value data type for the property.
Definition: qgsproperty.h:186
void setOrigin(const QString &origin)
Sets the origin of the property.
Definition: qgsproperty.h:156
QString description() const
Descriptive name of the property.
Definition: qgsproperty.h:161
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
@ 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
@ 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.
Definition: qgsproperty.h:137
QgsPropertyDefinition()=default
Constructs an empty property.
void setDataType(DataType type)
Sets the data type.
Definition: qgsproperty.h:181
void setName(const QString &name)
Sets the name of the property.
Definition: qgsproperty.h:142
QString origin() const
Returns the origin of the property.
Definition: qgsproperty.h:149
void setComment(const QString &comment)
Sets comment of the property.
Definition: qgsproperty.h:171
DataType
Valid data types required by property.
Definition: qgsproperty.h:83
@ DataTypeBoolean
Property requires a boolean value.
Definition: qgsproperty.h:104
@ 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.
Definition: qgsproperty.h:228
#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)