Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 54089ff
Show file tree
Hide file tree
Showing 23 changed files with 128 additions and 59 deletions.
6 changes: 5 additions & 1 deletion python/core/composer/qgscomposerobject.sip
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion python/core/qgsdiagramrenderer.sip
Expand Up @@ -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
Expand Down Expand Up @@ -209,7 +215,6 @@ class QgsDiagramLayerSettings

void setDataDefinedProperties( const QgsPropertyCollection& collection );

static const QgsPropertiesDefinition PROPERTY_DEFINITIONS;

};

Expand Down
8 changes: 6 additions & 2 deletions python/core/qgspallabeling.sip
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion python/core/symbology-ng/qgssymbollayer.sip
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/app/composer/qgscomposeritemwidget.cpp
Expand Up @@ -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 );
Expand Down
10 changes: 5 additions & 5 deletions src/app/composer/qgscompositionwidget.cpp
Expand Up @@ -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() );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsdiagramproperties.cpp
Expand Up @@ -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 );
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgslabelinggui.cpp
Expand Up @@ -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 );
}
Expand Down
16 changes: 11 additions & 5 deletions src/core/composer/qgscomposerobject.cpp
Expand Up @@ -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>"
Expand Down Expand Up @@ -76,6 +76,12 @@ void QgsComposerObject::initPropertyDefinitions()
};
}

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

QgsComposerObject::QgsComposerObject( QgsComposition* composition )
: QObject( nullptr )
, mComposition( composition )
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 9 additions & 3 deletions src/core/composer/qgscomposerobject.h
Expand Up @@ -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.
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposerutils.cpp
Expand Up @@ -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 );
Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposition.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions src/core/qgsdiagramrenderer.cpp
Expand Up @@ -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 ) },
Expand All @@ -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 )
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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 );
Expand Down
13 changes: 10 additions & 3 deletions src/core/qgsdiagramrenderer.h
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -324,6 +327,10 @@ class CORE_EXPORT QgsDiagramLayerSettings
QgsPropertyCollection mDataDefinedProperties;

static void initPropertyDefinitions();

//! Property definitions
static QgsPropertiesDefinition sPropertyDefinitions;

};

/** \ingroup core
Expand Down
25 changes: 15 additions & 10 deletions src/core/qgspallabeling.cpp
Expand Up @@ -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 ) },
Expand Down Expand Up @@ -392,6 +392,11 @@ QgsPalLayerSettings QgsPalLayerSettings::fromLayer( QgsVectorLayer* layer )
return settings;
}

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

QgsExpression* QgsPalLayerSettings::getLabelExpression()
{
Expand Down Expand Up @@ -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() )
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit 54089ff

Please sign in to comment.