Skip to content
Permalink
Browse files

Follow up 4a3871, fix broken data defined properties

(definitions are often accessed before a QgsSymboLayer/etc
is constructed)
  • Loading branch information
nyalldawson committed Feb 5, 2017
1 parent fa8189a commit 54089ff710c9f18b0de42f2188e1bc25b8fd23d6
@@ -60,7 +60,11 @@ class QgsComposerObject : QObject, QgsExpressionContextGenerator
ScalebarLineWidth, //!< Scalebar line width
};

static const QgsPropertiesDefinition PROPERTY_DEFINITIONS;
/**
* Returns the composer object property definitions.
* @note added in QGIS 3.0
*/
static const QgsPropertiesDefinition& propertyDefinitions();

/** Specifies whether the value returned by a function should be the original, user
* set value, or the current evaluated value for the property. This may differ if
@@ -53,6 +53,12 @@ class QgsDiagramLayerSettings
StartAngle, /*! Angle offset for pie diagram */
};

/**
* Returns the diagram property definitions.
* @note added in QGIS 3.0
*/
static const QgsPropertiesDefinition& propertyDefinitions();

QgsDiagramLayerSettings();

//! Copy constructor
@@ -209,7 +215,6 @@ class QgsDiagramLayerSettings

void setDataDefinedProperties( const QgsPropertyCollection& collection );

static const QgsPropertiesDefinition PROPERTY_DEFINITIONS;

};

@@ -270,6 +270,12 @@ class QgsPalLayerSettings
AlwaysShow
};

/**
* Returns the labeling property definitions.
* @note added in QGIS 3.0
*/
static const QgsPropertiesDefinition& propertyDefinitions();

// whether to label this layer
bool enabled;

@@ -460,8 +466,6 @@ class QgsPalLayerSettings
int mFeatsSendingToPal; // total features tested for sending into PAL (relative to maxNumLabels)
int mFeatsRegPal; // number of features registered in PAL, when using limitNumLabels

static const QgsPropertiesDefinition PROPERTY_DEFINITIONS;

};

class QgsLabelCandidate
@@ -122,7 +122,11 @@ class QgsSymbolLayer
PropertyArrowType, //!< Arrow type
};

static const QgsPropertiesDefinition PROPERTY_DEFINITIONS;
/**
* Returns the symbol layer property definitions.
* @note added in QGIS 3.0
*/
static const QgsPropertiesDefinition& propertyDefinitions();

virtual ~QgsSymbolLayer();

@@ -77,7 +77,7 @@ void QgsComposerConfigObject::updateDataDefinedButtons()
void QgsComposerConfigObject::initializeDataDefinedButton( QgsPropertyOverrideButton* button, QgsComposerObject::DataDefinedProperty key )
{
button->blockSignals( true );
button->init( key, mComposerObject->dataDefinedProperties(), QgsComposerObject::PROPERTY_DEFINITIONS, atlasCoverageLayer() );
button->init( key, mComposerObject->dataDefinedProperties(), QgsComposerObject::propertyDefinitions(), atlasCoverageLayer() );
connect( button, &QgsPropertyOverrideButton::changed, this, &QgsComposerConfigObject::updateDataDefinedProperty );
button->registerExpressionContextGenerator( mComposerObject );
button->blockSignals( false );
@@ -156,11 +156,11 @@ void QgsCompositionWidget::populateDataDefinedButtons()
connect( button, &QgsPropertyOverrideButton::changed, this, &QgsCompositionWidget::updateDataDefinedProperty );
}

mPaperSizeDDBtn->init( QgsComposerObject::PresetPaperSize, mComposition->dataDefinedProperties(), QgsComposerObject::PROPERTY_DEFINITIONS, vl );
mPaperWidthDDBtn->init( QgsComposerObject::PaperWidth, mComposition->dataDefinedProperties(), QgsComposerObject::PROPERTY_DEFINITIONS, vl );
mPaperHeightDDBtn->init( QgsComposerObject::PaperHeight, mComposition->dataDefinedProperties(), QgsComposerObject::PROPERTY_DEFINITIONS, vl );
mNumPagesDDBtn->init( QgsComposerObject::NumPages, mComposition->dataDefinedProperties(), QgsComposerObject::PROPERTY_DEFINITIONS, vl );
mPaperOrientationDDBtn->init( QgsComposerObject::PaperOrientation, mComposition->dataDefinedProperties(), QgsComposerObject::PROPERTY_DEFINITIONS, vl );
mPaperSizeDDBtn->init( QgsComposerObject::PresetPaperSize, mComposition->dataDefinedProperties(), QgsComposerObject::propertyDefinitions(), vl );
mPaperWidthDDBtn->init( QgsComposerObject::PaperWidth, mComposition->dataDefinedProperties(), QgsComposerObject::propertyDefinitions(), vl );
mPaperHeightDDBtn->init( QgsComposerObject::PaperHeight, mComposition->dataDefinedProperties(), QgsComposerObject::propertyDefinitions(), vl );
mNumPagesDDBtn->init( QgsComposerObject::NumPages, mComposition->dataDefinedProperties(), QgsComposerObject::propertyDefinitions(), vl );
mPaperOrientationDDBtn->init( QgsComposerObject::PaperOrientation, mComposition->dataDefinedProperties(), QgsComposerObject::propertyDefinitions(), vl );

//initial state of controls - disable related controls when dd buttons are active
mPaperSizeComboBox->setEnabled( !mPaperSizeDDBtn->isActive() );
@@ -429,7 +429,7 @@ QgsDiagramProperties::~QgsDiagramProperties()

void QgsDiagramProperties::registerDataDefinedButton( QgsPropertyOverrideButton * button, QgsDiagramLayerSettings::Property key )
{
button->init( key, mDataDefinedProperties, QgsDiagramLayerSettings::PROPERTY_DEFINITIONS, mLayer );
button->init( key, mDataDefinedProperties, QgsDiagramLayerSettings::propertyDefinitions(), mLayer );
connect( button, &QgsPropertyOverrideButton::changed, this, &QgsDiagramProperties::updateProperty );
button->registerExpressionContextGenerator( this );
}
@@ -44,7 +44,7 @@ QgsExpressionContext QgsLabelingGui::createExpressionContext() const

void QgsLabelingGui::registerDataDefinedButton( QgsPropertyOverrideButton* button, QgsPalLayerSettings::Property key )
{
button->init( key, mDataDefinedProperties, QgsPalLayerSettings::PROPERTY_DEFINITIONS, mLayer );
button->init( key, mDataDefinedProperties, QgsPalLayerSettings::propertyDefinitions(), mLayer );
connect( button, &QgsPropertyOverrideButton::changed, this, &QgsLabelingGui::updateProperty );
button->registerExpressionContextGenerator( this );
}
@@ -23,14 +23,14 @@
#include "qgsproject.h"
#include "qgsvectorlayer.h"

QgsPropertiesDefinition QgsComposerObject::PROPERTY_DEFINITIONS;
QgsPropertiesDefinition QgsComposerObject::sPropertyDefinitions;

void QgsComposerObject::initPropertyDefinitions()
{
if ( !PROPERTY_DEFINITIONS.isEmpty() )
if ( !sPropertyDefinitions.isEmpty() )
return;

PROPERTY_DEFINITIONS = QgsPropertiesDefinition
sPropertyDefinitions = QgsPropertiesDefinition
{
{ QgsComposerObject::TestProperty, QgsPropertyDefinition( "dataDefinedProperty" , QgsPropertyDefinition::DataTypeString, "invalid property", QString() ) },
{ QgsComposerObject::PresetPaperSize, QgsPropertyDefinition( "dataDefinedPaperSize" , QgsPropertyDefinition::DataTypeString, QObject::tr( "Paper size" ), QObject::tr( "string " ) + QLatin1String( "[<b>A5</b>|<b>A4</b>|<b>A3</b>|<b>A2</b>|<b>A1</b>|<b>A0</b>"
@@ -76,6 +76,12 @@ void QgsComposerObject::initPropertyDefinitions()
};
}

const QgsPropertiesDefinition& QgsComposerObject::propertyDefinitions()
{
QgsComposerObject::initPropertyDefinitions();
return sPropertyDefinitions;
}

QgsComposerObject::QgsComposerObject( QgsComposition* composition )
: QObject( nullptr )
, mComposition( composition )
@@ -109,7 +115,7 @@ bool QgsComposerObject::writeXml( QDomElement &elem, QDomDocument &doc ) const
}

QDomElement ddPropsElement = doc.createElement( QStringLiteral( "dataDefinedProperties" ) );
mDataDefinedProperties.writeXml( ddPropsElement, doc, PROPERTY_DEFINITIONS );
mDataDefinedProperties.writeXml( ddPropsElement, doc, sPropertyDefinitions );
elem.appendChild( ddPropsElement );

//custom properties
@@ -132,7 +138,7 @@ bool QgsComposerObject::readXml( const QDomElement &itemElem, const QDomDocument
QDomNode propsNode = itemElem.namedItem( QStringLiteral( "dataDefinedProperties" ) );
if ( !propsNode.isNull() )
{
mDataDefinedProperties.readXml( propsNode.toElement(), doc, PROPERTY_DEFINITIONS );
mDataDefinedProperties.readXml( propsNode.toElement(), doc, sPropertyDefinitions );
}

//custom properties
@@ -88,9 +88,6 @@ class CORE_EXPORT QgsComposerObject: public QObject, public QgsExpressionContext
ScalebarLineWidth, //!< Scalebar line width
};

//! Property definitions
static QgsPropertiesDefinition PROPERTY_DEFINITIONS;

/** Specifies whether the value returned by a function should be the original, user
* set value, or the current evaluated value for the property. This may differ if
* a property has a data defined expression active.
@@ -101,6 +98,12 @@ class CORE_EXPORT QgsComposerObject: public QObject, public QgsExpressionContext
OriginalValue //!< Return the original, user set value
};

/**
* Returns the composer object property definitions.
* @note added in QGIS 3.0
*/
static const QgsPropertiesDefinition& propertyDefinitions();

/** Constructor
* @param composition parent composition
*/
@@ -229,6 +232,9 @@ class CORE_EXPORT QgsComposerObject: public QObject, public QgsExpressionContext
void prepareProperties() const;

private:
//! Property definitions
static QgsPropertiesDefinition sPropertyDefinitions;

static void initPropertyDefinitions();

friend class TestQgsComposerObject;
@@ -322,8 +322,8 @@ bool QgsComposerUtils::decodePresetPaperSize( const QString& presetString, doubl

void QgsComposerUtils::readOldDataDefinedPropertyMap( const QDomElement &itemElem, QgsPropertyCollection& dataDefinedProperties )
{
QgsPropertiesDefinition::const_iterator i = QgsComposerObject::PROPERTY_DEFINITIONS.constBegin();
for ( ; i != QgsComposerObject::PROPERTY_DEFINITIONS.constEnd(); ++i )
QgsPropertiesDefinition::const_iterator i = QgsComposerObject::propertyDefinitions().constBegin();
for ( ; i != QgsComposerObject::propertyDefinitions().constEnd(); ++i )
{
QString elemName = i.value().name();
QDomNodeList ddNodeList = itemElem.elementsByTagName( elemName );
@@ -902,7 +902,7 @@ bool QgsComposition::writeXml( QDomElement& composerElem, QDomDocument& doc )

//data defined properties
QDomElement ddPropsElement = doc.createElement( QStringLiteral( "dataDefinedProperties" ) );
mDataDefinedProperties.writeXml( ddPropsElement, doc, QgsComposerObject::PROPERTY_DEFINITIONS );
mDataDefinedProperties.writeXml( ddPropsElement, doc, QgsComposerObject::propertyDefinitions() );
compositionElem.appendChild( ddPropsElement );

composerElem.appendChild( compositionElem );
@@ -989,7 +989,7 @@ bool QgsComposition::readXml( const QDomElement& compositionElem, const QDomDocu
QDomNode propsNode = compositionElem.namedItem( QStringLiteral( "dataDefinedProperties" ) );
if ( !propsNode.isNull() )
{
mDataDefinedProperties.readXml( propsNode.toElement(), doc, QgsComposerObject::PROPERTY_DEFINITIONS );
mDataDefinedProperties.readXml( propsNode.toElement(), doc, QgsComposerObject::propertyDefinitions() );
}

//custom properties
@@ -25,14 +25,14 @@
#include <QDomElement>
#include <QPainter>

QgsPropertiesDefinition QgsDiagramLayerSettings::PROPERTY_DEFINITIONS;
QgsPropertiesDefinition QgsDiagramLayerSettings::sPropertyDefinitions;

void QgsDiagramLayerSettings::initPropertyDefinitions()
{
if ( !PROPERTY_DEFINITIONS.isEmpty() )
if ( !sPropertyDefinitions.isEmpty() )
return;

PROPERTY_DEFINITIONS = QgsPropertiesDefinition
sPropertyDefinitions = QgsPropertiesDefinition
{
{ QgsDiagramLayerSettings::BackgroundColor, QgsPropertyDefinition( "backgroundColor", QObject::tr( "Background color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsDiagramLayerSettings::OutlineColor, QgsPropertyDefinition( "outlineColor", QObject::tr( "Outline color" ), QgsPropertyDefinition::ColorWithAlpha ) },
@@ -49,6 +49,12 @@ void QgsDiagramLayerSettings::initPropertyDefinitions()
};
}

const QgsPropertiesDefinition& QgsDiagramLayerSettings::propertyDefinitions()
{
initPropertyDefinitions();
return sPropertyDefinitions;
}

QgsDiagramLayerSettings::QgsDiagramLayerSettings( const QgsDiagramLayerSettings& rh )
: mCt( rh.mCt )
, mPlacement( rh.mPlacement )
@@ -105,7 +111,7 @@ void QgsDiagramLayerSettings::readXml( const QDomElement& elem, const QgsVectorL
QDomNodeList propertyElems = elem.elementsByTagName( "properties" );
if ( !propertyElems.isEmpty() )
{
( void )mDataDefinedProperties.readXml( propertyElems.at( 0 ).toElement(), elem.ownerDocument(), PROPERTY_DEFINITIONS );
( void )mDataDefinedProperties.readXml( propertyElems.at( 0 ).toElement(), elem.ownerDocument(), sPropertyDefinitions );
}
else
{
@@ -148,7 +154,7 @@ void QgsDiagramLayerSettings::writeXml( QDomElement& layerElem, QDomDocument& do

QDomElement diagramLayerElem = doc.createElement( QStringLiteral( "DiagramLayerSettings" ) );
QDomElement propertiesElem = doc.createElement( "properties" );
( void )mDataDefinedProperties.writeXml( propertiesElem, doc, PROPERTY_DEFINITIONS );
( void )mDataDefinedProperties.writeXml( propertiesElem, doc, sPropertyDefinitions );
diagramLayerElem.appendChild( propertiesElem );
diagramLayerElem.setAttribute( QStringLiteral( "placement" ), mPlacement );
diagramLayerElem.setAttribute( QStringLiteral( "linePlacementFlags" ), mPlacementFlags );
@@ -95,6 +95,12 @@ class CORE_EXPORT QgsDiagramLayerSettings
StartAngle, //! Angle offset for pie diagram
};

/**
* Returns the diagram property definitions.
* @note added in QGIS 3.0
*/
static const QgsPropertiesDefinition& propertyDefinitions();

/**
* Constructor for QgsDiagramLayerSettings.
*/
@@ -286,9 +292,6 @@ class CORE_EXPORT QgsDiagramLayerSettings
*/
void setDataDefinedProperties( const QgsPropertyCollection& collection ) { mDataDefinedProperties = collection; }

//! Property definitions
static QgsPropertiesDefinition PROPERTY_DEFINITIONS;

private:

//! Associated coordinate transform, or invalid transform for no transformation
@@ -324,6 +327,10 @@ class CORE_EXPORT QgsDiagramLayerSettings
QgsPropertyCollection mDataDefinedProperties;

static void initPropertyDefinitions();

//! Property definitions
static QgsPropertiesDefinition sPropertyDefinitions;

};

/** \ingroup core
@@ -90,14 +90,14 @@ const QVector< QgsPalLayerSettings::PredefinedPointPosition > QgsPalLayerSetting
<< QgsPalLayerSettings::TopMiddle
<< QgsPalLayerSettings::BottomMiddle;*/

QgsPropertiesDefinition QgsPalLayerSettings::PROPERTY_DEFINITIONS;
QgsPropertiesDefinition QgsPalLayerSettings::sPropertyDefinitions;

void QgsPalLayerSettings::initPropertyDefinitions()
{
if ( !PROPERTY_DEFINITIONS.isEmpty() )
if ( !sPropertyDefinitions.isEmpty() )
return;

PROPERTY_DEFINITIONS = QgsPropertiesDefinition
sPropertyDefinitions = QgsPropertiesDefinition
{
{ QgsPalLayerSettings::Size, QgsPropertyDefinition( "Size" , QObject::tr( "Font size" ), QgsPropertyDefinition::DoublePositive ) },
{ QgsPalLayerSettings::Bold, QgsPropertyDefinition( "Bold", QObject::tr( "Bold style" ), QgsPropertyDefinition::Boolean ) },
@@ -392,6 +392,11 @@ QgsPalLayerSettings QgsPalLayerSettings::fromLayer( QgsVectorLayer* layer )
return settings;
}

const QgsPropertiesDefinition& QgsPalLayerSettings::propertyDefinitions()
{
initPropertyDefinitions();
return sPropertyDefinitions;
}

QgsExpression* QgsPalLayerSettings::getLabelExpression()
{
@@ -438,7 +443,7 @@ QString updateDataDefinedString( const QString& value )

void QgsPalLayerSettings::readOldDataDefinedProperty( QgsVectorLayer* layer, QgsPalLayerSettings::Property p )
{
QString newPropertyName = "labeling/dataDefined/" + PROPERTY_DEFINITIONS.value( p ).name();
QString newPropertyName = "labeling/dataDefined/" + sPropertyDefinitions.value( p ).name();
QVariant newPropertyField = layer->customProperty( newPropertyName, QVariant() );

if ( !newPropertyField.isValid() )
@@ -476,8 +481,8 @@ void QgsPalLayerSettings::readOldDataDefinedPropertyMap( QgsVectorLayer* layer,
return;
}

QgsPropertiesDefinition::const_iterator i = PROPERTY_DEFINITIONS.constBegin();
for ( ; i != PROPERTY_DEFINITIONS.constEnd(); ++i )
QgsPropertiesDefinition::const_iterator i = sPropertyDefinitions.constBegin();
for ( ; i != sPropertyDefinitions.constEnd(); ++i )
{
if ( layer )
{
@@ -650,7 +655,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
QDomDocument doc( QStringLiteral( "dd" ) );
doc.setContent( layer->customProperty( QStringLiteral( "labeling/ddProperties" ) ).toString() );
QDomElement elem = doc.firstChildElement( QStringLiteral( "properties" ) );
mDataDefinedProperties.readXml( elem, doc, PROPERTY_DEFINITIONS );
mDataDefinedProperties.readXml( elem, doc, sPropertyDefinitions );
}
else
{
@@ -741,7 +746,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )

doc = QDomDocument( QStringLiteral( "dd" ) );
QDomElement ddElem = doc.createElement( QStringLiteral( "properties" ) );
mDataDefinedProperties.writeXml( ddElem, doc, PROPERTY_DEFINITIONS );
mDataDefinedProperties.writeXml( ddElem, doc, sPropertyDefinitions );
QString ddProps;
QTextStream streamProps( &ddProps );
ddElem.save( streamProps, -1 );
@@ -857,7 +862,7 @@ void QgsPalLayerSettings::readXml( QDomElement& elem )
QDomElement ddElem = elem.firstChildElement( QStringLiteral( "dd_properties" ) );
if ( !ddElem.isNull() )
{
mDataDefinedProperties.readXml( ddElem, ddElem.ownerDocument(), PROPERTY_DEFINITIONS );
mDataDefinedProperties.readXml( ddElem, ddElem.ownerDocument(), sPropertyDefinitions );
}
else
{
@@ -946,7 +951,7 @@ QDomElement QgsPalLayerSettings::writeXml( QDomDocument& doc )
renderingElem.setAttribute( QStringLiteral( "zIndex" ), zIndex );

QDomElement ddElem = doc.createElement( QStringLiteral( "dd_properties" ) );
mDataDefinedProperties.writeXml( ddElem, doc, PROPERTY_DEFINITIONS );
mDataDefinedProperties.writeXml( ddElem, doc, sPropertyDefinitions );

QDomElement elem = doc.createElement( QStringLiteral( "settings" ) );
elem.appendChild( textStyleElem );

0 comments on commit 54089ff

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