Skip to content
Permalink
Browse files
Use floating point numbers for size and dpi. This is necessary to avo…
…id 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@13995 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Aug 2, 2010
1 parent f94e105 commit c8edb3da7b2ebe2128a1df058445f3ff2aa2574b
@@ -44,11 +44,14 @@ class QgsComposerMap : QgsComposerItem
BoundaryDirection
};

/**This function is deprecated*/
void draw( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi );

/** \brief Draw to paint device
@param extent map extent
@param size size in scene coordinates
@param dpi scene dpi*/
void draw( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi );
void draw( QPainter *painter, const QgsRectangle& extent, const QSizeF& size, double dpi );

/** \brief Reimplementation of QCanvasItem::paint - draw on canvas */
void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );
@@ -90,11 +90,14 @@ class QgsMapRenderer : QObject
void enableOverviewMode(bool isOverview = true);

void setOutputSize(QSize size, int dpi);
void setOutputSize( QSizeF size, double dpi );

//!accessor for output dpi
int outputDpi();
double outputDpi();

//!accessor for output size
QSize outputSize();
QSizeF outputSizeF();

//! transform extent in layer's CRS to extent in output CRS
QgsRectangle layerExtentToOutputExtent(QgsMapLayer* theLayer, QgsRectangle extent);
@@ -96,9 +96,14 @@ QgsComposerMap::~QgsComposerMap()
{
}

void QgsComposerMap::draw( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi )
{
draw( painter, extent, QSizeF( size.width(), size.height() ), dpi );
}

/* This function is called by paint() and cache() to render the map. It does not override any functions
from QGraphicsItem. */
void QgsComposerMap::draw( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi )
void QgsComposerMap::draw( QPainter *painter, const QgsRectangle& extent, const QSizeF& size, double dpi )
{
if ( !painter )
{
@@ -206,7 +211,7 @@ void QgsComposerMap::cache( void )

QPainter p( &mCacheImage );

draw( &p, requestExtent, QSize( w, h ), mCacheImage.logicalDpiX() );
draw( &p, requestExtent, QSizeF( w, h ), mCacheImage.logicalDpiX() );
p.end();
mCacheUpdated = true;

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

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

//shift such that rotation point is at 0/0 point in the coordinate system
@@ -74,12 +74,15 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
BoundaryDirection
};

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

/** \brief Draw to paint device
@param extent map extent
@param size size in scene coordinates
@param dpi scene dpi*/
void draw( QPainter *painter, const QgsRectangle& extent, const QSizeF& size, double dpi );

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

@@ -124,24 +124,38 @@ bool QgsMapRenderer::setExtent( const QgsRectangle& extent )


void QgsMapRenderer::setOutputSize( QSize size, int dpi )
{
mSize = QSizeF( size.width(), size.height() );
mScaleCalculator->setDpi( dpi );
adjustExtentToSize();
}

void QgsMapRenderer::setOutputSize( QSizeF size, double dpi )
{
mSize = size;
mScaleCalculator->setDpi( dpi );
adjustExtentToSize();
}
int QgsMapRenderer::outputDpi()

double QgsMapRenderer::outputDpi()
{
return mScaleCalculator->dpi();
}

QSize QgsMapRenderer::outputSize()
{
return mSize.toSize();
}

QSizeF QgsMapRenderer::outputSizeF()
{
return mSize;
}

void QgsMapRenderer::adjustExtentToSize()
{
int myHeight = mSize.height();
int myWidth = mSize.width();
double myHeight = mSize.height();
double myWidth = mSize.width();

QgsMapToPixel newCoordXForm;

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

// calculate the translation and scaling parameters
// mapUnitsPerPixel = map units per pixel
double mapUnitsPerPixelY = static_cast<double>( mExtent.height() )
/ static_cast<double>( myHeight );
double mapUnitsPerPixelX = static_cast<double>( mExtent.width() )
/ static_cast<double>( myWidth );
double mapUnitsPerPixelY = mExtent.height() / myHeight;
double mapUnitsPerPixelX = mExtent.width() / myWidth;
mMapUnitsPerPixel = mapUnitsPerPixelY > mapUnitsPerPixelX ? mapUnitsPerPixelY : mapUnitsPerPixelX;

// calculate the actual extent of the mapCanvas
@@ -121,11 +121,13 @@ class CORE_EXPORT QgsMapRenderer : public QObject
void enableOverviewMode( bool isOverview = true ) { mOverview = isOverview; }

void setOutputSize( QSize size, int dpi );
void setOutputSize( QSizeF size, double dpi );

//!accessor for output dpi
int outputDpi();
double outputDpi();
//!accessor for output size
QSize outputSize();
QSizeF outputSizeF();

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

QSize mSize;
QSizeF mSize;

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

0 comments on commit c8edb3d

Please sign in to comment.