QGIS API Documentation  2.99.0-Master (314842d)
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 QgsComposition;
34 class QgsComposerEffect;
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
97 
100  enum ZoomMode
101  {
102  Zoom = 0,
105  NoZoom
106  };
107 
111  QgsComposerItem( QgsComposition *composition, bool manageZValue = true );
112 
120  QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition *composition, bool manageZValue = true );
121  virtual ~QgsComposerItem();
122 
124  virtual int type() const override { return ComposerItem; }
125 
133  virtual bool isRemoved() const { return mRemovedFromComposition; }
134 
142  void setIsRemoved( const bool removed ) { mRemovedFromComposition = removed; }
143 
145  virtual void setSelected( bool s );
146 
148  virtual bool selected() const { return QGraphicsRectItem::isSelected(); }
149 
151  void move( double dx, double dy );
152 
156  virtual void moveContent( double dx, double dy ) { Q_UNUSED( dx ); Q_UNUSED( dy ); }
157 
164  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) { Q_UNUSED( factor ); Q_UNUSED( point ); Q_UNUSED( mode ); }
165 
172  int page() const;
173 
180  QPointF pagePos() const;
181 
190  void updatePagePos( double newPageWidth, double newPageHeight );
191 
199  void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft, int page = -1 );
200 
211  void setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint = UpperLeft, bool posIncludesFrame = false, int page = -1 );
212 
215  ItemPositionMode lastUsedPositionMode() { return mLastUsedPositionMode; }
216 
219  virtual void setSceneRect( const QRectF &rectangle );
220 
222  bool _writeXml( QDomElement &itemElem, QDomDocument &doc ) const;
223 
225  bool _readXml( const QDomElement &itemElem, const QDomDocument &doc );
226 
234  bool hasFrame() const {return mFrame;}
235 
243  virtual void setFrameEnabled( const bool drawFrame );
244 
253  virtual void setFrameStrokeColor( const QColor &color );
254 
263  QColor frameStrokeColor() const { return mFrameColor; }
264 
273  virtual void setFrameStrokeWidth( const double strokeWidth );
274 
283  double frameStrokeWidth() const { return mFrameWidth; }
284 
293  Qt::PenJoinStyle frameJoinStyle() const { return mFrameJoinStyle; }
294 
303  void setFrameJoinStyle( const Qt::PenJoinStyle style );
304 
312  virtual double estimatedFrameBleed() const;
313 
322  virtual QRectF rectWithFrame() const;
323 
329  bool hasBackground() const {return mBackground;}
330 
337  void setBackgroundEnabled( const bool drawBackground ) { mBackground = drawBackground; }
338 
344  QColor backgroundColor() const { return mBackgroundColor; }
345 
352  void setBackgroundColor( const QColor &backgroundColor );
353 
358  QPainter::CompositionMode blendMode() const { return mBlendMode; }
359 
364  void setBlendMode( const QPainter::CompositionMode blendMode );
365 
370  int transparency() const { return mTransparency; }
371 
376  void setTransparency( const int transparency );
377 
384  bool effectsEnabled() const { return mEffectsEnabled; }
385 
392  void setEffectsEnabled( const bool effectsEnabled );
393 
395  virtual void addItem( QgsComposerItem *item ) { Q_UNUSED( item ); }
396  virtual void removeItems() {}
397 
398  virtual void beginItemCommand( const QString &text ) { beginCommand( text ); }
399 
403  void beginCommand( const QString &commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );
404 
405  virtual void endItemCommand() { endCommand(); }
407  void endCommand();
408  void cancelCommand();
409 
410  //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
411  //painter down by the same factor for drawing
412 
417  void setPositionLock( const bool lock );
418 
423  bool positionLock() const { return mItemPositionLocked; }
424 
433  double itemRotation( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
434 
440  virtual void updateItem();
441 
446  QString id() const { return mId; }
447 
452  virtual void setId( const QString &id );
453 
460  QString uuid() const { return mUuid; }
461 
469  virtual QString displayName() const;
470 
478  virtual void setVisibility( const bool visible );
479 
487  bool excludeFromExports( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
488 
494  virtual void setExcludeFromExports( const bool exclude );
495 
501  bool isGroupMember() const { return mIsGroupMember; }
502 
508  void setIsGroupMember( const bool isGroupMember );
509 
516  virtual int numberExportLayers() const { return 0; }
517 
523  virtual void setCurrentExportLayer( const int layerIdx = -1 ) { mCurrentExportLayer = layerIdx; }
524 
529  virtual QgsExpressionContext createExpressionContext() const override;
530 
539  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
540 
549  bool updatesEnabled() const { return mUpdatesEnabled; }
550 
551  public slots:
552 
560  virtual void setItemRotation( const double r, const bool adjustPosition = false );
561 
562  void repaint() override;
563 
572  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
573 
574  protected:
577 
583 
585  QGraphicsRectItem *mBoundingResizeRectangle = nullptr;
586  QGraphicsLineItem *mHAlignSnapItem = nullptr;
587  QGraphicsLineItem *mVAlignSnapItem = nullptr;
588 
590  bool mFrame;
592  QColor mFrameColor;
594  double mFrameWidth = 0.3;
596  Qt::PenJoinStyle mFrameJoinStyle = Qt::MiterJoin;
597 
602 
606 
609 
612 
617 
619  QPainter::CompositionMode mBlendMode;
621  QgsComposerEffect *mEffect = nullptr;
622 
625 
628 
633 
636 
639 
645 
649  virtual void drawSelectionBoxes( QPainter *p );
650 
652  virtual void drawFrame( QPainter *p );
653 
655  virtual void drawBackground( QPainter *p );
656 
659  double rectHandlerBorderTolerance() const;
660 
664  double horizontalViewScaleFactor() const;
665 
666  //some utility functions
667 
669  QGraphicsLineItem *hAlignSnapItem();
670  void deleteHAlignSnapItem();
672  QGraphicsLineItem *vAlignSnapItem();
673  void deleteVAlignSnapItem();
674  void deleteAlignItems();
675 
687  QRectF evalItemRect( const QRectF &newRect, const bool resizeOnly = false, const QgsExpressionContext *context = nullptr );
688 
693  bool shouldDrawItem() const;
694 
695  signals:
697  void itemRotationChanged( double newRotation );
699  void sizeChanged();
700 
704  void frameChanged();
705 
709  void lockChanged();
710 
711  private:
712  // id (not unique)
713  QString mId;
714  // name (unique)
715  QString mUuid;
716  // name (temporary when loaded from template)
717  QString mTemplateUuid;
718  // true if composition manages the z value for this item
719  bool mCompositionManagesZValue;
720 
725  bool mUpdatesEnabled = true;
726 
734  void refreshRotation( const bool updateItem = true, const bool rotateAroundCenter = false, const QgsExpressionContext &context = QgsExpressionContext() );
735 
742  void refreshTransparency( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
743 
749  void refreshFrameColor( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
750 
756  void refreshBackgroundColor( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
757 
761  void refreshBlendMode( const QgsExpressionContext &context );
762 
763  void init( const bool manageZValue );
764 
765  friend class QgsComposerItemGroup; // to access mTemplateUuid
766 };
767 
768 #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.
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.