Skip to content

Commit a51fb58

Browse files
author
mhugent
committed
Consider transparency in composer legend (even though legend items for transparent layers are problematic because of background). Removed private section from qgslegendmodel.sip as private members cannot be used anyway
git-svn-id: http://svn.osgeo.org/qgis/trunk@11439 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 52c420b commit a51fb58

File tree

5 files changed

+71
-50
lines changed

5 files changed

+71
-50
lines changed

python/core/qgslegendmodel.sip

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,4 @@ class QgsLegendModel: QStandardItemModel
3232

3333
signals:
3434
void layersChanged();
35-
36-
private:
37-
/**Adds classification items of vector layers
38-
@return 0 in case of success*/
39-
int addVectorLayerItems( QStandardItem* layerItem, QgsMapLayer* vlayer );
40-
41-
/**Adds item of raster layer
42-
@return 0 in case of success*/
43-
int addRasterLayerItem( QStandardItem* layerItem, QgsMapLayer* rlayer );
44-
45-
/**Insert a symbol into QgsLegendModel symbol storage*/
46-
void insertSymbol( QgsSymbol* s );
47-
/**Removes and deletes a symbol*/
48-
void removeSymbol( QgsSymbol* s );
49-
/**Removes and deletes all stored symbols*/
50-
void removeAllSymbols();
51-
52-
/**Creates a model item for a vector symbol. The calling function takes ownership*/
53-
QStandardItem* itemFromSymbol( QgsSymbol* s );
54-
};
35+
};

src/core/composer/qgscomposerlegend.cpp

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
108108
if ( currentLayerItem )
109109
{
110110
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+
111118
if ( visibleLayerIds.contains( currentLayerId ) )
112119
{
113120
//Let the user omit the layer title item by having an empty layer title string
@@ -126,7 +133,7 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
126133
maxXCoord = std::max( maxXCoord, 2 * mBoxSpace + textWidthMillimeters( mLayerFont, currentLayerItem->text() ) );
127134

128135
//and child items
129-
drawLayerChildItems( painter, currentLayerItem, currentYCoordinate, maxXCoord );
136+
drawLayerChildItems( painter, currentLayerItem, currentYCoordinate, maxXCoord, opacity );
130137
}
131138
}
132139
}
@@ -170,7 +177,7 @@ void QgsComposerLegend::adjustBoxSize()
170177
}
171178
}
172179

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 )
174181
{
175182
if ( !layerItem )
176183
{
@@ -212,7 +219,7 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
212219
if ( symbol ) //item with symbol?
213220
{
214221
//draw symbol
215-
drawSymbol( p, symbol, currentYCoord + ( itemHeight - mSymbolHeight ) / 2, currentXCoord, realSymbolHeight );
222+
drawSymbol( p, symbol, currentYCoord + ( itemHeight - mSymbolHeight ) / 2, currentXCoord, realSymbolHeight, layerOpacity );
216223
realItemHeight = std::max( realSymbolHeight, itemHeight );
217224
currentXCoord += mIconLabelSpace;
218225
}
@@ -239,7 +246,7 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
239246
}
240247
}
241248

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
243250
{
244251
if ( !s )
245252
{
@@ -250,14 +257,14 @@ void QgsComposerLegend::drawSymbol( QPainter* p, QgsSymbol* s, double currentYCo
250257
switch ( symbolType )
251258
{
252259
case QGis::Point:
253-
drawPointSymbol( p, s, currentYCoord, currentXPosition, symbolHeight );
260+
drawPointSymbol( p, s, currentYCoord, currentXPosition, symbolHeight, layerOpacity );
254261
break;
255262
case QGis::Line:
256-
drawLineSymbol( p, s, currentYCoord, currentXPosition );
263+
drawLineSymbol( p, s, currentYCoord, currentXPosition, layerOpacity );
257264
symbolHeight = mSymbolHeight;
258265
break;
259266
case QGis::Polygon:
260-
drawPolygonSymbol( p, s, currentYCoord, currentXPosition );
267+
drawPolygonSymbol( p, s, currentYCoord, currentXPosition, layerOpacity );
261268
symbolHeight = mSymbolHeight;
262269
break;
263270
case QGis::UnknownGeometry:
@@ -266,7 +273,7 @@ void QgsComposerLegend::drawSymbol( QPainter* p, QgsSymbol* s, double currentYCo
266273
}
267274
}
268275

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
270277
{
271278
if ( !s )
272279
{
@@ -287,7 +294,7 @@ void QgsComposerLegend::drawPointSymbol( QPainter* p, QgsSymbol* s, double curre
287294
}
288295

289296
//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 );
291298

292299
if ( p )
293300
{
@@ -303,7 +310,7 @@ void QgsComposerLegend::drawPointSymbol( QPainter* p, QgsSymbol* s, double curre
303310
symbolHeight = s->pointSize(); //pointImage.height() / rasterScaleFactor;
304311
}
305312

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
307314
{
308315
if ( !s )
309316
{
@@ -315,15 +322,19 @@ void QgsComposerLegend::drawLineSymbol( QPainter* p, QgsSymbol* s, double curren
315322
if ( p )
316323
{
317324
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 );
319330
p->drawLine( QPointF( currentXPosition, yCoord ), QPointF( currentXPosition + mSymbolWidth, yCoord ) );
320331
p->restore();
321332
}
322333

323334
currentXPosition += mSymbolWidth;
324335
}
325336

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
327338
{
328339
if ( !s )
329340
{
@@ -332,16 +343,25 @@ void QgsComposerLegend::drawPolygonSymbol( QPainter* p, QgsSymbol* s, double cur
332343

333344
if ( p )
334345
{
335-
//scale brush
346+
//scale brush and set transparencies
336347
QBrush symbolBrush = s->brush();
348+
QColor brushColor = symbolBrush.color();
349+
brushColor.setAlpha( opacity );
350+
symbolBrush.setColor( brushColor );
337351
QPaintDevice* paintDevice = p->device();
338352
if ( paintDevice )
339353
{
340354
double rasterScaleFactor = ( paintDevice->logicalDpiX() + paintDevice->logicalDpiY() ) / 2.0 / 25.4;
341355
QgsRenderer::scaleBrush( symbolBrush, rasterScaleFactor );
342356
}
343357
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+
345365
p->drawRect( QRectF( currentXPosition, currentYCoord, mSymbolWidth, mSymbolHeight ) );
346366
}
347367

src/core/composer/qgscomposerlegend.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,16 @@ class CORE_EXPORT QgsComposerLegend: public QObject, public QgsComposerItem
127127
@param layerItem parent model item (layer)
128128
@param currentYCoord in/out: current y position of legend item
129129
@param maxXCoord in/out: maximum x-coordinate of the whole legend
130+
@param layerOpacity opacity of the corresponding map layer
130131
*/
131-
void drawLayerChildItems( QPainter* p, QStandardItem* layerItem, double& currentYCoord, double& maxXCoord );
132+
void drawLayerChildItems( QPainter* p, QStandardItem* layerItem, double& currentYCoord, double& maxXCoord, int layerOpacity = 255 );
132133

133134
/**Draws a symbol at the current y position and returns the new x position. Returns real symbol height, because for points,
134135
it is possible that it differs from mSymbolHeight*/
135-
void drawSymbol( QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition, double& symbolHeight ) const;
136-
void drawPointSymbol( QPainter*, QgsSymbol* s, double currentYCoord, double& currentXPosition, double& symbolHeight ) const;
137-
void drawLineSymbol( QPainter*, QgsSymbol* s, double currentYCoord, double& currentXPosition ) const;
138-
void drawPolygonSymbol( QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition ) const;
136+
void drawSymbol( QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition, double& symbolHeight, int layerOpacity = 255 ) const;
137+
void drawPointSymbol( QPainter*, QgsSymbol* s, double currentYCoord, double& currentXPosition, double& symbolHeight, int opacity = 255 ) const;
138+
void drawLineSymbol( QPainter*, QgsSymbol* s, double currentYCoord, double& currentXPosition, int opacity = 255 ) const;
139+
void drawPolygonSymbol( QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition, int opacity = 255 ) const;
139140

140141
/**Helper function that lists ids of layers contained in map canvas*/
141142
QStringList layerIdList() const;

src/core/composer/qgslegendmodel.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ int QgsLegendModel::addVectorLayerItems( QStandardItem* layerItem, QgsMapLayer*
9191
{
9292
return 2;
9393
}
94+
int opacity = vectorLayer->getTransparency();
9495

9596
const QgsRenderer* vectorRenderer = vectorLayer->renderer();
9697
if ( !vectorRenderer )
@@ -127,7 +128,7 @@ int QgsLegendModel::addVectorLayerItems( QStandardItem* layerItem, QgsMapLayer*
127128
continue;
128129
}
129130

130-
QStandardItem* currentSymbolItem = itemFromSymbol( *symbolIt );
131+
QStandardItem* currentSymbolItem = itemFromSymbol( *symbolIt, opacity );
131132
if ( !currentSymbolItem )
132133
{
133134
continue;
@@ -278,6 +279,7 @@ void QgsLegendModel::updateVectorClassificationItem( QStandardItem* classificati
278279
{
279280
return;
280281
}
282+
int opacity = vl->getTransparency();
281283

282284
const QgsRenderer* layerRenderer = vl->renderer();
283285
if ( !layerRenderer )
@@ -297,7 +299,7 @@ void QgsLegendModel::updateVectorClassificationItem( QStandardItem* classificati
297299
if ( currentSymbol->lowerValue() + " - " + currentSymbol->upperValue() == itemText )
298300
{
299301
removeSymbol( symbol );
300-
parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol ) );
302+
parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol, opacity ) );
301303
parentItem->removeRow( classificationItem->row() );
302304
return;
303305
}
@@ -311,7 +313,7 @@ void QgsLegendModel::updateVectorClassificationItem( QStandardItem* classificati
311313
if ( currentSymbol->lowerValue() == itemText )
312314
{
313315
removeSymbol( symbol );
314-
parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol ) );
316+
parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol, opacity ) );
315317
parentItem->removeRow( classificationItem->row() );
316318
return;
317319
}
@@ -325,7 +327,7 @@ void QgsLegendModel::updateVectorClassificationItem( QStandardItem* classificati
325327
if ( currentSymbol->label() == itemText )
326328
{
327329
removeSymbol( symbol );
328-
parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol ) );
330+
parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol, opacity ) );
329331
parentItem->removeRow( classificationItem->row() );
330332
return;
331333
}
@@ -415,7 +417,7 @@ void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer )
415417
emit layersChanged();
416418
}
417419

418-
QStandardItem* QgsLegendModel::itemFromSymbol( QgsSymbol* s )
420+
QStandardItem* QgsLegendModel::itemFromSymbol( QgsSymbol* s, int opacity )
419421
{
420422
QStandardItem* currentSymbolItem = 0;
421423

@@ -444,22 +446,39 @@ QStandardItem* QgsLegendModel::itemFromSymbol( QgsSymbol* s )
444446
}
445447

446448
//icon item
449+
QImage symbolImage;
447450
switch ( s->type() )
448451
{
449452
case QGis::Point:
450-
currentSymbolItem = new QStandardItem( QIcon( QPixmap::fromImage( s->getPointSymbolAsImage() ) ), itemText );
453+
symbolImage = s->getPointSymbolAsImage();
451454
break;
452455
case QGis::Line:
453-
currentSymbolItem = new QStandardItem( QIcon( QPixmap::fromImage( s->getLineSymbolAsImage() ) ), itemText );
456+
symbolImage = s->getLineSymbolAsImage();
454457
break;
455458
case QGis::Polygon:
456-
currentSymbolItem = new QStandardItem( QIcon( QPixmap::fromImage( s->getPolygonSymbolAsImage() ) ), itemText );
459+
symbolImage = s->getPolygonSymbolAsImage();
457460
break;
458461
default:
459-
currentSymbolItem = 0;
460-
break;
462+
return 0;
461463
}
462464

465+
if ( opacity != 255 )
466+
{
467+
//todo: manipulate image pixel by pixel...
468+
QRgb oldColor;
469+
for ( int i = 0; i < symbolImage.height(); ++i )
470+
{
471+
QRgb* scanLineBuffer = ( QRgb* ) symbolImage.scanLine( i );
472+
for ( int j = 0; j < symbolImage.width(); ++j )
473+
{
474+
oldColor = symbolImage.pixel( j, i );
475+
scanLineBuffer[j] = qRgba( qRed( oldColor ), qGreen( oldColor ), qBlue( oldColor ), opacity );
476+
}
477+
}
478+
}
479+
480+
currentSymbolItem = new QStandardItem( QIcon( QPixmap::fromImage( symbolImage ) ), itemText );
481+
463482
if ( !currentSymbolItem )
464483
{
465484
return 0;

src/core/composer/qgslegendmodel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel
7676
void removeAllSymbols();
7777

7878
/**Creates a model item for a vector symbol. The calling function takes ownership*/
79-
QStandardItem* itemFromSymbol( QgsSymbol* s );
79+
QStandardItem* itemFromSymbol( QgsSymbol* s, int opacity );
8080

8181
/**Keep track of copied symbols to delete them if not used anymore*/
8282
QSet<QgsSymbol*> mSymbols;

0 commit comments

Comments
 (0)