Quantum GIS API Documentation
1.8
|
00001 /*************************************************************************** 00002 qgscomposershape.cpp 00003 ---------------------- 00004 begin : November 2009 00005 copyright : (C) 2009 by Marco Hugentobler 00006 email : [email protected] 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 Q_UNUSED( itemStyle ); 00042 Q_UNUSED( pWidget ); 00043 if ( !painter ) 00044 { 00045 return; 00046 } 00047 drawBackground( painter ); 00048 00049 painter->save(); 00050 painter->setRenderHint( QPainter::Antialiasing ); 00051 painter->setPen( mPen ); 00052 painter->setBrush( mBrush ); 00053 00054 painter->translate( rect().width() / 2.0, rect().height() / 2.0 ); 00055 painter->rotate( mRotation ); 00056 painter->translate( -mShapeWidth / 2.0, -mShapeHeight / 2.0 ); 00057 00058 double halfPenWidth = mPen.widthF() / 2.0; 00059 00060 switch ( mShape ) 00061 { 00062 case Ellipse: 00063 painter->drawEllipse( QRectF( halfPenWidth, halfPenWidth , mShapeWidth - mPen.widthF(), mShapeHeight - mPen.widthF() ) ); 00064 break; 00065 case Rectangle: 00066 painter->drawRect( QRectF( halfPenWidth, halfPenWidth , mShapeWidth - mPen.widthF(), mShapeHeight - mPen.widthF() ) ); 00067 break; 00068 case Triangle: 00069 QPolygonF triangle; 00070 triangle << QPointF( halfPenWidth, mShapeHeight - halfPenWidth ); 00071 triangle << QPointF( mShapeWidth - halfPenWidth, mShapeHeight - halfPenWidth ); 00072 triangle << QPointF( mShapeWidth / 2.0, halfPenWidth ); 00073 painter->drawPolygon( triangle ); 00074 break; 00075 } 00076 00077 painter->restore(); 00078 00079 drawFrame( painter ); 00080 if ( isSelected() ) 00081 { 00082 drawSelectionBoxes( painter ); 00083 } 00084 } 00085 00086 bool QgsComposerShape::writeXML( QDomElement& elem, QDomDocument & doc ) const 00087 { 00088 QDomElement composerShapeElem = doc.createElement( "ComposerShape" ); 00089 composerShapeElem.setAttribute( "shapeType", mShape ); 00090 composerShapeElem.setAttribute( "outlineWidth", QString::number( mPen.widthF() ) ); 00091 composerShapeElem.setAttribute( "transparentFill", mBrush.style() == Qt::NoBrush ); 00092 composerShapeElem.setAttribute( "shapeWidth", QString::number( mShapeWidth ) ); 00093 composerShapeElem.setAttribute( "shapeHeight", QString::number( mShapeHeight ) ); 00094 QDomElement outlineColorElem = doc.createElement( "OutlineColor" ); 00095 outlineColorElem.setAttribute( "red", mPen.color().red() ); 00096 outlineColorElem.setAttribute( "green", mPen.color().green() ); 00097 outlineColorElem.setAttribute( "blue", mPen.color().blue() ); 00098 outlineColorElem.setAttribute( "alpha", mPen.color().alpha() ); 00099 composerShapeElem.appendChild( outlineColorElem ); 00100 QDomElement fillColorElem = doc.createElement( "FillColor" ); 00101 fillColorElem.setAttribute( "red", mBrush.color().red() ); 00102 fillColorElem.setAttribute( "green", mBrush.color().green() ); 00103 fillColorElem.setAttribute( "blue", mBrush.color().blue() ); 00104 fillColorElem.setAttribute( "alpha", mBrush.color().alpha() ); 00105 composerShapeElem.appendChild( fillColorElem ); 00106 elem.appendChild( composerShapeElem ); 00107 return _writeXML( composerShapeElem, doc ); 00108 } 00109 00110 bool QgsComposerShape::readXML( const QDomElement& itemElem, const QDomDocument& doc ) 00111 { 00112 mShape = QgsComposerShape::Shape( itemElem.attribute( "shapeType", "0" ).toInt() ); 00113 mShapeWidth = itemElem.attribute( "shapeWidth", "10" ).toDouble(); 00114 mShapeHeight = itemElem.attribute( "shapeHeight", "10" ).toDouble(); 00115 mPen.setWidthF( itemElem.attribute( "outlineWidth", "0.4" ).toDouble() ); 00116 00117 //transparent fill 00118 bool transparent = itemElem.attribute( "transparentFill", "1" ).toInt() == 1; 00119 if ( transparent ) 00120 { 00121 mBrush.setStyle( Qt::NoBrush ); 00122 } 00123 else 00124 { 00125 mBrush.setStyle( Qt::SolidPattern ); 00126 } 00127 00128 //outline color 00129 QDomNodeList outlineColorList = itemElem.elementsByTagName( "OutlineColor" ); 00130 if ( outlineColorList.size() > 0 ) 00131 { 00132 QDomElement outlineColorElem = outlineColorList.at( 0 ).toElement(); 00133 int penRed = outlineColorElem.attribute( "red", "0" ).toInt(); 00134 int penGreen = outlineColorElem.attribute( "green", "0" ).toInt(); 00135 int penBlue = outlineColorElem.attribute( "blue", "0" ).toInt(); 00136 int penAlpha = outlineColorElem.attribute( "alpha", "255" ).toInt(); 00137 mPen.setColor( QColor( penRed, penGreen, penBlue, penAlpha ) ); 00138 } 00139 00140 //fill color 00141 QDomNodeList fillNodeList = itemElem.elementsByTagName( "FillColor" ); 00142 if ( fillNodeList.size() > 0 ) 00143 { 00144 QDomElement fillColorElem = fillNodeList.at( 0 ).toElement(); 00145 int brushRed = fillColorElem.attribute( "red", "0" ).toInt(); 00146 int brushGreen = fillColorElem.attribute( "green", "0" ).toInt(); 00147 int brushBlue = fillColorElem.attribute( "blue", "0" ).toInt(); 00148 int brushAlpha = fillColorElem.attribute( "alpha", "255" ).toInt(); 00149 mBrush.setColor( QColor( brushRed, brushGreen, brushBlue, brushAlpha ) ); 00150 } 00151 00152 00153 //restore general composer item properties 00154 QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" ); 00155 if ( composerItemList.size() > 0 ) 00156 { 00157 QDomElement composerItemElem = composerItemList.at( 0 ).toElement(); 00158 _readXML( composerItemElem, doc ); 00159 } 00160 emit itemChanged(); 00161 return true; 00162 } 00163 00164 void QgsComposerShape::setLineWidth( double width ) 00165 { 00166 mPen.setWidthF( width ); 00167 } 00168 00169 double QgsComposerShape::lineWidth() const 00170 { 00171 return mPen.widthF(); 00172 } 00173 00174 void QgsComposerShape::setOutlineColor( const QColor& color ) 00175 { 00176 mPen.setColor( color ); 00177 } 00178 00179 QColor QgsComposerShape::outlineColor() const 00180 { 00181 return mPen.color(); 00182 } 00183 00184 void QgsComposerShape::setFillColor( const QColor& color ) 00185 { 00186 mBrush.setColor( color ); 00187 } 00188 00189 QColor QgsComposerShape::fillColor() const 00190 { 00191 return mBrush.color(); 00192 } 00193 00194 bool QgsComposerShape::transparentFill() const 00195 { 00196 return mBrush.style() == Qt::NoBrush; 00197 } 00198 00199 void QgsComposerShape::setTransparentFill( bool transparent ) 00200 { 00201 if ( transparent ) 00202 { 00203 mBrush.setStyle( Qt::NoBrush ); 00204 } 00205 else 00206 { 00207 mBrush.setStyle( Qt::SolidPattern ); 00208 } 00209 } 00210 00211 void QgsComposerShape::initGraphicsSettings() 00212 { 00213 mPen.setColor( QColor( 0, 0, 0 ) ); 00214 mPen.setWidthF( 1 ); 00215 mPen.setJoinStyle( Qt::RoundJoin ); 00216 mBrush.setColor( QColor( 0, 0, 0 ) ); 00217 mBrush.setStyle( Qt::NoBrush ); 00218 00219 //set composer item brush and pen to transparent white by default 00220 setPen( QPen( QColor( 255, 255, 255, 0 ) ) ); 00221 setBrush( QBrush( QColor( 255, 255, 255, 0 ) ) ); 00222 } 00223 00224 void QgsComposerShape::setRotation( double r ) 00225 { 00226 //adapt rectangle size 00227 double width = mShapeWidth; 00228 double height = mShapeHeight; 00229 sizeChangedByRotation( width, height ); 00230 00231 //adapt scene rect to have the same center and the new width / height 00232 double x = transform().dx() + rect().width() / 2.0 - width / 2.0; 00233 double y = transform().dy() + rect().height() / 2.0 - height / 2.0; 00234 QgsComposerItem::setSceneRect( QRectF( x, y, width, height ) ); 00235 00236 QgsComposerItem::setRotation( r ); 00237 } 00238 00239 void QgsComposerShape::setSceneRect( const QRectF& rectangle ) 00240 { 00241 00242 00243 //consider to change size of the shape if the rectangle changes width and/or height 00244 if ( rectangle.width() != rect().width() || rectangle.height() != rect().height() ) 00245 { 00246 double newShapeWidth = rectangle.width(); 00247 double newShapeHeight = rectangle.height(); 00248 imageSizeConsideringRotation( newShapeWidth, newShapeHeight ); 00249 mShapeWidth = newShapeWidth; 00250 mShapeHeight = newShapeHeight; 00251 } 00252 00253 QgsComposerItem::setSceneRect( rectangle ); 00254 }