QGIS API Documentation  2.99.0-Master (90ae728)
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  { 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>"
37  "<b>B5</b>|<b>B4</b>|<b>B3</b>|<b>B2</b>|<b>B1</b>|<b>B0</b>"
38  "<b>Legal</b>|<b>Ansi A</b>|<b>Ansi B</b>|<b>Ansi C</b>|<b>Ansi D</b>|<b>Ansi E</b>"
39  "<b>Arch A</b>|<b>Arch B</b>|<b>Arch C</b>|<b>Arch D</b>|<b>Arch E</b>|<b>Arch E1</b>]"
40  ) ) },
41  { QgsComposerObject::PaperWidth, QgsPropertyDefinition( "dataDefinedPaperWidth", QObject::tr( "Page width" ), QgsPropertyDefinition::DoublePositive ) },
42  { QgsComposerObject::PaperHeight, QgsPropertyDefinition( "dataDefinedPaperHeight" , QObject::tr( "Page height" ), QgsPropertyDefinition::DoublePositive ) },
43  { QgsComposerObject::NumPages, QgsPropertyDefinition( "dataDefinedNumPages" , QObject::tr( "Number of pages" ), QgsPropertyDefinition::IntegerPositive ) },
44  { QgsComposerObject::PaperOrientation, QgsPropertyDefinition( "dataDefinedPaperOrientation", QgsPropertyDefinition::DataTypeString, QObject::tr( "Symbol size" ), QObject::tr( "string " ) + QLatin1String( "[<b>portrait</b>|<b>landscape</b>]" ) ) },
45  { QgsComposerObject::PageNumber, QgsPropertyDefinition( "dataDefinedPageNumber" , QObject::tr( "Page number" ), QgsPropertyDefinition::IntegerPositive ) },
46  { QgsComposerObject::PositionX, QgsPropertyDefinition( "dataDefinedPositionX" , QObject::tr( "Position (X)" ), QgsPropertyDefinition::Double ) },
47  { QgsComposerObject::PositionY, QgsPropertyDefinition( "dataDefinedPositionY" , QObject::tr( "Position (Y)" ), QgsPropertyDefinition::Double ) },
48  { QgsComposerObject::ItemWidth, QgsPropertyDefinition( "dataDefinedWidth" , QObject::tr( "Width" ), QgsPropertyDefinition::DoublePositive ) },
49  { QgsComposerObject::ItemHeight, QgsPropertyDefinition( "dataDefinedHeight" , QObject::tr( "Height" ), QgsPropertyDefinition::DoublePositive ) },
50  { QgsComposerObject::ItemRotation, QgsPropertyDefinition( "dataDefinedRotation" , QObject::tr( "Rotation angle" ), QgsPropertyDefinition::Rotation ) },
51  { QgsComposerObject::Transparency, QgsPropertyDefinition( "dataDefinedTransparency" , QObject::tr( "Transparency" ), QgsPropertyDefinition::Transparency ) },
52  { QgsComposerObject::BlendMode, QgsPropertyDefinition( "dataDefinedBlendMode" , QObject::tr( "Blend mode" ), QgsPropertyDefinition::BlendMode ) },
53  { QgsComposerObject::ExcludeFromExports, QgsPropertyDefinition( "dataDefinedExcludeExports", QObject::tr( "Exclude item from exports" ), QgsPropertyDefinition::Boolean ) },
54  { QgsComposerObject::FrameColor, QgsPropertyDefinition( "dataDefinedFrameColor", QObject::tr( "Frame color" ), QgsPropertyDefinition::ColorWithAlpha ) },
55  { QgsComposerObject::BackgroundColor, QgsPropertyDefinition( "dataDefinedBackgroundColor", QObject::tr( "Background color" ), QgsPropertyDefinition::ColorWithAlpha ) },
56  { QgsComposerObject::MapRotation, QgsPropertyDefinition( "dataDefinedMapRotation" , QObject::tr( "Map rotation" ), QgsPropertyDefinition::Rotation ) },
57  { QgsComposerObject::MapScale, QgsPropertyDefinition( "dataDefinedMapScale" , QObject::tr( "Map scale" ), QgsPropertyDefinition::DoublePositive ) },
58  { QgsComposerObject::MapXMin, QgsPropertyDefinition( "dataDefinedMapXMin" , QObject::tr( "Extent minimum X" ), QgsPropertyDefinition::Double ) },
59  { QgsComposerObject::MapYMin, QgsPropertyDefinition( "dataDefinedMapYMin" , QObject::tr( "Extent minimum Y" ), QgsPropertyDefinition::Double ) },
60  { QgsComposerObject::MapXMax, QgsPropertyDefinition( "dataDefinedMapXMax" , QObject::tr( "Extent maximum X" ), QgsPropertyDefinition::Double ) },
61  { QgsComposerObject::MapYMax, QgsPropertyDefinition( "dataDefinedMapYMax" , QObject::tr( "Extent maximum Y" ), QgsPropertyDefinition::Double ) },
62  { QgsComposerObject::MapAtlasMargin, QgsPropertyDefinition( "dataDefinedMapAtlasMargin" , QObject::tr( "Atlas margin" ), QgsPropertyDefinition::DoublePositive ) },
63  { QgsComposerObject::MapLayers, QgsPropertyDefinition( "dataDefinedMapLayers", QgsPropertyDefinition::DataTypeString, QObject::tr( "Symbol size" ), tr( "list of map layer names separated by | characters" ) ) },
64  { QgsComposerObject::MapStylePreset, QgsPropertyDefinition( "dataDefinedMapStylePreset", QgsPropertyDefinition::DataTypeString, QObject::tr( "Symbol size" ), tr( "list of map layer names separated by | characters" ) ) },
65  { QgsComposerObject::PictureSource, QgsPropertyDefinition( "dataDefinedSource" , QObject::tr( "Picture source (URL)" ), QgsPropertyDefinition::String ) },
66  { QgsComposerObject::SourceUrl, QgsPropertyDefinition( "dataDefinedSourceUrl" , QObject::tr( "Source URL" ), QgsPropertyDefinition::String ) },
67  { QgsComposerObject::PictureSvgBackgroundColor, QgsPropertyDefinition( "dataDefinedSvgBackgroundColor" , QObject::tr( "SVG background color" ), QgsPropertyDefinition::ColorWithAlpha ) },
68  { QgsComposerObject::PictureSvgStrokeColor, QgsPropertyDefinition( "dataDefinedSvgStrokeColor" , QObject::tr( "SVG stroke color" ), QgsPropertyDefinition::ColorWithAlpha ) },
69  { QgsComposerObject::PictureSvgStrokeWidth, QgsPropertyDefinition( "dataDefinedSvgStrokeWidth" , QObject::tr( "SVG stroke width" ), QgsPropertyDefinition::StrokeWidth ) },
70  { QgsComposerObject::LegendTitle, QgsPropertyDefinition( "dataDefinedLegendTitle" , QObject::tr( "Legend title" ), QgsPropertyDefinition::String ) },
71  { QgsComposerObject::LegendColumnCount, QgsPropertyDefinition( "dataDefinedLegendColumns" , QObject::tr( "Number of columns" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) },
72  { QgsComposerObject::ScalebarFillColor, QgsPropertyDefinition( "dataDefinedScalebarFill" , QObject::tr( "Fill color" ), QgsPropertyDefinition::ColorWithAlpha ) },
73  { QgsComposerObject::ScalebarFillColor2, QgsPropertyDefinition( "dataDefinedScalebarFill2" , QObject::tr( "Secondary fill color" ), QgsPropertyDefinition::ColorWithAlpha ) },
74  { QgsComposerObject::ScalebarLineColor, QgsPropertyDefinition( "dataDefinedScalebarLineColor" , QObject::tr( "Line color" ), QgsPropertyDefinition::ColorWithAlpha ) },
75  { QgsComposerObject::ScalebarLineWidth, QgsPropertyDefinition( "dataDefinedScalebarLineWidth" , QObject::tr( "Line width" ), QgsPropertyDefinition::StrokeWidth ) },
76  };
77 }
78 
80 {
81  QgsComposerObject::initPropertyDefinitions();
82  return sPropertyDefinitions;
83 }
84 
86  : QObject( nullptr )
87  , mComposition( composition )
88 {
89  initPropertyDefinitions();
90 
91  // data defined strings
92 
93  if ( mComposition )
94  {
95  //connect to atlas toggling on/off and coverage layer and feature changes
96  //to update data defined values
100  //also, refreshing composition triggers a recalculation of data defined properties
102 
103  //toggling atlas or changing coverage layer requires data defined expressions to be reprepared
106  }
107 
108 }
109 
110 bool QgsComposerObject::writeXml( QDomElement &elem, QDomDocument &doc ) const
111 {
112  if ( elem.isNull() )
113  {
114  return false;
115  }
116 
117  QDomElement ddPropsElement = doc.createElement( QStringLiteral( "dataDefinedProperties" ) );
118  mDataDefinedProperties.writeXml( ddPropsElement, doc, sPropertyDefinitions );
119  elem.appendChild( ddPropsElement );
120 
121  //custom properties
122  mCustomProperties.writeXml( elem, doc );
123 
124  return true;
125 }
126 
127 bool QgsComposerObject::readXml( const QDomElement &itemElem, const QDomDocument &doc )
128 {
129  Q_UNUSED( doc );
130  if ( itemElem.isNull() )
131  {
132  return false;
133  }
134 
135  //old (pre 3.0) data defined properties
137 
138  QDomNode propsNode = itemElem.namedItem( QStringLiteral( "dataDefinedProperties" ) );
139  if ( !propsNode.isNull() )
140  {
141  mDataDefinedProperties.readXml( propsNode.toElement(), doc, sPropertyDefinitions );
142  }
143 
144  //custom properties
145  mCustomProperties.readXml( itemElem );
146 
147  return true;
148 }
149 
151 {
152  //nothing to do in base class for now
153 }
154 
156 {
157  Q_UNUSED( property );
158  Q_UNUSED( context );
159 
160  //nothing to do in base class for now
161 }
162 
163 void QgsComposerObject::prepareProperties() const
164 {
166  mDataDefinedProperties.prepare( context );
167 }
168 
169 void QgsComposerObject::setCustomProperty( const QString& key, const QVariant& value )
170 {
171  mCustomProperties.setValue( key, value );
172 }
173 
174 QVariant QgsComposerObject::customProperty( const QString& key, const QVariant& defaultValue ) const
175 {
176  return mCustomProperties.value( key, defaultValue );
177 }
178 
179 void QgsComposerObject::removeCustomProperty( const QString& key )
180 {
181  mCustomProperties.remove( key );
182 }
183 
185 {
186  return mCustomProperties.keys();
187 }
188 
190 {
191  if ( mComposition )
192  {
194  }
195  else
196  {
198  }
199 }
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.
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.
bool readXml(const QDomElement &collectionElem, const QDomDocument &doc, const QgsPropertiesDefinition &definitions) override
Reads property collection state from an XML element.
Map extent y maximum.
bool writeXml(QDomElement &collectionElem, QDomDocument &doc, const QgsPropertiesDefinition &definitions) const override
Writes the current state of the property collection into an XML element.
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.
Preset paper size for composition.