QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgscomposeritemcommand.h"
21 #include "qgscomposereffect.h"
22 #include "qgscomposerobject.h"
23 #include "qgsmaprenderer.h" // for blend mode functions & enums
24 #include <QGraphicsRectItem>
25 #include <QObject>
26 
27 class QWidget;
28 class QDomDocument;
29 class QDomElement;
30 class QGraphicsLineItem;
32 class QgsDataDefined;
33 class QgsComposition;
35 
39 class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRectItem
40 {
41  Q_OBJECT
42  public:
43 
44  enum ItemType
45  {
46  // base class for the items
47  ComposerItem = UserType + 100,
48 
49  // derived classes
55  ComposerPaper, // QgsPaperItem
64  ComposerFrame
65  };
66 
69  {
79  NoAction
80  };
81 
83  {
92  LowerRight
93  };
94 
95  //note - must sync with QgsMapCanvas::WheelAction.
96  //TODO - QGIS 3.0 move QgsMapCanvas::WheelAction from GUI->CORE and remove this enum
99  enum ZoomMode
100  {
101  Zoom = 0,
104  NoZoom
105  };
106 
110  QgsComposerItem( QgsComposition* composition, bool manageZValue = true );
118  QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition, bool manageZValue = true );
119  virtual ~QgsComposerItem();
120 
122  virtual int type() const override { return ComposerItem; }
123 
131  virtual bool isRemoved() const { return mRemovedFromComposition; }
132 
140  void setIsRemoved( const bool removed ) { mRemovedFromComposition = removed; }
141 
143  virtual void setSelected( bool s );
144 
146  virtual bool selected() const { return QGraphicsRectItem::isSelected(); }
147 
149  void move( double dx, double dy );
150 
154  virtual void moveContent( double dx, double dy ) { Q_UNUSED( dx ); Q_UNUSED( dy ); }
155 
162  Q_DECL_DEPRECATED virtual void zoomContent( int delta, double x, double y ) { Q_UNUSED( delta ); Q_UNUSED( x ); Q_UNUSED( y ); }
163 
170  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) { Q_UNUSED( factor ); Q_UNUSED( point ); Q_UNUSED( mode ); }
171 
178  int page() const;
179 
186  QPointF pagePos() const;
187 
196  void updatePagePos( double newPageWidth, double newPageHeight );
197 
205  void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft, int page = -1 );
206 
217  void setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint = UpperLeft, bool posIncludesFrame = false, int page = -1 );
218 
221  ItemPositionMode lastUsedPositionMode() { return mLastUsedPositionMode; }
222 
225  virtual void setSceneRect( const QRectF& rectangle );
226 
228  bool _writeXML( QDomElement& itemElem, QDomDocument& doc ) const;
229 
231  bool _readXML( const QDomElement& itemElem, const QDomDocument& doc );
232 
240  bool hasFrame() const {return mFrame;}
241 
249  virtual void setFrameEnabled( const bool drawFrame );
250 
259  virtual void setFrameOutlineColor( const QColor& color );
260 
269  QColor frameOutlineColor() const { return pen().color(); }
270 
279  virtual void setFrameOutlineWidth( const double outlineWidth );
280 
289  double frameOutlineWidth() const { return pen().widthF(); }
290 
299  Qt::PenJoinStyle frameJoinStyle() const { return mFrameJoinStyle; }
300 
309  void setFrameJoinStyle( const Qt::PenJoinStyle style );
310 
318  virtual double estimatedFrameBleed() const;
319 
328  virtual QRectF rectWithFrame() const;
329 
335  bool hasBackground() const {return mBackground;}
336 
343  void setBackgroundEnabled( const bool drawBackground ) { mBackground = drawBackground; }
344 
350  QColor backgroundColor() const { return mBackgroundColor; }
351 
358  void setBackgroundColor( const QColor& backgroundColor );
359 
364  QPainter::CompositionMode blendMode() const { return mBlendMode; }
365 
370  void setBlendMode( const QPainter::CompositionMode blendMode );
371 
376  int transparency() const { return mTransparency; }
377 
382  void setTransparency( const int transparency );
383 
390  bool effectsEnabled() const { return mEffectsEnabled; }
391 
398  void setEffectsEnabled( const bool effectsEnabled );
399 
401  virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
402  virtual void removeItems() {}
403 
404  virtual void beginItemCommand( const QString& text ) { beginCommand( text ); }
405 
409  void beginCommand( const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
410 
411  virtual void endItemCommand() { endCommand(); }
413  void endCommand();
414  void cancelCommand();
415 
416  //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
417  //painter down by the same factor for drawing
418 
423  Q_DECL_DEPRECATED void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font, const QColor& c = QColor() ) const;
424 
435  Q_DECL_DEPRECATED void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop, int flags = Qt::TextWordWrap ) const;
436 
440  Q_DECL_DEPRECATED double textWidthMillimeters( const QFont& font, const QString& text ) const;
441 
445  Q_DECL_DEPRECATED double fontHeightCharacterMM( const QFont& font, QChar c ) const;
446 
450  Q_DECL_DEPRECATED double fontAscentMillimeters( const QFont& font ) const;
451 
455  Q_DECL_DEPRECATED double fontDescentMillimeters( const QFont& font ) const;
456 
462  Q_DECL_DEPRECATED double fontHeightMillimeters( const QFont& font ) const;
463 
467  Q_DECL_DEPRECATED double pixelFontSize( double pointSize ) const;
468 
472  Q_DECL_DEPRECATED QFont scaledFontPixelSize( const QFont& font ) const;
473 
478  void setPositionLock( const bool lock );
479 
484  bool positionLock() const { return mItemPositionLocked; }
485 
494  double itemRotation( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
495 
499  Q_DECL_DEPRECATED double rotation() const { return mEvaluatedItemRotation; }
500 
502  virtual void updateItem() { QGraphicsRectItem::update(); }
503 
508  QString id() const { return mId; }
509 
514  virtual void setId( const QString& id );
515 
522  QString uuid() const { return mUuid; }
523 
531  virtual QString displayName() const;
532 
540  virtual void setVisibility( const bool visible );
541 
549  bool excludeFromExports( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
550 
556  virtual void setExcludeFromExports( const bool exclude );
557 
563  bool isGroupMember() const { return mIsGroupMember; }
564 
570  void setIsGroupMember( const bool isGroupMember );
571 
578  virtual int numberExportLayers() const { return 0; }
579 
585  virtual void setCurrentExportLayer( const int layerIdx = -1 ) { mCurrentExportLayer = layerIdx; }
586 
591  virtual QgsExpressionContext* createExpressionContext() const override;
592 
593  public slots:
597  virtual void setRotation( double r );
598 
606  virtual void setItemRotation( const double r, const bool adjustPosition = false );
607 
608  void repaint() override;
609 
618  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
619 
620  protected:
623 
629 
634 
636  bool mFrame;
642  Qt::PenJoinStyle mFrameJoinStyle;
643 
647 
650 
657 
659  QPainter::CompositionMode mBlendMode;
662 
665 
668 
673 
676 
679 
685 
689  virtual void drawSelectionBoxes( QPainter* p );
690 
692  virtual void drawFrame( QPainter* p );
693 
695  virtual void drawBackground( QPainter* p );
696 
700  Q_DECL_DEPRECATED void drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const;
701 
705  Q_DECL_DEPRECATED double angle( QPointF p1, QPointF p2 ) const;
706 
709  double rectHandlerBorderTolerance() const;
710 
714  Q_DECL_DEPRECATED double lockSymbolSize() const;
715 
719  double horizontalViewScaleFactor() const;
720 
721  //some utility functions
722 
726  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height, double rotation ) const;
727 
731  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
732 
740  Q_DECL_DEPRECATED QRectF largestRotatedRectWithinBounds( const QRectF& originalRect, const QRectF& boundsRect, double rotation ) const;
741 
745  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height, double rotation ) const;
746 
750  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
751 
755  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height, double rotation );
756 
760  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
761 
768  Q_DECL_DEPRECATED void rotate( double angle, double& x, double& y ) const;
769 
771  QGraphicsLineItem* hAlignSnapItem();
772  void deleteHAlignSnapItem();
774  QGraphicsLineItem* vAlignSnapItem();
775  void deleteVAlignSnapItem();
776  void deleteAlignItems();
777 
789  QRectF evalItemRect( const QRectF &newRect, const bool resizeOnly = false, const QgsExpressionContext* context = nullptr );
790 
795  bool shouldDrawItem() const;
796 
797  signals:
799  void itemRotationChanged( double newRotation );
801  void sizeChanged();
805  void frameChanged();
809  void lockChanged();
810 
811  private:
812  // id (not unique)
813  QString mId;
814  // name (unique)
815  QString mUuid;
816  // name (temporary when loaded from template)
817  QString mTemplateUuid;
818  // true if composition manages the z value for this item
819  bool mCompositionManagesZValue;
820 
828  void refreshRotation( const bool updateItem = true, const bool rotateAroundCenter = false, const QgsExpressionContext &context = QgsExpressionContext() );
829 
836  void refreshTransparency( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
837 
841  void refreshBlendMode( const QgsExpressionContext &context );
842 
843  void init( const bool manageZValue );
844 
845  friend class QgsComposerItemGroup; // to access mTemplateUuid
846 };
847 
848 #endif
bool mExcludeFromExports
Whether item should be excluded in exports.
void setSelected(bool selected)
int mTransparency
Item transparency.
A base class for objects which belong to a map composition.
bool isGroupMember() const
Returns whether this item is part of a group.
A container class for data source field mapping or expression.
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 (eg 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
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.
QPainter::CompositionMode mBlendMode
Composition blend mode for item.
void update(const QRectF &rect)
DataDefinedProperty
Data defined properties for different item types.
virtual void moveContent(double dx, double dy)
Move Content of item.
QColor color() const
double frameOutlineWidth() const
Returns the frame&#39;s outline width.
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.
virtual void updateItem()
Updates item, with the possibility to do custom update for subclasses.
QGraphicsRectItem * mBoundingResizeRectangle
Rectangle used during move and resize actions.
bool mFrame
True if item fram needs to be painted.
virtual bool selected() const
Is selected.
bool isSelected() const
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...
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.
double ANALYSIS_EXPORT angle(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
virtual bool isRemoved() const
Returns whether this item has been removed from the composition.
Qt::PenJoinStyle mFrameJoinStyle
Frame join style.
QColor mBackgroundColor
Background color.
QGraphicsLineItem * mVAlignSnapItem
virtual Q_DECL_DEPRECATED void zoomContent(int delta, double x, double y)
Zoom content of item.
QGraphicsLineItem * mHAlignSnapItem
int mCurrentExportLayer
The layer that needs to be exported.
QColor frameOutlineColor() const
Returns the frame&#39;s outline color.
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.
Q_DECL_DEPRECATED double rotation() const
Returns the rotation for the composer item.
qreal widthF() const
void setRotation(qreal angle)
bool hasFrame() const
Whether this item has a frame or not.
QgsComposerEffect * mEffect
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.
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)
double mItemRotation
Item rotation in degrees, clockwise.
void rotate(qreal angle)
virtual QgsExpressionContext * createExpressionContext() const
Creates an expression context relating to the objects&#39; current state.
bool mEvaluatedExcludeFromExports
Temporary evaluated item exclusion.
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.
int transparency() const
Returns the item&#39;s transparency.