|
Quantum GIS API Documentation
master-ce49b66
|
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 setFrameEnabled( true ); 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 setFrameEnabled( true ); 00030 } 00031 00032 QgsComposerShape::~QgsComposerShape() 00033 { 00034 00035 } 00036 00037 void QgsComposerShape::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) 00038 { 00039 Q_UNUSED( itemStyle ); 00040 Q_UNUSED( pWidget ); 00041 if ( !painter ) 00042 { 00043 return; 00044 } 00045 drawBackground( painter ); 00046 drawFrame( painter ); 00047 00048 if ( isSelected() ) 00049 { 00050 drawSelectionBoxes( painter ); 00051 } 00052 } 00053 00054 00055 void QgsComposerShape::drawShape( QPainter* p ) 00056 { 00057 00058 p->save(); 00059 p->setRenderHint( QPainter::Antialiasing ); 00060 00061 p->translate( rect().width() / 2.0, rect().height() / 2.0 ); 00062 p->rotate( mRotation ); 00063 p->translate( -rect().width() / 2.0, -rect().height() / 2.0 ); 00064 00065 switch ( mShape ) 00066 { 00067 case Ellipse: 00068 p->drawEllipse( QRectF( 0, 0 , rect().width(), rect().height() ) ); 00069 break; 00070 case Rectangle: 00071 p->drawRect( QRectF( 0, 0 , rect().width(), rect().height() ) ); 00072 break; 00073 case Triangle: 00074 QPolygonF triangle; 00075 triangle << QPointF( 0, rect().height() ); 00076 triangle << QPointF( rect().width() , rect().height() ); 00077 triangle << QPointF( rect().width() / 2.0, 0 ); 00078 p->drawPolygon( triangle ); 00079 break; 00080 } 00081 p->restore(); 00082 00083 } 00084 00085 00086 void QgsComposerShape::drawFrame( QPainter* p ) 00087 { 00088 if ( mFrame && p ) 00089 { 00090 p->setPen( pen() ); 00091 p->setBrush( Qt::NoBrush ); 00092 p->setRenderHint( QPainter::Antialiasing, true ); 00093 drawShape( p ); 00094 } 00095 } 00096 00097 void QgsComposerShape::drawBackground( QPainter* p ) 00098 { 00099 if ( mBackground && p ) 00100 { 00101 p->setBrush( brush() );//this causes a problem in atlas generation 00102 p->setPen( Qt::NoPen ); 00103 p->setRenderHint( QPainter::Antialiasing, true ); 00104 drawShape( p ); 00105 } 00106 } 00107 00108 00109 bool QgsComposerShape::writeXML( QDomElement& elem, QDomDocument & doc ) const 00110 { 00111 QDomElement composerShapeElem = doc.createElement( "ComposerShape" ); 00112 composerShapeElem.setAttribute( "shapeType", mShape ); 00113 elem.appendChild( composerShapeElem ); 00114 return _writeXML( composerShapeElem, doc ); 00115 } 00116 00117 bool QgsComposerShape::readXML( const QDomElement& itemElem, const QDomDocument& doc ) 00118 { 00119 mShape = QgsComposerShape::Shape( itemElem.attribute( "shapeType", "0" ).toInt() ); 00120 00121 //restore general composer item properties 00122 QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" ); 00123 if ( composerItemList.size() > 0 ) 00124 { 00125 QDomElement composerItemElem = composerItemList.at( 0 ).toElement(); 00126 _readXML( composerItemElem, doc ); 00127 } 00128 emit itemChanged(); 00129 return true; 00130 } 00131 00132 00133 void QgsComposerShape::setRotation( double r ) 00134 { 00135 //adapt rectangle size 00136 double width = rect().width(); 00137 double height = rect().height(); 00138 sizeChangedByRotation( width, height ); 00139 00140 //adapt scene rect to have the same center and the new width / height 00141 double x = transform().dx() + rect().width() / 2.0 - width / 2.0; 00142 double y = transform().dy() + rect().height() / 2.0 - height / 2.0; 00143 QgsComposerItem::setSceneRect( QRectF( x, y, width, height ) ); 00144 00145 QgsComposerItem::setRotation( r ); 00146 } 00147 00148 void QgsComposerShape::setSceneRect( const QRectF& rectangle ) 00149 { 00150 00151 00152 //consider to change size of the shape if the rectangle changes width and/or height 00153 if ( rectangle.width() != rect().width() || rectangle.height() != rect().height() ) 00154 { 00155 double newShapeWidth = rectangle.width(); 00156 double newShapeHeight = rectangle.height(); 00157 imageSizeConsideringRotation( newShapeWidth, newShapeHeight ); 00158 } 00159 00160 QgsComposerItem::setSceneRect( rectangle ); 00161 }