Skip to content

Commit

Permalink
Add separate bool for controlling whether vector layer labels
Browse files Browse the repository at this point in the history
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 aa0d377
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 13 deletions.
22 changes: 22 additions & 0 deletions python/core/qgsvectorlayer.sip.in
Expand Up @@ -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;
Expand Down Expand Up @@ -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/ );
Expand Down
1 change: 1 addition & 0 deletions src/app/dwg/qgsdwgimportdialog.cpp
Expand Up @@ -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" ) );
Expand Down
10 changes: 7 additions & 3 deletions src/app/qgslabelingwidget.cpp
Expand Up @@ -50,6 +50,7 @@ void QgsLabelingWidget::resetSettings()
if ( mOldSettings )
{
mLayer->setLabeling( mOldSettings.release() );
mLayer->setLabelsEnabled( mOldLabelsEnabled );
}
setLayer( mLayer );
}
Expand All @@ -75,6 +76,7 @@ void QgsLabelingWidget::setLayer( QgsMapLayer *mapLayer )
}
else
mOldSettings.reset();
mOldLabelsEnabled = mLayer->labelsEnabled();

adaptToLayer();
}
Expand All @@ -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();

Expand All @@ -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 );
}
}

Expand Down
1 change: 1 addition & 0 deletions src/app/qgslabelingwidget.h
Expand Up @@ -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
12 changes: 6 additions & 6 deletions src/app/qgsmaptoollabel.cpp
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/dxf/qgsdxfexport.cpp
Expand Up @@ -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 ) )
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsmaprendererjob.cpp
Expand Up @@ -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;
Expand Down
17 changes: 16 additions & 1 deletion src/core/qgsvectorlayer.cpp
Expand Up @@ -233,6 +233,8 @@ QgsVectorLayer *QgsVectorLayer::clone() const
{
layer->setLabeling( labeling()->clone() );
}
layer->setLabelsEnabled( labelsEnabled() );

layer->setSimplifyMethod( simplifyMethod() );

if ( diagramRenderer() )
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() )
Expand Down Expand Up @@ -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() ) );
Expand Down Expand Up @@ -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() )
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -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
Expand Down Expand Up @@ -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; }

Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayerrenderer.cpp
Expand Up @@ -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 )
Expand Down
1 change: 1 addition & 0 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -2556,6 +2556,7 @@ namespace QgsWms

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

Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/testqgsdxfexport.cpp
Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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 ) );
Expand Down
3 changes: 3 additions & 0 deletions tests/src/core/testqgslabelingengine.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/testqgsmaprotation.cpp
Expand Up @@ -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) );
Expand Down

0 comments on commit aa0d377

Please sign in to comment.