41
41
42
42
QgsComposerMap::QgsComposerMap ( QgsComposition *composition, int x, int y, int width, int height )
43
43
: QgsComposerItem( x, y, width, height, composition ), mKeepLayerSet( false ),
44
- mOverviewFrameMapId( -1 ), mOverviewBlendMode( QPainter::CompositionMode_SourceOver ), mOverviewInverted( false ), mGridEnabled( false ), mGridStyle( Solid ),
44
+ mOverviewFrameMapId( -1 ), mOverviewBlendMode( QPainter::CompositionMode_SourceOver ), mOverviewInverted( false ), mOverviewCentered( false ),
45
+ mGridEnabled( false ), mGridStyle( Solid ),
45
46
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationFontColor( QColor( 0 , 0 , 0 ) ),
46
47
mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ), mGridBlendMode( QPainter::CompositionMode_SourceOver ),
47
48
mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ),
@@ -86,7 +87,8 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
86
87
87
88
QgsComposerMap::QgsComposerMap ( QgsComposition *composition )
88
89
: QgsComposerItem( 0 , 0 , 10 , 10 , composition ), mKeepLayerSet( false ), mOverviewFrameMapId( -1 ),
89
- mOverviewBlendMode( QPainter::CompositionMode_SourceOver ), mOverviewInverted( false ), mGridEnabled( false ), mGridStyle( Solid ),
90
+ mOverviewBlendMode( QPainter::CompositionMode_SourceOver ), mOverviewInverted( false ), mOverviewCentered( false ),
91
+ mGridEnabled( false ), mGridStyle( Solid ),
90
92
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationFontColor( QColor( 0 , 0 , 0 ) ),
91
93
mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ), mGridBlendMode( QPainter::CompositionMode_SourceOver ),
92
94
mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ),
@@ -116,6 +118,28 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )
116
118
initGridAnnotationFormatFromProject ();
117
119
}
118
120
121
+ void QgsComposerMap::extentCenteredOnOverview ( QgsRectangle& extent ) const
122
+ {
123
+ extent = mExtent ;
124
+ if ( ! mOverviewCentered ) {
125
+ return ;
126
+ }
127
+
128
+ if ( mOverviewFrameMapId != -1 ) {
129
+ const QgsComposerMap* overviewFrameMap = mComposition ->getComposerMapById ( mOverviewFrameMapId );
130
+ QgsRectangle otherExtent = overviewFrameMap->extent ();
131
+
132
+ if ( ! mExtent .contains ( otherExtent ) ) {
133
+ QgsPoint center = otherExtent.center ();
134
+ QgsRectangle movedExtent ( center.x () - mExtent .width () / 2 ,
135
+ center.y () - mExtent .height () / 2 ,
136
+ center.x () - mExtent .width () / 2 + mExtent .width (),
137
+ center.y () - mExtent .height () / 2 + mExtent .height () );
138
+ extent = movedExtent;
139
+ }
140
+ }
141
+ }
142
+
119
143
QgsComposerMap::~QgsComposerMap ()
120
144
{
121
145
delete mOverviewFrameMapSymbol ;
@@ -297,6 +321,10 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
297
321
298
322
QgsRectangle requestRectangle;
299
323
requestedExtent ( requestRectangle );
324
+
325
+ QgsRectangle cExtent;
326
+ extentCenteredOnOverview ( cExtent );
327
+
300
328
double horizontalVScaleFactor = horizontalViewScaleFactor ();
301
329
if ( horizontalVScaleFactor < 0 )
302
330
{
@@ -305,15 +333,15 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
305
333
306
334
double imagePixelWidth = mExtent .width () / requestRectangle.width () * mCacheImage .width () ; // how many pixels of the image are for the map extent?
307
335
double scale = rect ().width () / imagePixelWidth;
308
- QgsPoint rotationPoint = QgsPoint (( mExtent .xMaximum () + mExtent .xMinimum () ) / 2.0 , ( mExtent .yMaximum () + mExtent .yMinimum () ) / 2.0 );
336
+ QgsPoint rotationPoint = QgsPoint (( cExtent .xMaximum () + cExtent .xMinimum () ) / 2.0 , ( cExtent .yMaximum () + cExtent .yMinimum () ) / 2.0 );
309
337
310
338
// shift such that rotation point is at 0/0 point in the coordinate system
311
339
double yShiftMM = ( requestRectangle.yMaximum () - rotationPoint.y () ) * mapUnitsToMM ();
312
340
double xShiftMM = ( requestRectangle.xMinimum () - rotationPoint.x () ) * mapUnitsToMM ();
313
341
314
342
// shift such that top left point of the extent at point 0/0 in item coordinate system
315
- double xTopLeftShift = ( rotationPoint.x () - mExtent .xMinimum () ) * mapUnitsToMM ();
316
- double yTopLeftShift = ( mExtent .yMaximum () - rotationPoint.y () ) * mapUnitsToMM ();
343
+ double xTopLeftShift = ( rotationPoint.x () - cExtent .xMinimum () ) * mapUnitsToMM ();
344
+ double yTopLeftShift = ( cExtent .yMaximum () - rotationPoint.y () ) * mapUnitsToMM ();
317
345
318
346
painter->save ();
319
347
@@ -351,16 +379,20 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
351
379
QgsRectangle requestRectangle;
352
380
requestedExtent ( requestRectangle );
353
381
382
+ QgsRectangle cExtent;
383
+ extentCenteredOnOverview ( cExtent );
384
+
354
385
QSizeF theSize ( requestRectangle.width () * mapUnitsToMM (), requestRectangle.height () * mapUnitsToMM () );
355
- QgsPoint rotationPoint = QgsPoint (( mExtent .xMaximum () + mExtent .xMinimum () ) / 2.0 , ( mExtent .yMaximum () + mExtent .yMinimum () ) / 2.0 );
386
+
387
+ QgsPoint rotationPoint = QgsPoint (( cExtent.xMaximum () + cExtent.xMinimum () ) / 2.0 , ( cExtent.yMaximum () + cExtent.yMinimum () ) / 2.0 );
356
388
357
389
// shift such that rotation point is at 0/0 point in the coordinate system
358
390
double yShiftMM = ( requestRectangle.yMaximum () - rotationPoint.y () ) * mapUnitsToMM ();
359
391
double xShiftMM = ( requestRectangle.xMinimum () - rotationPoint.x () ) * mapUnitsToMM ();
360
392
361
393
// shift such that top left point of the extent at point 0/0 in item coordinate system
362
- double xTopLeftShift = ( rotationPoint.x () - mExtent .xMinimum () ) * mapUnitsToMM ();
363
- double yTopLeftShift = ( mExtent .yMaximum () - rotationPoint.y () ) * mapUnitsToMM ();
394
+ double xTopLeftShift = ( rotationPoint.x () - cExtent .xMinimum () ) * mapUnitsToMM ();
395
+ double yTopLeftShift = ( cExtent .yMaximum () - rotationPoint.y () ) * mapUnitsToMM ();
364
396
painter->save ();
365
397
painter->translate ( mXOffset , mYOffset );
366
398
painter->translate ( xTopLeftShift, yTopLeftShift );
@@ -757,6 +789,9 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
757
789
{
758
790
overviewFrameElem.setAttribute ( " overviewInverted" , " false" );
759
791
}
792
+
793
+ overviewFrameElem.setAttribute ( " overviewCentered" , mOverviewCentered ? " true" : " false" );
794
+
760
795
QDomElement overviewFrameStyleElem = QgsSymbolLayerV2Utils::saveSymbol ( QString (), mOverviewFrameMapSymbol , doc );
761
796
overviewFrameElem.appendChild ( overviewFrameStyleElem );
762
797
composerMapElem.appendChild ( overviewFrameElem );
@@ -874,6 +909,15 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
874
909
setOverviewInverted ( false );
875
910
}
876
911
912
+ if ( overviewFrameElem.attribute ( " overviewCentered" ).compare ( " true" , Qt::CaseInsensitive ) == 0 )
913
+ {
914
+ setOverviewCentered ( true );
915
+ }
916
+ else
917
+ {
918
+ setOverviewCentered ( false );
919
+ }
920
+
877
921
QDomElement overviewFrameSymbolElem = overviewFrameElem.firstChildElement ( " symbol" );
878
922
if ( !overviewFrameSymbolElem.isNull () )
879
923
{
@@ -1727,63 +1771,70 @@ double QgsComposerMap::maxExtension() const
1727
1771
return maxExtension + mAnnotationFrameDistance + gridFrameDist;
1728
1772
}
1729
1773
1730
- void QgsComposerMap::mapPolygon ( QPolygonF& poly ) const
1774
+ void QgsComposerMap::mapPolygon ( const QgsRectangle& extent, QPolygonF& poly ) const
1731
1775
{
1732
1776
poly.clear ();
1733
1777
if ( mRotation == 0 )
1734
1778
{
1735
- poly << QPointF ( mExtent .xMinimum (), mExtent .yMaximum () );
1736
- poly << QPointF ( mExtent .xMaximum (), mExtent .yMaximum () );
1737
- poly << QPointF ( mExtent .xMaximum (), mExtent .yMinimum () );
1738
- poly << QPointF ( mExtent .xMinimum (), mExtent .yMinimum () );
1779
+ poly << QPointF ( extent .xMinimum (), extent .yMaximum () );
1780
+ poly << QPointF ( extent .xMaximum (), extent .yMaximum () );
1781
+ poly << QPointF ( extent .xMaximum (), extent .yMinimum () );
1782
+ poly << QPointF ( extent .xMinimum (), extent .yMinimum () );
1739
1783
return ;
1740
1784
}
1741
1785
1742
1786
// there is rotation
1743
- QgsPoint rotationPoint (( mExtent .xMaximum () + mExtent .xMinimum () ) / 2.0 , ( mExtent .yMaximum () + mExtent .yMinimum () ) / 2.0 );
1787
+ QgsPoint rotationPoint (( extent .xMaximum () + extent .xMinimum () ) / 2.0 , ( extent .yMaximum () + extent .yMinimum () ) / 2.0 );
1744
1788
double dx, dy; // x-, y- shift from rotation point to corner point
1745
1789
1746
1790
// top left point
1747
- dx = rotationPoint.x () - mExtent .xMinimum ();
1748
- dy = rotationPoint.y () - mExtent .yMaximum ();
1791
+ dx = rotationPoint.x () - extent .xMinimum ();
1792
+ dy = rotationPoint.y () - extent .yMaximum ();
1749
1793
rotate ( mRotation , dx, dy );
1750
1794
poly << QPointF ( rotationPoint.x () + dx, rotationPoint.y () + dy );
1751
1795
1752
1796
// top right point
1753
- dx = rotationPoint.x () - mExtent .xMaximum ();
1754
- dy = rotationPoint.y () - mExtent .yMaximum ();
1797
+ dx = rotationPoint.x () - extent .xMaximum ();
1798
+ dy = rotationPoint.y () - extent .yMaximum ();
1755
1799
rotate ( mRotation , dx, dy );
1756
1800
poly << QPointF ( rotationPoint.x () + dx, rotationPoint.y () + dy );
1757
1801
1758
1802
// bottom right point
1759
- dx = rotationPoint.x () - mExtent .xMaximum ();
1760
- dy = rotationPoint.y () - mExtent .yMinimum ();
1803
+ dx = rotationPoint.x () - extent .xMaximum ();
1804
+ dy = rotationPoint.y () - extent .yMinimum ();
1761
1805
rotate ( mRotation , dx, dy );
1762
1806
poly << QPointF ( rotationPoint.x () + dx, rotationPoint.y () + dy );
1763
1807
1764
1808
// bottom left point
1765
- dx = rotationPoint.x () - mExtent .xMinimum ();
1766
- dy = rotationPoint.y () - mExtent .yMinimum ();
1809
+ dx = rotationPoint.x () - extent .xMinimum ();
1810
+ dy = rotationPoint.y () - extent .yMinimum ();
1767
1811
rotate ( mRotation , dx, dy );
1768
1812
poly << QPointF ( rotationPoint.x () + dx, rotationPoint.y () + dy );
1769
1813
}
1770
1814
1815
+ void QgsComposerMap::mapPolygon ( QPolygonF& poly ) const
1816
+ {
1817
+ return mapPolygon ( mExtent , poly );
1818
+ }
1819
+
1771
1820
void QgsComposerMap::requestedExtent ( QgsRectangle& extent ) const
1772
1821
{
1822
+ QgsRectangle newExtent;
1823
+ extentCenteredOnOverview ( newExtent );
1773
1824
if ( mRotation == 0 )
1774
1825
{
1775
- extent = mExtent ;
1776
- return ;
1826
+ extent = newExtent;
1827
+ }
1828
+ else
1829
+ {
1830
+ QPolygonF poly;
1831
+ mapPolygon ( newExtent, poly );
1832
+ QRectF bRect = poly.boundingRect ();
1833
+ extent.setXMinimum ( bRect.left () );
1834
+ extent.setXMaximum ( bRect.right () );
1835
+ extent.setYMinimum ( bRect.top () );
1836
+ extent.setYMaximum ( bRect.bottom () );
1777
1837
}
1778
-
1779
- QPolygonF poly;
1780
- mapPolygon ( poly );
1781
- QRectF bRect = poly.boundingRect ();
1782
- extent.setXMinimum ( bRect.left () );
1783
- extent.setXMaximum ( bRect.right () );
1784
- extent.setYMinimum ( bRect.top () );
1785
- extent.setYMaximum ( bRect.bottom () );
1786
- return ;
1787
1838
}
1788
1839
1789
1840
double QgsComposerMap::mapUnitsToMM () const
@@ -1836,6 +1887,12 @@ void QgsComposerMap::setOverviewInverted( bool inverted )
1836
1887
update ();
1837
1888
}
1838
1889
1890
+ void QgsComposerMap::setOverviewCentered ( bool centered )
1891
+ {
1892
+ mOverviewCentered = centered;
1893
+ update ();
1894
+ }
1895
+
1839
1896
void QgsComposerMap::setGridLineSymbol ( QgsLineSymbolV2* symbol )
1840
1897
{
1841
1898
delete mGridLineSymbol ;
@@ -2165,7 +2222,8 @@ void QgsComposerMap::drawOverviewMapExtent( QPainter* p )
2165
2222
}
2166
2223
2167
2224
QgsRectangle otherExtent = overviewFrameMap->extent ();
2168
- QgsRectangle thisExtent = extent ();
2225
+ QgsRectangle thisExtent;
2226
+ extentCenteredOnOverview ( thisExtent );
2169
2227
QgsRectangle intersectRect = thisExtent.intersect ( &otherExtent );
2170
2228
2171
2229
QgsRenderContext context;
0 commit comments