QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
23 void QgsExpressionFieldBuffer::addExpression( const QString &exp, const QgsField &fld )
24 {
25  mExpressions << ExpressionField( exp, fld );
26 }
27 
29 {
30  mExpressions.removeAt( index );
31 }
32 
33 void QgsExpressionFieldBuffer::renameExpression( int index, const QString &name )
34 {
35  mExpressions[index].field.setName( name );
36 }
37 
38 void QgsExpressionFieldBuffer::updateExpression( int index, const QString &exp )
39 {
40  mExpressions[index].cachedExpression = QgsExpression( exp );
41 }
42 
43 void 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 
66 void 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  QDomNodeList fields = expressionFieldsElem.elementsByTagName( QStringLiteral( "field" ) );
75 
76  for ( int i = 0; i < fields.size(); ++i )
77  {
78  QDomElement field = fields.at( i ).toElement();
79  QString exp = field.attribute( QStringLiteral( "expression" ) );
80  QString name = field.attribute( QStringLiteral( "name" ) );
81  QString comment = field.attribute( QStringLiteral( "comment" ) );
82  int precision = field.attribute( QStringLiteral( "precision" ) ).toInt();
83  int length = field.attribute( QStringLiteral( "length" ) ).toInt();
84  QVariant::Type type = static_cast< QVariant::Type >( field.attribute( QStringLiteral( "type" ) ).toInt() );
85  QVariant::Type subType = static_cast< QVariant::Type >( field.attribute( QStringLiteral( "subType" ), QStringLiteral( "0" ) ).toInt() );
86  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 }
QgsExpressionFieldBuffer::writeXml
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves expressions to xml under the layer node.
Definition: qgsexpressionfieldbuffer.cpp:43
qgsexpressionfieldbuffer.h
QgsExpressionFieldBuffer::ExpressionField
Definition: qgsexpressionfieldbuffer.h:51
QgsFields
Definition: qgsfields.h:44
QgsExpressionFieldBuffer::updateExpression
void updateExpression(int index, const QString &exp)
Changes the expression at a given index.
Definition: qgsexpressionfieldbuffer.cpp:38
QgsExpressionFieldBuffer::removeExpression
void removeExpression(int index)
Remove an expression from the buffer.
Definition: qgsexpressionfieldbuffer.cpp:28
QgsExpressionFieldBuffer::readXml
void readXml(const QDomNode &layer_node)
Reads expressions from project file.
Definition: qgsexpressionfieldbuffer.cpp:66
precision
int precision
Definition: qgswfsgetfeature.cpp:103
QgsExpressionFieldBuffer::addExpression
void addExpression(const QString &exp, const QgsField &fld)
Add an expression to the buffer.
Definition: qgsexpressionfieldbuffer.cpp:23
typeName
const QString & typeName
Definition: qgswfsgetfeature.cpp:109
qgsvectorlayer.h
QgsExpressionFieldBuffer::updateFields
void updateFields(QgsFields &flds)
Adds fields with the expressions buffered in this object to a QgsFields object.
Definition: qgsexpressionfieldbuffer.cpp:93
QgsExpressionFieldBuffer::renameExpression
void renameExpression(int index, const QString &name)
Renames an expression field at a given index.
Definition: qgsexpressionfieldbuffer.cpp:33
QgsExpression
Definition: qgsexpression.h:113
QgsFields::appendExpressionField
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
QgsField
Definition: qgsfield.h:49