/
qgscolorramplegendnode.cpp
343 lines (288 loc) · 13.5 KB
/
qgscolorramplegendnode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/***************************************************************************
qgscolorramplegendnode.cpp
--------------------------------------
Date : December 2020
Copyright : (C) 2020 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgscolorramplegendnode.h"
#include "qgslegendsettings.h"
#include "qgslayertreemodel.h"
#include "qgslayertreelayer.h"
#include "qgssymbollayerutils.h"
#include "qgsexpressioncontextutils.h"
#include "qgstextrenderer.h"
#include "qgsnumericformat.h"
QgsColorRampLegendNode::QgsColorRampLegendNode( QgsLayerTreeLayer *nodeLayer, QgsColorRamp *ramp, const QString &minimumLabel, const QString &maximumLabel, QObject *parent )
: QgsLayerTreeModelLegendNode( nodeLayer, parent )
, mRamp( ramp )
{
mSettings.setMinimumLabel( minimumLabel );
mSettings.setMaximumLabel( maximumLabel );
const int iconSize = QgsLayerTreeModel::scaleIconSize( 16 );
mIconSize = QSize( iconSize, iconSize * 6 );
connect( nodeLayer, &QObject::destroyed, this, [ = ]() { mLayerNode = nullptr; } );
}
QgsColorRampLegendNode::QgsColorRampLegendNode( QgsLayerTreeLayer *nodeLayer, QgsColorRamp *ramp, const QgsColorRampLegendNodeSettings &settings, double minimumValue, double maximumValue, QObject *parent )
: QgsLayerTreeModelLegendNode( nodeLayer, parent )
, mRamp( ramp )
, mSettings( settings )
, mMinimumValue( minimumValue )
, mMaximumValue( maximumValue )
{
const int iconSize = QgsLayerTreeModel::scaleIconSize( 16 );
mIconSize = QSize( iconSize, iconSize * 6 );
connect( nodeLayer, &QObject::destroyed, this, [ = ]() { mLayerNode = nullptr; } );
}
const QgsColorRamp *QgsColorRampLegendNode::ramp() const
{
return mRamp.get();
}
QgsColorRampLegendNodeSettings QgsColorRampLegendNode::settings() const
{
return mSettings;
}
void QgsColorRampLegendNode::setSettings( const QgsColorRampLegendNodeSettings &settings )
{
mSettings = settings;
}
QString QgsColorRampLegendNode::labelForMinimum() const
{
if ( !mSettings.minimumLabel().isEmpty() )
return mSettings.prefix() + mSettings.minimumLabel() + mSettings.suffix();
QgsNumericFormatContext numericContext;
return mSettings.prefix() + mSettings.numericFormat()->formatDouble( mMinimumValue, numericContext ) + mSettings.suffix();
}
QString QgsColorRampLegendNode::labelForMaximum() const
{
if ( !mSettings.maximumLabel().isEmpty() )
return mSettings.prefix() + mSettings.maximumLabel() + mSettings.suffix();
QgsNumericFormatContext numericContext;
return mSettings.prefix() + mSettings.numericFormat()->formatDouble( mMaximumValue, numericContext ) + mSettings.suffix();
}
QVariant QgsColorRampLegendNode::data( int role ) const
{
if ( role == Qt::DisplayRole )
{
return QString();
}
else if ( role == Qt::EditRole )
{
return QString();
}
else if ( role == Qt::DecorationRole )
{
if ( mPixmap.isNull() || mPixmap.size() != mIconSize )
{
QPixmap pix;
if ( mRamp )
{
pix = QgsSymbolLayerUtils::colorRampPreviewPixmap( mRamp.get(), mIconSize, 0, Qt::Vertical, mSettings.direction() != QgsColorRampLegendNodeSettings::MaximumToMinimum, false );
}
else
{
pix = QPixmap( mIconSize );
pix.fill( Qt::transparent );
}
const QFont font = data( Qt::FontRole ).value< QFont >();
const QString minLabel = labelForMinimum();
const QString maxLabel = labelForMaximum();
const QFontMetrics fm( font );
const int maxTextWidth = std::max( fm.boundingRect( minLabel ).width(), fm.boundingRect( maxLabel ).width() );
const int labelGapFromRamp = fm.boundingRect( QStringLiteral( "x" ) ).width();
const int extraAllowance = labelGapFromRamp * 0.4; // extra allowance to avoid text clipping on right
const QRect labelRect( mIconSize.width() + labelGapFromRamp, 0, maxTextWidth + extraAllowance, mIconSize.height() );
mPixmap = QPixmap( mIconSize.width() + maxTextWidth + labelGapFromRamp + extraAllowance, mIconSize.height() );
mPixmap.fill( Qt::transparent );
QPainter p( &mPixmap );
p.drawPixmap( 0, 0, pix );
p.setFont( font );
p.drawText( labelRect, Qt::AlignBottom | Qt::AlignLeft, mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? minLabel : maxLabel );
p.drawText( labelRect, Qt::AlignTop | Qt::AlignLeft, mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? maxLabel : minLabel );
p.end();
}
return mPixmap;
}
else if ( role == QgsLayerTreeModelLegendNode::NodeTypeRole )
{
return QgsLayerTreeModelLegendNode::ColorRampLegend;
}
return QVariant();
}
QSizeF QgsColorRampLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double ) const
{
if ( !mRamp )
{
return QSizeF();
}
// setup temporary render context
QgsRenderContext *context = nullptr;
std::unique_ptr< QgsRenderContext > tempRenderContext;
if ( ctx && ctx->context )
context = ctx->context;
else
{
tempRenderContext = qgis::make_unique< QgsRenderContext >();
// QGIS 4.0 - make ItemContext compulsory, so we don't have to construct temporary render contexts here
Q_NOWARN_DEPRECATED_PUSH
tempRenderContext->setScaleFactor( settings.dpi() / 25.4 );
tempRenderContext->setRendererScale( settings.mapScale() );
tempRenderContext->setFlag( QgsRenderContext::Antialiasing, true );
tempRenderContext->setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
Q_NOWARN_DEPRECATED_POP
tempRenderContext->setForceVectorOutput( true );
tempRenderContext->setPainter( ctx ? ctx->painter : nullptr );
// setup a minimal expression context
QgsExpressionContext expContext;
expContext.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( nullptr ) );
tempRenderContext->setExpressionContext( expContext );
context = tempRenderContext.get();
}
QFont symbolLabelFont = settings.style( QgsLegendStyle::SymbolLabel ).font();
QgsTextFormat format = QgsTextFormat::fromQFont( symbolLabelFont );
format.setColor( settings.fontColor() );
const QString minLabel = labelForMinimum();
const QString maxLabel = labelForMaximum();
double minHeightMm = QgsTextRenderer::textHeight( *context, format, QStringList() << minLabel << maxLabel, QgsTextRenderer::Rect ) / context->scaleFactor();
const double height = ctx && ctx->patchSize.height() > 0 ? std::max( minHeightMm / 2, ctx->patchSize.height() ) : std::max( minHeightMm, settings.symbolSize().height() );
const double width = ctx && ctx->patchSize.width() > 0 ? ctx->patchSize.width() : settings.symbolSize().width();
if ( ctx && ctx->painter )
{
double currentYCoord = ctx->top;
QPainter *p = ctx->painter;
//setup painter scaling to dots so that raster symbology is drawn to scale
double dotsPerMM = context->scaleFactor();
double opacity = 1;
if ( QgsMapLayer *layer = layerNode()->layer() )
opacity = layer->opacity();
QgsScopedQPainterState painterState( p );
context->setPainterFlagsUsingContext( p );
double rampLeftMm = 0;
double rampTopMm = currentYCoord;
switch ( settings.symbolAlignment() )
{
case Qt::AlignLeft:
default:
rampLeftMm = ctx->columnLeft;
break;
case Qt::AlignRight:
rampLeftMm = ctx->columnRight - width;
break;
}
p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
const double gradientTop = rampTopMm * dotsPerMM;
const double gradientBottom = gradientTop + height * dotsPerMM;
QLinearGradient gradient( 0, mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? gradientBottom : gradientTop,
0, mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? gradientTop : gradientBottom );
if ( mRamp->type() == QgsGradientColorRamp::typeString() || mRamp->type() == QgsCptCityColorRamp::typeString() )
{
//color ramp gradient
QgsGradientColorRamp *gradRamp = static_cast<QgsGradientColorRamp *>( mRamp.get() );
gradRamp->addStopsToGradient( &gradient, opacity );
}
if ( settings.drawRasterStroke() )
{
QPen pen;
pen.setColor( settings.rasterStrokeColor() );
pen.setWidthF( settings.rasterStrokeWidth() * dotsPerMM );
pen.setJoinStyle( Qt::MiterJoin );
ctx->painter->setPen( pen );
}
else
{
ctx->painter->setPen( Qt::NoPen );
}
p->setBrush( QBrush( gradient ) );
p->drawRect( rampLeftMm * dotsPerMM, rampTopMm * dotsPerMM, width * dotsPerMM, height * dotsPerMM );
}
return QSizeF( width, height );
}
QSizeF QgsColorRampLegendNode::drawSymbolText( const QgsLegendSettings &settings, QgsLayerTreeModelLegendNode::ItemContext *ctx, QSizeF symbolSize ) const
{
if ( !mRamp )
{
return QSizeF();
}
// setup temporary render context
QgsRenderContext *context = nullptr;
std::unique_ptr< QgsRenderContext > tempRenderContext;
if ( ctx && ctx->context )
context = ctx->context;
else
{
tempRenderContext = qgis::make_unique< QgsRenderContext >();
// QGIS 4.0 - make ItemContext compulsory, so we don't have to construct temporary render contexts here
Q_NOWARN_DEPRECATED_PUSH
tempRenderContext->setScaleFactor( settings.dpi() / 25.4 );
tempRenderContext->setRendererScale( settings.mapScale() );
tempRenderContext->setFlag( QgsRenderContext::Antialiasing, true );
tempRenderContext->setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
Q_NOWARN_DEPRECATED_POP
tempRenderContext->setForceVectorOutput( true );
tempRenderContext->setPainter( ctx ? ctx->painter : nullptr );
// setup a minimal expression context
QgsExpressionContext expContext;
expContext.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( nullptr ) );
tempRenderContext->setExpressionContext( expContext );
context = tempRenderContext.get();
}
QFont symbolLabelFont = settings.style( QgsLegendStyle::SymbolLabel ).font();
QgsTextFormat format = QgsTextFormat::fromQFont( symbolLabelFont );
format.setColor( settings.fontColor() );
const QString minLabel = labelForMinimum();
const QString maxLabel = labelForMaximum();
const double height = symbolSize.height();
const double width = symbolSize.width();
double textWidth = 0;
if ( ctx && ctx->painter )
{
double currentYCoord = ctx->top;
QPainter *p = ctx->painter;
//setup painter scaling to dots so that raster symbology is drawn to scale
double dotsPerMM = context->scaleFactor();
QgsScopedQPainterState painterState( p );
context->setPainterFlagsUsingContext( p );
p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
double labelXMin = 0;
double labelXMax = 0;
switch ( settings.symbolAlignment() )
{
case Qt::AlignLeft:
default:
labelXMin = ctx->columnLeft + std::max( width, ctx->maxSiblingSymbolWidth )
+ settings.style( QgsLegendStyle::Symbol ).margin( QgsLegendStyle::Right )
+ settings.style( QgsLegendStyle::SymbolLabel ).margin( QgsLegendStyle::Left );
labelXMax = ctx->columnRight;
break;
case Qt::AlignRight:
labelXMin = ctx->columnLeft;
// NOTE -- while the below calculations use the flipped margins from the style, that's only done because
// those are the only margins we expose and use for now! (and we expose them as generic margins, not side-specific
// ones) TODO when/if we expose other margin settings, these should be reversed...
labelXMax = ctx->columnRight - std::max( width, ctx->maxSiblingSymbolWidth )
- settings.style( QgsLegendStyle::Symbol ).margin( QgsLegendStyle::Right )
- settings.style( QgsLegendStyle::SymbolLabel ).margin( QgsLegendStyle::Left );
break;
}
const QRectF textRect( labelXMin * dotsPerMM, currentYCoord * dotsPerMM, ( labelXMax - labelXMin ) * dotsPerMM, height * dotsPerMM );
QgsTextRenderer::drawText( textRect, 0, QgsTextRenderer::convertQtHAlignment( settings.style( QgsLegendStyle::SymbolLabel ).alignment() ),
QStringList() << ( mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? maxLabel : minLabel ),
*context, format, true, QgsTextRenderer::AlignTop );
QgsTextRenderer::drawText( textRect, 0, QgsTextRenderer::convertQtHAlignment( settings.style( QgsLegendStyle::SymbolLabel ).alignment() ),
QStringList() << ( mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? minLabel : maxLabel ),
*context, format, true, QgsTextRenderer::AlignBottom );
}
else
{
// we only need this when we are calculating the size of the node, not at render time
textWidth = QgsTextRenderer::textWidth( *context, format, QStringList() << minLabel << maxLabel ) / context->scaleFactor();
}
return QSizeF( textWidth, height );
}