QGIS API Documentation 3.39.0-Master (7b5d8bea57d)
Loading...
Searching...
No Matches
qgsexpressionbasednumericformat.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsexpressionbasednumericformat.cpp
3 --------------------------
4 begin : August 2024
5 copyright : (C) 2024 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
17#include "qgis.h"
18
23
25{
26 return QStringLiteral( "expression" );
27}
28
30{
31 return QObject::tr( "Custom Expression" );
32}
33
38
40{
41 QgsExpressionContext expContext = context.expressionContext();
42 expContext.setOriginalValueVariable( value );
43
44 return mExpression.evaluate( &expContext ).toString();
45}
46
51
52QgsNumericFormat *QgsExpressionBasedNumericFormat::create( const QVariantMap &configuration, const QgsReadWriteContext & ) const
53{
54 std::unique_ptr< QgsExpressionBasedNumericFormat > res = std::make_unique< QgsExpressionBasedNumericFormat >();
55 res->mExpression = QgsExpression( configuration.value( QStringLiteral( "expression" ), QStringLiteral( "@value" ) ).toString() );
56 return res.release();
57}
58
60{
61 QVariantMap res;
62 res.insert( QStringLiteral( "expression" ), mExpression.expression() );
63 return res;
64}
65
66void QgsExpressionBasedNumericFormat::setExpression( const QString &expression )
67{
68 mExpression = QgsExpression( expression );
69}
QString expression() const
Returns the expression used to calculate the text representation of a value.
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
QString visibleName() const override
Returns the translated, user-visible name for this format.
void setExpression(const QString &expression)
Sets the expression used to calculate the text representation of a value.
int sortKey() override
Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.
QString formatDouble(double value, const QgsNumericFormatContext &context) const override
Returns a formatted string representation of a numeric double value.
QString id() const override
Returns a unique id for this numeric format.
QgsNumericFormat * create(const QVariantMap &configuration, const QgsReadWriteContext &context) const override
Creates a new copy of the format, using the supplied configuration.
QgsNumericFormat * clone() const override
Clones the format, returning a new object.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
QString expression() const
Returns the original, unmodified expression string.
QVariant evaluate()
Evaluate the feature and return the result.
A context for numeric formats.
QgsExpressionContext expressionContext() const
Returns the expression context to use when evaluating QgsExpressions.
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
The class is used as a container of context for various read/write operations on other objects.