Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 {
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  mShapeWidth = width;
30  mShapeHeight = height;
32 }
33 
35 {
36 
37 }
38 
39 void QgsComposerShape::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
40 {
41  if ( !painter )
42  {
43  return;
44  }
45  drawBackground( painter );
46 
47  painter->save();
48  painter->setRenderHint( QPainter::Antialiasing );
49  painter->setPen( mPen );
50  painter->setBrush( mBrush );
51 
52  painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
53  painter->rotate( mRotation );
54  painter->translate( -mShapeWidth / 2.0, -mShapeHeight / 2.0 );
55 
56  double halfPenWidth = mPen.widthF() / 2.0;
57 
58  switch ( mShape )
59  {
60  case Ellipse:
61  painter->drawEllipse( QRectF( halfPenWidth, halfPenWidth , mShapeWidth - mPen.widthF(), mShapeHeight - mPen.widthF() ) );
62  break;
63  case Rectangle:
64  painter->drawRect( QRectF( halfPenWidth, halfPenWidth , mShapeWidth - mPen.widthF(), mShapeHeight - mPen.widthF() ) );
65  break;
66  case Triangle:
67  QPolygonF triangle;
68  triangle << QPointF( halfPenWidth, mShapeHeight - halfPenWidth );
69  triangle << QPointF( mShapeWidth - halfPenWidth, mShapeHeight - halfPenWidth );
70  triangle << QPointF( mShapeWidth / 2.0, halfPenWidth );
71  painter->drawPolygon( triangle );
72  break;
73  }
74 
75  painter->restore();
76 
77  drawFrame( painter );
78  if ( isSelected() )
79  {
80  drawSelectionBoxes( painter );
81  }
82 }
83 
84 bool QgsComposerShape::writeXML( QDomElement& elem, QDomDocument & doc ) const
85 {
86  QDomElement composerShapeElem = doc.createElement( "ComposerShape" );
87  composerShapeElem.setAttribute( "shapeType", mShape );
88  composerShapeElem.setAttribute( "outlineWidth", mPen.widthF() );
89  composerShapeElem.setAttribute( "transparentFill", mBrush.style() == Qt::NoBrush );
90  composerShapeElem.setAttribute( "shapeWidth", mShapeWidth );
91  composerShapeElem.setAttribute( "shapeHeight", mShapeHeight );
92  QDomElement outlineColorElem = doc.createElement( "OutlineColor" );
93  outlineColorElem.setAttribute( "red", mPen.color().red() );
94  outlineColorElem.setAttribute( "green", mPen.color().green() );
95  outlineColorElem.setAttribute( "blue", mPen.color().blue() );
96  outlineColorElem.setAttribute( "alpha", mPen.color().alpha() );
97  composerShapeElem.appendChild( outlineColorElem );
98  QDomElement fillColorElem = doc.createElement( "FillColor" );
99  fillColorElem.setAttribute( "red", mBrush.color().red() );
100  fillColorElem.setAttribute( "green", mBrush.color().green() );
101  fillColorElem.setAttribute( "blue", mBrush.color().blue() );
102  fillColorElem.setAttribute( "alpha", mBrush.color().alpha() );
103  composerShapeElem.appendChild( fillColorElem );
104  elem.appendChild( composerShapeElem );
105  return _writeXML( composerShapeElem, doc );
106 }
107 
108 bool QgsComposerShape::readXML( const QDomElement& itemElem, const QDomDocument& doc )
109 {
110  mShape = QgsComposerShape::Shape( itemElem.attribute( "shapeType", "0" ).toInt() );
111  mShapeWidth = itemElem.attribute( "shapeWidth", "10" ).toDouble();
112  mShapeHeight = itemElem.attribute( "shapeHeight", "10" ).toDouble();
113  mPen.setWidthF( itemElem.attribute( "outlineWidth", "0.4" ).toDouble() );
114 
115  //transparent fill
116  bool transparent = itemElem.attribute( "transparentFill", "1" ).toInt() == 1;
117  if ( transparent )
118  {
119  mBrush.setStyle( Qt::NoBrush );
120  }
121  else
122  {
123  mBrush.setStyle( Qt::SolidPattern );
124  }
125 
126  //outline color
127  QDomNodeList outlineColorList = itemElem.elementsByTagName( "OutlineColor" );
128  if ( outlineColorList.size() > 0 )
129  {
130  QDomElement outlineColorElem = outlineColorList.at( 0 ).toElement();
131  int penRed = outlineColorElem.attribute( "red", "0" ).toInt();
132  int penGreen = outlineColorElem.attribute( "green", "0" ).toInt();
133  int penBlue = outlineColorElem.attribute( "blue", "0" ).toInt();
134  int penAlpha = outlineColorElem.attribute( "alpha", "255" ).toInt();
135  mPen.setColor( QColor( penRed, penGreen, penBlue, penAlpha ) );
136  }
137 
138  //fill color
139  QDomNodeList fillNodeList = itemElem.elementsByTagName( "FillColor" );
140  if ( fillNodeList.size() > 0 )
141  {
142  QDomElement fillColorElem = fillNodeList.at( 0 ).toElement();
143  int brushRed = fillColorElem.attribute( "red", "0" ).toInt();
144  int brushGreen = fillColorElem.attribute( "green", "0" ).toInt();
145  int brushBlue = fillColorElem.attribute( "blue", "0" ).toInt();
146  int brushAlpha = fillColorElem.attribute( "alpha", "255" ).toInt();
147  mBrush.setColor( QColor( brushRed, brushGreen, brushBlue, brushAlpha ) );
148  }
149 
150 
151  //restore general composer item properties
152  QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
153  if ( composerItemList.size() > 0 )
154  {
155  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
156  _readXML( composerItemElem, doc );
157  }
158  emit itemChanged();
159  return true;
160 }
161 
162 void QgsComposerShape::setLineWidth( double width )
163 {
164  mPen.setWidthF( width );
165 }
166 
168 {
169  return mPen.widthF();
170 }
171 
172 void QgsComposerShape::setOutlineColor( const QColor& color )
173 {
174  mPen.setColor( color );
175 }
176 
178 {
179  return mPen.color();
180 }
181 
182 void QgsComposerShape::setFillColor( const QColor& color )
183 {
184  mBrush.setColor( color );
185 }
186 
188 {
189  return mBrush.color();
190 }
191 
193 {
194  return mBrush.style() == Qt::NoBrush;
195 }
196 
197 void QgsComposerShape::setTransparentFill( bool transparent )
198 {
199  if ( transparent )
200  {
201  mBrush.setStyle( Qt::NoBrush );
202  }
203  else
204  {
205  mBrush.setStyle( Qt::SolidPattern );
206  }
207 }
208 
210 {
211  mPen.setColor( QColor( 0, 0, 0 ) );
212  mPen.setWidthF( 1 );
213  mPen.setJoinStyle( Qt::RoundJoin );
214  mBrush.setColor( QColor( 0, 0, 0 ) );
215  mBrush.setStyle( Qt::NoBrush );
216 
217  //set composer item brush and pen to transparent white by default
218  setPen( QPen( QColor( 255, 255, 255, 0 ) ) );
219  setBrush( QBrush( QColor( 255, 255, 255, 0 ) ) );
220 }
221 
223 {
224  //adapt rectangle size
225  double width = mShapeWidth;
226  double height = mShapeHeight;
227  sizeChangedByRotation( width, height );
228 
229  //adapt scene rect to have the same center and the new width / height
230  double x = transform().dx() + rect().width() / 2.0 - width / 2.0;
231  double y = transform().dy() + rect().height() / 2.0 - height / 2.0;
232  QgsComposerItem::setSceneRect( QRectF( x, y, width, height ) );
233 
235 }
236 
237 void QgsComposerShape::setSceneRect( const QRectF& rectangle )
238 {
239 
240 
241  //consider to change size of the shape if the rectangle changes width and/or height
242  if ( rectangle.width() != rect().width() || rectangle.height() != rect().height() )
243  {
244  double newShapeWidth = rectangle.width();
245  double newShapeHeight = rectangle.height();
246  imageSizeConsideringRotation( newShapeWidth, newShapeHeight );
247  mShapeWidth = newShapeWidth;
248  mShapeHeight = newShapeHeight;
249  }
250 
251  QgsComposerItem::setSceneRect( rectangle );
252 }