Skip to content
Permalink
Browse files

Add method to access label string as expression, QgsExpression only c…

…reated once then cached.
  • Loading branch information
NathanW2 committed Oct 5, 2011
1 parent f64ae1c commit 107247e8e9eab33163f66c15eb5f571a1f82a0d9
Showing with 41 additions and 13 deletions.
  1. +3 −3 src/CMakeLists.txt
  2. +18 −8 src/core/qgspallabeling.cpp
  3. +12 −2 src/core/qgspallabeling.h
  4. +8 −0 src/gui/qgsexpressionbuilder.h
@@ -4,10 +4,10 @@ ADD_SUBDIRECTORY(ui)
ADD_SUBDIRECTORY(gui)
ADD_SUBDIRECTORY(app)
ADD_SUBDIRECTORY(providers)
ADD_SUBDIRECTORY(plugins)
ADD_SUBDIRECTORY(helpviewer)
#ADD_SUBDIRECTORY(plugins)
#ADD_SUBDIRECTORY(helpviewer)
ADD_SUBDIRECTORY(crssync)
ADD_SUBDIRECTORY(browser)
#ADD_SUBDIRECTORY(browser)

IF (WITH_BINDINGS)
ADD_SUBDIRECTORY(python)
@@ -133,7 +133,7 @@ class QgsPalGeometry : public PalGeometry
// -------------

QgsPalLayerSettings::QgsPalLayerSettings()
: palLayer( NULL ), fontMetrics( NULL ), ct( NULL ), extentGeom( NULL )
: palLayer( NULL ), fontMetrics( NULL ), ct( NULL ), extentGeom( NULL ), expression( NULL )
{
placement = AroundPoint;
placementFlags = 0;
@@ -195,6 +195,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
fontMetrics = NULL;
ct = NULL;
extentGeom = NULL;
expression = NULL;
}


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

delete fontMetrics;
delete ct;

delete expression;
delete extentGeom;
}

QgsExpression* QgsPalLayerSettings::getLabelExpression()
{
if (expression == NULL)
{
expression = new QgsExpression( fieldName );
}
return expression;
}

static QColor _readColor( QgsVectorLayer* layer, QString property )
{
int r = layer->customProperty( property + "R" ).toInt();
@@ -458,17 +468,17 @@ void QgsPalLayerSettings::registerFeature(QgsVectorLayer* layer, QgsFeature& f,
// Check to see if we are a expression string.
if (isExpression)
{
QgsExpression exp( fieldName );
if ( exp.hasParserError() )
QgsExpression* exp = getLabelExpression();
if ( exp->hasParserError() )
{
QgsDebugMsg("PASER HAS ERROR:" + exp.parserErrorString());
QgsDebugMsg("PASER HAS ERROR:" + exp->parserErrorString());
return;
}
QVariant result = exp.evaluate(&f,layer->dataProvider()->fields());
QVariant result = exp->evaluate(&f,layer->dataProvider()->fields());
QgsDebugMsg("VALUE = " + result.toString());
if (exp.hasEvalError())
if (exp->hasEvalError())
{
QgsDebugMsg("Expression Label Error = " + exp.evalErrorString());
QgsDebugMsg("Expression Label Error = " + exp->evalErrorString());
return;
}
labelText = result.toString();
@@ -46,9 +46,10 @@ namespace pal

class QgsMapToPixel;
class QgsFeature;
#include "qgspoint.h"

#include "qgspoint.h"
#include "qgsmaprenderer.h" // definition of QgsLabelingEngineInterface
#include "qgsexpression.h"

class QgsPalGeometry;
class QgsVectorLayer;
@@ -98,7 +99,15 @@ class CORE_EXPORT QgsPalLayerSettings
};

QString fieldName;
bool isExpression; // is this label made from a expression string eg FieldName || 'mm'

/** Is this label made from a expression string eg FieldName || 'mm'
*/
bool isExpression;

/** Returns the QgsExpression for this label settings.
*/
QgsExpression* getLabelExpression();

Placement placement;
unsigned int placementFlags;
QFont textFont;
@@ -162,6 +171,7 @@ class CORE_EXPORT QgsPalLayerSettings
/**Checks if a feature is larger than a minimum size (in mm)
@return true if above size, false if below*/
bool checkMinimumSizeMM( const QgsRenderContext& ct, QgsGeometry* geom, double minSize ) const;
QgsExpression* expression;
};

class CORE_EXPORT QgsLabelCandidate
@@ -55,13 +55,21 @@ class QgsExpressionItem : public QStandardItem

QString getExpressionText() { return mExpressionText; }

/** Get the help text that is associated with this expression item.
*
* @return The help text.
*/
QString getHelpText() { return mHelpText; }
/** Set the help text for the current item
*
* @note The help text can be set as a html string.
*/
void setHelpText(QString helpText) { mHelpText = helpText; }

/** Get the type of expression item eg header, field, ExpressionNode.
*
* @return The QgsExpressionItem::ItemType
*/
QgsExpressionItem::ItemType getItemType() { return mType ; }

private:

0 comments on commit 107247e

Please sign in to comment.
You can’t perform that action at this time.