QGIS API Documentation
2.0.1-Dufour
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
src
core
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
23
QgsDataDefined::QgsDataDefined
(
bool
active,
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
36
QgsDataDefined::~QgsDataDefined
()
37
{
38
mExpressionParams
.clear();
39
delete
mExpression
;
40
}
41
42
bool
QgsDataDefined::prepareExpression
(
QgsVectorLayer
* layer )
43
{
44
if
( !
mUseExpression
||
mExpressionString
.isEmpty() )
45
{
46
return
false
;
47
}
48
49
mExpression
=
new
QgsExpression
(
mExpressionString
);
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
;
76
mExprRefColmuns
=
mExpression
->
referencedColumns
();
77
78
return
true
;
79
}
80
81
QStringList
QgsDataDefined::referencedColumns
(
QgsVectorLayer
* layer )
82
{
83
if
( !
mExprRefColmuns
.isEmpty() )
84
{
85
return
mExprRefColmuns
;
86
}
87
88
if
(
mUseExpression
)
89
{
90
if
( !
mExpression
|| !
mExpressionPrepared
)
91
{
92
prepareExpression
( layer );
93
}
94
}
95
else
if
( !
mField
.isEmpty() )
96
{
97
mExprRefColmuns
<<
mField
;
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
}
Generated on Tue Sep 24 2013 14:41:46 for QGIS API Documentation by
1.8.1.2