Quantum GIS API Documentation
1.7.4
|
00001 /*************************************************************************** 00002 qgscomposeritemgroup.cpp 00003 ------------------------ 00004 begin : 2nd June 2008 00005 copyright : (C) 2008 by Marco Hugentobler 00006 email : marco dot hugentobler at karto dot baug dot ethz dot ch 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 "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 if ( *itemIt ) 00035 { 00036 mComposition->removeItem( *itemIt ); 00037 ( *itemIt )->setFlag( QGraphicsItem::ItemIsSelectable, true ); 00038 } 00039 } 00040 } 00041 00042 void QgsComposerItemGroup::addItem( QgsComposerItem* item ) 00043 { 00044 if ( !item ) 00045 { 00046 return; 00047 } 00048 00049 if ( mItems.contains( item ) ) 00050 { 00051 return; 00052 } 00053 mItems.insert( item ); 00054 item->setSelected( false ); 00055 item->setFlag( QGraphicsItem::ItemIsSelectable, false ); //item in groups cannot be selected 00056 00057 //update extent (which is in scene coordinates) 00058 double minXItem = item->transform().dx(); 00059 double minYItem = item->transform().dy(); 00060 double maxXItem = minXItem + item->rect().width(); 00061 double maxYItem = minYItem + item->rect().height(); 00062 00063 if ( mSceneBoundingRectangle.isEmpty() ) //we add the first item 00064 { 00065 mSceneBoundingRectangle.setLeft( minXItem ); 00066 mSceneBoundingRectangle.setTop( minYItem ); 00067 mSceneBoundingRectangle.setRight( maxXItem ); 00068 mSceneBoundingRectangle.setBottom( maxYItem ); 00069 } 00070 00071 else 00072 { 00073 if ( minXItem < mSceneBoundingRectangle.left() ) 00074 { 00075 mSceneBoundingRectangle.setLeft( minXItem ); 00076 } 00077 if ( minYItem < mSceneBoundingRectangle.top() ) 00078 { 00079 mSceneBoundingRectangle.setTop( minYItem ); 00080 } 00081 if ( maxXItem > mSceneBoundingRectangle.right() ) 00082 { 00083 mSceneBoundingRectangle.setRight( maxXItem ); 00084 } 00085 if ( maxYItem > mSceneBoundingRectangle.bottom() ) 00086 { 00087 mSceneBoundingRectangle.setBottom( maxYItem ); 00088 } 00089 } 00090 00091 QgsComposerItem::setSceneRect( mSceneBoundingRectangle ); //call method of superclass to avoid repositioning of items 00092 00093 } 00094 00095 void QgsComposerItemGroup::removeItems() 00096 { 00097 QSet<QgsComposerItem*>::iterator item_it = mItems.begin(); 00098 for ( ; item_it != mItems.end(); ++item_it ) 00099 { 00100 ( *item_it )->setFlag( QGraphicsItem::ItemIsSelectable, true ); //enable item selection again 00101 ( *item_it )->setSelected( true ); 00102 } 00103 mItems.clear(); 00104 } 00105 00106 void QgsComposerItemGroup::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget ) 00107 { 00108 drawFrame( painter ); 00109 if ( isSelected() ) 00110 { 00111 drawSelectionBoxes( painter ); 00112 } 00113 } 00114 00115 void QgsComposerItemGroup::setSceneRect( const QRectF& rectangle ) 00116 { 00117 //calculate values between 0 and 1 for boundaries of all contained items, depending on their positions in the item group rectangle. 00118 //then position the item boundaries in the new item group rect such that these values are the same 00119 double xLeftCurrent = transform().dx(); 00120 double xRightCurrent = xLeftCurrent + rect().width(); 00121 double yTopCurrent = transform().dy(); 00122 double yBottomCurrent = yTopCurrent + rect().height(); 00123 00124 double xItemLeft, xItemRight, yItemTop, yItemBottom; 00125 double xItemLeftNew, xItemRightNew, yItemTopNew, yItemBottomNew; 00126 double xParamLeft, xParamRight, yParamTop, yParamBottom; 00127 00128 00129 QSet<QgsComposerItem*>::iterator item_it = mItems.begin(); 00130 for ( ; item_it != mItems.end(); ++item_it ) 00131 { 00132 xItemLeft = ( *item_it )->transform().dx(); 00133 xItemRight = xItemLeft + ( *item_it )->rect().width(); 00134 yItemTop = ( *item_it )->transform().dy(); 00135 yItemBottom = yItemTop + ( *item_it )->rect().height(); 00136 00137 xParamLeft = ( xItemLeft - xLeftCurrent ) / ( xRightCurrent - xLeftCurrent ); 00138 xParamRight = ( xItemRight - xLeftCurrent ) / ( xRightCurrent - xLeftCurrent ); 00139 yParamTop = ( yItemTop - yTopCurrent ) / ( yBottomCurrent - yTopCurrent ); 00140 yParamBottom = ( yItemBottom - yTopCurrent ) / ( yBottomCurrent - yTopCurrent ); 00141 00142 xItemLeftNew = xParamLeft * rectangle.right() + ( 1 - xParamLeft ) * rectangle.left(); 00143 xItemRightNew = xParamRight * rectangle.right() + ( 1 - xParamRight ) * rectangle.left(); 00144 yItemTopNew = yParamTop * rectangle.bottom() + ( 1 - yParamTop ) * rectangle.top(); 00145 yItemBottomNew = yParamBottom * rectangle.bottom() + ( 1 - yParamBottom ) * rectangle.top(); 00146 00147 ( *item_it )->setSceneRect( QRectF( xItemLeftNew, yItemTopNew, xItemRightNew - xItemLeftNew, yItemBottomNew - yItemTopNew ) ); 00148 } 00149 QgsComposerItem::setSceneRect( rectangle ); 00150 } 00151 00152 void QgsComposerItemGroup::drawFrame( QPainter* p ) 00153 { 00154 if ( !mComposition ) 00155 { 00156 return; 00157 } 00158 00159 if ( mFrame && mComposition->plotStyle() == QgsComposition::Preview ) 00160 { 00161 QPen newPen( pen() ); 00162 newPen.setStyle( Qt::DashLine ); 00163 newPen.setColor( QColor( 128, 128, 128, 128 ) ); 00164 p->setPen( newPen ); 00165 p->setRenderHint( QPainter::Antialiasing, true ); 00166 p->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) ); 00167 } 00168 }