Skip to content

Commit

Permalink
Use updated api in labeling map tools to retrieve layers by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 5, 2021
1 parent d6dae3e commit 1cba45b
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/app/labeling/qgslabelpropertydialog.cpp
Expand Up @@ -105,7 +105,7 @@ void QgsLabelPropertyDialog::buttonBox_clicked( QAbstractButton *button )
void QgsLabelPropertyDialog::init( const QString &layerId, const QString &providerId, QgsFeatureId featureId, const QString &labelText )
{
//get feature attributes
QgsVectorLayer *vlayer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
QgsVectorLayer *vlayer = qobject_cast< QgsVectorLayer * >( mCanvas->layer( layerId ) );
if ( !vlayer )
{
return;
Expand Down
2 changes: 1 addition & 1 deletion src/app/labeling/qgsmaptoolchangelabelproperties.cpp
Expand Up @@ -65,7 +65,7 @@ void QgsMapToolChangeLabelProperties::canvasPressEvent( QgsMapMouseEvent *e )
return;
}

mCurrentLabel = LabelDetails( labelPos );
mCurrentLabel = LabelDetails( labelPos, canvas() );
if ( !mCurrentLabel.valid || !mCurrentLabel.layer )
{
return;
Expand Down
16 changes: 8 additions & 8 deletions src/app/labeling/qgsmaptoollabel.cpp
Expand Up @@ -68,12 +68,12 @@ bool QgsMapToolLabel::labelAtPosition( QMouseEvent *e, QgsLabelPosition &p )
return false;

QList<QgsLabelPosition> labelPosList = labelingResults->labelsAtPosition( pt );
labelPosList.erase( std::remove_if( labelPosList.begin(), labelPosList.end(), []( const QgsLabelPosition & position )
labelPosList.erase( std::remove_if( labelPosList.begin(), labelPosList.end(), [this]( const QgsLabelPosition & position )
{
if ( position.layerID.isEmpty() )
return true;

if ( QgsMapLayer *layer = QgsProject::instance()->mapLayer( position.layerID ) )
if ( QgsMapLayer *layer = QgsMapTool::layer( position.layerID ) )
{
// strip out any labels from non vector layers (e.g. those from vector tile layers). Only vector layer labels
// are supported by the map tools.
Expand Down Expand Up @@ -158,12 +158,12 @@ bool QgsMapToolLabel::calloutAtPosition( QMouseEvent *e, QgsCalloutPosition &p,
const double tol = QgsTolerance::vertexSearchRadius( canvas()->mapSettings() );

QList<QgsCalloutPosition> calloutPosList = labelingResults->calloutsWithinRectangle( QgsRectangle::fromCenterAndSize( pt, tol * 2, tol * 2 ) );
calloutPosList.erase( std::remove_if( calloutPosList.begin(), calloutPosList.end(), []( const QgsCalloutPosition & position )
calloutPosList.erase( std::remove_if( calloutPosList.begin(), calloutPosList.end(), [ this ]( const QgsCalloutPosition & position )
{
if ( position.layerID.isEmpty() )
return true;

if ( QgsMapLayer *layer = QgsProject::instance()->mapLayer( position.layerID ) )
if ( QgsMapLayer *layer = QgsMapTool::layer( position.layerID ) )
{
// strip out any callouts from non vector layers (e.g. those from vector tile layers). Only vector layer callouts
// are supported by the map tools.
Expand Down Expand Up @@ -880,10 +880,10 @@ bool QgsMapToolLabel::diagramCanShowHide( QgsVectorLayer *vlayer, int &showCol )

//

QgsMapToolLabel::LabelDetails::LabelDetails( const QgsLabelPosition &p )
QgsMapToolLabel::LabelDetails::LabelDetails( const QgsLabelPosition &p, QgsMapCanvas *canvas )
: pos( p )
{
layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( pos.layerID );
layer = qobject_cast< QgsVectorLayer * >( canvas->layer( pos.layerID ) );
if ( layer && layer->labelsEnabled() && !p.isDiagram )
{
settings = layer->labeling()->settings( pos.providerID );
Expand Down Expand Up @@ -1010,7 +1010,7 @@ bool QgsMapToolLabel::createAuxiliaryFields( QgsCalloutIndexes &calloutIndexes )
bool QgsMapToolLabel::createAuxiliaryFields( QgsCalloutPosition &details, QgsCalloutIndexes &calloutIndexes )
{
bool newAuxiliaryLayer = false;
QgsVectorLayer *vlayer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( details.layerID );
QgsVectorLayer *vlayer = qobject_cast< QgsVectorLayer * >( QgsMapTool::layer( details.layerID ) );

if ( !vlayer )
return newAuxiliaryLayer;
Expand Down Expand Up @@ -1112,7 +1112,7 @@ void QgsMapToolLabel::updateHoveredLabel( QgsMapMouseEvent *e )
return;
}

LabelDetails newHoverLabel( labelPos );
LabelDetails newHoverLabel( labelPos, canvas() );

if ( mCurrentHoverLabel.valid &&
newHoverLabel.layer == mCurrentHoverLabel.layer &&
Expand Down
2 changes: 1 addition & 1 deletion src/app/labeling/qgsmaptoollabel.h
Expand Up @@ -100,7 +100,7 @@ class APP_EXPORT QgsMapToolLabel: public QgsMapToolAdvancedDigitizing
struct APP_EXPORT LabelDetails
{
LabelDetails() = default;
explicit LabelDetails( const QgsLabelPosition &p );
explicit LabelDetails( const QgsLabelPosition &p, QgsMapCanvas *canvas );
bool valid = false;
QgsLabelPosition pos;
QgsVectorLayer *layer = nullptr;
Expand Down
12 changes: 6 additions & 6 deletions src/app/labeling/qgsmaptoolmovelabel.cpp
Expand Up @@ -124,7 +124,7 @@ void QgsMapToolMoveLabel::cadCanvasPressEvent( QgsMapMouseEvent *e )

clearHoveredLabel();

QgsVectorLayer *vlayer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( mCurrentCallout.layerID );
QgsVectorLayer *vlayer = qobject_cast< QgsVectorLayer * >( QgsMapTool::layer( mCurrentCallout.layerID ) );
if ( !vlayer || xCol < 0 || yCol < 0 )
{
return;
Expand Down Expand Up @@ -174,7 +174,7 @@ void QgsMapToolMoveLabel::cadCanvasPressEvent( QgsMapMouseEvent *e )

clearHoveredLabel();

mCurrentLabel = LabelDetails( labelPos );
mCurrentLabel = LabelDetails( labelPos, canvas() );

QgsVectorLayer *vlayer = mCurrentLabel.layer;
if ( !vlayer )
Expand Down Expand Up @@ -275,7 +275,7 @@ void QgsMapToolMoveLabel::cadCanvasPressEvent( QgsMapMouseEvent *e )

deleteRubberBands();

QgsVectorLayer *vlayer = !isCalloutMove ? mCurrentLabel.layer : QgsProject::instance()->mapLayer<QgsVectorLayer *>( mCurrentCallout.layerID );
QgsVectorLayer *vlayer = !isCalloutMove ? mCurrentLabel.layer : qobject_cast< QgsVectorLayer * >( QgsMapTool::layer( mCurrentCallout.layerID ) );
if ( !vlayer )
{
return;
Expand Down Expand Up @@ -452,7 +452,7 @@ void QgsMapToolMoveLabel::keyReleaseEvent( QKeyEvent *e )

// delete the stored label/callout position
const bool isCalloutMove = !mCurrentCallout.layerID.isEmpty();
QgsVectorLayer *vlayer = !isCalloutMove ? mCurrentLabel.layer : QgsProject::instance()->mapLayer<QgsVectorLayer *>( mCurrentCallout.layerID );
QgsVectorLayer *vlayer = !isCalloutMove ? mCurrentLabel.layer : qobject_cast< QgsVectorLayer * >( QgsMapTool::layer( mCurrentCallout.layerID ) );
const QgsFeatureId featureId = !isCalloutMove ? mCurrentLabel.pos.featureId : mCurrentCallout.featureId;
if ( vlayer )
{
Expand Down Expand Up @@ -514,7 +514,7 @@ void QgsMapToolMoveLabel::keyReleaseEvent( QKeyEvent *e )

bool QgsMapToolMoveLabel::canModifyCallout( const QgsCalloutPosition &pos, bool isOrigin, int &xCol, int &yCol )
{
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( pos.layerID );
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsMapTool::layer( pos.layerID ) );
QgsPalLayerSettings settings;
if ( layer && layer->labelsEnabled() )
{
Expand Down Expand Up @@ -558,7 +558,7 @@ bool QgsMapToolMoveLabel::canModifyCallout( const QgsCalloutPosition &pos, bool

bool QgsMapToolMoveLabel::currentCalloutDataDefinedPosition( double &x, bool &xSuccess, double &y, bool &ySuccess, int &xCol, int &yCol )
{
QgsVectorLayer *vlayer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( mCurrentCallout.layerID );
QgsVectorLayer *vlayer = qobject_cast< QgsVectorLayer * >( QgsMapTool::layer( mCurrentCallout.layerID ) );
const QgsFeatureId featureId = mCurrentCallout.featureId;

xSuccess = false;
Expand Down
6 changes: 3 additions & 3 deletions src/app/labeling/qgsmaptoolpinlabels.cpp
Expand Up @@ -196,7 +196,7 @@ void QgsMapToolPinLabels::highlightPinnedLabels()
QList<QgsLabelPosition>::const_iterator it;
for ( const QgsLabelPosition &pos : labelPosList )
{
mCurrentLabel = LabelDetails( pos );
mCurrentLabel = LabelDetails( pos, canvas() );

if ( isPinned() )
{
Expand All @@ -209,7 +209,7 @@ void QgsMapToolPinLabels::highlightPinnedLabels()
}

QColor lblcolor = QColor( 54, 129, 255, 63 );
QgsMapLayer *layer = QgsProject::instance()->mapLayer( pos.layerID );
QgsMapLayer *layer = QgsMapTool::layer( pos.layerID );
if ( !layer )
{
continue;
Expand Down Expand Up @@ -287,7 +287,7 @@ void QgsMapToolPinLabels::pinUnpinLabels( const QgsRectangle &ext, QMouseEvent *
{
const QgsLabelPosition &pos = *it;

mCurrentLabel = LabelDetails( pos );
mCurrentLabel = LabelDetails( pos, canvas() );

if ( !mCurrentLabel.valid )
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/labeling/qgsmaptoolrotatelabel.cpp
Expand Up @@ -99,7 +99,7 @@ void QgsMapToolRotateLabel::canvasPressEvent( QgsMapMouseEvent *e )
return;
}

mCurrentLabel = LabelDetails( labelPos );
mCurrentLabel = LabelDetails( labelPos, canvas() );

if ( !mCurrentLabel.valid )
return;
Expand Down
2 changes: 1 addition & 1 deletion src/app/labeling/qgsmaptoolshowhidelabels.cpp
Expand Up @@ -316,7 +316,7 @@ bool QgsMapToolShowHideLabels::selectedLabelFeatures( QgsVectorLayer *vlayer,

bool QgsMapToolShowHideLabels::showHide( const QgsLabelPosition &pos, bool show )
{
LabelDetails details = LabelDetails( pos );
LabelDetails details = LabelDetails( pos, canvas() );

if ( !details.valid )
return false;
Expand Down
10 changes: 5 additions & 5 deletions tests/src/app/testqgsmaptoollabel.cpp
Expand Up @@ -295,7 +295,7 @@ class TestQgsMapToolLabel : public QObject
QVERIFY( tool->labelAtPosition( event.get(), pos ) );
QCOMPARE( pos.layerID, vl1->id() );
QCOMPARE( pos.labelText, QStringLiteral( "label" ) );
tool->mCurrentLabel = QgsMapToolLabel::LabelDetails( pos );
tool->mCurrentLabel = QgsMapToolLabel::LabelDetails( pos, canvas.get() );

// defaults to bottom left
QString hali, vali;
Expand All @@ -315,7 +315,7 @@ class TestQgsMapToolLabel : public QObject
QVERIFY( tool->labelAtPosition( event.get(), pos ) );
QCOMPARE( pos.layerID, vl1->id() );
QCOMPARE( pos.labelText, QStringLiteral( "label" ) );
tool->mCurrentLabel = QgsMapToolLabel::LabelDetails( pos );
tool->mCurrentLabel = QgsMapToolLabel::LabelDetails( pos, canvas.get() );

tool->currentAlignment( hali, vali );
QCOMPARE( hali, QStringLiteral( "right" ) );
Expand All @@ -330,7 +330,7 @@ class TestQgsMapToolLabel : public QObject
QVERIFY( tool->labelAtPosition( event.get(), pos ) );
QCOMPARE( pos.layerID, vl1->id() );
QCOMPARE( pos.labelText, QStringLiteral( "label" ) );
tool->mCurrentLabel = QgsMapToolLabel::LabelDetails( pos );
tool->mCurrentLabel = QgsMapToolLabel::LabelDetails( pos, canvas.get() );

tool->currentAlignment( hali, vali );
QCOMPARE( hali, QStringLiteral( "center" ) );
Expand All @@ -354,7 +354,7 @@ class TestQgsMapToolLabel : public QObject
QVERIFY( tool->labelAtPosition( event.get(), pos ) );
QCOMPARE( pos.layerID, vl1->id() );
QCOMPARE( pos.labelText, QStringLiteral( "label" ) );
tool->mCurrentLabel = QgsMapToolLabel::LabelDetails( pos );
tool->mCurrentLabel = QgsMapToolLabel::LabelDetails( pos, canvas.get() );

tool->currentAlignment( hali, vali );
QCOMPARE( hali, QStringLiteral( "left" ) );
Expand All @@ -369,7 +369,7 @@ class TestQgsMapToolLabel : public QObject
QVERIFY( tool->labelAtPosition( event.get(), pos ) );
QCOMPARE( pos.layerID, vl1->id() );
QCOMPARE( pos.labelText, QStringLiteral( "label" ) );
tool->mCurrentLabel = QgsMapToolLabel::LabelDetails( pos );
tool->mCurrentLabel = QgsMapToolLabel::LabelDetails( pos, canvas.get() );

tool->currentAlignment( hali, vali );
QCOMPARE( hali, QStringLiteral( "right" ) );
Expand Down

0 comments on commit 1cba45b

Please sign in to comment.