QGIS API Documentation  3.0.2-Girona (307d082)
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  Q_FOREACH ( const ExpressionField &fld, mExpressions )
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 
65 void 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  QDomNodeList fields = expressionFieldsElem.elementsByTagName( QStringLiteral( "field" ) );
74 
75  for ( int i = 0; i < fields.size(); ++i )
76  {
77  QDomElement field = fields.at( i ).toElement();
78  QString exp = field.attribute( QStringLiteral( "expression" ) );
79  QString name = field.attribute( QStringLiteral( "name" ) );
80  QString comment = field.attribute( QStringLiteral( "comment" ) );
81  int precision = field.attribute( QStringLiteral( "precision" ) ).toInt();
82  int length = field.attribute( QStringLiteral( "length" ) ).toInt();
83  QVariant::Type type = static_cast< QVariant::Type >( field.attribute( QStringLiteral( "type" ) ).toInt() );
84  QVariant::Type subType = static_cast< QVariant::Type >( field.attribute( QStringLiteral( "subType" ), QStringLiteral( "0" ) ).toInt() );
85  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  Q_FOREACH ( const ExpressionField &fld, mExpressions )
96  {
97  flds.appendExpressionField( fld.field, index );
98  ++index;
99  }
100 }
void renameExpression(int index, const QString &name)
Renames an expression field at a given index.
QString name
Definition: qgsfield.h:57
void addExpression(const QString &exp, const QgsField &fld)
Add an expression to the buffer.
int precision
Definition: qgsfield.h:54
QString comment
Definition: qgsfield.h:56
Container of fields for a vector layer.
Definition: qgsfields.h:42
bool appendExpressionField(const QgsField &field, int originIndex)
Append an expression field. The field must have unique name, otherwise it is rejected (returns false)...
Definition: qgsfields.cpp:72
int length
Definition: qgsfield.h:53
QString typeName() const
Gets the field type.
Definition: qgsfield.cpp:103
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves expressions to xml under the layer node.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
void updateFields(QgsFields &flds)
Adds fields with the expressions buffered in this object to a QgsFields object.
void updateExpression(int index, const QString &exp)
Changes the expression at a given index.
void readXml(const QDomNode &layer_node)
Reads expressions from project file.
void removeExpression(int index)
Remove an expression from the buffer.
QVariant::Type subType() const
If the field is a collection, gets its element&#39;s type.
Definition: qgsfield.cpp:98
QVariant::Type type
Definition: qgsfield.h:55