QGIS API Documentation  2.12.0-Lyon
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
62  ComposerFrame
63  };
64 
67  {
77  NoAction
78  };
79 
81  {
90  LowerRight
91  };
92 
93  //note - must sync with QgsMapCanvas::WheelAction.
94  //TODO - QGIS 3.0 move QgsMapCanvas::WheelAction from GUI->CORE and remove this enum
97  enum ZoomMode
98  {
99  Zoom = 0,
102  NoZoom
103  };
104 
108  QgsComposerItem( QgsComposition* composition, bool manageZValue = true );
116  QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition, bool manageZValue = true );
117  virtual ~QgsComposerItem();
118 
120  virtual int type() const override { return ComposerItem; }
121 
129  virtual bool isRemoved() const { return mRemovedFromComposition; }
130 
138  void setIsRemoved( const bool removed ) { mRemovedFromComposition = removed; }
139 
141  virtual void setSelected( bool s );
142 
144  virtual bool selected() const { return QGraphicsRectItem::isSelected(); }
145 
147  void move( double dx, double dy );
148 
152  virtual void moveContent( double dx, double dy ) { Q_UNUSED( dx ); Q_UNUSED( dy ); }
153 
160  Q_DECL_DEPRECATED virtual void zoomContent( int delta, double x, double y ) { Q_UNUSED( delta ); Q_UNUSED( x ); Q_UNUSED( y ); }
161 
168  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) { Q_UNUSED( factor ); Q_UNUSED( point ); Q_UNUSED( mode ); }
169 
176  int page() const;
177 
184  QPointF pagePos() const;
185 
194  void updatePagePos( double newPageWidth, double newPageHeight );
195 
203  void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft, int page = -1 );
204 
215  void setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint = UpperLeft, bool posIncludesFrame = false, int page = -1 );
216 
219  ItemPositionMode lastUsedPositionMode() { return mLastUsedPositionMode; }
220 
223  virtual void setSceneRect( const QRectF& rectangle );
224 
226  bool _writeXML( QDomElement& itemElem, QDomDocument& doc ) const;
227 
229  bool _readXML( const QDomElement& itemElem, const QDomDocument& doc );
230 
238  bool hasFrame() const {return mFrame;}
239 
247  virtual void setFrameEnabled( const bool drawFrame );
248 
257  virtual void setFrameOutlineColor( const QColor& color );
258 
267  QColor frameOutlineColor() const { return pen().color(); }
268 
277  virtual void setFrameOutlineWidth( const double outlineWidth );
278 
287  double frameOutlineWidth() const { return pen().widthF(); }
288 
297  Qt::PenJoinStyle frameJoinStyle() const { return mFrameJoinStyle; }
298 
307  void setFrameJoinStyle( const Qt::PenJoinStyle style );
308 
316  virtual double estimatedFrameBleed() const;
317 
326  virtual QRectF rectWithFrame() const;
327 
333  bool hasBackground() const {return mBackground;}
334 
341  void setBackgroundEnabled( const bool drawBackground ) { mBackground = drawBackground; }
342 
348  QColor backgroundColor() const { return mBackgroundColor; }
349 
356  void setBackgroundColor( const QColor& backgroundColor );
357 
362  QPainter::CompositionMode blendMode() const { return mBlendMode; }
363 
368  void setBlendMode( const QPainter::CompositionMode blendMode );
369 
374  int transparency() const { return mTransparency; }
375 
380  void setTransparency( const int transparency );
381 
388  bool effectsEnabled() const { return mEffectsEnabled; }
389 
396  void setEffectsEnabled( const bool effectsEnabled );
397 
399  virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
400  virtual void removeItems() {}
401 
402  virtual void beginItemCommand( const QString& text ) { beginCommand( text ); }
403 
407  void beginCommand( const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
408 
409  virtual void endItemCommand() { endCommand(); }
411  void endCommand();
412  void cancelCommand();
413 
414  //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
415  //painter down by the same factor for drawing
416 
421  Q_DECL_DEPRECATED void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font, const QColor& c = QColor() ) const;
422 
433  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;
434 
438  Q_DECL_DEPRECATED double textWidthMillimeters( const QFont& font, const QString& text ) const;
439 
443  Q_DECL_DEPRECATED double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
444 
448  Q_DECL_DEPRECATED double fontAscentMillimeters( const QFont& font ) const;
449 
453  Q_DECL_DEPRECATED double fontDescentMillimeters( const QFont& font ) const;
454 
460  Q_DECL_DEPRECATED double fontHeightMillimeters( const QFont& font ) const;
461 
465  Q_DECL_DEPRECATED double pixelFontSize( double pointSize ) const;
466 
470  Q_DECL_DEPRECATED QFont scaledFontPixelSize( const QFont& font ) const;
471 
476  void setPositionLock( const bool lock );
477 
482  bool positionLock() const { return mItemPositionLocked; }
483 
492  double itemRotation( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
493 
498  Q_DECL_DEPRECATED double rotation() const { return mEvaluatedItemRotation; }
499 
501  virtual void updateItem() { QGraphicsRectItem::update(); }
502 
507  QString id() const { return mId; }
508 
513  virtual void setId( const QString& id );
514 
521  QString uuid() const { return mUuid; }
522 
530  virtual QString displayName() const;
531 
539  virtual void setVisibility( const bool visible );
540 
548  bool excludeFromExports( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
549 
555  virtual void setExcludeFromExports( const bool exclude );
556 
562  bool isGroupMember() const { return mIsGroupMember; }
563 
569  void setIsGroupMember( const bool isGroupMember );
570 
577  virtual int numberExportLayers() const { return 0; }
578 
584  virtual void setCurrentExportLayer( const int layerIdx = -1 ) { mCurrentExportLayer = layerIdx; }
585 
586  virtual QgsExpressionContext* createExpressionContext() const override;
587 
588  public slots:
592  virtual void setRotation( double r );
593 
601  virtual void setItemRotation( const double r, const bool adjustPosition = false );
602 
603  void repaint() override;
604 
614 
615  protected:
618 
624 
629 
631  bool mFrame;
637  Qt::PenJoinStyle mFrameJoinStyle;
638 
642 
645 
652 
654  QPainter::CompositionMode mBlendMode;
657 
660 
663 
667 
670 
673 
678 
682  virtual void drawSelectionBoxes( QPainter* p );
683 
685  virtual void drawFrame( QPainter* p );
686 
688  virtual void drawBackground( QPainter* p );
689 
693  Q_DECL_DEPRECATED void drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const;
694 
696  Q_DECL_DEPRECATED double angle( const QPointF& p1, const QPointF& p2 ) const;
697 
700  double rectHandlerBorderTolerance() const;
701 
705  Q_DECL_DEPRECATED double lockSymbolSize() const;
706 
710  double horizontalViewScaleFactor() const;
711 
712  //some utility functions
713 
717  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height, double rotation ) const;
718 
722  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
723 
731  Q_DECL_DEPRECATED QRectF largestRotatedRectWithinBounds( const QRectF& originalRect, const QRectF& boundsRect, double rotation ) const;
732 
736  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height, double rotation ) const;
737 
741  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
742 
746  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height, double rotation );
747 
751  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
752 
759  Q_DECL_DEPRECATED void rotate( double angle, double& x, double& y ) const;
760 
762  QGraphicsLineItem* hAlignSnapItem();
763  void deleteHAlignSnapItem();
765  QGraphicsLineItem* vAlignSnapItem();
766  void deleteVAlignSnapItem();
767  void deleteAlignItems();
768 
780  QRectF evalItemRect( const QRectF &newRect, const bool resizeOnly = false, const QgsExpressionContext* context = 0 );
781 
786  bool shouldDrawItem() const;
787 
788  signals:
790  void itemRotationChanged( double newRotation );
792  void sizeChanged();
796  void frameChanged();
800  void lockChanged();
801 
802  private:
803  // id (not unique)
804  QString mId;
805  // name (unique)
806  QString mUuid;
807  // name (temporary when loaded from template)
808  QString mTemplateUuid;
809  // true if composition manages the z value for this item
810  bool mCompositionManagesZValue;
811 
819  void refreshRotation( const bool updateItem = true, const bool rotateAroundCenter = false, const QgsExpressionContext &context = QgsExpressionContext() );
820 
827  void refreshTransparency( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
828 
832  void refreshBlendMode( const QgsExpressionContext &context );
833 
834  void init( const bool manageZValue );
835 
836  friend class QgsComposerItemGroup; // to access mTemplateUuid
837 };
838 
839 #endif
bool positionLock() const
Returns whether position lock for mouse drags is enabled returns true if item is locked for mouse mov...
bool mExcludeFromExports
Whether item should be excluded in exports.
void setSelected(bool selected)
bool effectsEnabled() const
Returns whether effects (eg blend modes) are enabled for the item.
int mTransparency
Item transparency.
A base class for objects which belong to a map composition.
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's last used position mode.
virtual bool selected() const
Is selected.
virtual void removeItems()
Qt::PenJoinStyle frameJoinStyle() const
Returns the join style used for drawing the item's frame.
A item that forms part of a map composition.
QgsComposerItem::MouseMoveAction mCurrentMouseMoveAction
double mLastValidViewScaleFactor
Backup to restore item appearance if no view scale factor is available.
ItemPositionMode mLastUsedPositionMode
The item's position mode.
A container for grouping several QgsComposerItems.
virtual void zoomContent(const double factor, const QPointF point, const ZoomMode mode=QgsComposerItem::Zoom)
Zoom content of item.
DataDefinedProperty
Data defined properties for different item types.
QColor backgroundColor() const
Gets the background color for this item.
QPainter::CompositionMode mBlendMode
Composition blend mode for item.
void update(const QRectF &rect)
double frameOutlineWidth() const
Returns the frame's outline width.
virtual void moveContent(double dx, double dy)
Move Content of item.
QColor color() const
virtual QgsExpressionContext * createExpressionContext() const
Creates an expression context relating to the objects's current state.
QString uuid() const
Get item identification name.
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.
int transparency() const
Returns the item's transparency.
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.
bool isSelected() const
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
double mEvaluatedItemRotation
Temporary evaluated item rotation in degrees, clockwise.
virtual void repaint()
Triggers a redraw for the item.
bool mRemovedFromComposition
True if item has been removed from the composition.
PropertyValueType
Specifies whether the value returned by a function should be the original, user set value...
virtual int numberExportLayers() const
Get the number of layers that this item requires for exporting as layers.
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.
QColor frameOutlineColor() const
Returns the frame's outline color.
Q_DECL_DEPRECATED double rotation() const
Returns the rotation for the composer item.
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.
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.
virtual void refreshDataDefinedProperty(const DataDefinedProperty property=AllProperties, const QgsExpressionContext *context=0)
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
QPainter::CompositionMode blendMode() const
Returns the item's composition blending mode.
bool hasFrame() const
Whether this item has a frame or not.
bool hasBackground() const
Whether this item has a Background or not.
qreal widthF() const
void setRotation(qreal angle)
QgsComposerEffect * mEffect
MouseMoveAction
Describes the action (move or resize in different directon) to be done during mouse move...
bool mBackground
True if item background needs to be painted.
bool isGroupMember() const
Returns whether this item is part of a group.
ZoomMode
Modes for zooming item content.
bool mIsGroupMember
Whether or not this item is part of a group.
double mItemRotation
Item rotation in degrees, clockwise.
void rotate(qreal angle)
bool mEvaluatedExcludeFromExports
Temporary evaluated item exclusion.
virtual int type() const override
Return correct graphics item type.
QString id() const
Get item's id (which is not necessarly unique)