QGIS API Documentation  2.99.0-Master (5753576)
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 "qgsdatadefined.h"
24 #include "qgsproject.h"
25 
27  : QObject( nullptr )
28  , mComposition( composition )
29 {
30 
31  // data defined strings
32  mDataDefinedNames.insert( QgsComposerObject::TestProperty, QStringLiteral( "dataDefinedTestProperty" ) );
33 
34  if ( mComposition )
35  {
36  //connect to atlas toggling on/off and coverage layer and feature changes
37  //to update data defined values
38  connect( &mComposition->atlasComposition(), SIGNAL( toggled( bool ) ), this, SLOT( refreshDataDefinedProperty() ) );
39  connect( &mComposition->atlasComposition(), SIGNAL( coverageLayerChanged( QgsVectorLayer* ) ), this, SLOT( refreshDataDefinedProperty() ) );
40  connect( &mComposition->atlasComposition(), SIGNAL( featureChanged( QgsFeature* ) ), this, SLOT( refreshDataDefinedProperty() ) );
41  //also, refreshing composition triggers a recalculation of data defined properties
42  connect( mComposition, SIGNAL( refreshItemsTriggered() ), this, SLOT( refreshDataDefinedProperty() ) );
43 
44  //toggling atlas or changing coverage layer requires data defined expressions to be reprepared
45  connect( &mComposition->atlasComposition(), SIGNAL( toggled( bool ) ), this, SLOT( prepareDataDefinedExpressions() ) );
46  connect( &mComposition->atlasComposition(), SIGNAL( coverageLayerChanged( QgsVectorLayer* ) ), this, SLOT( prepareDataDefinedExpressions() ) );
47  }
48 
49 }
50 
52 {
53  qDeleteAll( mDataDefinedProperties );
54 }
55 
56 bool QgsComposerObject::writeXml( QDomElement &elem, QDomDocument &doc ) const
57 {
58  if ( elem.isNull() )
59  {
60  return false;
61  }
62 
63  //data defined properties
64  QgsComposerUtils::writeDataDefinedPropertyMap( elem, doc, &mDataDefinedNames, &mDataDefinedProperties );
65 
66  //custom properties
67  mCustomProperties.writeXml( elem, doc );
68 
69  return true;
70 }
71 
72 bool QgsComposerObject::readXml( const QDomElement &itemElem, const QDomDocument &doc )
73 {
74  Q_UNUSED( doc );
75  if ( itemElem.isNull() )
76  {
77  return false;
78  }
79 
80  //data defined properties
81  QgsComposerUtils::readDataDefinedPropertyMap( itemElem, &mDataDefinedNames, &mDataDefinedProperties );
82 
83  //custom properties
84  mCustomProperties.readXml( itemElem );
85 
86  return true;
87 }
88 
90 {
91  if ( property == QgsComposerObject::AllProperties || property == QgsComposerObject::NoProperty )
92  {
93  //bad property requested, don't return anything
94  return nullptr;
95  }
96 
97  //find corresponding QgsDataDefined and return it
98  QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >::const_iterator it = mDataDefinedProperties.constFind( property );
99  if ( it != mDataDefinedProperties.constEnd() )
100  {
101  return it.value();
102  }
103 
104  //could not find matching QgsDataDefined
105  return nullptr;
106 }
107 
108 void QgsComposerObject::setDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property, const bool active, const bool useExpression, const QString &expression, const QString &field )
109 {
110  if ( property == QgsComposerObject::AllProperties || property == QgsComposerObject::NoProperty )
111  {
112  //bad property requested
113  return;
114  }
115 
116  bool defaultVals = ( !active && !useExpression && expression.isEmpty() && field.isEmpty() );
117 
118  if ( mDataDefinedProperties.contains( property ) )
119  {
120  QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >::const_iterator it = mDataDefinedProperties.constFind( property );
121  if ( it != mDataDefinedProperties.constEnd() )
122  {
123  QgsDataDefined* dd = it.value();
124  dd->setActive( active );
125  dd->setExpressionString( expression );
126  dd->setField( field );
127  dd->setUseExpression( useExpression );
128  }
129  }
130  else if ( !defaultVals )
131  {
132  QgsDataDefined* dd = new QgsDataDefined( active, useExpression, expression, field );
133  mDataDefinedProperties.insert( property, dd );
134  }
135 }
136 
138 {
139  //nothing to do in base class for now
140 }
141 
143 {
144  Q_UNUSED( property );
145  Q_UNUSED( context );
146 
147  //nothing to do in base class for now
148 }
149 
150 bool QgsComposerObject::dataDefinedEvaluate( const DataDefinedProperty property, QVariant &expressionValue, const QgsExpressionContext& context ) const
151 {
152  if ( !mComposition )
153  {
154  return false;
155  }
156  return mComposition->dataDefinedEvaluate( property, expressionValue, context, &mDataDefinedProperties );
157 }
158 
159 void QgsComposerObject::prepareDataDefinedExpressions() const
160 {
162 
163  //prepare all QgsDataDefineds
164  QMap< DataDefinedProperty, QgsDataDefined* >::const_iterator it = mDataDefinedProperties.constBegin();
165  if ( it != mDataDefinedProperties.constEnd() )
166  {
167  it.value()->prepareExpression( context );
168  }
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 }
void setActive(bool active)
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from the object.
A container class for data source field mapping or expression.
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.
static void readDataDefinedPropertyMap(const QDomElement &itemElem, QMap< QgsComposerObject::DataDefinedProperty, QString > *dataDefinedNames, QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined * > *dataDefinedProperties)
Reads all data defined properties from xml.
QMap< QgsComposerObject::DataDefinedProperty, QString > mDataDefinedNames
Map of data defined properties for the item to string name to use when exporting item to xml...
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...
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.
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
Write store contents to XML.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:136
static void writeDataDefinedPropertyMap(QDomElement &itemElem, QDomDocument &doc, const QMap< QgsComposerObject::DataDefinedProperty, QString > *dataDefinedNames, const QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined * > *dataDefinedProperties)
Writes data defined properties to xml.
void remove(const QString &key)
Remove a key (entry) from the store.
DataDefinedProperty
Data defined properties for different item types.
QgsComposerObject(QgsComposition *composition)
Constructor.
void setUseExpression(bool use)
Controls if the field or the expression part is active.
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...
void setField(const QString &field)
Set the field name which this QgsDataDefined represents.
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...
bool dataDefinedEvaluate(const QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue, const QgsExpressionContext &context=QgsExpressionContext()) const
Evaluate a data defined property and return the calculated value.
void removeCustomProperty(const QString &key)
Remove a custom property from the object.
virtual void repaint()
Triggers a redraw for the item.
QStringList keys() const
Return list of stored keys.
Graphics scene for map printing.
QgsDataDefined * dataDefinedProperty(const DataDefinedProperty property) const
Returns a reference to the data defined settings for one of the item&#39;s data defined properties...
QgsComposition * mComposition
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the compositions&#39;s current state.
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the object.
void setDataDefinedProperty(const DataDefinedProperty property, const bool active, const bool useExpression, const QString &expression, const QString &field)
Sets parameters for a data defined property for the item.
QgsAtlasComposition & atlasComposition()
virtual bool writeXml(QDomElement &elem, QDomDocument &doc) const
Stores item state in DOM element.
Dummy property with no effect on item.
Represents a vector layer which manages a vector based data sets.
void setExpressionString(const QString &expr)
Sets the expression for this QgsDataDefined.
All properties for item.