QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
19#include "qgslayout.h"
20#include "qgslayoutitemmap.h"
22#include "qgslayoututils.h"
23#include "qgsmarkersymbol.h"
25#include "qgssymbollayerutils.h"
26
27#include <QPainter>
28#include <QString>
29
30#include "moc_qgslayoutitemmarker.cpp"
31
32using namespace Qt::StringLiterals;
33
36 , mNorthArrowHandler( new QgsLayoutNorthArrowHandler( this ) )
37{
38 setBackgroundEnabled( false );
39 setFrameEnabled( false );
41 QVariantMap properties;
42 properties.insert( u"size"_s, u"4"_s );
43 mShapeStyleSymbol = QgsMarkerSymbol::createSimple( properties );
44 refreshSymbol();
45
46 connect( this, &QgsLayoutItemMarker::sizePositionChanged, this, [this] {
47 updateBoundingRect();
48 update();
49 } );
50
51 connect( mNorthArrowHandler, &QgsLayoutNorthArrowHandler::arrowRotationChanged, this, &QgsLayoutItemMarker::northArrowRotationChanged );
52}
53
55
60
65
67{
68 return QgsApplication::getThemeIcon( u"/mLayoutItemMarker.svg"_s );
69}
70
71void QgsLayoutItemMarker::refreshSymbol()
72{
73 if ( auto *lLayout = layout() )
74 {
75 QgsRenderContext rc = QgsLayoutUtils::createRenderContextForLayout( lLayout, nullptr, lLayout->renderContext().dpi() );
76
77 std::unique_ptr< QgsMarkerSymbol > sym( mShapeStyleSymbol->clone() );
78 sym->setAngle( sym->angle() + mNorthArrowRotation );
79 sym->startRender( rc );
80 QRectF bounds = sym->bounds( QPointF( 0, 0 ), rc );
81 sym->stopRender( rc );
82 mPoint = QPointF( -bounds.left() * 25.4 / lLayout->renderContext().dpi(), -bounds.top() * 25.4 / lLayout->renderContext().dpi() );
83 bounds.translate( mPoint );
84
85 const QgsLayoutSize newSizeMm = QgsLayoutSize( bounds.size() * 25.4 / lLayout->renderContext().dpi(), Qgis::LayoutUnit::Millimeters );
86 mFixedSize = mLayout->renderContext().measurementConverter().convert( newSizeMm, sizeWithUnits().units() );
87
88 attemptResize( mFixedSize );
89 }
90
91 updateBoundingRect();
92
93 update();
94 emit frameChanged();
95}
96
97void QgsLayoutItemMarker::updateBoundingRect()
98{
99 QRectF rectangle = rect();
100
101 // add a bit, to account for antialiasing on stroke and miter effects on stroke
102 rectangle.adjust( -5, -5, 5, 5 );
103 if ( rectangle != mCurrentRectangle )
104 {
105 prepareGeometryChange();
106 mCurrentRectangle = rectangle;
107 }
108}
109
110void QgsLayoutItemMarker::northArrowRotationChanged( double rotation )
111{
112 mNorthArrowRotation = rotation;
113 refreshSymbol();
114}
115
117{
118 if ( !symbol )
119 return;
120
121 mShapeStyleSymbol.reset( symbol );
122 refreshSymbol();
123}
124
126{
127 return mShapeStyleSymbol.get();
128}
129
131{
132 mNorthArrowHandler->setLinkedMap( map );
133}
134
136{
137 return mNorthArrowHandler->linkedMap();
138}
139
141{
142 return mNorthArrowHandler->northMode();
143}
144
146{
147 mNorthArrowHandler->setNorthMode( mode );
148}
149
151{
152 return mNorthArrowHandler->northOffset();
153}
154
156{
157 mNorthArrowHandler->setNorthOffset( offset );
158}
159
161{
162 return mCurrentRectangle;
163}
164
166{
167 return mFixedSize;
168}
169
171{
172 if ( mShapeStyleSymbol )
173 {
174 QgsStyleSymbolEntity entity( mShapeStyleSymbol.get() );
175 if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, uuid(), displayName() ) ) )
176 return false;
177 }
178
179 return true;
180}
181
183{
184 QPainter *painter = context.renderContext().painter();
185 painter->setPen( Qt::NoPen );
186 painter->setBrush( Qt::NoBrush );
187
188 const double scale = context.renderContext().convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
189
190 const QPointF shapePoint = mPoint * scale;
191
192 std::unique_ptr< QgsMarkerSymbol > sym( mShapeStyleSymbol->clone() );
193 sym->setAngle( sym->angle() + mNorthArrowRotation );
194 sym->startRender( context.renderContext() );
195 sym->renderPoint( shapePoint, nullptr, context.renderContext() );
196 sym->stopRender( context.renderContext() );
197}
198
199bool QgsLayoutItemMarker::writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
200{
201 const QDomElement shapeStyleElem = QgsSymbolLayerUtils::saveSymbol( QString(), mShapeStyleSymbol.get(), document, context );
202 element.appendChild( shapeStyleElem );
203
204 //rotation
205 element.setAttribute( u"arrowRotation"_s, QString::number( mNorthArrowRotation ) );
206 if ( !mNorthArrowHandler->linkedMap() )
207 {
208 element.setAttribute( u"mapUuid"_s, QString() );
209 }
210 else
211 {
212 element.setAttribute( u"mapUuid"_s, mNorthArrowHandler->linkedMap()->uuid() );
213 }
214 element.setAttribute( u"northMode"_s, mNorthArrowHandler->northMode() );
215 element.setAttribute( u"northOffset"_s, mNorthArrowHandler->northOffset() );
216
217 return true;
218}
219
220bool QgsLayoutItemMarker::readPropertiesFromElement( const QDomElement &element, const QDomDocument &, const QgsReadWriteContext &context )
221{
222 const QDomElement shapeStyleSymbolElem = element.firstChildElement( u"symbol"_s );
223 if ( !shapeStyleSymbolElem.isNull() )
224 {
225 mShapeStyleSymbol = QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( shapeStyleSymbolElem, context );
226 }
227
228 //picture rotation
229 if ( !qgsDoubleNear( element.attribute( u"arrowRotation"_s, u"0"_s ).toDouble(), 0.0 ) )
230 {
231 mNorthArrowRotation = element.attribute( u"arrowRotation"_s, u"0"_s ).toDouble();
232 }
233
234 //rotation map
235 mNorthArrowHandler->setNorthMode( static_cast< QgsLayoutNorthArrowHandler::NorthMode >( element.attribute( u"northMode"_s, u"0"_s ).toInt() ) );
236 mNorthArrowHandler->setNorthOffset( element.attribute( u"northOffset"_s, u"0"_s ).toDouble() );
237
238 mNorthArrowHandler->setLinkedMap( nullptr );
239 mRotationMapUuid = element.attribute( u"mapUuid"_s );
240
241 refreshSymbol();
242
243 return true;
244}
245
247{
248 if ( !mLayout || mRotationMapUuid.isEmpty() )
249 {
250 mNorthArrowHandler->setLinkedMap( nullptr );
251 }
252 else
253 {
254 mNorthArrowHandler->setLinkedMap( qobject_cast< QgsLayoutItemMap * >( mLayout->itemByUuid( mRotationMapUuid, true ) ) );
255 }
256 emit changed();
257}
@ Millimeters
Millimeters.
Definition qgis.h:5361
@ Millimeters
Millimeters.
Definition qgis.h:5341
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
~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.
friend class QgsLayout
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
QgsLayoutItem(QgsLayout *layout, bool manageZValue=true)
Constructor for QgsLayoutItem, with the specified parent layout.
@ Middle
Center of item.
friend class QgsLayoutItemMap
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 arrowRotationChanged(double newRotation)
Emitted on arrow rotation change.
NorthMode
Method for syncing rotation to a map's North direction.
const QgsLayout * layout() const
Returns the layout the object is attached to.
void changed()
Emitted when the object's properties change.
QPointer< QgsLayout > mLayout
Provides a method of storing sizes, consisting of a width and height, for use in QGIS layouts.
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
A marker symbol type, for rendering Point and MultiPoint geometries.
static std::unique_ptr< QgsMarkerSymbol > createSimple(const QVariantMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
A container for the context for various read/write operations on 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:1393
static std::unique_ptr< QgsSymbol > loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
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:6975
Contains information relating to the style entity currently being visited.