void QgsMapRenderer::adjustExtentToSize() { double myHeight = mSize.height(); double myWidth = mSize.width(); QgsMapToPixel newCoordXForm; if ( !myWidth || !myHeight ) { mScale = 1; newCoordXForm.setParameters( 0, 0, 0, 0 ); return; } // calculate the translation and scaling parameters // mapUnitsPerPixel = map units per pixel double mapUnitsPerPixelY = mExtent.height() / myHeight; double mapUnitsPerPixelX = mExtent.width() / myWidth; mMapUnitsPerPixel = mapUnitsPerPixelY > mapUnitsPerPixelX ? mapUnitsPerPixelY : mapUnitsPerPixelX; // calculate the actual extent of the mapCanvas double dxmin, dxmax, dymin, dymax, whitespace; if ( mapUnitsPerPixelY > mapUnitsPerPixelX ) { dymin = mExtent.yMinimum(); dymax = mExtent.yMaximum(); whitespace = (( myWidth * mMapUnitsPerPixel ) - mExtent.width() ) * 0.5; dxmin = mExtent.xMinimum() - whitespace; dxmax = mExtent.xMaximum() + whitespace; } else { dxmin = mExtent.xMinimum(); dxmax = mExtent.xMaximum(); whitespace = (( myHeight * mMapUnitsPerPixel ) - mExtent.height() ) * 0.5; dymin = mExtent.yMinimum() - whitespace; dymax = mExtent.yMaximum() + whitespace; } QgsDebugMsg( QString( "Map units per pixel (x,y) : %1, %2\n" ).arg( mapUnitsPerPixelX ).arg( mapUnitsPerPixelY ) ); QgsDebugMsg( QString( "Pixmap dimensions (x,y) : %1, %2\n" ).arg( myWidth ).arg( myHeight ) ); QgsDebugMsg( QString( "Extent dimensions (x,y) : %1, %2\n" ).arg( mExtent.width() ).arg( mExtent.height() ) ); QgsDebugMsg( mExtent.toString() ); // update extent mExtent.setXMinimum( dxmin ); mExtent.setXMaximum( dxmax ); mExtent.setYMinimum( dymin ); mExtent.setYMaximum( dymax ); // update the scale updateScale(); QgsDebugMsg( QString( "Scale (assuming meters as map units) = 1:%1" ).arg( mScale ) ); newCoordXForm.setParameters( mMapUnitsPerPixel, dxmin, dymin, myHeight ); mRenderContext.setMapToPixel( newCoordXForm ); mRenderContext.setExtent( mExtent ); }
QgsLabelPreview::QgsLabelPreview( QWidget* parent ) : QLabel( parent ) { mTmpLyr = new QgsPalLayerSettings(); // construct a device-based render context QgsMapToPixel newCoordXForm; newCoordXForm.setParameters( 0, 0, 0, 0 ); mContext = new QgsRenderContext(); mContext->setMapToPixel( newCoordXForm ); }
QPixmap QgsFontButton::createDragIcon( QSize size, const QgsTextFormat *tempFormat, const QFont *tempFont ) const { if ( !tempFormat ) tempFormat = &mFormat; if ( !tempFont ) tempFont = &mFont; //create an icon pixmap QPixmap pixmap( size.width(), size.height() ); pixmap.fill( Qt::transparent ); QPainter p; p.begin( &pixmap ); p.setRenderHint( QPainter::Antialiasing ); QRect rect( 0, 0, size.width(), size.height() ); if ( mMode == ModeQFont || tempFormat->color().lightnessF() < 0.7 ) { p.setBrush( QBrush( QColor( 255, 255, 255 ) ) ); p.setPen( QPen( QColor( 150, 150, 150 ), 0 ) ); } else { p.setBrush( QBrush( QColor( 0, 0, 0 ) ) ); p.setPen( QPen( QColor( 100, 100, 100 ), 0 ) ); } p.drawRect( rect ); p.setBrush( Qt::NoBrush ); p.setPen( Qt::NoPen ); switch ( mMode ) { case ModeTextRenderer: { QgsRenderContext context; QgsMapToPixel newCoordXForm; newCoordXForm.setParameters( 1, 0, 0, 0, 0, 0 ); context.setMapToPixel( newCoordXForm ); context.setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 ); context.setUseAdvancedEffects( true ); context.setPainter( &p ); // slightly inset text to account for buffer/background double xtrans = 0; if ( tempFormat->buffer().enabled() ) xtrans = context.convertToPainterUnits( tempFormat->buffer().size(), tempFormat->buffer().sizeUnit(), tempFormat->buffer().sizeMapUnitScale() ); if ( tempFormat->background().enabled() && tempFormat->background().sizeType() != QgsTextBackgroundSettings::SizeFixed ) xtrans = std::max( xtrans, context.convertToPainterUnits( tempFormat->background().size().width(), tempFormat->background().sizeUnit(), tempFormat->background().sizeMapUnitScale() ) ); double ytrans = 0.0; if ( tempFormat->buffer().enabled() ) ytrans = std::max( ytrans, context.convertToPainterUnits( tempFormat->buffer().size(), tempFormat->buffer().sizeUnit(), tempFormat->buffer().sizeMapUnitScale() ) ); if ( tempFormat->background().enabled() ) ytrans = std::max( ytrans, context.convertToPainterUnits( tempFormat->background().size().height(), tempFormat->background().sizeUnit(), tempFormat->background().sizeMapUnitScale() ) ); QRectF textRect = rect; textRect.setLeft( xtrans ); textRect.setWidth( textRect.width() - xtrans ); textRect.setTop( ytrans ); if ( textRect.height() > 300 ) textRect.setHeight( 300 ); if ( textRect.width() > 2000 ) textRect.setWidth( 2000 ); QgsTextRenderer::drawText( textRect, 0, QgsTextRenderer::AlignCenter, QStringList() << tr( "Aa" ), context, *tempFormat ); break; } case ModeQFont: { p.setBrush( Qt::NoBrush ); p.setPen( QColor( 0, 0, 0 ) ); p.setFont( *tempFont ); QRectF textRect = rect; textRect.setLeft( 2 ); p.drawText( textRect, Qt::AlignVCenter, tr( "Aa" ) ); break; } } p.end(); return pixmap; }