QGIS API Documentation  2.18.3-Las Palmas (77b8c3d)
qgscomposerpolyline.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerpolyline.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 "qgscomposerpolyline.h"
18 #include "qgscomposition.h"
19 #include "qgscomposerutils.h"
20 #include "qgssymbollayerv2utils.h"
21 #include "qgssymbolv2.h"
22 #include <limits>
23 
25  : QgsComposerNodesItem( "ComposerPolyline", c )
26  , mPolylineStyleSymbol( nullptr )
27 {
29 }
30 
32  : QgsComposerNodesItem( "ComposerPolyline", polyline, c )
33  , mPolylineStyleSymbol( nullptr )
34 {
36 }
37 
39 {
40 }
41 
42 bool QgsComposerPolyline::_addNode( const int indexPoint,
43  const QPointF &newPoint,
44  const double radius )
45 {
46  const double distStart = computeDistance( newPoint, mPolygon[0] );
47  const double distEnd = computeDistance( newPoint, mPolygon[mPolygon.size()-1] );
48 
49  if ( indexPoint == ( mPolygon.size() - 1 ) )
50  {
51  if ( distEnd < radius )
52  mPolygon.append( newPoint );
53  else if ( distStart < radius )
54  mPolygon.insert( 0, newPoint );
55  }
56  else
57  mPolygon.insert( indexPoint + 1, newPoint );
58 
59  return true;
60 }
61 
63 {
64  if ( index < 0 || index >= mPolygon.size() )
65  return false;
66 
67  mPolygon.remove( index );
68 
69  if ( mPolygon.size() < 2 )
70  mPolygon.clear();
71  else
72  {
73  int newSelectNode = index;
74  if ( index >= mPolygon.size() )
75  newSelectNode = mPolygon.size() - 1;
76  setSelectedNode( newSelectNode );
77  }
78 
79  return true;
80 }
81 
83 {
84  QgsStringMap properties;
85  properties.insert( "color", "0,0,0,255" );
86  properties.insert( "width", "0.3" );
87  properties.insert( "capstyle", "square" );
88 
90 
91  emit frameChanged();
92 }
93 
95 {
96  if ( !id().isEmpty() )
97  return id();
98 
99  return tr( "<polyline>" );
100 }
101 
103 {
104  double dotsPerMM = painter->device()->logicalDpiX() / 25.4;
105 
107  ms.setOutputDpi( painter->device()->logicalDpiX() );
108 
110  context.setPainter( painter );
111  context.setForceVectorOutput( true );
112 
113  QScopedPointer<QgsExpressionContext> expressionContext;
114  expressionContext.reset( createExpressionContext() );
115  context.setExpressionContext( *expressionContext.data() );
116 
117  painter->scale( 1 / dotsPerMM, 1 / dotsPerMM ); // scale painter from mm to dots
118  QTransform t = QTransform::fromScale( dotsPerMM, dotsPerMM );
119 
120  mPolylineStyleSymbol->startRender( context );
121  mPolylineStyleSymbol->renderPolyline( t.map( mPolygon ), nullptr, context );
122  mPolylineStyleSymbol->stopRender( context );
123  painter->scale( dotsPerMM, dotsPerMM );
124 }
125 
127 {
128  mPolylineStyleSymbol.reset( QgsSymbolLayerV2Utils::loadSymbol<QgsLineSymbolV2>( elmt ) );
129 }
130 
132 {
133  mPolylineStyleSymbol.reset( static_cast<QgsLineSymbolV2*>( symbol->clone() ) );
134  update();
135  emit frameChanged();
136 }
137 
139 {
142  doc );
143  elmt.appendChild( pe );
144 }
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.
void _readXMLStyle(const QDomElement &elmt) override
Read symbol in XML.
virtual QgsLineSymbolV2 * clone() const override
QDomNode appendChild(const QDomNode &newChild)
void append(const T &value)
virtual QString displayName() const override
Overridden to return shape name.
void setPolylineStyleSymbol(QgsLineSymbolV2 *symbol)
Set the QgsSymbolV2 used to draw the shape.
QPoint map(const QPoint &point) const
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
void scale(qreal sx, qreal sy)
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)
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)
void update(const QRectF &rect)
void reset(T *other)
The QgsMapSettings class contains configuration for rendering of the map.
void clear()
void frameChanged()
Emitted if the item&#39;s frame style changes.
static QgsLineSymbolV2 * createSimple(const QgsStringMap &properties)
Create a line symbol with one symbol layer: SimpleLine with specified properties. ...
double computeDistance(const QPointF &pt1, const QPointF &pt2) const
Compute an euclidian distance between 2 nodes.
~QgsComposerPolyline()
Destructor.
void remove(int i)
QPaintDevice * device() const
void _writeXMLStyle(QDomDocument &doc, QDomElement &elmt) const override
Write the symbol in an XML document.
void setPainter(QPainter *p)
bool setSelectedNode(const int index)
Select a node.
Graphics scene for map printing.
int logicalDpiX() const
T * data() const
QScopedPointer< QgsLineSymbolV2 > mPolylineStyleSymbol
QgsSymbolV2 use to draw the shape.
void createDefaultPolylineStyleSymbol()
Create a default symbol.
QgsComposition * mComposition
Contains information about the context of a rendering operation.
void _draw(QPainter *painter) override
Draw nodes for the current shape.
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
QgsComposerPolyline(QgsComposition *c)
Constructor.
iterator insert(const Key &key, const T &value)
bool _addNode(const int indexPoint, const QPointF &newPoint, const double radius) override
Add the node newPoint at the given position according to some criteres.
int size() const
bool _removeNode(const int nodeIndex) override
Method called in removeNode.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
QString id() const
Get item&#39;s id (which is not necessarly unique)