QGIS API Documentation  2.99.0-Master (f867b65)
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 #include <qgslayoutitemshape.h>
57 #include <qgslayoutitempage.h>
58 #endif
59 
60 
61 #ifdef SIP_RUN
63  // the conversions have to be static, because they're using multiple inheritance
64  // (seen in PyQt4 .sip files for some QGraphicsItem classes)
65  if ( dynamic_cast< QgsComposerItem * >( sipCpp ) )
66  {
67  switch ( sipCpp->type() )
68  {
70  sipType = sipType_QgsComposerItem;
71  *sipCppRet = static_cast<QgsComposerItem *>( sipCpp );
72  break;
74  sipType = sipType_QgsComposerArrow;
75  *sipCppRet = static_cast<QgsComposerArrow *>( sipCpp );
76  break;
78  sipType = sipType_QgsComposerItemGroup;
79  *sipCppRet = static_cast<QgsComposerItemGroup *>( sipCpp );
80  break;
82  sipType = sipType_QgsComposerLabel;
83  *sipCppRet = static_cast<QgsComposerLabel *>( sipCpp );
84  break;
86  sipType = sipType_QgsComposerLegend;
87  *sipCppRet = static_cast<QgsComposerLegend *>( sipCpp );
88  break;
90  sipType = sipType_QgsComposerMap;
91  *sipCppRet = static_cast<QgsComposerMap *>( sipCpp );
92  break;
94  sipType = sipType_QgsPaperItem;
95  *sipCppRet = static_cast<QgsPaperItem *>( sipCpp );
96  break;
98  sipType = sipType_QgsComposerPicture;
99  *sipCppRet = static_cast<QgsComposerPicture *>( sipCpp );
100  break;
102  sipType = sipType_QgsComposerScaleBar;
103  *sipCppRet = static_cast<QgsComposerScaleBar *>( sipCpp );
104  break;
106  sipType = sipType_QgsComposerShape;
107  *sipCppRet = static_cast<QgsComposerShape *>( sipCpp );
108  break;
110  sipType = sipType_QgsComposerPolygon;
111  *sipCppRet = static_cast<QgsComposerPolygon *>( sipCpp );
112  break;
114  sipType = sipType_QgsComposerPolyline;
115  *sipCppRet = static_cast<QgsComposerPolyline *>( sipCpp );
116  break;
118  sipType = sipType_QgsComposerFrame;
119  *sipCppRet = static_cast<QgsComposerFrame *>( sipCpp );
120  break;
121  default:
122  sipType = 0;
123  }
124  }
125  else
126  {
127  switch ( sipCpp->type() )
128  {
129  // really, these *should* use the constants from QgsLayoutItemRegistry, but sip doesn't like that!
130  case QGraphicsItem::UserType + 101:
131  sipType = sipType_QgsLayoutItemPage;
132  *sipCppRet = static_cast<QgsLayoutItemPage *>( sipCpp );
133  break;
134  default:
135  sipType = 0;
136  }
137  }
138 
139  SIP_END
140 #endif
141 
142 
143  Q_OBJECT
144  public:
145 
146  enum ItemType
147  {
148  // base class for the items
149  ComposerItem = UserType + 100,
150 
151  // derived classes
157  ComposerPaper, // QgsPaperItem
166  ComposerFrame
167  };
168 
171  {
181  NoAction
182  };
183 
185  {
194  LowerRight
195  };
196 
197  //note - must sync with QgsMapCanvas::WheelAction.
198  //TODO - QGIS 3.0 move QgsMapCanvas::WheelAction from GUI->CORE and remove this enum
199 
202  enum ZoomMode
203  {
204  Zoom = 0,
207  NoZoom
208  };
209 
213  QgsComposerItem( QgsComposition *composition SIP_TRANSFERTHIS, bool manageZValue = true );
214 
222  QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition *composition SIP_TRANSFERTHIS, bool manageZValue = true );
223  virtual ~QgsComposerItem();
224 
226  virtual int type() const override { return ComposerItem; }
227 
235  virtual bool isRemoved() const { return mRemovedFromComposition; }
236 
244  void setIsRemoved( const bool removed ) { mRemovedFromComposition = removed; }
245 
247  virtual void setSelected( bool s );
248 
250  virtual bool selected() const { return QGraphicsRectItem::isSelected(); }
251 
253  void move( double dx, double dy );
254 
258  virtual void moveContent( double dx, double dy ) { Q_UNUSED( dx ); Q_UNUSED( dy ); }
259 
266  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) { Q_UNUSED( factor ); Q_UNUSED( point ); Q_UNUSED( mode ); }
267 
274  int page() const;
275 
282  QPointF pagePos() const;
283 
292  void updatePagePos( double newPageWidth, double newPageHeight );
293 
301  void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft, int page = -1 );
302 
313  void setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint = UpperLeft, bool posIncludesFrame = false, int page = -1 );
314 
317  ItemPositionMode lastUsedPositionMode() { return mLastUsedPositionMode; }
318 
321  virtual void setSceneRect( const QRectF &rectangle );
322 
324  bool _writeXml( QDomElement &itemElem, QDomDocument &doc ) const;
325 
327  bool _readXml( const QDomElement &itemElem, const QDomDocument &doc );
328 
336  bool hasFrame() const {return mFrame;}
337 
345  virtual void setFrameEnabled( const bool drawFrame );
346 
355  virtual void setFrameStrokeColor( const QColor &color );
356 
365  QColor frameStrokeColor() const { return mFrameColor; }
366 
375  virtual void setFrameStrokeWidth( const double strokeWidth );
376 
385  double frameStrokeWidth() const { return mFrameWidth; }
386 
395  Qt::PenJoinStyle frameJoinStyle() const { return mFrameJoinStyle; }
396 
405  void setFrameJoinStyle( const Qt::PenJoinStyle style );
406 
414  virtual double estimatedFrameBleed() const;
415 
424  virtual QRectF rectWithFrame() const;
425 
431  bool hasBackground() const {return mBackground;}
432 
439  void setBackgroundEnabled( const bool drawBackground ) { mBackground = drawBackground; }
440 
446  QColor backgroundColor() const { return mBackgroundColor; }
447 
454  void setBackgroundColor( const QColor &backgroundColor );
455 
460  QPainter::CompositionMode blendMode() const { return mBlendMode; }
461 
466  void setBlendMode( const QPainter::CompositionMode blendMode );
467 
474  double itemOpacity() const { return mOpacity; }
475 
482  void setItemOpacity( const double opacity );
483 
490  bool effectsEnabled() const { return mEffectsEnabled; }
491 
498  void setEffectsEnabled( const bool effectsEnabled );
499 
501  virtual void addItem( QgsComposerItem *item ) { Q_UNUSED( item ); }
502  virtual void removeItems() {}
503 
504  virtual void beginItemCommand( const QString &text ) { beginCommand( text ); }
505 
509  void beginCommand( const QString &commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
510 
511  virtual void endItemCommand() { endCommand(); }
513  void endCommand();
514  void cancelCommand();
515 
516  //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
517  //painter down by the same factor for drawing
518 
523  void setPositionLock( const bool lock );
524 
529  bool positionLock() const { return mItemPositionLocked; }
530 
539  double itemRotation( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
540 
546  virtual void updateItem();
547 
552  QString id() const { return mId; }
553 
558  virtual void setId( const QString &id );
559 
566  QString uuid() const { return mUuid; }
567 
575  virtual QString displayName() const;
576 
584  virtual void setVisibility( const bool visible );
585 
593  bool excludeFromExports( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
594 
600  virtual void setExcludeFromExports( const bool exclude );
601 
607  bool isGroupMember() const { return mIsGroupMember; }
608 
614  void setIsGroupMember( const bool isGroupMember );
615 
622  virtual int numberExportLayers() const { return 0; }
623 
629  virtual void setCurrentExportLayer( const int layerIdx = -1 ) { mCurrentExportLayer = layerIdx; }
630 
635  virtual QgsExpressionContext createExpressionContext() const override;
636 
645  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
646 
655  bool updatesEnabled() const { return mUpdatesEnabled; }
656 
657  public slots:
658 
667  virtual void setItemRotation( const double rotation, const bool adjustPosition = false );
668 
669  void repaint() override;
670 
679  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
680 
681  protected:
684 
690 
692  QGraphicsRectItem *mBoundingResizeRectangle = nullptr;
693  QGraphicsLineItem *mHAlignSnapItem = nullptr;
694  QGraphicsLineItem *mVAlignSnapItem = nullptr;
695 
697  bool mFrame;
699  QColor mFrameColor;
701  double mFrameWidth = 0.3;
703  Qt::PenJoinStyle mFrameJoinStyle = Qt::MiterJoin;
704 
709 
713 
716 
719 
724 
726  QPainter::CompositionMode mBlendMode;
728  QgsComposerEffect *mEffect = nullptr;
729 
731  double mOpacity = 1.0;
732 
735 
740 
743 
746 
752 
756  virtual void drawSelectionBoxes( QPainter *p );
757 
759  virtual void drawFrame( QPainter *p );
760 
762  virtual void drawBackground( QPainter *p );
763 
766  double rectHandlerBorderTolerance() const;
767 
771  double horizontalViewScaleFactor() const;
772 
773  //some utility functions
774 
776  QGraphicsLineItem *hAlignSnapItem();
777  void deleteHAlignSnapItem();
779  QGraphicsLineItem *vAlignSnapItem();
780  void deleteVAlignSnapItem();
781  void deleteAlignItems();
782 
794  QRectF evalItemRect( const QRectF &newRect, const bool resizeOnly = false, const QgsExpressionContext *context = nullptr );
795 
800  bool shouldDrawItem() const;
801 
802  signals:
804  void itemRotationChanged( double newRotation );
806  void sizeChanged();
807 
811  void frameChanged();
812 
816  void lockChanged();
817 
818  private:
819  // id (not unique)
820  QString mId;
821  // name (unique)
822  QString mUuid;
823  // name (temporary when loaded from template)
824  QString mTemplateUuid;
825  // true if composition manages the z value for this item
826  bool mCompositionManagesZValue;
827 
832  bool mUpdatesEnabled = true;
833 
841  void refreshRotation( const bool updateItem = true, const bool rotateAroundCenter = false, const QgsExpressionContext &context = QgsExpressionContext() );
842 
849  void refreshOpacity( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
850 
856  void refreshFrameColor( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
857 
863  void refreshBackgroundColor( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
864 
868  void refreshBlendMode( const QgsExpressionContext &context );
869 
870  void init( const bool manageZValue );
871 
872  friend class QgsComposerItemGroup; // to access mTemplateUuid
873 };
874 
875 #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.
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.