Skip to content
Permalink
Browse files

Add method to convert double precision device coordinates to map coor…

…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
mhugent committed Oct 23, 2010
1 parent 2f466c7 commit 4c9b6b2071bbcc6a32a6baef9a91672ef76aa7b2
@@ -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
@@ -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 );
@@ -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;
@@ -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
*/
@@ -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;
@@ -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 )
{
@@ -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
@@ -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() )
@@ -21,6 +21,7 @@
#ifndef QGSPALLABELING_H
#define QGSPALLABELING_H

class QFontMetricsF;
class QPainter;
class QgsMapRenderer;
class QgsRectangle;
@@ -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 );
@@ -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;

0 comments on commit 4c9b6b2

Please sign in to comment.
You can’t perform that action at this time.