QGIS API Documentation  2.99.0-Master (08c2e66)
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 "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 
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 = nullptr;
562  QGraphicsLineItem* mHAlignSnapItem = nullptr;
563  QGraphicsLineItem* mVAlignSnapItem = nullptr;
564 
566  bool mFrame;
568  QColor mFrameColor;
570  double mFrameWidth = 0.3;
572  Qt::PenJoinStyle mFrameJoinStyle = Qt::MiterJoin;
573 
578 
582 
585 
588 
593 
595  QPainter::CompositionMode mBlendMode;
597  QgsComposerEffect *mEffect = nullptr;
598 
601 
604 
609 
612 
615 
621 
625  virtual void drawSelectionBoxes( QPainter* p );
626 
628  virtual void drawFrame( QPainter* p );
629 
631  virtual void drawBackground( QPainter* p );
632 
635  double rectHandlerBorderTolerance() const;
636 
640  double horizontalViewScaleFactor() const;
641 
642  //some utility functions
643 
645  QGraphicsLineItem* hAlignSnapItem();
646  void deleteHAlignSnapItem();
648  QGraphicsLineItem* vAlignSnapItem();
649  void deleteVAlignSnapItem();
650  void deleteAlignItems();
651 
663  QRectF evalItemRect( const QRectF &newRect, const bool resizeOnly = false, const QgsExpressionContext* context = nullptr );
664 
669  bool shouldDrawItem() const;
670 
671  signals:
673  void itemRotationChanged( double newRotation );
675  void sizeChanged();
676 
680  void frameChanged();
681 
685  void lockChanged();
686 
687  private:
688  // id (not unique)
689  QString mId;
690  // name (unique)
691  QString mUuid;
692  // name (temporary when loaded from template)
693  QString mTemplateUuid;
694  // true if composition manages the z value for this item
695  bool mCompositionManagesZValue;
696 
704  void refreshRotation( const bool updateItem = true, const bool rotateAroundCenter = false, const QgsExpressionContext &context = QgsExpressionContext() );
705 
712  void refreshTransparency( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
713 
719  void refreshFrameColor( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
720 
726  void refreshBackgroundColor( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
727 
731  void refreshBlendMode( const QgsExpressionContext &context );
732 
733  void init( const bool manageZValue );
734 
735  friend class QgsComposerItemGroup; // to access mTemplateUuid
736 };
737 
738 #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.
bool effectsEnabled() const
Returns whether effects (e.g., blend modes) are enabled for the item.
int mTransparency
Item transparency.
A base class for objects which belong to a map composition.
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.
virtual bool selected() const
Is selected.
virtual void removeItems()
Qt::PenJoinStyle frameJoinStyle() const
Returns the join style used for drawing the item&#39;s frame.
double frameStrokeWidth() const
Returns the frame&#39;s stroke width.
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...
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.
virtual void zoomContent(const double factor, const QPointF point, const ZoomMode mode=QgsComposerItem::Zoom)
Zoom content of item.
QColor backgroundColor() const
Gets the background color for this 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.
QString uuid() const
Get item identification name.
QColor frameStrokeColor() const
Returns the frame&#39;s stroke color.
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&#39;s transparency.
virtual void updateItem()
Updates item, with the possibility to do custom update for subclasses.
bool mFrame
True if item fram needs to be painted.
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.
virtual void repaint()
Triggers a redraw for the item.
bool mRemovedFromComposition
True if item has been removed from the composition.
virtual QgsExpressionContext createExpressionContext() const
Creates an expression context relating to the objects&#39; current state.
virtual int numberExportLayers() const
Get the number of layers that this item requires for exporting as layers.
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.
QPainter::CompositionMode blendMode() const
Returns the item&#39;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.
bool mBackground
True if item background needs to be painted.
bool isGroupMember() const
Returns whether this item is part of a group.
bool mIsGroupMember
Whether or not this item is part of a group.
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.
Zoom while maintaining relative position of point.
QString id() const
Get item&#39;s id (which is not necessarly unique)