QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
42 void QgsDataDefined::setExpressionString( const QString &expr )
43 {
44  mExpressionString = expr;
45  mExpressionPrepared = false;
46 }
47 
49 {
50  if ( !mUseExpression || mExpressionString.isEmpty() )
51  {
52  return false;
53  }
54 
56  if ( mExpression->hasParserError() )
57  {
58  QgsDebugMsg( "Parser error:" + mExpression->parserErrorString() );
59  return false;
60  }
61 
62  // setup expression parameters
63  QVariant scaleV = mExpressionParams.value( "scale" );
64  if ( scaleV.isValid() )
65  {
66  bool ok;
67  double scale = scaleV.toDouble( &ok );
68  if ( ok )
69  {
70  mExpression->setScale( scale );
71  }
72  }
73 
74  if ( layer )
75  {
76  mExpression->prepare( layer->pendingFields() );
77  }
78  else
79  {
80  //preparing expression without a layer set, so pass empty field list
81  QgsFields empty;
82  mExpression->prepare( empty );
83  }
84 
85  if ( mExpression->hasEvalError() )
86  {
87  QgsDebugMsg( "Prepare error:" + mExpression->evalErrorString() );
88  return false;
89  }
90 
91  mExpressionPrepared = true;
93 
94  return true;
95 }
96 
98 {
99  if ( !mExprRefColmuns.isEmpty() )
100  {
101  return mExprRefColmuns;
102  }
103 
104  if ( mUseExpression )
105  {
106  if ( !mExpression || !mExpressionPrepared )
107  {
108  prepareExpression( layer );
109  }
110  }
111  else if ( !mField.isEmpty() )
112  {
114  }
115 
116  return mExprRefColmuns;
117 }
118 
119 void QgsDataDefined::insertExpressionParam( QString key, QVariant param )
120 {
121  mExpressionParams.insert( key, param );
122 }
123 
124 QMap< QString, QString > QgsDataDefined::toMap()
125 {
126  QMap< QString, QString > map;
127  map.insert( "active", ( mActive ? "1" : "0" ) );
128  map.insert( "useexpr", ( mUseExpression ? "1" : "0" ) );
129  map.insert( "expression", mExpressionString );
130  map.insert( "field", mField );
131 
132  return map;
133 }
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:89
bool mExpressionPrepared
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:96
bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
void insertExpressionParam(QString key, QVariant param)
Container of fields for a vector layer.
Definition: qgsfield.h:161
QStringList mExprRefColmuns
QgsExpression * mExpression
QMap< QString, QVariant > mExpressionParams
QMap< QString, QString > toMap()
QStringList referencedColumns()
Get list of columns referenced by the expression.
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)
QString mExpressionString
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:98
QString evalErrorString() const
Returns evaluation error.
void setExpressionString(const QString &expr)
QStringList referencedColumns(QgsVectorLayer *layer)