QGIS API Documentation  2.8.2-Wien
 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 }