QGIS API Documentation 3.27.0-Master (95e00c50d2)
qgsexpressionfieldbuffer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsexpressionfieldbuffer.cpp
3 ---------------------------
4 begin : May 27, 2014
5 copyright : (C) 2014 by Matthias Kuhn
6 email : matthias at opengis dot ch
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsvectorlayer.h"
21
22
23void QgsExpressionFieldBuffer::addExpression( const QString &exp, const QgsField &fld )
24{
25 mExpressions << ExpressionField( exp, fld );
26}
27
29{
30 mExpressions.removeAt( index );
31}
32
33void QgsExpressionFieldBuffer::renameExpression( int index, const QString &name )
34{
35 mExpressions[index].field.setName( name );
36}
37
38void QgsExpressionFieldBuffer::updateExpression( int index, const QString &exp )
39{
40 mExpressions[index].cachedExpression = QgsExpression( exp );
41}
42
43void QgsExpressionFieldBuffer::writeXml( QDomNode &layerNode, QDomDocument &document ) const
44{
45 QDomElement expressionFieldsElem = document.createElement( QStringLiteral( "expressionfields" ) );
46 layerNode.appendChild( expressionFieldsElem );
47
48 const auto constMExpressions = mExpressions;
49 for ( const ExpressionField &fld : constMExpressions )
50 {
51 QDomElement fldElem = document.createElement( QStringLiteral( "field" ) );
52
53 fldElem.setAttribute( QStringLiteral( "expression" ), fld.cachedExpression.expression() );
54 fldElem.setAttribute( QStringLiteral( "name" ), fld.field.name() );
55 fldElem.setAttribute( QStringLiteral( "precision" ), fld.field.precision() );
56 fldElem.setAttribute( QStringLiteral( "comment" ), fld.field.comment() );
57 fldElem.setAttribute( QStringLiteral( "length" ), fld.field.length() );
58 fldElem.setAttribute( QStringLiteral( "type" ), fld.field.type() );
59 fldElem.setAttribute( QStringLiteral( "subType" ), fld.field.subType() );
60 fldElem.setAttribute( QStringLiteral( "typeName" ), fld.field.typeName() );
61
62 expressionFieldsElem.appendChild( fldElem );
63 }
64}
65
66void QgsExpressionFieldBuffer::readXml( const QDomNode &layerNode )
67{
68 mExpressions.clear();
69
70 const QDomElement expressionFieldsElem = layerNode.firstChildElement( QStringLiteral( "expressionfields" ) );
71
72 if ( !expressionFieldsElem.isNull() )
73 {
74 const QDomNodeList fields = expressionFieldsElem.elementsByTagName( QStringLiteral( "field" ) );
75
76 for ( int i = 0; i < fields.size(); ++i )
77 {
78 const QDomElement field = fields.at( i ).toElement();
79 const QString exp = field.attribute( QStringLiteral( "expression" ) );
80 const QString name = field.attribute( QStringLiteral( "name" ) );
81 const QString comment = field.attribute( QStringLiteral( "comment" ) );
82 const int precision = field.attribute( QStringLiteral( "precision" ) ).toInt();
83 const int length = field.attribute( QStringLiteral( "length" ) ).toInt();
84 const QVariant::Type type = static_cast< QVariant::Type >( field.attribute( QStringLiteral( "type" ) ).toInt() );
85 const QVariant::Type subType = static_cast< QVariant::Type >( field.attribute( QStringLiteral( "subType" ), QStringLiteral( "0" ) ).toInt() );
86 const QString typeName = field.attribute( QStringLiteral( "typeName" ) );
87
88 mExpressions.append( ExpressionField( exp, QgsField( name, type, typeName, length, precision, comment, subType ) ) );
89 }
90 }
91}
92
94{
95 int index = 0;
96 const auto constMExpressions = mExpressions;
97 for ( const ExpressionField &fld : constMExpressions )
98 {
99 flds.appendExpressionField( fld.field, index );
100 ++index;
101 }
102}
void removeExpression(int index)
Remove an expression from the buffer.
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves expressions to xml under the layer node.
void readXml(const QDomNode &layer_node)
Reads expressions from project file.
void updateFields(QgsFields &flds) const
Adds fields with the expressions buffered in this object to a QgsFields object.
void addExpression(const QString &exp, const QgsField &fld)
Add an expression to the buffer.
void updateExpression(int index, const QString &exp)
Changes the expression at a given index.
void renameExpression(int index, const QString &name)
Renames an expression field at a given index.
Class for parsing and evaluation of expressions (formerly called "search strings").
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
Container of fields for a vector layer.
Definition: qgsfields.h:45
bool appendExpressionField(const QgsField &field, int originIndex)
Appends an expression field. The field must have unique name, otherwise it is rejected (returns false...
Definition: qgsfields.cpp:90
const QgsField & field
Definition: qgsfield.h:463
const QString & typeName
int precision