QGIS API Documentation  2.99.0-Master (e077efd)
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 "qgscomposerobject.h"
22 #include <QGraphicsRectItem>
23 #include <QObject>
24 #include <QPainter>
25 
26 class QWidget;
27 class QDomDocument;
28 class QDomElement;
29 class QGraphicsLineItem;
31 class QgsDataDefined;
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 setFrameOutlineColor( const QColor& color );
254 
263  QColor frameOutlineColor() const { return pen().color(); }
264 
273  virtual void setFrameOutlineWidth( const double outlineWidth );
274 
283  double frameOutlineWidth() const { return pen().widthF(); }
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 
436  virtual void updateItem() { QGraphicsRectItem::update(); }
437 
442  QString id() const { return mId; }
443 
448  virtual void setId( const QString& id );
449 
456  QString uuid() const { return mUuid; }
457 
465  virtual QString displayName() const;
466 
474  virtual void setVisibility( const bool visible );
475 
483  bool excludeFromExports( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
484 
490  virtual void setExcludeFromExports( const bool exclude );
491 
497  bool isGroupMember() const { return mIsGroupMember; }
498 
504  void setIsGroupMember( const bool isGroupMember );
505 
512  virtual int numberExportLayers() const { return 0; }
513 
519  virtual void setCurrentExportLayer( const int layerIdx = -1 ) { mCurrentExportLayer = layerIdx; }
520 
525  virtual QgsExpressionContext createExpressionContext() const override;
526 
527  public slots:
528 
536  virtual void setItemRotation( const double r, const bool adjustPosition = false );
537 
538  void repaint() override;
539 
548  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
549 
550  protected:
553 
559 
561  QGraphicsRectItem* mBoundingResizeRectangle;
562  QGraphicsLineItem* mHAlignSnapItem;
563  QGraphicsLineItem* mVAlignSnapItem;
564 
566  bool mFrame;
572  Qt::PenJoinStyle mFrameJoinStyle;
573 
577 
580 
583 
588 
590  QPainter::CompositionMode mBlendMode;
593 
596 
599 
604 
607 
610 
616 
620  virtual void drawSelectionBoxes( QPainter* p );
621 
623  virtual void drawFrame( QPainter* p );
624 
626  virtual void drawBackground( QPainter* p );
627 
630  double rectHandlerBorderTolerance() const;
631 
635  double horizontalViewScaleFactor() const;
636 
637  //some utility functions
638 
640  QGraphicsLineItem* hAlignSnapItem();
641  void deleteHAlignSnapItem();
643  QGraphicsLineItem* vAlignSnapItem();
644  void deleteVAlignSnapItem();
645  void deleteAlignItems();
646 
658  QRectF evalItemRect( const QRectF &newRect, const bool resizeOnly = false, const QgsExpressionContext* context = nullptr );
659 
664  bool shouldDrawItem() const;
665 
666  signals:
668  void itemRotationChanged( double newRotation );
670  void sizeChanged();
671 
675  void frameChanged();
676 
680  void lockChanged();
681 
682  private:
683  // id (not unique)
684  QString mId;
685  // name (unique)
686  QString mUuid;
687  // name (temporary when loaded from template)
688  QString mTemplateUuid;
689  // true if composition manages the z value for this item
690  bool mCompositionManagesZValue;
691 
699  void refreshRotation( const bool updateItem = true, const bool rotateAroundCenter = false, const QgsExpressionContext &context = QgsExpressionContext() );
700 
707  void refreshTransparency( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
708 
712  void refreshBlendMode( const QgsExpressionContext &context );
713 
714  void init( const bool manageZValue );
715 
716  friend class QgsComposerItemGroup; // to access mTemplateUuid
717 };
718 
719 #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 (eg 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.