Skip to content

Commit

Permalink
highlight made transparent for other layers again
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Feb 9, 2014
1 parent 0d4968c commit b05c93c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
70 changes: 59 additions & 11 deletions src/gui/qgshighlight.cpp
Expand Up @@ -14,6 +14,9 @@
***************************************************************************/

#include <typeinfo>

#include <QImage>

#include "qgsmarkersymbollayerv2.h"
#include "qgslinesymbollayerv2.h"

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

QColor fillColor( color.red(), color.green(), color.blue(), 63 );
// Temporary fill color must be similar to outline color (antialiasing between
// outline and temporary fill) but also different enough to be distinguishable
// so that it can be replaced by transparent fill.
// Unfortunately the result of the transparent fill rendered over the original
// (not highlighted) feature color may be either lighter or darker.
if ( color.lightness() < 200 )
{
mTemporaryFillColor = color.lighter( 120 );
}
else
{
mTemporaryFillColor = color.darker( 120 );
}
mTemporaryFillColor.setAlpha( 255 );

for ( int i = symbol->symbolLayerCount() - 1; i >= 0; i-- )
{
Expand All @@ -133,14 +149,9 @@ void QgsHighlight::setSymbolColor( QgsSymbolV2* symbol, const QColor & color )
}
else
{
// We must insert additional highlight symbol layer above each original layer
// otherwise lower layers would become visible through transparent fill color.
QgsSymbolLayerV2* highlightLayer = symbolLayer->clone();

highlightLayer->setColor( color ); // line symbology layers
highlightLayer->setOutlineColor( color ); // marker and fill symbology layers
highlightLayer->setFillColor( fillColor ); // marker and fill symbology layers
symbol->insertSymbolLayer( i + 1, highlightLayer );
symbolLayer->setColor( color ); // line symbology layers
symbolLayer->setOutlineColor( color ); // marker and fill symbology layers
symbolLayer->setFillColor( mTemporaryFillColor ); // marker and fill symbology layers
}
}
}
Expand Down Expand Up @@ -300,17 +311,54 @@ void QgsHighlight::paint( QPainter* p )
updateRect();
return; // it will be repainted after updateRect()
}
double height = toCanvasCoordinates( QgsPoint( extent.xMinimum(), extent.yMinimum() ) ).y() - toCanvasCoordinates( QgsPoint( extent.xMinimum(), extent.yMaximum() ) ).y();


QPointF ll = toCanvasCoordinates( QgsPoint( extent.xMinimum(), extent.yMinimum() ) );
QPointF ur = toCanvasCoordinates( QgsPoint( extent.xMaximum(), extent.yMaximum() ) );
double height = ll.y() - ur.y();
double width = ur.x() - ll.x();

// Because lower level outlines must be covered by upper level fill color
// we render first with temporary opaque color, which is then replaced
// by final transparent fill color.
QImage image = QImage(( int )width, ( int )height, QImage::Format_ARGB32 );
image.fill( 0 );
QPainter *imagePainter = new QPainter( &image );
imagePainter->setRenderHint( QPainter::Antialiasing, true );

QgsMapToPixel mapToPixel = QgsMapToPixel( mMapCanvas->mapUnitsPerPixel(),
height, extent.yMinimum(), extent.xMinimum() );
context.setMapToPixel( mapToPixel );
context.setExtent( extent );
context.setCoordinateTransform( 0 ); // we reprojected geometry in init()
context.setPainter( p );
context.setPainter( imagePainter );

mRenderer->startRender( context, layer );
mRenderer->renderFeature( mFeature, context );
mRenderer->stopRender( context );

imagePainter->end();

QRgb temporaryRgb = mTemporaryFillColor.rgba();
QColor color = QColor( mBrush.color() );
color.setAlpha( 63 );
QRgb colorRgb = color.rgba();

for ( int r = 0; r < image.height(); r++ )
{
QRgb *line = ( QRgb * ) image.scanLine( r );
for ( int c = 0; c < image.width(); c++ )
{
if ( line[c] == temporaryRgb )
{
line[c] = colorRgb;
}
}
}

p->drawImage( 0, 0, image );

delete imagePainter;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgshighlight.h
Expand Up @@ -20,6 +20,7 @@
#include "qgsgeometry.h"
#include "qgsrendererv2.h"
#include <QBrush>
#include <QColor>
#include <QList>
#include <QPen>
#include <QPainter>
Expand Down Expand Up @@ -73,6 +74,7 @@ class GUI_EXPORT QgsHighlight: public QgsMapCanvasItem
QgsMapLayer *mLayer;
QgsFeature mFeature;
QgsFeatureRendererV2 *mRenderer;
QColor mTemporaryFillColor;
};

#endif

0 comments on commit b05c93c

Please sign in to comment.