17#include "moc_qgslayoutviewtooladdnodeitem.cpp"
31#include <QGraphicsRectItem>
45 mItemMetadataId = metadataId;
50 if ( event->button() == Qt::LeftButton )
57 layout()->addItem( mRubberBand.get() );
69 else if ( event->button() == Qt::RightButton && mRubberBand )
74 mPolygon.remove( mPolygon.count() - 1 );
80 if (
QgsLayoutNodesItem *nodesItem = qobject_cast<QgsLayoutNodesItem *>( item.get() ) )
82 nodesItem->setNodes( mPolygon );
83 if ( !nodesItem->isValid() )
105 moveTemporaryNode( event->
snappedPoint(), event->modifiers() );
124 if ( !mRubberBand || event->isAutoRepeat() )
130 if ( event->key() == Qt::Key_Delete || event->key() == Qt::Key_Backspace )
132 if ( mPolygon.size() > 2 )
136 setRubberBandNodes();
137 moveTemporaryNode(
view()->mapToScene(
view()->mapFromGlobal( QCursor::pos() ) ), event->modifiers() );
145 else if ( event->key() == Qt::Key_Escape )
165void QgsLayoutViewToolAddNodeItem::addNode( QPointF scenePoint )
167 mPolygon.append( scenePoint );
169 if ( mPolygon.size() == 1 )
170 mPolygon.append( scenePoint );
172 setRubberBandNodes();
175void QgsLayoutViewToolAddNodeItem::moveTemporaryNode( QPointF scenePoint, Qt::KeyboardModifiers modifiers )
177 if ( mPolygon.isEmpty() )
180 if ( mPolygon.size() > 1 && ( modifiers & Qt::ShiftModifier ) )
182 const QPointF start = mPolygon.at( mPolygon.size() - 2 );
183 QLineF newLine = QLineF( start, scenePoint );
187 newLine.setAngle( angle );
188 scenePoint = newLine.p2();
191 mPolygon.replace( mPolygon.size() - 1, scenePoint );
192 setRubberBandNodes();
195void QgsLayoutViewToolAddNodeItem::setRubberBandNodes()
197 QList<QGraphicsItem *> items = mRubberBand->childItems();
202 if ( items.isEmpty() )
204 items << mRubberBand.get();
207 if ( QGraphicsPolygonItem *polygonItem =
dynamic_cast<QGraphicsPolygonItem *
>( items[0] ) )
210 if ( items.size() == 2 &&
dynamic_cast<QGraphicsPolygonItem *
>( items[1] ) !=
nullptr )
212 if ( mPolygon.size() > 3 )
214 polygonItem->setPolygon( QPolygonF( mPolygon.mid( 0, mPolygon.size() - 1 ) ) );
218 polygonItem->setPolygon( QPolygonF() );
220 dynamic_cast<QGraphicsPolygonItem *
>( items[1] )->setPolygon( mPolygon );
225 polygonItem->setPolygon( mPolygon );
228 else if ( QGraphicsPathItem *polylineItem =
dynamic_cast<QGraphicsPathItem *
>( items[0] ) )
231 if ( items.size() == 2 &&
dynamic_cast<QGraphicsPathItem *
>( items[1] ) !=
nullptr )
233 if ( mPolygon.size() > 2 )
236 path.addPolygon( QPolygonF( mPolygon.mid( 0, mPolygon.size() - 1 ) ) );
237 polylineItem->setPath( path );
241 polylineItem->setPath( QPainterPath() );
243 if ( mPolygon.size() > 1 )
246 path.addPolygon( mPolygon.mid( mPolygon.size() - 2 ) );
247 dynamic_cast<QGraphicsPathItem *
>( items[1] )->setPath( path );
255 path.addPolygon( mPolygon );
256 polylineItem->setPath( path );
263 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 deselectAll()
Deselects all items in the view.
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)