38
38
#include < cmath>
39
39
40
40
QgsComposerMap::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 ),
42
42
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ),
43
43
mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ), mTopGridAnnotationPosition( OutsideMapFrame ),
44
44
mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ),
45
45
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mGridFrameStyle( NoGridFrame ), mGridFrameWidth( 2.0 ),
46
46
mCrossLength( 3 ), mMapCanvas( 0 ), mDrawCanvasItems( true )
47
47
{
48
48
mComposition = composition;
49
+ mOverviewFrameMapSymbol = 0 ;
50
+ createDefaultOverviewFrameSymbol ();
49
51
50
52
// mId = mComposition->composerMapItems().size();
51
53
int maxId = -1 ;
@@ -85,13 +87,16 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
85
87
}
86
88
87
89
QgsComposerMap::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 ),
89
91
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ),
90
92
mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ), mTopGridAnnotationPosition( OutsideMapFrame ),
91
93
mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ),
92
94
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mGridFrameStyle( NoGridFrame ), mGridFrameWidth( 2.0 ), mCrossLength( 3 ),
93
95
mMapCanvas( 0 ), mDrawCanvasItems( true )
94
96
{
97
+ mOverviewFrameMapSymbol = 0 ;
98
+ createDefaultOverviewFrameSymbol ();
99
+
95
100
// Offset
96
101
mXOffset = 0.0 ;
97
102
mYOffset = 0.0 ;
@@ -110,6 +115,7 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )
110
115
111
116
QgsComposerMap::~QgsComposerMap ()
112
117
{
118
+ delete mOverviewFrameMapSymbol ;
113
119
}
114
120
115
121
void QgsComposerMap::draw ( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi )
@@ -364,6 +370,10 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
364
370
drawSelectionBoxes ( painter );
365
371
}
366
372
373
+ if ( mOverviewFrameMapId != -1 )
374
+ {
375
+ drawOverviewMapExtent ( painter );
376
+ }
367
377
368
378
painter->restore ();
369
379
}
@@ -659,6 +669,13 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
659
669
composerMapElem.setAttribute ( " drawCanvasItems" , " false" );
660
670
}
661
671
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
+
662
679
// extent
663
680
QDomElement extentElem = doc.createElement ( " Extent" );
664
681
extentElem.setAttribute ( " xmin" , QString::number ( mExtent .xMinimum () ) );
@@ -679,6 +696,9 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
679
696
}
680
697
composerMapElem.appendChild ( layerSetElem );
681
698
699
+ // overview map frame
700
+ composerMapElem.setAttribute ( " overviewFrameMap" , mOverviewFrameMapId );
701
+
682
702
// grid
683
703
QDomElement gridElem = doc.createElement ( " Grid" );
684
704
gridElem.setAttribute ( " show" , mGridEnabled );
@@ -746,6 +766,18 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
746
766
mPreviewMode = Rectangle;
747
767
}
748
768
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
+
749
781
// extent
750
782
QDomNodeList extentNodeList = itemElem.elementsByTagName ( " Extent" );
751
783
if ( extentNodeList.size () > 0 )
@@ -1536,6 +1568,34 @@ double QgsComposerMap::mapUnitsToMM() const
1536
1568
return rect ().width () / extentWidth;
1537
1569
}
1538
1570
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
+
1539
1599
void QgsComposerMap::transformShift ( double & xShift, double & yShift ) const
1540
1600
{
1541
1601
double mmToMapUnits = 1.0 / mapUnitsToMM ();
@@ -1845,4 +1905,55 @@ void QgsComposerMap::sortGridLinesOnBorders( const QList< QPair< double, QLineF
1845
1905
}
1846
1906
}
1847
1907
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
+ }
1848
1920
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