QGIS API Documentation  2.99.0-Master (dcec6bb)
qgscomposeritem.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposeritem.h
3  -------------------
4  begin : January 2005
5  copyright : (C) 2005 by Radim Blazek
6  email : [email protected]
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 #ifndef QGSCOMPOSERITEM_H
18 #define QGSCOMPOSERITEM_H
19 
20 #include "qgis_core.h"
21 #include "qgis.h"
22 #include "qgscomposeritemcommand.h"
23 #include "qgscomposerobject.h"
24 #include <QGraphicsRectItem>
25 #include <QObject>
26 #include <QPainter>
27 
28 class QWidget;
29 class QDomDocument;
30 class QDomElement;
31 class QGraphicsLineItem;
33 class QgsComposition;
35 class QgsComposerEffect;
36 
40 class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRectItem
41 {
42 #ifdef SIP_RUN
43 #include <qgscomposerarrow.h>
44 #include <qgscomposerframe.h>
45 #include <qgscomposeritemgroup.h>
46 #include <qgscomposerlabel.h>
47 #include <qgscomposerlegend.h>
48 #include <qgscomposermap.h>
49 #include <qgspaperitem.h>
50 #include <qgscomposerpicture.h>
51 #include <qgscomposerscalebar.h>
52 #include <qgscomposershape.h>
53 #include <qgscomposerpolygon.h>
54 #include <qgscomposerpolyline.h>
55 #include <qgscomposertexttable.h>
56 #endif
57 
58 
59 #ifdef SIP_RUN
61  // the conversions have to be static, because they're using multiple inheritance
62  // (seen in PyQt4 .sip files for some QGraphicsItem classes)
63  switch ( sipCpp->type() )
64  {
66  sipType = sipType_QgsComposerItem;
67  *sipCppRet = static_cast<QgsComposerItem *>( sipCpp );
68  break;
70  sipType = sipType_QgsComposerArrow;
71  *sipCppRet = static_cast<QgsComposerArrow *>( sipCpp );
72  break;
74  sipType = sipType_QgsComposerItemGroup;
75  *sipCppRet = static_cast<QgsComposerItemGroup *>( sipCpp );
76  break;
78  sipType = sipType_QgsComposerLabel;
79  *sipCppRet = static_cast<QgsComposerLabel *>( sipCpp );
80  break;
82  sipType = sipType_QgsComposerLegend;
83  *sipCppRet = static_cast<QgsComposerLegend *>( sipCpp );
84  break;
86  sipType = sipType_QgsComposerMap;
87  *sipCppRet = static_cast<QgsComposerMap *>( sipCpp );
88  break;
90  sipType = sipType_QgsPaperItem;
91  *sipCppRet = static_cast<QgsPaperItem *>( sipCpp );
92  break;
94  sipType = sipType_QgsComposerPicture;
95  *sipCppRet = static_cast<QgsComposerPicture *>( sipCpp );
96  break;
98  sipType = sipType_QgsComposerScaleBar;
99  *sipCppRet = static_cast<QgsComposerScaleBar *>( sipCpp );
100  break;
102  sipType = sipType_QgsComposerShape;
103  *sipCppRet = static_cast<QgsComposerShape *>( sipCpp );
104  break;
106  sipType = sipType_QgsComposerPolygon;
107  *sipCppRet = static_cast<QgsComposerPolygon *>( sipCpp );
108  break;
110  sipType = sipType_QgsComposerPolyline;
111  *sipCppRet = static_cast<QgsComposerPolyline *>( sipCpp );
112  break;
114  sipType = sipType_QgsComposerFrame;
115  *sipCppRet = static_cast<QgsComposerFrame *>( sipCpp );
116  break;
117  default:
118  sipType = 0;
119  }
120  SIP_END
121 #endif
122 
123 
124  Q_OBJECT
125  public:
126 
127  enum ItemType
128  {
129  // base class for the items
130  ComposerItem = UserType + 100,
131 
132  // derived classes
138  ComposerPaper, // QgsPaperItem
147  ComposerFrame
148  };
149 
152  {
162  NoAction
163  };
164 
166  {
175  LowerRight
176  };
177 
178  //note - must sync with QgsMapCanvas::WheelAction.
179  //TODO - QGIS 3.0 move QgsMapCanvas::WheelAction from GUI->CORE and remove this enum
180 
183  enum ZoomMode
184  {
185  Zoom = 0,
188  NoZoom
189  };
190 
194  QgsComposerItem( QgsComposition *composition SIP_TRANSFERTHIS, bool manageZValue = true );
195 
203  QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition *composition SIP_TRANSFERTHIS, bool manageZValue = true );
204  virtual ~QgsComposerItem();
205 
207  virtual int type() const override { return ComposerItem; }
208 
216  virtual bool isRemoved() const { return mRemovedFromComposition; }
217 
225  void setIsRemoved( const bool removed ) { mRemovedFromComposition = removed; }
226 
228  virtual void setSelected( bool s );
229 
231  virtual bool selected() const { return QGraphicsRectItem::isSelected(); }
232 
234  void move( double dx, double dy );
235 
239  virtual void moveContent( double dx, double dy ) { Q_UNUSED( dx ); Q_UNUSED( dy ); }
240 
247  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) { Q_UNUSED( factor ); Q_UNUSED( point ); Q_UNUSED( mode ); }
248 
255  int page() const;
256 
263  QPointF pagePos() const;
264 
273  void updatePagePos( double newPageWidth, double newPageHeight );
274 
282  void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft, int page = -1 );
283 
294  void setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint = UpperLeft, bool posIncludesFrame = false, int page = -1 );
295 
298  ItemPositionMode lastUsedPositionMode() { return mLastUsedPositionMode; }
299 
302  virtual void setSceneRect( const QRectF &rectangle );
303 
305  bool _writeXml( QDomElement &itemElem, QDomDocument &doc ) const;
306 
308  bool _readXml( const QDomElement &itemElem, const QDomDocument &doc );
309 
317  bool hasFrame() const {return mFrame;}
318 
326  virtual void setFrameEnabled( const bool drawFrame );
327 
336  virtual void setFrameStrokeColor( const QColor &color );
337 
346  QColor frameStrokeColor() const { return mFrameColor; }
347 
356  virtual void setFrameStrokeWidth( const double strokeWidth );
357 
366  double frameStrokeWidth() const { return mFrameWidth; }
367 
376  Qt::PenJoinStyle frameJoinStyle() const { return mFrameJoinStyle; }
377 
386  void setFrameJoinStyle( const Qt::PenJoinStyle style );
387 
395  virtual double estimatedFrameBleed() const;
396 
405  virtual QRectF rectWithFrame() const;
406 
412  bool hasBackground() const {return mBackground;}
413 
420  void setBackgroundEnabled( const bool drawBackground ) { mBackground = drawBackground; }
421 
427  QColor backgroundColor() const { return mBackgroundColor; }
428 
435  void setBackgroundColor( const QColor &backgroundColor );
436 
441  QPainter::CompositionMode blendMode() const { return mBlendMode; }
442 
447  void setBlendMode( const QPainter::CompositionMode blendMode );
448 
455  double itemOpacity() const { return mOpacity; }
456 
463  void setItemOpacity( const double opacity );
464 
471  bool effectsEnabled() const { return mEffectsEnabled; }
472 
479  void setEffectsEnabled( const bool effectsEnabled );
480 
482  virtual void addItem( QgsComposerItem *item ) { Q_UNUSED( item ); }
483  virtual void removeItems() {}
484 
485  virtual void beginItemCommand( const QString &text ) { beginCommand( text ); }
486 
490  void beginCommand( const QString &commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
491 
492  virtual void endItemCommand() { endCommand(); }
494  void endCommand();
495  void cancelCommand();
496 
497  //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
498  //painter down by the same factor for drawing
499 
504  void setPositionLock( const bool lock );
505 
510  bool positionLock() const { return mItemPositionLocked; }
511 
520  double itemRotation( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
521 
527  virtual void updateItem();
528 
533  QString id() const { return mId; }
534 
539  virtual void setId( const QString &id );
540 
547  QString uuid() const { return mUuid; }
548 
556  virtual QString displayName() const;
557 
565  virtual void setVisibility( const bool visible );
566 
574  bool excludeFromExports( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
575 
581  virtual void setExcludeFromExports( const bool exclude );
582 
588  bool isGroupMember() const { return mIsGroupMember; }
589 
595  void setIsGroupMember( const bool isGroupMember );
596 
603  virtual int numberExportLayers() const { return 0; }
604 
610  virtual void setCurrentExportLayer( const int layerIdx = -1 ) { mCurrentExportLayer = layerIdx; }
611 
616  virtual QgsExpressionContext createExpressionContext() const override;
617 
626  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
627 
636  bool updatesEnabled() const { return mUpdatesEnabled; }
637 
638  public slots:
639 
648  virtual void setItemRotation( const double rotation, const bool adjustPosition = false );
649 
650  void repaint() override;
651 
660  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
661 
662  protected:
665 
671 
673  QGraphicsRectItem *mBoundingResizeRectangle = nullptr;
674  QGraphicsLineItem *mHAlignSnapItem = nullptr;
675  QGraphicsLineItem *mVAlignSnapItem = nullptr;
676 
678  bool mFrame;
680  QColor mFrameColor;
682  double mFrameWidth = 0.3;
684  Qt::PenJoinStyle mFrameJoinStyle = Qt::MiterJoin;
685 
690 
694 
697 
700 
705 
707  QPainter::CompositionMode mBlendMode;
709  QgsComposerEffect *mEffect = nullptr;
710 
712  double mOpacity = 1.0;
713 
716 
721 
724 
727 
733 
737  virtual void drawSelectionBoxes( QPainter *p );
738 
740  virtual void drawFrame( QPainter *p );
741 
743  virtual void drawBackground( QPainter *p );
744 
747  double rectHandlerBorderTolerance() const;
748 
752  double horizontalViewScaleFactor() const;
753 
754  //some utility functions
755 
757  QGraphicsLineItem *hAlignSnapItem();
758  void deleteHAlignSnapItem();
760  QGraphicsLineItem *vAlignSnapItem();
761  void deleteVAlignSnapItem();
762  void deleteAlignItems();
763 
775  QRectF evalItemRect( const QRectF &newRect, const bool resizeOnly = false, const QgsExpressionContext *context = nullptr );
776 
781  bool shouldDrawItem() const;
782 
783  signals:
785  void itemRotationChanged( double newRotation );
787  void sizeChanged();
788 
792  void frameChanged();
793 
797  void lockChanged();
798 
799  private:
800  // id (not unique)
801  QString mId;
802  // name (unique)
803  QString mUuid;
804  // name (temporary when loaded from template)
805  QString mTemplateUuid;
806  // true if composition manages the z value for this item
807  bool mCompositionManagesZValue;
808 
813  bool mUpdatesEnabled = true;
814 
822  void refreshRotation( const bool updateItem = true, const bool rotateAroundCenter = false, const QgsExpressionContext &context = QgsExpressionContext() );
823 
830  void refreshOpacity( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
831 
837  void refreshFrameColor( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
838 
844  void refreshBackgroundColor( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
845 
849  void refreshBlendMode( const QgsExpressionContext &context );
850 
851  void init( const bool manageZValue );
852 
853  friend class QgsComposerItemGroup; // to access mTemplateUuid
854 };
855 
856 #endif
bool mExcludeFromExports
Whether item should be excluded in exports.
Item representing the paper.
Definition: qgspaperitem.h:44
A scale bar item that can be added to a map composition.
A base class for objects which belong to a map composition.
Composer item for polylines.
bool isGroupMember() const
Returns whether this item is part of a group.
virtual QgsExpressionContext createExpressionContext() const
Creates an expression context relating to the objects&#39; current state.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
An item that draws an arrow between two points.
QPointF mLastMouseEventPos
Position of the last mouse move event (in scene coordinates)
virtual void beginItemCommand(const QString &text)
ItemPositionMode lastUsedPositionMode()
Returns item&#39;s last used position mode.
bool effectsEnabled() const
Returns whether effects (e.g., blend modes) are enabled for the item.
virtual void removeItems()
ZoomMode
Modes for zooming item content.
A item that forms part of a map composition.
QgsComposerItem::MouseMoveAction mCurrentMouseMoveAction
QColor frameStrokeColor() const
Returns the frame&#39;s stroke color.
Zoom to center of content.
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...
virtual int numberExportLayers() const
Get the number of layers that this item requires for exporting as layers.
double mLastValidViewScaleFactor
Backup to restore item appearance if no view scale factor is available.
ItemPositionMode mLastUsedPositionMode
The item&#39;s position mode.
A container for grouping several QgsComposerItems.
QColor backgroundColor() const
Gets the background color for this item.
virtual void zoomContent(const double factor, const QPointF point, const ZoomMode mode=QgsComposerItem::Zoom)
Zoom content of item.
double itemOpacity() const
Returns the item&#39;s opacity.
bool updatesEnabled() const
Returns whether updates to the item are enabled.
QPainter::CompositionMode mBlendMode
Composition blend mode for item.
DataDefinedProperty
Data defined properties for different item types.
A composer class that displays svg files or raster format (jpg, png, ...)
virtual void moveContent(double dx, double dy)
Move Content of item.
virtual void setCurrentExportLayer(const int layerIdx=-1)
Sets the current layer to draw for exporting.
void setIsRemoved(const bool removed)
Sets whether this item has been removed from the composition.
bool mFrame
True if item fram needs to be painted.
virtual bool selected() const
Is selected.
MouseMoveAction
Describes the action (move or resize in different directon) to be done during mouse move...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
#define SIP_END
Definition: qgis_sip.h:175
double mEvaluatedItemRotation
Temporary evaluated item rotation in degrees, clockwise.
bool hasBackground() const
Whether this item has a Background or not.
virtual void repaint()
Triggers a redraw for the item.
bool mRemovedFromComposition
True if item has been removed from the composition.
QString uuid() const
Get item identification name.
PropertyValueType
Specifies whether the value returned by a function should be the original, user set value...
QPointF mMouseMoveStartPos
Start point of the last mouse move action (in scene coordinates)
void setBackgroundEnabled(const bool drawBackground)
Set whether this item has a Background drawn around it or not.
Graphics scene for map printing.
Object representing map window.
Frame item for a composer multiframe item.
Return the current evaluated value for the property.
virtual bool isRemoved() const
Returns whether this item has been removed from the composition.
QColor mBackgroundColor
Background color.
int mCurrentExportLayer
The layer that needs to be exported.
A composer items that draws common shapes (ellipse, triangle, rectangle)
Composer item for polygons.
virtual void endItemCommand()
virtual void addItem(QgsComposerItem *item)
Composite operations for item groups do nothing per default.
bool mItemPositionLocked
True if item position and size cannot be changed with mouse move.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
A label that can be placed onto a map composition.
void setUpdatesEnabled(bool enabled)
Sets whether updates to the item are enabled.
bool hasFrame() const
Whether this item has a frame or not.
bool positionLock() const
Returns whether position lock for mouse drags is enabled returns true if item is locked for mouse mov...
bool mBackground
True if item background needs to be painted.
A legend that can be placed onto a map composition.
QPainter::CompositionMode blendMode() const
Returns the item&#39;s composition blending mode.
bool mIsGroupMember
Whether or not this item is part of a group.
QString id() const
Get item&#39;s id (which is not necessarly unique)
Zoom and recenter content to point.
double mItemRotation
Item rotation in degrees, clockwise.
QColor mFrameColor
Item frame color.
bool mEvaluatedExcludeFromExports
Temporary evaluated item exclusion.
All properties for item.
virtual int type() const override
Return correct graphics item type.
Qt::PenJoinStyle frameJoinStyle() const
Returns the join style used for drawing the item&#39;s frame.
Zoom while maintaining relative position of point.
double frameStrokeWidth() const
Returns the frame&#39;s stroke width.