Skip to content

Commit

Permalink
Fix saving of "obstacle only" state + misc fixes to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed May 16, 2017
1 parent bfdc84f commit f153e19
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 67 deletions.
1 change: 1 addition & 0 deletions doc/api_break.dox
Expand Up @@ -1667,6 +1667,7 @@ QgsPalLabeling {#qgis_api_break_3_0_QgsPalLabeling}
QgsPalLayerSettings {#qgis_api_break_3_0_QgsPalLayerSettings}
-------------------

- "enabled" member variable has been removed. Labeling is enabled if layer.labeling() does not return null pointer. To disable labeling, call layer.setLabeling() with null pointer.
- ct is now a QgsCoordinateTransform object, not a pointer. An invalid QgsCoordinateTransform will
be used instead of a null pointer if no transformation is required.
- prepareGeometry() and geometryRequiresPreparation() now take a QgsCoordinateTransform reference, not a pointer. An invalid QgsCoordinateTransform should be used instead of a null pointer if no transformation is required.
Expand Down
3 changes: 0 additions & 3 deletions python/core/qgspallabeling.sip
Expand Up @@ -273,9 +273,6 @@ class QgsPalLayerSettings
*/
static const QgsPropertiesDefinition &propertyDefinitions();

// whether to label this layer
bool enabled;

/** Whether to draw labels for this layer. For some layers it may be desirable
* to register their features as obstacles for other labels without requiring
* labels to be drawn for the layer itself. In this case drawLabels can be set
Expand Down
1 change: 0 additions & 1 deletion src/app/dwg/qgsdwgimportdialog.cpp
Expand Up @@ -356,7 +356,6 @@ void QgsDwgImportDialog::createGroup( QgsLayerTreeGroup *group, QString name, QS
QgsPalLayerSettings pls;
pls.setFormat( tf );

pls.enabled = true;
pls.drawLabels = true;
pls.fieldName = "text";
pls.wrapChar = "\\P";
Expand Down
1 change: 0 additions & 1 deletion src/app/qgslabelinggui.cpp
Expand Up @@ -295,7 +295,6 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
{
QgsPalLayerSettings lyr;

lyr.enabled = ( mMode == Labels || mMode == ObstaclesOnly );
lyr.drawLabels = ( mMode == Labels );

bool isExpression;
Expand Down
10 changes: 1 addition & 9 deletions src/app/qgslabelingwidget.cpp
Expand Up @@ -90,15 +90,7 @@ void QgsLabelingWidget::adaptToLayer()
{
QgsPalLayerSettings lyr = mLayer->labeling()->settings();

// enable/disable main options based upon whether layer is being labeled
if ( !lyr.enabled )
{
mLabelModeComboBox->setCurrentIndex( 0 );
}
else
{
mLabelModeComboBox->setCurrentIndex( lyr.drawLabels ? 1 : 3 );
}
mLabelModeComboBox->setCurrentIndex( lyr.drawLabels ? 1 : 3 );
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoollabel.cpp
Expand Up @@ -698,7 +698,7 @@ QgsMapToolLabel::LabelDetails::LabelDetails( const QgsLabelPosition &p )
if ( p.isDiagram )
valid = layer->diagramsEnabled();
else
valid = settings.enabled;
valid = true;
}

if ( !valid )
Expand Down
12 changes: 5 additions & 7 deletions src/core/qgspallabeling.cpp
Expand Up @@ -233,7 +233,6 @@ QgsPalLayerSettings::QgsPalLayerSettings()
{
initPropertyDefinitions();

enabled = false;
drawLabels = true;
isExpression = false;
fieldIndex = 0;
Expand Down Expand Up @@ -317,7 +316,6 @@ QgsPalLayerSettings &QgsPalLayerSettings::operator=( const QgsPalLayerSettings &

// copy only permanent stuff

enabled = s.enabled;
drawLabels = s.drawLabels;

// text style
Expand Down Expand Up @@ -534,7 +532,6 @@ void QgsPalLayerSettings::readFromLayerCustomProperties( QgsVectorLayer *layer )

// NOTE: set defaults for newly added properties, for backwards compatibility

enabled = layer->labelsEnabled();
drawLabels = layer->customProperty( QStringLiteral( "labeling/drawLabels" ), true ).toBool();

mFormat.readFromLayer( layer );
Expand Down Expand Up @@ -673,9 +670,6 @@ void QgsPalLayerSettings::readFromLayerCustomProperties( QgsVectorLayer *layer )

void QgsPalLayerSettings::readXml( QDomElement &elem, const QgsReadWriteContext &context )
{
enabled = true;
drawLabels = true;

// text style
QDomElement textStyleElem = elem.firstChildElement( QStringLiteral( "text-style" ) );
fieldName = textStyleElem.attribute( QStringLiteral( "fieldName" ) );
Expand Down Expand Up @@ -756,6 +750,9 @@ void QgsPalLayerSettings::readXml( QDomElement &elem, const QgsReadWriteContext

// rendering
QDomElement renderingElem = elem.firstChildElement( QStringLiteral( "rendering" ) );

drawLabels = renderingElem.attribute( QStringLiteral( "drawLabels" ), QStringLiteral( "1" ) ).toInt();

scaleMin = renderingElem.attribute( QStringLiteral( "scaleMin" ), QStringLiteral( "0" ) ).toInt();
scaleMax = renderingElem.attribute( QStringLiteral( "scaleMax" ), QStringLiteral( "0" ) ).toInt();
scaleVisibility = renderingElem.attribute( QStringLiteral( "scaleVisibility" ) ).toInt();
Expand Down Expand Up @@ -794,7 +791,7 @@ void QgsPalLayerSettings::readXml( QDomElement &elem, const QgsReadWriteContext

QDomElement QgsPalLayerSettings::writeXml( QDomDocument &doc, const QgsReadWriteContext &context )
{
// we assume (enabled == true && drawLabels == true) so those are not saved


QDomElement textStyleElem = mFormat.writeXml( doc, context );

Expand Down Expand Up @@ -848,6 +845,7 @@ QDomElement QgsPalLayerSettings::writeXml( QDomDocument &doc, const QgsReadWrite

// rendering
QDomElement renderingElem = doc.createElement( QStringLiteral( "rendering" ) );
renderingElem.setAttribute( QStringLiteral( "drawLabels" ), drawLabels );
renderingElem.setAttribute( QStringLiteral( "scaleVisibility" ), scaleVisibility );
renderingElem.setAttribute( QStringLiteral( "scaleMin" ), scaleMin );
renderingElem.setAttribute( QStringLiteral( "scaleMax" ), scaleMax );
Expand Down
4 changes: 0 additions & 4 deletions src/core/qgspallabeling.h
Expand Up @@ -377,10 +377,6 @@ class CORE_EXPORT QgsPalLayerSettings
*/
static const QgsPropertiesDefinition &propertyDefinitions();


// whether to label this layer
bool enabled;

/** Whether to draw labels for this layer. For some layers it may be desirable
* to register their features as obstacles for other labels without requiring
* labels to be drawn for the layer itself. In this case drawLabels can be set
Expand Down
3 changes: 1 addition & 2 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -3513,8 +3513,7 @@ void QgsVectorLayer::readSldLabeling( const QDomNode &node )
QDomElement propertyNameElem = labelElem.firstChildElement( QStringLiteral( "PropertyName" ) );
if ( !propertyNameElem.isNull() )
{
// enable labeling + set labeling defaults
settings.enabled = true;
// set labeling defaults

// label attribute
QString labelAttribute = propertyNameElem.text();
Expand Down
1 change: 0 additions & 1 deletion src/server/qgswmsconfigparser.cpp
Expand Up @@ -389,7 +389,6 @@ QgsVectorLayer *QgsWmsConfigParser::createHighlightLayer( int i, const QString &
}

QgsPalLayerSettings settings;
settings.enabled = true;
settings.fieldName = "label";

//give highest priority to highlight layers and make sure the labels are always drawn
Expand Down
10 changes: 4 additions & 6 deletions tests/src/core/testqgslabelingengine.cpp
Expand Up @@ -98,8 +98,8 @@ void TestQgsLabelingEngine::cleanup()
void TestQgsLabelingEngine::setDefaultLabelParams( QgsPalLayerSettings &settings )
{
QgsTextFormat format;
format.setFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Bold" ) ) );
format.setSize( 12.4 ); // TODO: why does it render nothing when point size == 12 ???
format.setFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Bold" ) ).family() );
format.setSize( 12 );
format.setNamedStyle( "Bold" );
format.setColor( QColor( 200, 0, 200 ) );
settings.setFormat( format );
Expand Down Expand Up @@ -127,10 +127,11 @@ void TestQgsLabelingEngine::testBasic()
context.setPainter( &p );

QgsPalLayerSettings settings;
settings.enabled = true;
settings.fieldName = "Class";
setDefaultLabelParams( settings );

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

QgsLabelingEngine engine;
engine.setMapSettings( mapSettings );
engine.addProvider( new QgsVectorLayerLabelProvider( vl, QString(), true, &settings ) );
Expand Down Expand Up @@ -211,7 +212,6 @@ void TestQgsLabelingEngine::testRuleBased()
QgsRuleBasedLabeling::Rule *root = new QgsRuleBasedLabeling::Rule( 0 );

QgsPalLayerSettings s1;
s1.enabled = true;
s1.fieldName = QStringLiteral( "Class" );
s1.obstacle = false;
s1.dist = 2;
Expand All @@ -228,7 +228,6 @@ void TestQgsLabelingEngine::testRuleBased()
root->appendChild( new QgsRuleBasedLabeling::Rule( new QgsPalLayerSettings( s1 ) ) );

QgsPalLayerSettings s2;
s2.enabled = true;
s2.fieldName = QStringLiteral( "Class" );
s2.obstacle = false;
s2.dist = 2;
Expand Down Expand Up @@ -303,7 +302,6 @@ void TestQgsLabelingEngine::zOrder()
context.setPainter( &p );

QgsPalLayerSettings pls1;
pls1.enabled = true;
pls1.fieldName = QStringLiteral( "Class" );
pls1.placement = QgsPalLayerSettings::OverPoint;
pls1.quadOffset = QgsPalLayerSettings::QuadrantAboveRight;
Expand Down
57 changes: 29 additions & 28 deletions tests/src/python/test_qgsmaprenderer.py
Expand Up @@ -18,8 +18,11 @@
QgsMapRendererParallelJob,
QgsMapRendererSequentialJob,
QgsMapRendererCustomPainterJob,
QgsPalLayerSettings,
QgsRectangle,
QgsTextFormat,
QgsVectorLayer,
QgsVectorLayerSimpleLabeling,
QgsFeature,
QgsGeometry,
QgsMapSettings,
Expand Down Expand Up @@ -123,9 +126,9 @@ def checkRepaintLabeledLayerInvalidatesLabelCache(self, job_type):
layer = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")

layer.setCustomProperty("labeling", "pal")
layer.setCustomProperty("labeling/enabled", True)
layer.setCustomProperty("labeling/fieldName", "fldtxt")
labelSettings = QgsPalLayerSettings()
labelSettings.fieldName = "fldtxt"
layer.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))

settings = QgsMapSettings()
settings.setExtent(QgsRectangle(5, 25, 25, 45))
Expand Down Expand Up @@ -164,9 +167,9 @@ def checkAddingNewLabeledLayerInvalidatesLabelCache(self, job_type):
layer = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")

layer.setCustomProperty("labeling", "pal")
layer.setCustomProperty("labeling/enabled", True)
layer.setCustomProperty("labeling/fieldName", "fldtxt")
labelSettings = QgsPalLayerSettings()
labelSettings.fieldName = "fldtxt"
layer.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))

settings = QgsMapSettings()
settings.setExtent(QgsRectangle(5, 25, 25, 45))
Expand All @@ -188,9 +191,7 @@ def checkAddingNewLabeledLayerInvalidatesLabelCache(self, job_type):
# add another labeled layer
layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")
layer2.setCustomProperty("labeling", "pal")
layer2.setCustomProperty("labeling/enabled", True)
layer2.setCustomProperty("labeling/fieldName", "fldtxt")
layer2.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))
settings.setLayers([layer, layer2])

# second job should not be able to use label cache, since a new layer was added
Expand All @@ -210,9 +211,9 @@ def checkAddingNewNonLabeledLayerKeepsLabelCache(self, job_type):
layer = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")

layer.setCustomProperty("labeling", "pal")
layer.setCustomProperty("labeling/enabled", True)
layer.setCustomProperty("labeling/fieldName", "fldtxt")
labelSettings = QgsPalLayerSettings()
labelSettings.fieldName = "fldtxt"
layer.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))

settings = QgsMapSettings()
settings.setExtent(QgsRectangle(5, 25, 25, 45))
Expand Down Expand Up @@ -253,15 +254,13 @@ def checkRemovingLabeledLayerInvalidatesLabelCache(self, job_type):
layer = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")

layer.setCustomProperty("labeling", "pal")
layer.setCustomProperty("labeling/enabled", True)
layer.setCustomProperty("labeling/fieldName", "fldtxt")
labelSettings = QgsPalLayerSettings()
labelSettings.fieldName = "fldtxt"
layer.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))

layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")
layer2.setCustomProperty("labeling", "pal")
layer2.setCustomProperty("labeling/enabled", True)
layer2.setCustomProperty("labeling/fieldName", "fldtxt")
layer2.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))

settings = QgsMapSettings()
settings.setExtent(QgsRectangle(5, 25, 25, 45))
Expand Down Expand Up @@ -300,9 +299,9 @@ def checkRemovingNonLabeledLayerKeepsLabelCache(self, job_type):
layer = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")

layer.setCustomProperty("labeling", "pal")
layer.setCustomProperty("labeling/enabled", True)
layer.setCustomProperty("labeling/fieldName", "fldtxt")
labelSettings = QgsPalLayerSettings()
labelSettings.fieldName = "fldtxt"
layer.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))

layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")
Expand Down Expand Up @@ -344,16 +343,18 @@ def checkLabeledLayerWithBlendModesCannotBeCached(self, job_type):
layer = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")

layer.setCustomProperty("labeling", "pal")
layer.setCustomProperty("labeling/enabled", True)
layer.setCustomProperty("labeling/fieldName", "fldtxt")
labelSettings = QgsPalLayerSettings()
labelSettings.fieldName = "fldtxt"
layer.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings))

layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")
layer2.setCustomProperty("labeling", "pal")
layer2.setCustomProperty("labeling/enabled", True)
layer2.setCustomProperty("labeling/fieldName", "fldtxt")
layer2.setCustomProperty("labeling/blendMode", 5)
labelSettings2 = QgsPalLayerSettings()
labelSettings2.fieldName = "fldtxt"
format2 = QgsTextFormat()
format2.setBlendMode(QPainter.CompositionMode_SourceIn)
labelSettings2.setFormat(format2)
layer2.setLabeling(QgsVectorLayerSimpleLabeling(labelSettings2))

settings = QgsMapSettings()
settings.setExtent(QgsRectangle(5, 25, 25, 45))
Expand Down
1 change: 0 additions & 1 deletion tests/src/python/test_qgspallabeling_base.py
Expand Up @@ -251,7 +251,6 @@ def configTest(self, prefix, abbr):

def defaultLayerSettings(self):
lyr = QgsPalLayerSettings()
lyr.enabled = True
lyr.fieldName = 'text' # default in test data sources
font = self.getTestFont()
font.setPointSize(32)
Expand Down
6 changes: 3 additions & 3 deletions tests/src/python/test_qgspallabeling_placement.py
Expand Up @@ -66,7 +66,8 @@ def setUp(self):
self._MapSettings.setLabelingEngineSettings(engine_settings)

def checkTest(self, **kwargs):
self.layer.setLabeling(QgsVectorLayerSimpleLabeling(self.lyr))
if kwargs.get('apply_simple_labeling', True):
self.layer.setLabeling(QgsVectorLayerSimpleLabeling(self.lyr))

ms = self._MapSettings # class settings
settings_type = 'Class'
Expand Down Expand Up @@ -172,8 +173,7 @@ def test_polygon_multiple_labels(self):
# is INSIDE the polygon
self.layer = TestQgsPalLabeling.loadFeatureLayer('polygon_rule_based')
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
self.lyr.placement = QgsPalLayerSettings.Horizontal
self.checkTest()
self.checkTest(apply_simple_labeling=False)
self.removeMapLayer(self.layer)
self.layer = None

Expand Down

0 comments on commit f153e19

Please sign in to comment.