QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
22void QgsExpressionFieldBuffer::addExpression( const QString &exp, const QgsField &fld )
23{
24 mExpressions << ExpressionField( exp, fld );
25}
26
28{
29 mExpressions.removeAt( index );
30}
31
32void QgsExpressionFieldBuffer::renameExpression( int index, const QString &name )
33{
34 mExpressions[index].field.setName( name );
35}
36
37void QgsExpressionFieldBuffer::updateExpression( int index, const QString &exp )
38{
39 mExpressions[index].cachedExpression = QgsExpression( exp );
40}
41
42void QgsExpressionFieldBuffer::writeXml( QDomNode &layerNode, QDomDocument &document ) const
43{
44 QDomElement expressionFieldsElem = document.createElement( QStringLiteral( "expressionfields" ) );
45 layerNode.appendChild( expressionFieldsElem );
46
47 const auto constMExpressions = mExpressions;
48 for ( const ExpressionField &fld : constMExpressions )
49 {
50 QDomElement fldElem = document.createElement( QStringLiteral( "field" ) );
51
52 fldElem.setAttribute( QStringLiteral( "expression" ), fld.cachedExpression.expression() );
53 fldElem.setAttribute( QStringLiteral( "name" ), fld.field.name() );
54 fldElem.setAttribute( QStringLiteral( "precision" ), fld.field.precision() );
55 fldElem.setAttribute( QStringLiteral( "comment" ), fld.field.comment() );
56 fldElem.setAttribute( QStringLiteral( "length" ), fld.field.length() );
57 fldElem.setAttribute( QStringLiteral( "type" ), fld.field.type() );
58 fldElem.setAttribute( QStringLiteral( "subType" ), fld.field.subType() );
59 fldElem.setAttribute( QStringLiteral( "typeName" ), fld.field.typeName() );
60
61 expressionFieldsElem.appendChild( fldElem );
62 }
63}
64
65void QgsExpressionFieldBuffer::readXml( const QDomNode &layerNode )
66{
67 mExpressions.clear();
68
69 const QDomElement expressionFieldsElem = layerNode.firstChildElement( QStringLiteral( "expressionfields" ) );
70
71 if ( !expressionFieldsElem.isNull() )
72 {
73 const QDomNodeList fields = expressionFieldsElem.elementsByTagName( QStringLiteral( "field" ) );
74
75 for ( int i = 0; i < fields.size(); ++i )
76 {
77 const QDomElement field = fields.at( i ).toElement();
78 const QString exp = field.attribute( QStringLiteral( "expression" ) );
79 const QString name = field.attribute( QStringLiteral( "name" ) );
80 const QString comment = field.attribute( QStringLiteral( "comment" ) );
81 const int precision = field.attribute( QStringLiteral( "precision" ) ).toInt();
82 const int length = field.attribute( QStringLiteral( "length" ) ).toInt();
83 const QMetaType::Type type = static_cast< QMetaType::Type >( field.attribute( QStringLiteral( "type" ) ).toInt() );
84 const QMetaType::Type subType = static_cast< QMetaType::Type >( field.attribute( QStringLiteral( "subType" ), QStringLiteral( "0" ) ).toInt() );
85 const QString typeName = field.attribute( QStringLiteral( "typeName" ) );
86
87 mExpressions.append( ExpressionField( exp, QgsField( name, type, typeName, length, precision, comment, subType ) ) );
88 }
89 }
90}
91
93{
94 int index = 0;
95 const auto constMExpressions = mExpressions;
96 for ( const ExpressionField &fld : constMExpressions )
97 {
98 flds.appendExpressionField( fld.field, index );
99 ++index;
100 }
101}
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.
Handles parsing and evaluation of expressions (formerly called "search strings").
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
Container of fields for a vector layer.
Definition qgsfields.h:46
bool appendExpressionField(const QgsField &field, int originIndex)
Appends an expression field. The field must have unique name, otherwise it is rejected (returns false...