Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Convert remaining bits where labeling was configured using setCustomP…
…roperty()
  • Loading branch information
wonder-sk committed May 16, 2017
1 parent 21f03bc commit 828bd38
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 82 deletions.
16 changes: 16 additions & 0 deletions doc/api_break.dox
Expand Up @@ -319,6 +319,22 @@ General changes {#qgis_api_break_3_0_global}
- Network analysis library has been merged into analysis library


Labeling {#qgis_api_break_3_0_Labeling}
--------

Labeling of vector layers used to be configured in 2.x using setCustomProperties() method with various
lots of options prefixed with "labeling" keyword, for example: layer.setCustomProperties("labeling/fieldName", "my_field").
While 2.x projects and .qml files containing configuration using custom properties will still work,
calling these API methods will have no effect on layer's labeling configuration anymore: all settings
are now stored in objects derived from QgsAbstractVectorLayerLabeling:

- QgsVectorLayerSimpleLabeling - when all features are labeled the same way
- QgsRuleBasedLabeling - rule-based labeling

The current configuration of labeling can be queried with vector layer's labeling() method and modified with
setLabeling() method.


Data Providers {#qgis_api_break_3_0_DataProviders}
--------------

Expand Down
93 changes: 42 additions & 51 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -3466,6 +3466,8 @@ int QgsVectorLayer::layerTransparency() const

void QgsVectorLayer::readSldLabeling( const QDomNode &node )
{
setLabeling( nullptr ); // start with no labeling

QDomElement element = node.toElement();
if ( element.isNull() )
return;
Expand Down Expand Up @@ -3500,46 +3502,22 @@ void QgsVectorLayer::readSldLabeling( const QDomNode &node )
return;
}

QgsPalLayerSettings settings;

// Label
setCustomProperty( QStringLiteral( "labeling/enabled" ), false );
QDomElement labelElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Label" ) );
if ( !labelElem.isNull() )
{
QDomElement propertyNameElem = labelElem.firstChildElement( QStringLiteral( "PropertyName" ) );
if ( !propertyNameElem.isNull() )
{
// enable labeling
setCustomProperty( QStringLiteral( "labeling" ), "pal" );
setCustomProperty( QStringLiteral( "labeling/enabled" ), true );

// set labeling defaults
setCustomProperty( QStringLiteral( "labeling/fontFamily" ), "Sans-Serif" );
setCustomProperty( QStringLiteral( "labeling/fontItalic" ), false );
setCustomProperty( QStringLiteral( "labeling/fontSize" ), 10 );
setCustomProperty( QStringLiteral( "labeling/fontSizeInMapUnits" ), false );
setCustomProperty( QStringLiteral( "labeling/fontBold" ), false );
setCustomProperty( QStringLiteral( "labeling/fontUnderline" ), false );
setCustomProperty( QStringLiteral( "labeling/textColorR" ), 0 );
setCustomProperty( QStringLiteral( "labeling/textColorG" ), 0 );
setCustomProperty( QStringLiteral( "labeling/textColorB" ), 0 );
setCustomProperty( QStringLiteral( "labeling/textTransp" ), 0 );
setCustomProperty( QStringLiteral( "labeling/bufferDraw" ), false );
setCustomProperty( QStringLiteral( "labeling/bufferSize" ), 1 );
setCustomProperty( QStringLiteral( "labeling/bufferSizeInMapUnits" ), false );
setCustomProperty( QStringLiteral( "labeling/bufferColorR" ), 255 );
setCustomProperty( QStringLiteral( "labeling/bufferColorG" ), 255 );
setCustomProperty( QStringLiteral( "labeling/bufferColorB" ), 255 );
setCustomProperty( QStringLiteral( "labeling/bufferTransp" ), 0 );
setCustomProperty( QStringLiteral( "labeling/placement" ), QgsPalLayerSettings::AroundPoint );
setCustomProperty( QStringLiteral( "labeling/xOffset" ), 0 );
setCustomProperty( QStringLiteral( "labeling/yOffset" ), 0 );
setCustomProperty( QStringLiteral( "labeling/labelOffsetInMapUnits" ), false );
setCustomProperty( QStringLiteral( "labeling/angleOffset" ), 0 );
// enable labeling + set labeling defaults
settings.enabled = true;

// label attribute
QString labelAttribute = propertyNameElem.text();
setCustomProperty( QStringLiteral( "labeling/fieldName" ), labelAttribute );
setCustomProperty( QStringLiteral( "labeling/isExpression" ), false );
settings.fieldName = labelAttribute;
settings.isExpression = false;

int fieldIndex = mFields.lookupField( labelAttribute );
if ( fieldIndex == -1 )
Expand All @@ -3548,7 +3526,7 @@ void QgsVectorLayer::readSldLabeling( const QDomNode &node )
QgsExpression exp( labelAttribute );
if ( !exp.hasEvalError() )
{
setCustomProperty( QStringLiteral( "labeling/isExpression" ), true );
settings.isExpression = true;
}
else
{
Expand All @@ -3568,6 +3546,12 @@ void QgsVectorLayer::readSldLabeling( const QDomNode &node )
return;
}

QString fontFamily = "Sans-Serif";
int fontPointSize = 10;
int fontWeight = -1;
bool fontItalic = false;
bool fontUnderline = false;

// Font
QDomElement fontElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Font" ) );
if ( !fontElem.isNull() )
Expand All @@ -3583,51 +3567,57 @@ void QgsVectorLayer::readSldLabeling( const QDomNode &node )
elemText = cssElem.text();
if ( cssName == QLatin1String( "font-family" ) )
{
setCustomProperty( QStringLiteral( "labeling/fontFamily" ), elemText );
fontFamily = elemText;
}
else if ( cssName == QLatin1String( "font-style" ) )
{
setCustomProperty( QStringLiteral( "labeling/fontItalic" ), ( elemText == QLatin1String( "italic" ) ) || ( elemText == QLatin1String( "Italic" ) ) );
fontItalic = ( elemText == QLatin1String( "italic" ) ) || ( elemText == QLatin1String( "Italic" ) );
}
else if ( cssName == QLatin1String( "font-size" ) )
{
bool ok;
int fontSize = elemText.toInt( &ok );
if ( ok )
{
setCustomProperty( QStringLiteral( "labeling/fontSize" ), fontSize );
fontPointSize = fontSize;
}
}
else if ( cssName == QLatin1String( "font-weight" ) )
{
setCustomProperty( QStringLiteral( "labeling/fontBold" ), ( elemText == QLatin1String( "bold" ) ) || ( elemText == QLatin1String( "Bold" ) ) );
if ( ( elemText == QLatin1String( "bold" ) ) || ( elemText == QLatin1String( "Bold" ) ) )
fontWeight = QFont::Bold;
}
else if ( cssName == QLatin1String( "font-underline" ) )
{
setCustomProperty( QStringLiteral( "labeling/fontUnderline" ), ( elemText == QLatin1String( "underline" ) ) || ( elemText == QLatin1String( "Underline" ) ) );
fontUnderline = ( elemText == QLatin1String( "underline" ) ) || ( elemText == QLatin1String( "Underline" ) );
}
}

cssElem = cssElem.nextSiblingElement( QStringLiteral( "CssParameter" ) );
}
}

QgsTextFormat format;
QFont font( fontFamily, fontPointSize, fontWeight, fontItalic );
font.setUnderline( fontUnderline );
format.setFont( font );
format.setSize( fontPointSize );

// Fill
QColor textColor = QgsOgcUtils::colorFromOgcFill( textSymbolizerElem.firstChildElement( QStringLiteral( "Fill" ) ) );
if ( textColor.isValid() )
{
setCustomProperty( QStringLiteral( "labeling/textColorR" ), textColor.red() );
setCustomProperty( QStringLiteral( "labeling/textColorG" ), textColor.green() );
setCustomProperty( QStringLiteral( "labeling/textColorB" ), textColor.blue() );
setCustomProperty( QStringLiteral( "labeling/textTransp" ), 100 - static_cast< int >( 100 * textColor.alphaF() ) );
format.setColor( textColor );
}

QgsTextBufferSettings bufferSettings;

// Halo
QDomElement haloElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Halo" ) );
if ( !haloElem.isNull() )
{
setCustomProperty( QStringLiteral( "labeling/bufferDraw" ), true );
setCustomProperty( QStringLiteral( "labeling/bufferSize" ), 1 );
bufferSettings.setEnabled( true );
bufferSettings.setSize( 1 );

QDomElement radiusElem = haloElem.firstChildElement( QStringLiteral( "Radius" ) );
if ( !radiusElem.isNull() )
Expand All @@ -3636,17 +3626,14 @@ void QgsVectorLayer::readSldLabeling( const QDomNode &node )
double bufferSize = radiusElem.text().toDouble( &ok );
if ( ok )
{
setCustomProperty( QStringLiteral( "labeling/bufferSize" ), bufferSize );
bufferSettings.setSize( bufferSize );
}
}

QColor bufferColor = QgsOgcUtils::colorFromOgcFill( haloElem.firstChildElement( QStringLiteral( "Fill" ) ) );
if ( bufferColor.isValid() )
{
setCustomProperty( QStringLiteral( "labeling/bufferColorR" ), bufferColor.red() );
setCustomProperty( QStringLiteral( "labeling/bufferColorG" ), bufferColor.green() );
setCustomProperty( QStringLiteral( "labeling/bufferColorB" ), bufferColor.blue() );
setCustomProperty( QStringLiteral( "labeling/bufferTransp" ), 100 - static_cast< int >( 100 * bufferColor.alphaF() ) );
bufferSettings.setColor( bufferColor );
}
}

Expand All @@ -3658,7 +3645,7 @@ void QgsVectorLayer::readSldLabeling( const QDomNode &node )
QDomElement pointPlacementElem = labelPlacementElem.firstChildElement( QStringLiteral( "PointPlacement" ) );
if ( !pointPlacementElem.isNull() )
{
setCustomProperty( QStringLiteral( "labeling/placement" ), QgsPalLayerSettings::OverPoint );
settings.placement = QgsPalLayerSettings::OverPoint;

QDomElement displacementElem = pointPlacementElem.firstChildElement( QStringLiteral( "Displacement" ) );
if ( !displacementElem.isNull() )
Expand All @@ -3670,7 +3657,7 @@ void QgsVectorLayer::readSldLabeling( const QDomNode &node )
double xOffset = displacementXElem.text().toDouble( &ok );
if ( ok )
{
setCustomProperty( QStringLiteral( "labeling/xOffset" ), xOffset );
settings.xOffset = xOffset;
}
}
QDomElement displacementYElem = displacementElem.firstChildElement( QStringLiteral( "DisplacementY" ) );
Expand All @@ -3680,7 +3667,7 @@ void QgsVectorLayer::readSldLabeling( const QDomNode &node )
double yOffset = displacementYElem.text().toDouble( &ok );
if ( ok )
{
setCustomProperty( QStringLiteral( "labeling/yOffset" ), yOffset );
settings.yOffset = yOffset;
}
}
}
Expand All @@ -3692,11 +3679,15 @@ void QgsVectorLayer::readSldLabeling( const QDomNode &node )
double rotation = rotationElem.text().toDouble( &ok );
if ( ok )
{
setCustomProperty( QStringLiteral( "labeling/angleOffset" ), rotation );
settings.angleOffset = rotation;
}
}
}
}

format.setBuffer( bufferSettings );
settings.setFormat( format );
setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
}

QgsEditFormConfig QgsVectorLayer::editFormConfig() const
Expand Down
71 changes: 42 additions & 29 deletions src/server/qgswmsconfigparser.cpp
Expand Up @@ -19,6 +19,7 @@

#include "qgswmsconfigparser.h"
#include "qgsmaplayer.h"
#include "qgspallabeling.h"
#include "qgsproject.h"
#include "qgsmapserviceexception.h"

Expand All @@ -38,6 +39,7 @@
#include "qgsrenderer.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgsvectorlayerlabeling.h"


QgsWmsConfigParser::QgsWmsConfigParser()
Expand Down Expand Up @@ -386,6 +388,23 @@ QgsVectorLayer *QgsWmsConfigParser::createHighlightLayer( int i, const QString &
return 0;
}

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

//give highest priority to highlight layers and make sure the labels are always drawn
settings.priority = 10;
settings.displayAll = true;

QgsTextFormat textFormat;
QgsTextBufferSettings bufferSettings;

QString fontFamily;
int fontSize = 12;
int fontWeight = -1;

QgsPropertyCollection &ddp = settings.dataDefinedProperties();

QgsFeature fet( layer->pendingFields() );
if ( !labelString.isEmpty() )
{
Expand All @@ -403,76 +422,70 @@ QgsVectorLayer *QgsWmsConfigParser::createHighlightLayer( int i, const QString &
}
}

layer->setCustomProperty( QStringLiteral( "labeling/fieldName" ), "label" );
layer->setCustomProperty( QStringLiteral( "labeling/enabled" ), "true" );
layer->setCustomProperty( QStringLiteral( "labeling" ), "pal" );
//give highest priority to highlight layers and make sure the labels are always drawn
layer->setCustomProperty( QStringLiteral( "labeling/priority" ), "10" );
layer->setCustomProperty( QStringLiteral( "labeling/displayAll" ), "true" );

//fontsize?
if ( i < labelSizeSplit.size() )
{
layer->setCustomProperty( QStringLiteral( "labeling/fontSize" ), labelSizeSplit.at( i ) );
fontSize = labelSizeSplit.at( i ).toInt();
}
//font color
if ( i < labelColorSplit.size() )
{
QColor c( labelColorSplit.at( i ) );
layer->setCustomProperty( QStringLiteral( "labeling/textColorR" ), c.red() );
layer->setCustomProperty( QStringLiteral( "labeling/textColorG" ), c.green() );
layer->setCustomProperty( QStringLiteral( "labeling/textColorB" ), c.blue() );
textFormat.setColor( c );
}
//font weight
if ( i < labelWeightSplit.size() )
{
layer->setCustomProperty( QStringLiteral( "labeling/fontWeight" ), labelWeightSplit.at( i ) );
fontWeight = labelWeightSplit.at( i ).toInt();
}

//font family list
if ( i < labelFontSplit.size() )
{
layer->setCustomProperty( QStringLiteral( "labeling/fontFamily" ), labelFontSplit.at( i ) );
fontFamily = labelFontSplit.at( i );
}

//buffer
if ( i < labelBufferSizeSplit.size() )
{
layer->setCustomProperty( QStringLiteral( "labeling/bufferSize" ), labelBufferSizeSplit.at( i ) );
bufferSettings.setSize( labelBufferSizeSplit.at( i ).toInt() );
}

//buffer color
if ( i < labelBufferColorSplit.size() )
{
QColor c( labelBufferColorSplit.at( i ) );
layer->setCustomProperty( QStringLiteral( "labeling/bufferColorR" ), c.red() );
layer->setCustomProperty( QStringLiteral( "labeling/bufferColorG" ), c.green() );
layer->setCustomProperty( QStringLiteral( "labeling/bufferColorB" ), c.blue() );
bufferSettings.setColor( c );
}

//placement
int placement = 0;
switch ( geomType )
{
case QgsWkbTypes::PointGeometry:
placement = 0;
layer->setCustomProperty( QStringLiteral( "labeling/dist" ), 2 );
layer->setCustomProperty( QStringLiteral( "labeling/placementFlags" ), 0 );
settings.placement = QgsPalLayerSettings::AroundPoint;
settings.dist = 2;
settings.placementFlags = 0;
break;
case QgsWkbTypes::PolygonGeometry:
layer->setCustomProperty( QStringLiteral( "labeling/dataDefinedProperty9" ), 1 );
layer->setCustomProperty( QStringLiteral( "labeling/dataDefinedProperty10" ), 2 );
layer->setCustomProperty( QStringLiteral( "labeling/dataDefinedProperty11" ), 3 );
layer->setCustomProperty( QStringLiteral( "labeling/dataDefinedProperty12" ), 4 );
settings.placement = QgsPalLayerSettings::AroundPoint;
ddp.setProperty( QgsPalLayerSettings::PositionX, QgsProperty::fromField( "x" ) );
ddp.setProperty( QgsPalLayerSettings::PositionY, QgsProperty::fromField( "y" ) );
ddp.setProperty( QgsPalLayerSettings::Hali, QgsProperty::fromField( "hali" ) );
ddp.setProperty( QgsPalLayerSettings::Vali, QgsProperty::fromValue( "vali" ) );
break;
default:
placement = 2; //parallel placement for line
layer->setCustomProperty( QStringLiteral( "labeling/dist" ), 2 );
layer->setCustomProperty( QStringLiteral( "labeling/placementFlags" ), 10 );
settings.placement = QgsPalLayerSettings::Line; //parallel placement for line
settings.dist = 2;
settings.placementFlags = 10;
}
layer->setCustomProperty( QStringLiteral( "labeling/placement" ), placement );
}

textFormat.setFont( QFont( fontFamily, fontSize, fontWeight ) );
textFormat.setSize( fontSize );
textFormat.setBuffer( bufferSettings );
settings.setFormat( textFormat );
layer->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );

fet.setGeometry( geom );
layer->dataProvider()->addFeatures( QgsFeatureList() << fet );
return layer;
Expand Down
3 changes: 2 additions & 1 deletion src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -2184,8 +2184,9 @@ namespace QgsWms
}

//labeling
if ( vl->customProperty( QStringLiteral( "labeling/enabled" ) ).toString() == QLatin1String( "true" ) )
if ( vl->labeling() )
{
// TODO: this need a complete re-work: there may be simple or rule-based labeling. we need to temporarily replace labeling instance.
double labelTransparency = vl->customProperty( QStringLiteral( "labeling/textTransp" ) ).toDouble();
labelTransparencies.push_back( qMakePair( vl, labelTransparency ) );
vl->setCustomProperty( QStringLiteral( "labeling/textTransp" ), labelTransparency + ( 100 - labelTransparency ) * ( 1.0 - opacityRatio ) );
Expand Down

0 comments on commit 828bd38

Please sign in to comment.