Skip to content

Commit

Permalink
Port some basic functionality from QgsComposerObject
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 18, 2017
1 parent a6156d9 commit 15b65fa
Show file tree
Hide file tree
Showing 4 changed files with 358 additions and 0 deletions.
115 changes: 115 additions & 0 deletions python/core/layout/qgslayoutobject.sip
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,63 @@ class QgsLayoutObject: QObject
%End
public:

enum DataDefinedProperty
{
NoProperty,
AllProperties,
TestProperty,
//composer
PresetPaperSize,
PaperWidth,
PaperHeight,
NumPages,
PaperOrientation,
//general
PageNumber,
PositionX,
PositionY,
ItemWidth,
ItemHeight,
ItemRotation,
Transparency,
Opacity,
BlendMode,
ExcludeFromExports,
FrameColor,
BackgroundColor,
//composer
MapRotation,
MapScale,
MapXMin,
MapYMin,
MapXMax,
MapYMax,
MapAtlasMargin,
MapLayers,
MapStylePreset,
//composer
PictureSource,
PictureSvgBackgroundColor,
PictureSvgStrokeColor,
PictureSvgStrokeWidth,
//html
SourceUrl,
//legend
LegendTitle,
LegendColumnCount,
//scalebar
ScalebarFillColor,
ScalebarFillColor2,
ScalebarLineColor,
ScalebarLineWidth,
};

static const QgsPropertiesDefinition &propertyDefinitions();
%Docstring
Returns the layout object property definitions.
:rtype: QgsPropertiesDefinition
%End

QgsLayoutObject( QgsLayout *layout );
%Docstring
Constructor for QgsLayoutObject, with the specified parent ``layout``.
Expand All @@ -38,9 +95,67 @@ class QgsLayoutObject: QObject
:rtype: QgsLayout
%End

QgsPropertyCollection &dataDefinedProperties();
%Docstring
Returns a reference to the object's property collection, used for data defined overrides.
.. seealso:: setDataDefinedProperties()
:rtype: QgsPropertyCollection
%End


void setDataDefinedProperties( const QgsPropertyCollection &collection );
%Docstring
Sets the objects's property collection, used for data defined overrides.
\param collection property collection. Existing properties will be replaced.
.. seealso:: dataDefinedProperties()
%End


void setCustomProperty( const QString &key, const QVariant &value );
%Docstring
Set a custom property for the object.
\param key property key. If a property with the same key already exists it will be overwritten.
\param value property value
.. seealso:: customProperty()
.. seealso:: removeCustomProperty()
.. seealso:: customProperties()
%End

QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;
%Docstring
Read a custom property from the object.
\param key property key
\param defaultValue default value to return if property with matching key does not exist
:return: value of matching property
.. seealso:: setCustomProperty()
.. seealso:: removeCustomProperty()
.. seealso:: customProperties()
:rtype: QVariant
%End

void removeCustomProperty( const QString &key );
%Docstring
Remove a custom property from the object.
\param key property key
.. seealso:: setCustomProperty()
.. seealso:: customProperty()
.. seealso:: customProperties()
%End

QStringList customProperties() const;
%Docstring
Return list of keys stored in custom properties for the object.
.. seealso:: setCustomProperty()
.. seealso:: customProperty()
.. seealso:: removeCustomProperty()
:rtype: list of str
%End

protected:




};

/************************************************************************
Expand Down
85 changes: 85 additions & 0 deletions src/core/layout/qgslayoutobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,93 @@
#include "qgslayout.h"
#include "qgslayoutobject.h"


QgsPropertiesDefinition QgsLayoutObject::sPropertyDefinitions;

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

sPropertyDefinitions = QgsPropertiesDefinition
{
{ QgsLayoutObject::TestProperty, QgsPropertyDefinition( "dataDefinedProperty", QgsPropertyDefinition::DataTypeString, "invalid property", QString() ) },
{
QgsLayoutObject::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>"
"<b>B5</b>|<b>B4</b>|<b>B3</b>|<b>B2</b>|<b>B1</b>|<b>B0</b>"
"<b>Legal</b>|<b>Ansi A</b>|<b>Ansi B</b>|<b>Ansi C</b>|<b>Ansi D</b>|<b>Ansi E</b>"
"<b>Arch A</b>|<b>Arch B</b>|<b>Arch C</b>|<b>Arch D</b>|<b>Arch E</b>|<b>Arch E1</b>]"
) )
},
{ QgsLayoutObject::PaperWidth, QgsPropertyDefinition( "dataDefinedPaperWidth", QObject::tr( "Page width" ), QgsPropertyDefinition::DoublePositive ) },
{ QgsLayoutObject::PaperHeight, QgsPropertyDefinition( "dataDefinedPaperHeight", QObject::tr( "Page height" ), QgsPropertyDefinition::DoublePositive ) },
{ QgsLayoutObject::NumPages, QgsPropertyDefinition( "dataDefinedNumPages", QObject::tr( "Number of pages" ), QgsPropertyDefinition::IntegerPositive ) },
{ QgsLayoutObject::PaperOrientation, QgsPropertyDefinition( "dataDefinedPaperOrientation", QgsPropertyDefinition::DataTypeString, QObject::tr( "Symbol size" ), QObject::tr( "string " ) + QLatin1String( "[<b>portrait</b>|<b>landscape</b>]" ) ) },
{ QgsLayoutObject::PageNumber, QgsPropertyDefinition( "dataDefinedPageNumber", QObject::tr( "Page number" ), QgsPropertyDefinition::IntegerPositive ) },
{ QgsLayoutObject::PositionX, QgsPropertyDefinition( "dataDefinedPositionX", QObject::tr( "Position (X)" ), QgsPropertyDefinition::Double ) },
{ QgsLayoutObject::PositionY, QgsPropertyDefinition( "dataDefinedPositionY", QObject::tr( "Position (Y)" ), QgsPropertyDefinition::Double ) },
{ QgsLayoutObject::ItemWidth, QgsPropertyDefinition( "dataDefinedWidth", QObject::tr( "Width" ), QgsPropertyDefinition::DoublePositive ) },
{ QgsLayoutObject::ItemHeight, QgsPropertyDefinition( "dataDefinedHeight", QObject::tr( "Height" ), QgsPropertyDefinition::DoublePositive ) },
{ QgsLayoutObject::ItemRotation, QgsPropertyDefinition( "dataDefinedRotation", QObject::tr( "Rotation angle" ), QgsPropertyDefinition::Rotation ) },
{ QgsLayoutObject::Transparency, QgsPropertyDefinition( "dataDefinedTransparency", QObject::tr( "Transparency" ), QgsPropertyDefinition::Opacity ) },
{ QgsLayoutObject::Opacity, QgsPropertyDefinition( "dataDefinedOpacity", QObject::tr( "Opacity" ), QgsPropertyDefinition::Opacity ) },
{ QgsLayoutObject::BlendMode, QgsPropertyDefinition( "dataDefinedBlendMode", QObject::tr( "Blend mode" ), QgsPropertyDefinition::BlendMode ) },
{ QgsLayoutObject::ExcludeFromExports, QgsPropertyDefinition( "dataDefinedExcludeExports", QObject::tr( "Exclude item from exports" ), QgsPropertyDefinition::Boolean ) },
{ QgsLayoutObject::FrameColor, QgsPropertyDefinition( "dataDefinedFrameColor", QObject::tr( "Frame color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsLayoutObject::BackgroundColor, QgsPropertyDefinition( "dataDefinedBackgroundColor", QObject::tr( "Background color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsLayoutObject::MapRotation, QgsPropertyDefinition( "dataDefinedMapRotation", QObject::tr( "Map rotation" ), QgsPropertyDefinition::Rotation ) },
{ QgsLayoutObject::MapScale, QgsPropertyDefinition( "dataDefinedMapScale", QObject::tr( "Map scale" ), QgsPropertyDefinition::DoublePositive ) },
{ QgsLayoutObject::MapXMin, QgsPropertyDefinition( "dataDefinedMapXMin", QObject::tr( "Extent minimum X" ), QgsPropertyDefinition::Double ) },
{ QgsLayoutObject::MapYMin, QgsPropertyDefinition( "dataDefinedMapYMin", QObject::tr( "Extent minimum Y" ), QgsPropertyDefinition::Double ) },
{ QgsLayoutObject::MapXMax, QgsPropertyDefinition( "dataDefinedMapXMax", QObject::tr( "Extent maximum X" ), QgsPropertyDefinition::Double ) },
{ QgsLayoutObject::MapYMax, QgsPropertyDefinition( "dataDefinedMapYMax", QObject::tr( "Extent maximum Y" ), QgsPropertyDefinition::Double ) },
{ QgsLayoutObject::MapAtlasMargin, QgsPropertyDefinition( "dataDefinedMapAtlasMargin", QObject::tr( "Atlas margin" ), QgsPropertyDefinition::DoublePositive ) },
{ QgsLayoutObject::MapLayers, QgsPropertyDefinition( "dataDefinedMapLayers", QgsPropertyDefinition::DataTypeString, QObject::tr( "Symbol size" ), tr( "list of map layer names separated by | characters" ) ) },
{ QgsLayoutObject::MapStylePreset, QgsPropertyDefinition( "dataDefinedMapStylePreset", QgsPropertyDefinition::DataTypeString, QObject::tr( "Symbol size" ), tr( "list of map layer names separated by | characters" ) ) },
{ QgsLayoutObject::PictureSource, QgsPropertyDefinition( "dataDefinedSource", QObject::tr( "Picture source (URL)" ), QgsPropertyDefinition::String ) },
{ QgsLayoutObject::SourceUrl, QgsPropertyDefinition( "dataDefinedSourceUrl", QObject::tr( "Source URL" ), QgsPropertyDefinition::String ) },
{ QgsLayoutObject::PictureSvgBackgroundColor, QgsPropertyDefinition( "dataDefinedSvgBackgroundColor", QObject::tr( "SVG background color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsLayoutObject::PictureSvgStrokeColor, QgsPropertyDefinition( "dataDefinedSvgStrokeColor", QObject::tr( "SVG stroke color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsLayoutObject::PictureSvgStrokeWidth, QgsPropertyDefinition( "dataDefinedSvgStrokeWidth", QObject::tr( "SVG stroke width" ), QgsPropertyDefinition::StrokeWidth ) },
{ QgsLayoutObject::LegendTitle, QgsPropertyDefinition( "dataDefinedLegendTitle", QObject::tr( "Legend title" ), QgsPropertyDefinition::String ) },
{ QgsLayoutObject::LegendColumnCount, QgsPropertyDefinition( "dataDefinedLegendColumns", QObject::tr( "Number of columns" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) },
{ QgsLayoutObject::ScalebarFillColor, QgsPropertyDefinition( "dataDefinedScalebarFill", QObject::tr( "Fill color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsLayoutObject::ScalebarFillColor2, QgsPropertyDefinition( "dataDefinedScalebarFill2", QObject::tr( "Secondary fill color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsLayoutObject::ScalebarLineColor, QgsPropertyDefinition( "dataDefinedScalebarLineColor", QObject::tr( "Line color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsLayoutObject::ScalebarLineWidth, QgsPropertyDefinition( "dataDefinedScalebarLineWidth", QObject::tr( "Line width" ), QgsPropertyDefinition::StrokeWidth ) },
};
}

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

QgsLayoutObject::QgsLayoutObject( QgsLayout *layout )
: QObject( nullptr )
, mLayout( layout )
{
initPropertyDefinitions();
}


void QgsLayoutObject::setCustomProperty( const QString &key, const QVariant &value )
{
mCustomProperties.setValue( key, value );
}

QVariant QgsLayoutObject::customProperty( const QString &key, const QVariant &defaultValue ) const
{
return mCustomProperties.value( key, defaultValue );
}

void QgsLayoutObject::removeCustomProperty( const QString &key )
{
mCustomProperties.remove( key );
}

QStringList QgsLayoutObject::customProperties() const
{
return mCustomProperties.keys();
}
128 changes: 128 additions & 0 deletions src/core/layout/qgslayoutobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgspropertycollection.h"
#include "qgsobjectcustomproperties.h"
#include <QObject>
#include <QDomNode>
#include <QMap>
Expand All @@ -36,6 +38,64 @@ class CORE_EXPORT QgsLayoutObject: public QObject
Q_OBJECT
public:

/** Data defined properties for different item types
*/
enum DataDefinedProperty
{
NoProperty = 0, //!< No property
AllProperties, //!< All properties for item
TestProperty, //!< Dummy property with no effect on item
//composer page properties
PresetPaperSize, //!< Preset paper size for composition
PaperWidth, //!< Paper width
PaperHeight, //!< Paper height
NumPages, //!< Number of pages in composition
PaperOrientation, //!< Paper orientation
//general composer item properties
PageNumber, //!< Page number for item placement
PositionX, //!< X position on page
PositionY, //!< Y position on page
ItemWidth, //!< Width of item
ItemHeight, //!< Height of item
ItemRotation, //!< Rotation of item
Transparency, //!< Item transparency (deprecated)
Opacity, //!< Item opacity
BlendMode, //!< Item blend mode
ExcludeFromExports, //!< Exclude item from exports
FrameColor, //!< Item frame color
BackgroundColor, //!< Item background color
//composer map
MapRotation, //!< Map rotation
MapScale, //!< Map scale
MapXMin, //!< Map extent x minimum
MapYMin, //!< Map extent y minimum
MapXMax, //!< Map extent x maximum
MapYMax, //!< Map extent y maximum
MapAtlasMargin, //!< Map atlas margin
MapLayers, //!< Map layer set
MapStylePreset, //!< Layer and style map theme
//composer picture
PictureSource, //!< Picture source url
PictureSvgBackgroundColor, //!< SVG background color
PictureSvgStrokeColor, //!< SVG stroke color
PictureSvgStrokeWidth, //!< SVG stroke width
//html item
SourceUrl, //!< Html source url
//legend item
LegendTitle, //!< Legend title
LegendColumnCount, //!< Legend column count
//scalebar item
ScalebarFillColor, //!< Scalebar fill color
ScalebarFillColor2, //!< Scalebar secondary fill color
ScalebarLineColor, //!< Scalebar line color
ScalebarLineWidth, //!< Scalebar line width
};

/**
* Returns the layout object property definitions.
*/
static const QgsPropertiesDefinition &propertyDefinitions();

/**
* Constructor for QgsLayoutObject, with the specified parent \a layout.
* \note While ownership of a QgsLayoutObject is not passed to the layout,
Expand All @@ -54,12 +114,80 @@ class CORE_EXPORT QgsLayoutObject: public QObject
*/
QgsLayout *layout() { return mLayout; }

/**
* Returns a reference to the object's property collection, used for data defined overrides.
* \see setDataDefinedProperties()
*/
QgsPropertyCollection &dataDefinedProperties() { return mDataDefinedProperties; }

/**
* Returns a reference to the object's property collection, used for data defined overrides.
* \see setDataDefinedProperties()
*/
const QgsPropertyCollection &dataDefinedProperties() const { return mDataDefinedProperties; } SIP_SKIP

/**
* Sets the objects's property collection, used for data defined overrides.
* \param collection property collection. Existing properties will be replaced.
* \see dataDefinedProperties()
*/
void setDataDefinedProperties( const QgsPropertyCollection &collection ) { mDataDefinedProperties = collection; }


/**
* Set a custom property for the object.
* \param key property key. If a property with the same key already exists it will be overwritten.
* \param value property value
* \see customProperty()
* \see removeCustomProperty()
* \see customProperties()
*/
void setCustomProperty( const QString &key, const QVariant &value );

/**
* Read a custom property from the object.
* \param key property key
* \param defaultValue default value to return if property with matching key does not exist
* \returns value of matching property
* \see setCustomProperty()
* \see removeCustomProperty()
* \see customProperties()
*/
QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;

/**
* Remove a custom property from the object.
* \param key property key
* \see setCustomProperty()
* \see customProperty()
* \see customProperties()
*/
void removeCustomProperty( const QString &key );

/**
* Return list of keys stored in custom properties for the object.
* \see setCustomProperty()
* \see customProperty()
* \see removeCustomProperty()
*/
QStringList customProperties() const;

protected:

QgsLayout *mLayout = nullptr;

QgsPropertyCollection mDataDefinedProperties;

//! Custom properties for object
QgsObjectCustomProperties mCustomProperties;

private:

//! Property definitions
static QgsPropertiesDefinition sPropertyDefinitions;

static void initPropertyDefinitions();

friend class TestQgsLayoutObject;
};

Expand Down
Loading

0 comments on commit 15b65fa

Please sign in to comment.