Skip to content
Permalink
Browse files
highlight made transparent for other layers again
  • Loading branch information
blazek committed Feb 9, 2014
1 parent 0d4968c commit b05c93c3dda6a6a96bcf560703ab835012a7b1bd
Showing with 61 additions and 11 deletions.
  1. +59 −11 src/gui/qgshighlight.cpp
  2. +2 −0 src/gui/qgshighlight.h
@@ -14,6 +14,9 @@
***************************************************************************/

#include <typeinfo>

#include <QImage>

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

@@ -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-- )
{
@@ -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
}
}
}
@@ -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;
}
}
}
@@ -20,6 +20,7 @@
#include "qgsgeometry.h"
#include "qgsrendererv2.h"
#include <QBrush>
#include <QColor>
#include <QList>
#include <QPen>
#include <QPainter>
@@ -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.