Skip to content

Commit 1ccf558

Browse files
author
Sandro Santilli
committed
Add a new visiblePolygon method to QgsMapSettings
Centralizes code to return a possible rotated map area boundaries. Makes use of the new method from the grid decoration. Adds new testcase for QgsMapSettings, including test for visibleExtent, mapUnitsPerPixel and the new visiblePolygon method.
1 parent 198d8fe commit 1ccf558

File tree

6 files changed

+164
-9
lines changed

6 files changed

+164
-9
lines changed

python/core/qgsmapsettings.sip

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ class QgsMapSettings
110110
bool hasValidSettings() const;
111111
//! Return the actual extent derived from requested extent that takes takes output image size into account
112112
QgsRectangle visibleExtent() const;
113+
//! Return the visible area as a polygon (may be rotated)
114+
//! @note added in 2.8
115+
QPolygonF visiblePolygon() const;
113116
//! Return the distance in geographical coordinates that equals to one pixel in the map
114117
double mapUnitsPerPixel() const;
115118
//! Return the calculated scale of the map

src/app/qgsdecorationgrid.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -524,15 +524,7 @@ QPolygonF canvasPolygon()
524524

525525
const QgsMapCanvas& mapCanvas = canvas();
526526
const QgsMapSettings& mapSettings = mapCanvas.mapSettings();
527-
const QSize& sz = mapSettings.outputSize();
528-
const QgsMapToPixel& m2p = mapSettings.mapToPixel();
529-
530-
poly << m2p.toMapCoordinatesF( 0, 0 ).toQPointF();
531-
poly << m2p.toMapCoordinatesF( sz.width(), 0 ).toQPointF();
532-
poly << m2p.toMapCoordinatesF( sz.width(), sz.height() ).toQPointF();
533-
poly << m2p.toMapCoordinatesF( 0, sz.height() ).toQPointF();
534-
535-
return poly;
527+
return mapSettings.visiblePolygon();
536528
}
537529

538530
bool clipByRect( QLineF& line, const QPolygonF& rect )

src/core/qgsmapsettings.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,21 @@ QgsRectangle QgsMapSettings::visibleExtent() const
311311
return mVisibleExtent;
312312
}
313313

314+
QPolygonF QgsMapSettings::visiblePolygon() const
315+
{
316+
QPolygonF poly;
317+
318+
const QSize& sz = outputSize();
319+
const QgsMapToPixel& m2p = mapToPixel();
320+
321+
poly << m2p.toMapCoordinatesF( 0, 0 ).toQPointF();
322+
poly << m2p.toMapCoordinatesF( sz.width(), 0 ).toQPointF();
323+
poly << m2p.toMapCoordinatesF( sz.width(), sz.height() ).toQPointF();
324+
poly << m2p.toMapCoordinatesF( 0, sz.height() ).toQPointF();
325+
326+
return poly;
327+
}
328+
314329
double QgsMapSettings::mapUnitsPerPixel() const
315330
{
316331
return mMapUnitsPerPixel;

src/core/qgsmapsettings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ class CORE_EXPORT QgsMapSettings
156156
bool hasValidSettings() const;
157157
//! Return the actual extent derived from requested extent that takes takes output image size into account
158158
QgsRectangle visibleExtent() const;
159+
//! Return the visible area as a polygon (may be rotated)
160+
//! @note added in 2.8
161+
QPolygonF visiblePolygon() const;
159162
//! Return the distance in geographical coordinates that equals to one pixel in the map
160163
double mapUnitsPerPixel() const;
161164
//! Return the calculated scale of the map

tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ ADD_QGIS_TEST(invertedpolygontest testqgsinvertedpolygonrenderer.cpp )
140140
ADD_QGIS_TEST(colorschemeregistry testqgscolorschemeregistry.cpp)
141141
ADD_QGIS_TEST(colorscheme testqgscolorscheme.cpp)
142142
ADD_QGIS_TEST(maptopixeltest testqgsmaptopixel.cpp)
143+
ADD_QGIS_TEST(mapsettingstest testqgsmapsettings.cpp)
143144
ADD_QGIS_TEST(networkcontentfetcher testqgsnetworkcontentfetcher.cpp )
144145
ADD_QGIS_TEST(legendrenderertest testqgslegendrenderer.cpp )
145146
ADD_QGIS_TEST(vectorlayerjoinbuffer testqgsvectorlayerjoinbuffer.cpp )
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/***************************************************************************
2+
testqgsmapsettings.cpp
3+
--------------------------------------
4+
Date : Tue 6 Feb 2015
5+
Copyright : (C) 2014 by Sandro Santilli
6+
Email : strk@keybit.net
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#include <QtTest/QtTest>
16+
#include <QObject>
17+
#include <QString>
18+
#include <QObject>
19+
//header for class being tested
20+
#include <qgsrectangle.h>
21+
#include <qgsmapsettings.h>
22+
#include <qgspoint.h>
23+
#include <math.h>
24+
#include "qgslogger.h"
25+
26+
class TestQgsMapSettings: public QObject
27+
{
28+
Q_OBJECT
29+
private slots:
30+
void visibleExtent();
31+
void mapUnitsPerPixel();
32+
void visiblePolygon();
33+
private:
34+
QString toString( const QPolygonF& p, int decimalPlaces = 2 ) const;
35+
};
36+
37+
QString TestQgsMapSettings::toString( const QPolygonF& p, int dec ) const
38+
{
39+
QString s;
40+
const char *sep = "";
41+
double r = pow( 10, dec );
42+
for ( int i = 0; i < p.size(); ++i )
43+
{
44+
s += QString( "%1%2 %3" ).arg( sep )
45+
.arg( int( p[i].x() * r ) / r )
46+
.arg( int( p[i].y() * r ) / r );
47+
sep = ",";
48+
}
49+
50+
return s;
51+
}
52+
53+
void TestQgsMapSettings::visibleExtent()
54+
{
55+
QgsMapSettings ms;
56+
57+
ms.setExtent( QgsRectangle( 0, 0, 100, 100 ) );
58+
ms.setOutputSize( QSize( 100, 50 ) );
59+
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "-50,0 : 150,100" ) );
60+
61+
ms.setExtent( QgsRectangle( 0, 0, 100, 100 ) );
62+
ms.setOutputSize( QSize( 100, 100 ) );
63+
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "0,0 : 100,100" ) );
64+
65+
ms.setExtent( QgsRectangle( 0, 0, 100, 100 ) );
66+
ms.setOutputSize( QSize( 50, 100 ) );
67+
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "0,-50 : 100,150" ) );
68+
69+
ms.setExtent( QgsRectangle( 0, 0, 100, 100 ) );
70+
ms.setOutputSize( QSize( 50, 100 ) );
71+
ms.setRotation( 90 );
72+
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "-50,0 : 150,100" ) );
73+
ms.setRotation( -90 );
74+
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "-50,0 : 150,100" ) );
75+
76+
ms.setExtent( QgsRectangle( 0, 0, 100, 50 ) );
77+
ms.setOutputSize( QSize( 50, 100 ) );
78+
ms.setRotation( 0 );
79+
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "0,-75 : 100,125" ) );
80+
ms.setRotation( 90 );
81+
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "-50,-25 : 150,75" ) );
82+
ms.setRotation( -90 );
83+
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "-50,-25 : 150,75" ) );
84+
ms.setRotation( 45 );
85+
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "-56,-81 : 156,131" ) );
86+
}
87+
88+
void TestQgsMapSettings::mapUnitsPerPixel()
89+
{
90+
QgsMapSettings ms;
91+
ms.setExtent( QgsRectangle( 0, 0, 100, 100 ) );
92+
93+
ms.setOutputSize( QSize( 100, 50 ) );
94+
QCOMPARE( ms.mapUnitsPerPixel(), 2.0 );
95+
96+
ms.setOutputSize( QSize( 100, 100 ) );
97+
QCOMPARE( ms.mapUnitsPerPixel(), 1.0 );
98+
99+
ms.setOutputSize( QSize( 50, 100 ) );
100+
QCOMPARE( ms.mapUnitsPerPixel(), 2.0 );
101+
102+
ms.setOutputSize( QSize( 5000, 1000 ) );
103+
QCOMPARE( ms.mapUnitsPerPixel(), 0.1 );
104+
105+
ms.setOutputSize( QSize( 1000, 500 ) );
106+
QCOMPARE( ms.mapUnitsPerPixel(), 0.2 );
107+
}
108+
109+
void TestQgsMapSettings::visiblePolygon()
110+
{
111+
QgsMapSettings ms;
112+
113+
ms.setExtent( QgsRectangle( 0, 0, 100, 100 ) );
114+
ms.setOutputSize( QSize( 100, 50 ) );
115+
QCOMPARE( toString( ms.visiblePolygon() ),
116+
QString( "-50 100,150 100,150 0,-50 0" ) );
117+
118+
ms.setExtent( QgsRectangle( 0, -50, 100, 0 ) );
119+
ms.setOutputSize( QSize( 100, 50 ) );
120+
ms.setRotation( 90 );
121+
QCOMPARE( toString( ms.visiblePolygon() ),
122+
QString( "25 -75,25 25,75 25,75 -75" ) );
123+
ms.setRotation( -90 );
124+
QCOMPARE( toString( ms.visiblePolygon() ),
125+
QString( "75 25,75 -75,25 -75,25 25" ) );
126+
ms.setRotation( 30 );
127+
QCOMPARE( toString( ms.visiblePolygon() ),
128+
QString( "-5.8 -28.34,80.8 21.65,105.8 -21.65,19.19 -71.65" ) );
129+
ms.setRotation( -30 );
130+
QCOMPARE( toString( ms.visiblePolygon() ),
131+
QString( "19.19 21.65,105.8 -28.34,80.8 -71.65,-5.8 -21.65" ) );
132+
ms.setRotation( 45 );
133+
QCOMPARE( toString( ms.visiblePolygon() ),
134+
QString( "-3.03 -42.67,67.67 28.03,103.03 -7.32,32.32 -78.03" ) );
135+
ms.setRotation( -45 );
136+
QCOMPARE( toString( ms.visiblePolygon() ),
137+
QString( "32.32 28.03,103.03 -42.67,67.67 -78.03,-3.03 -7.32" ) );
138+
}
139+
140+
QTEST_MAIN( TestQgsMapSettings )
141+
#include "testqgsmapsettings.moc"

0 commit comments

Comments
 (0)