Skip to content

Commit a84b4a8

Browse files
committed
Implement anchor point shift and expose in simple marker dialog
1 parent bc95d0b commit a84b4a8

File tree

6 files changed

+338
-239
lines changed

6 files changed

+338
-239
lines changed

src/core/symbology-ng/qgsmarkersymbollayerv2.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ QgsSymbolLayerV2* QgsSimpleMarkerSymbolLayerV2::create( const QgsStringMap& prop
8585
m->setOutlineWidthUnit( QgsSymbolLayerV2Utils::decodeOutputUnit( props["outline_width_unit"] ) );
8686
}
8787

88+
if ( props.contains( "horizontal_anchor_point" ) )
89+
{
90+
m->setHorizontalAnchorPoint( QgsMarkerSymbolLayerV2::HorizontalAnchorPoint( props[ "horizontal_anchor_point" ].toInt() ) );
91+
}
92+
if ( props.contains( "vertical_anchor_point" ) )
93+
{
94+
m->setVerticalAnchorPoint( QgsMarkerSymbolLayerV2::VerticalAnchorPoint( props[ "vertical_anchor_point" ].toInt() ) );
95+
}
96+
8897
//data defined properties
8998
if ( props.contains( "name_expression" ) )
9099
{
@@ -546,6 +555,8 @@ QgsStringMap QgsSimpleMarkerSymbolLayerV2::properties() const
546555
map["scale_method"] = QgsSymbolLayerV2Utils::encodeScaleMethod( mScaleMethod );
547556
map["outline_width"] = QString::number( mOutlineWidth );
548557
map["outline_width_unit"] = QgsSymbolLayerV2Utils::encodeOutputUnit( mOutlineWidthUnit );
558+
map["horizontal_anchor_point"] = QString::number( mHorizontalAnchorPoint );
559+
map["vertical_anchor_point"] = QString::number( mVerticalAnchorPoint );
549560

550561
//data define properties
551562
saveDataDefinedProperties( map );
@@ -560,6 +571,8 @@ QgsSymbolLayerV2* QgsSimpleMarkerSymbolLayerV2::clone() const
560571
m->setOffsetUnit( mOffsetUnit );
561572
m->setOutlineWidth( mOutlineWidth );
562573
m->setOutlineWidthUnit( mOutlineWidthUnit );
574+
m->setHorizontalAnchorPoint( mHorizontalAnchorPoint );
575+
m->setVerticalAnchorPoint( mVerticalAnchorPoint );
563576
copyDataDefinedProperties( m );
564577
return m;
565578
}

src/core/symbology-ng/qgssymbollayerv2.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ void QgsSymbolLayerV2::copyDataDefinedProperties( QgsSymbolLayerV2* destLayer )
148148

149149

150150
QgsMarkerSymbolLayerV2::QgsMarkerSymbolLayerV2( bool locked )
151-
: QgsSymbolLayerV2( QgsSymbolV2::Marker, locked ), mSizeUnit( QgsSymbolV2::MM ), mOffsetUnit( QgsSymbolV2::MM )
151+
: QgsSymbolLayerV2( QgsSymbolV2::Marker, locked ), mSizeUnit( QgsSymbolV2::MM ), mOffsetUnit( QgsSymbolV2::MM ),
152+
mHorizontalAnchorPoint( HCenter ), mVerticalAnchorPoint( VCenter )
152153
{
153154
}
154155

@@ -191,13 +192,31 @@ void QgsMarkerSymbolLayerV2::markerOffset( QgsSymbolV2RenderContext& context, do
191192
offsetX *= QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mOffsetUnit );
192193
offsetY *= QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mOffsetUnit );
193194

194-
//consider anchor point corrections
195-
if( mHorizontalAnchorPoint == HCenter && mVerticalAnchorPoint == VCenter )
195+
//correct horizontal position according to anchor point
196+
if ( mHorizontalAnchorPoint == HCenter && mVerticalAnchorPoint == VCenter )
196197
{
197-
return;
198+
return;
198199
}
199200

201+
double anchorPointCorrection = mSize * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mSizeUnit ) / 2.0;
202+
if ( mHorizontalAnchorPoint == Left )
203+
{
204+
offsetX += anchorPointCorrection;
205+
}
206+
else if ( mHorizontalAnchorPoint == Right )
207+
{
208+
offsetX -= anchorPointCorrection;
209+
}
200210

211+
//correct vertical position according to anchor point
212+
if ( mVerticalAnchorPoint == Top )
213+
{
214+
offsetY += anchorPointCorrection;
215+
}
216+
else if ( mVerticalAnchorPoint == Bottom )
217+
{
218+
offsetY -= anchorPointCorrection;
219+
}
201220
}
202221

203222
QPointF QgsMarkerSymbolLayerV2::_rotatedOffset( const QPointF& offset, double angle )

src/core/symbology-ng/qgssymbollayerv2.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,16 @@ class CORE_EXPORT QgsMarkerSymbolLayerV2 : public QgsSymbolLayerV2
124124

125125
enum HorizontalAnchorPoint
126126
{
127-
Left,
128-
HCenter,
129-
Right
127+
Left,
128+
HCenter,
129+
Right
130130
};
131131

132132
enum VerticalAnchorPoint
133133
{
134-
Top,
135-
VCenter,
136-
Bottom
134+
Top,
135+
VCenter,
136+
Bottom
137137
};
138138

139139
virtual void renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context ) = 0;
@@ -166,6 +166,12 @@ class CORE_EXPORT QgsMarkerSymbolLayerV2 : public QgsSymbolLayerV2
166166
virtual void setOutputUnit( QgsSymbolV2::OutputUnit unit );
167167
virtual QgsSymbolV2::OutputUnit outputUnit() const;
168168

169+
void setHorizontalAnchorPoint( HorizontalAnchorPoint h ) { mHorizontalAnchorPoint = h; }
170+
HorizontalAnchorPoint horizontalAnchorPoint() const { return mHorizontalAnchorPoint; }
171+
172+
void setVerticalAnchorPoint( VerticalAnchorPoint v ) { mVerticalAnchorPoint = v; }
173+
VerticalAnchorPoint verticalAnchorPoint() const { return mVerticalAnchorPoint; }
174+
169175
protected:
170176
QgsMarkerSymbolLayerV2( bool locked = false );
171177
//handles marker offset and anchor point shift together

src/gui/symbology-ng/qgssymbollayerv2widget.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,14 @@ void QgsSimpleMarkerSymbolLayerV2Widget::setSymbolLayer( QgsSymbolLayerV2* layer
320320
mOutlineWidthUnitComboBox->blockSignals( true );
321321
mOutlineWidthUnitComboBox->setCurrentIndex( mLayer->outlineWidthUnit() );
322322
mOutlineWidthUnitComboBox->blockSignals( false );
323+
324+
//anchor points
325+
mHorizontalAnchorComboBox->blockSignals( true );
326+
mVerticalAnchorComboBox->blockSignals( true );
327+
mHorizontalAnchorComboBox->setCurrentIndex( mLayer->horizontalAnchorPoint() );
328+
mVerticalAnchorComboBox->setCurrentIndex( mLayer->verticalAnchorPoint() );
329+
mHorizontalAnchorComboBox->blockSignals( false );
330+
mVerticalAnchorComboBox->blockSignals( false );
323331
}
324332

325333
QgsSymbolLayerV2* QgsSimpleMarkerSymbolLayerV2Widget::symbolLayer()
@@ -440,6 +448,24 @@ void QgsSimpleMarkerSymbolLayerV2Widget::on_mDataDefinedPropertiesButton_clicked
440448
}
441449
}
442450

451+
void QgsSimpleMarkerSymbolLayerV2Widget::on_mHorizontalAnchorComboBox_currentIndexChanged( int index )
452+
{
453+
if ( mLayer )
454+
{
455+
mLayer->setHorizontalAnchorPoint(( QgsMarkerSymbolLayerV2::HorizontalAnchorPoint ) index );
456+
emit changed();
457+
}
458+
}
459+
460+
void QgsSimpleMarkerSymbolLayerV2Widget::on_mVerticalAnchorComboBox_currentIndexChanged( int index )
461+
{
462+
if ( mLayer )
463+
{
464+
mLayer->setVerticalAnchorPoint(( QgsMarkerSymbolLayerV2::VerticalAnchorPoint ) index );
465+
emit changed();
466+
}
467+
}
468+
443469

444470
///////////
445471

src/gui/symbology-ng/qgssymbollayerv2widget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ class GUI_EXPORT QgsSimpleMarkerSymbolLayerV2Widget : public QgsSymbolLayerV2Wid
110110
void on_mOutlineWidthUnitComboBox_currentIndexChanged( int index );
111111
void on_mDataDefinedPropertiesButton_clicked();
112112
void on_mOutlineWidthSpinBox_valueChanged( double d );
113+
void on_mHorizontalAnchorComboBox_currentIndexChanged( int index );
114+
void on_mVerticalAnchorComboBox_currentIndexChanged( int index );
113115

114116
protected:
115117
QgsSimpleMarkerSymbolLayerV2* mLayer;

0 commit comments

Comments
 (0)