QGIS API Documentation  2.99.0-Master (8ec3eaf)
qgscomposershape.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposershape.cpp
3  ----------------------
4  begin : November 2009
5  copyright : (C) 2009 by Marco Hugentobler
6  email : [email protected]
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgscomposershape.h"
19 #include "qgscomposition.h"
20 #include "qgssymbol.h"
21 #include "qgssymbollayerutils.h"
22 #include "qgscomposermodel.h"
23 #include "qgsmapsettings.h"
24 #include <QPainter>
25 
27  : QgsComposerItem( composition )
28  , mShape( Ellipse )
29  , mCornerRadius( 0 )
30  , mUseSymbol( false ) //default to not using symbol for shapes, to preserve 2.0 api
31  , mShapeStyleSymbol( nullptr )
32  , mMaxSymbolBleed( 0 )
33 {
34  setFrameEnabled( true );
35  createDefaultShapeStyleSymbol();
36 
37  if ( mComposition )
38  {
39  //connect to atlas feature changes
40  //to update symbol style (in case of data-defined symbology)
41  connect( &mComposition->atlasComposition(), SIGNAL( featureChanged( QgsFeature* ) ), this, SLOT( repaint() ) );
42  }
43 }
44 
45 QgsComposerShape::QgsComposerShape( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition )
46  : QgsComposerItem( x, y, width, height, composition )
47  , mShape( Ellipse )
48  , mCornerRadius( 0 )
49  , mUseSymbol( false ) //default to not using Symbol for shapes, to preserve 2.0 api
50  , mShapeStyleSymbol( nullptr )
51  , mMaxSymbolBleed( 0 )
52 {
53  setSceneRect( QRectF( x, y, width, height ) );
54  setFrameEnabled( true );
55  createDefaultShapeStyleSymbol();
56 
57  if ( mComposition )
58  {
59  //connect to atlas feature changes
60  //to update symbol style (in case of data-defined symbology)
61  connect( &mComposition->atlasComposition(), SIGNAL( featureChanged( QgsFeature* ) ), this, SLOT( repaint() ) );
62  }
63 }
64 
66 {
67  delete mShapeStyleSymbol;
68 }
69 
70 void QgsComposerShape::setUseSymbol( bool useSymbol )
71 {
72  mUseSymbol = useSymbol;
73  setFrameEnabled( !useSymbol );
74 }
75 
77 {
78  delete mShapeStyleSymbol;
79  mShapeStyleSymbol = static_cast<QgsFillSymbol*>( symbol->clone() );
80  refreshSymbol();
81 }
82 
84 {
85  mMaxSymbolBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( mShapeStyleSymbol );
86  updateBoundingRect();
87 
88  update();
89  emit frameChanged();
90 }
91 
92 void QgsComposerShape::createDefaultShapeStyleSymbol()
93 {
94  delete mShapeStyleSymbol;
95  QgsStringMap properties;
96  properties.insert( QStringLiteral( "color" ), QStringLiteral( "white" ) );
97  properties.insert( QStringLiteral( "style" ), QStringLiteral( "solid" ) );
98  properties.insert( QStringLiteral( "style_border" ), QStringLiteral( "solid" ) );
99  properties.insert( QStringLiteral( "color_border" ), QStringLiteral( "black" ) );
100  properties.insert( QStringLiteral( "width_border" ), QStringLiteral( "0.3" ) );
101  properties.insert( QStringLiteral( "joinstyle" ), QStringLiteral( "miter" ) );
102  mShapeStyleSymbol = QgsFillSymbol::createSimple( properties );
103 
104  mMaxSymbolBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( mShapeStyleSymbol );
105  updateBoundingRect();
106 
107  emit frameChanged();
108 }
109 
110 void QgsComposerShape::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
111 {
112  Q_UNUSED( itemStyle );
113  Q_UNUSED( pWidget );
114  if ( !painter )
115  {
116  return;
117  }
118  if ( !shouldDrawItem() )
119  {
120  return;
121  }
122 
123  drawBackground( painter );
124  drawFrame( painter );
125 
126  if ( isSelected() )
127  {
128  drawSelectionBoxes( painter );
129  }
130 }
131 
132 
133 void QgsComposerShape::drawShape( QPainter* p )
134 {
135  if ( mUseSymbol )
136  {
137  drawShapeUsingSymbol( p );
138  return;
139  }
140 
141  //draw using QPainter brush and pen to keep 2.0 api compatibility
142  p->save();
143  p->setRenderHint( QPainter::Antialiasing );
144 
145  switch ( mShape )
146  {
147  case Ellipse:
148  p->drawEllipse( QRectF( 0, 0, rect().width(), rect().height() ) );
149  break;
150  case Rectangle:
151  //if corner radius set, then draw a rounded rectangle
152  if ( mCornerRadius > 0 )
153  {
154  p->drawRoundedRect( QRectF( 0, 0, rect().width(), rect().height() ), mCornerRadius, mCornerRadius );
155  }
156  else
157  {
158  p->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
159  }
160  break;
161  case Triangle:
162  QPolygonF triangle;
163  triangle << QPointF( 0, rect().height() );
164  triangle << QPointF( rect().width(), rect().height() );
165  triangle << QPointF( rect().width() / 2.0, 0 );
166  p->drawPolygon( triangle );
167  break;
168  }
169  p->restore();
170 }
171 
172 void QgsComposerShape::drawShapeUsingSymbol( QPainter* p )
173 {
174  p->save();
175  p->setRenderHint( QPainter::Antialiasing );
176 
177  //setup painter scaling to dots so that raster symbology is drawn to scale
178  double dotsPerMM = p->device()->logicalDpiX() / 25.4;
179 
180  //setup render context
182  //context units should be in dots
183  ms.setOutputDpi( p->device()->logicalDpiX() );
185  context.setPainter( p );
186  context.setForceVectorOutput( true );
187  QgsExpressionContext expressionContext = createExpressionContext();
188  context.setExpressionContext( expressionContext );
189 
190  p->scale( 1 / dotsPerMM, 1 / dotsPerMM ); // scale painter from mm to dots
191 
192  //generate polygon to draw
193  QList<QPolygonF> rings; //empty list
194  QPolygonF shapePolygon;
195 
196  //shapes with curves must be enlarged before conversion to QPolygonF, or
197  //the curves are approximated too much and appear jaggy
198  QTransform t = QTransform::fromScale( 100, 100 );
199  //inverse transform used to scale created polygons back to expected size
200  QTransform ti = t.inverted();
201 
202  switch ( mShape )
203  {
204  case Ellipse:
205  {
206  //create an ellipse
207  QPainterPath ellipsePath;
208  ellipsePath.addEllipse( QRectF( 0, 0, rect().width() * dotsPerMM, rect().height() * dotsPerMM ) );
209  QPolygonF ellipsePoly = ellipsePath.toFillPolygon( t );
210  shapePolygon = ti.map( ellipsePoly );
211  break;
212  }
213  case Rectangle:
214  {
215  //if corner radius set, then draw a rounded rectangle
216  if ( mCornerRadius > 0 )
217  {
218  QPainterPath roundedRectPath;
219  roundedRectPath.addRoundedRect( QRectF( 0, 0, rect().width() * dotsPerMM, rect().height() * dotsPerMM ), mCornerRadius * dotsPerMM, mCornerRadius * dotsPerMM );
220  QPolygonF roundedPoly = roundedRectPath.toFillPolygon( t );
221  shapePolygon = ti.map( roundedPoly );
222  }
223  else
224  {
225  shapePolygon = QPolygonF( QRectF( 0, 0, rect().width() * dotsPerMM, rect().height() * dotsPerMM ) );
226  }
227  break;
228  }
229  case Triangle:
230  {
231  shapePolygon << QPointF( 0, rect().height() * dotsPerMM );
232  shapePolygon << QPointF( rect().width() * dotsPerMM, rect().height() * dotsPerMM );
233  shapePolygon << QPointF( rect().width() / 2.0 * dotsPerMM, 0 );
234  shapePolygon << QPointF( 0, rect().height() * dotsPerMM );
235  break;
236  }
237  }
238 
239  mShapeStyleSymbol->startRender( context );
240  mShapeStyleSymbol->renderPolygon( shapePolygon, &rings, nullptr, context );
241  mShapeStyleSymbol->stopRender( context );
242 
243  p->restore();
244 }
245 
246 
247 void QgsComposerShape::drawFrame( QPainter* p )
248 {
249  if ( mFrame && p && !mUseSymbol )
250  {
251  p->setPen( pen() );
252  p->setBrush( Qt::NoBrush );
253  p->setRenderHint( QPainter::Antialiasing, true );
254  drawShape( p );
255  }
256 }
257 
259 {
260  if ( p && ( mBackground || mUseSymbol ) )
261  {
262  p->setBrush( brush() );//this causes a problem in atlas generation
263  p->setPen( Qt::NoPen );
264  p->setRenderHint( QPainter::Antialiasing, true );
265  drawShape( p );
266  }
267 }
268 
270 {
271  return mMaxSymbolBleed;
272 }
273 
274 bool QgsComposerShape::writeXml( QDomElement& elem, QDomDocument & doc ) const
275 {
276  QDomElement composerShapeElem = doc.createElement( QStringLiteral( "ComposerShape" ) );
277  composerShapeElem.setAttribute( QStringLiteral( "shapeType" ), mShape );
278  composerShapeElem.setAttribute( QStringLiteral( "cornerRadius" ), mCornerRadius );
279 
280  QDomElement shapeStyleElem = QgsSymbolLayerUtils::saveSymbol( QString(), mShapeStyleSymbol, doc );
281  composerShapeElem.appendChild( shapeStyleElem );
282 
283  elem.appendChild( composerShapeElem );
284  return _writeXml( composerShapeElem, doc );
285 }
286 
287 bool QgsComposerShape::readXml( const QDomElement& itemElem, const QDomDocument& doc )
288 {
289  mShape = QgsComposerShape::Shape( itemElem.attribute( QStringLiteral( "shapeType" ), QStringLiteral( "0" ) ).toInt() );
290  mCornerRadius = itemElem.attribute( QStringLiteral( "cornerRadius" ), QStringLiteral( "0" ) ).toDouble();
291 
292  //restore general composer item properties
293  QDomNodeList composerItemList = itemElem.elementsByTagName( QStringLiteral( "ComposerItem" ) );
294  if ( !composerItemList.isEmpty() )
295  {
296  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
297 
298  //rotation
299  if ( !qgsDoubleNear( composerItemElem.attribute( QStringLiteral( "rotation" ), QStringLiteral( "0" ) ).toDouble(), 0.0 ) )
300  {
301  //check for old (pre 2.1) rotation attribute
302  setItemRotation( composerItemElem.attribute( QStringLiteral( "rotation" ), QStringLiteral( "0" ) ).toDouble() );
303  }
304 
305  _readXml( composerItemElem, doc );
306  }
307 
308  QDomElement shapeStyleSymbolElem = itemElem.firstChildElement( QStringLiteral( "symbol" ) );
309  if ( !shapeStyleSymbolElem.isNull() )
310  {
311  delete mShapeStyleSymbol;
312  mShapeStyleSymbol = QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( shapeStyleSymbolElem );
313  }
314  else
315  {
316  //upgrade project file from 2.0 to use symbol styling
317  delete mShapeStyleSymbol;
318  QgsStringMap properties;
319  properties.insert( QStringLiteral( "color" ), QgsSymbolLayerUtils::encodeColor( brush().color() ) );
320  if ( hasBackground() )
321  {
322  properties.insert( QStringLiteral( "style" ), QStringLiteral( "solid" ) );
323  }
324  else
325  {
326  properties.insert( QStringLiteral( "style" ), QStringLiteral( "no" ) );
327  }
328  if ( hasFrame() )
329  {
330  properties.insert( QStringLiteral( "style_border" ), QStringLiteral( "solid" ) );
331  }
332  else
333  {
334  properties.insert( QStringLiteral( "style_border" ), QStringLiteral( "no" ) );
335  }
336  properties.insert( QStringLiteral( "color_border" ), QgsSymbolLayerUtils::encodeColor( pen().color() ) );
337  properties.insert( QStringLiteral( "width_border" ), QString::number( pen().widthF() ) );
338 
339  //for pre 2.0 projects, shape color and outline were specified in a different element...
340  QDomNodeList outlineColorList = itemElem.elementsByTagName( QStringLiteral( "OutlineColor" ) );
341  if ( !outlineColorList.isEmpty() )
342  {
343  QDomElement frameColorElem = outlineColorList.at( 0 ).toElement();
344  bool redOk, greenOk, blueOk, alphaOk, widthOk;
345  int penRed, penGreen, penBlue, penAlpha;
346  double penWidth;
347 
348  penWidth = itemElem.attribute( QStringLiteral( "outlineWidth" ) ).toDouble( &widthOk );
349  penRed = frameColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
350  penGreen = frameColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
351  penBlue = frameColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
352  penAlpha = frameColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
353 
354  if ( redOk && greenOk && blueOk && alphaOk && widthOk )
355  {
356  properties.insert( QStringLiteral( "color_border" ), QgsSymbolLayerUtils::encodeColor( QColor( penRed, penGreen, penBlue, penAlpha ) ) );
357  properties.insert( QStringLiteral( "width_border" ), QString::number( penWidth ) );
358  }
359  }
360  QDomNodeList fillColorList = itemElem.elementsByTagName( QStringLiteral( "FillColor" ) );
361  if ( !fillColorList.isEmpty() )
362  {
363  QDomElement fillColorElem = fillColorList.at( 0 ).toElement();
364  bool redOk, greenOk, blueOk, alphaOk;
365  int fillRed, fillGreen, fillBlue, fillAlpha;
366 
367  fillRed = fillColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
368  fillGreen = fillColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
369  fillBlue = fillColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
370  fillAlpha = fillColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
371 
372  if ( redOk && greenOk && blueOk && alphaOk )
373  {
374  properties.insert( QStringLiteral( "color" ), QgsSymbolLayerUtils::encodeColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) ) );
375  properties.insert( QStringLiteral( "style" ), QStringLiteral( "solid" ) );
376  }
377  }
378  if ( itemElem.hasAttribute( QStringLiteral( "transparentFill" ) ) )
379  {
380  //old style (pre 2.0) of specifying that shapes had no fill
381  bool hasOldTransparentFill = itemElem.attribute( QStringLiteral( "transparentFill" ), QStringLiteral( "0" ) ).toInt();
382  if ( hasOldTransparentFill )
383  {
384  properties.insert( QStringLiteral( "style" ), QStringLiteral( "no" ) );
385  }
386  }
387 
388  mShapeStyleSymbol = QgsFillSymbol::createSimple( properties );
389  }
390  emit itemChanged();
391  return true;
392 }
393 
395 {
396  if ( s == mShape )
397  {
398  return;
399  }
400 
401  mShape = s;
402 
403  if ( mComposition && id().isEmpty() )
404  {
405  //notify the model that the display name has changed
407  }
408 }
409 
411 {
412  mCornerRadius = radius;
413 }
414 
416 {
417  return mCurrentRectangle;
418 }
419 
420 void QgsComposerShape::updateBoundingRect()
421 {
422  QRectF rectangle = rect();
423  rectangle.adjust( -mMaxSymbolBleed, -mMaxSymbolBleed, mMaxSymbolBleed, mMaxSymbolBleed );
424  if ( rectangle != mCurrentRectangle )
425  {
426  prepareGeometryChange();
427  mCurrentRectangle = rectangle;
428  }
429 }
430 
431 void QgsComposerShape::setSceneRect( const QRectF& rectangle )
432 {
433  // Reimplemented from QgsComposerItem as we need to call updateBoundingRect after the shape's size changes
434 
435  //update rect for data defined size and position
436  QRectF evaluatedRect = evalItemRect( rectangle );
437  QgsComposerItem::setSceneRect( evaluatedRect );
438 
439  updateBoundingRect();
440  update();
441 }
442 
444 {
445  if ( !id().isEmpty() )
446  {
447  return id();
448  }
449 
450  switch ( mShape )
451  {
452  case Ellipse:
453  return tr( "<ellipse>" );
454  case Rectangle:
455  return tr( "<rectangle>" );
456  case Triangle:
457  return tr( "<triangle>" );
458  }
459 
460  return tr( "<shape>" );
461 }
void setForceVectorOutput(bool force)
void setShapeType(QgsComposerShape::Shape s)
void setSceneRect(const QRectF &rectangle) override
Sets new scene rectangle bounds and recalculates hight and extent.
virtual QString displayName() const override
Get item display name.
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
static QDomElement saveSymbol(const QString &symbolName, QgsSymbol *symbol, QDomDocument &doc)
bool writeXml(QDomElement &elem, QDomDocument &doc) const override
Stores state in Dom element.
QgsComposerModel * itemsModel()
Returns the items model attached to the composition.
void itemChanged()
Emitted when the item changes.
static QgsFillSymbol * createSimple(const QgsStringMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties. ...
Definition: qgssymbol.cpp:1066
A item that forms part of a map composition.
void setOutputDpi(double dpi)
Set DPI used for conversion between real world units (e.g. mm) and pixels.
QRectF evalItemRect(const QRectF &newRect, const bool resizeOnly=false, const QgsExpressionContext *context=nullptr)
Evaluates an item&#39;s bounding rect to consider data defined position and size of item and reference po...
virtual void setFrameEnabled(const bool drawFrame)
Set whether this item has a frame drawn around it or not.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:135
QMap< QString, QString > QgsStringMap
Definition: qgis.h:328
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:196
virtual void drawBackground(QPainter *p) override
Draw background.
void updateItemDisplayName(QgsComposerItem *item)
Must be called when an item&#39;s display name is modified.
void startRender(QgsRenderContext &context, const QgsFields &fields=QgsFields())
Begins the rendering process for the symbol.
Definition: qgssymbol.cpp:387
The QgsMapSettings class contains configuration for rendering of the map.
static QString encodeColor(const QColor &color)
void setUseSymbol(bool useSymbol)
Controls whether the shape should be drawn using a QgsFillSymbol.
void frameChanged()
Emitted if the item&#39;s frame style changes.
bool _writeXml(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document. Usually called from writeXml methods of ...
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
void setCornerRadius(double radius)
Sets radius for rounded rectangle corners. Added in v2.1.
void renderPolygon(const QPolygonF &points, QList< QPolygonF > *rings, const QgsFeature *f, QgsRenderContext &context, int layer=-1, bool selected=false)
Definition: qgssymbol.cpp:1732
virtual QgsFillSymbol * clone() const override
Get a deep copy of this symbol.
Definition: qgssymbol.cpp:1835
bool mFrame
True if item fram needs to be painted.
virtual void drawFrame(QPainter *p) override
Draw black frame around item.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setShapeStyleSymbol(QgsFillSymbol *symbol)
Sets the QgsFillSymbol used to draw the shape.
void setPainter(QPainter *p)
bool hasBackground() const
Whether this item has a Background or not.
void repaint() override
bool readXml(const QDomElement &itemElem, const QDomDocument &doc) override
Sets state from Dom document.
bool _readXml(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document. Usually called from readXml methods of su...
Graphics scene for map printing.
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
static double estimateMaxSymbolBleed(QgsSymbol *symbol)
Returns the maximum estimated bleed for the symbol.
void refreshSymbol()
Should be called after the shape&#39;s symbol is changed.
virtual QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the item&#39;s current state.
QgsComposition * mComposition
Contains information about the context of a rendering operation.
const QgsComposition * composition() const
Returns the composition the item is attached to.
virtual void setItemRotation(const double r, const bool adjustPosition=false)
Sets the item rotation.
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
virtual void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Reimplementation of QCanvasItem::paint - draw on canvas.
QgsAtlasComposition & atlasComposition()
bool hasFrame() const
Whether this item has a frame or not.
bool mBackground
True if item background needs to be painted.
void stopRender(QgsRenderContext &context)
Ends the rendering process.
Definition: qgssymbol.cpp:408
QString id() const
Get item&#39;s id (which is not necessarly unique)
virtual double estimatedFrameBleed() const override
Reimplement estimatedFrameBleed, since frames on shapes are drawn using symbology rather than the ite...
QRectF boundingRect() const override
Depending on the symbol style, the bounding rectangle can be larger than the shape.
QgsComposerShape(QgsComposition *composition)
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.