Skip to content

Commit 9351aaa

Browse files
author
mhugent
committedAug 2, 2010
Use floating point numbers for size and dpi. This is necessary to avoid distortions on devices where painter units are not pixels (e.g. graphics scene in print composer)
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13995 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

6 files changed

+46
-18
lines changed

6 files changed

+46
-18
lines changed
 

‎python/core/qgscomposermap.sip

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ class QgsComposerMap : QgsComposerItem
4444
BoundaryDirection
4545
};
4646

47+
/**This function is deprecated*/
48+
void draw( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi );
49+
4750
/** \brief Draw to paint device
4851
@param extent map extent
4952
@param size size in scene coordinates
5053
@param dpi scene dpi*/
51-
void draw( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi );
54+
void draw( QPainter *painter, const QgsRectangle& extent, const QSizeF& size, double dpi );
5255

5356
/** \brief Reimplementation of QCanvasItem::paint - draw on canvas */
5457
void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );

‎python/core/qgsmaprenderer.sip

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,14 @@ class QgsMapRenderer : QObject
9090
void enableOverviewMode(bool isOverview = true);
9191

9292
void setOutputSize(QSize size, int dpi);
93+
void setOutputSize( QSizeF size, double dpi );
9394

9495
//!accessor for output dpi
95-
int outputDpi();
96+
double outputDpi();
97+
9698
//!accessor for output size
9799
QSize outputSize();
100+
QSizeF outputSizeF();
98101

99102
//! transform extent in layer's CRS to extent in output CRS
100103
QgsRectangle layerExtentToOutputExtent(QgsMapLayer* theLayer, QgsRectangle extent);

‎src/core/composer/qgscomposermap.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,14 @@ QgsComposerMap::~QgsComposerMap()
9696
{
9797
}
9898

99+
void QgsComposerMap::draw( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi )
100+
{
101+
draw( painter, extent, QSizeF( size.width(), size.height() ), dpi );
102+
}
103+
99104
/* This function is called by paint() and cache() to render the map. It does not override any functions
100105
from QGraphicsItem. */
101-
void QgsComposerMap::draw( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi )
106+
void QgsComposerMap::draw( QPainter *painter, const QgsRectangle& extent, const QSizeF& size, double dpi )
102107
{
103108
if ( !painter )
104109
{
@@ -206,7 +211,7 @@ void QgsComposerMap::cache( void )
206211

207212
QPainter p( &mCacheImage );
208213

209-
draw( &p, requestExtent, QSize( w, h ), mCacheImage.logicalDpiX() );
214+
draw( &p, requestExtent, QSizeF( w, h ), mCacheImage.logicalDpiX() );
210215
p.end();
211216
mCacheUpdated = true;
212217

@@ -293,7 +298,7 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
293298
QgsRectangle requestRectangle;
294299
requestedExtent( requestRectangle );
295300

296-
QSize theSize( requestRectangle.width() * mapUnitsToMM(), requestRectangle.height() * mapUnitsToMM() );
301+
QSizeF theSize( requestRectangle.width() * mapUnitsToMM(), requestRectangle.height() * mapUnitsToMM() );
297302
QgsPoint rotationPoint = QgsPoint(( mExtent.xMaximum() + mExtent.xMinimum() ) / 2.0, ( mExtent.yMaximum() + mExtent.yMinimum() ) / 2.0 );
298303

299304
//shift such that rotation point is at 0/0 point in the coordinate system

‎src/core/composer/qgscomposermap.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,15 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
7474
BoundaryDirection
7575
};
7676

77-
/** \brief Draw to paint device
78-
@param extent map extent
79-
@param size size in scene coordinates
80-
@param dpi scene dpi*/
77+
/**This function is deprecated*/
8178
void draw( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi );
8279

80+
/** \brief Draw to paint device
81+
@param extent map extent
82+
@param size size in scene coordinates
83+
@param dpi scene dpi*/
84+
void draw( QPainter *painter, const QgsRectangle& extent, const QSizeF& size, double dpi );
85+
8386
/** \brief Reimplementation of QCanvasItem::paint - draw on canvas */
8487
void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );
8588

‎src/core/qgsmaprenderer.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,38 @@ bool QgsMapRenderer::setExtent( const QgsRectangle& extent )
124124

125125

126126
void QgsMapRenderer::setOutputSize( QSize size, int dpi )
127+
{
128+
mSize = QSizeF( size.width(), size.height() );
129+
mScaleCalculator->setDpi( dpi );
130+
adjustExtentToSize();
131+
}
132+
133+
void QgsMapRenderer::setOutputSize( QSizeF size, double dpi )
127134
{
128135
mSize = size;
129136
mScaleCalculator->setDpi( dpi );
130137
adjustExtentToSize();
131138
}
132-
int QgsMapRenderer::outputDpi()
139+
140+
double QgsMapRenderer::outputDpi()
133141
{
134142
return mScaleCalculator->dpi();
135143
}
144+
136145
QSize QgsMapRenderer::outputSize()
146+
{
147+
return mSize.toSize();
148+
}
149+
150+
QSizeF QgsMapRenderer::outputSizeF()
137151
{
138152
return mSize;
139153
}
140154

141155
void QgsMapRenderer::adjustExtentToSize()
142156
{
143-
int myHeight = mSize.height();
144-
int myWidth = mSize.width();
157+
double myHeight = mSize.height();
158+
double myWidth = mSize.width();
145159

146160
QgsMapToPixel newCoordXForm;
147161

@@ -154,10 +168,8 @@ void QgsMapRenderer::adjustExtentToSize()
154168

155169
// calculate the translation and scaling parameters
156170
// mapUnitsPerPixel = map units per pixel
157-
double mapUnitsPerPixelY = static_cast<double>( mExtent.height() )
158-
/ static_cast<double>( myHeight );
159-
double mapUnitsPerPixelX = static_cast<double>( mExtent.width() )
160-
/ static_cast<double>( myWidth );
171+
double mapUnitsPerPixelY = mExtent.height() / myHeight;
172+
double mapUnitsPerPixelX = mExtent.width() / myWidth;
161173
mMapUnitsPerPixel = mapUnitsPerPixelY > mapUnitsPerPixelX ? mapUnitsPerPixelY : mapUnitsPerPixelX;
162174

163175
// calculate the actual extent of the mapCanvas

‎src/core/qgsmaprenderer.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,13 @@ class CORE_EXPORT QgsMapRenderer : public QObject
121121
void enableOverviewMode( bool isOverview = true ) { mOverview = isOverview; }
122122

123123
void setOutputSize( QSize size, int dpi );
124+
void setOutputSize( QSizeF size, double dpi );
124125

125126
//!accessor for output dpi
126-
int outputDpi();
127+
double outputDpi();
127128
//!accessor for output size
128129
QSize outputSize();
130+
QSizeF outputSizeF();
129131

130132
//! transform extent in layer's CRS to extent in output CRS
131133
QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );
@@ -249,7 +251,7 @@ class CORE_EXPORT QgsMapRenderer : public QObject
249251
//! indicates whether it's map image for overview
250252
bool mOverview;
251253

252-
QSize mSize;
254+
QSizeF mSize;
253255

254256
//! detemines whether on the fly projection support is enabled
255257
bool mProjectionsEnabled;

0 commit comments

Comments
 (0)
Please sign in to comment.