Skip to content

Commit

Permalink
More data defined anchor points
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Sep 20, 2013
1 parent 03cfba7 commit dec8953
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/core/symbology-ng/qgsmarkersymbollayerv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ QgsSymbolLayerV2* QgsSimpleMarkerSymbolLayerV2::create( const QgsStringMap& prop
{
m->setDataDefinedProperty( "offset", props["offset_expression"] );
}
if ( props.contains( "horizontal_anchor_point_expression" ) )
{
m->setDataDefinedProperty( "horizontal_anchor_point", props[ "horizontal_anchor_point_expression" ] );
}
if ( props.contains( "vertical_anchor_point_expression" ) )
{
m->setDataDefinedProperty( "vertical_anchor_point", props[ "vertical_anchor_point_expression" ] );
}
return m;
}

Expand Down Expand Up @@ -818,6 +826,14 @@ QgsSymbolLayerV2* QgsSvgMarkerSymbolLayerV2::create( const QgsStringMap& props )
{
m->setDataDefinedProperty( "outline", props["outline_expression"] );
}
if ( props.contains( "horizontal_anchor_point_expression" ) )
{
m->setDataDefinedProperty( "horizontal_anchor_point", props[ "horizontal_anchor_point_expression" ] );
}
if ( props.contains( "vertical_anchor_point_expression" ) )
{
m->setDataDefinedProperty( "vertical_anchor_point", props[ "vertical_anchor_point_expression" ] );
}
return m;
}

Expand Down
55 changes: 50 additions & 5 deletions src/core/symbology-ng/qgssymbollayerv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,28 +192,41 @@ void QgsMarkerSymbolLayerV2::markerOffset( QgsSymbolV2RenderContext& context, do
offsetX *= QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mOffsetUnit );
offsetY *= QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mOffsetUnit );

HorizontalAnchorPoint horizontalAnchorPoint = mHorizontalAnchorPoint;
VerticalAnchorPoint verticalAnchorPoint = mVerticalAnchorPoint;
QgsExpression* horizontalAnchorExpression = expression( "horizontal_anchor_point" );
if ( horizontalAnchorExpression )
{
horizontalAnchorPoint = decodeHorizontalAnchorPoint( horizontalAnchorExpression->evaluate( const_cast<QgsFeature*>( context.feature() ) ).toString() );
}
QgsExpression* verticalAnchorExpression = expression( "vertical_anchor_point" );
if ( verticalAnchorExpression )
{
verticalAnchorPoint = decodeVerticalAnchorPoint( verticalAnchorExpression->evaluate( const_cast<QgsFeature*>( context.feature() ) ).toString() );
}

//correct horizontal position according to anchor point
if ( mHorizontalAnchorPoint == HCenter && mVerticalAnchorPoint == VCenter )
if ( horizontalAnchorPoint == HCenter && verticalAnchorPoint == VCenter )
{
return;
}

double anchorPointCorrection = mSize * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mSizeUnit ) / 2.0;
if ( mHorizontalAnchorPoint == Left )
if ( horizontalAnchorPoint == Left )
{
offsetX += anchorPointCorrection;
}
else if ( mHorizontalAnchorPoint == Right )
else if ( horizontalAnchorPoint == Right )
{
offsetX -= anchorPointCorrection;
}

//correct vertical position according to anchor point
if ( mVerticalAnchorPoint == Top )
if ( verticalAnchorPoint == Top )
{
offsetY += anchorPointCorrection;
}
else if ( mVerticalAnchorPoint == Bottom )
else if ( verticalAnchorPoint == Bottom )
{
offsetY -= anchorPointCorrection;
}
Expand All @@ -226,6 +239,38 @@ QPointF QgsMarkerSymbolLayerV2::_rotatedOffset( const QPointF& offset, double an
return QPointF( offset.x() * c - offset.y() * s, offset.x() * s + offset.y() * c );
}

QgsMarkerSymbolLayerV2::HorizontalAnchorPoint QgsMarkerSymbolLayerV2::decodeHorizontalAnchorPoint( const QString& str )
{
if ( str.compare( "left", Qt::CaseInsensitive ) == 0 )
{
return QgsMarkerSymbolLayerV2::Left;
}
else if ( str.compare( "right", Qt::CaseInsensitive ) == 0 )
{
return QgsMarkerSymbolLayerV2::Right;
}
else
{
return QgsMarkerSymbolLayerV2::HCenter;
}
}

QgsMarkerSymbolLayerV2::VerticalAnchorPoint QgsMarkerSymbolLayerV2::decodeVerticalAnchorPoint( const QString& str )
{
if ( str.compare( "top", Qt::CaseInsensitive ) == 0 )
{
return QgsMarkerSymbolLayerV2::Top;
}
else if ( str.compare( "bottom", Qt::CaseInsensitive ) == 0 )
{
return QgsMarkerSymbolLayerV2::Bottom;
}
else
{
return QgsMarkerSymbolLayerV2::VCenter;
}
}

QgsSymbolV2::OutputUnit QgsMarkerSymbolLayerV2::outputUnit() const
{
QgsSymbolV2::OutputUnit unit = mSizeUnit;
Expand Down
4 changes: 4 additions & 0 deletions src/core/symbology-ng/qgssymbollayerv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ class CORE_EXPORT QgsMarkerSymbolLayerV2 : public QgsSymbolLayerV2
QgsSymbolV2::ScaleMethod mScaleMethod;
HorizontalAnchorPoint mHorizontalAnchorPoint;
VerticalAnchorPoint mVerticalAnchorPoint;

private:
static QgsMarkerSymbolLayerV2::HorizontalAnchorPoint decodeHorizontalAnchorPoint( const QString& str );
static QgsMarkerSymbolLayerV2::VerticalAnchorPoint decodeVerticalAnchorPoint( const QString& str );
};

class CORE_EXPORT QgsLineSymbolLayerV2 : public QgsSymbolLayerV2
Expand Down
10 changes: 10 additions & 0 deletions src/gui/symbology-ng/qgsdatadefinedsymboldialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,13 @@ QString QgsDataDefinedSymbolDialog::fileNameHelpText()
{
return tr( "'<filename>'" );
}

QString QgsDataDefinedSymbolDialog::horizontalAnchorHelpText()
{
return tr( "'left'|'hcenter'|'right'" );
}

QString QgsDataDefinedSymbolDialog::verticalAnchorHelpText()
{
return tr( "'top'|'vcenter'|'bottom'" );
}
2 changes: 2 additions & 0 deletions src/gui/symbology-ng/qgsdatadefinedsymboldialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class GUI_EXPORT QgsDataDefinedSymbolDialog: public QDialog, private Ui::QgsData
static QString colorHelpText();
static QString offsetHelpText();
static QString fileNameHelpText();
static QString horizontalAnchorHelpText();
static QString verticalAnchorHelpText();

private slots:
void expressionButtonClicked();
Expand Down
12 changes: 12 additions & 0 deletions src/gui/symbology-ng/qgssymbollayerv2widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ void QgsSimpleMarkerSymbolLayerV2Widget::on_mDataDefinedPropertiesButton_clicked
QgsDataDefinedSymbolDialog::doubleHelpText() );
dataDefinedProperties << QgsDataDefinedSymbolDialog::DataDefinedSymbolEntry( "offset", tr( "Offset" ), mLayer->dataDefinedPropertyString( "offset" ),
QgsDataDefinedSymbolDialog::offsetHelpText() );
dataDefinedProperties << QgsDataDefinedSymbolDialog::DataDefinedSymbolEntry( "horizontal_anchor_point", tr( "Horizontal anchor point" ), mLayer->dataDefinedPropertyString( "horizontal_anchor_point" ),
QgsDataDefinedSymbolDialog::horizontalAnchorHelpText() );
dataDefinedProperties << QgsDataDefinedSymbolDialog::DataDefinedSymbolEntry( "vertical_anchor_point", tr( "Vertical anchor point" ), mLayer->dataDefinedPropertyString( "vertical_anchor_point" ),
QgsDataDefinedSymbolDialog::verticalAnchorHelpText() );
QgsDataDefinedSymbolDialog d( dataDefinedProperties, mVectorLayer );
if ( d.exec() == QDialog::Accepted )
{
Expand Down Expand Up @@ -588,6 +592,10 @@ void QgsSimpleFillSymbolLayerV2Widget::on_mDataDefinedPropertiesButton_clicked()
QgsDataDefinedSymbolDialog::colorHelpText() );
dataDefinedProperties << QgsDataDefinedSymbolDialog::DataDefinedSymbolEntry( "width_border", tr( "Border width" ), mLayer->dataDefinedPropertyString( "width_border" ),
QgsDataDefinedSymbolDialog::doubleHelpText() );
dataDefinedProperties << QgsDataDefinedSymbolDialog::DataDefinedSymbolEntry( "horizontal_anchor_point", tr( "Horizontal anchor point" ), mLayer->dataDefinedPropertyString( "horizontal_anchor_point" ),
QgsDataDefinedSymbolDialog::horizontalAnchorHelpText() );
dataDefinedProperties << QgsDataDefinedSymbolDialog::DataDefinedSymbolEntry( "vertical_anchor_point", tr( "Vertical anchor point" ), mLayer->dataDefinedPropertyString( "vertical_anchor_point" ),
QgsDataDefinedSymbolDialog::verticalAnchorHelpText() );
QgsDataDefinedSymbolDialog d( dataDefinedProperties, mVectorLayer );
if ( d.exec() == QDialog::Accepted )
{
Expand Down Expand Up @@ -1185,6 +1193,10 @@ void QgsSvgMarkerSymbolLayerV2Widget::on_mDataDefinedPropertiesButton_clicked()
QgsDataDefinedSymbolDialog::colorHelpText() );
dataDefinedProperties << QgsDataDefinedSymbolDialog::DataDefinedSymbolEntry( "outline", tr( "Border color" ), mLayer->dataDefinedPropertyString( "outline" ),
QgsDataDefinedSymbolDialog::colorHelpText() );
dataDefinedProperties << QgsDataDefinedSymbolDialog::DataDefinedSymbolEntry( "horizontal_anchor_point", tr( "Horizontal anchor point" ), mLayer->dataDefinedPropertyString( "horizontal_anchor_point" ),
QgsDataDefinedSymbolDialog::horizontalAnchorHelpText() );
dataDefinedProperties << QgsDataDefinedSymbolDialog::DataDefinedSymbolEntry( "vertical_anchor_point", tr( "Vertical anchor point" ), mLayer->dataDefinedPropertyString( "vertical_anchor_point" ),
QgsDataDefinedSymbolDialog::verticalAnchorHelpText() );
QgsDataDefinedSymbolDialog d( dataDefinedProperties, mVectorLayer );
if ( d.exec() == QDialog::Accepted )
{
Expand Down

0 comments on commit dec8953

Please sign in to comment.