QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgscomposerpolygon.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerpolygon.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 "qgscomposerpolygon.h"
18 #include "qgscomposition.h"
19 #include "qgscomposerutils.h"
20 #include "qgssymbollayerv2utils.h"
21 #include "qgssymbolv2.h"
22 #include <limits>
23 
25  : QgsComposerNodesItem( "ComposerPolygon", c )
26  , mPolygonStyleSymbol( nullptr )
27 {
29 }
30 
32  : QgsComposerNodesItem( "ComposerPolygon", polygon, c )
33  , mPolygonStyleSymbol( nullptr )
34 {
36 }
37 
39 {
40 }
41 
42 bool QgsComposerPolygon::_addNode( const int indexPoint,
43  const QPointF &newPoint,
44  const double radius )
45 {
46  Q_UNUSED( radius );
47  mPolygon.insert( indexPoint + 1, newPoint );
48  return true;
49 }
50 
52 {
53  QgsStringMap properties;
54  properties.insert( "color", "white" );
55  properties.insert( "style", "solid" );
56  properties.insert( "style_border", "solid" );
57  properties.insert( "color_border", "black" );
58  properties.insert( "width_border", "0.3" );
59  properties.insert( "joinstyle", "miter" );
60 
62 
63  emit frameChanged();
64 }
65 
67 {
68  if ( !id().isEmpty() )
69  return id();
70 
71  return tr( "<polygon>" );
72 }
73 
75 {
76  //setup painter scaling to dots so that raster symbology is drawn to scale
77  const double dotsPerMM = painter->device()->logicalDpiX() / 25.4;
78 
80  ms.setOutputDpi( painter->device()->logicalDpiX() );
81 
83  context.setPainter( painter );
84  context.setForceVectorOutput( true );
85 
86  QScopedPointer<QgsExpressionContext> expressionContext;
87  expressionContext.reset( createExpressionContext() );
88  context.setExpressionContext( *expressionContext.data() );
89 
90  painter->scale( 1 / dotsPerMM, 1 / dotsPerMM ); // scale painter from mm to dots
91  QTransform t = QTransform::fromScale( dotsPerMM, dotsPerMM );
92 
93  QList<QPolygonF> rings; //empty
94  QPainterPath polygonPath;
95  polygonPath.addPolygon( mPolygon );
96 
97  mPolygonStyleSymbol->startRender( context );
98  mPolygonStyleSymbol->renderPolygon( polygonPath.toFillPolygon( t ), &rings,
99  nullptr, context );
100  mPolygonStyleSymbol->stopRender( context );
101  painter->scale( dotsPerMM, dotsPerMM );
102 }
103 
105 {
106  mPolygonStyleSymbol.reset( QgsSymbolLayerV2Utils::loadSymbol<QgsFillSymbolV2>( elmt ) );
107 }
108 
110 {
111  mPolygonStyleSymbol.reset( static_cast<QgsFillSymbolV2*>( symbol->clone() ) );
112  update();
113  emit frameChanged();
114 }
115 
117 {
120  doc );
121  elmt.appendChild( pe );
122 }
123 
125 {
126  if ( index < 0 || index >= mPolygon.size() )
127  return false;
128 
129  mPolygon.remove( index );
130 
131  if ( mPolygon.size() < 3 )
132  mPolygon.clear();
133  else
134  {
135  int newSelectNode = index;
136  if ( index == mPolygon.size() )
137  newSelectNode = 0;
138  setSelectedNode( newSelectNode );
139  }
140 
141  return true;
142 }
void setForceVectorOutput(bool force)
An abstract composer item that provides generic methods for nodes based shapes such as polygon or pol...
static unsigned index
QTransform fromScale(qreal sx, qreal sy)
QPolygonF mPolygon
Storage meaning for shape&#39;s nodes.
QgsComposerPolygon(QgsComposition *c)
Constructor.
QDomNode appendChild(const QDomNode &newChild)
bool _addNode(const int indexPoint, const QPointF &newPoint, const double radius) override
Add the node newPoint at the given position according to some criteres.
void scale(qreal sx, qreal sy)
static QgsFillSymbolV2 * createSimple(const QgsStringMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties. ...
void setOutputDpi(double dpi)
Set DPI used for conversion between real world units (e.g. mm) and pixels.
void insert(int i, const T &value)
static QDomElement saveSymbol(const QString &symbolName, QgsSymbolV2 *symbol, QDomDocument &doc)
bool _removeNode(const int nodeIndex) override
Method called in removeNode.
QPolygonF toFillPolygon(const QMatrix &matrix) const
virtual QgsExpressionContext * createExpressionContext() const override
Creates an expression context relating to the item&#39;s current state.
QString tr(const char *sourceText, const char *disambiguation, int n)
virtual QString displayName() const override
Overridden to return shape name.
void update(const QRectF &rect)
void setPolygonStyleSymbol(QgsFillSymbolV2 *symbol)
Set the QgsSymbolV2 used to draw the shape.
void reset(T *other)
The QgsMapSettings class contains configuration for rendering of the map.
void _writeXMLStyle(QDomDocument &doc, QDomElement &elmt) const override
Write the symbol in an XML document.
void clear()
void frameChanged()
Emitted if the item&#39;s frame style changes.
void createDefaultPolygonStyleSymbol()
Create a default symbol.
void addPolygon(const QPolygonF &polygon)
~QgsComposerPolygon()
Destructor.
void remove(int i)
QPaintDevice * device() const
void setPainter(QPainter *p)
bool setSelectedNode(const int index)
Select a node.
Graphics scene for map printing.
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
int logicalDpiX() const
T * data() const
virtual QgsFillSymbolV2 * clone() const override
QgsComposition * mComposition
Contains information about the context of a rendering operation.
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
void _draw(QPainter *painter) override
Draw nodes for the current shape.
void _readXMLStyle(const QDomElement &elmt) override
Read symbol in XML.
iterator insert(const Key &key, const T &value)
int size() const
QString id() const
Get item&#39;s id (which is not necessarly unique)
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
QScopedPointer< QgsFillSymbolV2 > mPolygonStyleSymbol
QgsSymbolV2 use to draw the shape.