Skip to content

Commit a94ca70

Browse files
committedJan 30, 2017
[FEATURE] Control over annotation contents margins (refs #10555)
Allows setting left/top/right/bottom margins for the contents within an annotation.
1 parent b2b365f commit a94ca70

File tree

16 files changed

+311
-110
lines changed

16 files changed

+311
-110
lines changed
 

‎python/core/annotations/qgsannotation.sip

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class QgsAnnotation : QObject
2828
void setFrameSize( QSizeF size );
2929
QSizeF frameSize() const;
3030

31+
void setContentsMargin( const QgsMargins& margins );
32+
QgsMargins contentsMargin() const;
33+
3134
void setFrameBorderWidth( double width );
3235
double frameBorderWidth() const;
3336

‎src/app/qgsannotationwidget.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ QgsAnnotationWidget::QgsAnnotationWidget( QgsMapCanvasAnnotationItem* item, QWid
6060
mBackgroundColorButton->setNoColorString( tr( "Transparent" ) );
6161
mBackgroundColorButton->setShowNoColor( true );
6262

63+
whileBlocking( mSpinTopMargin )->setValue( annotation->contentsMargin().top() );
64+
whileBlocking( mSpinLeftMargin )->setValue( annotation->contentsMargin().left() );
65+
whileBlocking( mSpinRightMargin )->setValue( annotation->contentsMargin().right() );
66+
whileBlocking( mSpinBottomMargin )->setValue( annotation->contentsMargin().bottom() );
67+
6368
mLayerComboBox->setLayer( annotation->mapLayer() );
6469

6570
connect( mBackgroundColorButton, &QgsColorButton::colorChanged, this, &QgsAnnotationWidget::backgroundColorChanged );
@@ -92,6 +97,10 @@ void QgsAnnotationWidget::apply()
9297
annotation->setFrameBackgroundColor( mBackgroundColorButton->color() );
9398
annotation->setMarkerSymbol( mMarkerSymbol->clone() );
9499
annotation->setMapLayer( mLayerComboBox->currentLayer() );
100+
annotation->setContentsMargin( QgsMargins( mSpinLeftMargin->value(),
101+
mSpinTopMargin->value(),
102+
mSpinRightMargin->value(),
103+
mSpinBottomMargin->value() ) );
95104
}
96105
mItem->update();
97106
}

‎src/core/annotations/qgsannotation.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ void QgsAnnotation::setFrameSize( QSizeF size )
8383
emit appearanceChanged();
8484
}
8585

86+
void QgsAnnotation::setContentsMargin( const QgsMargins& margins )
87+
{
88+
mContentsMargins = margins;
89+
emit appearanceChanged();
90+
}
91+
8692
void QgsAnnotation::setFrameBorderWidth( double width )
8793
{
8894
mFrameBorderWidth = width;
@@ -117,15 +123,16 @@ void QgsAnnotation::render( QgsRenderContext& context ) const
117123
}
118124
if ( mHasFixedMapPosition )
119125
{
120-
painter->translate( mOffsetFromReferencePoint.x() + mFrameBorderWidth / 2.0,
121-
mOffsetFromReferencePoint.y() + mFrameBorderWidth / 2.0 );
126+
painter->translate( mOffsetFromReferencePoint.x() + context.convertToPainterUnits( mContentsMargins.left(), QgsUnitTypes::RenderMillimeters ),
127+
mOffsetFromReferencePoint.y() + context.convertToPainterUnits( mContentsMargins.top(), QgsUnitTypes::RenderMillimeters ) );
122128
}
123129
else
124130
{
125-
painter->translate( mFrameBorderWidth / 2.0, mFrameBorderWidth / 2.0 );
131+
painter->translate( context.convertToPainterUnits( mContentsMargins.left(), QgsUnitTypes::RenderMillimeters ),
132+
context.convertToPainterUnits( mContentsMargins.top(), QgsUnitTypes::RenderMillimeters ) );
126133
}
127-
QSizeF size( mFrameSize.width() - mFrameBorderWidth,
128-
mFrameSize.height() - mFrameBorderWidth );
134+
QSizeF size( mFrameSize.width() - context.convertToPainterUnits( mContentsMargins.left() + mContentsMargins.right(), QgsUnitTypes::RenderMillimeters ),
135+
mFrameSize.height() - context.convertToPainterUnits( mContentsMargins.top() + mContentsMargins.bottom(), QgsUnitTypes::RenderMillimeters ) );
129136

130137
renderAnnotation( context, size );
131138
painter->restore();
@@ -316,6 +323,7 @@ void QgsAnnotation::_writeXml( QDomElement& itemElem, QDomDocument& doc ) const
316323
annotationElem.setAttribute( QStringLiteral( "canvasPosX" ), qgsDoubleToString( mRelativePosition.x() ) );
317324
annotationElem.setAttribute( QStringLiteral( "canvasPosY" ), qgsDoubleToString( mRelativePosition.y() ) );
318325
annotationElem.setAttribute( QStringLiteral( "frameBorderWidth" ), qgsDoubleToString( mFrameBorderWidth ) );
326+
annotationElem.setAttribute( QStringLiteral( "contentsMargin" ), mContentsMargins.toString() );
319327
annotationElem.setAttribute( QStringLiteral( "frameColor" ), mFrameColor.name() );
320328
annotationElem.setAttribute( QStringLiteral( "frameColorAlpha" ), mFrameColor.alpha() );
321329
annotationElem.setAttribute( QStringLiteral( "frameBackgroundColor" ), mFrameBackgroundColor.name() );
@@ -358,6 +366,7 @@ void QgsAnnotation::_readXml( const QDomElement& annotationElem, const QDomDocum
358366
}
359367

360368
mFrameBorderWidth = annotationElem.attribute( QStringLiteral( "frameBorderWidth" ), QStringLiteral( "0.5" ) ).toDouble();
369+
mContentsMargins = QgsMargins::fromString( annotationElem.attribute( QStringLiteral( "contentsMargin" ) ) );
361370
mFrameColor.setNamedColor( annotationElem.attribute( QStringLiteral( "frameColor" ), QStringLiteral( "#000000" ) ) );
362371
mFrameColor.setAlpha( annotationElem.attribute( QStringLiteral( "frameColorAlpha" ), QStringLiteral( "255" ) ).toInt() );
363372
mFrameBackgroundColor.setNamedColor( annotationElem.attribute( QStringLiteral( "frameBackgroundColor" ) ) );

‎src/core/annotations/qgsannotation.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgscoordinatereferencesystem.h"
2424
#include "qgsrendercontext.h"
2525
#include "qgssymbol.h"
26+
#include "qgsmargins.h"
2627
#include "qgsmaplayer.h"
2728

2829
/** \ingroup core
@@ -156,6 +157,20 @@ class CORE_EXPORT QgsAnnotation : public QObject
156157
*/
157158
QSizeF frameSize() const { return mFrameSize; }
158159

160+
/**
161+
* Sets the margins (in millimeters) between the outside of the frame and the annotation
162+
* content.
163+
* @see contentsMargin()
164+
*/
165+
void setContentsMargin( const QgsMargins& margins );
166+
167+
/**
168+
* Returns the margins (in millimeters) between the outside of the frame and the annotation
169+
* content.
170+
* @see setContentsMargin()
171+
*/
172+
QgsMargins contentsMargin() const { return mContentsMargins; }
173+
159174
/**
160175
* Sets the annotation's frame's border width (in pixels).
161176
* @see frameBorderWidth()
@@ -337,6 +352,8 @@ class CORE_EXPORT QgsAnnotation : public QObject
337352
//! Point symbol that is to be drawn at the map reference location
338353
QScopedPointer<QgsMarkerSymbol> mMarkerSymbol;
339354

355+
QgsMargins mContentsMargins;
356+
340357
//! Width of the frame
341358
double mFrameBorderWidth = 1.0;
342359

‎src/core/annotations/qgshtmlannotation.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ QSizeF QgsHtmlAnnotation::minimumFrameSize() const
8686
if ( mWebPage )
8787
{
8888
QSizeF widgetMinSize = QSizeF( 0, 0 ); // mWebPage->minimumSize();
89-
return QSizeF( 2 * frameBorderWidth() + widgetMinSize.width(), 2 * frameBorderWidth() + widgetMinSize.height() );
89+
return QSizeF( contentsMargin().left() + contentsMargin().right() + widgetMinSize.width(),
90+
contentsMargin().top() + contentsMargin().bottom() + widgetMinSize.height() );
9091
}
9192
else
9293
{

‎src/gui/qgsformannotation.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ QSizeF QgsFormAnnotation::minimumFrameSize() const
109109
if ( mDesignerWidget )
110110
{
111111
QSizeF widgetMinSize = mMinimumSize;
112-
return QSizeF( 2 * frameBorderWidth() + widgetMinSize.width(), 2 * frameBorderWidth() + widgetMinSize.height() );
112+
return QSizeF( contentsMargin().left() + contentsMargin().right() + widgetMinSize.width(),
113+
contentsMargin().top() + contentsMargin().bottom() + widgetMinSize.height() );
113114
}
114115
else
115116
{

0 commit comments

Comments
 (0)
Please sign in to comment.