QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgslayoutitempolygon.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutitempolygon.cpp
3 begin : March 2016
4 copyright : (C) 2016 Paul Blottiere, Oslandia
5 email : paul dot blottiere at oslandia dot com
6 ***************************************************************************/
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
19#include "qgslayoututils.h"
20#include "qgslayout.h"
22#include "qgsreadwritecontext.h"
23#include "qgssymbollayerutils.h"
24#include "qgssymbol.h"
26#include "qgsfillsymbol.h"
27
28#include <limits>
29
31 : QgsLayoutNodesItem( layout )
32{
33 createDefaultPolygonStyleSymbol();
34}
35
36QgsLayoutItemPolygon::QgsLayoutItemPolygon( const QPolygonF &polygon, QgsLayout *layout )
37 : QgsLayoutNodesItem( polygon, layout )
38{
39 createDefaultPolygonStyleSymbol();
40}
41
43
48
53
55{
56 return QgsApplication::getThemeIcon( QStringLiteral( "/mLayoutItemPolygon.svg" ) );
57}
58
59bool QgsLayoutItemPolygon::_addNode( const int indexPoint,
60 QPointF newPoint,
61 const double radius )
62{
63 Q_UNUSED( radius )
64 mPolygon.insert( indexPoint + 1, newPoint );
65 return true;
66}
67
68void QgsLayoutItemPolygon::createDefaultPolygonStyleSymbol()
69{
70 QVariantMap properties;
71 properties.insert( QStringLiteral( "color" ), QStringLiteral( "white" ) );
72 properties.insert( QStringLiteral( "style" ), QStringLiteral( "solid" ) );
73 properties.insert( QStringLiteral( "style_border" ), QStringLiteral( "solid" ) );
74 properties.insert( QStringLiteral( "color_border" ), QStringLiteral( "black" ) );
75 properties.insert( QStringLiteral( "width_border" ), QStringLiteral( "0.3" ) );
76 properties.insert( QStringLiteral( "joinstyle" ), QStringLiteral( "miter" ) );
77
78 mPolygonStyleSymbol.reset( QgsFillSymbol::createSimple( properties ) );
79
80 refreshSymbol();
81}
82
83void QgsLayoutItemPolygon::refreshSymbol()
84{
85 if ( auto *lLayout = layout() )
86 {
87 const QgsRenderContext rc = QgsLayoutUtils::createRenderContextForLayout( lLayout, nullptr, lLayout->renderContext().dpi() );
88 mMaxSymbolBleed = ( 25.4 / lLayout->renderContext().dpi() ) * QgsSymbolLayerUtils::estimateMaxSymbolBleed( mPolygonStyleSymbol.get(), rc );
89 }
90
92
93 emit frameChanged();
94}
95
97{
98 if ( !id().isEmpty() )
99 return id();
100
101 return tr( "<Polygon>" );
102}
103
105{
106 if ( mPolygonStyleSymbol )
107 {
108 QgsStyleSymbolEntity entity( mPolygonStyleSymbol.get() );
109 if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, uuid(), displayName() ) ) )
110 return false;
111 }
112
113 return true;
114}
115
116QgsLayoutItem::Flags QgsLayoutItemPolygon::itemFlags() const
117{
118 QgsLayoutItem::Flags flags = QgsLayoutNodesItem::itemFlags();
120 return flags;
121}
122
124{
125 QPolygonF path = mapToScene( mPolygon );
126 // ensure polygon is closed
127 if ( path.at( 0 ) != path.constLast() )
128 path << path.at( 0 );
129 return QgsGeometry::fromQPolygonF( path );
130}
131
133{
134 return mPolygonStyleSymbol.get();
135}
136
137void QgsLayoutItemPolygon::_draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem * )
138{
139 //setup painter scaling to dots so that raster symbology is drawn to scale
140 const double scale = context.renderContext().convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
141 const QTransform t = QTransform::fromScale( scale, scale );
142
143 const QVector<QPolygonF> rings; //empty
144 QPainterPath polygonPath;
145 polygonPath.addPolygon( mPolygon );
146
147 mPolygonStyleSymbol->startRender( context.renderContext() );
148 mPolygonStyleSymbol->renderPolygon( polygonPath.toFillPolygon( t ), &rings,
149 nullptr, context.renderContext() );
150 mPolygonStyleSymbol->stopRender( context.renderContext() );
151}
152
153void QgsLayoutItemPolygon::_readXmlStyle( const QDomElement &elmt, const QgsReadWriteContext &context )
154{
155 mPolygonStyleSymbol.reset( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( elmt, context ) );
156}
157
159{
160 mPolygonStyleSymbol.reset( static_cast<QgsFillSymbol *>( symbol->clone() ) );
161 refreshSymbol();
162}
163
164void QgsLayoutItemPolygon::_writeXmlStyle( QDomDocument &doc, QDomElement &elmt, const QgsReadWriteContext &context ) const
165{
166 const QDomElement pe = QgsSymbolLayerUtils::saveSymbol( QString(),
167 mPolygonStyleSymbol.get(),
168 doc,
169 context );
170 elmt.appendChild( pe );
171}
172
174{
175 if ( index < 0 || index >= mPolygon.size() )
176 return false;
177
178 mPolygon.remove( index );
179
180 if ( mPolygon.size() < 3 )
181 mPolygon.clear();
182 else
183 {
184 int newSelectNode = index;
185 if ( index == mPolygon.size() )
186 newSelectNode = 0;
187 setSelectedNode( newSelectNode );
188 }
189
190 return true;
191}
192
@ Millimeters
Millimeters.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
static QgsFillSymbol * createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
QgsFillSymbol * clone() const override
Returns a deep copy of this symbol.
A geometry is the spatial representation of a feature.
static QgsGeometry fromQPolygonF(const QPolygonF &polygon)
Construct geometry from a QPolygonF.
Layout item for node based polygon shapes.
QIcon icon() const override
Returns the item's icon.
int type() const override
static QgsLayoutItemPolygon * create(QgsLayout *layout)
Returns a new polygon item for the specified layout.
QgsLayoutItemPolygon(QgsLayout *layout)
Constructor for QgsLayoutItemPolygon for the specified layout.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
QgsGeometry clipPath() const override
Returns the clipping path generated by this item, in layout coordinates.
void setSymbol(QgsFillSymbol *symbol)
Sets the symbol used to draw the shape.
QgsFillSymbol * symbol()
Returns the fill symbol used to draw the shape.
bool _removeNode(int nodeIndex) override
Method called in removeNode.
void _draw(QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem *itemStyle=nullptr) override
Method called in paint.
~QgsLayoutItemPolygon() override
bool _addNode(int indexPoint, QPointF newPoint, double radius) override
Method called in addNode.
void _writeXmlStyle(QDomDocument &doc, QDomElement &elmt, const QgsReadWriteContext &context) const override
Method called in writeXml.
void _readXmlStyle(const QDomElement &elmt, const QgsReadWriteContext &context) override
Method called in readXml.
QgsLayoutItem::Flags itemFlags() const override
Returns the item's flags, which indicate how the item behaves.
QString displayName() const override
Gets item display name.
@ LayoutPolygon
Polygon shape item.
Contains settings and helpers relating to a render of a QgsLayoutItem.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
@ FlagProvidesClipPath
Item can act as a clipping path provider (see clipPath())
virtual QString uuid() const
Returns the item identification string.
QString id() const
Returns the item's ID name.
void frameChanged()
Emitted if the item's frame style changes.
An abstract layout item that provides generic methods for node based shapes such as polygon or polyli...
QgsLayoutItem::Flags itemFlags() const override
Returns the item's flags, which indicate how the item behaves.
double mMaxSymbolBleed
Max symbol bleed.
bool setSelectedNode(int index)
Selects a node by index.
void updateSceneRect()
Update the current scene rectangle for this item.
QPolygonF mPolygon
Shape's nodes.
const QgsLayout * layout() const
Returns the layout the object is attached to.
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:50
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).
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:1341
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
Contains information relating to the style entity currently being visited.