QGIS API Documentation 3.41.0-Master (25ec5511245)
Loading...
Searching...
No Matches
qgslayoutitemmarker.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutitemmarker.cpp
3 ---------------------
4 begin : April 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgslayoutitemmarker.h"
18#include "moc_qgslayoutitemmarker.cpp"
19#include "qgslayout.h"
20#include "qgslayoututils.h"
21#include "qgssymbollayerutils.h"
24#include "qgslayoutitemmap.h"
25#include "qgsmarkersymbol.h"
26
27#include <QPainter>
28
30 : QgsLayoutItem( layout )
31 , mNorthArrowHandler( new QgsLayoutNorthArrowHandler( this ) )
32{
33 setBackgroundEnabled( false );
34 setFrameEnabled( false );
36 QVariantMap properties;
37 properties.insert( QStringLiteral( "size" ), QStringLiteral( "4" ) );
38 mShapeStyleSymbol.reset( QgsMarkerSymbol::createSimple( properties ) );
39 refreshSymbol();
40
41 connect( this, &QgsLayoutItemMarker::sizePositionChanged, this, [this]
42 {
43 updateBoundingRect();
44 update();
45 } );
46
47 connect( mNorthArrowHandler, &QgsLayoutNorthArrowHandler::arrowRotationChanged, this, &QgsLayoutItemMarker::northArrowRotationChanged );
48}
49
51
56
61
63{
64 return QgsApplication::getThemeIcon( QStringLiteral( "/mLayoutItemMarker.svg" ) );
65}
66
67void QgsLayoutItemMarker::refreshSymbol()
68{
69 if ( auto *lLayout = layout() )
70 {
71 QgsRenderContext rc = QgsLayoutUtils::createRenderContextForLayout( lLayout, nullptr, lLayout->renderContext().dpi() );
72
73 std::unique_ptr< QgsMarkerSymbol > sym( mShapeStyleSymbol->clone() );
74 sym->setAngle( sym->angle() + mNorthArrowRotation );
75 sym->startRender( rc );
76 QRectF bounds = sym->bounds( QPointF( 0, 0 ), rc );
77 sym->stopRender( rc );
78 mPoint = QPointF( -bounds.left() * 25.4 / lLayout->renderContext().dpi(),
79 -bounds.top() * 25.4 / lLayout->renderContext().dpi() );
80 bounds.translate( mPoint );
81
82 const QgsLayoutSize newSizeMm = QgsLayoutSize( bounds.size() * 25.4 / lLayout->renderContext().dpi(), Qgis::LayoutUnit::Millimeters );
83 mFixedSize = mLayout->renderContext().measurementConverter().convert( newSizeMm, sizeWithUnits().units() );
84
85 attemptResize( mFixedSize );
86 }
87
88 updateBoundingRect();
89
90 update();
91 emit frameChanged();
92}
93
94void QgsLayoutItemMarker::updateBoundingRect()
95{
96 QRectF rectangle = rect();
97
98 // add a bit, to account for antialiasing on stroke and miter effects on stroke
99 rectangle.adjust( -5, -5, 5, 5 );
100 if ( rectangle != mCurrentRectangle )
101 {
102 prepareGeometryChange();
103 mCurrentRectangle = rectangle;
104 }
105}
106
107void QgsLayoutItemMarker::northArrowRotationChanged( double rotation )
108{
109 mNorthArrowRotation = rotation;
110 refreshSymbol();
111}
112
114{
115 if ( !symbol )
116 return;
117
118 mShapeStyleSymbol.reset( symbol );
119 refreshSymbol();
120}
121
123{
124 return mShapeStyleSymbol.get();
125}
126
128{
129 mNorthArrowHandler->setLinkedMap( map );
130}
131
133{
134 return mNorthArrowHandler->linkedMap();
135}
136
138{
139 return mNorthArrowHandler->northMode();
140
141}
142
144{
145 mNorthArrowHandler->setNorthMode( mode );
146
147}
148
150{
151 return mNorthArrowHandler->northOffset();
152}
153
155{
156 mNorthArrowHandler->setNorthOffset( offset );
157}
158
160{
161 return mCurrentRectangle;
162}
163
165{
166 return mFixedSize;
167}
168
170{
171 if ( mShapeStyleSymbol )
172 {
173 QgsStyleSymbolEntity entity( mShapeStyleSymbol.get() );
174 if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, uuid(), displayName() ) ) )
175 return false;
176 }
177
178 return true;
179}
180
182{
183 QPainter *painter = context.renderContext().painter();
184 painter->setPen( Qt::NoPen );
185 painter->setBrush( Qt::NoBrush );
186
187 const double scale = context.renderContext().convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
188
189 const QPointF shapePoint = mPoint * scale;
190
191 std::unique_ptr< QgsMarkerSymbol > sym( mShapeStyleSymbol->clone() );
192 sym->setAngle( sym->angle() + mNorthArrowRotation );
193 sym->startRender( context.renderContext() );
194 sym->renderPoint( shapePoint, nullptr, context.renderContext() );
195 sym->stopRender( context.renderContext() );
196}
197
198bool QgsLayoutItemMarker::writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
199{
200 const QDomElement shapeStyleElem = QgsSymbolLayerUtils::saveSymbol( QString(), mShapeStyleSymbol.get(), document, context );
201 element.appendChild( shapeStyleElem );
202
203 //rotation
204 element.setAttribute( QStringLiteral( "arrowRotation" ), QString::number( mNorthArrowRotation ) );
205 if ( !mNorthArrowHandler->linkedMap() )
206 {
207 element.setAttribute( QStringLiteral( "mapUuid" ), QString() );
208 }
209 else
210 {
211 element.setAttribute( QStringLiteral( "mapUuid" ), mNorthArrowHandler->linkedMap()->uuid() );
212 }
213 element.setAttribute( QStringLiteral( "northMode" ), mNorthArrowHandler->northMode() );
214 element.setAttribute( QStringLiteral( "northOffset" ), mNorthArrowHandler->northOffset() );
215
216 return true;
217}
218
219bool QgsLayoutItemMarker::readPropertiesFromElement( const QDomElement &element, const QDomDocument &, const QgsReadWriteContext &context )
220{
221 const QDomElement shapeStyleSymbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
222 if ( !shapeStyleSymbolElem.isNull() )
223 {
224 mShapeStyleSymbol.reset( QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( shapeStyleSymbolElem, context ) );
225 }
226
227 //picture rotation
228 if ( !qgsDoubleNear( element.attribute( QStringLiteral( "arrowRotation" ), QStringLiteral( "0" ) ).toDouble(), 0.0 ) )
229 {
230 mNorthArrowRotation = element.attribute( QStringLiteral( "arrowRotation" ), QStringLiteral( "0" ) ).toDouble();
231 }
232
233 //rotation map
234 mNorthArrowHandler->setNorthMode( static_cast< QgsLayoutNorthArrowHandler::NorthMode >( element.attribute( QStringLiteral( "northMode" ), QStringLiteral( "0" ) ).toInt() ) );
235 mNorthArrowHandler->setNorthOffset( element.attribute( QStringLiteral( "northOffset" ), QStringLiteral( "0" ) ).toDouble() );
236
237 mNorthArrowHandler->setLinkedMap( nullptr );
238 mRotationMapUuid = element.attribute( QStringLiteral( "mapUuid" ) );
239
240 refreshSymbol();
241
242 return true;
243}
244
246{
247 if ( !mLayout || mRotationMapUuid.isEmpty() )
248 {
249 mNorthArrowHandler->setLinkedMap( nullptr );
250 }
251 else
252 {
253 mNorthArrowHandler->setLinkedMap( qobject_cast< QgsLayoutItemMap * >( mLayout->itemByUuid( mRotationMapUuid, true ) ) );
254 }
255 emit changed();
256}
@ Millimeters
Millimeters.
@ Millimeters
Millimeters.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Layout graphical items for displaying a map.
A layout item for showing marker symbols.
~QgsLayoutItemMarker() override
double northOffset() const
Returns the offset added to the marker's rotation from a map's North.
void draw(QgsLayoutItemRenderContext &context) override
Draws the item's contents using the specified item render context.
bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
QIcon icon() const override
Returns the item's icon.
void setLinkedMap(QgsLayoutItemMap *map)
Sets the map object for rotation.
QgsLayoutNorthArrowHandler::NorthMode northMode() const
Returns the mode used to align the marker to a map's North.
QgsLayoutItemMarker(QgsLayout *layout)
Constructor for QgsLayoutItemMarker, with the specified parent layout.
QgsLayoutSize fixedSize() const override
Returns the fixed size of the item, if applicable, or an empty size if item can be freely resized.
void setNorthOffset(double offset)
Sets the offset added to the marker's rotation from a map's North.
int type() const override
void setNorthMode(QgsLayoutNorthArrowHandler::NorthMode mode)
Sets the mode used to align the marker to a map's North.
static QgsLayoutItemMarker * create(QgsLayout *layout)
Returns a new marker item for the specified layout.
QgsLayoutItemMap * linkedMap() const
Returns the linked rotation map, if set.
void setSymbol(QgsMarkerSymbol *symbol)
Sets the marker symbol used to draw the shape.
void finalizeRestoreFromXml() override
Called after all pending items have been restored from XML.
QRectF boundingRect() const override
QgsMarkerSymbol * symbol()
Returns the marker symbol used to draw the shape.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
Contains settings and helpers relating to a render of a QgsLayoutItem.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
Base class for graphical items within a QgsLayout.
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
@ Middle
Center of item.
virtual void setFrameEnabled(bool drawFrame)
Sets whether this item has a frame drawn around it or not.
virtual QString displayName() const
Gets item display name.
virtual void attemptResize(const QgsLayoutSize &size, bool includesFrame=false)
Attempts to resize the item to a specified target size.
void sizePositionChanged()
Emitted when the item's size or position changes.
virtual QString uuid() const
Returns the item identification string.
void frameChanged()
Emitted if the item's frame style changes.
void setReferencePoint(ReferencePoint point)
Sets the reference point for positioning of the layout item.
void setBackgroundEnabled(bool drawBackground)
Sets whether this item has a background drawn under it or not.
An object which handles north-arrow type behavior for layout items.
void setNorthOffset(double offset)
Sets the offset added to the arrows's rotation from a map's North.
NorthMode northMode() const
Returns the mode used to calculate the arrow rotation.
QgsLayoutItemMap * linkedMap() const
Returns the linked rotation map, if set.
void arrowRotationChanged(double newRotation)
Emitted on arrow rotation change.
NorthMode
Method for syncing rotation to a map's North direction.
void setNorthMode(NorthMode mode)
Sets the mode used to calculate the arrow rotation.
void setLinkedMap(QgsLayoutItemMap *map)
Sets the linked map item.
double northOffset() const
Returns the offset added to the arrows's rotation from a map's North.
const QgsLayout * layout() const
Returns the layout the object is attached to.
void changed()
Emitted when the object's properties change.
QPointer< QgsLayout > mLayout
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:49
A marker symbol type, for rendering Point and MultiPoint geometries.
static QgsMarkerSymbol * createSimple(const QVariantMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
An interface for classes which can visit style entity (e.g.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
A symbol entity for QgsStyle databases.
Definition qgsstyle.h:1396
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5949
Contains information relating to the style entity currently being visited.