Condense duplicate code

This commit is contained in:
Nyall Dawson 2022-11-19 09:15:20 +10:00
parent 2fc245b127
commit cb8875a87b
10 changed files with 40 additions and 38 deletions

View File

@ -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 );

View File

@ -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:

View File

@ -2151,7 +2151,7 @@ void QgsIdentifyResultsDialog::highlightFeature( QTreeWidgetItem *item )
highlight->setWidth( 2 );
}
QgsIdentifyMenu::styleHighlight( highlight );
highlight->applyDefaultStyle();
highlight->show();
mHighlights.insert( featItem, highlight );
}

View File

@ -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++;
}
@ -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 )

View File

@ -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 );

View File

@ -43,7 +43,6 @@
#include "qgsfeaturelistcombobox.h"
#include "qgsexpressioncontextutils.h"
#include "qgsfeaturefiltermodel.h"
#include "qgsidentifymenu.h"
#include "qgsvectorlayerutils.h"
@ -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 );

View File

@ -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() )

View File

@ -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;

View File

@ -673,7 +673,7 @@ 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 );
}
@ -681,17 +681,7 @@ void QgsIdentifyMenu::handleMenuHover()
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()

View File

@ -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;