Skip to content
Permalink
Browse files

Fix offset for ellipse renderer

  • Loading branch information
mhugent committed Apr 19, 2013
1 parent 4885c7b commit bc62fb037cf6f67c184b8931a1dd15e79d460b40
@@ -32,6 +32,7 @@ QgsEllipseSymbolLayerV2::QgsEllipseSymbolLayerV2(): mSymbolName( "circle" ), mSy
mPen.setJoinStyle( Qt::MiterJoin );
mBrush.setColor( mFillColor );
mBrush.setStyle( Qt::SolidPattern );
mOffset = QPointF( 0, 0 );

mAngle = 0;
}
@@ -83,6 +84,14 @@ QgsSymbolLayerV2* QgsEllipseSymbolLayerV2::create( const QgsStringMap& propertie
{
layer->setOutlineColor( QgsSymbolLayerV2Utils::decodeColor( properties["outline_color"] ) );
}
if ( properties.contains( "offset" ) )
{
layer->setOffset( QgsSymbolLayerV2Utils::decodePoint( properties["offset"] ) );
}
if ( properties.contains( "offset_unit" ) )
{
layer->setOffsetUnit( QgsSymbolLayerV2Utils::decodeOutputUnit( properties["offset_unit"] ) );
}

//data defined properties
if ( properties.contains( "width_expression" ) )
@@ -113,6 +122,10 @@ QgsSymbolLayerV2* QgsEllipseSymbolLayerV2::create( const QgsStringMap& propertie
{
layer->setDataDefinedProperty( "symbol_name", properties["symbol_name_expression"] );
}
if ( properties.contains( "offset_expression" ) )
{
layer->setDataDefinedProperty( "offset", properties["offset_expression"] );
}

//compatibility with old project file format
if ( !properties["width_field"].isEmpty() )
@@ -183,6 +196,12 @@ void QgsEllipseSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2Rend
preparePath( symbolName, context, context.feature() );
}

//offset
double offsetX = 0;
double offsetY = 0;
markerOffset( context, offsetX, offsetY );
QPointF off( offsetX, offsetY );

QPainter* p = context.renderContext().painter();
if ( !p )
{
@@ -199,9 +218,11 @@ void QgsEllipseSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2Rend
{
rotation = mAngle;
}
if ( rotation )
off = _rotatedOffset( off, rotation );

QMatrix transform;
transform.translate( point.x(), point.y() );
transform.translate( point.x() + off.x(), point.y() + off.y() );
if ( !qgsDoubleNear( rotation, 0.0 ) )
{
transform.rotate( rotation );
@@ -362,6 +383,8 @@ QgsStringMap QgsEllipseSymbolLayerV2::properties() const
map["outline_width_unit"] = QgsSymbolLayerV2Utils::encodeOutputUnit( mOutlineWidthUnit );
map["fill_color"] = QgsSymbolLayerV2Utils::encodeColor( mFillColor );
map["outline_color"] = QgsSymbolLayerV2Utils::encodeColor( mOutlineColor );
map["offset"] = QgsSymbolLayerV2Utils::encodePoint( mOffset );
map["offset_unit"] = QgsSymbolLayerV2Utils::encodeOutputUnit( mOffsetUnit );
saveDataDefinedProperties( map );
return map;
}
@@ -370,7 +393,7 @@ bool QgsEllipseSymbolLayerV2::hasDataDefinedProperty() const
{
return ( dataDefinedProperty( "width" ) || dataDefinedProperty( "height" ) || dataDefinedProperty( "rotation" )
|| dataDefinedProperty( "outline_width" ) || dataDefinedProperty( "fill_color" ) || dataDefinedProperty( "outline_color" )
|| dataDefinedProperty( "symbol_name" ) );
|| dataDefinedProperty( "symbol_name" ) || dataDefinedProperty( "offset" ) );
}

void QgsEllipseSymbolLayerV2::preparePath( const QString& symbolName, QgsSymbolV2RenderContext& context, const QgsFeature* f )
@@ -30,21 +30,6 @@

#include <cmath>

// MSVC compiler doesn't have defined M_PI in math.h
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#define DEG2RAD(x) ((x)*M_PI/180)


static QPointF _rotatedOffset( const QPointF& offset, double angle )
{
angle = DEG2RAD( angle );
double c = cos( angle ), s = sin( angle );
return QPointF( offset.x() * c - offset.y() * s, offset.x() * s + offset.y() * c );
}

//////

QgsSimpleMarkerSymbolLayerV2::QgsSimpleMarkerSymbolLayerV2( QString name, QColor color, QColor borderColor, double size, double angle, QgsSymbolV2::ScaleMethod scaleMethod )
@@ -426,23 +411,6 @@ bool QgsSimpleMarkerSymbolLayerV2::preparePath( QString name )
return false;
}

void QgsSimpleMarkerSymbolLayerV2::markerOffset( QgsSymbolV2RenderContext& context, double& offsetX, double& offsetY )
{
offsetX = mOffset.x();
offsetY = mOffset.y();

QgsExpression* offsetExpression = expression( "offset" );
if ( offsetExpression )
{
QPointF offset = QgsSymbolLayerV2Utils::decodePoint( offsetExpression->evaluate( const_cast<QgsFeature*>( context.feature() ) ).toString() );
offsetX = offset.x();
offsetY = offset.y();
}

offsetX *= QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mOffsetUnit );
offsetY *= QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mOffsetUnit );
}

void QgsSimpleMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context )
{
QgsRenderContext& rc = context.renderContext();
@@ -99,9 +99,6 @@ class CORE_EXPORT QgsSimpleMarkerSymbolLayerV2 : public QgsMarkerSymbolLayerV2
QBrush mSelBrush;
QImage mSelCache;
bool mUsingCache;

private:
void markerOffset( QgsSymbolV2RenderContext& context, double& offsetX, double& offsetY );
};

//////////
@@ -175,6 +175,30 @@ void QgsMarkerSymbolLayerV2::setOutputUnit( QgsSymbolV2::OutputUnit unit )
mOffsetUnit = unit;
}

void QgsMarkerSymbolLayerV2::markerOffset( QgsSymbolV2RenderContext& context, double& offsetX, double& offsetY )
{
offsetX = mOffset.x();
offsetY = mOffset.y();

QgsExpression* offsetExpression = expression( "offset" );
if ( offsetExpression )
{
QPointF offset = QgsSymbolLayerV2Utils::decodePoint( offsetExpression->evaluate( const_cast<QgsFeature*>( context.feature() ) ).toString() );
offsetX = offset.x();
offsetY = offset.y();
}

offsetX *= QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mOffsetUnit );
offsetY *= QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mOffsetUnit );
}

QPointF QgsMarkerSymbolLayerV2::_rotatedOffset( const QPointF& offset, double angle )
{
angle = DEG2RAD( angle );
double c = cos( angle ), s = sin( angle );
return QPointF( offset.x() * c - offset.y() * s, offset.x() * s + offset.y() * c );
}

QgsSymbolV2::OutputUnit QgsMarkerSymbolLayerV2::outputUnit() const
{
QgsSymbolV2::OutputUnit unit = mSizeUnit;
@@ -15,7 +15,12 @@
#ifndef QGSSYMBOLLAYERV2_H
#define QGSSYMBOLLAYERV2_H

// MSVC compiler doesn't have defined M_PI in math.h
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#define DEG2RAD(x) ((x)*M_PI/180)

#include <QColor>
#include <QMap>
@@ -147,6 +152,8 @@ class CORE_EXPORT QgsMarkerSymbolLayerV2 : public QgsSymbolLayerV2

protected:
QgsMarkerSymbolLayerV2( bool locked = false );
void markerOffset( QgsSymbolV2RenderContext& context, double& offsetX, double& offsetY );
static QPointF _rotatedOffset( const QPointF& offset, double angle );

double mAngle;
double mSize;
@@ -42,8 +42,8 @@ QgsEllipseSymbolLayerV2Widget::QgsEllipseSymbolLayerV2Widget( const QgsVectorLay
delete lyr;
}

blockComboSignals( true );
blockComboSignals( false );
connect( spinOffsetX, SIGNAL( valueChanged( double ) ), this, SLOT( setOffset() ) );
connect( spinOffsetY, SIGNAL( valueChanged( double ) ), this, SLOT( setOffset() ) );
}

void QgsEllipseSymbolLayerV2Widget::setSymbolLayer( QgsSymbolLayerV2* layer )
@@ -78,6 +78,10 @@ void QgsEllipseSymbolLayerV2Widget::setSymbolLayer( QgsSymbolLayerV2* layer )
mOutlineWidthUnitComboBox->setCurrentIndex( mLayer->outlineWidthUnit() );
mSymbolHeightUnitComboBox->setCurrentIndex( mLayer->symbolHeightUnit() );
}

QPointF offsetPt = mLayer->offset();
spinOffsetX->setValue( offsetPt.x() );
spinOffsetY->setValue( offsetPt.y() );
blockComboSignals( false );
}

@@ -162,6 +166,7 @@ void QgsEllipseSymbolLayerV2Widget::on_mSymbolWidthUnitComboBox_currentIndexChan
if ( mLayer )
{
mLayer->setSymbolWidthUnit(( QgsSymbolV2::OutputUnit ) index );
emit changed();
}
}

@@ -170,6 +175,7 @@ void QgsEllipseSymbolLayerV2Widget::on_mOutlineWidthUnitComboBox_currentIndexCha
if ( mLayer )
{
mLayer->setOutlineWidthUnit(( QgsSymbolV2::OutputUnit ) index );
emit changed();
}
}

@@ -178,6 +184,16 @@ void QgsEllipseSymbolLayerV2Widget::on_mSymbolHeightUnitComboBox_currentIndexCha
if ( mLayer )
{
mLayer->setSymbolHeightUnit(( QgsSymbolV2::OutputUnit ) index );
emit changed();
}
}

void QgsEllipseSymbolLayerV2Widget::on_mOffsetUnitComboBox_currentIndexChanged( int index )
{
if ( mLayer )
{
mLayer->setOffsetUnit(( QgsSymbolV2::OutputUnit ) index );
emit changed();
}
}

@@ -203,6 +219,7 @@ void QgsEllipseSymbolLayerV2Widget::on_mDataDefinedPropertiesButton_clicked()
dataDefinedProperties.insert( "fill_color", qMakePair( tr( "Fill color" ), mLayer->dataDefinedPropertyString( "fill_color" ) ) );
dataDefinedProperties.insert( "outline_color", qMakePair( tr( "Border color" ), mLayer->dataDefinedPropertyString( "outline_color" ) ) );
dataDefinedProperties.insert( "symbol_name", qMakePair( tr( "Symbol name" ), mLayer->dataDefinedPropertyString( "symbol_name" ) ) );
dataDefinedProperties.insert( "offset", qMakePair( tr( "Offset" ), mLayer->dataDefinedPropertyString( "offset" ) ) );

QgsDataDefinedSymbolDialog d( dataDefinedProperties, mVectorLayer );
if ( d.exec() == QDialog::Accepted )
@@ -222,3 +239,11 @@ void QgsEllipseSymbolLayerV2Widget::on_mDataDefinedPropertiesButton_clicked()
emit changed();
}
}

void QgsEllipseSymbolLayerV2Widget::setOffset()
{
mLayer->setOffset( QPointF( spinOffsetX->value(), spinOffsetY->value() ) );
emit changed();
}


@@ -51,8 +51,10 @@ class GUI_EXPORT QgsEllipseSymbolLayerV2Widget: public QgsSymbolLayerV2Widget, p
void on_mSymbolWidthUnitComboBox_currentIndexChanged( int index );
void on_mOutlineWidthUnitComboBox_currentIndexChanged( int index );
void on_mSymbolHeightUnitComboBox_currentIndexChanged( int index );
void on_mOffsetUnitComboBox_currentIndexChanged( int index );

void on_mDataDefinedPropertiesButton_clicked();
void setOffset();
};

#endif // QGSELLIPSESYMBOLLAYERV2WIDGET_H
@@ -255,7 +255,7 @@
</item>
</layout>
</item>
<item row="6" column="0" colspan="2">
<item row="7" column="0" colspan="2">
<widget class="QPushButton" name="mDataDefinedPropertiesButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
@@ -268,6 +268,63 @@
</property>
</widget>
</item>
<item row="6" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QDoubleSpinBox" name="spinOffsetX">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="decimals">
<number>5</number>
</property>
<property name="minimum">
<double>-100.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="spinOffsetY">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="decimals">
<number>5</number>
</property>
<property name="minimum">
<double>-100.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="mOffsetUnitComboBox">
<item>
<property name="text">
<string>Millimeter</string>
</property>
</item>
<item>
<property name="text">
<string>Map unit</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Offset X,Y</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">

0 comments on commit bc62fb0

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