3838#include < cmath>
3939
4040QgsComposerMap::QgsComposerMap ( QgsComposition *composition, int x, int y, int width, int height )
41- : QgsComposerItem( x, y, width, height, composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ),
41+ : QgsComposerItem( x, y, width, height, composition ), mKeepLayerSet( false ), mOverviewFrameMapId( - 1 ), mGridEnabled( false ), mGridStyle( Solid ),
4242 mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ),
4343 mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ), mTopGridAnnotationPosition( OutsideMapFrame ),
4444 mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ),
4545 mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mGridFrameStyle( NoGridFrame ), mGridFrameWidth( 2.0 ),
4646 mCrossLength( 3 ), mMapCanvas( 0 ), mDrawCanvasItems( true )
4747{
4848 mComposition = composition;
49+ mOverviewFrameMapSymbol = 0 ;
50+ createDefaultOverviewFrameSymbol ();
4951
5052 // mId = mComposition->composerMapItems().size();
5153 int maxId = -1 ;
@@ -85,13 +87,16 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
8587}
8688
8789QgsComposerMap::QgsComposerMap ( QgsComposition *composition )
88- : QgsComposerItem( 0 , 0 , 10 , 10 , composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ),
90+ : QgsComposerItem( 0 , 0 , 10 , 10 , composition ), mKeepLayerSet( false ), mOverviewFrameMapId( - 1 ), mGridEnabled( false ), mGridStyle( Solid ),
8991 mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ),
9092 mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ), mTopGridAnnotationPosition( OutsideMapFrame ),
9193 mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ),
9294 mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mGridFrameStyle( NoGridFrame ), mGridFrameWidth( 2.0 ), mCrossLength( 3 ),
9395 mMapCanvas( 0 ), mDrawCanvasItems( true )
9496{
97+ mOverviewFrameMapSymbol = 0 ;
98+ createDefaultOverviewFrameSymbol ();
99+
95100 // Offset
96101 mXOffset = 0.0 ;
97102 mYOffset = 0.0 ;
@@ -110,6 +115,7 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )
110115
111116QgsComposerMap::~QgsComposerMap ()
112117{
118+ delete mOverviewFrameMapSymbol ;
113119}
114120
115121void QgsComposerMap::draw ( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi )
@@ -364,6 +370,10 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
364370 drawSelectionBoxes ( painter );
365371 }
366372
373+ if ( mOverviewFrameMapId != -1 )
374+ {
375+ drawOverviewMapExtent ( painter );
376+ }
367377
368378 painter->restore ();
369379}
@@ -659,6 +669,13 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
659669 composerMapElem.setAttribute ( " drawCanvasItems" , " false" );
660670 }
661671
672+ // overview map frame
673+ QDomElement overviewFrameElem = doc.createElement ( " overviewFrame" );
674+ overviewFrameElem.setAttribute ( " overviewFrameMap" , mOverviewFrameMapId );
675+ QDomElement overviewFrameStyleElem = QgsSymbolLayerV2Utils::saveSymbol ( QString (), mOverviewFrameMapSymbol , doc );
676+ overviewFrameElem.appendChild ( overviewFrameStyleElem );
677+ composerMapElem.appendChild ( overviewFrameElem );
678+
662679 // extent
663680 QDomElement extentElem = doc.createElement ( " Extent" );
664681 extentElem.setAttribute ( " xmin" , QString::number ( mExtent .xMinimum () ) );
@@ -679,6 +696,9 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
679696 }
680697 composerMapElem.appendChild ( layerSetElem );
681698
699+ // overview map frame
700+ composerMapElem.setAttribute ( " overviewFrameMap" , mOverviewFrameMapId );
701+
682702 // grid
683703 QDomElement gridElem = doc.createElement ( " Grid" );
684704 gridElem.setAttribute ( " show" , mGridEnabled );
@@ -746,6 +766,18 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
746766 mPreviewMode = Rectangle;
747767 }
748768
769+ QDomElement overviewFrameElem = itemElem.firstChildElement ( " overviewFrame" );
770+ if ( !overviewFrameElem.isNull () )
771+ {
772+ setOverviewFrameMap ( overviewFrameElem.attribute ( " overviewFrameMap" , " -1" ).toInt () );
773+ QDomElement overviewFrameSymbolElem = overviewFrameElem.firstChildElement ( " symbol" );
774+ if ( !overviewFrameSymbolElem.isNull () )
775+ {
776+ delete mOverviewFrameMapSymbol ;
777+ mOverviewFrameMapSymbol = dynamic_cast <QgsFillSymbolV2*>( QgsSymbolLayerV2Utils::loadSymbol ( overviewFrameSymbolElem ) );
778+ }
779+ }
780+
749781 // extent
750782 QDomNodeList extentNodeList = itemElem.elementsByTagName ( " Extent" );
751783 if ( extentNodeList.size () > 0 )
@@ -1536,6 +1568,34 @@ double QgsComposerMap::mapUnitsToMM() const
15361568 return rect ().width () / extentWidth;
15371569}
15381570
1571+ void QgsComposerMap::setOverviewFrameMap ( int mapId )
1572+ {
1573+ if ( mOverviewFrameMapId != -1 )
1574+ {
1575+ const QgsComposerMap* map = mComposition ->getComposerMapById ( mapId );
1576+ if ( map )
1577+ {
1578+ QObject::disconnect ( map, SIGNAL ( extentChanged () ), this , SLOT ( repaint () ) );
1579+ }
1580+ }
1581+ mOverviewFrameMapId = mapId;
1582+ if ( mOverviewFrameMapId != -1 )
1583+ {
1584+ const QgsComposerMap* map = mComposition ->getComposerMapById ( mapId );
1585+ if ( map )
1586+ {
1587+ QObject::connect ( map, SIGNAL ( extentChanged () ), this , SLOT ( repaint () ) );
1588+ }
1589+ }
1590+ update ();
1591+ }
1592+
1593+ void QgsComposerMap::setOverviewFrameMapSymbol ( QgsFillSymbolV2* symbol )
1594+ {
1595+ delete mOverviewFrameMapSymbol ;
1596+ mOverviewFrameMapSymbol = symbol;
1597+ }
1598+
15391599void QgsComposerMap::transformShift ( double & xShift, double & yShift ) const
15401600{
15411601 double mmToMapUnits = 1.0 / mapUnitsToMM ();
@@ -1845,4 +1905,55 @@ void QgsComposerMap::sortGridLinesOnBorders( const QList< QPair< double, QLineF
18451905 }
18461906}
18471907
1908+ void QgsComposerMap::drawOverviewMapExtent ( QPainter* p )
1909+ {
1910+ if ( mOverviewFrameMapId == -1 || !mComposition )
1911+ {
1912+ return ;
1913+ }
1914+
1915+ const QgsComposerMap* overviewFrameMap = mComposition ->getComposerMapById ( mOverviewFrameMapId );
1916+ if ( !overviewFrameMap )
1917+ {
1918+ return ;
1919+ }
18481920
1921+ QgsRectangle otherExtent = overviewFrameMap->extent ();
1922+ QgsRectangle thisExtent = extent ();
1923+ QgsRectangle intersectRect = thisExtent.intersect ( &otherExtent );
1924+
1925+ QgsRenderContext context;
1926+ context.setPainter ( p );
1927+ if ( mPreviewMode == Rectangle )
1928+ {
1929+ return ;
1930+ }
1931+ else
1932+ {
1933+ context.setScaleFactor ( 1.0 );
1934+ context.setRasterScaleFactor ( mComposition ->printResolution () / 25.4 );
1935+ }
1936+
1937+ QPolygonF polygon;
1938+ double x = ( intersectRect.xMinimum () - thisExtent.xMinimum () ) / thisExtent.width () * rect ().width ();
1939+ double y = ( thisExtent.yMaximum () - intersectRect.yMaximum () ) / thisExtent.height () * rect ().height ();
1940+ double width = intersectRect.width () / thisExtent.width () * rect ().width ();
1941+ double height = intersectRect.height () / thisExtent.height () * rect ().height ();
1942+ polygon << QPointF ( x, y ) << QPointF ( x + width, y ) << QPointF ( x + width, y + height ) << QPointF ( x, y + height ) << QPointF ( x, y );
1943+
1944+ QList<QPolygonF> rings; // empty list
1945+ mOverviewFrameMapSymbol ->startRender ( context );
1946+ mOverviewFrameMapSymbol ->renderPolygon ( polygon, &rings, 0 , context );
1947+ mOverviewFrameMapSymbol ->stopRender ( context );
1948+ }
1949+
1950+ void QgsComposerMap::createDefaultOverviewFrameSymbol ()
1951+ {
1952+ delete mOverviewFrameMapSymbol ;
1953+ QgsStringMap properties;
1954+ properties.insert ( " color" , " 255,0,0,125" );
1955+ properties.insert ( " style" , " solid" );
1956+ properties.insert ( " style_border" , " no" );
1957+ mOverviewFrameMapSymbol = QgsFillSymbolV2::createSimple ( properties );
1958+ mOverviewFrameMapSymbol ->setAlpha ( 0.3 );
1959+ }
0 commit comments