Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Cleanup ellipse symbol layer to store only field names, not indices
  • Loading branch information
mhugent committed Nov 1, 2011
1 parent 1177de1 commit cc91d14
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 142 deletions.
179 changes: 72 additions & 107 deletions src/core/symbology-ng/qgsellipsesymbollayerv2.cpp
@@ -1,6 +1,7 @@
#include "qgsellipsesymbollayerv2.h"
#include "qgsfeature.h"
#include "qgsrendercontext.h"
#include "qgsvectorlayer.h"
#include <QPainter>
#include <QSet>

Expand All @@ -14,13 +15,13 @@ QgsEllipseSymbolLayerV2::QgsEllipseSymbolLayerV2(): mSymbolName( "circle" ), mSy
mBrush.setStyle( Qt::SolidPattern );

mAngle = 0;
mWidthField.first = -1;
mHeightField.first = -1;
mRotationField.first = -1;
mOutlineWidthField.first = -1;
mFillColorField.first = -1;
mOutlineColorField.first = -1;
mSymbolNameField.first = -1;
mWidthIndex = -1;
mHeightIndex = -1;
mRotationIndex = -1;
mOutlineWidthIndex = -1;
mFillColorIndex = -1;
mOutlineColorIndex = -1;
mSymbolNameIndex = -1;
}

QgsEllipseSymbolLayerV2::~QgsEllipseSymbolLayerV2()
Expand Down Expand Up @@ -60,33 +61,33 @@ QgsSymbolLayerV2* QgsEllipseSymbolLayerV2::create( const QgsStringMap& propertie
}

//data defined properties
if ( properties.contains( "height_index" ) && properties.contains( "height_field" ) )
if ( properties.contains( "height_field" ) )
{
layer->setHeightField( properties["height_index"].toInt(), properties["height_field"] );
layer->setHeightField( properties["height_field"] );
}
if ( properties.contains( "width_index" ) && properties.contains( "width_field" ) )
if ( properties.contains( "width_field" ) )
{
layer->setWidthField( properties["width_index"].toInt(), properties["width_field"] );
layer->setWidthField( properties["width_field"] );
}
if ( properties.contains( "rotation_index" ) && properties.contains( "rotation_field" ) )
if ( properties.contains( "rotation_field" ) )
{
layer->setRotationField( properties["rotation_index"].toInt(), properties["rotation_field"] );
layer->setRotationField( properties["rotation_field"] );
}
if ( properties.contains( "outline_width_index" ) && properties.contains( "outline_width_field" ) )
if ( properties.contains( "outline_width_field" ) )
{
layer->setOutlineWidthField( properties["outline_width_index"].toInt(), properties["outline_width_field"] );
layer->setOutlineWidthField( properties["outline_width_field"] );
}
if ( properties.contains( "fill_color_index" ) && properties.contains( "fill_color_field" ) )
if ( properties.contains( "fill_color_field" ) )
{
layer->setFillColorField( properties["fill_color_index"].toInt(), properties["fill_color_field"] );
layer->setFillColorField( properties["fill_color_field"] );
}
if ( properties.contains( "outline_color_index" ) && properties.contains( "outline_color_field" ) )
if ( properties.contains( "outline_color_field" ) )
{
layer->setOutlineColorField( properties["outline_color_index"].toInt(), properties["outline_color_field"] );
layer->setOutlineColorField( properties["outline_color_field"] );
}
if ( properties.contains( "symbol_name_index" ) && properties.contains( "symbol_name_field" ) )
if ( properties.contains( "symbol_name_field" ) )
{
layer->setSymbolNameField( properties["symbol_name_index"].toInt(), properties["symbol_name_field"] );
layer->setSymbolNameField( properties["symbol_name_field"] );
}

return layer;
Expand All @@ -98,23 +99,23 @@ void QgsEllipseSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2Rend

if ( f )
{
if ( mOutlineWidthField.first != -1 )
if ( mOutlineWidthIndex != -1 )
{
double width = context.outputLineWidth( f->attributeMap()[mOutlineWidthField.first].toDouble() );
double width = context.outputLineWidth( f->attributeMap()[mOutlineWidthIndex].toDouble() );
mPen.setWidthF( width );
}
if ( mFillColorField.first != -1 )
if ( mFillColorIndex != -1 )
{
mBrush.setColor( QColor( f->attributeMap()[mFillColorField.first].toString() ) );
mBrush.setColor( QColor( f->attributeMap()[mFillColorIndex].toString() ) );
}
if ( mOutlineColorField.first != -1 )
if ( mOutlineColorIndex != -1 )
{
mPen.setColor( QColor( f->attributeMap()[mOutlineColorField.first].toString() ) );
mPen.setColor( QColor( f->attributeMap()[mOutlineColorIndex].toString() ) );
}

if ( mWidthField.first != -1 || mHeightField.first != -1 || mSymbolNameField.first != -1 )
if ( mWidthIndex != -1 || mHeightIndex != -1 || mSymbolNameIndex != -1 )
{
QString symbolName = ( mSymbolNameField.first == -1 ) ? mSymbolName : f->attributeMap()[mSymbolNameField.first].toString();
QString symbolName = ( mSymbolNameIndex == -1 ) ? mSymbolName : f->attributeMap()[mSymbolNameIndex].toString();
preparePath( symbolName, context, f );
}
}
Expand All @@ -127,9 +128,9 @@ void QgsEllipseSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2Rend

//priority for rotation: 1. data defined symbol level, 2. symbol layer rotation (mAngle)
double rotation = 0.0;
if ( f && mRotationField.first != -1 )
if ( f && mRotationIndex != -1 )
{
rotation = f->attributeMap()[mRotationField.first].toDouble();
rotation = f->attributeMap()[mRotationIndex].toDouble();
}
else if ( !doubleNear( mAngle, 0.0 ) )
{
Expand Down Expand Up @@ -162,6 +163,19 @@ void QgsEllipseSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
mPen.setColor( mOutlineColor );
mPen.setWidthF( context.outputLineWidth( mOutlineWidth ) );
mBrush.setColor( mFillColor );

//resolve data defined attribute indices
const QgsVectorLayer* vlayer = context.layer();
if ( vlayer )
{
mWidthIndex = vlayer->fieldNameIndex( mWidthField );
mHeightIndex = vlayer->fieldNameIndex( mHeightField );
mRotationIndex = vlayer->fieldNameIndex( mRotationField );
mOutlineWidthIndex = vlayer->fieldNameIndex( mOutlineWidthField );
mFillColorIndex = vlayer->fieldNameIndex( mFillColorField );
mOutlineColorIndex = vlayer->fieldNameIndex( mOutlineColorField );
mSymbolNameIndex = vlayer->fieldNameIndex( mSymbolNameField );
}
}

void QgsEllipseSymbolLayerV2::stopRender( QgsSymbolV2RenderContext & )
Expand All @@ -178,32 +192,25 @@ QgsStringMap QgsEllipseSymbolLayerV2::properties() const
QgsStringMap map;
map["symbol_name"] = mSymbolName;
map["symbol_width"] = QString::number( mSymbolWidth );
map["width_index"] = QString::number( mWidthField.first );
map["width_field"] = mWidthField.second;
map["width_field"] = mWidthField;
map["symbol_height"] = QString::number( mSymbolHeight );
map["height_index"] = QString::number( mHeightField.first );
map["height_field"] = mHeightField.second;
map["height_field"] = mHeightField;
map["angle"] = QString::number( mAngle );
map["rotation_index"] = QString::number( mRotationField.first );
map["rotation_field"] = mRotationField.second;
map["rotation_field"] = mRotationField;
map["outline_width"] = QString::number( mOutlineWidth );
map["outline_width_index"] = QString::number( mOutlineWidthField.first );
map["outline_width_field"] = mOutlineWidthField.second;
map["outline_width_field"] = mOutlineWidthField;
map["fill_color"] = QgsSymbolLayerV2Utils::encodeColor( mFillColor );
map["fill_color_index"] = QString::number( mFillColorField.first );
map["fill_color_field"] = mFillColorField.second;
map["fill_color_field"] = mFillColorField;
map["outline_color"] = QgsSymbolLayerV2Utils::encodeColor( mOutlineColor );
map["outline_color_index"] = QString::number( mOutlineColorField.first );
map["outline_color_field"] = mOutlineColorField.second;
map["symbol_name_index"] = QString::number( mSymbolNameField.first );
map["symbol_name_field"] = mSymbolNameField.second;
map["outline_color_field"] = mOutlineColorField;
map["symbol_name_field"] = mSymbolNameField;
return map;
}

bool QgsEllipseSymbolLayerV2::hasDataDefinedProperty() const
{
return ( mWidthField.first != -1 || mHeightField.first != -1 || mOutlineWidthField.first != -1
|| mFillColorField.first != -1 || mOutlineColorField.first != -1 );
return ( mWidthIndex != -1 || mHeightIndex != -1 || mOutlineWidthIndex != -1
|| mFillColorIndex != -1 || mOutlineColorIndex != -1 );
}

void QgsEllipseSymbolLayerV2::preparePath( const QString& symbolName, QgsSymbolV2RenderContext& context, const QgsFeature* f )
Expand All @@ -212,9 +219,9 @@ void QgsEllipseSymbolLayerV2::preparePath( const QString& symbolName, QgsSymbolV

double width = 0;

if ( f && mWidthField.first != -1 ) //1. priority: data defined setting on symbol layer level
if ( f && mWidthIndex != -1 ) //1. priority: data defined setting on symbol layer level
{
width = context.outputLineWidth( f->attributeMap()[mWidthField.first].toDouble() );
width = context.outputLineWidth( f->attributeMap()[mWidthIndex].toDouble() );
}
else if ( context.renderHints() & QgsSymbolV2::DataDefinedSizeScale ) //2. priority: is data defined size on symbol level
{
Expand All @@ -226,9 +233,9 @@ void QgsEllipseSymbolLayerV2::preparePath( const QString& symbolName, QgsSymbolV
}

double height = 0;
if ( f && mHeightField.first != -1 ) //1. priority: data defined setting on symbol layer level
if ( f && mHeightIndex != -1 ) //1. priority: data defined setting on symbol layer level
{
height = context.outputLineWidth( f->attributeMap()[mHeightField.first].toDouble() );
height = context.outputLineWidth( f->attributeMap()[mHeightIndex].toDouble() );
}
else if ( context.renderHints() & QgsSymbolV2::DataDefinedSizeScale ) //2. priority: is data defined size on symbol level
{
Expand Down Expand Up @@ -266,75 +273,33 @@ void QgsEllipseSymbolLayerV2::preparePath( const QString& symbolName, QgsSymbolV
QSet<QString> QgsEllipseSymbolLayerV2::usedAttributes() const
{
QSet<QString> dataDefinedAttributes;
if ( mWidthField.first != -1 )
if ( !mWidthField.isEmpty() )
{
dataDefinedAttributes.insert( mWidthField.second );
dataDefinedAttributes.insert( mWidthField );
}
if ( mHeightField.first != -1 )
if ( !mHeightField.isEmpty() )
{
dataDefinedAttributes.insert( mHeightField.second );
dataDefinedAttributes.insert( mHeightField );
}
if ( mRotationField.first != -1 )
if ( !mRotationField.isEmpty() )
{
dataDefinedAttributes.insert( mRotationField.second );
dataDefinedAttributes.insert( mRotationField );
}
if ( mOutlineWidthField.first != -1 )
if ( !mOutlineWidthField.isEmpty() )
{
dataDefinedAttributes.insert( mOutlineWidthField.second );
dataDefinedAttributes.insert( mOutlineWidthField );
}
if ( mFillColorField.first != -1 )
if ( !mFillColorField.isEmpty() )
{
dataDefinedAttributes.insert( mFillColorField.second );
dataDefinedAttributes.insert( mFillColorField );
}
if ( mOutlineColorField.first != -1 )
if ( !mOutlineColorField.isEmpty() )
{
dataDefinedAttributes.insert( mOutlineColorField.second );
dataDefinedAttributes.insert( mOutlineColorField );
}
if ( mSymbolNameField.first != -1 )
if ( !mSymbolNameField.isEmpty() )
{
dataDefinedAttributes.insert( mSymbolNameField.second );
dataDefinedAttributes.insert( mSymbolNameField );
}
return dataDefinedAttributes;
}

void QgsEllipseSymbolLayerV2::setSymbolNameField( int index, const QString& field )
{
mSymbolNameField.first = index;
mSymbolNameField.second = field;
}

void QgsEllipseSymbolLayerV2::setWidthField( int index, const QString& field )
{
mWidthField.first = index;
mWidthField.second = field;
}

void QgsEllipseSymbolLayerV2::setHeightField( int index, const QString& field )
{
mHeightField.first = index;
mHeightField.second = field;
}

void QgsEllipseSymbolLayerV2::setRotationField( int index, const QString& field )
{
mRotationField.first = index;
mRotationField.second = field;
}

void QgsEllipseSymbolLayerV2::setOutlineWidthField( int index, const QString& field )
{
mOutlineWidthField.first = index;
mOutlineWidthField.second = field;
}

void QgsEllipseSymbolLayerV2::setFillColorField( int index, const QString& field )
{
mFillColorField.first = index;
mFillColorField.second = field;
}

void QgsEllipseSymbolLayerV2::setOutlineColorField( int index, const QString& field )
{
mOutlineColorField.first = index;
mOutlineColorField.second = field;
}
49 changes: 35 additions & 14 deletions src/core/symbology-ng/qgsellipsesymbollayerv2.h
Expand Up @@ -23,41 +23,41 @@ class CORE_EXPORT QgsEllipseSymbolLayerV2: public QgsMarkerSymbolLayerV2
void setSymbolName( const QString& name ) { mSymbolName = name; }
QString symbolName() const { return mSymbolName; }

void setSymbolNameField( int index, const QString& field );
const QPair<int, QString>& symbolNameField() const { return mSymbolNameField; }
void setSymbolNameField( const QString& field ) { mSymbolNameField = field; }
const QString& symbolNameField() const { return mSymbolNameField; }

void setSymbolWidth( double w ) { mSymbolWidth = w; }
double symbolWidth() const { return mSymbolWidth; }

void setWidthField( int index, const QString& field );
const QPair<int, QString>& widthField() const { return mWidthField; }
void setWidthField( const QString& field ) { mWidthField = field; }
const QString& widthField() const { return mWidthField; }

void setSymbolHeight( double h ) { mSymbolHeight = h; }
double symbolHeight() const { return mSymbolHeight; }

void setHeightField( int index, const QString& field );
const QPair<int, QString>& heightField() const { return mHeightField; }
void setHeightField( const QString& field ) { mHeightField = field; }
const QString& heightField() const { return mHeightField; }

void setRotationField( int index, const QString& field );
const QPair<int, QString>& rotationField() const { return mRotationField; }
void setRotationField( const QString& field ) { mRotationField = field; }
const QString& rotationField() const { return mRotationField; }

void setOutlineWidth( double w ) { mOutlineWidth = w; }
double outlineWidth() const { return mOutlineWidth; }

void setOutlineWidthField( int index, const QString& field );
const QPair<int, QString>& outlineWidthField() const { return mOutlineWidthField; }
void setOutlineWidthField( const QString& field ) { mOutlineWidthField = field; }
const QString& outlineWidthField() const { return mOutlineWidthField; }

void setFillColor( const QColor& c ) { mFillColor = c;}
QColor fillColor() const { return mFillColor; }

void setFillColorField( int index, const QString& field );
const QPair<int, QString>& fillColorField() const { return mFillColorField; }
void setFillColorField( const QString& field ) { mFillColorField = field; }
const QString& fillColorField() const { return mFillColorField; }

void setOutlineColor( const QColor& c ) { mOutlineColor = c; }
QColor outlineColor() const { return mOutlineColor; }

void setOutlineColorField( int index, const QString& field );
const QPair<int, QString>& outlineColorField() const { return mOutlineColorField; }
void setOutlineColorField( const QString& field ) { mOutlineColorField = field; }
const QString& outlineColorField() const { return mOutlineColorField; }

QSet<QString> usedAttributes() const;

Expand All @@ -69,6 +69,7 @@ class CORE_EXPORT QgsEllipseSymbolLayerV2: public QgsMarkerSymbolLayerV2
QColor mOutlineColor;
double mOutlineWidth;

#if 0
/**Take width from attribute (-1 if fixed width)*/
QPair<int, QString> mWidthField;
/**Take height from attribute (-1 if fixed height)*/
Expand All @@ -83,6 +84,26 @@ class CORE_EXPORT QgsEllipseSymbolLayerV2: public QgsMarkerSymbolLayerV2
QPair<int, QString> mOutlineColorField;
/**Take shape name from attribute (-1 if fixed shape type)*/
QPair<int, QString> mSymbolNameField;
#endif //0

//data defined property fields
QString mWidthField;
QString mHeightField;
QString mRotationField;
QString mOutlineWidthField;
QString mFillColorField;
QString mOutlineColorField;
QString mSymbolNameField;

//field indices for data defined properties
//resolved in startRender method
int mWidthIndex;
int mHeightIndex;
int mRotationIndex;
int mOutlineWidthIndex;
int mFillColorIndex;
int mOutlineColorIndex;
int mSymbolNameIndex;

QPainterPath mPainterPath;

Expand Down

0 comments on commit cc91d14

Please sign in to comment.