QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 : marco@hugis.net
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 <QPainter>
20 
21 QgsComposerShape::QgsComposerShape( QgsComposition* composition ): QgsComposerItem( composition ), mShape( Ellipse )
22 {
23  setFrameEnabled( true );
24 }
25 
26 QgsComposerShape::QgsComposerShape( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition ): QgsComposerItem( x, y, width, height, composition ), mShape( Ellipse )
27 {
28  setSceneRect( QRectF( x, y, width, height ) );
29  setFrameEnabled( true );
30 }
31 
33 {
34 
35 }
36 
37 void QgsComposerShape::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
38 {
39  Q_UNUSED( itemStyle );
40  Q_UNUSED( pWidget );
41  if ( !painter )
42  {
43  return;
44  }
45  drawBackground( painter );
46  drawFrame( painter );
47 
48  if ( isSelected() )
49  {
50  drawSelectionBoxes( painter );
51  }
52 }
53 
54 
55 void QgsComposerShape::drawShape( QPainter* p )
56 {
57 
58  p->save();
59  p->setRenderHint( QPainter::Antialiasing );
60 
61  p->translate( rect().width() / 2.0, rect().height() / 2.0 );
62  p->rotate( mRotation );
63  p->translate( -rect().width() / 2.0, -rect().height() / 2.0 );
64 
65  switch ( mShape )
66  {
67  case Ellipse:
68  p->drawEllipse( QRectF( 0, 0 , rect().width(), rect().height() ) );
69  break;
70  case Rectangle:
71  p->drawRect( QRectF( 0, 0 , rect().width(), rect().height() ) );
72  break;
73  case Triangle:
74  QPolygonF triangle;
75  triangle << QPointF( 0, rect().height() );
76  triangle << QPointF( rect().width() , rect().height() );
77  triangle << QPointF( rect().width() / 2.0, 0 );
78  p->drawPolygon( triangle );
79  break;
80  }
81  p->restore();
82 
83 }
84 
85 
86 void QgsComposerShape::drawFrame( QPainter* p )
87 {
88  if ( mFrame && p )
89  {
90  p->setPen( pen() );
91  p->setBrush( Qt::NoBrush );
92  p->setRenderHint( QPainter::Antialiasing, true );
93  drawShape( p );
94  }
95 }
96 
98 {
99  if ( mBackground && p )
100  {
101  p->setBrush( brush() );//this causes a problem in atlas generation
102  p->setPen( Qt::NoPen );
103  p->setRenderHint( QPainter::Antialiasing, true );
104  drawShape( p );
105  }
106 }
107 
108 
109 bool QgsComposerShape::writeXML( QDomElement& elem, QDomDocument & doc ) const
110 {
111  QDomElement composerShapeElem = doc.createElement( "ComposerShape" );
112  composerShapeElem.setAttribute( "shapeType", mShape );
113  elem.appendChild( composerShapeElem );
114  return _writeXML( composerShapeElem, doc );
115 }
116 
117 bool QgsComposerShape::readXML( const QDomElement& itemElem, const QDomDocument& doc )
118 {
119  mShape = QgsComposerShape::Shape( itemElem.attribute( "shapeType", "0" ).toInt() );
120 
121  //restore general composer item properties
122  QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
123  if ( composerItemList.size() > 0 )
124  {
125  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
126  _readXML( composerItemElem, doc );
127  }
128  emit itemChanged();
129  return true;
130 }
131 
132 
134 {
135  //adapt rectangle size
136  double width = rect().width();
137  double height = rect().height();
138  sizeChangedByRotation( width, height );
139 
140  //adapt scene rect to have the same center and the new width / height
141  double x = transform().dx() + rect().width() / 2.0 - width / 2.0;
142  double y = transform().dy() + rect().height() / 2.0 - height / 2.0;
143  QgsComposerItem::setSceneRect( QRectF( x, y, width, height ) );
144 
146 }
147 
148 void QgsComposerShape::setSceneRect( const QRectF& rectangle )
149 {
150 
151 
152  //consider to change size of the shape if the rectangle changes width and/or height
153  if ( rectangle.width() != rect().width() || rectangle.height() != rect().height() )
154  {
155  double newShapeWidth = rectangle.width();
156  double newShapeHeight = rectangle.height();
157  imageSizeConsideringRotation( newShapeWidth, newShapeHeight );
158  }
159 
160  QgsComposerItem::setSceneRect( rectangle );
161 }
Shape mShape
Ellipse, rectangle or triangle.
virtual void setRotation(double r)
virtual void drawFrame(QPainter *p)
Draw black frame around item.
A item that forms part of a map composition.
double mRotation
Item rotation in degrees, clockwise.
bool writeXML(QDomElement &elem, QDomDocument &doc) const
stores state in Dom element
void itemChanged()
Used e.g.
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget)
Reimplementation of QCanvasItem::paint - draw on canvas.
virtual void drawSelectionBoxes(QPainter *p)
Draw selection boxes around item.
bool mFrame
True if item fram needs to be painted.
void drawShape(QPainter *p)
Graphics scene for map printing.
void sizeChangedByRotation(double &width, double &height)
Calculates width / height of the bounding box of a rotated rectangle (mRotation)
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
virtual void setRotation(double r)
Sets item rotation and resizes item bounds such that the shape always has the same size...
virtual void drawBackground(QPainter *p)
Draw background.
bool readXML(const QDomElement &itemElem, const QDomDocument &doc)
sets state from Dom document
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...
bool imageSizeConsideringRotation(double &width, double &height) const
Calculates width and hight of the picture (in mm) such that it fits into the item frame with the give...
void setFrameEnabled(bool drawFrame)
Set whether this item has a frame drawn around it or not.
bool mBackground
True if item background needs to be painted.
QgsComposerShape(QgsComposition *composition)
void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...