QGIS API Documentation  2.9.0-Master
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 dot kuhn at gmx 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 
26 void QgsExpressionFieldBuffer::addExpression( const QString& exp, const QgsField& fld )
27 {
28  mExpressions << ExpressionField( exp, fld );
29 }
30 
32 {
33  mExpressions.removeAt( index );
34 }
35 
36 void QgsExpressionFieldBuffer::updateExpression( int index, const QString& exp )
37 {
38  mExpressions[index].expression = exp;
39 }
40 
41 void QgsExpressionFieldBuffer::writeXml( QDomNode& layerNode, QDomDocument& document ) const
42 {
43  QDomElement expressionFieldsElem = document.createElement( "expressionfields" );
44  layerNode.appendChild( expressionFieldsElem );
45 
46  Q_FOREACH ( const ExpressionField& fld, mExpressions )
47  {
48  QDomElement fldElem = document.createElement( "field" );
49 
50  fldElem.setAttribute( "expression", fld.expression );
51  fldElem.setAttribute( "name", fld.field.name() );
52  fldElem.setAttribute( "precision", fld.field.precision() );
53  fldElem.setAttribute( "comment", fld.field.comment() );
54  fldElem.setAttribute( "length", fld.field.length() );
55  fldElem.setAttribute( "type", fld.field.type() );
56  fldElem.setAttribute( "typeName", fld.field.typeName() );
57 
58  expressionFieldsElem.appendChild( fldElem );
59  }
60 }
61 
62 void QgsExpressionFieldBuffer::readXml( const QDomNode& layerNode )
63 {
64  mExpressions.clear();
65 
66  const QDomElement expressionFieldsElem = layerNode.firstChildElement( "expressionfields" );
67 
68  if ( !expressionFieldsElem.isNull() )
69  {
70  QDomNodeList fields = expressionFieldsElem.elementsByTagName( "field" );
71 
72  for ( unsigned int i = 0; i < fields.length(); ++i )
73  {
74  QDomElement field = fields.at( i ).toElement();
75  QString exp = field.attribute( "expression" );
76  QString name = field.attribute( "name" );
77  QString comment = field.attribute( "comment" );
78  int precision = field.attribute( "precision" ).toInt();
79  int length = field.attribute( "length" ).toInt();
80  QVariant::Type type = ( QVariant::Type )( field.attribute( "type" ).toInt() );
81  QString typeName = field.attribute( "typeName" );
82 
83  mExpressions.append( ExpressionField( exp, QgsField( name, type, typeName, length, precision, comment ) ) );
84  }
85  }
86 }
87 
89 {
90  int index = 0;
91  Q_FOREACH ( const ExpressionField& fld, mExpressions )
92  {
93  flds.appendExpressionField( fld.field, index );
94  ++index;
95  }
96 }
const QString & name() const
Gets the name of the field.
Definition: qgsfield.cpp:69
static unsigned index
struct QgsExpressionFieldBuffer::ExpressionField ExpressionField
void addExpression(const QString &exp, const QgsField &fld)
Add an expression to the buffer.
int precision() const
Gets the precision of the field.
Definition: qgsfield.cpp:89
Container of fields for a vector layer.
Definition: qgsfield.h:169
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:221
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:38
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.
const QString & typeName() const
Gets the field type.
Definition: qgsfield.cpp:79
void readXml(const QDomNode &layer_node)
Reads expressions from project file.
void removeExpression(int index)
Remove an expression from the buffer.
int length() const
Gets the length of the field.
Definition: qgsfield.cpp:84
const QString & comment() const
Returns the field comment.
Definition: qgsfield.cpp:94
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:74