void QgsLayoutItemScaleBar::resizeToMinimumWidth() { if ( !mStyle ) return; double widthMM = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width(); QgsLayoutSize currentSize = sizeWithUnits(); currentSize.setWidth( mLayout->renderContext().measurementConverter().convert( QgsLayoutMeasurement( widthMM, QgsUnitTypes::LayoutMillimeters ), currentSize.units() ).length() ); attemptResize( currentSize ); update(); emit changed(); }
void QgsLayoutItemLegend::adjustBoxSize() { if ( !mSizeToContents ) return; if ( !mInitialMapScaleCalculated ) { // this is messy - but until we have painted the item we have no knowledge of the current DPI // and so cannot correctly calculate the map scale. This results in incorrect size calculations // for marker symbols with size in map units, causing the legends to initially expand to huge // sizes if we attempt to calculate the box size first. return; } QgsLegendRenderer legendRenderer( mLegendModel.get(), mSettings ); QSizeF size = legendRenderer.minimumSize(); QgsDebugMsg( QStringLiteral( "width = %1 height = %2" ).arg( size.width() ).arg( size.height() ) ); if ( size.isValid() ) { QgsLayoutSize newSize = mLayout->convertFromLayoutUnits( size, sizeWithUnits().units() ); //set new rect, respecting position mode and data defined size/position attemptResize( newSize ); } }
void QgsLayoutItemLegend::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) { if ( !painter ) return; if ( mFilterAskedForUpdate ) { mFilterAskedForUpdate = false; doUpdateFilterByMap(); } int dpi = painter->device()->logicalDpiX(); double dotsPerMM = dpi / 25.4; if ( mLayout ) { mSettings.setUseAdvancedEffects( mLayout->renderContext().flags() & QgsLayoutRenderContext::FlagUseAdvancedEffects ); mSettings.setDpi( dpi ); } if ( mMap && mLayout ) { mSettings.setMmPerMapUnit( mLayout->convertFromLayoutUnits( mMap->mapUnitsToLayoutUnits(), QgsUnitTypes::LayoutMillimeters ).length() ); // use a temporary QgsMapSettings to find out real map scale QSizeF mapSizePixels = QSizeF( mMap->rect().width() * dotsPerMM, mMap->rect().height() * dotsPerMM ); QgsRectangle mapExtent = mMap->extent(); QgsMapSettings ms = mMap->mapSettings( mapExtent, mapSizePixels, dpi, false ); mSettings.setMapScale( ms.scale() ); } mInitialMapScaleCalculated = true; QgsLegendRenderer legendRenderer( mLegendModel.get(), mSettings ); legendRenderer.setLegendSize( mForceResize && mSizeToContents ? QSize() : rect().size() ); //adjust box if width or height is too small if ( mSizeToContents ) { QSizeF size = legendRenderer.minimumSize(); if ( mForceResize ) { mForceResize = false; //set new rect, respecting position mode and data defined size/position QgsLayoutSize newSize = mLayout->convertFromLayoutUnits( size, sizeWithUnits().units() ); attemptResize( newSize ); } else if ( size.height() > rect().height() || size.width() > rect().width() ) { //need to resize box QSizeF targetSize = rect().size(); if ( size.height() > targetSize.height() ) targetSize.setHeight( size.height() ); if ( size.width() > targetSize.width() ) targetSize.setWidth( size.width() ); QgsLayoutSize newSize = mLayout->convertFromLayoutUnits( targetSize, sizeWithUnits().units() ); //set new rect, respecting position mode and data defined size/position attemptResize( newSize ); } } QgsLayoutItem::paint( painter, itemStyle, pWidget ); }