Skip to content

Commit

Permalink
Add method to convert double precision device coordinates to map coor…
Browse files Browse the repository at this point in the history
…ds (important for print composer). Change pallabeling to use FontMetricsF and toMapCoordinatesF. Fixes bug #3065

git-svn-id: http://svn.osgeo.org/qgis/trunk@14430 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Oct 23, 2010
1 parent 2f466c7 commit 4c9b6b2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
5 changes: 5 additions & 0 deletions python/core/qgsmaptopixel.sip
Expand Up @@ -53,6 +53,11 @@ class QgsMapToPixel
// std::vector<double>& y);

QgsPoint toMapCoordinates(int x, int y);

/*! Transform device coordinates to map (world) coordinates
@note: this method was added in version 1.6*/
QgsPoint toMapCoordinatesF( double x, double y ) const;

/*! Tranform device coordinates to map (world) coordinates
* @param p Point to be converted to map cooordinates
* @return QgsPoint in map coorndiates
Expand Down
7 changes: 6 additions & 1 deletion src/core/qgsmaptopixel.cpp
Expand Up @@ -36,7 +36,7 @@ QgsMapToPixel::~QgsMapToPixel()
{
}

QgsPoint QgsMapToPixel::toMapPoint( int x, int y ) const
QgsPoint QgsMapToPixel::toMapPoint( double x, double y ) const
{
double mx = x * mMapUnitsPerPixel + xMin;
double my = -1 * (( y - yMax ) * mMapUnitsPerPixel - yMin );
Expand All @@ -54,6 +54,11 @@ QgsPoint QgsMapToPixel::toMapCoordinates( int x, int y ) const
return toMapPoint( x, y );
}

QgsPoint QgsMapToPixel::toMapCoordinatesF( double x, double y ) const
{
return toMapPoint( x, y );
}

void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
{
mMapUnitsPerPixel = mapUnitsPerPixel;
Expand Down
7 changes: 6 additions & 1 deletion src/core/qgsmaptopixel.h
Expand Up @@ -75,13 +75,18 @@ class CORE_EXPORT QgsMapToPixel
std::vector<double>& y ) const;

QgsPoint toMapCoordinates( int x, int y ) const;

/*! Transform device coordinates to map (world) coordinates
@note: this method was added in version 1.6*/
QgsPoint toMapCoordinatesF( double x, double y ) const;

/*! Tranform device coordinates to map (world) coordinates
* @param p Point to be converted to map cooordinates
* @return QgsPoint in map coorndiates
*/
QgsPoint toMapCoordinates( QPoint p ) const;

QgsPoint toMapPoint( int x, int y ) const;
QgsPoint toMapPoint( double x, double y ) const;
/*! Set map units per pixel
* @param mapUnitsPerPixel Map units per pixel
*/
Expand Down
12 changes: 6 additions & 6 deletions src/core/qgspallabeling.cpp
Expand Up @@ -78,19 +78,19 @@ class QgsPalGeometry : public PalGeometry
const char* strId() { return mStrId.data(); }
QString text() { return mText; }

pal::LabelInfo* info( QFontMetrics* fm, const QgsMapToPixel* xform, double fontScale )
pal::LabelInfo* info( QFontMetricsF* fm, const QgsMapToPixel* xform, double fontScale )
{
if ( mInfo ) return mInfo;

// create label info!
QgsPoint ptZero = xform->toMapCoordinates( 0, 0 );
QgsPoint ptSize = xform->toMapCoordinates( 0, ( int )( -fm->height() / fontScale ) );
QgsPoint ptSize = xform->toMapCoordinatesF( 0.0, -fm->height() / fontScale );

mInfo = new pal::LabelInfo( mText.count(), ptSize.y() - ptZero.y() );
for ( int i = 0; i < mText.count(); i++ )
{
mInfo->char_info[i].chr = mText[i].unicode();
ptSize = xform->toMapCoordinates(( int )( fm->width( mText[i] ) / fontScale ) , 0 );
ptSize = xform->toMapCoordinatesF( fm->width( mText[i] ) / fontScale , 0.0 );
mInfo->char_info[i].width = ptSize.x() - ptZero.x();
}
return mInfo;
Expand Down Expand Up @@ -365,7 +365,7 @@ bool QgsPalLayerSettings::checkMinimumSizeMM( const QgsRenderContext& ct, QgsGeo
return true; //should never be reached. Return true in this case to label such geometries anyway.
}

void QgsPalLayerSettings::calculateLabelSize( const QFontMetrics* fm, QString text, double& labelX, double& labelY )
void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString text, double& labelX, double& labelY )
{
if ( !fm )
{
Expand Down Expand Up @@ -426,7 +426,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
}
labelFont.setPixelSize( sizeToPixel( sizeDouble, context ) );
}
QFontMetrics labelFontMetrics( labelFont );
QFontMetricsF labelFontMetrics( labelFont );
calculateLabelSize( &labelFontMetrics, labelText, labelX, labelY );
}
else
Expand Down Expand Up @@ -704,7 +704,7 @@ int QgsPalLabeling::prepareLayer( QgsVectorLayer* layer, QSet<int>& attrIndices,
// save the pal layer to our layer context (with some additional info)
lyr.palLayer = l;
lyr.fieldIndex = fldIndex;
lyr.fontMetrics = new QFontMetrics( lyr.textFont );
lyr.fontMetrics = new QFontMetricsF( lyr.textFont );

lyr.xform = mMapRenderer->coordinateTransform();
if ( mMapRenderer->hasCrsTransformEnabled() )
Expand Down
5 changes: 3 additions & 2 deletions src/core/qgspallabeling.h
Expand Up @@ -21,6 +21,7 @@
#ifndef QGSPALLABELING_H
#define QGSPALLABELING_H

class QFontMetricsF;
class QPainter;
class QgsMapRenderer;
class QgsRectangle;
Expand Down Expand Up @@ -115,7 +116,7 @@ class CORE_EXPORT QgsPalLayerSettings
bool fontSizeInMapUnits; //true if font size is in map units (otherwise in points)

// called from register feature hook
void calculateLabelSize( const QFontMetrics* fm, QString text, double& labelX, double& labelY );
void calculateLabelSize( const QFontMetricsF* fm, QString text, double& labelX, double& labelY );

// implementation of register feature hook
void registerFeature( QgsFeature& f, const QgsRenderContext& context );
Expand All @@ -131,7 +132,7 @@ class CORE_EXPORT QgsPalLayerSettings
// temporary stuff: set when layer gets prepared
pal::Layer* palLayer;
int fieldIndex;
QFontMetrics* fontMetrics;
QFontMetricsF* fontMetrics;
const QgsMapToPixel* xform;
const QgsCoordinateTransform* ct;
QgsPoint ptZero, ptOne;
Expand Down

0 comments on commit 4c9b6b2

Please sign in to comment.