Skip to content
Permalink
Browse files

[FEATURE] Control over annotation contents margins (refs #10555)

Allows setting left/top/right/bottom margins for the contents
within an annotation.
  • Loading branch information
nyalldawson committed Jan 30, 2017
1 parent b2b365f commit a94ca701ac29a3afd2abf3f70ce9f3eefeefc0e0
@@ -28,6 +28,9 @@ class QgsAnnotation : QObject
void setFrameSize( QSizeF size );
QSizeF frameSize() const;

void setContentsMargin( const QgsMargins& margins );
QgsMargins contentsMargin() const;

void setFrameBorderWidth( double width );
double frameBorderWidth() const;

@@ -60,6 +60,11 @@ QgsAnnotationWidget::QgsAnnotationWidget( QgsMapCanvasAnnotationItem* item, QWid
mBackgroundColorButton->setNoColorString( tr( "Transparent" ) );
mBackgroundColorButton->setShowNoColor( true );

whileBlocking( mSpinTopMargin )->setValue( annotation->contentsMargin().top() );
whileBlocking( mSpinLeftMargin )->setValue( annotation->contentsMargin().left() );
whileBlocking( mSpinRightMargin )->setValue( annotation->contentsMargin().right() );
whileBlocking( mSpinBottomMargin )->setValue( annotation->contentsMargin().bottom() );

mLayerComboBox->setLayer( annotation->mapLayer() );

connect( mBackgroundColorButton, &QgsColorButton::colorChanged, this, &QgsAnnotationWidget::backgroundColorChanged );
@@ -92,6 +97,10 @@ void QgsAnnotationWidget::apply()
annotation->setFrameBackgroundColor( mBackgroundColorButton->color() );
annotation->setMarkerSymbol( mMarkerSymbol->clone() );
annotation->setMapLayer( mLayerComboBox->currentLayer() );
annotation->setContentsMargin( QgsMargins( mSpinLeftMargin->value(),
mSpinTopMargin->value(),
mSpinRightMargin->value(),
mSpinBottomMargin->value() ) );
}
mItem->update();
}
@@ -83,6 +83,12 @@ void QgsAnnotation::setFrameSize( QSizeF size )
emit appearanceChanged();
}

void QgsAnnotation::setContentsMargin( const QgsMargins& margins )
{
mContentsMargins = margins;
emit appearanceChanged();
}

void QgsAnnotation::setFrameBorderWidth( double width )
{
mFrameBorderWidth = width;
@@ -117,15 +123,16 @@ void QgsAnnotation::render( QgsRenderContext& context ) const
}
if ( mHasFixedMapPosition )
{
painter->translate( mOffsetFromReferencePoint.x() + mFrameBorderWidth / 2.0,
mOffsetFromReferencePoint.y() + mFrameBorderWidth / 2.0 );
painter->translate( mOffsetFromReferencePoint.x() + context.convertToPainterUnits( mContentsMargins.left(), QgsUnitTypes::RenderMillimeters ),
mOffsetFromReferencePoint.y() + context.convertToPainterUnits( mContentsMargins.top(), QgsUnitTypes::RenderMillimeters ) );
}
else
{
painter->translate( mFrameBorderWidth / 2.0, mFrameBorderWidth / 2.0 );
painter->translate( context.convertToPainterUnits( mContentsMargins.left(), QgsUnitTypes::RenderMillimeters ),
context.convertToPainterUnits( mContentsMargins.top(), QgsUnitTypes::RenderMillimeters ) );
}
QSizeF size( mFrameSize.width() - mFrameBorderWidth,
mFrameSize.height() - mFrameBorderWidth );
QSizeF size( mFrameSize.width() - context.convertToPainterUnits( mContentsMargins.left() + mContentsMargins.right(), QgsUnitTypes::RenderMillimeters ),
mFrameSize.height() - context.convertToPainterUnits( mContentsMargins.top() + mContentsMargins.bottom(), QgsUnitTypes::RenderMillimeters ) );

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

mFrameBorderWidth = annotationElem.attribute( QStringLiteral( "frameBorderWidth" ), QStringLiteral( "0.5" ) ).toDouble();
mContentsMargins = QgsMargins::fromString( annotationElem.attribute( QStringLiteral( "contentsMargin" ) ) );
mFrameColor.setNamedColor( annotationElem.attribute( QStringLiteral( "frameColor" ), QStringLiteral( "#000000" ) ) );
mFrameColor.setAlpha( annotationElem.attribute( QStringLiteral( "frameColorAlpha" ), QStringLiteral( "255" ) ).toInt() );
mFrameBackgroundColor.setNamedColor( annotationElem.attribute( QStringLiteral( "frameBackgroundColor" ) ) );
@@ -23,6 +23,7 @@
#include "qgscoordinatereferencesystem.h"
#include "qgsrendercontext.h"
#include "qgssymbol.h"
#include "qgsmargins.h"
#include "qgsmaplayer.h"

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

/**
* Sets the margins (in millimeters) between the outside of the frame and the annotation
* content.
* @see contentsMargin()
*/
void setContentsMargin( const QgsMargins& margins );

/**
* Returns the margins (in millimeters) between the outside of the frame and the annotation
* content.
* @see setContentsMargin()
*/
QgsMargins contentsMargin() const { return mContentsMargins; }

/**
* Sets the annotation's frame's border width (in pixels).
* @see frameBorderWidth()
@@ -337,6 +352,8 @@ class CORE_EXPORT QgsAnnotation : public QObject
//! Point symbol that is to be drawn at the map reference location
QScopedPointer<QgsMarkerSymbol> mMarkerSymbol;

QgsMargins mContentsMargins;

//! Width of the frame
double mFrameBorderWidth = 1.0;

@@ -86,7 +86,8 @@ QSizeF QgsHtmlAnnotation::minimumFrameSize() const
if ( mWebPage )
{
QSizeF widgetMinSize = QSizeF( 0, 0 ); // mWebPage->minimumSize();
return QSizeF( 2 * frameBorderWidth() + widgetMinSize.width(), 2 * frameBorderWidth() + widgetMinSize.height() );
return QSizeF( contentsMargin().left() + contentsMargin().right() + widgetMinSize.width(),
contentsMargin().top() + contentsMargin().bottom() + widgetMinSize.height() );
}
else
{
@@ -109,7 +109,8 @@ QSizeF QgsFormAnnotation::minimumFrameSize() const
if ( mDesignerWidget )
{
QSizeF widgetMinSize = mMinimumSize;
return QSizeF( 2 * frameBorderWidth() + widgetMinSize.width(), 2 * frameBorderWidth() + widgetMinSize.height() );
return QSizeF( contentsMargin().left() + contentsMargin().right() + widgetMinSize.width(),
contentsMargin().top() + contentsMargin().bottom() + widgetMinSize.height() );
}
else
{

0 comments on commit a94ca70

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