Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/layer/layerdetaildata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "qgslegendsettings.h"
#include "qgslayertreemodel.h"

#include "mmstyle.h"

LayerDetailData::LayerDetailData( QObject *parent )
: QObject{parent}
{
Expand Down Expand Up @@ -100,6 +102,14 @@ void LayerDetailData::setLayerTreeNode( QgsLayerTreeNode *newLayerTreeNode )
// setup render context for legend
QgsLegendSettings legendSettings;

QgsTextFormat textFormat = legendSettings.rstyle( QgsLegendStyle::Style::SymbolLabel ).textFormat();
textFormat.setSize( 14 ); // 14 is the pixel size of MMStyle::p5, used in form widgets
textFormat.setSizeUnit( Qgis::RenderUnit::Pixels );
textFormat.setColor( MMStyle::nightColor() );
legendSettings.rstyle( QgsLegendStyle::Style::SymbolLabel ).setTextFormat( textFormat );
legendSettings.rstyle( QgsLegendStyle::Style::SymbolLabel ).setMargin( 4 ); // match the top and left total margin of the legend
legendSettings.rstyle( QgsLegendStyle::Style::Title ).setMargin( 0 ); // Our empty Title still adds some margins, so we set those to zero

mLayerTreeNode->setCustomProperty( QStringLiteral( "legend/title-style" ), QStringLiteral( "hidden" ) );

QgsLayerTree *tree = new QgsLayerTree();
Expand Down
39 changes: 22 additions & 17 deletions app/layer/layerdetaillegendimageprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,36 @@ QImage LayerDetailLegendImageProvider::requestImage( const QString &, QSize *siz
// it by dpr.

QScreen *screen = QGuiApplication::screens().at( 0 );
qreal pdpi = screen->physicalDotsPerInch() * screen->devicePixelRatio();
qreal dpm = pdpi / 25.4;
const qreal ldpi = screen->logicalDotsPerInch();
const qreal dpr = screen->devicePixelRatio();

QSize desiredSize( requestedSize );
if ( desiredSize.isEmpty() )
{
// fallback size
desiredSize = QSize( 60, 60 );
}
const qreal dpmm = ldpi * dpr / 25.4;

// 60 is fallback size, not sure how it came to be
const int width = requestedSize.isEmpty() ? static_cast<int>( 60 * dpr ) : static_cast<int>( requestedSize.width() * dpr );
const int height = static_cast<int>( minimumSize.height() * dpmm );

QImage legend = QImage( desiredSize.width(), minimumSize.height() * dpm, QImage::Format_ARGB32_Premultiplied );
QImage legend = QImage( width, height, QImage::Format_ARGB32_Premultiplied );

QPainter painter( &legend );
painter.setRenderHint( QPainter::Antialiasing );
{
QPainter painter( &legend );
painter.setRenderHint( QPainter::Antialiasing );

QgsRenderContext context = QgsRenderContext::fromQPainter( &painter );

QgsRenderContext context = QgsRenderContext::fromQPainter( &painter );
painter.scale( dpmm, dpmm );

painter.scale( dpm, dpm );
legend.fill( Qt::transparent );

legend.fill( Qt::transparent );
renderer->drawLegend( context );
}

renderer->drawLegend( context );
// Tag with DPR only after painting so QPainter does not auto-scale the
// coordinate system by dpr (which would otherwise double-apply the factor).
legend.setDevicePixelRatio( dpr );

size->setHeight( desiredSize.height() );
size->setWidth( desiredSize.width() );
size->setWidth( static_cast<int>( width / dpr ) );
size->setHeight( static_cast<int>( height / dpr ) );

return legend;
}
Expand Down
Loading
Loading