QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
23 {
24 }
25 
27 {
28  mExpressions << ExpressionField( exp, fld );
29 }
30 
32 {
33  mExpressions.removeAt( index );
34 }
35 
37 {
38  mExpressions[index].field.setName( name );
39 }
40 
42 {
43  mExpressions[index].expression = exp;
44  mExpressions[index].cachedExpression = QgsExpression( exp );
45 }
46 
47 void QgsExpressionFieldBuffer::writeXml( QDomNode& layerNode, QDomDocument& document ) const
48 {
49  QDomElement expressionFieldsElem = document.createElement( "expressionfields" );
50  layerNode.appendChild( expressionFieldsElem );
51 
52  Q_FOREACH ( const ExpressionField& fld, mExpressions )
53  {
54  QDomElement fldElem = document.createElement( "field" );
55 
56  fldElem.setAttribute( "expression", fld.expression );
57  fldElem.setAttribute( "name", fld.field.name() );
58  fldElem.setAttribute( "precision", fld.field.precision() );
59  fldElem.setAttribute( "comment", fld.field.comment() );
60  fldElem.setAttribute( "length", fld.field.length() );
61  fldElem.setAttribute( "type", fld.field.type() );
62  fldElem.setAttribute( "typeName", fld.field.typeName() );
63 
64  expressionFieldsElem.appendChild( fldElem );
65  }
66 }
67 
69 {
70  mExpressions.clear();
71 
72  const QDomElement expressionFieldsElem = layerNode.firstChildElement( "expressionfields" );
73 
74  if ( !expressionFieldsElem.isNull() )
75  {
76  QDomNodeList fields = expressionFieldsElem.elementsByTagName( "field" );
77 
78  for ( int i = 0; i < fields.size(); ++i )
79  {
80  QDomElement field = fields.at( i ).toElement();
81  QString exp = field.attribute( "expression" );
82  QString name = field.attribute( "name" );
83  QString comment = field.attribute( "comment" );
84  int precision = field.attribute( "precision" ).toInt();
85  int length = field.attribute( "length" ).toInt();
86  QVariant::Type type = static_cast< QVariant::Type >( field.attribute( "type" ).toInt() );
87  QString typeName = field.attribute( "typeName" );
88 
89  mExpressions.append( ExpressionField( exp, QgsField( name, type, typeName, length, precision, comment ) ) );
90  }
91  }
92 }
93 
95 {
96  int index = 0;
97  Q_FOREACH ( const ExpressionField& fld, mExpressions )
98  {
99  flds.appendExpressionField( fld.field, index );
100  ++index;
101  }
102 }
Class for parsing and evaluation of expressions (formerly called "search strings").
QDomNodeList elementsByTagName(const QString &tagname) const
static unsigned index
void renameExpression(int index, const QString &name)
Renames an expression field at a given index.
struct QgsExpressionFieldBuffer::ExpressionField ExpressionField
QString name
Definition: qgsfield.h:52
void addExpression(const QString &exp, const QgsField &fld)
Add an expression to the buffer.
int precision
Definition: qgsfield.h:50
QDomNode appendChild(const QDomNode &newChild)
QString attribute(const QString &name, const QString &defValue) const
QString comment
Definition: qgsfield.h:51
Container of fields for a vector layer.
Definition: qgsfield.h:252
bool appendExpressionField(const QgsField &field, int originIndex)
Append an expression field. The field must have unique name, otherwise it is rejected (returns false)...
Definition: qgsfield.cpp:359
int length
Definition: qgsfield.h:49
QDomElement toElement() const
QString typeName() const
Gets the field type.
Definition: qgsfield.cpp:102
void setAttribute(const QString &name, const QString &value)
int toInt(bool *ok, int base) const
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:44
void updateFields(QgsFields &flds)
Adds fields with the expressions buffered in this object to a QgsFields object.
bool isNull() const
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.
QDomElement firstChildElement(const QString &tagName) const
void removeExpression(int index)
Remove an expression from the buffer.
int size() const
QDomElement createElement(const QString &tagName)
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:97
QDomNode at(int index) const