QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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
18
19#include <limits>
20
21#include "qgsfillsymbol.h"
22#include "qgslayout.h"
25#include "qgslayoututils.h"
26#include "qgsreadwritecontext.h"
28#include "qgssymbol.h"
29#include "qgssymbollayerutils.h"
30
31#include <QString>
32
33#include "moc_qgslayoutitempolygon.cpp"
34
35using namespace Qt::StringLiterals;
36
39{
40 createDefaultPolygonStyleSymbol();
41}
42
44 : QgsLayoutNodesItem( polygon, layout )
45{
46 createDefaultPolygonStyleSymbol();
47}
48
50
55
60
62{
63 return QgsApplication::getThemeIcon( u"/mLayoutItemPolygon.svg"_s );
64}
65
66bool QgsLayoutItemPolygon::_addNode( const int indexPoint,
67 QPointF newPoint,
68 const double radius )
69{
70 Q_UNUSED( radius )
71 mPolygon.insert( indexPoint + 1, newPoint );
72 return true;
73}
74
75void QgsLayoutItemPolygon::createDefaultPolygonStyleSymbol()
76{
77 QVariantMap properties;
78 properties.insert( u"color"_s, u"white"_s );
79 properties.insert( u"style"_s, u"solid"_s );
80 properties.insert( u"style_border"_s, u"solid"_s );
81 properties.insert( u"color_border"_s, u"black"_s );
82 properties.insert( u"width_border"_s, u"0.3"_s );
83 properties.insert( u"joinstyle"_s, u"miter"_s );
84
85 mPolygonStyleSymbol = QgsFillSymbol::createSimple( properties );
86
87 refreshSymbol();
88}
89
90void QgsLayoutItemPolygon::refreshSymbol()
91{
92 if ( auto *lLayout = layout() )
93 {
94 const QgsRenderContext rc = QgsLayoutUtils::createRenderContextForLayout( lLayout, nullptr, lLayout->renderContext().dpi() );
95 mMaxSymbolBleed = ( 25.4 / lLayout->renderContext().dpi() ) * QgsSymbolLayerUtils::estimateMaxSymbolBleed( mPolygonStyleSymbol.get(), rc );
96 }
97
99
100 emit frameChanged();
101}
102
104{
105 if ( !id().isEmpty() )
106 return id();
107
108 return tr( "<Polygon>" );
109}
110
112{
113 if ( mPolygonStyleSymbol )
114 {
115 QgsStyleSymbolEntity entity( mPolygonStyleSymbol.get() );
116 if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, uuid(), displayName() ) ) )
117 return false;
118 }
119
120 return true;
121}
122
129
131{
132 QPolygonF path = mapToScene( mPolygon );
133 // ensure polygon is closed
134 if ( path.at( 0 ) != path.constLast() )
135 path << path.at( 0 );
136 return QgsGeometry::fromQPolygonF( path );
137}
138
139
141{
142 // A Polygon is valid if it has at least 3 unique points
143 QList<QPointF> uniquePoints;
144 int seen = 0;
145 for ( QPointF point : mPolygon )
146 {
147 if ( !uniquePoints.contains( point ) )
148 {
149 uniquePoints.append( point );
150 if ( ++seen > 2 )
151 return true;
152 }
153 }
154 return false;
155}
156
158{
159 return mPolygonStyleSymbol.get();
160}
161
162void QgsLayoutItemPolygon::_draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem * )
163{
164 QgsRenderContext renderContext = context.renderContext();
165 // symbol clipping messes with geometry generators used in the symbol for this item, and has no
166 // valid use here. See https://github.com/qgis/QGIS/issues/58909
168
169 //setup painter scaling to dots so that raster symbology is drawn to scale
170 const double scale = renderContext.convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
171 const QTransform t = QTransform::fromScale( scale, scale );
172
173 const QVector<QPolygonF> rings; //empty
174 QPainterPath polygonPath;
175 polygonPath.addPolygon( mPolygon );
176
177 mPolygonStyleSymbol->startRender( renderContext );
178 mPolygonStyleSymbol->renderPolygon( polygonPath.toFillPolygon( t ), &rings,
179 nullptr, renderContext );
180 mPolygonStyleSymbol->stopRender( renderContext );
181}
182
183void QgsLayoutItemPolygon::_readXmlStyle( const QDomElement &elmt, const QgsReadWriteContext &context )
184{
185 mPolygonStyleSymbol = QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( elmt, context );
186}
187
189{
190 mPolygonStyleSymbol.reset( static_cast<QgsFillSymbol *>( symbol->clone() ) );
191 refreshSymbol();
192}
193
194void QgsLayoutItemPolygon::_writeXmlStyle( QDomDocument &doc, QDomElement &elmt, const QgsReadWriteContext &context ) const
195{
196 const QDomElement pe = QgsSymbolLayerUtils::saveSymbol( QString(),
197 mPolygonStyleSymbol.get(),
198 doc,
199 context );
200 elmt.appendChild( pe );
201}
202
204{
205 if ( index < 0 || index >= mPolygon.size() || mPolygon.size() <= 3 )
206 return false;
207
208 mPolygon.remove( index );
209
210 int newSelectNode = index;
211 if ( index == mPolygon.size() )
212 newSelectNode = 0;
213 setSelectedNode( newSelectNode );
214
215 return true;
216}
217
@ Millimeters
Millimeters.
Definition qgis.h:5280
@ DisableSymbolClippingToExtent
Force symbol clipping to map extent to be disabled in all situations. This will result in slower rend...
Definition qgis.h:2828
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 std::unique_ptr< QgsFillSymbol > createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
A geometry is the spatial representation of a feature.
static QgsGeometry fromQPolygonF(const QPolygonF &polygon)
Construct geometry from a QPolygonF.
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.
bool isValid() const override
Must be reimplemented in subclasses.
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.
friend class QgsLayout
@ 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.
QFlags< Flag > Flags
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.
QgsLayoutNodesItem(QgsLayout *layout)
Constructor for QgsLayoutNodesItem, attached to the specified layout.
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.
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).
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
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:1398
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.
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.