QGIS API Documentation  2.0.1-Dufour
 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 }