Skip to content

Commit cebb6ff

Browse files
author
Sandro Santilli
committed
Refactor QgsMapToPixel to only build matrix on parameter change
Also add deprecation notices and provide full-parameters constructor
1 parent ec085bf commit cebb6ff

File tree

2 files changed

+144
-38
lines changed

2 files changed

+144
-38
lines changed

src/core/qgsmaptopixel.cpp

Lines changed: 92 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,52 @@
2323

2424
#include "qgslogger.h"
2525

26+
// @deprecated in 2.8
27+
QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel,
28+
double xc,
29+
double yc,
30+
int width,
31+
int height,
32+
double rotation )
33+
: mMapUnitsPerPixel( mapUnitsPerPixel )
34+
, mWidth( width )
35+
, mHeight( height )
36+
, mRotation( rotation )
37+
, xCenter( xc )
38+
, yCenter( yc )
39+
, xMin ( xc - ( mWidth * mMapUnitsPerPixel / 2.0 ) )
40+
, yMin ( yc - ( mHeight * mMapUnitsPerPixel / 2.0 ) )
41+
{
42+
updateMatrix();
43+
}
44+
45+
QgsMapToPixel::QgsMapToPixel()
46+
: mMapUnitsPerPixel( 1 )
47+
, mWidth( 1 )
48+
, mHeight( 1 )
49+
, mRotation( 0.0 )
50+
, xCenter( 0.5 )
51+
, yCenter( 0.5 )
52+
, xMin ( 0 )
53+
, yMin ( 0 )
54+
{
55+
updateMatrix();
56+
}
57+
2658
QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel,
2759
double height,
2860
double ymin,
2961
double xmin )
3062
: mMapUnitsPerPixel( mapUnitsPerPixel )
63+
, mWidth( -1 )
3164
, mHeight( height )
32-
, yMin( ymin )
33-
, xMin( xmin )
34-
, mMapRotation( 0 )
65+
, mRotation( 0.0 )
66+
, xCenter( 0.0 )
67+
, yCenter( 0.0 )
68+
, xMin ( xmin )
69+
, yMin ( ymin )
3570
{
71+
updateMatrix();
3672
}
3773

3874
QgsMapToPixel::~QgsMapToPixel()
@@ -46,13 +82,11 @@ int QgsMapToPixel::mapHeight() const
4682

4783
int QgsMapToPixel::mapWidth() const
4884
{
49-
return (( xCenter -xMin )*2 ) / mMapUnitsPerPixel;
85+
return mWidth;
5086
}
5187

52-
QTransform QgsMapToPixel::getMatrix() const
88+
void QgsMapToPixel::updateMatrix()
5389
{
54-
double cy = mapHeight() / 2.0;
55-
double cx = mapWidth() / 2.0;
5690
double rotation = mapRotation();
5791

5892
#if 0 // debugging
@@ -71,17 +105,25 @@ QTransform QgsMapToPixel::getMatrix() const
71105
// Returning a simplified matrix in hope it'll give expected
72106
// results from an existing test, see
73107
// https://travis-ci.org/qgis/QGIS/builds/42508945
74-
return QTransform::fromScale( 1.0 / mMapUnitsPerPixel, -1.0 / mMapUnitsPerPixel )
108+
mMatrix = QTransform::fromScale( 1.0 / mMapUnitsPerPixel, -1.0 / mMapUnitsPerPixel )
75109
.translate( -xMin, - ( yMin + mHeight*mMapUnitsPerPixel ) );
110+
return;
76111
}
77112

78-
return QTransform::fromTranslate( cx, cy )
113+
double cy = mapHeight() / 2.0;
114+
double cx = mapWidth() / 2.0;
115+
mMatrix = QTransform::fromTranslate( cx, cy )
79116
.rotate( rotation )
80117
.scale( 1 / mMapUnitsPerPixel, -1 / mMapUnitsPerPixel )
81118
.translate( -xCenter, -yCenter )
82119
;
83120
}
84121

122+
const QTransform& QgsMapToPixel::getMatrix() const
123+
{
124+
return mMatrix;
125+
}
126+
85127
QgsPoint QgsMapToPixel::toMapPoint( double x, double y ) const
86128
{
87129
bool invertible;
@@ -115,6 +157,7 @@ QgsPoint QgsMapToPixel::toMapCoordinatesF( double x, double y ) const
115157
void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
116158
{
117159
mMapUnitsPerPixel = mapUnitsPerPixel;
160+
updateMatrix();
118161
}
119162

120163
double QgsMapToPixel::mapUnitsPerPixel() const
@@ -124,50 +167,75 @@ double QgsMapToPixel::mapUnitsPerPixel() const
124167

125168
void QgsMapToPixel::setMapRotation( double degrees, double cx, double cy )
126169
{
127-
mMapRotation = degrees;
170+
mRotation = degrees;
128171
xCenter = cx;
129172
yCenter = cy;
130-
assert( xCenter >= xMin );
131-
assert( yCenter >= yMin );
132-
//assert(yCenter <= yMin + mHeight*mMapUnitsPerPixel;
173+
if ( mWidth < 0 ) {
174+
// set width not that we can compute it
175+
mWidth = (( xCenter -xMin )*2 ) / mMapUnitsPerPixel;
176+
}
177+
updateMatrix();
133178
}
134179

135180
double QgsMapToPixel::mapRotation() const
136181
{
137-
return mMapRotation;
138-
}
139-
140-
void QgsMapToPixel::setViewportHeight( double height )
141-
{
142-
mHeight = height;
182+
return mRotation;
143183
}
144184

185+
// @deprecated in 2.8
145186
void QgsMapToPixel::setYMinimum( double ymin )
146187
{
147-
yMin = ymin;
188+
yCenter = ymin + mHeight * mMapUnitsPerPixel / 2.0;
189+
mRotation = 0.0;
190+
updateMatrix();
148191
}
149192

193+
// @deprecated in 2.8
150194
void QgsMapToPixel::setXMinimum( double xmin )
151195
{
152-
xMin = xmin;
196+
xCenter = xmin + mWidth * mMapUnitsPerPixel / 2.0;
197+
mRotation = 0.0;
198+
updateMatrix();
153199
}
154200

201+
// @deprecated in 2.8
155202
void QgsMapToPixel::setParameters( double mapUnitsPerPixel, double xmin, double ymin, double ymax )
156203
{
157204
mMapUnitsPerPixel = mapUnitsPerPixel;
158205
xMin = xmin;
159206
yMin = ymin;
160207
mHeight = ymax;
208+
xCenter = xmin + mWidth * mMapUnitsPerPixel / 2.0;
209+
yCenter = ymin + mHeight * mMapUnitsPerPixel / 2.0;
210+
mRotation = 0.0;
211+
updateMatrix();
212+
}
161213

214+
void QgsMapToPixel::setParameters( double mapUnitsPerPixel,
215+
double xc,
216+
double yc,
217+
int width,
218+
int height,
219+
double rotation )
220+
{
221+
mMapUnitsPerPixel = mapUnitsPerPixel;
222+
xCenter = xc;
223+
yCenter = yc;
224+
mWidth = width;
225+
mHeight = height;
226+
mRotation = rotation;
227+
xMin = xc - ( mWidth * mMapUnitsPerPixel / 2.0 );
228+
yMin = yc - ( mHeight * mMapUnitsPerPixel / 2.0 );
229+
updateMatrix();
162230
}
163231

164232
QString QgsMapToPixel::showParameters()
165233
{
166234
QString rep;
167235
QTextStream( &rep ) << "Map units/pixel: " << mMapUnitsPerPixel
168-
<< " X minimum: " << xMin << " Y minimum: " << yMin
169-
<< " Height: " << mHeight << " Rotation: " << mMapRotation
170-
<< " X center: " << xCenter << " Y center: " << yCenter;
236+
<< " center: " << xCenter << "," << yCenter
237+
<< " rotation: " << mRotation
238+
<< " size: " << mWidth << "x" << mHeight;
171239
return rep;
172240

173241
}

src/core/qgsmaptopixel.h

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,27 @@ class CORE_EXPORT QgsMapToPixel
3939
* @param height Map canvas height, in pixels
4040
* @param ymin Minimum y value of the map canvas
4141
* @param xmin Minimum x value of the map canvas
42+
* @deprecated in 2.8, use version with all parameters
4243
*/
43-
QgsMapToPixel( double mapUnitsPerPixel = 0, double height = 0, double ymin = 0,
44-
double xmin = 0 );
44+
QgsMapToPixel( double mapUnitsPerPixel, double height = 0, double ymin = 0, double xmin = 0 );
45+
46+
/* Constructor
47+
* @param mapUnitsPerPixel Map units per pixel
48+
* @param xc X ordinate of map center, in geographical units
49+
* @param yc Y ordinate of map center, in geographical units
50+
* @param width Output width, in pixels
51+
* @param height Output height, in pixels
52+
* @param degrees clockwise rotation in degrees
53+
* @note added in 2.8
54+
*/
55+
QgsMapToPixel( double mapUnitsPerPixel, double xc, double yc, int width, int height, double rotation );
56+
57+
/*! Constructor
58+
*
59+
* Use setParameters to fill
60+
*/
61+
QgsMapToPixel();
62+
4563
//! destructor
4664
~QgsMapToPixel();
4765
/*! Transform the point from map (world) coordinates to device coordinates
@@ -103,9 +121,12 @@ class CORE_EXPORT QgsMapToPixel
103121
double mapUnitsPerPixel() const;
104122

105123
//! Return current map width in pixels
124+
//! The information is only known if setRotation was used
125+
//! @note added in 2.8
106126
int mapWidth() const;
107127

108128
//! Return current map height in pixels
129+
//! @note added in 2.8
109130
int mapHeight() const;
110131

111132
//! Set map rotation in degrees (clockwise)
@@ -120,39 +141,56 @@ class CORE_EXPORT QgsMapToPixel
120141
double mapRotation() const;
121142

122143
//! Set maximum y value
123-
// @deprecated in 2.8, use setViewportHeight
144+
// @deprecated in 2.8, use setParameters
124145
// @note this really sets the viewport height, not ymax
125-
void setYMaximum( double yMax ) { setViewportHeight( yMax ); }
126-
//! Set viewport height
127-
//! @note added in 2.8
128-
void setViewportHeight( double height );
146+
void setYMaximum( double yMax ) { mHeight = yMax; }
147+
129148
//! Set minimum y value
149+
// @deprecated in 2.8, use setParameters
130150
void setYMinimum( double ymin );
151+
131152
//! set minimum x value
153+
// @deprecated in 2.8, use setParameters
132154
void setXMinimum( double xmin );
155+
133156
/*! Set parameters for use in transforming coordinates
134157
* @param mapUnitsPerPixel Map units per pixel
135158
* @param xmin Minimum x value
136159
* @param ymin Minimum y value
137160
* @param height Map height, in pixels
161+
* @deprecated in 2.8, use the version with full parameters
138162
*/
139163
void setParameters( double mapUnitsPerPixel, double xmin, double ymin, double height );
164+
165+
/*! Set parameters for use in transforming coordinates
166+
* @param mapUnitsPerPixel Map units per pixel
167+
* @param xc X ordinate of map center, in geographical units
168+
* @param yc Y ordinate of map center, in geographical units
169+
* @param width Output width, in pixels
170+
* @param height Output height, in pixels
171+
* @param degrees clockwise rotation in degrees
172+
* @note added in 2.8
173+
*/
174+
void setParameters( double mapUnitsPerPixel, double xc, double yc, int width, int height, double rotation );
175+
140176
//! String representation of the parameters used in the transform
141177
QString showParameters();
142178

143179
private:
144180
double mMapUnitsPerPixel;
145-
double mHeight;
146-
double yMin;
147-
double xMin;
148-
//! Map rotation around Z axis on map center as clockwise degrees
149-
//! @note added in 2.8
150-
double mMapRotation;
181+
int mWidth;
182+
int mHeight;
183+
double mRotation;
151184
double xCenter;
152185
double yCenter;
186+
double xMin; // @deprecated in 2.8
187+
double yMin; // @deprecated in 2.8
188+
QTransform mMatrix;
153189

154190
// Matrix to map from map (geographical) to screen (pixels) units
155-
QTransform getMatrix() const;
191+
const QTransform& getMatrix() const;
192+
193+
void updateMatrix();
156194
};
157195

158196

0 commit comments

Comments
 (0)