Skip to content

Commit 51a3657

Browse files
author
mhugent
committedOct 23, 2010
Add method to convert double precision device coordinates to map coords (important for print composer). Change pallabeling to use FontMetricsF and toMapCoordinatesF. Fixes bug #3065
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14430 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent ad7a771 commit 51a3657

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed
 

‎python/core/qgsmaptopixel.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ class QgsMapToPixel
5353
// std::vector<double>& y);
5454

5555
QgsPoint toMapCoordinates(int x, int y);
56+
57+
/*! Transform device coordinates to map (world) coordinates
58+
@note: this method was added in version 1.6*/
59+
QgsPoint toMapCoordinatesF( double x, double y ) const;
60+
5661
/*! Tranform device coordinates to map (world) coordinates
5762
* @param p Point to be converted to map cooordinates
5863
* @return QgsPoint in map coorndiates

‎src/core/qgsmaptopixel.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ QgsMapToPixel::~QgsMapToPixel()
3636
{
3737
}
3838

39-
QgsPoint QgsMapToPixel::toMapPoint( int x, int y ) const
39+
QgsPoint QgsMapToPixel::toMapPoint( double x, double y ) const
4040
{
4141
double mx = x * mMapUnitsPerPixel + xMin;
4242
double my = -1 * (( y - yMax ) * mMapUnitsPerPixel - yMin );
@@ -54,6 +54,11 @@ QgsPoint QgsMapToPixel::toMapCoordinates( int x, int y ) const
5454
return toMapPoint( x, y );
5555
}
5656

57+
QgsPoint QgsMapToPixel::toMapCoordinatesF( double x, double y ) const
58+
{
59+
return toMapPoint( x, y );
60+
}
61+
5762
void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
5863
{
5964
mMapUnitsPerPixel = mapUnitsPerPixel;

‎src/core/qgsmaptopixel.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,18 @@ class CORE_EXPORT QgsMapToPixel
7575
std::vector<double>& y ) const;
7676

7777
QgsPoint toMapCoordinates( int x, int y ) const;
78+
79+
/*! Transform device coordinates to map (world) coordinates
80+
@note: this method was added in version 1.6*/
81+
QgsPoint toMapCoordinatesF( double x, double y ) const;
82+
7883
/*! Tranform device coordinates to map (world) coordinates
7984
* @param p Point to be converted to map cooordinates
8085
* @return QgsPoint in map coorndiates
8186
*/
8287
QgsPoint toMapCoordinates( QPoint p ) const;
8388

84-
QgsPoint toMapPoint( int x, int y ) const;
89+
QgsPoint toMapPoint( double x, double y ) const;
8590
/*! Set map units per pixel
8691
* @param mapUnitsPerPixel Map units per pixel
8792
*/

‎src/core/qgspallabeling.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ class QgsPalGeometry : public PalGeometry
7878
const char* strId() { return mStrId.data(); }
7979
QString text() { return mText; }
8080

81-
pal::LabelInfo* info( QFontMetrics* fm, const QgsMapToPixel* xform, double fontScale )
81+
pal::LabelInfo* info( QFontMetricsF* fm, const QgsMapToPixel* xform, double fontScale )
8282
{
8383
if ( mInfo ) return mInfo;
8484

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

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

368-
void QgsPalLayerSettings::calculateLabelSize( const QFontMetrics* fm, QString text, double& labelX, double& labelY )
368+
void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString text, double& labelX, double& labelY )
369369
{
370370
if ( !fm )
371371
{
@@ -426,7 +426,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
426426
}
427427
labelFont.setPixelSize( sizeToPixel( sizeDouble, context ) );
428428
}
429-
QFontMetrics labelFontMetrics( labelFont );
429+
QFontMetricsF labelFontMetrics( labelFont );
430430
calculateLabelSize( &labelFontMetrics, labelText, labelX, labelY );
431431
}
432432
else
@@ -704,7 +704,7 @@ int QgsPalLabeling::prepareLayer( QgsVectorLayer* layer, QSet<int>& attrIndices,
704704
// save the pal layer to our layer context (with some additional info)
705705
lyr.palLayer = l;
706706
lyr.fieldIndex = fldIndex;
707-
lyr.fontMetrics = new QFontMetrics( lyr.textFont );
707+
lyr.fontMetrics = new QFontMetricsF( lyr.textFont );
708708

709709
lyr.xform = mMapRenderer->coordinateTransform();
710710
if ( mMapRenderer->hasCrsTransformEnabled() )

‎src/core/qgspallabeling.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#ifndef QGSPALLABELING_H
2222
#define QGSPALLABELING_H
2323

24+
class QFontMetricsF;
2425
class QPainter;
2526
class QgsMapRenderer;
2627
class QgsRectangle;
@@ -115,7 +116,7 @@ class CORE_EXPORT QgsPalLayerSettings
115116
bool fontSizeInMapUnits; //true if font size is in map units (otherwise in points)
116117

117118
// called from register feature hook
118-
void calculateLabelSize( const QFontMetrics* fm, QString text, double& labelX, double& labelY );
119+
void calculateLabelSize( const QFontMetricsF* fm, QString text, double& labelX, double& labelY );
119120

120121
// implementation of register feature hook
121122
void registerFeature( QgsFeature& f, const QgsRenderContext& context );
@@ -131,7 +132,7 @@ class CORE_EXPORT QgsPalLayerSettings
131132
// temporary stuff: set when layer gets prepared
132133
pal::Layer* palLayer;
133134
int fieldIndex;
134-
QFontMetrics* fontMetrics;
135+
QFontMetricsF* fontMetrics;
135136
const QgsMapToPixel* xform;
136137
const QgsCoordinateTransform* ct;
137138
QgsPoint ptZero, ptOne;

0 commit comments

Comments
 (0)
Please sign in to comment.