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