Skip to content
Permalink
Browse files

refactor to a more general convertFromRenderer function

  • Loading branch information
leyan committed Sep 9, 2014
1 parent 11d159c commit 9931dde90aedd23d23298335d3fb9e4b175837fe
Showing with 368 additions and 331 deletions.
  1. +4 −3 python/core/symbology-ng/qgscategorizedsymbolrendererv2.sip
  2. +4 −5 python/core/symbology-ng/qgsgraduatedsymbolrendererv2.sip
  3. +7 −1 python/core/symbology-ng/qgsinvertedpolygonrenderer.sip
  4. +6 −1 python/core/symbology-ng/qgspointdisplacementrenderer.sip
  5. +1 −5 python/core/symbology-ng/qgsrendererv2.sip
  6. +7 −3 python/core/symbology-ng/qgsrulebasedrendererv2.sip
  7. +4 −3 python/core/symbology-ng/qgssinglesymbolrendererv2.sip
  8. +17 −88 src/core/symbology-ng/qgscategorizedsymbolrendererv2.cpp
  9. +5 −6 src/core/symbology-ng/qgscategorizedsymbolrendererv2.h
  10. +17 −80 src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp
  11. +7 −7 src/core/symbology-ng/qgsgraduatedsymbolrendererv2.h
  12. +18 −1 src/core/symbology-ng/qgsinvertedpolygonrenderer.cpp
  13. +7 −1 src/core/symbology-ng/qgsinvertedpolygonrenderer.h
  14. +19 −1 src/core/symbology-ng/qgspointdisplacementrenderer.cpp
  15. +7 −2 src/core/symbology-ng/qgspointdisplacementrenderer.h
  16. +1 −6 src/core/symbology-ng/qgsrendererv2.cpp
  17. +1 −6 src/core/symbology-ng/qgsrendererv2.h
  18. +2 −1 src/core/symbology-ng/qgsrendererv2registry.cpp
  19. +174 −6 src/core/symbology-ng/qgsrulebasedrendererv2.cpp
  20. +7 −3 src/core/symbology-ng/qgsrulebasedrendererv2.h
  21. +17 −53 src/core/symbology-ng/qgssinglesymbolrendererv2.cpp
  22. +4 −5 src/core/symbology-ng/qgssinglesymbolrendererv2.h
  23. +4 −7 src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp
  24. +4 −7 src/gui/symbology-ng/qgsgraduatedsymbolrendererv2widget.cpp
  25. +8 −15 src/gui/symbology-ng/qgsinvertedpolygonrendererwidget.cpp
  26. +4 −3 src/gui/symbology-ng/qgspointdisplacementrendererwidget.cpp
  27. +2 −2 src/gui/symbology-ng/qgsrendererv2propertiesdialog.cpp
  28. +4 −1 src/gui/symbology-ng/qgsrulebasedrendererv2widget.cpp
  29. +6 −9 src/gui/symbology-ng/qgssinglesymbolrendererv2widget.cpp
@@ -55,7 +55,7 @@ class QgsCategorizedSymbolRendererV2 : QgsFeatureRendererV2

virtual QString dump() const;

virtual QgsFeatureRendererV2* clone() /Factory/;
virtual QgsFeatureRendererV2* clone() const /Factory/;

virtual void toSld( QDomDocument& doc, QDomElement &element ) const;

@@ -147,9 +147,10 @@ class QgsCategorizedSymbolRendererV2 : QgsFeatureRendererV2
// @note added in 2.5
virtual void checkLegendSymbolItem( QString key, bool state = true );

//! convert the renderer to a rule based renderer with equivalent rules
//! creates a QgsCategorizedSymbolRendererV2 from an existing renderer.
//! @note added in 2.5
virtual QgsRuleBasedRendererV2* convertToRuleBasedRenderer() /Factory/;
//! @returns a new renderer if the conversion was possible, otherwise 0.
static QgsCategorizedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer ) /Factory/;

protected:
void rebuildHash();
@@ -59,7 +59,7 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2

virtual QString dump() const;

virtual QgsFeatureRendererV2* clone() /Factory/;
virtual QgsFeatureRendererV2* clone() const /Factory/;

virtual void toSld( QDomDocument& doc, QDomElement &element ) const;

@@ -173,11 +173,10 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2
// @note added in 2.5
virtual void checkLegendSymbolItem( QString key, bool state = true );

//! convert the renderer to a rule based renderer with equivalent rules
//! creates a QgsGraduatedSymbolRendererV2 from an existing renderer.
//! @note added in 2.5
virtual QgsRuleBasedRendererV2* convertToRuleBasedRenderer() /Factory/;


//! @returns a new renderer if the conversion was possible, otherwise 0.
static QgsGraduatedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer ) /Factory/;

protected:
QgsSymbolV2* symbolForValue( double value );
@@ -12,7 +12,7 @@ class QgsInvertedPolygonRenderer : QgsFeatureRendererV2
virtual ~QgsInvertedPolygonRenderer();

/** Used to clone this feature renderer.*/
virtual QgsFeatureRendererV2* clone() /Factory/;
virtual QgsFeatureRendererV2* clone() const /Factory/;

virtual void startRender( QgsRenderContext& context, const QgsFields& fields );

@@ -81,4 +81,10 @@ class QgsInvertedPolygonRenderer : QgsFeatureRendererV2
This will involve some CPU-demanding computations and is thus disabled by default.
*/
void setPreprocessingEnabled( bool enabled );

/** creates a QgsInvertedPolygonRenderer by a conversion from an existing renderer.
@note added in 2.5
@returns a new renderer if the conversion was possible, otherwise 0.
*/
static QgsInvertedPolygonRenderer* convertFromRenderer( const QgsFeatureRendererV2* renderer ) /Factory/;
};
@@ -7,7 +7,7 @@ class QgsPointDisplacementRenderer : QgsFeatureRendererV2
QgsPointDisplacementRenderer( const QString& labelAttributeName = "" );
~QgsPointDisplacementRenderer();

QgsFeatureRendererV2* clone() /Factory/;
QgsFeatureRendererV2* clone() const /Factory/;

virtual void toSld( QDomDocument& doc, QDomElement &element ) const;

@@ -69,6 +69,11 @@ class QgsPointDisplacementRenderer : QgsFeatureRendererV2
void setTolerance( double t );
double tolerance() const;

//! creates a QgsPointDisplacementRenderer from an existing renderer.
//! @note added in 2.5
//! @returns a new renderer if the conversion was possible, otherwise 0.
static QgsPointDisplacementRenderer* convertFromRenderer(const QgsFeatureRendererV2 *renderer ) /Factory/;

private:
QgsPointDisplacementRenderer( const QgsPointDisplacementRenderer & );
QgsPointDisplacementRenderer & operator=( const QgsPointDisplacementRenderer & );
@@ -71,7 +71,7 @@ class QgsFeatureRendererV2

virtual ~QgsFeatureRendererV2();

virtual QgsFeatureRendererV2* clone() = 0 /Factory/;
virtual QgsFeatureRendererV2* clone() const = 0 /Factory/;

virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false );

@@ -175,10 +175,6 @@ class QgsFeatureRendererV2
//! @note added in 1.9
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );

//! convert the renderer to a rule based renderer with equivalent rules, if possible
//! @note added in 2.5
virtual QgsRuleBasedRendererV2* convertToRuleBasedRenderer() /Factory/;

protected:
QgsFeatureRendererV2( QString type );

@@ -164,7 +164,7 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2

virtual QList<QString> usedAttributes();

virtual QgsFeatureRendererV2* clone() /Factory/;
virtual QgsFeatureRendererV2* clone() const /Factory/;

virtual void toSld( QDomDocument& doc, QDomElement &element ) const;

@@ -231,9 +231,13 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2
//! take a rule and create a list of new rules with intervals of scales given by the passed scale denominators
static void refineRuleScales( QgsRuleBasedRendererV2::Rule* initialRule, QList<int> scales );

//! convert the renderer to a rule based renderer with equivalent rules, if possible
//! creates a QgsRuleBasedRendererV2 from an existing renderer.
//! @note added in 2.5
virtual QgsRuleBasedRendererV2* convertToRuleBasedRenderer() /Factory/;
//! @returns a new renderer if the conversion was possible, otherwise 0.
static QgsRuleBasedRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer ) /Factory/;

//! helper function to convert the size scale and rotation fields present in some other renderers to data defined symbology
static void convertToDataDefinedSymbology( QgsSymbolV2* symbol, QString sizeScaleField, QString rotationField );

private:
QgsRuleBasedRendererV2( const QgsRuleBasedRendererV2 & );
@@ -37,7 +37,7 @@ class QgsSingleSymbolRendererV2 : QgsFeatureRendererV2

virtual QString dump() const;

virtual QgsFeatureRendererV2* clone() /Factory/;
virtual QgsFeatureRendererV2* clone() const /Factory/;

virtual void toSld( QDomDocument& doc, QDomElement &element ) const;
static QgsFeatureRendererV2* createFromSld( QDomElement& element, QGis::GeometryType geomType );
@@ -66,9 +66,10 @@ class QgsSingleSymbolRendererV2 : QgsFeatureRendererV2
//! @note added in 2.6
virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const;

//! convert the renderer to a rule based renderer with equivalent rules
//! creates a QgsSingleSymbolRendererV2 from an existing renderer.
//! @note added in 2.5
virtual QgsRuleBasedRendererV2* convertToRuleBasedRenderer() /Factory/;
//! @returns a new renderer if the conversion was possible, otherwise 0.
static QgsSingleSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer ) /Factory/;

private:
QgsSingleSymbolRendererV2( const QgsSingleSymbolRendererV2 & );
@@ -19,7 +19,8 @@
#include "qgssymbolv2.h"
#include "qgssymbollayerv2utils.h"
#include "qgsvectorcolorrampv2.h"
#include "qgsrulebasedrendererv2.h"
#include "qgspointdisplacementrenderer.h"
#include "qgsinvertedpolygonrenderer.h"

#include "qgsfeature.h"
#include "qgsvectorlayer.h"
@@ -463,7 +464,7 @@ QString QgsCategorizedSymbolRendererV2::dump() const
return s;
}

QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2::clone()
QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2::clone() const
{
QgsCategorizedSymbolRendererV2* r = new QgsCategorizedSymbolRendererV2( mAttrName, mCategories );
if ( mSourceSymbol.data() )
@@ -760,93 +761,21 @@ void QgsCategorizedSymbolRendererV2::checkLegendSymbolItem( QString key, bool st

QgsMarkerSymbolV2 QgsCategorizedSymbolRendererV2::sSkipRender;

QgsRuleBasedRendererV2* QgsCategorizedSymbolRendererV2::convertToRuleBasedRenderer()
QgsCategorizedSymbolRendererV2* QgsCategorizedSymbolRendererV2::convertFromRenderer( const QgsFeatureRendererV2 *renderer )
{
QgsRuleBasedRendererV2::Rule* rootrule = new QgsRuleBasedRendererV2::Rule( NULL );

QString expression;
QString sizeExpression;
QString value;
for ( int i = 0; i < mCategories.size();++i )
if ( renderer->type() == "categorizedSymbol" )
{
QgsRuleBasedRendererV2::Rule* rule = new QgsRuleBasedRendererV2::Rule( NULL );

rule->setLabel( mCategories[i].label() );

//We first define the rule corresponding to the category
//If the value is a number, we can use it directly, otherwise we need to quote it in the rule
if ( QVariant( mCategories[i].value() ).convert( QVariant::Double ) )
{
value = mCategories[i].value().toString();
}
else
{
value = "'" + mCategories[i].value().toString() + "'";
}

//An empty category is equivalent to the ELSE keyword
if ( value == "''" )
expression = "ELSE";
else
expression = classAttribute() + " = " + value;
rule->setFilterExpression( expression );

//Then we construct an equivalent symbol.
//Ideally we could simply copy the symbol, but the categorized renderer allows a separate interface to specify
//data dependent area and rotation, so we need to convert these to obtain the same rendering

QgsSymbolV2* origSymbol = mCategories[i].symbol()->clone();

switch ( origSymbol->type() )
{
case QgsSymbolV2::Marker:
for ( int j = 0; j < origSymbol->symbolLayerCount();++j )
{
QgsMarkerSymbolLayerV2* msl = static_cast<QgsMarkerSymbolLayerV2*>( origSymbol->symbolLayer( j ) );
if ( mSizeScale.data() )
{
sizeExpression = QString( "%1*(%2)" ).arg( msl->size() ).arg( sizeScaleField() );
msl->setDataDefinedProperty( "size", sizeExpression );
}
if ( mRotation.data() )
{
msl->setDataDefinedProperty( "angle", rotationField() );
}
}
break;
case QgsSymbolV2::Line:
if ( mSizeScale.data() )
{
for ( int j = 0; j < origSymbol->symbolLayerCount();++j )
{
if ( origSymbol->symbolLayer( j )->layerType() == "SimpleLine" )
{
QgsLineSymbolLayerV2* lsl = static_cast<QgsLineSymbolLayerV2*>( origSymbol->symbolLayer( j ) );
sizeExpression = QString( "%1*(%2)" ).arg( lsl->width() ).arg( sizeScaleField() );
lsl->setDataDefinedProperty( "width", sizeExpression );
}
if ( origSymbol->symbolLayer( j )->layerType() == "MarkerLine" )
{
QgsSymbolV2* marker = origSymbol->symbolLayer( j )->subSymbol();
for ( int k = 0; k < marker->symbolLayerCount();++k )
{
QgsMarkerSymbolLayerV2* msl = static_cast<QgsMarkerSymbolLayerV2*>( marker->symbolLayer( k ) );
sizeExpression = QString( "%1*(%2)" ).arg( msl->size() ).arg( sizeScaleField() );
msl->setDataDefinedProperty( "size", sizeExpression );
}
}
}
}
break;
default:
break;
}

rule->setSymbol( origSymbol );

rootrule->appendChild( rule );
return dynamic_cast<QgsCategorizedSymbolRendererV2*>( renderer->clone() );
}

return new QgsRuleBasedRendererV2( rootrule );

if ( renderer->type() == "pointDisplacement" )
{
const QgsPointDisplacementRenderer* pointDisplacementRenderer = dynamic_cast<const QgsPointDisplacementRenderer*>( renderer );
return convertFromRenderer( pointDisplacementRenderer->embeddedRenderer() );
}
if ( renderer->type() == "invertedPolygonRenderer" )
{
const QgsInvertedPolygonRenderer* invertedPolygonRenderer = dynamic_cast<const QgsInvertedPolygonRenderer*>( renderer );
return convertFromRenderer( invertedPolygonRenderer->embeddedRenderer() );
}
return 0;
}
@@ -24,7 +24,6 @@

class QgsVectorColorRampV2;
class QgsVectorLayer;
class QgsRuleBasedRendererV2;

/* \brief categorized renderer */
class CORE_EXPORT QgsRendererCategoryV2
@@ -86,7 +85,7 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2

virtual QString dump() const;

virtual QgsFeatureRendererV2* clone();
virtual QgsFeatureRendererV2* clone() const;

virtual void toSld( QDomDocument& doc, QDomElement &element ) const;

@@ -166,9 +165,8 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
//! @note added in 2.0
QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }


//! items of symbology items in legend should be checkable
// @note added in 2.5
//! @note added in 2.5
virtual bool legendSymbolItemsCheckable() const;

//! item in symbology was checked
@@ -183,9 +181,10 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
//! @note added in 2.6
virtual QString legendClassificationAttribute() const { return classAttribute(); }

//! convert the renderer to a rule based renderer with equivalent rules
//! creates a QgsCategorizedSymbolRendererV2 from an existing renderer.
//! @note added in 2.5
virtual QgsRuleBasedRendererV2* convertToRuleBasedRenderer();
//! @returns a new renderer if the conversion was possible, otherwise 0.
static QgsCategorizedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer );

protected:
QString mAttrName;

0 comments on commit 9931dde

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