QGIS API Documentation  2.99.0-Master (ba079d8)
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  Q_OBJECT
43  public:
44 
45  enum ItemType
46  {
47  // base class for the items
48  ComposerItem = UserType + 100,
49 
50  // derived classes
56  ComposerPaper, // QgsPaperItem
65  ComposerFrame
66  };
67 
70  {
80  NoAction
81  };
82 
84  {
93  LowerRight
94  };
95 
96  //note - must sync with QgsMapCanvas::WheelAction.
97  //TODO - QGIS 3.0 move QgsMapCanvas::WheelAction from GUI->CORE and remove this enum
98 
101  enum ZoomMode
102  {
103  Zoom = 0,
106  NoZoom
107  };
108 
112  QgsComposerItem( QgsComposition *composition SIP_TRANSFERTHIS, bool manageZValue = true );
113 
121  QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition *composition SIP_TRANSFERTHIS, bool manageZValue = true );
122  virtual ~QgsComposerItem();
123 
125  virtual int type() const override { return ComposerItem; }
126 
134  virtual bool isRemoved() const { return mRemovedFromComposition; }
135 
143  void setIsRemoved( const bool removed ) { mRemovedFromComposition = removed; }
144 
146  virtual void setSelected( bool s );
147 
149  virtual bool selected() const { return QGraphicsRectItem::isSelected(); }
150 
152  void move( double dx, double dy );
153 
157  virtual void moveContent( double dx, double dy ) { Q_UNUSED( dx ); Q_UNUSED( dy ); }
158 
165  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) { Q_UNUSED( factor ); Q_UNUSED( point ); Q_UNUSED( mode ); }
166 
173  int page() const;
174 
181  QPointF pagePos() const;
182 
191  void updatePagePos( double newPageWidth, double newPageHeight );
192 
200  void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft, int page = -1 );
201 
212  void setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint = UpperLeft, bool posIncludesFrame = false, int page = -1 );
213 
216  ItemPositionMode lastUsedPositionMode() { return mLastUsedPositionMode; }
217 
220  virtual void setSceneRect( const QRectF &rectangle );
221 
223  bool _writeXml( QDomElement &itemElem, QDomDocument &doc ) const;
224 
226  bool _readXml( const QDomElement &itemElem, const QDomDocument &doc );
227 
235  bool hasFrame() const {return mFrame;}
236 
244  virtual void setFrameEnabled( const bool drawFrame );
245 
254  virtual void setFrameStrokeColor( const QColor &color );
255 
264  QColor frameStrokeColor() const { return mFrameColor; }
265 
274  virtual void setFrameStrokeWidth( const double strokeWidth );
275 
284  double frameStrokeWidth() const { return mFrameWidth; }
285 
294  Qt::PenJoinStyle frameJoinStyle() const { return mFrameJoinStyle; }
295 
304  void setFrameJoinStyle( const Qt::PenJoinStyle style );
305 
313  virtual double estimatedFrameBleed() const;
314 
323  virtual QRectF rectWithFrame() const;
324 
330  bool hasBackground() const {return mBackground;}
331 
338  void setBackgroundEnabled( const bool drawBackground ) { mBackground = drawBackground; }
339 
345  QColor backgroundColor() const { return mBackgroundColor; }
346 
353  void setBackgroundColor( const QColor &backgroundColor );
354 
359  QPainter::CompositionMode blendMode() const { return mBlendMode; }
360 
365  void setBlendMode( const QPainter::CompositionMode blendMode );
366 
371  int transparency() const { return mTransparency; }
372 
377  void setTransparency( const int transparency );
378 
385  bool effectsEnabled() const { return mEffectsEnabled; }
386 
393  void setEffectsEnabled( const bool effectsEnabled );
394 
396  virtual void addItem( QgsComposerItem *item ) { Q_UNUSED( item ); }
397  virtual void removeItems() {}
398 
399  virtual void beginItemCommand( const QString &text ) { beginCommand( text ); }
400 
404  void beginCommand( const QString &commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
405 
406  virtual void endItemCommand() { endCommand(); }
408  void endCommand();
409  void cancelCommand();
410 
411  //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
412  //painter down by the same factor for drawing
413 
418  void setPositionLock( const bool lock );
419 
424  bool positionLock() const { return mItemPositionLocked; }
425 
434  double itemRotation( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
435 
441  virtual void updateItem();
442 
447  QString id() const { return mId; }
448 
453  virtual void setId( const QString &id );
454 
461  QString uuid() const { return mUuid; }
462 
470  virtual QString displayName() const;
471 
479  virtual void setVisibility( const bool visible );
480 
488  bool excludeFromExports( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
489 
495  virtual void setExcludeFromExports( const bool exclude );
496 
502  bool isGroupMember() const { return mIsGroupMember; }
503 
509  void setIsGroupMember( const bool isGroupMember );
510 
517  virtual int numberExportLayers() const { return 0; }
518 
524  virtual void setCurrentExportLayer( const int layerIdx = -1 ) { mCurrentExportLayer = layerIdx; }
525 
530  virtual QgsExpressionContext createExpressionContext() const override;
531 
540  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
541 
550  bool updatesEnabled() const { return mUpdatesEnabled; }
551 
552  public slots:
553 
561  virtual void setItemRotation( const double r, const bool adjustPosition = false );
562 
563  void repaint() override;
564 
573  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
574 
575  protected:
578 
584 
586  QGraphicsRectItem *mBoundingResizeRectangle = nullptr;
587  QGraphicsLineItem *mHAlignSnapItem = nullptr;
588  QGraphicsLineItem *mVAlignSnapItem = nullptr;
589 
591  bool mFrame;
593  QColor mFrameColor;
595  double mFrameWidth = 0.3;
597  Qt::PenJoinStyle mFrameJoinStyle = Qt::MiterJoin;
598 
603 
607 
610 
613 
618 
620  QPainter::CompositionMode mBlendMode;
622  QgsComposerEffect *mEffect = nullptr;
623 
626 
629 
634 
637 
640 
646 
650  virtual void drawSelectionBoxes( QPainter *p );
651 
653  virtual void drawFrame( QPainter *p );
654 
656  virtual void drawBackground( QPainter *p );
657 
660  double rectHandlerBorderTolerance() const;
661 
665  double horizontalViewScaleFactor() const;
666 
667  //some utility functions
668 
670  QGraphicsLineItem *hAlignSnapItem();
671  void deleteHAlignSnapItem();
673  QGraphicsLineItem *vAlignSnapItem();
674  void deleteVAlignSnapItem();
675  void deleteAlignItems();
676 
688  QRectF evalItemRect( const QRectF &newRect, const bool resizeOnly = false, const QgsExpressionContext *context = nullptr );
689 
694  bool shouldDrawItem() const;
695 
696  signals:
698  void itemRotationChanged( double newRotation );
700  void sizeChanged();
701 
705  void frameChanged();
706 
710  void lockChanged();
711 
712  private:
713  // id (not unique)
714  QString mId;
715  // name (unique)
716  QString mUuid;
717  // name (temporary when loaded from template)
718  QString mTemplateUuid;
719  // true if composition manages the z value for this item
720  bool mCompositionManagesZValue;
721 
726  bool mUpdatesEnabled = true;
727 
735  void refreshRotation( const bool updateItem = true, const bool rotateAroundCenter = false, const QgsExpressionContext &context = QgsExpressionContext() );
736 
743  void refreshTransparency( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
744 
750  void refreshFrameColor( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
751 
757  void refreshBackgroundColor( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
758 
762  void refreshBlendMode( const QgsExpressionContext &context );
763 
764  void init( const bool manageZValue );
765 
766  friend class QgsComposerItemGroup; // to access mTemplateUuid
767 };
768 
769 #endif
bool mExcludeFromExports
Whether item should be excluded in exports.
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.
virtual QgsExpressionContext createExpressionContext() const
Creates an expression context relating to the objects&#39; current state.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:34
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.
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.
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...
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.
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.
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.
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.
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.
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.
int transparency() const
Returns the item&#39;s transparency.
double frameStrokeWidth() const
Returns the frame&#39;s stroke width.