Skip to content

Commit

Permalink
Add method to access label string as expression, QgsExpression only c…
Browse files Browse the repository at this point in the history
…reated once then cached.
  • Loading branch information
NathanW2 committed Oct 5, 2011
1 parent f64ae1c commit 107247e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/CMakeLists.txt
Expand Up @@ -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)
Expand Down
26 changes: 18 additions & 8 deletions src/core/qgspallabeling.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -195,6 +195,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
fontMetrics = NULL;
ct = NULL;
extentGeom = NULL;
expression = NULL;
}


Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 12 additions & 2 deletions src/core/qgspallabeling.h
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsexpressionbuilder.h
Expand Up @@ -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:
Expand Down

0 comments on commit 107247e

Please sign in to comment.