Skip to content
Permalink
Browse files
[labeling] Default to cartographic placement mode for point layers
Results in much nicer label placement, so should be the default mode
  • Loading branch information
nyalldawson committed May 25, 2021
1 parent e7c4c84 commit b190c8f06bbc03159efb1b1a8c4960ab64c27d06
@@ -103,6 +103,13 @@ Returns ``True`` if the visitor should continue visiting other objects, or ``Fal
should be canceled.

.. versionadded:: 3.10
%End

static QgsPalLayerSettings defaultSettingsForLayer( const QgsVectorLayer *layer );
%Docstring
Returns the default layer settings to use for the specified vector ``layer``.

.. versionadded:: 3.20
%End

protected:
@@ -1179,24 +1179,7 @@ void QgsAppLayerTreeViewMenuProvider::toggleLabels( bool enabled )
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;
}

QgsPalLayerSettings settings = QgsAbstractVectorLayerLabeling::defaultSettingsForLayer( vlayer );
vlayer->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
vlayer->setLabelsEnabled( true );
}
@@ -46,6 +46,30 @@ bool QgsAbstractVectorLayerLabeling::accept( QgsStyleEntityVisitorInterface * )
return true;
}

QgsPalLayerSettings QgsAbstractVectorLayerLabeling::defaultSettingsForLayer( const QgsVectorLayer *layer )
{
QgsPalLayerSettings settings;
settings.fieldName = layer->displayField();

switch ( layer->geometryType() )
{
case QgsWkbTypes::PointGeometry:
settings.placement = QgsPalLayerSettings::OrderedPositionsAroundPoint;
break;
case QgsWkbTypes::LineGeometry:
settings.placement = QgsPalLayerSettings::Line;
break;
case QgsWkbTypes::PolygonGeometry:
settings.placement = QgsPalLayerSettings::AroundPoint;
break;

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

QgsVectorLayerLabelProvider *QgsVectorLayerSimpleLabeling::provider( QgsVectorLayer *layer ) const
{
return new QgsVectorLayerLabelProvider( layer, QString(), false, mSettings.get() );
@@ -126,6 +126,13 @@ class CORE_EXPORT QgsAbstractVectorLayerLabeling
*/
virtual bool accept( QgsStyleEntityVisitorInterface *visitor ) const;

/**
* Returns the default layer settings to use for the specified vector \a layer.
*
* \since QGIS 3.20
*/
static QgsPalLayerSettings defaultSettingsForLayer( const QgsVectorLayer *layer );

protected:

/**
@@ -216,7 +216,9 @@ void QgsLabelingWidget::labelModeChanged( int index )
}

if ( !mSimpleSettings )
mSimpleSettings.reset( new QgsPalLayerSettings() );
{
mSimpleSettings = std::make_unique< QgsPalLayerSettings >( QgsAbstractVectorLayerLabeling::defaultSettingsForLayer( mLayer ) );
}

if ( mSimpleSettings->fieldName.isEmpty() )
mSimpleSettings->fieldName = mLayer->displayField();
@@ -133,8 +133,7 @@ void QgsRuleBasedLabelingWidget::setDockMode( bool dockMode )

void QgsRuleBasedLabelingWidget::addRule()
{

QgsRuleBasedLabeling::Rule *newrule = new QgsRuleBasedLabeling::Rule( new QgsPalLayerSettings );
QgsRuleBasedLabeling::Rule *newrule = new QgsRuleBasedLabeling::Rule( new QgsPalLayerSettings( QgsAbstractVectorLayerLabeling::defaultSettingsForLayer( mLayer ) ) );

QgsRuleBasedLabeling::Rule *current = currentRule();
if ( current )

0 comments on commit b190c8f

Please sign in to comment.