QGIS API Documentation  2.99.0-Master (a411669)
qgscomposerobject.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerobject.cpp
3  -------------------
4  begin : July 2014
5  copyright : (C) 2014 by Nyall Dawson,Radim Blazek
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include <QPainter>
19 
20 #include "qgscomposition.h"
21 #include "qgscomposerutils.h"
22 #include "qgscomposerobject.h"
23 #include "qgsproject.h"
24 #include "qgsvectorlayer.h"
25 
26 QgsPropertiesDefinition QgsComposerObject::sPropertyDefinitions;
27 
28 void QgsComposerObject::initPropertyDefinitions()
29 {
30  if ( !sPropertyDefinitions.isEmpty() )
31  return;
32 
33  sPropertyDefinitions = QgsPropertiesDefinition
34  {
35  { QgsComposerObject::TestProperty, QgsPropertyDefinition( "dataDefinedProperty", QgsPropertyDefinition::DataTypeString, "invalid property", QString() ) },
36  {
37  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>"
38  "<b>B5</b>|<b>B4</b>|<b>B3</b>|<b>B2</b>|<b>B1</b>|<b>B0</b>"
39  "<b>Legal</b>|<b>Ansi A</b>|<b>Ansi B</b>|<b>Ansi C</b>|<b>Ansi D</b>|<b>Ansi E</b>"
40  "<b>Arch A</b>|<b>Arch B</b>|<b>Arch C</b>|<b>Arch D</b>|<b>Arch E</b>|<b>Arch E1</b>]"
41  ) )
42  },
43  { QgsComposerObject::PaperWidth, QgsPropertyDefinition( "dataDefinedPaperWidth", QObject::tr( "Page width" ), QgsPropertyDefinition::DoublePositive ) },
44  { QgsComposerObject::PaperHeight, QgsPropertyDefinition( "dataDefinedPaperHeight", QObject::tr( "Page height" ), QgsPropertyDefinition::DoublePositive ) },
45  { QgsComposerObject::NumPages, QgsPropertyDefinition( "dataDefinedNumPages", QObject::tr( "Number of pages" ), QgsPropertyDefinition::IntegerPositive ) },
46  { QgsComposerObject::PaperOrientation, QgsPropertyDefinition( "dataDefinedPaperOrientation", QgsPropertyDefinition::DataTypeString, QObject::tr( "Symbol size" ), QObject::tr( "string " ) + QLatin1String( "[<b>portrait</b>|<b>landscape</b>]" ) ) },
47  { QgsComposerObject::PageNumber, QgsPropertyDefinition( "dataDefinedPageNumber", QObject::tr( "Page number" ), QgsPropertyDefinition::IntegerPositive ) },
48  { QgsComposerObject::PositionX, QgsPropertyDefinition( "dataDefinedPositionX", QObject::tr( "Position (X)" ), QgsPropertyDefinition::Double ) },
49  { QgsComposerObject::PositionY, QgsPropertyDefinition( "dataDefinedPositionY", QObject::tr( "Position (Y)" ), QgsPropertyDefinition::Double ) },
50  { QgsComposerObject::ItemWidth, QgsPropertyDefinition( "dataDefinedWidth", QObject::tr( "Width" ), QgsPropertyDefinition::DoublePositive ) },
51  { QgsComposerObject::ItemHeight, QgsPropertyDefinition( "dataDefinedHeight", QObject::tr( "Height" ), QgsPropertyDefinition::DoublePositive ) },
52  { QgsComposerObject::ItemRotation, QgsPropertyDefinition( "dataDefinedRotation", QObject::tr( "Rotation angle" ), QgsPropertyDefinition::Rotation ) },
53  { QgsComposerObject::Transparency, QgsPropertyDefinition( "dataDefinedTransparency", QObject::tr( "Transparency" ), QgsPropertyDefinition::Opacity ) },
54  { QgsComposerObject::Opacity, QgsPropertyDefinition( "dataDefinedOpacity", QObject::tr( "Opacity" ), QgsPropertyDefinition::Opacity ) },
55  { QgsComposerObject::BlendMode, QgsPropertyDefinition( "dataDefinedBlendMode", QObject::tr( "Blend mode" ), QgsPropertyDefinition::BlendMode ) },
56  { QgsComposerObject::ExcludeFromExports, QgsPropertyDefinition( "dataDefinedExcludeExports", QObject::tr( "Exclude item from exports" ), QgsPropertyDefinition::Boolean ) },
57  { QgsComposerObject::FrameColor, QgsPropertyDefinition( "dataDefinedFrameColor", QObject::tr( "Frame color" ), QgsPropertyDefinition::ColorWithAlpha ) },
58  { QgsComposerObject::BackgroundColor, QgsPropertyDefinition( "dataDefinedBackgroundColor", QObject::tr( "Background color" ), QgsPropertyDefinition::ColorWithAlpha ) },
59  { QgsComposerObject::MapRotation, QgsPropertyDefinition( "dataDefinedMapRotation", QObject::tr( "Map rotation" ), QgsPropertyDefinition::Rotation ) },
60  { QgsComposerObject::MapScale, QgsPropertyDefinition( "dataDefinedMapScale", QObject::tr( "Map scale" ), QgsPropertyDefinition::DoublePositive ) },
61  { QgsComposerObject::MapXMin, QgsPropertyDefinition( "dataDefinedMapXMin", QObject::tr( "Extent minimum X" ), QgsPropertyDefinition::Double ) },
62  { QgsComposerObject::MapYMin, QgsPropertyDefinition( "dataDefinedMapYMin", QObject::tr( "Extent minimum Y" ), QgsPropertyDefinition::Double ) },
63  { QgsComposerObject::MapXMax, QgsPropertyDefinition( "dataDefinedMapXMax", QObject::tr( "Extent maximum X" ), QgsPropertyDefinition::Double ) },
64  { QgsComposerObject::MapYMax, QgsPropertyDefinition( "dataDefinedMapYMax", QObject::tr( "Extent maximum Y" ), QgsPropertyDefinition::Double ) },
65  { QgsComposerObject::MapAtlasMargin, QgsPropertyDefinition( "dataDefinedMapAtlasMargin", QObject::tr( "Atlas margin" ), QgsPropertyDefinition::DoublePositive ) },
66  { QgsComposerObject::MapLayers, QgsPropertyDefinition( "dataDefinedMapLayers", QgsPropertyDefinition::DataTypeString, QObject::tr( "Symbol size" ), tr( "list of map layer names separated by | characters" ) ) },
67  { QgsComposerObject::MapStylePreset, QgsPropertyDefinition( "dataDefinedMapStylePreset", QgsPropertyDefinition::DataTypeString, QObject::tr( "Symbol size" ), tr( "list of map layer names separated by | characters" ) ) },
68  { QgsComposerObject::PictureSource, QgsPropertyDefinition( "dataDefinedSource", QObject::tr( "Picture source (URL)" ), QgsPropertyDefinition::String ) },
69  { QgsComposerObject::SourceUrl, QgsPropertyDefinition( "dataDefinedSourceUrl", QObject::tr( "Source URL" ), QgsPropertyDefinition::String ) },
70  { QgsComposerObject::PictureSvgBackgroundColor, QgsPropertyDefinition( "dataDefinedSvgBackgroundColor", QObject::tr( "SVG background color" ), QgsPropertyDefinition::ColorWithAlpha ) },
71  { QgsComposerObject::PictureSvgStrokeColor, QgsPropertyDefinition( "dataDefinedSvgStrokeColor", QObject::tr( "SVG stroke color" ), QgsPropertyDefinition::ColorWithAlpha ) },
72  { QgsComposerObject::PictureSvgStrokeWidth, QgsPropertyDefinition( "dataDefinedSvgStrokeWidth", QObject::tr( "SVG stroke width" ), QgsPropertyDefinition::StrokeWidth ) },
73  { QgsComposerObject::LegendTitle, QgsPropertyDefinition( "dataDefinedLegendTitle", QObject::tr( "Legend title" ), QgsPropertyDefinition::String ) },
74  { QgsComposerObject::LegendColumnCount, QgsPropertyDefinition( "dataDefinedLegendColumns", QObject::tr( "Number of columns" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) },
75  { QgsComposerObject::ScalebarFillColor, QgsPropertyDefinition( "dataDefinedScalebarFill", QObject::tr( "Fill color" ), QgsPropertyDefinition::ColorWithAlpha ) },
76  { QgsComposerObject::ScalebarFillColor2, QgsPropertyDefinition( "dataDefinedScalebarFill2", QObject::tr( "Secondary fill color" ), QgsPropertyDefinition::ColorWithAlpha ) },
77  { QgsComposerObject::ScalebarLineColor, QgsPropertyDefinition( "dataDefinedScalebarLineColor", QObject::tr( "Line color" ), QgsPropertyDefinition::ColorWithAlpha ) },
78  { QgsComposerObject::ScalebarLineWidth, QgsPropertyDefinition( "dataDefinedScalebarLineWidth", QObject::tr( "Line width" ), QgsPropertyDefinition::StrokeWidth ) },
79  };
80 }
81 
83 {
84  QgsComposerObject::initPropertyDefinitions();
85  return sPropertyDefinitions;
86 }
87 
89  : QObject( nullptr )
90  , mComposition( composition )
91 {
92  initPropertyDefinitions();
93 
94  // data defined strings
95 
96  if ( mComposition )
97  {
98  //connect to atlas toggling on/off and coverage layer and feature changes
99  //to update data defined values
103  //also, refreshing composition triggers a recalculation of data defined properties
105 
106  //toggling atlas or changing coverage layer requires data defined expressions to be reprepared
109  }
110 
111 }
112 
113 bool QgsComposerObject::writeXml( QDomElement &elem, QDomDocument &doc ) const
114 {
115  if ( elem.isNull() )
116  {
117  return false;
118  }
119 
120  QDomElement ddPropsElement = doc.createElement( QStringLiteral( "dataDefinedProperties" ) );
121  mDataDefinedProperties.writeXml( ddPropsElement, sPropertyDefinitions );
122  elem.appendChild( ddPropsElement );
123 
124  //custom properties
125  mCustomProperties.writeXml( elem, doc );
126 
127  return true;
128 }
129 
130 bool QgsComposerObject::readXml( const QDomElement &itemElem, const QDomDocument &doc )
131 {
132  Q_UNUSED( doc );
133  if ( itemElem.isNull() )
134  {
135  return false;
136  }
137 
138  //old (pre 3.0) data defined properties
140 
141  QDomNode propsNode = itemElem.namedItem( QStringLiteral( "dataDefinedProperties" ) );
142  if ( !propsNode.isNull() )
143  {
144  mDataDefinedProperties.readXml( propsNode.toElement(), sPropertyDefinitions );
145  }
147  {
148  // upgrade transparency -> opacity
150  exp = QStringLiteral( "100.0 - (%1)" ).arg( exp );
153  }
154 
155  //custom properties
156  mCustomProperties.readXml( itemElem );
157 
158  return true;
159 }
160 
162 {
163  //nothing to do in base class for now
164 }
165 
167 {
168  Q_UNUSED( property );
169  Q_UNUSED( context );
170 
171  //nothing to do in base class for now
172 }
173 
174 void QgsComposerObject::prepareProperties() const
175 {
177  mDataDefinedProperties.prepare( context );
178 }
179 
180 void QgsComposerObject::setCustomProperty( const QString &key, const QVariant &value )
181 {
182  mCustomProperties.setValue( key, value );
183 }
184 
185 QVariant QgsComposerObject::customProperty( const QString &key, const QVariant &defaultValue ) const
186 {
187  return mCustomProperties.value( key, defaultValue );
188 }
189 
190 void QgsComposerObject::removeCustomProperty( const QString &key )
191 {
192  mCustomProperties.remove( key );
193 }
194 
196 {
197  return mCustomProperties.keys();
198 }
199 
201 {
202  if ( mComposition )
203  {
205  }
206  else
207  {
209  }
210 }
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from the object.
QString asExpression() const
Returns an expression string representing the state of the property, or an empty string if the proper...
Positive integer values (including 0)
Definition: qgsproperty.h:55
virtual QgsExpressionContext createExpressionContext() const
Creates an expression context relating to the objects&#39; current state.
QStringList customProperties() const
Return list of keys stored in custom properties for the object.
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from XML.
QgsObjectCustomProperties mCustomProperties
Custom properties for object.
virtual bool readXml(const QDomElement &collectionElem, const QgsPropertiesDefinition &definitions)
Reads property collection state from an XML element.
Layer and style map theme.
virtual void refreshDataDefinedProperty(const DataDefinedProperty property=AllProperties, const QgsExpressionContext *context=nullptr)
Refreshes a data defined property for the item by reevaluating the property&#39;s value and redrawing the...
Non-zero positive integer values.
Definition: qgsproperty.h:56
static QgsProperty fromExpression(const QString &expression, bool isActive=true)
Returns a new ExpressionBasedProperty created from the specified expression.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Return value for the given key. If the key is not stored, default value will be used.
Color with alpha channel.
Definition: qgsproperty.h:64
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
Write store contents to XML.
void toggled(bool)
Emitted when atlas is enabled or disabled.
Scalebar secondary fill color.
Map extent x maximum.
Positive double value (including 0)
Definition: qgsproperty.h:58
Map extent x minimum.
void remove(const QString &key)
Remove a key (entry) from the store.
Rotation (value between 0-360 degrees)
Definition: qgsproperty.h:60
DataDefinedProperty
Data defined properties for different item types.
Any string value.
Definition: qgsproperty.h:61
bool isActive(int key) const override
Returns true if the collection contains an active property with the specified key.
QgsComposerObject(QgsComposition *composition)
Constructor.
virtual bool prepare(const QgsExpressionContext &context=QgsExpressionContext()) const override
Prepares the collection against a specified expression context.
void refreshItemsTriggered()
Is emitted when item in the composition must be refreshed.
void setValue(const QString &key, const QVariant &value)
Add an entry to the store. If the entry with the keys exists already, it will be overwritten.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the composer object property definitions.
virtual bool readXml(const QDomElement &itemElem, const QDomDocument &doc)
Sets item state from DOM element.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Map extent y minimum.
void removeCustomProperty(const QString &key)
Remove a custom property from the object.
QgsPropertyCollection mDataDefinedProperties
virtual void repaint()
Triggers a redraw for the item.
A store for object properties.
Definition: qgsproperty.h:229
QStringList keys() const
Return list of stored keys.
Double value (including negative values)
Definition: qgsproperty.h:57
Graphics scene for map printing.
Definition for a property.
Definition: qgsproperty.h:46
void featureChanged(QgsFeature *feature)
Is emitted when the current atlas feature changes.
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.
static void readOldDataDefinedPropertyMap(const QDomElement &itemElem, QgsPropertyCollection &dataDefinedProperties)
Reads all pre 3.0 data defined properties from an XML element.
void coverageLayerChanged(QgsVectorLayer *layer)
Is emitted when the coverage layer for an atlas changes.
Map extent y maximum.
QgsComposition * mComposition
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the compositions&#39;s current state.
Property requires a string value.
Definition: qgsproperty.h:91
const QgsComposition * composition() const
Returns the composition the item is attached to.
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the object.
Number of pages in composition.
QgsAtlasComposition & atlasComposition()
Item transparency (deprecated)
Page number for item placement.
virtual bool writeXml(QDomElement &elem, QDomDocument &doc) const
Stores item state in DOM element.
Dummy property with no effect on item.
virtual bool writeXml(QDomElement &collectionElem, const QgsPropertiesDefinition &definitions) const
Writes the current state of the property collection into an XML element.
Preset paper size for composition.
QgsProperty property(int key) const override
Returns a matching property from the collection, if one exists.