QGIS API Documentation  3.2.0-Bonn (bc43194)
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 <limits>
27 
29  : QgsLayoutNodesItem( layout )
30 {
31  createDefaultPolygonStyleSymbol();
32 }
33 
35  : QgsLayoutNodesItem( polygon, layout )
36 {
37  createDefaultPolygonStyleSymbol();
38 }
39 
41 {
42  return new QgsLayoutItemPolygon( layout );
43 }
44 
46 {
48 }
49 
51 {
52  return QgsApplication::getThemeIcon( QStringLiteral( "/mLayoutItemPolygon.svg" ) );
53 }
54 
55 bool QgsLayoutItemPolygon::_addNode( const int indexPoint,
56  QPointF newPoint,
57  const double radius )
58 {
59  Q_UNUSED( radius );
60  mPolygon.insert( indexPoint + 1, newPoint );
61  return true;
62 }
63 
64 void QgsLayoutItemPolygon::createDefaultPolygonStyleSymbol()
65 {
66  QgsStringMap properties;
67  properties.insert( QStringLiteral( "color" ), QStringLiteral( "white" ) );
68  properties.insert( QStringLiteral( "style" ), QStringLiteral( "solid" ) );
69  properties.insert( QStringLiteral( "style_border" ), QStringLiteral( "solid" ) );
70  properties.insert( QStringLiteral( "color_border" ), QStringLiteral( "black" ) );
71  properties.insert( QStringLiteral( "width_border" ), QStringLiteral( "0.3" ) );
72  properties.insert( QStringLiteral( "joinstyle" ), QStringLiteral( "miter" ) );
73 
74  mPolygonStyleSymbol.reset( QgsFillSymbol::createSimple( properties ) );
75 
76  refreshSymbol();
77 }
78 
79 void QgsLayoutItemPolygon::refreshSymbol()
80 {
81  if ( layout() )
82  {
83  QgsRenderContext rc = QgsLayoutUtils::createRenderContextForLayout( layout(), nullptr, layout()->renderContext().dpi() );
84  mMaxSymbolBleed = ( 25.4 / layout()->renderContext().dpi() ) * QgsSymbolLayerUtils::estimateMaxSymbolBleed( mPolygonStyleSymbol.get(), rc );
85  }
86 
88 
89  emit frameChanged();
90 }
91 
93 {
94  if ( !id().isEmpty() )
95  return id();
96 
97  return tr( "<Polygon>" );
98 }
99 
100 void QgsLayoutItemPolygon::_draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem * )
101 {
102  //setup painter scaling to dots so that raster symbology is drawn to scale
104  QTransform t = QTransform::fromScale( scale, scale );
105 
106  QList<QPolygonF> rings; //empty
107  QPainterPath polygonPath;
108  polygonPath.addPolygon( mPolygon );
109 
110  mPolygonStyleSymbol->startRender( context.renderContext() );
111  mPolygonStyleSymbol->renderPolygon( polygonPath.toFillPolygon( t ), &rings,
112  nullptr, context.renderContext() );
113  mPolygonStyleSymbol->stopRender( context.renderContext() );
114 }
115 
116 void QgsLayoutItemPolygon::_readXmlStyle( const QDomElement &elmt, const QgsReadWriteContext &context )
117 {
118  mPolygonStyleSymbol.reset( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( elmt, context ) );
119 }
120 
122 {
123  mPolygonStyleSymbol.reset( static_cast<QgsFillSymbol *>( symbol->clone() ) );
124  refreshSymbol();
125 }
126 
127 void QgsLayoutItemPolygon::_writeXmlStyle( QDomDocument &doc, QDomElement &elmt, const QgsReadWriteContext &context ) const
128 {
129  const QDomElement pe = QgsSymbolLayerUtils::saveSymbol( QString(),
130  mPolygonStyleSymbol.get(),
131  doc,
132  context );
133  elmt.appendChild( pe );
134 }
135 
136 bool QgsLayoutItemPolygon::_removeNode( const int index )
137 {
138  if ( index < 0 || index >= mPolygon.size() )
139  return false;
140 
141  mPolygon.remove( index );
142 
143  if ( mPolygon.size() < 3 )
144  mPolygon.clear();
145  else
146  {
147  int newSelectNode = index;
148  if ( index == mPolygon.size() )
149  newSelectNode = 0;
150  setSelectedNode( newSelectNode );
151  }
152 
153  return true;
154 }
The class is used as a container of context for various read/write operations on other objects...
void updateSceneRect()
Update the current scene rectangle for this item.
QgsFillSymbol * symbol()
Returns the fill symbol used to draw the shape.
static QgsLayoutItemPolygon * create(QgsLayout *layout)
Returns a new polygon item for the specified layout.
QgsLayoutItemPolygon(QgsLayout *layout)
Constructor for QgsLayoutItemPolygon for the specified layout.
An abstract layout item that provides generic methods for node based shapes such as polygon or polyli...
static QgsFillSymbol * createSimple(const QgsStringMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties. ...
Definition: qgssymbol.cpp:1109
void _writeXmlStyle(QDomDocument &doc, QDomElement &elmt, const QgsReadWriteContext &context) const override
Method called in writeXml.
double mMaxSymbolBleed
Max symbol bleed.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Layout item for node based polygon shapes.
QPolygonF mPolygon
Shape&#39;s nodes.
int type() const override
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout&#39;s render context, which stores information relating to the current ...
Definition: qgslayout.cpp:355
QMap< QString, QString > QgsStringMap
Definition: qgis.h:501
void frameChanged()
Emitted if the item&#39;s frame style changes.
void _draw(QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem *itemStyle=nullptr) override
Method called in paint.
const QgsLayout * layout() const
Returns the layout the object is attached to.
QgsFillSymbol * clone() const override
Gets a deep copy of this symbol.
Definition: qgssymbol.cpp:1866
double dpi() const
Returns the dpi for outputting the layout.
QgsRenderContext & renderContext()
Returns a reference to the context&#39;s render context.
Definition: qgslayoutitem.h:71
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
QIcon icon() const override
Returns the item&#39;s icon.
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
QString id() const
Returns the item&#39;s ID name.
bool _addNode(int indexPoint, QPointF newPoint, double radius) override
Method called in addNode.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
void _readXmlStyle(const QDomElement &elmt, const QgsReadWriteContext &context) override
Method called in readXml.
bool setSelectedNode(int index)
Selects a node by index.
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:43
void setSymbol(QgsFillSymbol *symbol)
Sets the symbol used to draw the shape.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
bool _removeNode(int nodeIndex) override
Method called in removeNode.
static QDomElement saveSymbol(const QString &symbolName, QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
QString displayName() const override
Gets item display name.