QGIS API Documentation  2.11.0-Master
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 "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;
34 
38 class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRectItem
39 {
40  Q_OBJECT
41  public:
42 
43  enum ItemType
44  {
45  // base class for the items
46  ComposerItem = UserType + 100,
47 
48  // derived classes
54  ComposerPaper, // QgsPaperItem
61  ComposerFrame
62  };
63 
66  {
76  NoAction
77  };
78 
80  {
89  LowerRight
90  };
91 
92  //note - must sync with QgsMapCanvas::WheelAction.
93  //TODO - QGIS 3.0 move QgsMapCanvas::WheelAction from GUI->CORE and remove this enum
96  enum ZoomMode
97  {
98  Zoom = 0,
101  NoZoom
102  };
103 
107  QgsComposerItem( QgsComposition* composition, bool manageZValue = true );
115  QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition, bool manageZValue = true );
116  virtual ~QgsComposerItem();
117 
119  virtual int type() const override { return ComposerItem; }
120 
128  virtual bool isRemoved() const { return mRemovedFromComposition; }
129 
137  void setIsRemoved( const bool removed ) { mRemovedFromComposition = removed; }
138 
140  virtual void setSelected( bool s );
141 
143  virtual bool selected() const { return QGraphicsRectItem::isSelected(); }
144 
146  void move( double dx, double dy );
147 
151  virtual void moveContent( double dx, double dy ) { Q_UNUSED( dx ); Q_UNUSED( dy ); }
152 
159  Q_DECL_DEPRECATED virtual void zoomContent( int delta, double x, double y ) { Q_UNUSED( delta ); Q_UNUSED( x ); Q_UNUSED( y ); }
160 
167  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) { Q_UNUSED( factor ); Q_UNUSED( point ); Q_UNUSED( mode ); }
168 
175  int page() const;
176 
183  QPointF pagePos() const;
184 
193  void updatePagePos( double newPageWidth, double newPageHeight );
194 
202  void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft, int page = -1 );
203 
214  void setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint = UpperLeft, bool posIncludesFrame = false, int page = -1 );
215 
218  ItemPositionMode lastUsedPositionMode() { return mLastUsedPositionMode; }
219 
222  virtual void setSceneRect( const QRectF& rectangle );
223 
225  bool _writeXML( QDomElement& itemElem, QDomDocument& doc ) const;
226 
228  bool _readXML( const QDomElement& itemElem, const QDomDocument& doc );
229 
237  bool hasFrame() const {return mFrame;}
238 
246  virtual void setFrameEnabled( const bool drawFrame );
247 
256  virtual void setFrameOutlineColor( const QColor& color );
257 
266  QColor frameOutlineColor() const { return pen().color(); }
267 
276  virtual void setFrameOutlineWidth( const double outlineWidth );
277 
286  double frameOutlineWidth() const { return pen().widthF(); }
287 
296  Qt::PenJoinStyle frameJoinStyle() const { return mFrameJoinStyle; }
297 
306  void setFrameJoinStyle( const Qt::PenJoinStyle style );
307 
315  virtual double estimatedFrameBleed() const;
316 
325  virtual QRectF rectWithFrame() const;
326 
332  bool hasBackground() const {return mBackground;}
333 
340  void setBackgroundEnabled( const bool drawBackground ) { mBackground = drawBackground; }
341 
347  QColor backgroundColor() const { return mBackgroundColor; }
348 
355  void setBackgroundColor( const QColor& backgroundColor );
356 
361  QPainter::CompositionMode blendMode() const { return mBlendMode; }
362 
367  void setBlendMode( const QPainter::CompositionMode blendMode );
368 
373  int transparency() const { return mTransparency; }
374 
379  void setTransparency( const int transparency );
380 
387  bool effectsEnabled() const { return mEffectsEnabled; }
388 
395  void setEffectsEnabled( const bool effectsEnabled );
396 
398  virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
399  virtual void removeItems() {}
400 
401  virtual void beginItemCommand( const QString& text ) { beginCommand( text ); }
402 
406  void beginCommand( const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
407 
408  virtual void endItemCommand() { endCommand(); }
410  void endCommand();
411  void cancelCommand();
412 
413  //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
414  //painter down by the same factor for drawing
415 
420  Q_DECL_DEPRECATED void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font, const QColor& c = QColor() ) const;
421 
432  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;
433 
437  Q_DECL_DEPRECATED double textWidthMillimeters( const QFont& font, const QString& text ) const;
438 
442  Q_DECL_DEPRECATED double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
443 
447  Q_DECL_DEPRECATED double fontAscentMillimeters( const QFont& font ) const;
448 
452  Q_DECL_DEPRECATED double fontDescentMillimeters( const QFont& font ) const;
453 
459  Q_DECL_DEPRECATED double fontHeightMillimeters( const QFont& font ) const;
460 
464  Q_DECL_DEPRECATED double pixelFontSize( double pointSize ) const;
465 
469  Q_DECL_DEPRECATED QFont scaledFontPixelSize( const QFont& font ) const;
470 
475  void setPositionLock( const bool lock );
476 
481  bool positionLock() const { return mItemPositionLocked; }
482 
491  double itemRotation( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
492 
497  Q_DECL_DEPRECATED double rotation() const { return mEvaluatedItemRotation; }
498 
500  virtual void updateItem() { QGraphicsRectItem::update(); }
501 
506  QString id() const { return mId; }
507 
512  virtual void setId( const QString& id );
513 
520  QString uuid() const { return mUuid; }
521 
529  virtual QString displayName() const;
530 
538  virtual void setVisibility( const bool visible );
539 
547  bool excludeFromExports( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
548 
554  virtual void setExcludeFromExports( const bool exclude );
555 
561  bool isGroupMember() const { return mIsGroupMember; }
562 
568  void setIsGroupMember( const bool isGroupMember );
569 
576  virtual int numberExportLayers() const { return 0; }
577 
583  virtual void setCurrentExportLayer( const int layerIdx = -1 ) { mCurrentExportLayer = layerIdx; }
584 
585  public slots:
589  virtual void setRotation( double r );
590 
598  virtual void setItemRotation( const double r, const bool adjustPosition = false );
599 
600  void repaint() override;
601 
610 
611  protected:
614 
620 
625 
627  bool mFrame;
633  Qt::PenJoinStyle mFrameJoinStyle;
634 
638 
641 
648 
650  QPainter::CompositionMode mBlendMode;
653 
656 
659 
663 
666 
669 
674 
678  virtual void drawSelectionBoxes( QPainter* p );
679 
681  virtual void drawFrame( QPainter* p );
682 
684  virtual void drawBackground( QPainter* p );
685 
689  Q_DECL_DEPRECATED void drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const;
690 
692  Q_DECL_DEPRECATED double angle( const QPointF& p1, const QPointF& p2 ) const;
693 
696  double rectHandlerBorderTolerance() const;
697 
701  Q_DECL_DEPRECATED double lockSymbolSize() const;
702 
706  double horizontalViewScaleFactor() const;
707 
708  //some utility functions
709 
713  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height, double rotation ) const;
714 
718  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
719 
727  Q_DECL_DEPRECATED QRectF largestRotatedRectWithinBounds( QRectF originalRect, QRectF boundsRect, double rotation ) const;
728 
732  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height, double rotation ) const;
733 
737  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
738 
742  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height, double rotation );
743 
747  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
748 
755  Q_DECL_DEPRECATED void rotate( double angle, double& x, double& y ) const;
756 
758  QGraphicsLineItem* hAlignSnapItem();
759  void deleteHAlignSnapItem();
761  QGraphicsLineItem* vAlignSnapItem();
762  void deleteVAlignSnapItem();
763  void deleteAlignItems();
764 
775  QRectF evalItemRect( const QRectF &newRect, const bool resizeOnly = false );
776 
781  bool shouldDrawItem() const;
782 
783  signals:
785  void itemRotationChanged( double newRotation );
787  void sizeChanged();
791  void frameChanged();
795  void lockChanged();
796 
797  private:
798  // id (not unique)
799  QString mId;
800  // name (unique)
801  QString mUuid;
802  // name (temporary when loaded from template)
803  QString mTemplateUuid;
804  // true if composition manages the z value for this item
805  bool mCompositionManagesZValue;
806 
813  void refreshRotation( const bool updateItem = true, const bool rotateAroundCenter = false );
814 
820  void refreshTransparency( const bool updateItem = true );
821 
825  void refreshBlendMode();
826 
827  void init( const bool manageZValue );
828 
829  friend class QgsComposerItemGroup; // to access mTemplateUuid
830 };
831 
832 #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
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
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.
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.
virtual void refreshDataDefinedProperty(const DataDefinedProperty property=AllProperties)
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
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)