QGIS API Documentation 3.27.0-Master (f261cc1f8b)
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"
20#include "qgsexpression.h"
22
23#include <QVariant>
24#include <QHash>
25#include <QString>
26#include <QStringList>
27#include <QDomElement>
28#include <QDomDocument>
29#include <QColor>
30#include <QDateTime>
31
33class QgsPropertyPrivate;
34
46class CORE_EXPORT QgsPropertyDefinition
47{
48 public:
49
52 {
53 Boolean = 0,
80 Custom = 3000,
81 };
82
85 {
86
92 DataTypeString = 0,
93
100
107 };
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
230class CORE_EXPORT QgsProperty
231{
232 public:
233
235 enum Type
236 {
241 };
242
248 static QVariantMap propertyMapToVariantMap( const QMap<QString, QgsProperty> &propertyMap );
249
256 static QMap<QString, QgsProperty> variantMapToPropertyMap( const QVariantMap &variantMap );
257
261 QgsProperty();
262
264
268 static QgsProperty fromExpression( const QString &expression, bool isActive = true );
269
273 static QgsProperty fromField( const QString &fieldName, bool isActive = true );
274
278 static QgsProperty fromValue( const QVariant &value, bool isActive = true );
279
281 QgsProperty( const QgsProperty &other );
282
283 QgsProperty &operator=( const QgsProperty &other );
284
288 operator bool() const;
289
290 bool operator==( const QgsProperty &other ) const;
291 bool operator!=( const QgsProperty &other ) const;
292
296 Type propertyType() const;
297
302 bool isActive() const;
303
318 bool isStaticValueInContext( const QgsExpressionContext &context, QVariant &staticValue SIP_OUT ) const;
319
324 void setActive( bool active );
325
331 void setStaticValue( const QVariant &value );
332
338 QVariant staticValue() const;
339
345 void setField( const QString &field );
346
352 QString field() const;
353
359 void setExpressionString( const QString &expression );
360
366 QString expressionString() const;
367
372 QString asExpression() const;
373
379 bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const;
380
388 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const;
389
395 bool isProjectColor() const;
396
411 QVariant value( const QgsExpressionContext &context, const QVariant &defaultValue = QVariant(), bool *ok SIP_OUT = nullptr ) const;
412
427 QDateTime valueAsDateTime( const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok SIP_OUT = nullptr ) const;
428
442 QString valueAsString( const QgsExpressionContext &context, const QString &defaultString = QString(), bool *ok SIP_OUT = nullptr ) const;
443
457 QColor valueAsColor( const QgsExpressionContext &context, const QColor &defaultColor = QColor(), bool *ok SIP_OUT = nullptr ) const;
458
472 double valueAsDouble( const QgsExpressionContext &context, double defaultValue = 0.0, bool *ok SIP_OUT = nullptr ) const;
473
487 int valueAsInt( const QgsExpressionContext &context, int defaultValue = 0, bool *ok SIP_OUT = nullptr ) const;
488
502 bool valueAsBool( const QgsExpressionContext &context, bool defaultValue = false, bool *ok SIP_OUT = nullptr ) const;
503
510 QVariant toVariant() const;
511
518 bool loadVariant( const QVariant &property );
519
526 void setTransformer( QgsPropertyTransformer *transformer SIP_TRANSFER );
527
532 const QgsPropertyTransformer *transformer() const;
533
540 bool convertToTransformer();
541
543 operator QVariant() const
544 {
545 return QVariant::fromValue( *this );
546 }
547
548
549#ifdef SIP_RUN
550 SIP_PYOBJECT __repr__();
551 % MethodCode
552 QString typeString;
553 QString definitionString;
554 switch ( sipCpp->propertyType() )
555 {
557 typeString = QStringLiteral( "static" );
558 definitionString = sipCpp->staticValue().toString();
559 break;
560
562 typeString = QStringLiteral( "field" );
563 definitionString = sipCpp->field();
564 break;
565
567 typeString = QStringLiteral( "expression" );
568 definitionString = sipCpp->expressionString();
569 break;
570
572 typeString = QStringLiteral( "invalid" );
573 break;
574 }
575
576 QString str = QStringLiteral( "<QgsProperty: %1%2%3>" ).arg( !sipCpp->isActive() && sipCpp->propertyType() != QgsProperty::InvalidProperty ? QStringLiteral( "INACTIVE " ) : QString(),
577 typeString,
578 definitionString.isEmpty() ? QString() : QStringLiteral( " (%1)" ).arg( definitionString ) );
579 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
580 % End
581#endif
582
583 private:
584
585 mutable QExplicitlySharedDataPointer<QgsPropertyPrivate> d;
586
591 QVariant propertyValue( const QgsExpressionContext &context, const QVariant &defaultValue = QVariant(), bool *ok = nullptr ) const;
592
593};
594
596
597#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:47
StandardPropertyTemplate standardTemplate() const
Returns the property's standard template, if applicable.
Definition: qgsproperty.h:194
QString helpText() const
Helper text for using the property, including a description of the valid values for the property.
Definition: qgsproperty.h:178
QString comment() const
Returns the comment of the property.
Definition: qgsproperty.h:168
DataType dataType() const
Returns the allowable field/value data type for the property.
Definition: qgsproperty.h:188
void setOrigin(const QString &origin)
Sets the origin of the property.
Definition: qgsproperty.h:158
QString description() const
Descriptive name of the property.
Definition: qgsproperty.h:163
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
@ 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
@ 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.
Definition: qgsproperty.h:139
QgsPropertyDefinition()=default
Constructs an empty property.
void setDataType(DataType type)
Sets the data type.
Definition: qgsproperty.h:183
void setName(const QString &name)
Sets the name of the property.
Definition: qgsproperty.h:144
QString origin() const
Returns the origin of the property.
Definition: qgsproperty.h:151
void setComment(const QString &comment)
Sets comment of the property.
Definition: qgsproperty.h:173
DataType
Valid data types required by property.
Definition: qgsproperty.h:85
@ DataTypeBoolean
Property requires a boolean value.
Definition: qgsproperty.h:106
@ 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.
Definition: qgsproperty.h:231
Type
Property types.
Definition: qgsproperty.h:236
@ ExpressionBasedProperty
Expression based property (QgsExpressionBasedProperty)
Definition: qgsproperty.h:240
@ StaticProperty
Static property (QgsStaticProperty)
Definition: qgsproperty.h:238
@ FieldBasedProperty
Field based property (QgsFieldBasedProperty)
Definition: qgsproperty.h:239
@ InvalidProperty
Invalid (not set) property.
Definition: qgsproperty.h:237
#define str(x)
Definition: qgis.cpp:37
#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:463