00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
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
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
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
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 return true;
00159 }
00160
00161 void QgsComposerShape::setLineWidth( double width )
00162 {
00163 mPen.setWidthF( width );
00164 }
00165
00166 double QgsComposerShape::lineWidth() const
00167 {
00168 return mPen.widthF();
00169 }
00170
00171 void QgsComposerShape::setOutlineColor( const QColor& color )
00172 {
00173 mPen.setColor( color );
00174 }
00175
00176 QColor QgsComposerShape::outlineColor() const
00177 {
00178 return mPen.color();
00179 }
00180
00181 void QgsComposerShape::setFillColor( const QColor& color )
00182 {
00183 mBrush.setColor( color );
00184 }
00185
00186 QColor QgsComposerShape::fillColor() const
00187 {
00188 return mBrush.color();
00189 }
00190
00191 bool QgsComposerShape::transparentFill() const
00192 {
00193 return mBrush.style() == Qt::NoBrush;
00194 }
00195
00196 void QgsComposerShape::setTransparentFill( bool transparent )
00197 {
00198 if ( transparent )
00199 {
00200 mBrush.setStyle( Qt::NoBrush );
00201 }
00202 else
00203 {
00204 mBrush.setStyle( Qt::SolidPattern );
00205 }
00206 }
00207
00208 void QgsComposerShape::initGraphicsSettings()
00209 {
00210 mPen.setColor( QColor( 0, 0, 0 ) );
00211 mPen.setWidthF( 1 );
00212 mPen.setJoinStyle( Qt::RoundJoin );
00213 mBrush.setColor( QColor( 0, 0, 0 ) );
00214 mBrush.setStyle( Qt::NoBrush );
00215
00216
00217 setPen( QPen( QColor( 255, 255, 255, 0 ) ) );
00218 setBrush( QBrush( QColor( 255, 255, 255, 0 ) ) );
00219 }
00220
00221 void QgsComposerShape::setRotation( double r )
00222 {
00223
00224 double width = mShapeWidth;
00225 double height = mShapeHeight;
00226 sizeChangedByRotation( width, height );
00227
00228
00229 double x = transform().dx() + rect().width() / 2.0 - width / 2.0;
00230 double y = transform().dy() + rect().height() / 2.0 - height / 2.0;
00231 QgsComposerItem::setSceneRect( QRectF( x, y, width, height ) );
00232
00233 QgsComposerItem::setRotation( r );
00234 }
00235
00236 void QgsComposerShape::setSceneRect( const QRectF& rectangle )
00237 {
00238
00239
00240
00241 if ( rectangle.width() != rect().width() || rectangle.height() != rect().height() )
00242 {
00243 double newShapeWidth = rectangle.width();
00244 double newShapeHeight = rectangle.height();
00245 imageSizeConsideringRotation( newShapeWidth, newShapeHeight );
00246 mShapeWidth = newShapeWidth;
00247 mShapeHeight = newShapeHeight;
00248 }
00249
00250 QgsComposerItem::setSceneRect( rectangle );
00251 }