QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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"
21 #include "qgsexpressioncontext.h"
22 #include "qgscolorramp.h"
23 
24 #include <QVariant>
25 #include <QHash>
26 #include <QString>
27 #include <QStringList>
28 #include <QDomElement>
29 #include <QDomDocument>
30 #include <QColor>
31 #include <QDateTime>
32 
34 class QgsPropertyPrivate;
35 
47 class CORE_EXPORT QgsPropertyDefinition
48 {
49  public:
50 
53  {
54  Boolean = 0,
70  Size,
81  Custom = 3000,
82  };
83 
85  enum DataType
86  {
87 
93  DataTypeString = 0,
94 
101 
108  };
109 
114 
123  QgsPropertyDefinition( const QString &name, const QString &description, StandardPropertyTemplate type, const QString &origin = QString(), const QString &comment = QString() );
124 
135  QgsPropertyDefinition( const QString &name, DataType dataType, const QString &description, const QString &helpText, const QString &origin = QString(), const QString &comment = QString() );
136 
140  QString name() const { return mName; }
141 
145  void setName( const QString &name ) { mName = name; }
146 
152  QString origin() const { return mOrigin; }
153 
159  void setOrigin( const QString &origin ) { mOrigin = origin; }
160 
164  QString description() const { return mDescription; }
165 
169  QString comment() const { return mComment; }
170 
174  void setComment( const QString &comment ) { mComment = comment; }
175 
179  QString helpText() const { return mHelpText; }
180 
184  void setDataType( DataType type ) { mTypes = type; }
185 
189  DataType dataType() const { return mTypes; }
190 
195  StandardPropertyTemplate standardTemplate() const { return mStandardType; }
196 
201  bool supportsAssistant() const;
202 
203  private:
204 
205  QString mName;
206  QString mDescription;
207  DataType mTypes = DataTypeString;
208  QString mHelpText;
209  StandardPropertyTemplate mStandardType = Custom;
210  QString mOrigin;
211  QString mComment;
212 
213  static QString trString();
214 };
215 
216 
231 class CORE_EXPORT QgsProperty
232 {
233  public:
234 
236  enum Type
237  {
242  };
243 
249  static QVariantMap propertyMapToVariantMap( const QMap<QString, QgsProperty> &propertyMap );
250 
257  static QMap<QString, QgsProperty> variantMapToPropertyMap( const QVariantMap &variantMap );
258 
262  QgsProperty();
263 
265 
269  static QgsProperty fromExpression( const QString &expression, bool isActive = true );
270 
274  static QgsProperty fromField( const QString &fieldName, bool isActive = true );
275 
279  static QgsProperty fromValue( const QVariant &value, bool isActive = true );
280 
282  QgsProperty( const QgsProperty &other );
283 
284  QgsProperty &operator=( const QgsProperty &other );
285 
289  operator bool() const;
290 
291  bool operator==( const QgsProperty &other ) const;
292  bool operator!=( const QgsProperty &other ) const;
293 
297  Type propertyType() const;
298 
303  bool isActive() const;
304 
309  void setActive( bool active );
310 
316  void setStaticValue( const QVariant &value );
317 
323  QVariant staticValue() const;
324 
330  void setField( const QString &field );
331 
337  QString field() const;
338 
344  void setExpressionString( const QString &expression );
345 
351  QString expressionString() const;
352 
357  QString asExpression() const;
358 
364  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const;
365 
373  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const;
374 
380  bool isProjectColor() const;
381 
396  QVariant value( const QgsExpressionContext &context, const QVariant &defaultValue = QVariant(), bool *ok SIP_OUT = nullptr ) const;
397 
412  QDateTime valueAsDateTime( const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok SIP_OUT = nullptr ) const;
413 
427  QString valueAsString( const QgsExpressionContext &context, const QString &defaultString = QString(), bool *ok SIP_OUT = nullptr ) const;
428 
442  QColor valueAsColor( const QgsExpressionContext &context, const QColor &defaultColor = QColor(), bool *ok SIP_OUT = nullptr ) const;
443 
457  double valueAsDouble( const QgsExpressionContext &context, double defaultValue = 0.0, bool *ok SIP_OUT = nullptr ) const;
458 
472  int valueAsInt( const QgsExpressionContext &context, int defaultValue = 0, bool *ok SIP_OUT = nullptr ) const;
473 
487  bool valueAsBool( const QgsExpressionContext &context, bool defaultValue = false, bool *ok SIP_OUT = nullptr ) const;
488 
495  QVariant toVariant() const;
496 
503  bool loadVariant( const QVariant &property );
504 
511  void setTransformer( QgsPropertyTransformer *transformer SIP_TRANSFER );
512 
517  const QgsPropertyTransformer *transformer() const;
518 
525  bool convertToTransformer();
526 
528  operator QVariant() const
529  {
530  return QVariant::fromValue( *this );
531  }
532 
533 
534 #ifdef SIP_RUN
535  SIP_PYOBJECT __repr__();
536  % MethodCode
537  QString typeString;
538  QString definitionString;
539  switch ( sipCpp->propertyType() )
540  {
542  typeString = QStringLiteral( "static" );
543  definitionString = sipCpp->staticValue().toString();
544  break;
545 
547  typeString = QStringLiteral( "field" );
548  definitionString = sipCpp->field();
549  break;
550 
552  typeString = QStringLiteral( "expression" );
553  definitionString = sipCpp->expressionString();
554  break;
555 
557  typeString = QStringLiteral( "invalid" );
558  break;
559  }
560 
561  QString str = QStringLiteral( "<QgsProperty: %1%2%3>" ).arg( !sipCpp->isActive() && sipCpp->propertyType() != QgsProperty::InvalidProperty ? QStringLiteral( "INACTIVE " ) : QString(),
562  typeString,
563  definitionString.isEmpty() ? QString() : QStringLiteral( " (%1)" ).arg( definitionString ) );
564  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
565  % End
566 #endif
567 
568  private:
569 
570  mutable QExplicitlySharedDataPointer<QgsPropertyPrivate> d;
571 
576  QVariant propertyValue( const QgsExpressionContext &context, const QVariant &defaultValue = QVariant(), bool *ok = nullptr ) const;
577 
578 };
579 
581 
582 #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:48
StandardPropertyTemplate standardTemplate() const
Returns the property's standard template, if applicable.
Definition: qgsproperty.h:195
QString helpText() const
Helper text for using the property, including a description of the valid values for the property.
Definition: qgsproperty.h:179
QString comment() const
Returns the comment of the property.
Definition: qgsproperty.h:169
DataType dataType() const
Returns the allowable field/value data type for the property.
Definition: qgsproperty.h:189
void setOrigin(const QString &origin)
Sets the origin of the property.
Definition: qgsproperty.h:159
QString description() const
Descriptive name of the property.
Definition: qgsproperty.h:164
StandardPropertyTemplate
Predefined standard property templates.
Definition: qgsproperty.h:53
@ HorizontalAnchor
Horizontal anchor point.
Definition: qgsproperty.h:76
@ Double
Double value (including negative values)
Definition: qgsproperty.h:58
@ VerticalAnchor
Vertical anchor point.
Definition: qgsproperty.h:77
@ Double0To1
Double value between 0-1 (inclusive)
Definition: qgsproperty.h:60
@ FillStyle
Fill style (eg solid, lines)
Definition: qgsproperty.h:74
@ StrokeWidth
Line stroke width.
Definition: qgsproperty.h:73
@ LineStyle
Line style (eg solid/dashed)
Definition: qgsproperty.h:72
@ Integer
Integer value (including negative values)
Definition: qgsproperty.h:55
@ String
Any string value.
Definition: qgsproperty.h:62
@ DateTime
DateTime value.
Definition: qgsproperty.h:80
@ BlendMode
Blend mode.
Definition: qgsproperty.h:68
@ RenderUnits
Render units (eg mm/pixels/map units)
Definition: qgsproperty.h:64
@ PenJoinStyle
Pen join style.
Definition: qgsproperty.h:67
@ SvgPath
Path to an SVG file.
Definition: qgsproperty.h:78
@ IntegerPositiveGreaterZero
Non-zero positive integer values.
Definition: qgsproperty.h:57
@ IntegerPositive
Positive integer values (including 0)
Definition: qgsproperty.h:56
@ Opacity
Opacity (0-100)
Definition: qgsproperty.h:63
@ CapStyle
Line cap style (eg round)
Definition: qgsproperty.h:75
@ ColorNoAlpha
Color with no alpha channel.
Definition: qgsproperty.h:66
@ Rotation
Rotation (value between 0-360 degrees)
Definition: qgsproperty.h:61
@ Size
1D size (eg marker radius, or square marker height/width)
Definition: qgsproperty.h:70
@ ColorWithAlpha
Color with alpha channel.
Definition: qgsproperty.h:65
@ DoublePositive
Positive double value (including 0)
Definition: qgsproperty.h:59
@ Size2D
2D size (width/height different)
Definition: qgsproperty.h:71
QString name() const
Returns the name of the property.
Definition: qgsproperty.h:140
QgsPropertyDefinition()=default
Constructs an empty property.
void setDataType(DataType type)
Sets the data type.
Definition: qgsproperty.h:184
void setName(const QString &name)
Sets the name of the property.
Definition: qgsproperty.h:145
QString origin() const
Returns the origin of the property.
Definition: qgsproperty.h:152
void setComment(const QString &comment)
Sets comment of the property.
Definition: qgsproperty.h:174
DataType
Valid data types required by property.
Definition: qgsproperty.h:86
@ DataTypeBoolean
Property requires a boolean value.
Definition: qgsproperty.h:107
@ DataTypeNumeric
Property requires a numeric value.
Definition: qgsproperty.h:100
Abstract base class for objects which transform the calculated value of a property.
A store for object properties.
Definition: qgsproperty.h:232
Type
Property types.
Definition: qgsproperty.h:237
@ ExpressionBasedProperty
Expression based property (QgsExpressionBasedProperty)
Definition: qgsproperty.h:241
@ StaticProperty
Static property (QgsStaticProperty)
Definition: qgsproperty.h:239
@ FieldBasedProperty
Field based property (QgsFieldBasedProperty)
Definition: qgsproperty.h:240
@ InvalidProperty
Invalid (not set) property.
Definition: qgsproperty.h:238
#define str(x)
Definition: qgis.cpp:37
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_OUT
Definition: qgis_sip.h:58
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
const QgsField & field
Definition: qgsfield.h:463
Q_DECLARE_METATYPE(QgsMeshTimeSettings)