QGIS API Documentation  2.99.0-Master (37c43df)
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 
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::renameExpression( int index, const QString& name )
37 {
38  mExpressions[index].field.setName( name );
39 }
40 
41 void QgsExpressionFieldBuffer::updateExpression( int index, const QString& exp )
42 {
43  mExpressions[index].cachedExpression = QgsExpression( exp );
44 }
45 
46 void QgsExpressionFieldBuffer::writeXml( QDomNode& layerNode, QDomDocument& document ) const
47 {
48  QDomElement expressionFieldsElem = document.createElement( QStringLiteral( "expressionfields" ) );
49  layerNode.appendChild( expressionFieldsElem );
50 
51  Q_FOREACH ( const ExpressionField& fld, mExpressions )
52  {
53  QDomElement fldElem = document.createElement( QStringLiteral( "field" ) );
54 
55  fldElem.setAttribute( QStringLiteral( "expression" ), fld.cachedExpression.expression() );
56  fldElem.setAttribute( QStringLiteral( "name" ), fld.field.name() );
57  fldElem.setAttribute( QStringLiteral( "precision" ), fld.field.precision() );
58  fldElem.setAttribute( QStringLiteral( "comment" ), fld.field.comment() );
59  fldElem.setAttribute( QStringLiteral( "length" ), fld.field.length() );
60  fldElem.setAttribute( QStringLiteral( "type" ), fld.field.type() );
61  fldElem.setAttribute( QStringLiteral( "subType" ), fld.field.subType() );
62  fldElem.setAttribute( QStringLiteral( "typeName" ), fld.field.typeName() );
63 
64  expressionFieldsElem.appendChild( fldElem );
65  }
66 }
67 
68 void QgsExpressionFieldBuffer::readXml( const QDomNode& layerNode )
69 {
70  mExpressions.clear();
71 
72  const QDomElement expressionFieldsElem = layerNode.firstChildElement( QStringLiteral( "expressionfields" ) );
73 
74  if ( !expressionFieldsElem.isNull() )
75  {
76  QDomNodeList fields = expressionFieldsElem.elementsByTagName( QStringLiteral( "field" ) );
77 
78  for ( int i = 0; i < fields.size(); ++i )
79  {
80  QDomElement field = fields.at( i ).toElement();
81  QString exp = field.attribute( QStringLiteral( "expression" ) );
82  QString name = field.attribute( QStringLiteral( "name" ) );
83  QString comment = field.attribute( QStringLiteral( "comment" ) );
84  int precision = field.attribute( QStringLiteral( "precision" ) ).toInt();
85  int length = field.attribute( QStringLiteral( "length" ) ).toInt();
86  QVariant::Type type = static_cast< QVariant::Type >( field.attribute( QStringLiteral( "type" ) ).toInt() );
87  QVariant::Type subType = static_cast< QVariant::Type >( field.attribute( QStringLiteral( "subType" ), 0 ).toInt() );
88  QString typeName = field.attribute( QStringLiteral( "typeName" ) );
89 
90  mExpressions.append( ExpressionField( exp, QgsField( name, type, typeName, length, precision, comment, subType ) ) );
91  }
92  }
93 }
94 
96 {
97  int index = 0;
98  Q_FOREACH ( const ExpressionField& fld, mExpressions )
99  {
100  flds.appendExpressionField( fld.field, index );
101  ++index;
102  }
103 }
Class for parsing and evaluation of expressions (formerly called "search strings").
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:55
void addExpression(const QString &exp, const QgsField &fld)
Add an expression to the buffer.
int precision
Definition: qgsfield.h:53
QString comment
Definition: qgsfield.h:54
Container of fields for a vector layer.
Definition: qgsfields.h:36
bool appendExpressionField(const QgsField &field, int originIndex)
Append an expression field. The field must have unique name, otherwise it is rejected (returns false)...
Definition: qgsfields.cpp:74
int length
Definition: qgsfield.h:52
QString typeName() const
Gets the field type.
Definition: qgsfield.cpp:108
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:47
void updateFields(QgsFields &flds)
Adds fields with the expressions buffered in this object to a QgsFields object.
QString expression() const
Return the original, unmodified expression string.
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.
void removeExpression(int index)
Remove an expression from the buffer.
QVariant::Type subType() const
If the field is a collection, gets its element&#39;s type.
Definition: qgsfield.cpp:103
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:98