Quantum GIS API Documentation  1.7.4
src/core/composer/qgscomposershape.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                          qgscomposershape.cpp
00003                          ----------------------
00004     begin                : November 2009
00005     copyright            : (C) 2009 by Marco Hugentobler
00006     email                : marco@hugis.net
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "qgscomposershape.h"
00019 #include <QPainter>
00020 
00021 QgsComposerShape::QgsComposerShape( QgsComposition* composition ): QgsComposerItem( composition ), mShape( Ellipse )
00022 {
00023   initGraphicsSettings();
00024 }
00025 
00026 QgsComposerShape::QgsComposerShape( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition ): QgsComposerItem( x, y, width, height, composition ), mShape( Ellipse )
00027 {
00028   setSceneRect( QRectF( x, y, width, height ) );
00029   mShapeWidth = width;
00030   mShapeHeight = height;
00031   initGraphicsSettings();
00032 }
00033 
00034 QgsComposerShape::~QgsComposerShape()
00035 {
00036 
00037 }
00038 
00039 void QgsComposerShape::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
00040 {
00041   if ( !painter )
00042   {
00043     return;
00044   }
00045   drawBackground( painter );
00046 
00047   painter->save();
00048   painter->setRenderHint( QPainter::Antialiasing );
00049   painter->setPen( mPen );
00050   painter->setBrush( mBrush );
00051 
00052   painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
00053   painter->rotate( mRotation );
00054   painter->translate( -mShapeWidth / 2.0, -mShapeHeight / 2.0 );
00055 
00056   double halfPenWidth = mPen.widthF() / 2.0;
00057 
00058   switch ( mShape )
00059   {
00060     case Ellipse:
00061       painter->drawEllipse( QRectF( halfPenWidth, halfPenWidth , mShapeWidth - mPen.widthF(), mShapeHeight - mPen.widthF() ) );
00062       break;
00063     case Rectangle:
00064       painter->drawRect( QRectF( halfPenWidth, halfPenWidth , mShapeWidth - mPen.widthF(), mShapeHeight - mPen.widthF() ) );
00065       break;
00066     case Triangle:
00067       QPolygonF triangle;
00068       triangle << QPointF( halfPenWidth, mShapeHeight - halfPenWidth );
00069       triangle << QPointF( mShapeWidth - halfPenWidth, mShapeHeight - halfPenWidth );
00070       triangle << QPointF( mShapeWidth / 2.0, halfPenWidth );
00071       painter->drawPolygon( triangle );
00072       break;
00073   }
00074 
00075   painter->restore();
00076 
00077   drawFrame( painter );
00078   if ( isSelected() )
00079   {
00080     drawSelectionBoxes( painter );
00081   }
00082 }
00083 
00084 bool QgsComposerShape::writeXML( QDomElement& elem, QDomDocument & doc ) const
00085 {
00086   QDomElement composerShapeElem = doc.createElement( "ComposerShape" );
00087   composerShapeElem.setAttribute( "shapeType", mShape );
00088   composerShapeElem.setAttribute( "outlineWidth", mPen.widthF() );
00089   composerShapeElem.setAttribute( "transparentFill", mBrush.style() == Qt::NoBrush );
00090   composerShapeElem.setAttribute( "shapeWidth", mShapeWidth );
00091   composerShapeElem.setAttribute( "shapeHeight", mShapeHeight );
00092   QDomElement outlineColorElem = doc.createElement( "OutlineColor" );
00093   outlineColorElem.setAttribute( "red", mPen.color().red() );
00094   outlineColorElem.setAttribute( "green", mPen.color().green() );
00095   outlineColorElem.setAttribute( "blue", mPen.color().blue() );
00096   outlineColorElem.setAttribute( "alpha", mPen.color().alpha() );
00097   composerShapeElem.appendChild( outlineColorElem );
00098   QDomElement fillColorElem = doc.createElement( "FillColor" );
00099   fillColorElem.setAttribute( "red", mBrush.color().red() );
00100   fillColorElem.setAttribute( "green", mBrush.color().green() );
00101   fillColorElem.setAttribute( "blue", mBrush.color().blue() );
00102   fillColorElem.setAttribute( "alpha", mBrush.color().alpha() );
00103   composerShapeElem.appendChild( fillColorElem );
00104   elem.appendChild( composerShapeElem );
00105   return _writeXML( composerShapeElem, doc );
00106 }
00107 
00108 bool QgsComposerShape::readXML( const QDomElement& itemElem, const QDomDocument& doc )
00109 {
00110   mShape = QgsComposerShape::Shape( itemElem.attribute( "shapeType", "0" ).toInt() );
00111   mShapeWidth = itemElem.attribute( "shapeWidth", "10" ).toDouble();
00112   mShapeHeight = itemElem.attribute( "shapeHeight", "10" ).toDouble();
00113   mPen.setWidthF( itemElem.attribute( "outlineWidth", "0.4" ).toDouble() );
00114 
00115   //transparent fill
00116   bool transparent = itemElem.attribute( "transparentFill", "1" ).toInt() == 1;
00117   if ( transparent )
00118   {
00119     mBrush.setStyle( Qt::NoBrush );
00120   }
00121   else
00122   {
00123     mBrush.setStyle( Qt::SolidPattern );
00124   }
00125 
00126   //outline color
00127   QDomNodeList outlineColorList = itemElem.elementsByTagName( "OutlineColor" );
00128   if ( outlineColorList.size() > 0 )
00129   {
00130     QDomElement outlineColorElem = outlineColorList.at( 0 ).toElement();
00131     int penRed = outlineColorElem.attribute( "red", "0" ).toInt();
00132     int penGreen = outlineColorElem.attribute( "green", "0" ).toInt();
00133     int penBlue = outlineColorElem.attribute( "blue", "0" ).toInt();
00134     int penAlpha = outlineColorElem.attribute( "alpha", "255" ).toInt();
00135     mPen.setColor( QColor( penRed, penGreen, penBlue, penAlpha ) );
00136   }
00137 
00138   //fill color
00139   QDomNodeList fillNodeList = itemElem.elementsByTagName( "FillColor" );
00140   if ( fillNodeList.size() > 0 )
00141   {
00142     QDomElement fillColorElem = fillNodeList.at( 0 ).toElement();
00143     int brushRed = fillColorElem.attribute( "red", "0" ).toInt();
00144     int brushGreen = fillColorElem.attribute( "green", "0" ).toInt();
00145     int brushBlue = fillColorElem.attribute( "blue", "0" ).toInt();
00146     int brushAlpha = fillColorElem.attribute( "alpha", "255" ).toInt();
00147     mBrush.setColor( QColor( brushRed, brushGreen, brushBlue, brushAlpha ) );
00148   }
00149 
00150 
00151   //restore general composer item properties
00152   QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
00153   if ( composerItemList.size() > 0 )
00154   {
00155     QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
00156     _readXML( composerItemElem, doc );
00157   }
00158   emit itemChanged();
00159   return true;
00160 }
00161 
00162 void QgsComposerShape::setLineWidth( double width )
00163 {
00164   mPen.setWidthF( width );
00165 }
00166 
00167 double QgsComposerShape::lineWidth() const
00168 {
00169   return mPen.widthF();
00170 }
00171 
00172 void QgsComposerShape::setOutlineColor( const QColor& color )
00173 {
00174   mPen.setColor( color );
00175 }
00176 
00177 QColor QgsComposerShape::outlineColor() const
00178 {
00179   return mPen.color();
00180 }
00181 
00182 void QgsComposerShape::setFillColor( const QColor& color )
00183 {
00184   mBrush.setColor( color );
00185 }
00186 
00187 QColor QgsComposerShape::fillColor() const
00188 {
00189   return mBrush.color();
00190 }
00191 
00192 bool QgsComposerShape::transparentFill() const
00193 {
00194   return mBrush.style() == Qt::NoBrush;
00195 }
00196 
00197 void QgsComposerShape::setTransparentFill( bool transparent )
00198 {
00199   if ( transparent )
00200   {
00201     mBrush.setStyle( Qt::NoBrush );
00202   }
00203   else
00204   {
00205     mBrush.setStyle( Qt::SolidPattern );
00206   }
00207 }
00208 
00209 void QgsComposerShape::initGraphicsSettings()
00210 {
00211   mPen.setColor( QColor( 0, 0, 0 ) );
00212   mPen.setWidthF( 1 );
00213   mPen.setJoinStyle( Qt::RoundJoin );
00214   mBrush.setColor( QColor( 0, 0, 0 ) );
00215   mBrush.setStyle( Qt::NoBrush );
00216 
00217   //set composer item brush and pen to transparent white by default
00218   setPen( QPen( QColor( 255, 255, 255, 0 ) ) );
00219   setBrush( QBrush( QColor( 255, 255, 255, 0 ) ) );
00220 }
00221 
00222 void QgsComposerShape::setRotation( double r )
00223 {
00224   //adapt rectangle size
00225   double width = mShapeWidth;
00226   double height = mShapeHeight;
00227   sizeChangedByRotation( width, height );
00228 
00229   //adapt scene rect to have the same center and the new width / height
00230   double x = transform().dx() + rect().width() / 2.0 - width / 2.0;
00231   double y = transform().dy() + rect().height() / 2.0 - height / 2.0;
00232   QgsComposerItem::setSceneRect( QRectF( x, y, width, height ) );
00233 
00234   QgsComposerItem::setRotation( r );
00235 }
00236 
00237 void QgsComposerShape::setSceneRect( const QRectF& rectangle )
00238 {
00239 
00240 
00241   //consider to change size of the shape if the rectangle changes width and/or height
00242   if ( rectangle.width() != rect().width() || rectangle.height() != rect().height() )
00243   {
00244     double newShapeWidth = rectangle.width();
00245     double newShapeHeight = rectangle.height();
00246     imageSizeConsideringRotation( newShapeWidth, newShapeHeight );
00247     mShapeWidth = newShapeWidth;
00248     mShapeHeight = newShapeHeight;
00249   }
00250 
00251   QgsComposerItem::setSceneRect( rectangle );
00252 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines