@@ -108,6 +108,13 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
108
108
if ( currentLayerItem )
109
109
{
110
110
QString currentLayerId = currentLayerItem->data ().toString ();
111
+ int opacity = 255 ;
112
+ QgsMapLayer* currentLayer = QgsMapLayerRegistry::instance ()->mapLayer ( currentLayerId );
113
+ if ( currentLayer )
114
+ {
115
+ opacity = currentLayer->getTransparency ();
116
+ }
117
+
111
118
if ( visibleLayerIds.contains ( currentLayerId ) )
112
119
{
113
120
// Let the user omit the layer title item by having an empty layer title string
@@ -126,7 +133,7 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
126
133
maxXCoord = std::max ( maxXCoord, 2 * mBoxSpace + textWidthMillimeters ( mLayerFont , currentLayerItem->text () ) );
127
134
128
135
// and child items
129
- drawLayerChildItems ( painter, currentLayerItem, currentYCoordinate, maxXCoord );
136
+ drawLayerChildItems ( painter, currentLayerItem, currentYCoordinate, maxXCoord, opacity );
130
137
}
131
138
}
132
139
}
@@ -170,7 +177,7 @@ void QgsComposerLegend::adjustBoxSize()
170
177
}
171
178
}
172
179
173
- void QgsComposerLegend::drawLayerChildItems ( QPainter* p, QStandardItem* layerItem, double & currentYCoord, double & maxXCoord )
180
+ void QgsComposerLegend::drawLayerChildItems ( QPainter* p, QStandardItem* layerItem, double & currentYCoord, double & maxXCoord, int layerOpacity )
174
181
{
175
182
if ( !layerItem )
176
183
{
@@ -212,7 +219,7 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
212
219
if ( symbol ) // item with symbol?
213
220
{
214
221
// draw symbol
215
- drawSymbol ( p, symbol, currentYCoord + ( itemHeight - mSymbolHeight ) / 2 , currentXCoord, realSymbolHeight );
222
+ drawSymbol ( p, symbol, currentYCoord + ( itemHeight - mSymbolHeight ) / 2 , currentXCoord, realSymbolHeight, layerOpacity );
216
223
realItemHeight = std::max ( realSymbolHeight, itemHeight );
217
224
currentXCoord += mIconLabelSpace ;
218
225
}
@@ -239,7 +246,7 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
239
246
}
240
247
}
241
248
242
- void QgsComposerLegend::drawSymbol ( QPainter* p, QgsSymbol* s, double currentYCoord, double & currentXPosition, double & symbolHeight ) const
249
+ void QgsComposerLegend::drawSymbol ( QPainter* p, QgsSymbol* s, double currentYCoord, double & currentXPosition, double & symbolHeight, int layerOpacity ) const
243
250
{
244
251
if ( !s )
245
252
{
@@ -250,14 +257,14 @@ void QgsComposerLegend::drawSymbol( QPainter* p, QgsSymbol* s, double currentYCo
250
257
switch ( symbolType )
251
258
{
252
259
case QGis::Point :
253
- drawPointSymbol ( p, s, currentYCoord, currentXPosition, symbolHeight );
260
+ drawPointSymbol ( p, s, currentYCoord, currentXPosition, symbolHeight, layerOpacity );
254
261
break ;
255
262
case QGis::Line:
256
- drawLineSymbol ( p, s, currentYCoord, currentXPosition );
263
+ drawLineSymbol ( p, s, currentYCoord, currentXPosition, layerOpacity );
257
264
symbolHeight = mSymbolHeight ;
258
265
break ;
259
266
case QGis::Polygon:
260
- drawPolygonSymbol ( p, s, currentYCoord, currentXPosition );
267
+ drawPolygonSymbol ( p, s, currentYCoord, currentXPosition, layerOpacity );
261
268
symbolHeight = mSymbolHeight ;
262
269
break ;
263
270
case QGis::UnknownGeometry:
@@ -266,7 +273,7 @@ void QgsComposerLegend::drawSymbol( QPainter* p, QgsSymbol* s, double currentYCo
266
273
}
267
274
}
268
275
269
- void QgsComposerLegend::drawPointSymbol ( QPainter* p, QgsSymbol* s, double currentYCoord, double & currentXPosition, double & symbolHeight ) const
276
+ void QgsComposerLegend::drawPointSymbol ( QPainter* p, QgsSymbol* s, double currentYCoord, double & currentXPosition, double & symbolHeight, int opacity ) const
270
277
{
271
278
if ( !s )
272
279
{
@@ -287,7 +294,7 @@ void QgsComposerLegend::drawPointSymbol( QPainter* p, QgsSymbol* s, double curre
287
294
}
288
295
289
296
// width scale is 1.0
290
- pointImage = s->getPointSymbolAsImage ( 1.0 , false , Qt::yellow, 1.0 , 0.0 , rasterScaleFactor );
297
+ pointImage = s->getPointSymbolAsImage ( 1.0 , false , Qt::yellow, 1.0 , 0.0 , rasterScaleFactor, opacity / 255.0 );
291
298
292
299
if ( p )
293
300
{
@@ -303,7 +310,7 @@ void QgsComposerLegend::drawPointSymbol( QPainter* p, QgsSymbol* s, double curre
303
310
symbolHeight = s->pointSize (); // pointImage.height() / rasterScaleFactor;
304
311
}
305
312
306
- void QgsComposerLegend::drawLineSymbol ( QPainter* p, QgsSymbol* s, double currentYCoord, double & currentXPosition ) const
313
+ void QgsComposerLegend::drawLineSymbol ( QPainter* p, QgsSymbol* s, double currentYCoord, double & currentXPosition, int opacity ) const
307
314
{
308
315
if ( !s )
309
316
{
@@ -315,15 +322,19 @@ void QgsComposerLegend::drawLineSymbol( QPainter* p, QgsSymbol* s, double curren
315
322
if ( p )
316
323
{
317
324
p->save ();
318
- p->setPen ( s->pen () );
325
+ QPen symbolPen = s->pen ();
326
+ QColor penColor = symbolPen.color ();
327
+ penColor.setAlpha ( opacity );
328
+ symbolPen.setColor ( penColor );
329
+ p->setPen ( symbolPen );
319
330
p->drawLine ( QPointF ( currentXPosition, yCoord ), QPointF ( currentXPosition + mSymbolWidth , yCoord ) );
320
331
p->restore ();
321
332
}
322
333
323
334
currentXPosition += mSymbolWidth ;
324
335
}
325
336
326
- void QgsComposerLegend::drawPolygonSymbol ( QPainter* p, QgsSymbol* s, double currentYCoord, double & currentXPosition ) const
337
+ void QgsComposerLegend::drawPolygonSymbol ( QPainter* p, QgsSymbol* s, double currentYCoord, double & currentXPosition, int opacity ) const
327
338
{
328
339
if ( !s )
329
340
{
@@ -332,16 +343,25 @@ void QgsComposerLegend::drawPolygonSymbol( QPainter* p, QgsSymbol* s, double cur
332
343
333
344
if ( p )
334
345
{
335
- // scale brush
346
+ // scale brush and set transparencies
336
347
QBrush symbolBrush = s->brush ();
348
+ QColor brushColor = symbolBrush.color ();
349
+ brushColor.setAlpha ( opacity );
350
+ symbolBrush.setColor ( brushColor );
337
351
QPaintDevice* paintDevice = p->device ();
338
352
if ( paintDevice )
339
353
{
340
354
double rasterScaleFactor = ( paintDevice->logicalDpiX () + paintDevice->logicalDpiY () ) / 2.0 / 25.4 ;
341
355
QgsRenderer::scaleBrush ( symbolBrush, rasterScaleFactor );
342
356
}
343
357
p->setBrush ( symbolBrush );
344
- p->setPen ( s->pen () );
358
+
359
+ QPen symbolPen = s->pen ();
360
+ QColor penColor = symbolPen.color ();
361
+ penColor.setAlpha ( opacity );
362
+ symbolPen.setColor ( penColor );
363
+ p->setPen ( symbolPen );
364
+
345
365
p->drawRect ( QRectF ( currentXPosition, currentYCoord, mSymbolWidth , mSymbolHeight ) );
346
366
}
347
367
0 commit comments