Skip to content

Commit 107247e

Browse files
committed
Add method to access label string as expression, QgsExpression only created once then cached.
1 parent f64ae1c commit 107247e

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

src/CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ ADD_SUBDIRECTORY(ui)
44
ADD_SUBDIRECTORY(gui)
55
ADD_SUBDIRECTORY(app)
66
ADD_SUBDIRECTORY(providers)
7-
ADD_SUBDIRECTORY(plugins)
8-
ADD_SUBDIRECTORY(helpviewer)
7+
#ADD_SUBDIRECTORY(plugins)
8+
#ADD_SUBDIRECTORY(helpviewer)
99
ADD_SUBDIRECTORY(crssync)
10-
ADD_SUBDIRECTORY(browser)
10+
#ADD_SUBDIRECTORY(browser)
1111

1212
IF (WITH_BINDINGS)
1313
ADD_SUBDIRECTORY(python)

src/core/qgspallabeling.cpp

+18-8
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class QgsPalGeometry : public PalGeometry
133133
// -------------
134134

135135
QgsPalLayerSettings::QgsPalLayerSettings()
136-
: palLayer( NULL ), fontMetrics( NULL ), ct( NULL ), extentGeom( NULL )
136+
: palLayer( NULL ), fontMetrics( NULL ), ct( NULL ), extentGeom( NULL ), expression( NULL )
137137
{
138138
placement = AroundPoint;
139139
placementFlags = 0;
@@ -195,6 +195,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
195195
fontMetrics = NULL;
196196
ct = NULL;
197197
extentGeom = NULL;
198+
expression = NULL;
198199
}
199200

200201

@@ -204,10 +205,19 @@ QgsPalLayerSettings::~QgsPalLayerSettings()
204205

205206
delete fontMetrics;
206207
delete ct;
207-
208+
delete expression;
208209
delete extentGeom;
209210
}
210211

212+
QgsExpression* QgsPalLayerSettings::getLabelExpression()
213+
{
214+
if (expression == NULL)
215+
{
216+
expression = new QgsExpression( fieldName );
217+
}
218+
return expression;
219+
}
220+
211221
static QColor _readColor( QgsVectorLayer* layer, QString property )
212222
{
213223
int r = layer->customProperty( property + "R" ).toInt();
@@ -458,17 +468,17 @@ void QgsPalLayerSettings::registerFeature(QgsVectorLayer* layer, QgsFeature& f,
458468
// Check to see if we are a expression string.
459469
if (isExpression)
460470
{
461-
QgsExpression exp( fieldName );
462-
if ( exp.hasParserError() )
471+
QgsExpression* exp = getLabelExpression();
472+
if ( exp->hasParserError() )
463473
{
464-
QgsDebugMsg("PASER HAS ERROR:" + exp.parserErrorString());
474+
QgsDebugMsg("PASER HAS ERROR:" + exp->parserErrorString());
465475
return;
466476
}
467-
QVariant result = exp.evaluate(&f,layer->dataProvider()->fields());
477+
QVariant result = exp->evaluate(&f,layer->dataProvider()->fields());
468478
QgsDebugMsg("VALUE = " + result.toString());
469-
if (exp.hasEvalError())
479+
if (exp->hasEvalError())
470480
{
471-
QgsDebugMsg("Expression Label Error = " + exp.evalErrorString());
481+
QgsDebugMsg("Expression Label Error = " + exp->evalErrorString());
472482
return;
473483
}
474484
labelText = result.toString();

src/core/qgspallabeling.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ namespace pal
4646

4747
class QgsMapToPixel;
4848
class QgsFeature;
49-
#include "qgspoint.h"
5049

50+
#include "qgspoint.h"
5151
#include "qgsmaprenderer.h" // definition of QgsLabelingEngineInterface
52+
#include "qgsexpression.h"
5253

5354
class QgsPalGeometry;
5455
class QgsVectorLayer;
@@ -98,7 +99,15 @@ class CORE_EXPORT QgsPalLayerSettings
9899
};
99100

100101
QString fieldName;
101-
bool isExpression; // is this label made from a expression string eg FieldName || 'mm'
102+
103+
/** Is this label made from a expression string eg FieldName || 'mm'
104+
*/
105+
bool isExpression;
106+
107+
/** Returns the QgsExpression for this label settings.
108+
*/
109+
QgsExpression* getLabelExpression();
110+
102111
Placement placement;
103112
unsigned int placementFlags;
104113
QFont textFont;
@@ -162,6 +171,7 @@ class CORE_EXPORT QgsPalLayerSettings
162171
/**Checks if a feature is larger than a minimum size (in mm)
163172
@return true if above size, false if below*/
164173
bool checkMinimumSizeMM( const QgsRenderContext& ct, QgsGeometry* geom, double minSize ) const;
174+
QgsExpression* expression;
165175
};
166176

167177
class CORE_EXPORT QgsLabelCandidate

src/gui/qgsexpressionbuilder.h

+8
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,21 @@ class QgsExpressionItem : public QStandardItem
5555

5656
QString getExpressionText() { return mExpressionText; }
5757

58+
/** Get the help text that is associated with this expression item.
59+
*
60+
* @return The help text.
61+
*/
5862
QString getHelpText() { return mHelpText; }
5963
/** Set the help text for the current item
6064
*
6165
* @note The help text can be set as a html string.
6266
*/
6367
void setHelpText(QString helpText) { mHelpText = helpText; }
6468

69+
/** Get the type of expression item eg header, field, ExpressionNode.
70+
*
71+
* @return The QgsExpressionItem::ItemType
72+
*/
6573
QgsExpressionItem::ItemType getItemType() { return mType ; }
6674

6775
private:

0 commit comments

Comments
 (0)