QGIS API Documentation  2.15.0-Master (af20121)
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].expression = exp;
39  mExpressions[index].cachedExpression = QgsExpression( exp );
40 }
41 
42 void QgsExpressionFieldBuffer::writeXml( QDomNode& layerNode, QDomDocument& document ) const
43 {
44  QDomElement expressionFieldsElem = document.createElement( "expressionfields" );
45  layerNode.appendChild( expressionFieldsElem );
46 
47  Q_FOREACH ( const ExpressionField& fld, mExpressions )
48  {
49  QDomElement fldElem = document.createElement( "field" );
50 
51  fldElem.setAttribute( "expression", fld.expression );
52  fldElem.setAttribute( "name", fld.field.name() );
53  fldElem.setAttribute( "precision", fld.field.precision() );
54  fldElem.setAttribute( "comment", fld.field.comment() );
55  fldElem.setAttribute( "length", fld.field.length() );
56  fldElem.setAttribute( "type", fld.field.type() );
57  fldElem.setAttribute( "typeName", fld.field.typeName() );
58 
59  expressionFieldsElem.appendChild( fldElem );
60  }
61 }
62 
64 {
65  mExpressions.clear();
66 
67  const QDomElement expressionFieldsElem = layerNode.firstChildElement( "expressionfields" );
68 
69  if ( !expressionFieldsElem.isNull() )
70  {
71  QDomNodeList fields = expressionFieldsElem.elementsByTagName( "field" );
72 
73  for ( int i = 0; i < fields.size(); ++i )
74  {
75  QDomElement field = fields.at( i ).toElement();
76  QString exp = field.attribute( "expression" );
77  QString name = field.attribute( "name" );
78  QString comment = field.attribute( "comment" );
79  int precision = field.attribute( "precision" ).toInt();
80  int length = field.attribute( "length" ).toInt();
81  QVariant::Type type = static_cast< QVariant::Type >( field.attribute( "type" ).toInt() );
82  QString typeName = field.attribute( "typeName" );
83 
84  mExpressions.append( ExpressionField( exp, QgsField( name, type, typeName, length, precision, comment ) ) );
85  }
86  }
87 }
88 
90 {
91  int index = 0;
92  Q_FOREACH ( const ExpressionField& fld, mExpressions )
93  {
94  flds.appendExpressionField( fld.field, index );
95  ++index;
96  }
97 }
Class for parsing and evaluation of expressions (formerly called "search strings").
QDomNodeList elementsByTagName(const QString &tagname) const
static unsigned index
struct QgsExpressionFieldBuffer::ExpressionField ExpressionField
void addExpression(const QString &exp, const QgsField &fld)
Add an expression to the buffer.
QDomNode appendChild(const QDomNode &newChild)
QString attribute(const QString &name, const QString &defValue) const
int precision() const
Gets the precision of the field.
Definition: qgsfield.cpp:104
Container of fields for a vector layer.
Definition: qgsfield.h:193
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:322
QString comment() const
Returns the field comment.
Definition: qgsfield.cpp:109
QDomElement toElement() const
QString typeName() const
Gets the field type.
Definition: qgsfield.cpp:94
void setAttribute(const QString &name, const QString &value)
QString name() const
Gets the name of the field.
Definition: qgsfield.cpp:84
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 length() const
Gets the length of the field.
Definition: qgsfield.cpp:99
int size() const
QDomElement createElement(const QString &tagName)
QDomNode at(int index) const
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:89