QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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::writeXml( QDomNode& layerNode, QDomDocument& document ) const
37 {
38  QDomElement expressionFieldsElem = document.createElement( "expressionfields" );
39  layerNode.appendChild( expressionFieldsElem );
40 
41  Q_FOREACH ( const ExpressionField& fld, mExpressions )
42  {
43  QDomElement fldElem = document.createElement( "field" );
44 
45  fldElem.setAttribute( "expression", fld.expression );
46  fldElem.setAttribute( "name", fld.field.name() );
47  fldElem.setAttribute( "precision", fld.field.precision() );
48  fldElem.setAttribute( "comment", fld.field.comment() );
49  fldElem.setAttribute( "length", fld.field.length() );
50  fldElem.setAttribute( "type", fld.field.type() );
51  fldElem.setAttribute( "typeName", fld.field.typeName() );
52 
53  expressionFieldsElem.appendChild( fldElem );
54  }
55 }
56 
57 void QgsExpressionFieldBuffer::readXml( const QDomNode& layerNode )
58 {
59  mExpressions.clear();
60 
61  const QDomElement expressionFieldsElem = layerNode.firstChildElement( "expressionfields" );
62 
63  if ( !expressionFieldsElem.isNull() )
64  {
65  QDomNodeList fields = expressionFieldsElem.elementsByTagName( "field" );
66 
67  for ( unsigned int i = 0; i < fields.length(); ++i )
68  {
69  QDomElement field = fields.at( i ).toElement();
70  QString exp = field.attribute( "expression" );
71  QString name = field.attribute( "name" );
72  QString comment = field.attribute( "comment" );
73  int precision = field.attribute( "precision" ).toInt();
74  int length = field.attribute( "length" ).toInt();
75  QVariant::Type type = ( QVariant::Type )( field.attribute( "type" ).toInt() );
76  QString typeName = field.attribute( "typeName" );
77 
78  mExpressions.append( ExpressionField( exp, QgsField( name, type, typeName, length, precision, comment ) ) );
79  }
80  }
81 }
82 
84 {
85  int index = 0;
86  Q_FOREACH ( const ExpressionField& fld, mExpressions )
87  {
88  flds.appendExpressionField( fld.field, index );
89  ++index;
90  }
91 }
const QString & name() const
Gets the name of the field.
Definition: qgsfield.cpp:58
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:78
Container of fields for a vector layer.
Definition: qgsfield.h:163
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:153
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:33
void updateFields(QgsFields &flds)
Adds fields with the expressions buffered in this object to a QgsFields object.
const QString & typeName() const
Gets the field type.
Definition: qgsfield.cpp:68
QList< ExpressionField > mExpressions
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:73
const QString & comment() const
Returns the field comment.
Definition: qgsfield.cpp:83
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:63