17#include "moc_qgslayoutviewtooladdnodeitem.cpp"
31#include <QGraphicsRectItem>
45 mItemMetadataId = metadataId;
50 if ( event->button() == Qt::LeftButton )
57 layout()->addItem( mRubberBand.get() );
66 else if ( event->button() == Qt::RightButton && mRubberBand )
71 mPolygon.remove( mPolygon.count() - 1 );
77 if (
QgsLayoutNodesItem *nodesItem = qobject_cast< QgsLayoutNodesItem * >( item.get() ) )
79 nodesItem->setNodes( mPolygon );
80 if ( !nodesItem->isValid() )
103 moveTemporaryNode( event->
snappedPoint(), event->modifiers() );
122 if ( !mRubberBand || event->isAutoRepeat() )
128 if ( event->key() == Qt::Key_Delete || event->key() == Qt::Key_Backspace )
130 if ( mPolygon.size() > 2 )
134 setRubberBandNodes();
142 else if ( event->key() == Qt::Key_Escape )
162void QgsLayoutViewToolAddNodeItem::addNode( QPointF scenePoint )
164 mPolygon.append( scenePoint );
166 if ( mPolygon.size() == 1 )
167 mPolygon.append( scenePoint );
169 setRubberBandNodes();
172void QgsLayoutViewToolAddNodeItem::moveTemporaryNode( QPointF scenePoint, Qt::KeyboardModifiers modifiers )
174 if ( mPolygon.isEmpty() )
177 if ( mPolygon.size() > 1 && ( modifiers & Qt::ShiftModifier ) )
179 const QPointF start = mPolygon.at( mPolygon.size() - 2 );
180 QLineF newLine = QLineF( start, scenePoint );
184 newLine.setAngle( angle );
185 scenePoint = newLine.p2();
188 mPolygon.replace( mPolygon.size() - 1, scenePoint );
189 setRubberBandNodes();
192void QgsLayoutViewToolAddNodeItem::setRubberBandNodes()
194 if ( QGraphicsPolygonItem *polygonItem =
dynamic_cast< QGraphicsPolygonItem *
>( mRubberBand.get() ) )
196 polygonItem->setPolygon( mPolygon );
198 else if ( QGraphicsPathItem *polylineItem =
dynamic_cast< QGraphicsPathItem *
>( mRubberBand.get() ) )
202 path.addPolygon( mPolygon );
203 polylineItem->setPath( path );
209 return mItemMetadataId;
static QgsLayoutItemGuiRegistry * layoutItemGuiRegistry()
Returns the global layout item GUI registry, used for registering the GUI behavior of layout items.
Base class for graphical items within a QgsLayout.
An abstract layout item that provides generic methods for node based shapes such as polygon or polyli...
static double snappedAngle(double angle)
Snaps an angle (in degrees) to its closest 45 degree angle.
A QgsLayoutViewMouseEvent is the result of a user interaction with the mouse on a QgsLayoutView.
QPointF snappedPoint() const
Returns the snapped event point location in layout coordinates.
A graphical widget to display and interact with QgsLayouts.
void setSelectedItem(QgsLayoutItem *item)
Clears any selected items and sets item as the current selection.
void addLayoutItem(QgsLayoutItem *item)
Adds an item to the layout.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)