Skip to content

Commit

Permalink
[FEATURE] Control over annotation contents margins (refs #10555)
Browse files Browse the repository at this point in the history
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 a94ca70
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 110 deletions.
3 changes: 3 additions & 0 deletions python/core/annotations/qgsannotation.sip
Expand Up @@ -28,6 +28,9 @@ class QgsAnnotation : QObject
void setFrameSize( QSizeF size ); void setFrameSize( QSizeF size );
QSizeF frameSize() const; QSizeF frameSize() const;


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

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


Expand Down
9 changes: 9 additions & 0 deletions src/app/qgsannotationwidget.cpp
Expand Up @@ -60,6 +60,11 @@ QgsAnnotationWidget::QgsAnnotationWidget( QgsMapCanvasAnnotationItem* item, QWid
mBackgroundColorButton->setNoColorString( tr( "Transparent" ) ); mBackgroundColorButton->setNoColorString( tr( "Transparent" ) );
mBackgroundColorButton->setShowNoColor( true ); 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() ); mLayerComboBox->setLayer( annotation->mapLayer() );


connect( mBackgroundColorButton, &QgsColorButton::colorChanged, this, &QgsAnnotationWidget::backgroundColorChanged ); connect( mBackgroundColorButton, &QgsColorButton::colorChanged, this, &QgsAnnotationWidget::backgroundColorChanged );
Expand Down Expand Up @@ -92,6 +97,10 @@ void QgsAnnotationWidget::apply()
annotation->setFrameBackgroundColor( mBackgroundColorButton->color() ); annotation->setFrameBackgroundColor( mBackgroundColorButton->color() );
annotation->setMarkerSymbol( mMarkerSymbol->clone() ); annotation->setMarkerSymbol( mMarkerSymbol->clone() );
annotation->setMapLayer( mLayerComboBox->currentLayer() ); annotation->setMapLayer( mLayerComboBox->currentLayer() );
annotation->setContentsMargin( QgsMargins( mSpinLeftMargin->value(),
mSpinTopMargin->value(),
mSpinRightMargin->value(),
mSpinBottomMargin->value() ) );
} }
mItem->update(); mItem->update();
} }
Expand Down
19 changes: 14 additions & 5 deletions src/core/annotations/qgsannotation.cpp
Expand Up @@ -83,6 +83,12 @@ void QgsAnnotation::setFrameSize( QSizeF size )
emit appearanceChanged(); emit appearanceChanged();
} }


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

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


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


mFrameBorderWidth = annotationElem.attribute( QStringLiteral( "frameBorderWidth" ), QStringLiteral( "0.5" ) ).toDouble(); 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.setNamedColor( annotationElem.attribute( QStringLiteral( "frameColor" ), QStringLiteral( "#000000" ) ) );
mFrameColor.setAlpha( annotationElem.attribute( QStringLiteral( "frameColorAlpha" ), QStringLiteral( "255" ) ).toInt() ); mFrameColor.setAlpha( annotationElem.attribute( QStringLiteral( "frameColorAlpha" ), QStringLiteral( "255" ) ).toInt() );
mFrameBackgroundColor.setNamedColor( annotationElem.attribute( QStringLiteral( "frameBackgroundColor" ) ) ); mFrameBackgroundColor.setNamedColor( annotationElem.attribute( QStringLiteral( "frameBackgroundColor" ) ) );
Expand Down
17 changes: 17 additions & 0 deletions src/core/annotations/qgsannotation.h
Expand Up @@ -23,6 +23,7 @@
#include "qgscoordinatereferencesystem.h" #include "qgscoordinatereferencesystem.h"
#include "qgsrendercontext.h" #include "qgsrendercontext.h"
#include "qgssymbol.h" #include "qgssymbol.h"
#include "qgsmargins.h"
#include "qgsmaplayer.h" #include "qgsmaplayer.h"


/** \ingroup core /** \ingroup core
Expand Down Expand Up @@ -156,6 +157,20 @@ class CORE_EXPORT QgsAnnotation : public QObject
*/ */
QSizeF frameSize() const { return mFrameSize; } 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). * Sets the annotation's frame's border width (in pixels).
* @see frameBorderWidth() * @see frameBorderWidth()
Expand Down Expand Up @@ -337,6 +352,8 @@ class CORE_EXPORT QgsAnnotation : public QObject
//! Point symbol that is to be drawn at the map reference location //! Point symbol that is to be drawn at the map reference location
QScopedPointer<QgsMarkerSymbol> mMarkerSymbol; QScopedPointer<QgsMarkerSymbol> mMarkerSymbol;


QgsMargins mContentsMargins;

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


Expand Down
3 changes: 2 additions & 1 deletion src/core/annotations/qgshtmlannotation.cpp
Expand Up @@ -86,7 +86,8 @@ QSizeF QgsHtmlAnnotation::minimumFrameSize() const
if ( mWebPage ) if ( mWebPage )
{ {
QSizeF widgetMinSize = QSizeF( 0, 0 ); // mWebPage->minimumSize(); 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 else
{ {
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsformannotation.cpp
Expand Up @@ -109,7 +109,8 @@ QSizeF QgsFormAnnotation::minimumFrameSize() const
if ( mDesignerWidget ) if ( mDesignerWidget )
{ {
QSizeF widgetMinSize = mMinimumSize; 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 else
{ {
Expand Down

0 comments on commit a94ca70

Please sign in to comment.