Skip to content

Commit

Permalink
typo, lambda and other minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vcloarec committed Dec 3, 2020
1 parent c79123b commit ac19bd3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 47 deletions.
2 changes: 1 addition & 1 deletion python/gui/auto_generated/qgsmaptool.sip.in
Expand Up @@ -223,7 +223,7 @@ Any new actions added to the menu should be correctly parented to ``menu``.

A pointer to the map mouse ``event`` can be provided to allow particular behavior depending on the map tool.

This method can return true to inform the caller that the menu was effectivly populated.
This method can return true to inform the caller that the menu was effectively populated.

The default implementation does nothing and returns false.

Expand Down
6 changes: 5 additions & 1 deletion src/app/qgsmaptoolselect.cpp
Expand Up @@ -144,9 +144,13 @@ QgsMapTool::Flags QgsMapToolSelect::flags() const
bool QgsMapToolSelect::populateContextMenuWithEvent( QMenu *menu, QgsMapMouseEvent *event )
{
Q_ASSERT( menu );
menu->addSeparator();
QgsVectorLayer *vlayer = QgsMapToolSelectUtils::getCurrentVectorLayer( mCanvas );

if ( !vlayer )
return false;

menu->addSeparator();

Qt::KeyboardModifiers modifiers = Qt::NoModifier;
QgsPointXY mapPoint;
if ( event )
Expand Down
84 changes: 42 additions & 42 deletions src/app/qgsmaptoolselectutils.cpp
Expand Up @@ -483,7 +483,7 @@ void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::onSearchFinished()
mAllFeatureIds = mFutureWatcher->result();
mActionChooseAll->setText( textForChooseAll( mAllFeatureIds.size() ) );
if ( !mAllFeatureIds.isEmpty() )
connect( mActionChooseAll, &QAction::hovered, this, &QgsMapToolSelectMenuActions::highlightFeatures );
connect( mActionChooseAll, &QAction::hovered, this, &QgsMapToolSelectMenuActions::highlightAllFeatures );
else
mActionChooseAll->setEnabled( false );
if ( mAllFeatureIds.count() > 1 )
Expand Down Expand Up @@ -514,7 +514,7 @@ QString QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::textForChooseAll( qi

QString featureCountText;
if ( featureCount < 0 )
featureCountText = QStringLiteral( "Searching..." );
featureCountText = tr( "Searching" );
else
featureCountText = QString::number( featureCount );

Expand Down Expand Up @@ -561,41 +561,45 @@ QString QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::textForChooseOneMenu

void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::populateChooseOneMenu( const QgsFeatureIds &ids )
{
if ( !mVectorLayer )
return;

QgsFeatureIds displayedFeatureIds;

QgsFeatureIds::ConstIterator it = ids.constBegin();
while ( displayedFeatureIds.count() <= 20 && it != ids.constEnd() ) //for now hardcoded, but maybe define a settings for this
displayedFeatureIds.insert( *( it++ ) );

QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mVectorLayer ) );
QgsExpression exp = mVectorLayer->displayExpression();
exp.prepare( &context );

for ( QgsFeatureId id : qgis::as_const( displayedFeatureIds ) )
{
QAction *featureAction = new QAction( tr( "Feature %1" ).arg( id ), this ) ;
featureAction->setData( id );
connect( featureAction, &QAction::triggered, this, &QgsMapToolSelectMenuActions::chooseOneCandidateFeature );
connect( featureAction, &QAction::hovered, this, &QgsMapToolSelectMenuActions::highlightFeatures );
QgsFeature feat = mVectorLayer->getFeature( id );
context.setFeature( feat );

QString featureTitle = exp.evaluate( &context ).toString();
if ( featureTitle.isEmpty() )
featureTitle = FID_TO_STRING( feat.id() );

QAction *featureAction = new QAction( tr( "Feature %1" ).arg( featureTitle ), this ) ;
connect( featureAction, &QAction::triggered, this, [this, id]() {chooseOneCandidateFeature( id );} );
connect( featureAction, &QAction::hovered, this, [this, id]() {this->highlightOneFeature( id );} );
mMenuChooseOne->addAction( featureAction );
}

mMenuChooseOne->setEnabled( ids.count() != 0 );
}

void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::chooseOneCandidateFeature()
void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::chooseOneCandidateFeature( QgsFeatureId id )
{
if ( !mVectorLayer )
return;
QAction *senderAction = qobject_cast<QAction *>( sender() );

if ( !senderAction )
return;

QVariant featureVariant = senderAction->data();

QgsFeatureIds ids;
if ( featureVariant.type() == QVariant::LongLong )
ids.insert( featureVariant.toLongLong() );

if ( !ids.empty() )
mVectorLayer->selectByIds( ids, mBehavior );
ids << id;
mVectorLayer->selectByIds( ids, mBehavior );
}

void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::chooseAllCandidateFeature()
Expand All @@ -615,38 +619,17 @@ void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::chooseAllCandidateFeatu
mVectorLayer->selectByIds( mAllFeatureIds, mBehavior );
}

void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::highlightFeatures()
void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::highlightAllFeatures()
{
removeHighlight();

if ( !mVectorLayer )
return;

QAction *senderAction = qobject_cast<QAction *>( sender() );
if ( !senderAction )
return;

QgsFeatureIds ids;
if ( senderAction == mActionChooseAll )
ids = mAllFeatureIds;
else
{
QVariant featureVariant = senderAction->data();
if ( featureVariant.type() == QVariant::List )
{
QVariantList list = featureVariant.toList();
for ( const QVariant &var : list )
ids.insert( var.toLongLong() );
}

if ( featureVariant.type() == QVariant::LongLong )
ids.insert( featureVariant.toLongLong() );
}

if ( !ids.empty() )
if ( !mAllFeatureIds.empty() )
{
int count = 0;
for ( const QgsFeatureId &id : ids )
for ( const QgsFeatureId &id : mAllFeatureIds )
{
QgsFeature feat = mVectorLayer->getFeature( id );
QgsGeometry geom = feat.geometry();
Expand All @@ -663,6 +646,23 @@ void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::highlightFeatures()
}
}

void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::highlightOneFeature( QgsFeatureId id )
{
removeHighlight();

if ( !mVectorLayer )
return;

QgsFeature feat = mVectorLayer->getFeature( id );
QgsGeometry geom = feat.geometry();
if ( !geom.isEmpty() )
{
QgsHighlight *hl = new QgsHighlight( mCanvas, geom, mVectorLayer );
styleHighlight( hl );
mHighlight.append( hl );
}
}

void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::styleHighlight( QgsHighlight *highlight )
{
QgsSettings settings;
Expand Down
6 changes: 4 additions & 2 deletions src/app/qgsmaptoolselectutils.h
Expand Up @@ -152,9 +152,8 @@ namespace QgsMapToolSelectUtils
void populateMenu( QMenu *menu );

private slots:
void chooseOneCandidateFeature();
void chooseAllCandidateFeature();
void highlightFeatures();
void highlightAllFeatures();
void onLayerDestroyed();
void removeHighlight();
void onSearchFinished();
Expand Down Expand Up @@ -197,6 +196,9 @@ namespace QgsMapToolSelectUtils
std::shared_ptr<DataForSearchingJob> mJobData;

static QgsFeatureIds search( std::shared_ptr<DataForSearchingJob> data );

void chooseOneCandidateFeature( QgsFeatureId id );
void highlightOneFeature( QgsFeatureId id );
};

}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsmaptool.h
Expand Up @@ -224,7 +224,7 @@ class GUI_EXPORT QgsMapTool : public QObject
*
* A pointer to the map mouse \a event can be provided to allow particular behavior depending on the map tool.
*
* This method can return true to inform the caller that the menu was effectivly populated.
* This method can return true to inform the caller that the menu was effectively populated.
*
* The default implementation does nothing and returns false.
*
Expand Down

0 comments on commit ac19bd3

Please sign in to comment.