Skip to content

Commit

Permalink
Condense duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 19, 2022
1 parent 2fc245b commit cb8875a
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 38 deletions.
7 changes: 7 additions & 0 deletions python/gui/auto_generated/qgshighlight.sip.in
Expand Up @@ -143,6 +143,13 @@ Returns the layer for which this highlight has been created.
virtual void updatePosition();


void applyDefaultStyle();
%Docstring
Applies the default style from the user settings to the highlight.

.. versionadded:: 3.30
%End

protected:
virtual void paint( QPainter *p );

Expand Down
5 changes: 3 additions & 2 deletions python/gui/auto_generated/qgsidentifymenu.sip.in
Expand Up @@ -161,11 +161,12 @@ exec
:param pos: the position where the menu will be executed
%End

static void styleHighlight( QgsHighlight *highlight );
static void styleHighlight( QgsHighlight *highlight ) /Deprecated/;
%Docstring
Applies style from the settings to the highlight

.. versionadded:: 3.8
.. deprecated::
Use :py:func:`QgsHighlight.applyDefaultStyle()` instead.
%End

protected:
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsidentifyresultsdialog.cpp
Expand Up @@ -2151,7 +2151,7 @@ void QgsIdentifyResultsDialog::highlightFeature( QTreeWidgetItem *item )
highlight->setWidth( 2 );
}

QgsIdentifyMenu::styleHighlight( highlight );
highlight->applyDefaultStyle();
highlight->show();
mHighlights.insert( featItem, highlight );
}
Expand Down
19 changes: 2 additions & 17 deletions src/app/qgsmaptoolselectutils.cpp
Expand Up @@ -799,7 +799,7 @@ void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::highlightAllFeatures()
if ( !geom.isEmpty() )
{
QgsHighlight *hl = new QgsHighlight( mCanvas, geom, mVectorLayer );
styleHighlight( hl );
hl->applyDefaultStyle();
mHighlight.append( hl );
count++;
}
Expand All @@ -821,26 +821,11 @@ void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::highlightOneFeature( Qg
if ( !geom.isEmpty() )
{
QgsHighlight *hl = new QgsHighlight( mCanvas, geom, mVectorLayer );
styleHighlight( hl );
hl->applyDefaultStyle();
mHighlight.append( hl );
}
}

void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::styleHighlight( QgsHighlight *highlight )
{
QgsSettings settings;
QColor color = QColor( settings.value( QStringLiteral( "Map/highlight/color" ), Qgis::DEFAULT_HIGHLIGHT_COLOR.name() ).toString() );
int alpha = settings.value( QStringLiteral( "Map/highlight/colorAlpha" ), Qgis::DEFAULT_HIGHLIGHT_COLOR.alpha() ).toInt();
double buffer = settings.value( QStringLiteral( "Map/highlight/buffer" ), Qgis::DEFAULT_HIGHLIGHT_BUFFER_MM ).toDouble();
double minWidth = settings.value( QStringLiteral( "Map/highlight/minWidth" ), Qgis::DEFAULT_HIGHLIGHT_MIN_WIDTH_MM ).toDouble();

highlight->setColor( color ); // sets also fill with default alpha
color.setAlpha( alpha );
highlight->setFillColor( color ); // sets fill with alpha
highlight->setBuffer( buffer );
highlight->setMinWidth( minWidth );
}

QgsFeatureIds QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::filterIds( const QgsFeatureIds &ids,
const QgsFeatureIds &existingSelection,
Qgis::SelectBehavior behavior )
Expand Down
2 changes: 0 additions & 2 deletions src/app/qgsmaptoolselectutils.h
Expand Up @@ -171,8 +171,6 @@ namespace QgsMapToolSelectUtils

void startFeatureSearch();

void styleHighlight( QgsHighlight *highlight );

QString textForChooseAll( qint64 featureCount = -1 ) const;
QString textForChooseOneMenu() const;
void populateChooseOneMenu( const QgsFeatureIds &ids );
Expand Down
3 changes: 1 addition & 2 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Expand Up @@ -43,7 +43,6 @@
#include "qgsfeaturelistcombobox.h"
#include "qgsexpressioncontextutils.h"
#include "qgsfeaturefiltermodel.h"
#include "qgsidentifymenu.h"
#include "qgsvectorlayerutils.h"


Expand Down Expand Up @@ -584,7 +583,7 @@ void QgsRelationReferenceWidget::highlightFeature( QgsFeature f, CanvasExtent ca
// highlight
deleteHighlight();
mHighlight = new QgsHighlight( mCanvas, f, mReferencedLayer );
QgsIdentifyMenu::styleHighlight( mHighlight );
mHighlight->applyDefaultStyle();
mHighlight->show();

QTimer *timer = new QTimer( this );
Expand Down
15 changes: 15 additions & 0 deletions src/gui/qgshighlight.cpp
Expand Up @@ -306,6 +306,21 @@ void QgsHighlight::updatePosition()
updateRect();
}

void QgsHighlight::applyDefaultStyle()
{
const QgsSettings settings;
QColor color = QColor( settings.value( QStringLiteral( "Map/highlight/color" ), Qgis::DEFAULT_HIGHLIGHT_COLOR.name() ).toString() );
const int alpha = settings.value( QStringLiteral( "Map/highlight/colorAlpha" ), Qgis::DEFAULT_HIGHLIGHT_COLOR.alpha() ).toInt();
const double buffer = settings.value( QStringLiteral( "Map/highlight/buffer" ), Qgis::DEFAULT_HIGHLIGHT_BUFFER_MM ).toDouble();
const double minWidth = settings.value( QStringLiteral( "Map/highlight/minWidth" ), Qgis::DEFAULT_HIGHLIGHT_MIN_WIDTH_MM ).toDouble();

setColor( color ); // sets also fill with default alpha
color.setAlpha( alpha );
setFillColor( color ); // sets fill with alpha
setBuffer( buffer );
setMinWidth( minWidth );
}

void QgsHighlight::paint( QPainter *p )
{
if ( mFeature.hasGeometry() )
Expand Down
7 changes: 7 additions & 0 deletions src/gui/qgshighlight.h
Expand Up @@ -172,6 +172,13 @@ class GUI_EXPORT QgsHighlight : public QgsMapCanvasItem

void updatePosition() override;

/**
* Applies the default style from the user settings to the highlight.
*
* \since QGIS 3.30
*/
void applyDefaultStyle();

protected:
void paint( QPainter *p ) override;

Expand Down
14 changes: 2 additions & 12 deletions src/gui/qgsidentifymenu.cpp
Expand Up @@ -673,25 +673,15 @@ void QgsIdentifyMenu::handleMenuHover()
continue;

QgsHighlight *hl = new QgsHighlight( mCanvas, result.mFeature.geometry(), vl );
styleHighlight( hl );
hl->applyDefaultStyle();
mRubberBands.append( hl );
connect( vl, &QObject::destroyed, this, &QgsIdentifyMenu::layerDestroyed );
}
}

void QgsIdentifyMenu::styleHighlight( QgsHighlight *highlight )
{
const QgsSettings settings;
QColor color = QColor( settings.value( QStringLiteral( "Map/highlight/color" ), Qgis::DEFAULT_HIGHLIGHT_COLOR.name() ).toString() );
const int alpha = settings.value( QStringLiteral( "Map/highlight/colorAlpha" ), Qgis::DEFAULT_HIGHLIGHT_COLOR.alpha() ).toInt();
const double buffer = settings.value( QStringLiteral( "Map/highlight/buffer" ), Qgis::DEFAULT_HIGHLIGHT_BUFFER_MM ).toDouble();
const double minWidth = settings.value( QStringLiteral( "Map/highlight/minWidth" ), Qgis::DEFAULT_HIGHLIGHT_MIN_WIDTH_MM ).toDouble();

highlight->setColor( color ); // sets also fill with default alpha
color.setAlpha( alpha );
highlight->setFillColor( color ); // sets fill with alpha
highlight->setBuffer( buffer );
highlight->setMinWidth( minWidth );
highlight->applyDefaultStyle();
}

void QgsIdentifyMenu::deleteRubberBands()
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsidentifymenu.h
Expand Up @@ -177,9 +177,9 @@ class GUI_EXPORT QgsIdentifyMenu : public QMenu
/**
* Applies style from the settings to the highlight
*
* \since QGIS 3.8
* \deprecated Use QgsHighlight::applyDefaultStyle() instead.
*/
static void styleHighlight( QgsHighlight *highlight );
Q_DECL_DEPRECATED static void styleHighlight( QgsHighlight *highlight ) SIP_DEPRECATED;

protected:
void closeEvent( QCloseEvent *e ) override;
Expand Down

0 comments on commit cb8875a

Please sign in to comment.