QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
247  QgsProperty();
248 
250 
254  static QgsProperty fromExpression( const QString &expression, bool isActive = true );
255 
259  static QgsProperty fromField( const QString &fieldName, bool isActive = true );
260 
264  static QgsProperty fromValue( const QVariant &value, bool isActive = true );
265 
267  QgsProperty( const QgsProperty &other );
268 
269  QgsProperty &operator=( const QgsProperty &other );
270 
274  operator bool() const;
275 
276  bool operator==( const QgsProperty &other ) const;
277  bool operator!=( const QgsProperty &other ) const;
278 
282  Type propertyType() const;
283 
288  bool isActive() const;
289 
294  void setActive( bool active );
295 
301  void setStaticValue( const QVariant &value );
302 
308  QVariant staticValue() const;
309 
315  void setField( const QString &field );
316 
322  QString field() const;
323 
329  void setExpressionString( const QString &expression );
330 
336  QString expressionString() const;
337 
342  QString asExpression() const;
343 
349  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const;
350 
358  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const;
359 
365  bool isProjectColor() const;
366 
381  QVariant value( const QgsExpressionContext &context, const QVariant &defaultValue = QVariant(), bool *ok SIP_OUT = nullptr ) const;
382 
397  QDateTime valueAsDateTime( const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok SIP_OUT = nullptr ) const;
398 
412  QString valueAsString( const QgsExpressionContext &context, const QString &defaultString = QString(), bool *ok SIP_OUT = nullptr ) const;
413 
427  QColor valueAsColor( const QgsExpressionContext &context, const QColor &defaultColor = QColor(), bool *ok SIP_OUT = nullptr ) const;
428 
442  double valueAsDouble( const QgsExpressionContext &context, double defaultValue = 0.0, bool *ok SIP_OUT = nullptr ) const;
443 
457  int valueAsInt( const QgsExpressionContext &context, int defaultValue = 0, bool *ok SIP_OUT = nullptr ) const;
458 
472  bool valueAsBool( const QgsExpressionContext &context, bool defaultValue = false, bool *ok SIP_OUT = nullptr ) const;
473 
480  QVariant toVariant() const;
481 
488  bool loadVariant( const QVariant &property );
489 
496  void setTransformer( QgsPropertyTransformer *transformer SIP_TRANSFER );
497 
502  const QgsPropertyTransformer *transformer() const;
503 
510  bool convertToTransformer();
511 
513  operator QVariant() const
514  {
515  return QVariant::fromValue( *this );
516  }
517 
518 
519 #ifdef SIP_RUN
520  SIP_PYOBJECT __repr__();
521  % MethodCode
522  QString typeString;
523  QString definitionString;
524  switch ( sipCpp->propertyType() )
525  {
527  typeString = QStringLiteral( "static" );
528  definitionString = sipCpp->staticValue().toString();
529  break;
530 
532  typeString = QStringLiteral( "field" );
533  definitionString = sipCpp->field();
534  break;
535 
537  typeString = QStringLiteral( "expression" );
538  definitionString = sipCpp->expressionString();
539  break;
540 
542  typeString = QStringLiteral( "invalid" );
543  break;
544  }
545 
546  QString str = QStringLiteral( "<QgsProperty: %1%2%3>" ).arg( !sipCpp->isActive() && sipCpp->propertyType() != QgsProperty::InvalidProperty ? QStringLiteral( "INACTIVE " ) : QString(),
547  typeString,
548  definitionString.isEmpty() ? QString() : QStringLiteral( " (%1)" ).arg( definitionString ) );
549  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
550  % End
551 #endif
552 
553  private:
554 
555  mutable QExplicitlySharedDataPointer<QgsPropertyPrivate> d;
556 
561  QVariant propertyValue( const QgsExpressionContext &context, const QVariant &defaultValue = QVariant(), bool *ok = nullptr ) const;
562 
563 };
564 
566 
567 #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 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:472
Q_DECLARE_METATYPE(QgsMeshTimeSettings)