Skip to content

Commit

Permalink
Fix some clang truncate to int warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 26, 2018
1 parent 1a2d0cc commit 1ae010a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion python/core/auto_generated/qgsscalecalculator.sip.in
Expand Up @@ -57,7 +57,7 @@ Set the map units
Returns current map units
%End

double calculate( const QgsRectangle &mapExtent, int canvasWidth );
double calculate( const QgsRectangle &mapExtent, double canvasWidth );
%Docstring
Calculate the scale denominator

Expand Down
30 changes: 15 additions & 15 deletions src/core/layout/qgslayoutitemmap.cpp
Expand Up @@ -837,21 +837,21 @@ void QgsLayoutItemMap::paint( QPainter *painter, const QStyleOptionGraphicsItem
// rasterize
double destinationDpi = QgsLayoutUtils::scaleFactorFromItemStyle( style ) * 25.4;
double layoutUnitsInInches = mLayout ? mLayout->convertFromLayoutUnits( 1, QgsUnitTypes::LayoutInches ).length() : 1;
int widthInPixels = std::round( boundingRect().width() * layoutUnitsInInches * destinationDpi );
int heightInPixels = std::round( boundingRect().height() * layoutUnitsInInches * destinationDpi );
int widthInPixels = static_cast< int >( std::round( boundingRect().width() * layoutUnitsInInches * destinationDpi ) );
int heightInPixels = static_cast< int >( std::round( boundingRect().height() * layoutUnitsInInches * destinationDpi ) );
QImage image = QImage( widthInPixels, heightInPixels, QImage::Format_ARGB32 );

image.fill( Qt::transparent );
image.setDotsPerMeterX( 1000 * destinationDpi / 25.4 );
image.setDotsPerMeterY( 1000 * destinationDpi / 25.4 );
image.setDotsPerMeterX( static_cast< int >( std::round( 1000 * destinationDpi / 25.4 ) ) );
image.setDotsPerMeterY( static_cast< int >( std::round( 1000 * destinationDpi / 25.4 ) ) );
double dotsPerMM = destinationDpi / 25.4;
QPainter p( &image );

QPointF tl = -boundingRect().topLeft();
QRect imagePaintRect( std::round( tl.x() * dotsPerMM ),
std::round( tl.y() * dotsPerMM ),
std::round( thisPaintRect.width() * dotsPerMM ),
std::round( thisPaintRect.height() * dotsPerMM ) );
QRect imagePaintRect( static_cast< int >( std::round( tl.x() * dotsPerMM ) ),
static_cast< int >( std::round( tl.y() * dotsPerMM ) ),
static_cast< int >( std::round( thisPaintRect.width() * dotsPerMM ) ),
static_cast< int >( std::round( thisPaintRect.height() * dotsPerMM ) ) );
p.setClipRect( imagePaintRect );

p.translate( imagePaintRect.topLeft() );
Expand Down Expand Up @@ -884,7 +884,7 @@ void QgsLayoutItemMap::paint( QPainter *painter, const QStyleOptionGraphicsItem

painter->save();
painter->scale( 1 / dotsPerMM, 1 / dotsPerMM ); // scale painter from mm to dots
painter->drawImage( std::round( -tl.x()* dotsPerMM ), std::round( -tl.y() * dotsPerMM ), image );
painter->drawImage( QPointF( -tl.x()* dotsPerMM, -tl.y() * dotsPerMM ), image );
painter->scale( dotsPerMM, dotsPerMM );
painter->restore();
}
Expand Down Expand Up @@ -994,21 +994,21 @@ void QgsLayoutItemMap::recreateCachedImageInBackground()
double widthLayoutUnits = ext.width() * mapUnitsToLayoutUnits();
double heightLayoutUnits = ext.height() * mapUnitsToLayoutUnits();

int w = widthLayoutUnits * mPreviewScaleFactor;
int h = heightLayoutUnits * mPreviewScaleFactor;
int w = static_cast< int >( std::round( widthLayoutUnits * mPreviewScaleFactor ) );
int h = static_cast< int >( std::round( heightLayoutUnits * mPreviewScaleFactor ) );

// limit size of image for better performance
if ( w > 5000 || h > 5000 )
{
if ( w > h )
{
w = 5000;
h = w * heightLayoutUnits / widthLayoutUnits;
h = static_cast< int>( std::round( w * heightLayoutUnits / widthLayoutUnits ) );
}
else
{
h = 5000;
w = h * widthLayoutUnits / heightLayoutUnits;
w = static_cast< int >( std::round( h * widthLayoutUnits / heightLayoutUnits ) );
}
}

Expand All @@ -1018,8 +1018,8 @@ void QgsLayoutItemMap::recreateCachedImageInBackground()
mCacheRenderingImage.reset( new QImage( w, h, QImage::Format_ARGB32 ) );

// set DPI of the image
mCacheRenderingImage->setDotsPerMeterX( 1000 * w / widthLayoutUnits );
mCacheRenderingImage->setDotsPerMeterY( 1000 * h / heightLayoutUnits );
mCacheRenderingImage->setDotsPerMeterX( static_cast< int >( std::round( 1000 * w / widthLayoutUnits ) ) );
mCacheRenderingImage->setDotsPerMeterY( static_cast< int >( std::round( 1000 * h / heightLayoutUnits ) ) );

if ( hasBackground() )
{
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsscalecalculator.cpp
Expand Up @@ -47,7 +47,7 @@ QgsUnitTypes::DistanceUnit QgsScaleCalculator::mapUnits() const
return mMapUnits;
}

double QgsScaleCalculator::calculate( const QgsRectangle &mapExtent, int canvasWidth )
double QgsScaleCalculator::calculate( const QgsRectangle &mapExtent, double canvasWidth )
{
double conversionFactor = 0;
double delta = 0;
Expand Down Expand Up @@ -77,7 +77,7 @@ double QgsScaleCalculator::calculate( const QgsRectangle &mapExtent, int canvasW
delta = calculateGeographicDistance( mapExtent );
break;
}
if ( canvasWidth == 0 || qgsDoubleNear( mDpi, 0.0 ) )
if ( qgsDoubleNear( canvasWidth, 0. ) || qgsDoubleNear( mDpi, 0.0 ) )
{
QgsDebugMsg( QStringLiteral( "Can't calculate scale from the input values" ) );
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsscalecalculator.h
Expand Up @@ -70,7 +70,7 @@ class CORE_EXPORT QgsScaleCalculator
* \param canvasWidth Width of the map canvas in pixel (physical) units
* \returns scale denominator of current map view, e.g. 1000.0 for a 1:1000 map.
*/
double calculate( const QgsRectangle &mapExtent, int canvasWidth );
double calculate( const QgsRectangle &mapExtent, double canvasWidth );

/**
* Calculate the distance between two points in geographic coordinates.
Expand Down

0 comments on commit 1ae010a

Please sign in to comment.