Quantum GIS API Documentation  1.8
src/core/composer/qgscomposeritemgroup.cpp
Go to the documentation of this file.
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   Q_UNUSED( option );
00109   Q_UNUSED( widget );
00110   drawFrame( painter );
00111   if ( isSelected() )
00112   {
00113     drawSelectionBoxes( painter );
00114   }
00115 }
00116 
00117 void QgsComposerItemGroup::setSceneRect( const QRectF& rectangle )
00118 {
00119   //calculate values between 0 and 1 for boundaries of all contained items, depending on their positions in the item group rectangle.
00120   //then position the item boundaries in the new item group rect such that these values are the same
00121   double xLeftCurrent = transform().dx();
00122   double xRightCurrent = xLeftCurrent + rect().width();
00123   double yTopCurrent = transform().dy();
00124   double yBottomCurrent = yTopCurrent + rect().height();
00125 
00126   double xItemLeft, xItemRight, yItemTop, yItemBottom;
00127   double xItemLeftNew, xItemRightNew, yItemTopNew, yItemBottomNew;
00128   double xParamLeft, xParamRight, yParamTop, yParamBottom;
00129 
00130 
00131   QSet<QgsComposerItem*>::iterator item_it = mItems.begin();
00132   for ( ; item_it != mItems.end(); ++item_it )
00133   {
00134     xItemLeft = ( *item_it )->transform().dx();
00135     xItemRight = xItemLeft + ( *item_it )->rect().width();
00136     yItemTop = ( *item_it )->transform().dy();
00137     yItemBottom = yItemTop + ( *item_it )->rect().height();
00138 
00139     xParamLeft = ( xItemLeft - xLeftCurrent ) / ( xRightCurrent - xLeftCurrent );
00140     xParamRight = ( xItemRight - xLeftCurrent ) / ( xRightCurrent - xLeftCurrent );
00141     yParamTop = ( yItemTop - yTopCurrent ) / ( yBottomCurrent - yTopCurrent );
00142     yParamBottom = ( yItemBottom - yTopCurrent ) / ( yBottomCurrent - yTopCurrent );
00143 
00144     xItemLeftNew = xParamLeft * rectangle.right()  + ( 1 - xParamLeft ) * rectangle.left();
00145     xItemRightNew = xParamRight * rectangle.right() + ( 1 - xParamRight ) * rectangle.left();
00146     yItemTopNew = yParamTop * rectangle.bottom() + ( 1 - yParamTop ) * rectangle.top();
00147     yItemBottomNew = yParamBottom * rectangle.bottom() + ( 1 - yParamBottom ) * rectangle.top();
00148 
00149     ( *item_it )->setSceneRect( QRectF( xItemLeftNew, yItemTopNew, xItemRightNew - xItemLeftNew, yItemBottomNew - yItemTopNew ) );
00150   }
00151   QgsComposerItem::setSceneRect( rectangle );
00152 }
00153 
00154 void QgsComposerItemGroup::drawFrame( QPainter* p )
00155 {
00156   if ( !mComposition )
00157   {
00158     return;
00159   }
00160 
00161   if ( mFrame && mComposition->plotStyle() == QgsComposition::Preview )
00162   {
00163     QPen newPen( pen() );
00164     newPen.setStyle( Qt::DashLine );
00165     newPen.setColor( QColor( 128, 128, 128, 128 ) );
00166     p->setPen( newPen );
00167     p->setRenderHint( QPainter::Antialiasing, true );
00168     p->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
00169   }
00170 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines