QGIS API Documentation 4.3.0-Master (e0370aa57ef)
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 if ( index < 0 || index >= mExpressions.size() )
34 return;
35
36 mExpressions.removeAt( index );
37}
38
39void QgsExpressionFieldBuffer::renameExpression( int index, const QString &name )
40{
41 if ( index < 0 || index >= mExpressions.size() )
42 return;
43
44 mExpressions[index].field.setName( name );
45}
46
47void QgsExpressionFieldBuffer::updateExpression( int index, const QString &exp )
48{
49 if ( index < 0 || index >= mExpressions.size() )
50 return;
51
52 mExpressions[index].cachedExpression = QgsExpression( exp );
53}
54
55void QgsExpressionFieldBuffer::writeXml( QDomNode &layerNode, QDomDocument &document ) const
56{
57 QDomElement expressionFieldsElem = document.createElement( u"expressionfields"_s );
58 layerNode.appendChild( expressionFieldsElem );
59
60 const auto constMExpressions = mExpressions;
61 for ( const ExpressionField &fld : constMExpressions )
62 {
63 QDomElement fldElem = document.createElement( u"field"_s );
64
65 fldElem.setAttribute( u"expression"_s, fld.cachedExpression.expression() );
66 fldElem.setAttribute( u"name"_s, fld.field.name() );
67 fldElem.setAttribute( u"precision"_s, fld.field.precision() );
68 fldElem.setAttribute( u"comment"_s, fld.field.comment() );
69 fldElem.setAttribute( u"length"_s, fld.field.length() );
70 fldElem.setAttribute( u"type"_s, fld.field.type() );
71 fldElem.setAttribute( u"subType"_s, fld.field.subType() );
72 fldElem.setAttribute( u"typeName"_s, fld.field.typeName() );
73
74 expressionFieldsElem.appendChild( fldElem );
75 }
76}
77
78void QgsExpressionFieldBuffer::readXml( const QDomNode &layerNode )
79{
80 mExpressions.clear();
81
82 const QDomElement expressionFieldsElem = layerNode.firstChildElement( u"expressionfields"_s );
83
84 if ( !expressionFieldsElem.isNull() )
85 {
86 const QDomNodeList fields = expressionFieldsElem.elementsByTagName( u"field"_s );
87
88 for ( int i = 0; i < fields.size(); ++i )
89 {
90 const QDomElement field = fields.at( i ).toElement();
91 const QString exp = field.attribute( u"expression"_s );
92 const QString name = field.attribute( u"name"_s );
93 const QString comment = field.attribute( u"comment"_s );
94 const int precision = field.attribute( u"precision"_s ).toInt();
95 const int length = field.attribute( u"length"_s ).toInt();
96 const QMetaType::Type type = static_cast< QMetaType::Type >( field.attribute( u"type"_s ).toInt() );
97 const QMetaType::Type subType = static_cast< QMetaType::Type >( field.attribute( u"subType"_s, u"0"_s ).toInt() );
98 const QString typeName = field.attribute( u"typeName"_s );
99
100 mExpressions.append( ExpressionField( exp, QgsField( name, type, typeName, length, precision, comment, subType ) ) );
101 }
102 }
103}
104
106{
107 int index = 0;
108 const auto constMExpressions = mExpressions;
109 for ( const ExpressionField &fld : constMExpressions )
110 {
111 flds.appendExpressionField( fld.field, index );
112 ++index;
113 }
114}
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:45
bool appendExpressionField(const QgsField &field, int originIndex)
Appends an expression field. The field must have unique name, otherwise it is rejected (returns false...