Skip to content
Permalink
Browse files

Support data defined fields in ellipse renderer

  • Loading branch information
mhugent committed Jun 9, 2011
1 parent 5a5f10c commit 45fcb78908a52474074ba5bcce72593c86b3dc5d
@@ -4,15 +4,19 @@
#include <QPainter>
#include <QSet>

QgsEllipseSymbolLayerV2::QgsEllipseSymbolLayerV2(): mDataDefinedWidth(-1), mDataDefinedHeight(-1),
mDataDefinedOutlineWidth(-1), mFillColor( Qt::black ), mDataDefinedFillColor(-1), mOutlineColor( Qt::white ), mDataDefinedOutlineColor(-1)
QgsEllipseSymbolLayerV2::QgsEllipseSymbolLayerV2(): mSymbolName("circle"), mSymbolWidth(4), mSymbolHeight(3), mFillColor( Qt::black ), mOutlineColor( Qt::white )
{
mSymbolName = "circle";
mPen.setColor( mOutlineColor );
mPen.setWidth( 1.0 );
mPen.setJoinStyle( Qt::MiterJoin );
mBrush.setColor( mFillColor );
mBrush.setStyle( Qt::SolidPattern );

mWidthField.first = -1;
mHeightField.first = -1;
mOutlineWidthField.first = -1;
mFillColorField.first = -1;
mOutlineColorField.first = -1;
}

QgsEllipseSymbolLayerV2::~QgsEllipseSymbolLayerV2()
@@ -30,42 +34,45 @@ QgsSymbolLayerV2* QgsEllipseSymbolLayerV2::create( const QgsStringMap& propertie
{
layer->setSymbolWidth( properties["symbol_width"].toDouble() );
}
if( properties.contains( "data_defined_width" ) )
{
layer->setDataDefinedWidth( properties["data_defined_width"].toInt() );
}
if( properties.contains("symbol_height") )
{
layer->setSymbolHeight( properties["symbol_height"].toDouble() );
}
if( properties.contains( "data_defined_height" ) )
{
layer->setDataDefinedHeight( properties["data_defined_height"].toInt() );
}
if( properties.contains( "outline_width" ) )
{
layer->setOutlineWidth( properties["outline_width"].toDouble() );
}
if( properties.contains( "data_defined_outline_width" ) )
{
layer->setDataDefinedOutlineWidth( properties["data_defined_outline_width"].toInt() );
}
if( properties.contains( "fill_color" ) )
{
layer->setFillColor( QgsSymbolLayerV2Utils::decodeColor( properties["fill_color"] ) );
}
if( properties.contains( "data_defined_fill_color" ) )
{
layer->setDataDefinedFillColor( properties["data_defined_fill_color"].toInt() );
}
if( properties.contains( "outline_color" ) )
{
layer->setOutlineColor( QgsSymbolLayerV2Utils::decodeColor( properties["outline_color"] ) );
}
if( properties.contains( "data_defined_outline_color" ) )

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

return layer;
}

@@ -75,20 +82,21 @@ void QgsEllipseSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2Rend

if( f )
{
if( mDataDefinedOutlineWidth != -1 )
if( mOutlineWidthField.first != -1 )
{
double width = context.outputLineWidth( f->attributeMap()[mDataDefinedOutlineWidth].toDouble() );
double width = context.outputLineWidth( f->attributeMap()[mOutlineWidthField.first].toDouble() );
mPen.setWidth( width );
}
if( mDataDefinedFillColor != -1 )
if( mFillColorField.first != -1 )
{
mBrush.setColor( QColor( f->attributeMap()[mDataDefinedFillColor].toString() ) );
mBrush.setColor( QColor( f->attributeMap()[mFillColorField.first].toString() ) );
}
if( mDataDefinedOutlineColor != -1 )
if( mOutlineColorField.first != -1 )
{
mPen.setColor( QColor( f->attributeMap()[mDataDefinedOutlineColor].toString() ) );
mPen.setColor( QColor( f->attributeMap()[mOutlineColorField.first].toString() ) );
}
if( mDataDefinedWidth != -1 || mDataDefinedHeight != -1 )

if( mWidthField.first != -1 || mHeightField.first != -1 )
{
preparePath( context, f );
}
@@ -140,42 +148,47 @@ QgsStringMap QgsEllipseSymbolLayerV2::properties() const
QgsStringMap map;
map["symbol_name"] = mSymbolName;
map["symbol_width"] = QString::number( mSymbolWidth );
map["data_defined_width"] = QString::number( mDataDefinedWidth );
map["height_index"] = QString::number( mHeightField.first );
map["height_field"] = mHeightField.second;
map["symbol_height"] = QString::number( mSymbolHeight );
map["data_defined_height"] = QString::number( mDataDefinedHeight );
map["width_index"] = QString::number( mWidthField.first );
map["width_field"] = mWidthField.second;
map["outline_width"] = QString::number( mOutlineWidth );
map["data_defined_outline_width"] = QString::number( mDataDefinedOutlineWidth );
map["outline_width_index"] = QString::number( mOutlineWidthField.first );
map["outline_width_field"] = mOutlineWidthField.second;
map["fill_color"] = QgsSymbolLayerV2Utils::encodeColor( mFillColor );
map["data_defined_fill_color"] = QString::number( mDataDefinedFillColor );
map["fill_color_index"] = QString::number( mFillColorField.first );
map["fill_color_field"] = mFillColorField.second;
map["outline_color"] = QgsSymbolLayerV2Utils::encodeColor( mOutlineColor );
map["data_defined_outline_color"] = QString::number( mDataDefinedOutlineColor );
map["outline_color_index"] = QString::number( mOutlineColorField.first );
map["outline_color_field"] = mOutlineColorField.second;
return map;
}

bool QgsEllipseSymbolLayerV2::hasDataDefinedProperty() const
{
return ( mDataDefinedWidth != -1 || mDataDefinedHeight != -1 || mDataDefinedOutlineWidth != -1
|| mDataDefinedFillColor != -1 || mDataDefinedOutlineColor != -1 );
return ( mWidthField.first != -1 || mHeightField.first != -1 || mOutlineWidthField.first != -1
|| mFillColorField.first != -1 || mOutlineColorField.first != -1 );
}

void QgsEllipseSymbolLayerV2::preparePath( QgsSymbolV2RenderContext& context, const QgsFeature* f )
{
mPainterPath = QPainterPath();

double width = 0;
if( f && mDataDefinedOutlineWidth != -1 )
if( f && mWidthField.first != -1 )
{
width = context.outputLineWidth( f->attributeMap()[mDataDefinedOutlineWidth].toDouble() );
width = context.outputLineWidth( f->attributeMap()[mWidthField.first].toDouble() );
}
else
{
width = context.outputLineWidth( mSymbolWidth );
}

double height = 0;
if( f && mDataDefinedHeight != -1 )
if( f && mHeightField.first != -1 )
{
height = context.outputLineWidth( f->attributeMap()[mDataDefinedHeight].toDouble() );
height = context.outputLineWidth( f->attributeMap()[mHeightField.first].toDouble() );
}
else
{
@@ -209,11 +222,55 @@ void QgsEllipseSymbolLayerV2::preparePath( QgsSymbolV2RenderContext& context, co
QSet<QString> QgsEllipseSymbolLayerV2::usedAttributes() const
{
QSet<QString> dataDefinedAttributes;
/*dataDefinedAttributes.insert( mDataDefinedWidth );
dataDefinedAttributes.insert( mDataDefinedHeight );
dataDefinedAttributes.insert( mDataDefinedOutlineWidth );
dataDefinedAttributes.insert( mDataDefinedFillColor );
dataDefinedAttributes.insert( mDataDefinedOutlineColor );
dataDefinedAttributes.remove( -1 );*/
if( mWidthField.first != -1 )
{
dataDefinedAttributes.insert( mWidthField.second );
}
if( mHeightField.first != -1 )
{
dataDefinedAttributes.insert( mHeightField.second );
}
if( mOutlineWidthField.first != -1 )
{
dataDefinedAttributes.insert( mOutlineWidthField.second );
}
if( mFillColorField.first != -1 )
{
dataDefinedAttributes.insert( mFillColorField.second );
}
if( mOutlineColorField.first != -1 )
{
dataDefinedAttributes.insert( mOutlineColorField.second );
}
return dataDefinedAttributes;
}

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::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;
}
@@ -26,52 +26,52 @@ class QgsEllipseSymbolLayerV2: public QgsMarkerSymbolLayerV2
void setSymbolWidth( double w ){ mSymbolWidth = w; }
double symbolWidth() const { return mSymbolWidth; }

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

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

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

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

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

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

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

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

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

QSet<QString> usedAttributes() const;

private:
QString mSymbolName;
double mSymbolWidth;
/**Take width from attribute (-1 if fixed width)*/
int mDataDefinedWidth;
QPair<int, QString> mWidthField;
double mSymbolHeight;
/**Take height from attribute (-1 if fixed height)*/
int mDataDefinedHeight;
QPair<int, QString> mHeightField;
double mOutlineWidth;
/**Take outline width from attribute (-1 if fixed outline width)*/
int mDataDefinedOutlineWidth;
QPair<int, QString> mOutlineWidthField;
QColor mFillColor;
/**Take fill color from attribute (-1 if fixed fill color)*/
int mDataDefinedFillColor;
QPair<int, QString> mFillColorField;
QColor mOutlineColor;
/**Take outline color from attribute (-1 if fixed outline color)*/
int mDataDefinedOutlineColor;
QPair<int, QString> mOutlineColorField;
QPainterPath mPainterPath;

QPen mPen;
@@ -282,7 +282,7 @@ QSet<QString> QgsSymbolV2::usedAttributes() const
////////////////////

QgsSymbolV2RenderContext::QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u, qreal alpha, bool selected, int renderHints, const QgsFeature* f )
: mRenderContext( c ), mOutputUnit( u ), mAlpha( alpha ), mSelected( selected ), mRenderHints( renderHints ), mFeature( 0 )
: mRenderContext( c ), mOutputUnit( u ), mAlpha( alpha ), mSelected( selected ), mRenderHints( renderHints ), mFeature( f )
{

}
@@ -48,6 +48,33 @@ void QgsEllipseSymbolLayerV2Widget::setSymbolLayer( QgsSymbolLayerV2* layer )
{
mShapeListWidget->setCurrentItem( symbolItemList.at( 0 ) );
}

//set combo entries to current values
blockComboSignals( true );
if( mLayer )
{
if( mLayer->widthField().first != -1 )
{
mDDSymbolWidthComboBox->setCurrentIndex( mDDSymbolWidthComboBox->findText( mLayer->widthField().second ) );
}
if( mLayer->heightField().first != -1 )
{
mDDSymbolHeightComboBox->setCurrentIndex( mDDSymbolHeightComboBox->findText( mLayer->heightField().second ) );
}
if( mLayer->outlineWidthField().first != -1 )
{
mDDOutlineWidthComboBox->setCurrentIndex( mDDOutlineWidthComboBox->findText( mLayer->outlineWidthField().second ) );
}
if( mLayer->fillColorField().first != -1 )
{
mDDFillColorComboBox->setCurrentIndex( mDDFillColorComboBox->findText( mLayer->fillColorField().second ) );
}
if( mLayer->outlineColorField().first != -1 )
{
mDDOutlineColorComboBox->setCurrentIndex( mDDOutlineColorComboBox->findText( mLayer->outlineColorField().second ) );
}
}
blockComboSignals( false );
}

QgsSymbolLayerV2* QgsEllipseSymbolLayerV2Widget::symbolLayer()
@@ -159,39 +186,39 @@ void QgsEllipseSymbolLayerV2Widget::on_mDDSymbolWidthComboBox_currentIndexChange
{
if( mLayer )
{
mLayer->setDataDefinedWidth( mDDSymbolWidthComboBox->itemData( idx ).toInt() );
mLayer->setWidthField( mDDSymbolWidthComboBox->itemData( idx ).toInt(), mDDSymbolWidthComboBox->itemText( idx ) );
}
}

void QgsEllipseSymbolLayerV2Widget::on_mDDSymbolHeightComboBox_currentIndexChanged( int idx )
{
if( mLayer )
{
mLayer->setDataDefinedHeight( mDDSymbolHeightComboBox->itemData( idx ).toInt() );
mLayer->setHeightField( mDDSymbolHeightComboBox->itemData( idx ).toInt(), mDDSymbolHeightComboBox->itemText( idx ));
}
}

void QgsEllipseSymbolLayerV2Widget::on_mDDOutlineWidthComboBox_currentIndexChanged( int idx )
{
if( mLayer )
{
mLayer->setDataDefinedOutlineWidth( mDDOutlineWidthComboBox->itemData( idx ).toInt() );
mLayer->setOutlineWidthField( mDDOutlineWidthComboBox->itemData( idx ).toInt(), mDDOutlineWidthComboBox->itemText( idx ) );
}
}

void QgsEllipseSymbolLayerV2Widget::on_mDDFillColorComboBox_currentIndexChanged( int idx )
{
if( mLayer )
{
mLayer->setDataDefinedFillColor( mDDFillColorComboBox->itemData( idx ).toInt() );
mLayer->setFillColorField( mDDFillColorComboBox->itemData( idx ).toInt(), mDDFillColorComboBox->itemText( idx ) );
}
}

void QgsEllipseSymbolLayerV2Widget::on_mDDOutlineColorComboBox_currentIndexChanged( int idx )
{
if( mLayer )
{
mLayer->setDataDefinedOutlineColor( mDDOutlineColorComboBox->itemData( idx ).toInt() );
mLayer->setOutlineColorField( mDDOutlineColorComboBox->itemData( idx ).toInt(), mDDOutlineColorComboBox->itemText( idx ) );
}
}

0 comments on commit 45fcb78

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