QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
qgsdatadefined.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdatadefined.cpp - Data defined container class
3  --------------------------------------
4  Date : 9-May-2013
5  Copyright : (C) 2013 by Larry Shaffer
6  Email : larrys at dakcarto dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsdatadefined.h"
17 
18 #include "qgslogger.h"
19 #include "qgsexpression.h"
20 #include "qgsfield.h"
21 #include "qgsvectorlayer.h"
22 
24  bool useexpr,
25  const QString& expr,
26  const QString& field )
27  : mActive( active )
28  , mUseExpression( useexpr )
29  , mExpressionString( expr )
30  , mField( field )
31 {
32  mExpression = 0;
33  mExpressionPrepared = false;
34 }
35 
37 {
38  mExpressionParams.clear();
39  delete mExpression;
40 }
41 
43 {
44  return ( !mActive && !mUseExpression && mExpressionString.isEmpty() && mField.isEmpty() );
45 }
46 
47 void QgsDataDefined::setExpressionString( const QString &expr )
48 {
49  mExpressionString = expr;
50  mExpressionPrepared = false;
51 }
52 
54 {
55  if ( !mUseExpression || mExpressionString.isEmpty() )
56  {
57  return false;
58  }
59 
60  mExpression = new QgsExpression( mExpressionString );
61  if ( mExpression->hasParserError() )
62  {
63  QgsDebugMsg( "Parser error:" + mExpression->parserErrorString() );
64  return false;
65  }
66 
67  // setup expression parameters
68  QVariant scaleV = mExpressionParams.value( "scale" );
69  if ( scaleV.isValid() )
70  {
71  bool ok;
72  double scale = scaleV.toDouble( &ok );
73  if ( ok )
74  {
75  mExpression->setScale( scale );
76  }
77  }
78 
79  if ( layer )
80  {
81  mExpression->prepare( layer->pendingFields() );
82  }
83  else
84  {
85  //preparing expression without a layer set, so pass empty field list
86  QgsFields empty;
87  mExpression->prepare( empty );
88  }
89 
90  if ( mExpression->hasEvalError() )
91  {
92  QgsDebugMsg( "Prepare error:" + mExpression->evalErrorString() );
93  return false;
94  }
95 
96  mExpressionPrepared = true;
97  mExprRefColmuns = mExpression->referencedColumns();
98 
99  return true;
100 }
101 
103 {
104  if ( !mExprRefColmuns.isEmpty() )
105  {
106  return mExprRefColmuns;
107  }
108 
109  if ( mUseExpression )
110  {
111  if ( !mExpression || !mExpressionPrepared )
112  {
113  prepareExpression( layer );
114  }
115  }
116  else if ( !mField.isEmpty() )
117  {
118  mExprRefColmuns << mField;
119  }
120 
121  return mExprRefColmuns;
122 }
123 
124 void QgsDataDefined::insertExpressionParam( QString key, QVariant param )
125 {
126  mExpressionParams.insert( key, param );
127 }
128 
129 QMap< QString, QString > QgsDataDefined::toMap()
130 {
131  QMap< QString, QString > map;
132  map.insert( "active", ( mActive ? "1" : "0" ) );
133  map.insert( "useexpr", ( mUseExpression ? "1" : "0" ) );
134  map.insert( "expression", mExpressionString );
135  map.insert( "field", mField );
136 
137  return map;
138 }
139 
140 QDomElement QgsDataDefined::toXmlElement( QDomDocument &document, const QString& elementName ) const
141 {
142  QDomElement element = document.createElement( elementName );
143  element.setAttribute( "active", mActive ? "true" : "false" );
144  element.setAttribute( "useExpr", mUseExpression ? "true" : "false" );
145  element.setAttribute( "expr", mExpressionString );
146  element.setAttribute( "field", mField );
147  return element;
148 }
149 
150 bool QgsDataDefined::setFromXmlElement( const QDomElement &element )
151 {
152  if ( element.isNull() )
153  {
154  return false;
155  }
156 
157  mActive = element.attribute( "active" ).compare( "true", Qt::CaseInsensitive ) == 0;
158  mUseExpression = element.attribute( "useExpr" ).compare( "true", Qt::CaseInsensitive ) == 0;
159  mField = element.attribute( "field" );
160  setExpressionString( element.attribute( "expr" ) );
161  return true;
162 }
163 
164 bool QgsDataDefined::operator==( const QgsDataDefined &other ) const
165 {
166  return other.isActive() == mActive && other.useExpression() == mUseExpression &&
167  other.field() == mField && other.expressionString() == mExpressionString;
168 }
169 
170 bool QgsDataDefined::operator!=( const QgsDataDefined &other ) const
171 {
172  return !( *this == other );
173 }
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:87
bool hasEvalError() const
Returns true if an error occurred when evaluating last input.
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
Definition: qgsexpression.h:94
QStringList referencedColumns() const
Get list of columns referenced by the expression.
A container class for data source field mapping or expression.
bool operator!=(const QgsDataDefined &other) const
QString field() const
bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
void insertExpressionParam(QString key, QVariant param)
Container of fields for a vector layer.
Definition: qgsfield.h:172
QString expressionString() const
bool setFromXmlElement(const QDomElement &element)
Sets the properties of the data defined container from an XML element.
QMap< QString, QString > toMap()
QDomElement toXmlElement(QDomDocument &document, const QString &elementName) const
Returns a DOM element containing the properties of the data defined container.
bool operator==(const QgsDataDefined &other) const
bool useExpression() const
QgsDataDefined(bool active=false, bool useexpr=false, const QString &expr=QString(), const QString &field=QString())
Construct a new data defined object.
bool prepareExpression(QgsVectorLayer *layer)
bool hasDefaultValues() const
Returns whether the data defined container is set to all the default values, ie, disabled, with empty expression and no assigned field.
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
void setScale(double scale)
Represents a vector layer which manages a vector based data sets.
QString parserErrorString() const
Returns parser error.
Definition: qgsexpression.h:96
QString evalErrorString() const
Returns evaluation error.
void setExpressionString(const QString &expr)
bool isActive() const
QStringList referencedColumns(QgsVectorLayer *layer)