QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgslayoutviewtooleditnodes.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutviewtooleditnodes.cpp
3 ---------------------------
4 Date : July 2017
5 Copyright : (C) 2017 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgslayoutviewtooleditnodes.cpp"
19#include "qgslayoutview.h"
20#include "qgslayout.h"
22#include "qgslayoutundostack.h"
23
30
32{
33 if ( mNodesItem && mNodesItemIndex != -1 )
34 {
35 layout()->undoStack()->beginCommand( mNodesItem, tr( "Remove Item Node" ) );
36 if ( mNodesItem->removeNode( mNodesItemIndex ) )
37 {
39 if ( mNodesItem->nodesSize() > 0 )
40 {
41 mNodesItemIndex = mNodesItem->selectedNode();
42 // setSelectedNode( mNodesItem, mNodesItemIndex );
43 }
44 else
45 {
46 mNodesItemIndex = -1;
47 mNodesItem = nullptr;
48 }
49 if ( mNodesItem )
50 mNodesItem->update();
51 }
52 else
53 {
55 }
56 }
57}
58
60{
61 displayNodes( true );
63}
64
66{
67 if ( event->button() != Qt::LeftButton )
68 {
69 event->ignore();
70 return;
71 }
72
73 const QList<QGraphicsItem *> itemsAtCursorPos = view()->items( event->pos().x(), event->pos().y(), mMoveContentSearchRadius, mMoveContentSearchRadius );
74 if ( itemsAtCursorPos.isEmpty() )
75 return;
76
77 mNodesItemIndex = -1;
78 mNodesItem = nullptr;
79 isMoving = false;
80
81 for ( QGraphicsItem *graphicsItem : itemsAtCursorPos )
82 {
83 QgsLayoutNodesItem *item = dynamic_cast<QgsLayoutNodesItem *>( graphicsItem );
84
85 if ( item && !item->isLocked() )
86 {
87 int index = item->nodeAtPosition( event->layoutPoint() );
88 if ( index != -1 )
89 {
90 mNodesItemIndex = index;
91 mNodesItem = item;
92 mMoveContentStartPos = event->layoutPoint();
93 }
94 }
95
96 if ( mNodesItem && mNodesItemIndex != -1 )
97 {
98 layout()->undoStack()->beginCommand( mNodesItem, tr( "Move Item Node" ) );
99 setSelectedNode( mNodesItem, mNodesItemIndex );
100 isMoving = true;
101 break;
102 }
103 }
104}
105
107{
108 if ( !isMoving )
109 {
110 event->ignore();
111 return;
112 }
113
114 if ( mNodesItem && mNodesItemIndex != -1 && event->layoutPoint() != mMoveContentStartPos )
115 {
116 mNodesItem->moveNode( mNodesItemIndex, event->snappedPoint() );
117 }
118}
119
121{
122 if ( event->button() != Qt::LeftButton || !isMoving )
123 {
124 event->ignore();
125 return;
126 }
127
128 isMoving = false;
129 if ( mNodesItemIndex != -1 )
130 {
131 if ( event->layoutPoint() != mMoveContentStartPos )
132 {
134 }
135 else
136 {
138 }
139 }
140}
141
143{
144 if ( event->button() != Qt::LeftButton )
145 {
146 event->ignore();
147 return;
148 }
149
150 // erase status previously set by the mousePressEvent method
151 if ( mNodesItemIndex != -1 )
152 {
153 mNodesItem = nullptr;
154 mNodesItemIndex = -1;
155 deselectNodes();
156 }
157
158 // search items in layout
159 const QList<QGraphicsItem *> itemsAtCursorPos = view()->items( event->pos().x(), event->pos().y(), mMoveContentSearchRadius, mMoveContentSearchRadius );
160
161 if ( itemsAtCursorPos.isEmpty() )
162 return;
163
164 bool rc = false;
165 for ( QGraphicsItem *graphicsItem : itemsAtCursorPos )
166 {
167 QgsLayoutNodesItem *item = dynamic_cast<QgsLayoutNodesItem *>( graphicsItem );
168
169 if ( item && !item->isLocked() )
170 {
171 layout()->undoStack()->beginCommand( item, tr( "Add Item Node" ) );
172 rc = item->addNode( event->layoutPoint() );
173
174 if ( rc )
175 {
177 mNodesItem = item;
178 mNodesItemIndex = mNodesItem->nodeAtPosition( event->layoutPoint() );
179 }
180 else
182 }
183
184 if ( rc )
185 break;
186 }
187
188 if ( rc )
189 {
190 setSelectedNode( mNodesItem, mNodesItemIndex );
191 mNodesItem->update();
192 }
193}
194
196{
197 if ( mNodesItem && mNodesItemIndex != -1 && ( event->key() == Qt::Key_Left || event->key() == Qt::Key_Right || event->key() == Qt::Key_Up || event->key() == Qt::Key_Down ) )
198 {
199 QPointF currentPos;
200
201 if ( mNodesItem->nodePosition( mNodesItemIndex, currentPos ) )
202 {
203 QPointF delta = view()->deltaForKeyEvent( event );
204
205 currentPos.setX( currentPos.x() + delta.x() );
206 currentPos.setY( currentPos.y() + delta.y() );
207
208 layout()->undoStack()->beginCommand( mNodesItem, tr( "Move Item Node" ), QgsLayoutItem::UndoNodeMove );
209 mNodesItem->moveNode( mNodesItemIndex, currentPos );
211 layout()->update();
212 }
213 }
214 else
215 {
216 event->ignore();
217 }
218}
219
221{
222 displayNodes( false );
223 deselectNodes();
225}
226
228{
229 QList<QgsLayoutItem *> items;
230 if ( mNodesItem )
231 items << mNodesItem;
232 return items;
233}
234
235void QgsLayoutViewToolEditNodes::displayNodes( bool display )
236{
237 QList<QgsLayoutNodesItem *> nodesShapes;
238 layout()->layoutItems( nodesShapes );
239
240 for ( QgsLayoutNodesItem *item : std::as_const( nodesShapes ) )
241 {
242 item->setDisplayNodes( display );
243 item->update();
244 }
245}
246
247void QgsLayoutViewToolEditNodes::deselectNodes()
248{
249 QList<QgsLayoutNodesItem *> nodesShapes;
250 layout()->layoutItems( nodesShapes );
251
252 for ( QgsLayoutNodesItem *item : std::as_const( nodesShapes ) )
253 {
254 item->deselectNode();
255 item->update();
256 }
257}
258
259void QgsLayoutViewToolEditNodes::setSelectedNode( QgsLayoutNodesItem *shape, int index )
260{
261 QList<QgsLayoutNodesItem *> nodesShapes;
262 layout()->layoutItems( nodesShapes );
263
264 for ( QgsLayoutNodesItem *item : std::as_const( nodesShapes ) )
265 {
266 if ( item == shape )
267 {
268 item->setSelectedNode( index );
269 layout()->setSelectedItem( item );
270 item->update();
271 }
272 else
273 {
274 item->deselectNode();
275 item->update();
276 }
277 }
278}
@ UndoNodeMove
Node move.
bool isLocked() const
Returns true if the item is locked, and cannot be interacted with using the mouse.
An abstract layout item that provides generic methods for node based shapes such as polygon or polyli...
bool addNode(QPointF point, bool checkArea=true, double radius=10)
Add a node in current shape.
int nodeAtPosition(QPointF point, bool searchInRadius=true, double radius=10) const
Search for the nearest node in the shape within a maximal area.
void endCommand()
Saves final state of an object and pushes the active command to the undo history.
void cancelCommand()
Cancels the active command, discarding it without pushing to the undo history.
void beginCommand(QgsLayoutUndoObjectInterface *object, const QString &commandText, int id=0)
Begins a new undo command for the specified object.
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.
QPointF layoutPoint() const
Returns the event point location in layout coordinates.
void layoutMoveEvent(QgsLayoutViewMouseEvent *event) override
Mouse move event for overriding.
void layoutDoubleClickEvent(QgsLayoutViewMouseEvent *event) override
Mouse double-click event for overriding.
void activate() override
Called when tool is set as the currently active layout tool.
QList< QgsLayoutItem * > ignoredSnapItems() const override
Returns a list of items which should be ignored while snapping events for this tool.
void layoutPressEvent(QgsLayoutViewMouseEvent *event) override
Mouse press event for overriding.
QgsLayoutViewToolEditNodes(QgsLayoutView *view)
Constructor for QgsLayoutViewToolEditNodes.
void keyPressEvent(QKeyEvent *event) override
Key press event for overriding.
void layoutReleaseEvent(QgsLayoutViewMouseEvent *event) override
Mouse release event for overriding.
void deactivate() override
Called when tool is deactivated.
void deleteSelectedNode()
Deletes the selected node from the item.
Abstract base class for all layout view tools.
void setCursor(const QCursor &cursor)
Sets a user defined cursor for use when the tool is active.
QgsLayoutView * view() const
Returns the view associated with the tool.
virtual void deactivate()
Called when tool is deactivated.
void setFlags(QgsLayoutViewTool::Flags flags)
Sets the combination of flags that will be used for the tool.
@ FlagSnaps
Tool utilizes snapped coordinates.
virtual void activate()
Called when tool is set as the currently active layout tool.
QgsLayout * layout() const
Returns the layout associated with the tool.
A graphical widget to display and interact with QgsLayouts.
QPointF deltaForKeyEvent(QKeyEvent *event)
Returns the delta (in layout coordinates) by which to move items for the given key event.
void layoutItems(QList< T * > &itemList) const
Returns a list of layout items of a specific type.
Definition qgslayout.h:120
void setSelectedItem(QgsLayoutItem *item)
Clears any selected items and sets item as the current selection.
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...