QGIS API Documentation  2.99.0-Master (40f86b2)
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::Transparency ) },
54  { QgsComposerObject::BlendMode, QgsPropertyDefinition( "dataDefinedBlendMode", QObject::tr( "Blend mode" ), QgsPropertyDefinition::BlendMode ) },
55  { QgsComposerObject::ExcludeFromExports, QgsPropertyDefinition( "dataDefinedExcludeExports", QObject::tr( "Exclude item from exports" ), QgsPropertyDefinition::Boolean ) },
56  { QgsComposerObject::FrameColor, QgsPropertyDefinition( "dataDefinedFrameColor", QObject::tr( "Frame color" ), QgsPropertyDefinition::ColorWithAlpha ) },
57  { QgsComposerObject::BackgroundColor, QgsPropertyDefinition( "dataDefinedBackgroundColor", QObject::tr( "Background color" ), QgsPropertyDefinition::ColorWithAlpha ) },
58  { QgsComposerObject::MapRotation, QgsPropertyDefinition( "dataDefinedMapRotation", QObject::tr( "Map rotation" ), QgsPropertyDefinition::Rotation ) },
59  { QgsComposerObject::MapScale, QgsPropertyDefinition( "dataDefinedMapScale", QObject::tr( "Map scale" ), QgsPropertyDefinition::DoublePositive ) },
60  { QgsComposerObject::MapXMin, QgsPropertyDefinition( "dataDefinedMapXMin", QObject::tr( "Extent minimum X" ), QgsPropertyDefinition::Double ) },
61  { QgsComposerObject::MapYMin, QgsPropertyDefinition( "dataDefinedMapYMin", QObject::tr( "Extent minimum Y" ), QgsPropertyDefinition::Double ) },
62  { QgsComposerObject::MapXMax, QgsPropertyDefinition( "dataDefinedMapXMax", QObject::tr( "Extent maximum X" ), QgsPropertyDefinition::Double ) },
63  { QgsComposerObject::MapYMax, QgsPropertyDefinition( "dataDefinedMapYMax", QObject::tr( "Extent maximum Y" ), QgsPropertyDefinition::Double ) },
64  { QgsComposerObject::MapAtlasMargin, QgsPropertyDefinition( "dataDefinedMapAtlasMargin", QObject::tr( "Atlas margin" ), QgsPropertyDefinition::DoublePositive ) },
65  { QgsComposerObject::MapLayers, QgsPropertyDefinition( "dataDefinedMapLayers", QgsPropertyDefinition::DataTypeString, QObject::tr( "Symbol size" ), tr( "list of map layer names separated by | characters" ) ) },
66  { QgsComposerObject::MapStylePreset, QgsPropertyDefinition( "dataDefinedMapStylePreset", QgsPropertyDefinition::DataTypeString, QObject::tr( "Symbol size" ), tr( "list of map layer names separated by | characters" ) ) },
67  { QgsComposerObject::PictureSource, QgsPropertyDefinition( "dataDefinedSource", QObject::tr( "Picture source (URL)" ), QgsPropertyDefinition::String ) },
68  { QgsComposerObject::SourceUrl, QgsPropertyDefinition( "dataDefinedSourceUrl", QObject::tr( "Source URL" ), QgsPropertyDefinition::String ) },
69  { QgsComposerObject::PictureSvgBackgroundColor, QgsPropertyDefinition( "dataDefinedSvgBackgroundColor", QObject::tr( "SVG background color" ), QgsPropertyDefinition::ColorWithAlpha ) },
70  { QgsComposerObject::PictureSvgStrokeColor, QgsPropertyDefinition( "dataDefinedSvgStrokeColor", QObject::tr( "SVG stroke color" ), QgsPropertyDefinition::ColorWithAlpha ) },
71  { QgsComposerObject::PictureSvgStrokeWidth, QgsPropertyDefinition( "dataDefinedSvgStrokeWidth", QObject::tr( "SVG stroke width" ), QgsPropertyDefinition::StrokeWidth ) },
72  { QgsComposerObject::LegendTitle, QgsPropertyDefinition( "dataDefinedLegendTitle", QObject::tr( "Legend title" ), QgsPropertyDefinition::String ) },
73  { QgsComposerObject::LegendColumnCount, QgsPropertyDefinition( "dataDefinedLegendColumns", QObject::tr( "Number of columns" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) },
74  { QgsComposerObject::ScalebarFillColor, QgsPropertyDefinition( "dataDefinedScalebarFill", QObject::tr( "Fill color" ), QgsPropertyDefinition::ColorWithAlpha ) },
75  { QgsComposerObject::ScalebarFillColor2, QgsPropertyDefinition( "dataDefinedScalebarFill2", QObject::tr( "Secondary fill color" ), QgsPropertyDefinition::ColorWithAlpha ) },
76  { QgsComposerObject::ScalebarLineColor, QgsPropertyDefinition( "dataDefinedScalebarLineColor", QObject::tr( "Line color" ), QgsPropertyDefinition::ColorWithAlpha ) },
77  { QgsComposerObject::ScalebarLineWidth, QgsPropertyDefinition( "dataDefinedScalebarLineWidth", QObject::tr( "Line width" ), QgsPropertyDefinition::StrokeWidth ) },
78  };
79 }
80 
82 {
83  QgsComposerObject::initPropertyDefinitions();
84  return sPropertyDefinitions;
85 }
86 
88  : QObject( nullptr )
89  , mComposition( composition )
90 {
91  initPropertyDefinitions();
92 
93  // data defined strings
94 
95  if ( mComposition )
96  {
97  //connect to atlas toggling on/off and coverage layer and feature changes
98  //to update data defined values
102  //also, refreshing composition triggers a recalculation of data defined properties
104 
105  //toggling atlas or changing coverage layer requires data defined expressions to be reprepared
108  }
109 
110 }
111 
112 bool QgsComposerObject::writeXml( QDomElement &elem, QDomDocument &doc ) const
113 {
114  if ( elem.isNull() )
115  {
116  return false;
117  }
118 
119  QDomElement ddPropsElement = doc.createElement( QStringLiteral( "dataDefinedProperties" ) );
120  mDataDefinedProperties.writeXml( ddPropsElement, sPropertyDefinitions );
121  elem.appendChild( ddPropsElement );
122 
123  //custom properties
124  mCustomProperties.writeXml( elem, doc );
125 
126  return true;
127 }
128 
129 bool QgsComposerObject::readXml( const QDomElement &itemElem, const QDomDocument &doc )
130 {
131  Q_UNUSED( doc );
132  if ( itemElem.isNull() )
133  {
134  return false;
135  }
136 
137  //old (pre 3.0) data defined properties
139 
140  QDomNode propsNode = itemElem.namedItem( QStringLiteral( "dataDefinedProperties" ) );
141  if ( !propsNode.isNull() )
142  {
143  mDataDefinedProperties.readXml( propsNode.toElement(), sPropertyDefinitions );
144  }
145 
146  //custom properties
147  mCustomProperties.readXml( itemElem );
148 
149  return true;
150 }
151 
153 {
154  //nothing to do in base class for now
155 }
156 
158 {
159  Q_UNUSED( property );
160  Q_UNUSED( context );
161 
162  //nothing to do in base class for now
163 }
164 
165 void QgsComposerObject::prepareProperties() const
166 {
168  mDataDefinedProperties.prepare( context );
169 }
170 
171 void QgsComposerObject::setCustomProperty( const QString &key, const QVariant &value )
172 {
173  mCustomProperties.setValue( key, value );
174 }
175 
176 QVariant QgsComposerObject::customProperty( const QString &key, const QVariant &defaultValue ) const
177 {
178  return mCustomProperties.value( key, defaultValue );
179 }
180 
181 void QgsComposerObject::removeCustomProperty( const QString &key )
182 {
183  mCustomProperties.remove( key );
184 }
185 
187 {
188  return mCustomProperties.keys();
189 }
190 
192 {
193  if ( mComposition )
194  {
196  }
197  else
198  {
200  }
201 }
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from the object.
Positive integer values (including 0)
Definition: qgsproperty.h:54
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:55
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:63
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:57
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:59
DataDefinedProperty
Data defined properties for different item types.
Any string value.
Definition: qgsproperty.h:60
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.
QStringList keys() const
Return list of stored keys.
Double value (including negative values)
Definition: qgsproperty.h:56
Graphics scene for map printing.
Definition for a property.
Definition: qgsproperty.h:45
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:90
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()
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.