Skip to content

Commit

Permalink
Keep label settings when changing between rulebased/simple labeling
Browse files Browse the repository at this point in the history
Fixes #17734
  • Loading branch information
nyalldawson committed Jan 19, 2018
1 parent aa0d377 commit 0516136
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/app/qgslabelingwidget.cpp
Expand Up @@ -153,6 +153,7 @@ void QgsLabelingWidget::labelModeChanged( int index )

if ( index == 2 )
{
// note - QgsRuleBasedLabelingWidget handles conversion of existing non-rule based labels to rule based
QgsRuleBasedLabelingWidget *ruleWidget = new QgsRuleBasedLabelingWidget( mLayer, mCanvas, this );
ruleWidget->setDockMode( dockMode() );
connect( ruleWidget, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
Expand All @@ -163,9 +164,26 @@ void QgsLabelingWidget::labelModeChanged( int index )
}
else if ( index == 1 || index == 3 )
{
mSimpleSettings.reset();
if ( mLayer->labeling() && mLayer->labeling()->type() == QLatin1String( "simple" ) )
{
mSimpleSettings.reset( new QgsPalLayerSettings( mLayer->labeling()->settings() ) );
else
}
else if ( mLayer->labeling() && mLayer->labeling()->type() == QLatin1String( "rule-based" ) )
{
// changing from rule-based to simple labels... grab first rule, and copy settings
const QgsRuleBasedLabeling *rl = static_cast<const QgsRuleBasedLabeling *>( mLayer->labeling() );
if ( const QgsRuleBasedLabeling::Rule *rootRule = rl->rootRule() )
{
if ( const QgsRuleBasedLabeling::Rule *firstChild = rootRule->children().value( 0 ) )
{
if ( firstChild->settings() )
mSimpleSettings.reset( new QgsPalLayerSettings( *firstChild->settings() ) );
}
}
}

if ( !mSimpleSettings )
mSimpleSettings.reset( new QgsPalLayerSettings() );

QgsLabelingGui *simpleWidget = new QgsLabelingGui( mLayer, mCanvas, *mSimpleSettings, this );
Expand Down
8 changes: 8 additions & 0 deletions src/app/qgsrulebasedlabelingwidget.cpp
Expand Up @@ -87,6 +87,14 @@ QgsRuleBasedLabelingWidget::QgsRuleBasedLabelingWidget( QgsVectorLayer *layer, Q
const QgsRuleBasedLabeling *rl = static_cast<const QgsRuleBasedLabeling *>( mLayer->labeling() );
mRootRule = rl->rootRule()->clone();
}
else if ( mLayer->labeling() && mLayer->labeling()->type() == QLatin1String( "simple" ) )
{
// copy simple label settings to first rule
mRootRule = new QgsRuleBasedLabeling::Rule( nullptr );
std::unique_ptr< QgsPalLayerSettings > newSettings = qgis::make_unique< QgsPalLayerSettings >( mLayer->labeling()->settings() );
newSettings->drawLabels = true; // otherwise we may be trying to copy a "blocking" setting to a rule - which is confusing for users!
mRootRule->appendChild( new QgsRuleBasedLabeling::Rule( newSettings.release() ) );
}
else
{
mRootRule = new QgsRuleBasedLabeling::Rule( nullptr );
Expand Down
9 changes: 9 additions & 0 deletions tests/src/python/test_qgsmaprenderer.py
Expand Up @@ -129,6 +129,7 @@ def checkRepaintLabeledLayerInvalidatesLabelCache(self, job_type):
labelSettings = QgsPalLayerSettings()
labelSettings.fieldName = "fldtxt"
layer.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))
layer.setLabelsEnabled(True)

settings = QgsMapSettings()
settings.setExtent(QgsRectangle(5, 25, 25, 45))
Expand Down Expand Up @@ -170,6 +171,7 @@ def checkAddingNewLabeledLayerInvalidatesLabelCache(self, job_type):
labelSettings = QgsPalLayerSettings()
labelSettings.fieldName = "fldtxt"
layer.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))
layer.setLabelsEnabled(True)

settings = QgsMapSettings()
settings.setExtent(QgsRectangle(5, 25, 25, 45))
Expand All @@ -192,6 +194,7 @@ def checkAddingNewLabeledLayerInvalidatesLabelCache(self, job_type):
layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")
layer2.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))
layer2.setLabelsEnabled(True)
settings.setLayers([layer, layer2])

# second job should not be able to use label cache, since a new layer was added
Expand All @@ -214,6 +217,7 @@ def checkAddingNewNonLabeledLayerKeepsLabelCache(self, job_type):
labelSettings = QgsPalLayerSettings()
labelSettings.fieldName = "fldtxt"
layer.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))
layer.setLabelsEnabled(True)

settings = QgsMapSettings()
settings.setExtent(QgsRectangle(5, 25, 25, 45))
Expand Down Expand Up @@ -257,10 +261,12 @@ def checkRemovingLabeledLayerInvalidatesLabelCache(self, job_type):
labelSettings = QgsPalLayerSettings()
labelSettings.fieldName = "fldtxt"
layer.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))
layer.setLabelsEnabled(True)

layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")
layer2.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))
layer2.setLabelsEnabled(True)

settings = QgsMapSettings()
settings.setExtent(QgsRectangle(5, 25, 25, 45))
Expand Down Expand Up @@ -302,6 +308,7 @@ def checkRemovingNonLabeledLayerKeepsLabelCache(self, job_type):
labelSettings = QgsPalLayerSettings()
labelSettings.fieldName = "fldtxt"
layer.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))
layer.setLabelsEnabled(True)

layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")
Expand Down Expand Up @@ -346,6 +353,7 @@ def checkLabeledLayerWithBlendModesCannotBeCached(self, job_type):
labelSettings = QgsPalLayerSettings()
labelSettings.fieldName = "fldtxt"
layer.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))
layer.setLabelsEnabled(True)

layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")
Expand All @@ -355,6 +363,7 @@ def checkLabeledLayerWithBlendModesCannotBeCached(self, job_type):
format2.setBlendMode(QPainter.CompositionMode_SourceIn)
labelSettings2.setFormat(format2)
layer2.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings2))
layer2.setLabelsEnabled(True)

settings = QgsMapSettings()
settings.setExtent(QgsRectangle(5, 25, 25, 45))
Expand Down

0 comments on commit 0516136

Please sign in to comment.