QGIS API Documentation  2.99.0-Master (19b062c)
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 : blazek@itc.it
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 
41 class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRectItem
42 {
43 #ifdef SIP_RUN
44 #include <qgscomposerarrow.h>
45 #include <qgscomposerframe.h>
46 #include <qgscomposeritemgroup.h>
47 #include <qgscomposerlabel.h>
48 #include <qgscomposerlegend.h>
49 #include <qgscomposermap.h>
50 #include <qgspaperitem.h>
51 #include <qgscomposerpicture.h>
52 #include <qgscomposerscalebar.h>
53 #include <qgscomposershape.h>
54 #include <qgscomposerpolygon.h>
55 #include <qgscomposerpolyline.h>
56 #include <qgscomposertexttable.h>
57 #include <qgslayoutitemshape.h>
58 #include <qgslayoutitempage.h>
59 #endif
60 
61 
62 #ifdef SIP_RUN
64  // the conversions have to be static, because they're using multiple inheritance
65  // (seen in PyQt4 .sip files for some QGraphicsItem classes)
66  if ( dynamic_cast< QgsComposerItem * >( sipCpp ) )
67  {
68  switch ( sipCpp->type() )
69  {
71  sipType = sipType_QgsComposerItem;
72  *sipCppRet = static_cast<QgsComposerItem *>( sipCpp );
73  break;
75  sipType = sipType_QgsComposerArrow;
76  *sipCppRet = static_cast<QgsComposerArrow *>( sipCpp );
77  break;
79  sipType = sipType_QgsComposerItemGroup;
80  *sipCppRet = static_cast<QgsComposerItemGroup *>( sipCpp );
81  break;
83  sipType = sipType_QgsComposerLabel;
84  *sipCppRet = static_cast<QgsComposerLabel *>( sipCpp );
85  break;
87  sipType = sipType_QgsComposerLegend;
88  *sipCppRet = static_cast<QgsComposerLegend *>( sipCpp );
89  break;
91  sipType = sipType_QgsComposerMap;
92  *sipCppRet = static_cast<QgsComposerMap *>( sipCpp );
93  break;
95  sipType = sipType_QgsPaperItem;
96  *sipCppRet = static_cast<QgsPaperItem *>( sipCpp );
97  break;
99  sipType = sipType_QgsComposerPicture;
100  *sipCppRet = static_cast<QgsComposerPicture *>( sipCpp );
101  break;
103  sipType = sipType_QgsComposerScaleBar;
104  *sipCppRet = static_cast<QgsComposerScaleBar *>( sipCpp );
105  break;
107  sipType = sipType_QgsComposerShape;
108  *sipCppRet = static_cast<QgsComposerShape *>( sipCpp );
109  break;
111  sipType = sipType_QgsComposerPolygon;
112  *sipCppRet = static_cast<QgsComposerPolygon *>( sipCpp );
113  break;
115  sipType = sipType_QgsComposerPolyline;
116  *sipCppRet = static_cast<QgsComposerPolyline *>( sipCpp );
117  break;
119  sipType = sipType_QgsComposerFrame;
120  *sipCppRet = static_cast<QgsComposerFrame *>( sipCpp );
121  break;
122  default:
123  sipType = 0;
124  }
125  }
126  else
127  {
128  switch ( sipCpp->type() )
129  {
130  // really, these *should* use the constants from QgsLayoutItemRegistry, but sip doesn't like that!
131  case QGraphicsItem::UserType + 101:
132  sipType = sipType_QgsLayoutItemPage;
133  *sipCppRet = static_cast<QgsLayoutItemPage *>( sipCpp );
134  break;
135  default:
136  sipType = 0;
137  }
138  }
139 
140  SIP_END
141 #endif
142 
143 
144  Q_OBJECT
145  public:
146 
147  enum ItemType
148  {
149  // base class for the items
150  ComposerItem = UserType + 100,
151 
152  // derived classes
158  ComposerPaper, // QgsPaperItem
167  ComposerFrame
168  };
169 
172  {
182  NoAction
183  };
184 
186  {
195  LowerRight
196  };
197 
198  //note - must sync with QgsMapCanvas::WheelAction.
199  //TODO - QGIS 3.0 move QgsMapCanvas::WheelAction from GUI->CORE and remove this enum
200 
204  enum ZoomMode
205  {
206  Zoom = 0,
209  NoZoom
210  };
211 
216  QgsComposerItem( QgsComposition *composition SIP_TRANSFERTHIS, bool manageZValue = true );
217 
226  QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition *composition SIP_TRANSFERTHIS, bool manageZValue = true );
227  virtual ~QgsComposerItem();
228 
230  virtual int type() const override { return ComposerItem; }
231 
240  virtual bool isRemoved() const { return mRemovedFromComposition; }
241 
250  void setIsRemoved( const bool removed ) { mRemovedFromComposition = removed; }
251 
253  virtual void setSelected( bool s );
254 
256  virtual bool selected() const { return QGraphicsRectItem::isSelected(); }
257 
259  void move( double dx, double dy );
260 
265  virtual void moveContent( double dx, double dy ) { Q_UNUSED( dx ); Q_UNUSED( dy ); }
266 
274  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) { Q_UNUSED( factor ); Q_UNUSED( point ); Q_UNUSED( mode ); }
275 
283  int page() const;
284 
292  QPointF pagePos() const;
293 
303  void updatePagePos( double newPageWidth, double newPageHeight );
304 
313  void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft, int page = -1 );
314 
326  void setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint = UpperLeft, bool posIncludesFrame = false, int page = -1 );
327 
331  ItemPositionMode lastUsedPositionMode() { return mLastUsedPositionMode; }
332 
336  virtual void setSceneRect( const QRectF &rectangle );
337 
339  bool _writeXml( QDomElement &itemElem, QDomDocument &doc ) const;
340 
342  bool _readXml( const QDomElement &itemElem, const QDomDocument &doc );
343 
352  bool hasFrame() const {return mFrame;}
353 
362  virtual void setFrameEnabled( const bool drawFrame );
363 
373  virtual void setFrameStrokeColor( const QColor &color );
374 
384  QColor frameStrokeColor() const { return mFrameColor; }
385 
395  virtual void setFrameStrokeWidth( const double strokeWidth );
396 
406  double frameStrokeWidth() const { return mFrameWidth; }
407 
417  Qt::PenJoinStyle frameJoinStyle() const { return mFrameJoinStyle; }
418 
428  void setFrameJoinStyle( const Qt::PenJoinStyle style );
429 
438  virtual double estimatedFrameBleed() const;
439 
449  virtual QRectF rectWithFrame() const;
450 
457  bool hasBackground() const {return mBackground;}
458 
466  void setBackgroundEnabled( const bool drawBackground ) { mBackground = drawBackground; }
467 
474  QColor backgroundColor() const { return mBackgroundColor; }
475 
483  void setBackgroundColor( const QColor &backgroundColor );
484 
490  QPainter::CompositionMode blendMode() const { return mBlendMode; }
491 
497  void setBlendMode( const QPainter::CompositionMode blendMode );
498 
506  double itemOpacity() const { return mOpacity; }
507 
515  void setItemOpacity( const double opacity );
516 
524  bool effectsEnabled() const { return mEffectsEnabled; }
525 
533  void setEffectsEnabled( const bool effectsEnabled );
534 
536  virtual void addItem( QgsComposerItem *item ) { Q_UNUSED( item ); }
537  virtual void removeItems() {}
538 
539  virtual void beginItemCommand( const QString &text ) { beginCommand( text ); }
540 
545  void beginCommand( const QString &commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
546 
547  virtual void endItemCommand() { endCommand(); }
549  void endCommand();
550  void cancelCommand();
551 
552  //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
553  //painter down by the same factor for drawing
554 
560  void setPositionLock( const bool lock );
561 
567  bool positionLock() const { return mItemPositionLocked; }
568 
577  double itemRotation( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
578 
584  virtual void updateItem();
585 
591  QString id() const { return mId; }
592 
598  virtual void setId( const QString &id );
599 
607  QString uuid() const { return mUuid; }
608 
617  virtual QString displayName() const;
618 
627  virtual void setVisibility( const bool visible );
628 
637  bool excludeFromExports( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
638 
645  virtual void setExcludeFromExports( const bool exclude );
646 
653  bool isGroupMember() const { return mIsGroupMember; }
654 
661  void setIsGroupMember( const bool isGroupMember );
662 
670  virtual int numberExportLayers() const { return 0; }
671 
678  virtual void setCurrentExportLayer( const int layerIdx = -1 ) { mCurrentExportLayer = layerIdx; }
679 
685  virtual QgsExpressionContext createExpressionContext() const override;
686 
695  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
696 
705  bool updatesEnabled() const { return mUpdatesEnabled; }
706 
707  public slots:
708 
717  virtual void setItemRotation( const double rotation, const bool adjustPosition = false );
718 
719  void repaint() override;
720 
730  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
731 
732  protected:
735 
741 
743  QGraphicsRectItem *mBoundingResizeRectangle = nullptr;
744  QGraphicsLineItem *mHAlignSnapItem = nullptr;
745  QGraphicsLineItem *mVAlignSnapItem = nullptr;
746 
748  bool mFrame;
750  QColor mFrameColor;
752  double mFrameWidth = 0.3;
754  Qt::PenJoinStyle mFrameJoinStyle = Qt::MiterJoin;
755 
760 
765 
768 
771 
777 
779  QPainter::CompositionMode mBlendMode;
781  QgsComposerEffect *mEffect = nullptr;
782 
784  double mOpacity = 1.0;
785 
788 
794 
797 
800 
807 
812  virtual void drawSelectionBoxes( QPainter *p );
813 
815  virtual void drawFrame( QPainter *p );
816 
818  virtual void drawBackground( QPainter *p );
819 
823  double rectHandlerBorderTolerance() const;
824 
829  double horizontalViewScaleFactor() const;
830 
831  //some utility functions
832 
834  QGraphicsLineItem *hAlignSnapItem();
835  void deleteHAlignSnapItem();
837  QGraphicsLineItem *vAlignSnapItem();
838  void deleteVAlignSnapItem();
839  void deleteAlignItems();
840 
853  QRectF evalItemRect( const QRectF &newRect, const bool resizeOnly = false, const QgsExpressionContext *context = nullptr );
854 
860  bool shouldDrawItem() const;
861 
862  signals:
864  void itemRotationChanged( double newRotation );
866  void sizeChanged();
867 
872  void frameChanged();
873 
878  void lockChanged();
879 
880  private:
881  // id (not unique)
882  QString mId;
883  // name (unique)
884  QString mUuid;
885  // name (temporary when loaded from template)
886  QString mTemplateUuid;
887  // true if composition manages the z value for this item
888  bool mCompositionManagesZValue;
889 
894  bool mUpdatesEnabled = true;
895 
904  void refreshRotation( const bool updateItem = true, const bool rotateAroundCenter = false, const QgsExpressionContext &context = QgsExpressionContext() );
905 
913  void refreshOpacity( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
914 
921  void refreshFrameColor( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
922 
929  void refreshBackgroundColor( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
930 
935  void refreshBlendMode( const QgsExpressionContext &context );
936 
937  void init( const bool manageZValue );
938 
939  friend class QgsComposerItemGroup; // to access mTemplateUuid
940 };
941 
942 #endif
bool mExcludeFromExports
Whether item should be excluded in exports.
Item representing the paper.
Definition: qgspaperitem.h:46
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.
Item representing the paper in a layout.
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.