Skip to content
Permalink
Browse files

Add separate bool for controlling whether vector layer labels

are enabled

Instead of clearing the labeling configuration in order to
disable labels, we need a way to disable them without
losing the configuration. Labels are oftern temporarily
switched on/off as an analysis tool, and we don't want the
settings to be lost as a result

Refs #17656
  • Loading branch information
nyalldawson committed Jan 19, 2018
1 parent 232d721 commit aa0d3771642f2e7b90ee7af0e17ad0375197c5e3
@@ -659,6 +659,22 @@ Returns whether the layer contains labels which are enabled and should be drawn.
:return: true if layer contains enabled labels

.. versionadded:: 2.9

.. seealso:: :py:func:`setLabelsEnabled`
%End

void setLabelsEnabled( bool enabled );
%Docstring
Sets whether labels should be ``enabled`` for the layer.

.. note::

Labels will only be rendered if labelsEnabled() is true and a labeling
object is returned by labeling().

.. seealso:: :py:func:`labelsEnabled`

.. seealso:: :py:func:`labeling`
%End

bool diagramsEnabled() const;
@@ -1332,7 +1348,13 @@ editing.
%Docstring
Access to labeling configuration. May be null if labeling is not used.

.. note::

Labels will only be rendered if labelsEnabled() returns true.

.. versionadded:: 3.0

.. seealso:: :py:func:`labelsEnabled`
%End

void setLabeling( QgsAbstractVectorLayerLabeling *labeling /Transfer/ );
@@ -414,6 +414,7 @@ void QgsDwgImportDialog::createGroup( QgsLayerTreeGroup *group, const QString &n

pls.placement = QgsPalLayerSettings::OrderedPositionsAroundPoint;
l->setLabeling( new QgsVectorLayerSimpleLabeling( pls ) );
l->setLabelsEnabled( true );
}

l = layer( layerGroup, layerFilter, QStringLiteral( "points" ) );
@@ -50,6 +50,7 @@ void QgsLabelingWidget::resetSettings()
if ( mOldSettings )
{
mLayer->setLabeling( mOldSettings.release() );
mLayer->setLabelsEnabled( mOldLabelsEnabled );
}
setLayer( mLayer );
}
@@ -75,6 +76,7 @@ void QgsLabelingWidget::setLayer( QgsMapLayer *mapLayer )
}
else
mOldSettings.reset();
mOldLabelsEnabled = mLayer->labelsEnabled();

adaptToLayer();
}
@@ -87,11 +89,11 @@ void QgsLabelingWidget::adaptToLayer()
whileBlocking( mLabelModeComboBox )->setCurrentIndex( -1 );

// pick the right mode of the layer
if ( mLayer->labeling() && mLayer->labeling()->type() == QLatin1String( "rule-based" ) )
if ( mLayer->labelsEnabled() && mLayer->labeling()->type() == QLatin1String( "rule-based" ) )
{
mLabelModeComboBox->setCurrentIndex( 2 );
}
else if ( mLayer->labeling() && mLayer->labeling()->type() == QLatin1String( "simple" ) )
else if ( mLayer->labelsEnabled() && mLayer->labeling()->type() == QLatin1String( "simple" ) )
{
QgsPalLayerSettings lyr = mLayer->labeling()->settings();

@@ -117,14 +119,16 @@ void QgsLabelingWidget::writeSettingsToLayer()
const QgsRuleBasedLabeling::Rule *rootRule = qobject_cast<QgsRuleBasedLabelingWidget *>( mWidget )->rootRule();

mLayer->setLabeling( new QgsRuleBasedLabeling( rootRule->clone() ) );
mLayer->setLabelsEnabled( true );
}
else if ( index == 1 || index == 3 )
{
mLayer->setLabeling( new QgsVectorLayerSimpleLabeling( qobject_cast<QgsLabelingGui *>( mWidget )->layerSettings() ) );
mLayer->setLabelsEnabled( true );
}
else
{
mLayer->setLabeling( nullptr );
mLayer->setLabelsEnabled( false );
}
}

@@ -73,6 +73,7 @@ class QgsLabelingWidget : public QgsMapLayerConfigWidget, private Ui::QgsLabelin
QWidget *mWidget = nullptr;
std::unique_ptr< QgsPalLayerSettings > mSimpleSettings;
std::unique_ptr< QgsAbstractVectorLayerLabeling > mOldSettings;
bool mOldLabelsEnabled = false;
};

#endif // QGSLABELINGWIDGET_H
@@ -470,7 +470,7 @@ bool QgsMapToolLabel::currentLabelDataDefinedPosition( double &x, bool &xSuccess

bool QgsMapToolLabel::layerIsRotatable( QgsVectorLayer *vlayer, int &rotationCol ) const
{
if ( !vlayer || !vlayer->isEditable() || !vlayer->labeling() )
if ( !vlayer || !vlayer->isEditable() || !vlayer->labelsEnabled() )
{
return false;
}
@@ -591,7 +591,7 @@ bool QgsMapToolLabel::diagramMoveable( QgsVectorLayer *vlayer, int &xCol, int &y

bool QgsMapToolLabel::labelMoveable( QgsVectorLayer *vlayer, int &xCol, int &yCol ) const
{
if ( !vlayer || !vlayer->isEditable() || !vlayer->labeling() )
if ( !vlayer || !vlayer->isEditable() || !vlayer->labelsEnabled() )
{
return false;
}
@@ -624,7 +624,7 @@ bool QgsMapToolLabel::layerCanPin( QgsVectorLayer *vlayer, int &xCol, int &yCol

bool QgsMapToolLabel::labelCanShowHide( QgsVectorLayer *vlayer, int &showCol ) const
{
if ( !vlayer || !vlayer->isEditable() || !vlayer->labeling() )
if ( !vlayer || !vlayer->isEditable() || !vlayer->labelsEnabled() )
{
return false;
}
@@ -691,7 +691,7 @@ QgsMapToolLabel::LabelDetails::LabelDetails( const QgsLabelPosition &p )
: pos( p )
{
layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( pos.layerID ) );
if ( layer && layer->labeling() && !p.isDiagram )
if ( layer && layer->labelsEnabled() && !p.isDiagram )
{
settings = layer->labeling()->settings( pos.providerID );
valid = true;
@@ -719,8 +719,8 @@ bool QgsMapToolLabel::createAuxiliaryFields( LabelDetails &details, QgsPalIndexe
QgsVectorLayer *vlayer = details.layer;
QString providerId = details.pos.providerID;

if ( !vlayer || !vlayer->labeling() )
return newAuxiliaryLayer;
if ( !vlayer || !vlayer->labelsEnabled() )
return false;

if ( !vlayer->auxiliaryLayer() )
{
@@ -1009,7 +1009,7 @@ void QgsDxfExport::writeEntities()
attributes << layerAttr;
}

const QgsAbstractVectorLayerLabeling *labeling = vl->labeling();
const QgsAbstractVectorLayerLabeling *labeling = vl->labelsEnabled() ? vl->labeling() : nullptr;
QgsDxfLabelProvider *lp = nullptr;
QgsDxfRuleBasedLabelProvider *rblp = nullptr;
if ( const QgsRuleBasedLabeling *rbl = dynamic_cast<const QgsRuleBasedLabeling *>( labeling ) )
@@ -91,7 +91,7 @@ bool QgsMapRendererJob::prepareLabelCache() const
QgsVectorLayer *vl = const_cast< QgsVectorLayer * >( qobject_cast<const QgsVectorLayer *>( ml ) );
if ( vl && QgsPalLabeling::staticWillUseLayer( vl ) )
labeledLayers << vl;
if ( vl && vl->labeling() && vl->labeling()->requiresAdvancedEffects() )
if ( vl && vl->labelsEnabled() && vl->labeling()->requiresAdvancedEffects() )
{
canCache = false;
break;
@@ -233,6 +233,8 @@ QgsVectorLayer *QgsVectorLayer::clone() const
{
layer->setLabeling( labeling()->clone() );
}
layer->setLabelsEnabled( labelsEnabled() );

layer->setSimplifyMethod( simplifyMethod() );

if ( diagramRenderer() )
@@ -666,7 +668,12 @@ QgsRectangle QgsVectorLayer::boundingBoxOfSelected() const

bool QgsVectorLayer::labelsEnabled() const
{
return static_cast< bool >( mLabeling );
return mLabelsEnabled && static_cast< bool >( mLabeling );
}

void QgsVectorLayer::setLabelsEnabled( bool enabled )
{
mLabelsEnabled = enabled;
}

bool QgsVectorLayer::diagramsEnabled() const
@@ -1949,6 +1956,11 @@ bool QgsVectorLayer::readStyle( const QDomNode &node, QString &errorMessage, con
}
setLabeling( labeling );

if ( node.toElement().hasAttribute( QStringLiteral( "labelsEnabled" ) ) )
mLabelsEnabled = node.toElement().attribute( QStringLiteral( "labelsEnabled" ) ).toInt();
else
mLabelsEnabled = true;

// get and set the blend mode if it exists
QDomNode blendModeNode = node.namedItem( QStringLiteral( "blendMode" ) );
if ( !blendModeNode.isNull() )
@@ -2224,6 +2236,7 @@ bool QgsVectorLayer::writeStyle( QDomNode &node, QDomDocument &doc, QString &err
QDomElement labelingElement = mLabeling->save( doc, context );
node.appendChild( labelingElement );
}
mapLayerNode.setAttribute( QStringLiteral( "labelsEnabled" ), mLabelsEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );

// save the simplification drawing settings
mapLayerNode.setAttribute( QStringLiteral( "simplifyDrawingHints" ), QString::number( mSimplifyMethod.simplifyHints() ) );
@@ -3711,6 +3724,7 @@ double QgsVectorLayer::opacity() const
void QgsVectorLayer::readSldLabeling( const QDomNode &node )
{
setLabeling( nullptr ); // start with no labeling
setLabelsEnabled( false );

QDomElement element = node.toElement();
if ( element.isNull() )
@@ -3931,6 +3945,7 @@ void QgsVectorLayer::readSldLabeling( const QDomNode &node )
format.setBuffer( bufferSettings );
settings.setFormat( format );
setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
setLabelsEnabled( true );
}

QgsEditFormConfig QgsVectorLayer::editFormConfig() const
@@ -695,9 +695,22 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
* Returns whether the layer contains labels which are enabled and should be drawn.
* \returns true if layer contains enabled labels
* \since QGIS 2.9
*
* \see setLabelsEnabled()
*/
bool labelsEnabled() const;

/**
* Sets whether labels should be \a enabled for the layer.
*
* \note Labels will only be rendered if labelsEnabled() is true and a labeling
* object is returned by labeling().
*
* \see labelsEnabled()
* \see labeling()
*/
void setLabelsEnabled( bool enabled );

/**
* Returns whether the layer contains diagrams which are enabled and should be drawn.
* \returns true if layer contains enabled diagrams
@@ -1249,13 +1262,17 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte

/**
* Access to const labeling configuration. May be null if labeling is not used.
* \note Labels will only be rendered if labelsEnabled() returns true.
* \since QGIS 3.0
* \see labelsEnabled()
*/
const QgsAbstractVectorLayerLabeling *labeling() const SIP_SKIP { return mLabeling; }

/**
* Access to labeling configuration. May be null if labeling is not used.
* \note Labels will only be rendered if labelsEnabled() returns true.
* \since QGIS 3.0
* \see labelsEnabled()
*/
QgsAbstractVectorLayerLabeling *labeling() { return mLabeling; }

@@ -2339,6 +2356,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
//! Labeling configuration
QgsAbstractVectorLayerLabeling *mLabeling = nullptr;

//! True if labels are enabled
bool mLabelsEnabled = false;

//! Whether 'labeling font not found' has be shown for this layer (only show once in QgsMessageBar, on first rendering)
bool mLabelFontNotFoundNotified = false;

@@ -480,7 +480,7 @@ void QgsVectorLayerRenderer::prepareLabeling( QgsVectorLayer *layer, QSet<QStrin
{
if ( QgsLabelingEngine *engine2 = mContext.labelingEngine() )
{
if ( layer->labeling() )
if ( layer->labelsEnabled() )
{
mLabelProvider = layer->labeling()->provider( layer );
if ( mLabelProvider )
@@ -2556,6 +2556,7 @@ namespace QgsWms

QgsVectorLayerSimpleLabeling *simpleLabeling = new QgsVectorLayerSimpleLabeling( palSettings );
layer->setLabeling( simpleLabeling );
layer->setLabelsEnabled( true );
}
fet.setGeometry( param.mGeom );

@@ -186,6 +186,7 @@ void TestQgsDxfExport::testMtext()
format.setColor( QColor( 200, 0, 200 ) );
settings.setFormat( format );
mPointLayer->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
mPointLayer->setLabelsEnabled( true );

QgsDxfExport d;
d.addLayers( QList< QPair< QgsVectorLayer *, int > >() << qMakePair( mPointLayer, -1 ) );
@@ -247,6 +248,7 @@ void TestQgsDxfExport::testText()
format.setColor( QColor( 200, 0, 200 ) );
settings.setFormat( format );
mPointLayer->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
mPointLayer->setLabelsEnabled( true );

QgsDxfExport d;
d.addLayers( QList< QPair< QgsVectorLayer *, int > >() << qMakePair( mPointLayer, -1 ) );
@@ -134,6 +134,7 @@ void TestQgsLabelingEngine::testBasic()
setDefaultLabelParams( settings );

vl->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) ); // TODO: this should not be necessary!
vl->setLabelsEnabled( true );

QgsLabelingEngine engine;
engine.setMapSettings( mapSettings );
@@ -246,6 +247,7 @@ void TestQgsLabelingEngine::testRuleBased()
root->appendChild( new QgsRuleBasedLabeling::Rule( new QgsPalLayerSettings( s2 ), 0, 0, QStringLiteral( "Class = 'Jet'" ) ) );

vl->setLabeling( new QgsRuleBasedLabeling( root ) );
vl->setLabelsEnabled( true );
//setDefaultLabelParams( vl );

QgsMapRendererSequentialJob job( mapSettings );
@@ -653,6 +655,7 @@ void TestQgsLabelingEngine::testRotateHidePartial()
vl2->dataProvider()->addFeature( f );

vl2->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) ); // TODO: this should not be necessary!
vl2->setLabelsEnabled( true );

// make a fake render context
QSize size( 640, 480 );
@@ -207,6 +207,7 @@ void TestQgsMapRotation::linesLayer()
format.setSize( 16 );
palSettings.setFormat( format );
mLinesLayer->setLabeling( new QgsVectorLayerSimpleLabeling( palSettings ) );
mLinesLayer->setLabelsEnabled( true );

QVERIFY( success );
mMapSettings->setExtent( mLinesLayer->extent() ); //QgsRectangle(-150,-150,150,150) );

0 comments on commit aa0d377

Please sign in to comment.
You can’t perform that action at this time.