QGIS API Documentation  2.3.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 
43 {
44  if ( !mUseExpression || mExpressionString.isEmpty() )
45  {
46  return false;
47  }
48 
50  if ( mExpression->hasParserError() )
51  {
52  QgsDebugMsg( "Parser error:" + mExpression->parserErrorString() );
53  return false;
54  }
55 
56  // setup expression parameters
57  QVariant scaleV = mExpressionParams.value( "scale" );
58  if ( scaleV.isValid() )
59  {
60  bool ok;
61  double scale = scaleV.toDouble( &ok );
62  if ( ok )
63  {
64  mExpression->setScale( scale );
65  }
66  }
67 
68  mExpression->prepare( layer->pendingFields() );
69  if ( mExpression->hasEvalError() )
70  {
71  QgsDebugMsg( "Prepare error:" + mExpression->evalErrorString() );
72  return false;
73  }
74 
75  mExpressionPrepared = true;
77 
78  return true;
79 }
80 
82 {
83  if ( !mExprRefColmuns.isEmpty() )
84  {
85  return mExprRefColmuns;
86  }
87 
88  if ( mUseExpression )
89  {
91  {
92  prepareExpression( layer );
93  }
94  }
95  else if ( !mField.isEmpty() )
96  {
98  }
99 
100  return mExprRefColmuns;
101 }
102 
103 void QgsDataDefined::insertExpressionParam( QString key, QVariant param )
104 {
105  mExpressionParams.insert( key, param );
106 }
107 
108 QMap< QString, QString > QgsDataDefined::toMap()
109 {
110  QMap< QString, QString > map;
111  map.insert( "active", ( mActive ? "1" : "0" ) );
112  map.insert( "useexpr", ( mUseExpression ? "1" : "0" ) );
113  map.insert( "expression", mExpressionString );
114  map.insert( "field", mField );
115 
116  return map;
117 }
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)
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.
QStringList referencedColumns(QgsVectorLayer *layer)