Skip to content

Commit b05c93c

Browse files
committedFeb 9, 2014
highlight made transparent for other layers again
1 parent 0d4968c commit b05c93c

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed
 

‎src/gui/qgshighlight.cpp

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
***************************************************************************/
1515

1616
#include <typeinfo>
17+
18+
#include <QImage>
19+
1720
#include "qgsmarkersymbollayerv2.h"
1821
#include "qgslinesymbollayerv2.h"
1922

@@ -120,7 +123,20 @@ void QgsHighlight::setSymbolColor( QgsSymbolV2* symbol, const QColor & color )
120123
{
121124
if ( !symbol ) return;
122125

123-
QColor fillColor( color.red(), color.green(), color.blue(), 63 );
126+
// Temporary fill color must be similar to outline color (antialiasing between
127+
// outline and temporary fill) but also different enough to be distinguishable
128+
// so that it can be replaced by transparent fill.
129+
// Unfortunately the result of the transparent fill rendered over the original
130+
// (not highlighted) feature color may be either lighter or darker.
131+
if ( color.lightness() < 200 )
132+
{
133+
mTemporaryFillColor = color.lighter( 120 );
134+
}
135+
else
136+
{
137+
mTemporaryFillColor = color.darker( 120 );
138+
}
139+
mTemporaryFillColor.setAlpha( 255 );
124140

125141
for ( int i = symbol->symbolLayerCount() - 1; i >= 0; i-- )
126142
{
@@ -133,14 +149,9 @@ void QgsHighlight::setSymbolColor( QgsSymbolV2* symbol, const QColor & color )
133149
}
134150
else
135151
{
136-
// We must insert additional highlight symbol layer above each original layer
137-
// otherwise lower layers would become visible through transparent fill color.
138-
QgsSymbolLayerV2* highlightLayer = symbolLayer->clone();
139-
140-
highlightLayer->setColor( color ); // line symbology layers
141-
highlightLayer->setOutlineColor( color ); // marker and fill symbology layers
142-
highlightLayer->setFillColor( fillColor ); // marker and fill symbology layers
143-
symbol->insertSymbolLayer( i + 1, highlightLayer );
152+
symbolLayer->setColor( color ); // line symbology layers
153+
symbolLayer->setOutlineColor( color ); // marker and fill symbology layers
154+
symbolLayer->setFillColor( mTemporaryFillColor ); // marker and fill symbology layers
144155
}
145156
}
146157
}
@@ -300,17 +311,54 @@ void QgsHighlight::paint( QPainter* p )
300311
updateRect();
301312
return; // it will be repainted after updateRect()
302313
}
303-
double height = toCanvasCoordinates( QgsPoint( extent.xMinimum(), extent.yMinimum() ) ).y() - toCanvasCoordinates( QgsPoint( extent.xMinimum(), extent.yMaximum() ) ).y();
314+
315+
316+
QPointF ll = toCanvasCoordinates( QgsPoint( extent.xMinimum(), extent.yMinimum() ) );
317+
QPointF ur = toCanvasCoordinates( QgsPoint( extent.xMaximum(), extent.yMaximum() ) );
318+
double height = ll.y() - ur.y();
319+
double width = ur.x() - ll.x();
320+
321+
// Because lower level outlines must be covered by upper level fill color
322+
// we render first with temporary opaque color, which is then replaced
323+
// by final transparent fill color.
324+
QImage image = QImage(( int )width, ( int )height, QImage::Format_ARGB32 );
325+
image.fill( 0 );
326+
QPainter *imagePainter = new QPainter( &image );
327+
imagePainter->setRenderHint( QPainter::Antialiasing, true );
304328

305329
QgsMapToPixel mapToPixel = QgsMapToPixel( mMapCanvas->mapUnitsPerPixel(),
306330
height, extent.yMinimum(), extent.xMinimum() );
307331
context.setMapToPixel( mapToPixel );
308332
context.setExtent( extent );
309333
context.setCoordinateTransform( 0 ); // we reprojected geometry in init()
310-
context.setPainter( p );
334+
context.setPainter( imagePainter );
335+
311336
mRenderer->startRender( context, layer );
312337
mRenderer->renderFeature( mFeature, context );
313338
mRenderer->stopRender( context );
339+
340+
imagePainter->end();
341+
342+
QRgb temporaryRgb = mTemporaryFillColor.rgba();
343+
QColor color = QColor( mBrush.color() );
344+
color.setAlpha( 63 );
345+
QRgb colorRgb = color.rgba();
346+
347+
for ( int r = 0; r < image.height(); r++ )
348+
{
349+
QRgb *line = ( QRgb * ) image.scanLine( r );
350+
for ( int c = 0; c < image.width(); c++ )
351+
{
352+
if ( line[c] == temporaryRgb )
353+
{
354+
line[c] = colorRgb;
355+
}
356+
}
357+
}
358+
359+
p->drawImage( 0, 0, image );
360+
361+
delete imagePainter;
314362
}
315363
}
316364
}

‎src/gui/qgshighlight.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgsgeometry.h"
2121
#include "qgsrendererv2.h"
2222
#include <QBrush>
23+
#include <QColor>
2324
#include <QList>
2425
#include <QPen>
2526
#include <QPainter>
@@ -73,6 +74,7 @@ class GUI_EXPORT QgsHighlight: public QgsMapCanvasItem
7374
QgsMapLayer *mLayer;
7475
QgsFeature mFeature;
7576
QgsFeatureRendererV2 *mRenderer;
77+
QColor mTemporaryFillColor;
7678
};
7779

7880
#endif

0 commit comments

Comments
 (0)