QGIS API Documentation  2.99.0-Master (0a63d1f)
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 "qgscomposeritemcommand.h"
22 #include "qgscomposerobject.h"
23 #include <QGraphicsRectItem>
24 #include <QObject>
25 #include <QPainter>
26 
27 class QWidget;
28 class QDomDocument;
29 class QDomElement;
30 class QGraphicsLineItem;
32 class QgsDataDefined;
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, bool manageZValue = true );
113 
121  QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition, 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 setFrameOutlineColor( const QColor& color );
255 
264  QColor frameOutlineColor() const { return pen().color(); }
265 
274  virtual void setFrameOutlineWidth( const double outlineWidth );
275 
284  double frameOutlineWidth() const { return pen().widthF(); }
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 
437  virtual void updateItem() { QGraphicsRectItem::update(); }
438 
443  QString id() const { return mId; }
444 
449  virtual void setId( const QString& id );
450 
457  QString uuid() const { return mUuid; }
458 
466  virtual QString displayName() const;
467 
475  virtual void setVisibility( const bool visible );
476 
484  bool excludeFromExports( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
485 
491  virtual void setExcludeFromExports( const bool exclude );
492 
498  bool isGroupMember() const { return mIsGroupMember; }
499 
505  void setIsGroupMember( const bool isGroupMember );
506 
513  virtual int numberExportLayers() const { return 0; }
514 
520  virtual void setCurrentExportLayer( const int layerIdx = -1 ) { mCurrentExportLayer = layerIdx; }
521 
526  virtual QgsExpressionContext createExpressionContext() const override;
527 
528  public slots:
529 
537  virtual void setItemRotation( const double r, const bool adjustPosition = false );
538 
539  void repaint() override;
540 
549  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
550 
551  protected:
554 
560 
562  QGraphicsRectItem* mBoundingResizeRectangle;
563  QGraphicsLineItem* mHAlignSnapItem;
564  QGraphicsLineItem* mVAlignSnapItem;
565 
567  bool mFrame;
573  Qt::PenJoinStyle mFrameJoinStyle;
574 
578 
581 
584 
589 
591  QPainter::CompositionMode mBlendMode;
594 
597 
600 
605 
608 
611 
617 
621  virtual void drawSelectionBoxes( QPainter* p );
622 
624  virtual void drawFrame( QPainter* p );
625 
627  virtual void drawBackground( QPainter* p );
628 
631  double rectHandlerBorderTolerance() const;
632 
636  double horizontalViewScaleFactor() const;
637 
638  //some utility functions
639 
641  QGraphicsLineItem* hAlignSnapItem();
642  void deleteHAlignSnapItem();
644  QGraphicsLineItem* vAlignSnapItem();
645  void deleteVAlignSnapItem();
646  void deleteAlignItems();
647 
659  QRectF evalItemRect( const QRectF &newRect, const bool resizeOnly = false, const QgsExpressionContext* context = nullptr );
660 
665  bool shouldDrawItem() const;
666 
667  signals:
669  void itemRotationChanged( double newRotation );
671  void sizeChanged();
672 
676  void frameChanged();
677 
681  void lockChanged();
682 
683  private:
684  // id (not unique)
685  QString mId;
686  // name (unique)
687  QString mUuid;
688  // name (temporary when loaded from template)
689  QString mTemplateUuid;
690  // true if composition manages the z value for this item
691  bool mCompositionManagesZValue;
692 
700  void refreshRotation( const bool updateItem = true, const bool rotateAroundCenter = false, const QgsExpressionContext &context = QgsExpressionContext() );
701 
708  void refreshTransparency( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
709 
713  void refreshBlendMode( const QgsExpressionContext &context );
714 
715  void init( const bool manageZValue );
716 
717  friend class QgsComposerItemGroup; // to access mTemplateUuid
718 };
719 
720 #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.
A container class for data source field mapping or expression.
virtual QgsExpressionContext createExpressionContext() const
Creates an expression context relating to the objects&#39; current state.
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
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.
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.
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.
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.
Qt::PenJoinStyle mFrameJoinStyle
Frame join style.
QColor mBackgroundColor
Background color.
QGraphicsLineItem * mVAlignSnapItem
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.
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)
Zoom and recenter content to point.
double mItemRotation
Item rotation in degrees, clockwise.
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.