00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "qgscomposeritemgroup.h"
00019 #include "qgscomposition.h"
00020 #include <QPen>
00021 #include <QPainter>
00022
00023 QgsComposerItemGroup::QgsComposerItemGroup( QgsComposition* c ): QgsComposerItem( c )
00024 {
00025 setZValue( 90 );
00026 show();
00027 }
00028
00029 QgsComposerItemGroup::~QgsComposerItemGroup()
00030 {
00031 QSet<QgsComposerItem*>::iterator itemIt = mItems.begin();
00032 for ( ; itemIt != mItems.end(); ++itemIt )
00033 {
00034 emit childItemDeleted( *itemIt );
00035 delete( *itemIt );
00036 }
00037 }
00038
00039 void QgsComposerItemGroup::addItem( QgsComposerItem* item )
00040 {
00041 if ( !item )
00042 {
00043 return;
00044 }
00045
00046 if ( mItems.contains( item ) )
00047 {
00048 return;
00049 }
00050 mItems.insert( item );
00051 item->setSelected( false );
00052 item->setFlag( QGraphicsItem::ItemIsSelectable, false );
00053
00054
00055 double minXItem = item->transform().dx();
00056 double minYItem = item->transform().dy();
00057 double maxXItem = minXItem + item->rect().width();
00058 double maxYItem = minYItem + item->rect().height();
00059
00060 if ( mSceneBoundingRectangle.isEmpty() )
00061 {
00062 mSceneBoundingRectangle.setLeft( minXItem );
00063 mSceneBoundingRectangle.setTop( minYItem );
00064 mSceneBoundingRectangle.setRight( maxXItem );
00065 mSceneBoundingRectangle.setBottom( maxYItem );
00066 }
00067
00068 else
00069 {
00070 if ( minXItem < mSceneBoundingRectangle.left() )
00071 {
00072 mSceneBoundingRectangle.setLeft( minXItem );
00073 }
00074 if ( minYItem < mSceneBoundingRectangle.top() )
00075 {
00076 mSceneBoundingRectangle.setTop( minYItem );
00077 }
00078 if ( maxXItem > mSceneBoundingRectangle.right() )
00079 {
00080 mSceneBoundingRectangle.setRight( maxXItem );
00081 }
00082 if ( maxYItem > mSceneBoundingRectangle.bottom() )
00083 {
00084 mSceneBoundingRectangle.setBottom( maxYItem );
00085 }
00086 }
00087
00088 QgsComposerItem::setSceneRect( mSceneBoundingRectangle );
00089
00090 }
00091
00092 void QgsComposerItemGroup::removeItems()
00093 {
00094 QSet<QgsComposerItem*>::iterator item_it = mItems.begin();
00095 for ( ; item_it != mItems.end(); ++item_it )
00096 {
00097 ( *item_it )->setFlag( QGraphicsItem::ItemIsSelectable, true );
00098 ( *item_it )->setSelected( true );
00099 }
00100 mItems.clear();
00101 }
00102
00103 void QgsComposerItemGroup::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
00104 {
00105 drawFrame( painter );
00106 if ( isSelected() )
00107 {
00108 drawSelectionBoxes( painter );
00109 }
00110 }
00111
00112 void QgsComposerItemGroup::setSceneRect( const QRectF& rectangle )
00113 {
00114
00115
00116 double xLeftCurrent = transform().dx();
00117 double xRightCurrent = xLeftCurrent + rect().width();
00118 double yTopCurrent = transform().dy();
00119 double yBottomCurrent = yTopCurrent + rect().height();
00120
00121 double xItemLeft, xItemRight, yItemTop, yItemBottom;
00122 double xItemLeftNew, xItemRightNew, yItemTopNew, yItemBottomNew;
00123 double xParamLeft, xParamRight, yParamTop, yParamBottom;
00124
00125
00126 QSet<QgsComposerItem*>::iterator item_it = mItems.begin();
00127 for ( ; item_it != mItems.end(); ++item_it )
00128 {
00129 xItemLeft = ( *item_it )->transform().dx();
00130 xItemRight = xItemLeft + ( *item_it )->rect().width();
00131 yItemTop = ( *item_it )->transform().dy();
00132 yItemBottom = yItemTop + ( *item_it )->rect().height();
00133
00134 xParamLeft = ( xItemLeft - xLeftCurrent ) / ( xRightCurrent - xLeftCurrent );
00135 xParamRight = ( xItemRight - xLeftCurrent ) / ( xRightCurrent - xLeftCurrent );
00136 yParamTop = ( yItemTop - yTopCurrent ) / ( yBottomCurrent - yTopCurrent );
00137 yParamBottom = ( yItemBottom - yTopCurrent ) / ( yBottomCurrent - yTopCurrent );
00138
00139 xItemLeftNew = xParamLeft * rectangle.right() + ( 1 - xParamLeft ) * rectangle.left();
00140 xItemRightNew = xParamRight * rectangle.right() + ( 1 - xParamRight ) * rectangle.left();
00141 yItemTopNew = yParamTop * rectangle.bottom() + ( 1 - yParamTop ) * rectangle.top();
00142 yItemBottomNew = yParamBottom * rectangle.bottom() + ( 1 - yParamBottom ) * rectangle.top();
00143
00144 ( *item_it )->setSceneRect( QRectF( xItemLeftNew, yItemTopNew, xItemRightNew - xItemLeftNew, yItemBottomNew - yItemTopNew ) );
00145 }
00146 QgsComposerItem::setSceneRect( rectangle );
00147 }
00148
00149 void QgsComposerItemGroup::drawFrame( QPainter* p )
00150 {
00151 if ( !mComposition )
00152 {
00153 return;
00154 }
00155
00156 if ( mFrame && mComposition->plotStyle() == QgsComposition::Preview )
00157 {
00158 QPen newPen( pen() );
00159 newPen.setStyle( Qt::DashLine );
00160 newPen.setColor( QColor( 128, 128, 128, 128 ) );
00161 p->setPen( newPen );
00162 p->setRenderHint( QPainter::Antialiasing, true );
00163 p->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
00164 }
00165 }
00166
00167 bool QgsComposerItemGroup::writeXML( QDomElement& elem, QDomDocument & doc ) const
00168 {
00169 return true;
00170 }
00171
00172 bool QgsComposerItemGroup::readXML( const QDomElement& itemElem, const QDomDocument& doc )
00173 {
00174 return false;
00175 }