QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
17 #include "qgslayoutitempolygon.h"
18 #include "qgslayoutitemregistry.h"
19 #include "qgslayoututils.h"
20 #include "qgslayout.h"
21 #include "qgspathresolver.h"
22 #include "qgsreadwritecontext.h"
23 #include "qgssymbollayerutils.h"
24 #include "qgssymbol.h"
25 #include "qgsmapsettings.h"
26 #include "qgsstyleentityvisitor.h"
27 
28 #include <limits>
29 
31  : QgsLayoutNodesItem( layout )
32 {
33  createDefaultPolygonStyleSymbol();
34 }
35 
36 QgsLayoutItemPolygon::QgsLayoutItemPolygon( const QPolygonF &polygon, QgsLayout *layout )
37  : QgsLayoutNodesItem( polygon, layout )
38 {
39  createDefaultPolygonStyleSymbol();
40 }
41 
43 {
44  return new QgsLayoutItemPolygon( layout );
45 }
46 
48 {
50 }
51 
53 {
54  return QgsApplication::getThemeIcon( QStringLiteral( "/mLayoutItemPolygon.svg" ) );
55 }
56 
57 bool QgsLayoutItemPolygon::_addNode( const int indexPoint,
58  QPointF newPoint,
59  const double radius )
60 {
61  Q_UNUSED( radius )
62  mPolygon.insert( indexPoint + 1, newPoint );
63  return true;
64 }
65 
66 void QgsLayoutItemPolygon::createDefaultPolygonStyleSymbol()
67 {
68  QgsStringMap properties;
69  properties.insert( QStringLiteral( "color" ), QStringLiteral( "white" ) );
70  properties.insert( QStringLiteral( "style" ), QStringLiteral( "solid" ) );
71  properties.insert( QStringLiteral( "style_border" ), QStringLiteral( "solid" ) );
72  properties.insert( QStringLiteral( "color_border" ), QStringLiteral( "black" ) );
73  properties.insert( QStringLiteral( "width_border" ), QStringLiteral( "0.3" ) );
74  properties.insert( QStringLiteral( "joinstyle" ), QStringLiteral( "miter" ) );
75 
76  mPolygonStyleSymbol.reset( QgsFillSymbol::createSimple( properties ) );
77 
78  refreshSymbol();
79 }
80 
81 void QgsLayoutItemPolygon::refreshSymbol()
82 {
83  if ( layout() )
84  {
85  QgsRenderContext rc = QgsLayoutUtils::createRenderContextForLayout( layout(), nullptr, layout()->renderContext().dpi() );
86  mMaxSymbolBleed = ( 25.4 / layout()->renderContext().dpi() ) * QgsSymbolLayerUtils::estimateMaxSymbolBleed( mPolygonStyleSymbol.get(), rc );
87  }
88 
90 
91  emit frameChanged();
92 }
93 
95 {
96  if ( !id().isEmpty() )
97  return id();
98 
99  return tr( "<Polygon>" );
100 }
101 
103 {
104  if ( mPolygonStyleSymbol )
105  {
106  QgsStyleSymbolEntity entity( mPolygonStyleSymbol.get() );
107  if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, uuid(), displayName() ) ) )
108  return false;
109  }
110 
111  return true;
112 }
113 
114 void QgsLayoutItemPolygon::_draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem * )
115 {
116  //setup painter scaling to dots so that raster symbology is drawn to scale
118  QTransform t = QTransform::fromScale( scale, scale );
119 
120  QVector<QPolygonF> rings; //empty
121  QPainterPath polygonPath;
122  polygonPath.addPolygon( mPolygon );
123 
124  mPolygonStyleSymbol->startRender( context.renderContext() );
125  mPolygonStyleSymbol->renderPolygon( polygonPath.toFillPolygon( t ), &rings,
126  nullptr, context.renderContext() );
127  mPolygonStyleSymbol->stopRender( context.renderContext() );
128 }
129 
130 void QgsLayoutItemPolygon::_readXmlStyle( const QDomElement &elmt, const QgsReadWriteContext &context )
131 {
132  mPolygonStyleSymbol.reset( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( elmt, context ) );
133 }
134 
136 {
137  mPolygonStyleSymbol.reset( static_cast<QgsFillSymbol *>( symbol->clone() ) );
138  refreshSymbol();
139 }
140 
141 void QgsLayoutItemPolygon::_writeXmlStyle( QDomDocument &doc, QDomElement &elmt, const QgsReadWriteContext &context ) const
142 {
143  const QDomElement pe = QgsSymbolLayerUtils::saveSymbol( QString(),
144  mPolygonStyleSymbol.get(),
145  doc,
146  context );
147  elmt.appendChild( pe );
148 }
149 
150 bool QgsLayoutItemPolygon::_removeNode( const int index )
151 {
152  if ( index < 0 || index >= mPolygon.size() )
153  return false;
154 
155  mPolygon.remove( index );
156 
157  if ( mPolygon.size() < 3 )
158  mPolygon.clear();
159  else
160  {
161  int newSelectNode = index;
162  if ( index == mPolygon.size() )
163  newSelectNode = 0;
164  setSelectedNode( newSelectNode );
165  }
166 
167  return true;
168 }
QgsLayoutItemRegistry::LayoutPolygon
@ LayoutPolygon
Polygon shape item.
Definition: qgslayoutitemregistry.h:322
QgsLayoutItem::id
QString id() const
Returns the item's ID name.
Definition: qgslayoutitem.h:354
QgsLayoutObject::layout
const QgsLayout * layout() const
Returns the layout the object is attached to.
Definition: qgslayoutobject.cpp:118
QgsLayoutItemPolygon::setSymbol
void setSymbol(QgsFillSymbol *symbol)
Sets the symbol used to draw the shape.
Definition: qgslayoutitempolygon.cpp:135
qgslayoutitempolygon.h
QgsRenderContext::convertToPainterUnits
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
Definition: qgsrendercontext.cpp:287
QgsLayoutNodesItem::mMaxSymbolBleed
double mMaxSymbolBleed
Max symbol bleed.
Definition: qgslayoutitemnodeitem.h:150
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:605
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
QgsLayoutItemPolygon::symbol
QgsFillSymbol * symbol()
Returns the fill symbol used to draw the shape.
Definition: qgslayoutitempolygon.h:75
QgsStyleSymbolEntity
Definition: qgsstyle.h:1134
QgsLayoutItemPolygon::_readXmlStyle
void _readXmlStyle(const QDomElement &elmt, const QgsReadWriteContext &context) override
Method called in readXml.
Definition: qgslayoutitempolygon.cpp:130
QgsLayout::renderContext
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout's render context, which stores information relating to the current ...
Definition: qgslayout.cpp:358
qgsreadwritecontext.h
qgssymbollayerutils.h
QgsLayoutItemRenderContext
Definition: qgslayoutitem.h:44
qgspathresolver.h
QgsRenderContext
Definition: qgsrendercontext.h:57
QgsStyleEntityVisitorInterface
Definition: qgsstyleentityvisitor.h:33
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:168
QgsLayoutNodesItem::setSelectedNode
bool setSelectedNode(int index)
Selects a node by index.
Definition: qgslayoutitemnodeitem.cpp:310
qgsmapsettings.h
QgsStyleEntityVisitorInterface::StyleLeaf
Contains information relating to the style entity currently being visited.
Definition: qgsstyleentityvisitor.h:60
QgsLayoutNodesItem::updateSceneRect
void updateSceneRect()
Update the current scene rectangle for this item.
Definition: qgslayoutitemnodeitem.cpp:323
QgsLayoutNodesItem::mPolygon
QPolygonF mPolygon
Shape's nodes.
Definition: qgslayoutitemnodeitem.h:147
qgslayoututils.h
QgsLayoutItemPolygon::QgsLayoutItemPolygon
QgsLayoutItemPolygon(QgsLayout *layout)
Constructor for QgsLayoutItemPolygon for the specified layout.
Definition: qgslayoutitempolygon.cpp:30
QgsLayoutUtils::createRenderContextForLayout
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
Definition: qgslayoututils.cpp:138
QgsLayoutItemPolygon::displayName
QString displayName() const override
Gets item display name.
Definition: qgslayoutitempolygon.cpp:94
QgsLayoutItem::frameChanged
void frameChanged()
Emitted if the item's frame style changes.
QgsLayoutItemPolygon::_writeXmlStyle
void _writeXmlStyle(QDomDocument &doc, QDomElement &elmt, const QgsReadWriteContext &context) const override
Method called in writeXml.
Definition: qgslayoutitempolygon.cpp:141
qgslayout.h
QgsLayoutItemRenderContext::renderContext
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
Definition: qgslayoutitem.h:72
QgsLayoutItem::uuid
virtual QString uuid() const
Returns the item identification string.
Definition: qgslayoutitem.h:340
QgsLayoutItemPolygon::_removeNode
bool _removeNode(int nodeIndex) override
Method called in removeNode.
Definition: qgslayoutitempolygon.cpp:150
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:714
QgsLayoutItemPolygon::_draw
void _draw(QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem *itemStyle=nullptr) override
Method called in paint.
Definition: qgslayoutitempolygon.cpp:114
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
QgsLayoutItemPolygon::_addNode
bool _addNode(int indexPoint, QPointF newPoint, double radius) override
Method called in addNode.
Definition: qgslayoutitempolygon.cpp:57
QgsLayoutNodesItem
Definition: qgslayoutitemnodeitem.h:29
QgsFillSymbol::clone
QgsFillSymbol * clone() const override
Returns a deep copy of this symbol.
Definition: qgssymbol.cpp:2263
QgsFillSymbol
Definition: qgssymbol.h:1212
QgsLayoutItemPolygon::accept
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
Definition: qgslayoutitempolygon.cpp:102
QgsStyleEntityVisitorInterface::visit
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
Definition: qgsstyleentityvisitor.h:153
QgsLayoutItemPolygon
Definition: qgslayoutitempolygon.h:30
QgsLayoutItemPolygon::create
static QgsLayoutItemPolygon * create(QgsLayout *layout)
Returns a new polygon item for the specified layout.
Definition: qgslayoutitempolygon.cpp:42
QgsLayoutRenderContext::dpi
double dpi() const
Returns the dpi for outputting the layout.
Definition: qgslayoutrendercontext.cpp:84
QgsFillSymbol::createSimple
static QgsFillSymbol * createSimple(const QgsStringMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
Definition: qgssymbol.cpp:1440
QgsSymbolLayerUtils::estimateMaxSymbolBleed
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
Definition: qgssymbollayerutils.cpp:820
qgslayoutitemregistry.h
qgssymbol.h
QgsSymbolLayerUtils::saveSymbol
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
Definition: qgssymbollayerutils.cpp:1180
QgsLayoutItemPolygon::icon
QIcon icon() const override
Returns the item's icon.
Definition: qgslayoutitempolygon.cpp:52
qgsstyleentityvisitor.h
QgsLayoutItemPolygon::type
int type() const override
Definition: qgslayoutitempolygon.cpp:47