QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgslayoutitemnodeitem.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutitemnodeitem.h
3  begin : March 2016
4  copyright : (C) 2016 Paul Blottiere, Oslandia
5  email : paul dot blottiere at oslandia dot com
6  ***************************************************************************/
7 
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #ifndef QGSLAYOUTITEMNODEITEM_H
18 #define QGSLAYOUTITEMNODEITEM_H
19 
20 #include "qgis_core.h"
21 #include "qgslayoutitem.h"
22 
29 class CORE_EXPORT QgsLayoutNodesItem: public QgsLayoutItem
30 {
31  Q_OBJECT
32 
33  public:
34 
39  void setNodes( const QPolygonF &nodes );
40 
45  QPolygonF nodes() const { return mPolygon; }
46 
55  bool addNode( QPointF point, bool checkArea = true, double radius = 10 );
56 
60  void setDisplayNodes( bool display = true ) { mDrawNodes = display; }
61 
67  bool moveNode( int index, QPointF node );
68 
77  int nodeAtPosition( QPointF point, bool searchInRadius = true, double radius = 10 ) const;
78 
85  bool nodePosition( int index, QPointF &position ) const;
86 
90  bool removeNode( int index );
91 
93  int nodesSize() const { return mPolygon.size(); }
94 
98  bool setSelectedNode( int index );
99 
103  int selectedNode() const { return mSelectedNode; }
104 
108  void deselectNode() { mSelectedNode = -1; }
109 
110  // Depending on the symbol style, the bounding rectangle can be larger than the shape
111  QRectF boundingRect() const override;
112 
113  // Reimplement estimatedFrameBleed, since frames on shapes are drawn using symbology
114  // rather than the item's pen
115  double estimatedFrameBleed() const override;
116 
117  protected:
118 
122  QgsLayoutNodesItem( QgsLayout *layout );
123 
127  QgsLayoutNodesItem( const QPolygonF &polygon, QgsLayout *layout );
128 
129  void draw( QgsLayoutItemRenderContext &context ) override;
130 
131  bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
132  bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
133 
135  QPolygonF mPolygon;
136 
138  double mMaxSymbolBleed = 0.0;
139 
141  virtual bool _addNode( int nodeIndex, QPointF newNode, double radius ) = 0;
142 
144  virtual bool _removeNode( int nodeIndex ) = 0;
145 
147  virtual void _draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) = 0;
148 
150  virtual void _readXmlStyle( const QDomElement &elmt, const QgsReadWriteContext &context ) = 0;
151 
153  virtual void _writeXmlStyle( QDomDocument &doc, QDomElement &elmt, const QgsReadWriteContext &context ) const = 0;
154 
159  void rescaleToFitBoundingBox();
160 
162  double computeDistance( QPointF pt1, QPointF pt2 ) const;
163 
165  void updateSceneRect();
166 
169 
170  protected slots:
171 
176  virtual void updateBoundingRect();
177 
178  private:
179 
180  void init();
181 
183  int mSelectedNode = -1;
184 
188  bool mDrawNodes = false;
189 
191  void drawNodes( QgsLayoutItemRenderContext &context ) const;
192  void drawSelectedNode( QgsLayoutItemRenderContext &context ) const;
193 
194 };
195 
196 #endif // QGSLAYOUTITEMNODEITEM_H
The class is used as a container of context for various read/write operations on other objects...
Base class for graphical items within a QgsLayout.
QRectF mCurrentRectangle
Current bounding rectangle of shape.
virtual double estimatedFrameBleed() const
Returns the estimated amount the item's frame bleeds outside the item's actual rectangle.
An abstract layout item that provides generic methods for node based shapes such as polygon or polyli...
QPolygonF mPolygon
Shape's nodes.
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
void deselectNode()
Deselects any selected nodes.
int selectedNode() const
Returns the currently selected node, or -1 if no node is selected.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:44
QPolygonF nodes() const
Returns the nodes the shape consists of.
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
int nodesSize() const
Returns the number of nodes in the shape.
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item's contents using the specified item render context.
void setDisplayNodes(bool display=true)
Set whether the item's nodes should be displayed.