QGIS API Documentation 3.99.0-Master (d270888f95f)
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
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
26void QgsExpressionFieldBuffer::addExpression( const QString &exp, const QgsField &fld )
27{
28 mExpressions << ExpressionField( exp, fld );
29}
30
32{
33 mExpressions.removeAt( index );
34}
35
36void QgsExpressionFieldBuffer::renameExpression( int index, const QString &name )
37{
38 mExpressions[index].field.setName( name );
39}
40
41void QgsExpressionFieldBuffer::updateExpression( int index, const QString &exp )
42{
43 mExpressions[index].cachedExpression = QgsExpression( exp );
44}
45
46void QgsExpressionFieldBuffer::writeXml( QDomNode &layerNode, QDomDocument &document ) const
47{
48 QDomElement expressionFieldsElem = document.createElement( u"expressionfields"_s );
49 layerNode.appendChild( expressionFieldsElem );
50
51 const auto constMExpressions = mExpressions;
52 for ( const ExpressionField &fld : constMExpressions )
53 {
54 QDomElement fldElem = document.createElement( u"field"_s );
55
56 fldElem.setAttribute( u"expression"_s, fld.cachedExpression.expression() );
57 fldElem.setAttribute( u"name"_s, fld.field.name() );
58 fldElem.setAttribute( u"precision"_s, fld.field.precision() );
59 fldElem.setAttribute( u"comment"_s, fld.field.comment() );
60 fldElem.setAttribute( u"length"_s, fld.field.length() );
61 fldElem.setAttribute( u"type"_s, fld.field.type() );
62 fldElem.setAttribute( u"subType"_s, fld.field.subType() );
63 fldElem.setAttribute( u"typeName"_s, fld.field.typeName() );
64
65 expressionFieldsElem.appendChild( fldElem );
66 }
67}
68
69void QgsExpressionFieldBuffer::readXml( const QDomNode &layerNode )
70{
71 mExpressions.clear();
72
73 const QDomElement expressionFieldsElem = layerNode.firstChildElement( u"expressionfields"_s );
74
75 if ( !expressionFieldsElem.isNull() )
76 {
77 const QDomNodeList fields = expressionFieldsElem.elementsByTagName( u"field"_s );
78
79 for ( int i = 0; i < fields.size(); ++i )
80 {
81 const QDomElement field = fields.at( i ).toElement();
82 const QString exp = field.attribute( u"expression"_s );
83 const QString name = field.attribute( u"name"_s );
84 const QString comment = field.attribute( u"comment"_s );
85 const int precision = field.attribute( u"precision"_s ).toInt();
86 const int length = field.attribute( u"length"_s ).toInt();
87 const QMetaType::Type type = static_cast< QMetaType::Type >( field.attribute( u"type"_s ).toInt() );
88 const QMetaType::Type subType = static_cast< QMetaType::Type >( field.attribute( u"subType"_s, u"0"_s ).toInt() );
89 const QString typeName = field.attribute( u"typeName"_s );
90
91 mExpressions.append( ExpressionField( exp, QgsField( name, type, typeName, length, precision, comment, subType ) ) );
92 }
93 }
94}
95
97{
98 int index = 0;
99 const auto constMExpressions = mExpressions;
100 for ( const ExpressionField &fld : constMExpressions )
101 {
102 flds.appendExpressionField( fld.field, index );
103 ++index;
104 }
105}
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:56
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...