Skip to content

Commit

Permalink
[feature] Add a shortcut action to toggle labels in the vector layer …
Browse files Browse the repository at this point in the history
…right

click menu

Allows for labels to be quickly switched on or off, without
losing the label configuration. If a layer has never had labeling
configured and the action is checked then a default simple
labeling is added to the layer.

Refs Natural resources Canada Contract: 3000720707
  • Loading branch information
nyalldawson committed Mar 3, 2021
1 parent 0432424 commit 54fc3ba
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -44,6 +44,7 @@
#include "qgsxmlutils.h"
#include "qgsmessagebar.h"
#include "qgspointcloudlayer.h"
#include "qgsvectorlayerlabeling.h"


QgsAppLayerTreeViewMenuProvider::QgsAppLayerTreeViewMenuProvider( QgsLayerTreeView *view, QgsMapCanvas *canvas )
Expand Down Expand Up @@ -184,6 +185,19 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
QAction *showFeatureCount = actions->actionShowFeatureCount( menu );
menu->addAction( showFeatureCount );
showFeatureCount->setEnabled( vlayer->isValid() );

const QString iconName = vlayer && vlayer->labeling() && vlayer->labeling()->type() == QLatin1String( "rule-based" )
? QStringLiteral( "labelingRuleBased.svg" )
: QStringLiteral( "labelingSingle.svg" );
QAction *actionShowLabels = new QAction( QgsApplication::getThemeIcon( iconName ), tr( "Show Labels" ), menu );
actionShowLabels->setCheckable( true );
actionShowLabels->setChecked( vlayer->labelsEnabled() );
const QString layerId = vlayer->id();
connect( actionShowLabels, &QAction::toggled, this, [this, layerId]( bool enabled )
{
toggleLabels( layerId, enabled );
} );
menu->addAction( actionShowLabels );
}

QAction *actionCopyLayer = new QAction( tr( "Copy Layer" ), menu );
Expand Down Expand Up @@ -1100,3 +1114,41 @@ void QgsAppLayerTreeViewMenuProvider::setLayerCrs( const QgsCoordinateReferenceS
}
}
}

void QgsAppLayerTreeViewMenuProvider::toggleLabels( const QString &layerId, bool enabled )
{
QgsVectorLayer *vlayer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
if ( !vlayer )
return;

if ( enabled && !vlayer->labeling() )
{
// no labeling setup - create default labeling for layer
QgsPalLayerSettings settings;
settings.fieldName = vlayer->displayField();
switch ( vlayer->geometryType() )
{
case QgsWkbTypes::PointGeometry:
case QgsWkbTypes::PolygonGeometry:
settings.placement = QgsPalLayerSettings::AroundPoint;
break;

case QgsWkbTypes::LineGeometry:
settings.placement = QgsPalLayerSettings::Line;
break;

case QgsWkbTypes::UnknownGeometry:
case QgsWkbTypes::NullGeometry:
break;
}

vlayer->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
vlayer->setLabelsEnabled( true );
}
else
{
vlayer->setLabelsEnabled( enabled );
}
vlayer->emitStyleChanged();
vlayer->triggerRepaint();
}
1 change: 1 addition & 0 deletions src/app/qgsapplayertreeviewmenuprovider.h
Expand Up @@ -72,6 +72,7 @@ class QgsAppLayerTreeViewMenuProvider : public QObject, public QgsLayerTreeViewM
void pasteSymbolLegendNodeSymbol( const QString &layerId, const QString &ruleKey );
void setSymbolLegendNodeColor( const QColor &color );
void setLayerCrs( const QgsCoordinateReferenceSystem &crs );
void toggleLabels( const QString &layerId, bool enabled );
private:
bool removeActionEnabled();
};
Expand Down

0 comments on commit 54fc3ba

Please sign in to comment.