Skip to content
Permalink
Browse files

Keep label settings when changing between rulebased/simple labeling

Fixes #17734
  • Loading branch information
nyalldawson committed Jan 19, 2018
1 parent aa0d377 commit 05161362d3876a2b1a6660a0027ace094f86ef8f
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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))
@@ -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))
@@ -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
@@ -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))
@@ -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))
@@ -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")
@@ -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")
@@ -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))

0 comments on commit 0516136

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