Skip to content

Commit 776619f

Browse files
committed
highlight fixes
1 parent d8ad1d8 commit 776619f

File tree

4 files changed

+32
-45
lines changed

4 files changed

+32
-45
lines changed

src/app/qgsidentifyresultsdialog.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1267,8 +1267,8 @@ void QgsIdentifyResultsDialog::highlightFeature( QTreeWidgetItem *item )
12671267
QSettings settings;
12681268
QColor color = QColor( settings.value( "/Map/identify/highlight/color", "#ff0000" ).toString() );
12691269
int alpha = settings.value( "/Map/identify/highlight/colorAlpha", "128" ).toInt();
1270-
double buffer = settings.value( "/Map/highlight/buffer", "0.5" ).toDouble();
1271-
double minWidth = settings.value( "/Map/highlight/minWidth", "1." ).toDouble();
1270+
double buffer = settings.value( "/Map/identify/highlight/buffer", "0.5" ).toDouble();
1271+
double minWidth = settings.value( "/Map/identify/highlight/minWidth", "1." ).toDouble();
12721272

12731273
highlight->setColor( color ); // sets also fill with default alpha
12741274
color.setAlpha( alpha );

src/app/qgsoptions.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
123123
int highlightAlpha = settings.value( "/Map/identify/highlight/colorAlpha", "63" ).toInt();
124124
highlightColor.setAlpha( highlightAlpha );
125125
mIdentifyHighlightColorButton->setColor( highlightColor );
126-
double highlightBuffer = settings.value( "/Map/highlight/buffer", "0.5" ).toDouble();
126+
double highlightBuffer = settings.value( "/Map/identify/highlight/buffer", "0.5" ).toDouble();
127127
mIdentifyHighlightBufferSpinBox->setValue( highlightBuffer );
128-
double highlightMinWidth = settings.value( "/Map/highlight/minWidth", "1." ).toDouble();
128+
double highlightMinWidth = settings.value( "/Map/identify/highlight/minWidth", "1." ).toDouble();
129129
mIdentifyHighlightMinWidthSpinBox->setValue( highlightMinWidth );
130130

131131
// custom environment variables
@@ -1048,8 +1048,8 @@ void QgsOptions::saveOptions()
10481048
settings.setValue( "/Map/identifyRadius", spinBoxIdentifyValue->value() );
10491049
settings.setValue( "/Map/identify/highlight/color", mIdentifyHighlightColorButton->color().name() );
10501050
settings.setValue( "/Map/identify/highlight/colorAlpha", mIdentifyHighlightColorButton->color().alpha() );
1051-
settings.setValue( "/Map/highlight/buffer", mIdentifyHighlightBufferSpinBox->value() );
1052-
settings.setValue( "/Map/highlight/minWidth", mIdentifyHighlightMinWidthSpinBox->value() );
1051+
settings.setValue( "/Map/identify/highlight/buffer", mIdentifyHighlightBufferSpinBox->value() );
1052+
settings.setValue( "/Map/identify/highlight/minWidth", mIdentifyHighlightMinWidthSpinBox->value() );
10531053

10541054
bool showLegendClassifiers = settings.value( "/qgis/showLegendClassifiers", false ).toBool();
10551055
settings.setValue( "/qgis/showLegendClassifiers", cbxLegendClassifiers->isChecked() );

src/gui/qgshighlight.cpp

+24-36
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void QgsHighlight::setFillColor( const QColor & fillColor )
131131
mBrush.setStyle( Qt::SolidPattern );
132132
}
133133

134-
QgsFeatureRendererV2 * QgsHighlight::getRenderer( const QgsRenderContext & context )
134+
QgsFeatureRendererV2 * QgsHighlight::getRenderer( const QgsRenderContext & context, const QColor & color, const QColor & fillColor )
135135
{
136136
QgsFeatureRendererV2 *renderer = 0;
137137
QgsVectorLayer *layer = vectorLayer();
@@ -144,30 +144,16 @@ QgsFeatureRendererV2 * QgsHighlight::getRenderer( const QgsRenderContext & conte
144144
foreach ( QgsSymbolV2* symbol, renderer->symbols() )
145145
{
146146
if ( !symbol ) continue;
147-
setSymbol( symbol, context, mPen.color() );
147+
setSymbol( symbol, context, color, fillColor );
148148
}
149149
}
150150
return renderer;
151151
}
152152

153-
void QgsHighlight::setSymbol( QgsSymbolV2* symbol, const QgsRenderContext & context, const QColor & color )
153+
void QgsHighlight::setSymbol( QgsSymbolV2* symbol, const QgsRenderContext & context, const QColor & color, const QColor & fillColor )
154154
{
155155
if ( !symbol ) return;
156156

157-
// Temporary fill color must be similar to outline color (antialiasing between
158-
// outline and temporary fill) but also different enough to be distinguishable
159-
// so that it can be replaced by transparent fill.
160-
// Unfortunately the result of the transparent fill rendered over the original
161-
// (not highlighted) feature color may be either lighter or darker.
162-
if ( color.lightness() < 200 )
163-
{
164-
mTemporaryFillColor = color.lighter( 120 );
165-
}
166-
else
167-
{
168-
mTemporaryFillColor = color.darker( 120 );
169-
}
170-
mTemporaryFillColor.setAlpha( 255 );
171157

172158
for ( int i = symbol->symbolLayerCount() - 1; i >= 0; i-- )
173159
{
@@ -176,13 +162,13 @@ void QgsHighlight::setSymbol( QgsSymbolV2* symbol, const QgsRenderContext & cont
176162

177163
if ( symbolLayer->subSymbol() )
178164
{
179-
setSymbol( symbolLayer->subSymbol(), context, color );
165+
setSymbol( symbolLayer->subSymbol(), context, color, fillColor );
180166
}
181167
else
182168
{
183169
symbolLayer->setColor( color ); // line symbology layers
184170
symbolLayer->setOutlineColor( color ); // marker and fill symbology layers
185-
symbolLayer->setFillColor( mTemporaryFillColor ); // marker and fill symbology layers
171+
symbolLayer->setFillColor( fillColor ); // marker and fill symbology layers
186172

187173
// Data defined widths overwrite what we set here (widths do not work with data defined)
188174
QgsSimpleMarkerSymbolLayerV2 * simpleMarker = dynamic_cast<QgsSimpleMarkerSymbolLayerV2*>( symbolLayer );
@@ -214,9 +200,8 @@ double QgsHighlight::getSymbolWidth( const QgsRenderContext & context, double wi
214200
{
215201
scale = QgsSymbolLayerV2Utils::lineWidthScaleFactor( context, QgsSymbolV2::MM ) / QgsSymbolLayerV2Utils::lineWidthScaleFactor( context, QgsSymbolV2::MapUnit );
216202
}
217-
return qMax( width + 2*mBuffer*scale, mMinWidth*scale );
218-
219-
203+
width = qMax( width + 2 * mBuffer * scale, mMinWidth * scale );
204+
return width;
220205
}
221206

222207
/*!
@@ -364,7 +349,13 @@ void QgsHighlight::paint( QPainter* p )
364349
QgsMapSettings mapSettings = mMapCanvas->mapSettings();
365350
QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
366351

367-
QgsFeatureRendererV2 *renderer = getRenderer( context );
352+
// Because lower level outlines must be covered by upper level fill color
353+
// we render first with temporary opaque color, which is then replaced
354+
// by final transparent fill color.
355+
QColor tmpColor( 255, 0, 0, 255 );
356+
QColor tmpFillColor( 0, 255, 0, 255 );
357+
358+
QgsFeatureRendererV2 *renderer = getRenderer( context, tmpColor, tmpFillColor );
368359
if ( layer && renderer )
369360
{
370361
QgsRectangle extent = mMapCanvas->extent();
@@ -374,10 +365,6 @@ void QgsHighlight::paint( QPainter* p )
374365
return; // it will be repainted after updateRect()
375366
}
376367

377-
// Because lower level outlines must be covered by upper level fill color
378-
// we render first with temporary opaque color, which is then replaced
379-
// by final transparent fill color.
380-
//QImage image = QImage(( int )width, ( int )height, QImage::Format_ARGB32 );
381368
QSize imageSize( mMapCanvas->mapSettings().outputSize() );
382369
QImage image = QImage( imageSize.width(), imageSize.height(), QImage::Format_ARGB32 );
383370
image.fill( 0 );
@@ -392,20 +379,21 @@ void QgsHighlight::paint( QPainter* p )
392379

393380
imagePainter->end();
394381

395-
// overwrite temporary fill color
396-
QRgb temporaryRgb = mTemporaryFillColor.rgba();
397-
QColor color = QColor( mBrush.color() );
398-
color.setAlpha( 63 );
399-
QRgb colorRgb = color.rgba();
400-
382+
QColor color( mPen.color() ); // true output color
383+
// coeficient to subtract alpha using green (temporary fill)
384+
double k = ( 255. - mBrush.color().alpha() ) / 255.;
401385
for ( int r = 0; r < image.height(); r++ )
402386
{
403-
QRgb *line = ( QRgb * ) image.scanLine( r );
404387
for ( int c = 0; c < image.width(); c++ )
405388
{
406-
if ( line[c] == temporaryRgb )
389+
QRgb rgba = image.pixel( c, r );
390+
int alpha = qAlpha( rgba );
391+
if ( alpha > 0 )
407392
{
408-
line[c] = colorRgb;
393+
int green = qGreen( rgba );
394+
color.setAlpha( alpha - ( green * k ) );
395+
396+
image.setPixel( c, r, color.rgba() );
409397
}
410398
}
411399
}

src/gui/qgshighlight.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ class GUI_EXPORT QgsHighlight: public QgsMapCanvasItem
7474

7575
private:
7676
void init();
77-
void setSymbol( QgsSymbolV2* symbol, const QgsRenderContext & context, const QColor & color );
77+
void setSymbol( QgsSymbolV2* symbol, const QgsRenderContext & context, const QColor & color, const QColor & fillColor );
7878
double getSymbolWidth( const QgsRenderContext & context, double width, QgsSymbolV2::OutputUnit unit );
7979
/** Get renderer for current color mode and colors. The renderer should be freed by caller. */
80-
QgsFeatureRendererV2 * getRenderer( const QgsRenderContext & context );
80+
QgsFeatureRendererV2 * getRenderer( const QgsRenderContext & context, const QColor & color, const QColor & fillColor );
8181
void paintPoint( QPainter *p, QgsPoint point );
8282
void paintLine( QPainter *p, QgsPolyline line );
8383
void paintPolygon( QPainter *p, QgsPolygon polygon );
@@ -91,7 +91,6 @@ class GUI_EXPORT QgsHighlight: public QgsMapCanvasItem
9191
QgsGeometry *mGeometry;
9292
QgsMapLayer *mLayer;
9393
QgsFeature mFeature;
94-
QColor mTemporaryFillColor;
9594
double mBuffer; // line / outline buffer in pixels
9695
double mMinWidth; // line / outline minimum width in pixels
9796
};

0 commit comments

Comments
 (0)