Skip to content

Commit 2041fc2

Browse files
committed
[composer] Refactor QgsComposerItem:
- New class QgsComposerObject, which both QgsComposerItem and QgsComposerMultiFrame derive from. This class contains the framework for data defined composition properties. - New class QgsComposerUtils, containing helpful static functions previously in QgsComposerItem - Test suites for data defined settings in QgsComposerObject and functions in QgsComposerUtils
1 parent 49bc38c commit 2041fc2

35 files changed

+1792
-652
lines changed

python/core/composer/qgscomposeritem.sip

Lines changed: 35 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** \ingroup MapComposer
22
* A item that forms part of a map composition.
33
*/
4-
class QgsComposerItem : QObject, QGraphicsRectItem
4+
class QgsComposerItem : QgsComposerObject, QGraphicsRectItem
55
{
66
%TypeHeaderCode
77
#include <qgscomposeritem.h>
@@ -133,46 +133,6 @@ class QgsComposerItem : QObject, QGraphicsRectItem
133133
LowerRight
134134
};
135135

136-
/** Data defined properties for different item types
137-
*/
138-
enum DataDefinedProperty
139-
{
140-
NoProperty = 0, /*< no property */
141-
AllProperties, /*< all properties for item */
142-
//composer page properties
143-
PresetPaperSize, /*< preset paper size for composition */
144-
PaperWidth, /*< paper width */
145-
PaperHeight, /*< paper height */
146-
NumPages, /*< number of pages in composition */
147-
PaperOrientation, /*< paper orientation */
148-
//general composer item properties
149-
PageNumber, /*< page number for item placement */
150-
PositionX, /*< x position on page */
151-
PositionY, /*< y position on page */
152-
ItemWidth, /*< width of item */
153-
ItemHeight, /*< height of item */
154-
ItemRotation, /*< rotation of item */
155-
Transparency, /*< item transparency */
156-
BlendMode, /*< item blend mode */
157-
//composer map
158-
MapRotation, /*< map rotation */
159-
MapScale, /*< map scale */
160-
MapXMin, /*< map extent x minimum */
161-
MapYMin, /*< map extent y minimum */
162-
MapXMax, /*< map extent x maximum */
163-
MapYMax /*< map extent y maximum */
164-
};
165-
166-
/** Specifies whether the value returned by a function should be the original, user
167-
* set value, or the current evaluated value for the property. This may differ if
168-
* a property has a data defined expression active.
169-
*/
170-
enum PropertyValueType
171-
{
172-
EvaluatedValue = 0, /*< return the current evaluated value for the property */
173-
OriginalValue /*< return the original, user set value */
174-
};
175-
176136
/**Constructor
177137
@param composition parent composition
178138
@param manageZValue true if the z-Value of this object should be managed by mComposition*/
@@ -259,21 +219,9 @@ class QgsComposerItem : QObject, QGraphicsRectItem
259219
corresponds to 1 scene size unit*/
260220
virtual void setSceneRect( const QRectF& rectangle );
261221

262-
/** stores state in Dom element
263-
* @param elem is Dom element corresponding to 'Composer' tag
264-
* @param doc is the Dom document
265-
*/
266-
virtual bool writeXML( QDomElement& elem, QDomDocument & doc ) const = 0;
267-
268222
/**Writes parameter that are not subclass specific in document. Usually called from writeXML methods of subclasses*/
269223
bool _writeXML( QDomElement& itemElem, QDomDocument& doc ) const;
270224

271-
/** sets state from Dom document
272-
* @param itemElem is Dom node corresponding to item tag
273-
* @param doc is Dom document
274-
*/
275-
virtual bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) = 0;
276-
277225
/**Reads parameter that are not subclass specific in document. Usually called from readXML methods of subclasses*/
278226
bool _readXML( const QDomElement& itemElem, const QDomDocument& doc );
279227

@@ -394,8 +342,6 @@ class QgsComposerItem : QObject, QGraphicsRectItem
394342
virtual void addItem( QgsComposerItem* item );
395343
virtual void removeItems();
396344

397-
const QgsComposition* composition() const;
398-
399345
virtual void beginItemCommand( const QString& text );
400346

401347
/**Starts new composer undo command
@@ -466,7 +412,7 @@ class QgsComposerItem : QObject, QGraphicsRectItem
466412
* settings).
467413
* @note this method was added in version 2.1
468414
*/
469-
double itemRotation( PropertyValueType valueType = EvaluatedValue ) const;
415+
double itemRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
470416

471417
/**Returns the rotation for the composer item
472418
* @deprecated Use itemRotation()
@@ -517,22 +463,6 @@ class QgsComposerItem : QObject, QGraphicsRectItem
517463
*/
518464
void setCurrentExportLayer( int layerIdx = -1 );
519465

520-
/**Returns a reference to the data defined settings for one of the item's data defined properties.
521-
* @param property data defined property to return
522-
* @note this method was added in version 2.5
523-
*/
524-
QgsDataDefined* dataDefinedProperty( DataDefinedProperty property );
525-
526-
/**Sets parameters for a data defined property for the item
527-
* @param property data defined property to set
528-
* @param active true if data defined property is active, false if it is disabled
529-
* @param useExpression true if the expression should be used
530-
* @param expression expression for data defined property
531-
* @field field name if the data defined property should take its value from a field
532-
* @note this method was added in version 2.5
533-
*/
534-
void setDataDefinedProperty( DataDefinedProperty property, bool active, bool useExpression, const QString &expression, const QString &field );
535-
536466
public slots:
537467
/**Sets the item rotation
538468
* @deprecated Use setItemRotation( double rotation ) instead
@@ -556,7 +486,7 @@ class QgsComposerItem : QObject, QGraphicsRectItem
556486
* refreshed.
557487
* @note this method was added in version 2.5
558488
*/
559-
virtual void refreshDataDefinedProperty( DataDefinedProperty property = AllProperties );
489+
virtual void refreshDataDefinedProperty( QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties );
560490

561491
protected:
562492

@@ -597,43 +527,52 @@ class QgsComposerItem : QObject, QGraphicsRectItem
597527

598528
//some utility functions
599529

600-
/**Calculates width and hight of the picture (in mm) such that it fits into the item frame with the given rotation*/
601-
bool imageSizeConsideringRotation( double& width, double& height, double rotation ) const;
530+
/**Calculates width and hight of the picture (in mm) such that it fits into the item frame with the given rotation.
531+
* @deprecated will be removed in QGIS 3.0
532+
*/
533+
bool imageSizeConsideringRotation( double& width, double& height, double rotation ) const /Deprecated/;
534+
602535
/**Calculates width and hight of the picture (in mm) such that it fits into the item frame with the given rotation
603-
* @deprecated Use bool imageSizeConsideringRotation( double& width, double& height, double rotation )
604-
* instead
536+
* @deprecated will be removed in QGIS 3.0
605537
*/
606538
bool imageSizeConsideringRotation( double& width, double& height ) const /Deprecated/;
607539

608540
/**Calculates the largest scaled version of originalRect which fits within boundsRect, when it is rotated by
609-
* a specified amount
610-
@param originalRect QRectF to be rotated and scaled
611-
@param boundsRect QRectF specifying the bounds which the rotated and scaled rectangle must fit within
612-
@param rotation the rotation in degrees to be applied to the rectangle
613-
*/
614-
QRectF largestRotatedRectWithinBounds( QRectF originalRect, QRectF boundsRect, double rotation ) const;
541+
* a specified amount
542+
* @param originalRect QRectF to be rotated and scaled
543+
* @param boundsRect QRectF specifying the bounds which the rotated and scaled rectangle must fit within
544+
* @param rotation the rotation in degrees to be applied to the rectangle
545+
* @deprecated use QgsComposerUtils::largestRotatedRectWithinBounds instead
546+
*/
547+
QRectF largestRotatedRectWithinBounds( QRectF originalRect, QRectF boundsRect, double rotation ) const /Deprecated/;
615548

616-
/**Calculates corner point after rotation and scaling*/
617-
bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height, double rotation ) const;
618549
/**Calculates corner point after rotation and scaling
619-
* @deprecated Use bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height, double rotation )
620-
* instead
550+
* @deprecated will be removed in QGIS 3.0
551+
*/
552+
bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height, double rotation ) const /Deprecated/;
553+
554+
/**Calculates corner point after rotation and scaling
555+
* @deprecated will be removed in QGIS 3.0
621556
*/
622557
bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const /Deprecated/;
623558

624-
/**Calculates width / height of the bounding box of a rotated rectangle*/
625-
void sizeChangedByRotation( double& width, double& height, double rotation );
559+
/**Calculates corner point after rotation and scaling
560+
* @deprecated will be removed in QGIS 3.0
561+
*/
562+
void sizeChangedByRotation( double& width, double& height, double rotation ) /Deprecated/;
563+
626564
/**Calculates width / height of the bounding box of a rotated rectangle
627-
* @deprecated Use void sizeChangedByRotation( double& width, double& height, double rotation )
628-
* instead
565+
* @deprecated will be removed in QGIS 3.0
629566
*/
630567
void sizeChangedByRotation( double& width, double& height ) /Deprecated/;
631-
568+
632569
/**Rotates a point / vector
633-
@param angle rotation angle in degrees, counterclockwise
634-
@param x in/out: x coordinate before / after the rotation
635-
@param y in/out: y cooreinate before / after the rotation*/
636-
void rotate( double angle, double& x, double& y ) const;
570+
* @param angle rotation angle in degrees, counterclockwise
571+
* @param x in/out: x coordinate before / after the rotation
572+
* @param y in/out: y cooreinate before / after the rotation
573+
* @deprecated use QgsComposerUtils:rotate instead
574+
*/
575+
void rotate( double angle, double& x, double& y ) const /Deprecated/;
637576

638577
/**Return horizontal align snap item. Creates a new graphics line if 0*/
639578
QGraphicsLineItem* hAlignSnapItem();
@@ -643,22 +582,12 @@ class QgsComposerItem : QObject, QGraphicsRectItem
643582
void deleteVAlignSnapItem();
644583
void deleteAlignItems();
645584

646-
/**Evaluate a data defined property and return the calculated value
647-
* @returns true if data defined property could be successfully evaluated
648-
* @param property data defined property to evaluate
649-
* @param expressionValue QVariant for storing the evaluated value
650-
* @note this method was added in version 2.5
651-
*/
652-
bool dataDefinedEvaluate( QgsComposerItem::DataDefinedProperty property, QVariant &expressionValue );
653-
654585
/**Update an item rect to consider data defined position and size of item*/
655586
QRectF evalItemRect( const QRectF &newRect );
656587

657588
signals:
658589
/**Is emitted on item rotation change*/
659590
void itemRotationChanged( double newRotation );
660-
/**Used e.g. by the item widgets to update the gui elements*/
661-
void itemChanged();
662591
/**Emitted if the rectangle changes*/
663592
void sizeChanged();
664593
/**Emitted if the item's frame style changes

python/core/composer/qgscomposermap.sip

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ class QgsComposerMap : QgsComposerItem
348348
* or the current evaluated rotation (which may be affected by data driven rotation
349349
* settings).
350350
*/
351-
double mapRotation( PropertyValueType valueType = EvaluatedValue ) const;
351+
// double mapRotation( QgsComposerBaseItem::PropertyValueType valueType = QgsComposerBaseItem::EvaluatedValue ) const;
352352

353353
void updateItem();
354354

@@ -513,5 +513,5 @@ class QgsComposerMap : QgsComposerItem
513513

514514
void overviewExtentChanged();
515515

516-
virtual void refreshDataDefinedProperty( DataDefinedProperty property = AllProperties );
516+
virtual void refreshDataDefinedProperty( QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties );
517517
};

python/core/composer/qgscomposermultiframe.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
/**Abstract base class for composer entries with the ability to distribute the content to several frames (items)*/
3-
class QgsComposerMultiFrame: QObject
3+
class QgsComposerMultiFrame: QgsComposerObject
44
{
55
%TypeHeaderCode
66
#include "qgscomposermultiframe.h"
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/** \ingroup MapComposer
2+
* A item that forms part of a map composition.
3+
*/
4+
class QgsComposerObject : QObject
5+
{
6+
%TypeHeaderCode
7+
#include <qgscomposerobject.h>
8+
%End
9+
10+
public:
11+
12+
/** Data defined properties for different item types
13+
*/
14+
enum DataDefinedProperty
15+
{
16+
NoProperty = 0, /*< no property */
17+
AllProperties, /*< all properties for item */
18+
//composer page properties
19+
PresetPaperSize, /*< preset paper size for composition */
20+
PaperWidth, /*< paper width */
21+
PaperHeight, /*< paper height */
22+
NumPages, /*< number of pages in composition */
23+
PaperOrientation, /*< paper orientation */
24+
//general composer item properties
25+
PageNumber, /*< page number for item placement */
26+
PositionX, /*< x position on page */
27+
PositionY, /*< y position on page */
28+
ItemWidth, /*< width of item */
29+
ItemHeight, /*< height of item */
30+
ItemRotation, /*< rotation of item */
31+
Transparency, /*< item transparency */
32+
BlendMode, /*< item blend mode */
33+
//composer map
34+
MapRotation, /*< map rotation */
35+
MapScale, /*< map scale */
36+
MapXMin, /*< map extent x minimum */
37+
MapYMin, /*< map extent y minimum */
38+
MapXMax, /*< map extent x maximum */
39+
MapYMax /*< map extent y maximum */
40+
};
41+
42+
/** Specifies whether the value returned by a function should be the original, user
43+
* set value, or the current evaluated value for the property. This may differ if
44+
* a property has a data defined expression active.
45+
*/
46+
enum PropertyValueType
47+
{
48+
EvaluatedValue = 0, /*< return the current evaluated value for the property */
49+
OriginalValue /*< return the original, user set value */
50+
};
51+
52+
/**Constructor
53+
* @param composition parent composition
54+
*/
55+
QgsComposerObject( QgsComposition* composition );
56+
virtual ~QgsComposerObject();
57+
58+
/**Returns the composition the item is attached to.
59+
* @returns QgsComposition for item.
60+
*/
61+
const QgsComposition* composition() const;
62+
63+
/**Stores item state in DOM element
64+
* @param elem is DOM element corresponding to 'Composer' tag
65+
* @param doc is the DOM document
66+
*/
67+
virtual bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
68+
69+
/**Sets item state from DOM element
70+
* @param itemElem is DOM node corresponding to item tag
71+
* @param doc is DOM document
72+
*/
73+
virtual bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
74+
75+
/**Returns a reference to the data defined settings for one of the item's data defined properties.
76+
* @param property data defined property to return
77+
* @note this method was added in version 2.5
78+
*/
79+
QgsDataDefined* dataDefinedProperty( const DataDefinedProperty property );
80+
81+
/**Sets parameters for a data defined property for the item
82+
* @param property data defined property to set
83+
* @param active true if data defined property is active, false if it is disabled
84+
* @param useExpression true if the expression should be used
85+
* @param expression expression for data defined property
86+
* @field field name if the data defined property should take its value from a field
87+
* @note this method was added in version 2.5
88+
*/
89+
void setDataDefinedProperty( const DataDefinedProperty property, const bool active, const bool useExpression, const QString &expression, const QString &field );
90+
91+
public slots:
92+
93+
/**Triggers a redraw for the item*/
94+
virtual void repaint();
95+
96+
protected:
97+
98+
/**Evaluate a data defined property and return the calculated value
99+
* @returns true if data defined property could be successfully evaluated
100+
* @param property data defined property to evaluate
101+
* @param expressionValue QVariant for storing the evaluated value
102+
* @note this method was added in version 2.5
103+
*/
104+
bool dataDefinedEvaluate( QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue );
105+
106+
signals:
107+
/**Emitted when the item changes. Signifies that the item widgets must update the
108+
* gui elements.
109+
*/
110+
void itemChanged();
111+
112+
};

0 commit comments

Comments
 (0)