KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_move_tool.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2019 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <algorithm>
22#include <cmath>
23#include <memory>
24#include <optional>
25#include <set>
26#include <wx/log.h>
27#include <wx/utils.h>
28#include <trigo.h>
30#include <tool/tool_manager.h>
35#include <tools/sch_move_tool.h>
36
37#include <sch_actions.h>
38#include <sch_commit.h>
39#include <eda_item.h>
40#include <sch_group.h>
41#include <sch_item.h>
42#include <sch_symbol.h>
43#include <sch_sheet.h>
44#include <sch_sheet_pin.h>
45#include <sch_line.h>
46#include <sch_connection.h>
47#include <sch_junction.h>
48#include <junction_helpers.h>
49#include <sch_edit_frame.h>
50#include <widgets/wx_infobar.h>
51#include <eeschema_id.h>
52#include <pgm_base.h>
53#include <view/view_controls.h>
55#include <math/box2.h>
56#include <base_units.h>
57#include <sch_screen.h>
58#include <sch_item_alignment.h>
59#include <trace_helpers.h>
60
61
62// For adding to or removing from selections
63#define QUIET_MODE true
64
65
66static bool isGraphicItemForDrop( const SCH_ITEM* aItem )
67{
68 switch( aItem->Type() )
69 {
70 case SCH_SHAPE_T:
71 case SCH_BITMAP_T:
72 case SCH_TEXT_T:
73 case SCH_TEXTBOX_T:
74 return true;
75 case SCH_LINE_T:
76 return static_cast<const SCH_LINE*>( aItem )->IsGraphicLine();
77 default:
78 return false;
79 }
80}
81
82
83// Distance of a point along a sheet border, clockwise from the top-left corner.
84static long long sheetBorderArc( const SCH_SHEET* aSheet, SHEET_SIDE aSide, const VECTOR2I& aPos )
85{
86 long long left = aSheet->GetPosition().x;
87 long long top = aSheet->GetPosition().y;
88 long long w = aSheet->GetSize().x;
89 long long h = aSheet->GetSize().y;
90 long long right = left + w;
91 long long bot = top + h;
92
93 switch( aSide )
94 {
95 case SHEET_SIDE::TOP: return aPos.x - left;
96 case SHEET_SIDE::RIGHT: return w + ( aPos.y - top );
97 case SHEET_SIDE::BOTTOM: return w + h + ( right - aPos.x );
98 case SHEET_SIDE::LEFT: return 2 * w + h + ( bot - aPos.y );
99 default: return 0;
100 }
101}
102
103
104// Inverse of sheetBorderArc(), turns a border distance back into a point and edge.
105static void sheetBorderPos( const SCH_SHEET* aSheet, long long aArc, VECTOR2I& aPos, SHEET_SIDE& aSide )
106{
107 long long left = aSheet->GetPosition().x;
108 long long top = aSheet->GetPosition().y;
109 long long w = aSheet->GetSize().x;
110 long long h = aSheet->GetSize().y;
111 long long right = left + w;
112 long long bot = top + h;
113 long long perimeter = 2 * ( w + h );
114
115 if( perimeter <= 0 )
116 {
117 aPos = VECTOR2I( (int) left, (int) top );
118 aSide = SHEET_SIDE::TOP;
119 return;
120 }
121
122 aArc = ( ( aArc % perimeter ) + perimeter ) % perimeter;
123
124 if( aArc <= w )
125 {
126 aSide = SHEET_SIDE::TOP;
127 aPos = VECTOR2I( (int) ( left + aArc ), (int) top );
128 }
129 else if( aArc <= w + h )
130 {
131 aSide = SHEET_SIDE::RIGHT;
132 aPos = VECTOR2I( (int) right, (int) ( top + ( aArc - w ) ) );
133 }
134 else if( aArc <= 2 * w + h )
135 {
136 aSide = SHEET_SIDE::BOTTOM;
137 aPos = VECTOR2I( (int) ( right - ( aArc - w - h ) ), (int) bot );
138 }
139 else
140 {
141 aSide = SHEET_SIDE::LEFT;
142 aPos = VECTOR2I( (int) left, (int) ( bot - ( aArc - 2 * w - h ) ) );
143 }
144}
145
146
147static void cloneWireConnection( SCH_LINE* aNewLine, SCH_ITEM* aSource, SCH_EDIT_FRAME* aFrame )
148{
149 if( !aNewLine || !aSource || !aFrame )
150 return;
151
152 SCH_LINE* sourceLine = dynamic_cast<SCH_LINE*>( aSource );
153
154 if( !sourceLine )
155 return;
156
157 SCH_SHEET_PATH sheetPath = aFrame->GetCurrentSheet();
158 SCH_CONNECTION* sourceConnection = sourceLine->Connection( &sheetPath );
159
160 if( !sourceConnection )
161 return;
162
163 SCH_CONNECTION* newConnection = aNewLine->InitializeConnection( sheetPath, nullptr );
164
165 if( !newConnection )
166 return;
167
168 newConnection->Clone( *sourceConnection );
169}
170
171
173 SCH_TOOL_BASE<SCH_EDIT_FRAME>( "eeschema.InteractiveMove" ),
174 m_inMoveTool( false ),
175 m_moveInProgress( false ),
176 m_mode( MOVE ),
177 m_moveOffset( 0, 0 )
178{
179}
180
181
183{
185
186 auto moveCondition =
187 []( const SELECTION& aSel )
188 {
189 if( aSel.Empty() || SELECTION_CONDITIONS::OnlyTypes( { SCH_MARKER_T } )( aSel ) )
190 return false;
191
193 return false;
194
195 return true;
196 };
197
198 // Add move actions to the selection tool menu
199 //
200 CONDITIONAL_MENU& selToolMenu = m_selectionTool->GetToolMenu().GetMenu();
201
202 selToolMenu.AddItem( SCH_ACTIONS::move, moveCondition, 150 );
203 selToolMenu.AddItem( SCH_ACTIONS::drag, moveCondition, 150 );
204 selToolMenu.AddItem( SCH_ACTIONS::alignToGrid, moveCondition, 150 );
205
206 return true;
207}
208
209
211{
212 SCH_TOOL_BASE::Reset( aReason );
213
214 if( aReason == MODEL_RELOAD || aReason == SUPERMODEL_RELOAD )
215 {
216 // If we were in the middle of a move/drag operation and the model changes (e.g., sheet
217 // switch), we need to clean up our state to avoid blocking future move/drag operations
218 if( m_moveInProgress )
219 {
220 // Clear the move state
221 m_moveInProgress = false;
222 m_mode = MOVE;
223 m_moveOffset = VECTOR2I( 0, 0 );
224 m_anchorPos.reset();
225 m_breakPos.reset();
226
227 // Clear cached data that references items from the previous sheet
228 m_dragAdditions.clear();
229 m_lineConnectionCache.clear();
230 m_newDragLines.clear();
231 m_changedDragLines.clear();
232 m_specialCaseLabels.clear();
234 m_sheetPinDragArc.clear();
235 m_hiddenJunctions.clear();
236
237 // Clear any preview
238 if( m_view )
239 m_view->ClearPreview();
240 }
241 }
242}
243
244
245void SCH_MOVE_TOOL::orthoLineDrag( SCH_COMMIT* aCommit, SCH_LINE* line, const VECTOR2I& splitDelta,
246 int& xBendCount, int& yBendCount, const EE_GRID_HELPER& grid )
247{
248 // If the move is not the same angle as this move, then we need to do something special with
249 // the unselected end to maintain orthogonality. Either drag some connected line that is the
250 // same angle as the move or add two lines to make a 90 degree connection
251 if( !EDA_ANGLE( splitDelta ).IsParallelTo( line->Angle() ) || line->GetLength() == 0 )
252 {
253 VECTOR2I unselectedEnd = line->HasFlag( STARTPOINT ) ? line->GetEndPoint()
254 : line->GetStartPoint();
255 VECTOR2I selectedEnd = line->HasFlag( STARTPOINT ) ? line->GetStartPoint()
256 : line->GetEndPoint();
257
258 // Look for pre-existing lines we can drag with us instead of creating new ones
259 bool foundAttachment = false;
260 bool foundJunction = false;
261 bool foundPin = false;
262 SCH_LINE* foundLine = nullptr;
263
264 for( EDA_ITEM* cItem : m_lineConnectionCache[line] )
265 {
266 foundAttachment = true;
267
268 // If the move is the same angle as a connected line, we can shrink/extend that line
269 // endpoint
270 switch( cItem->Type() )
271 {
272 case SCH_LINE_T:
273 {
274 SCH_LINE* cLine = static_cast<SCH_LINE*>( cItem );
275
276 // A matching angle on a non-zero-length line means lengthen/shorten will work
277 if( EDA_ANGLE( splitDelta ).IsParallelTo( cLine->Angle() )
278 && cLine->GetLength() != 0 )
279 {
280 foundLine = cLine;
281 }
282
283 // Zero length lines are lines that this algorithm has shortened to 0 so they also
284 // work but we should prefer using a segment with length and angle matching when
285 // we can (otherwise the zero length line will draw overlapping segments on them)
286 if( !foundLine && cLine->GetLength() == 0 )
287 foundLine = cLine;
288
289 break;
290 }
291 case SCH_JUNCTION_T:
292 foundJunction = true;
293 break;
294
295 case SCH_PIN_T:
296 foundPin = true;
297 break;
298
299 case SCH_SHEET_T:
300 for( const auto& pair : m_specialCaseSheetPins )
301 {
302 if( pair.first->IsConnected( selectedEnd ) )
303 {
304 foundPin = true;
305 break;
306 }
307 }
308
309 break;
310
311 default:
312 break;
313 }
314 }
315
316 // Ok... what if our original line is length zero from moving in its direction, and the
317 // last added segment of the 90 bend we are connected to is zero from moving it in its
318 // direction after it was added?
319 //
320 // If we are moving in original direction, we should lengthen the original drag wire.
321 // Otherwise we should lengthen the new wire.
322 bool preferOriginalLine = false;
323
324 if( foundLine
325 && foundLine->GetLength() == 0
326 && line->GetLength() == 0
327 && EDA_ANGLE( splitDelta ).IsParallelTo( line->GetStoredAngle() ) )
328 {
329 preferOriginalLine = true;
330 }
331 // If we have found an attachment, but not a line, we want to check if it's a junction.
332 // These are special-cased and get a single line added instead of a 90-degree bend. Except
333 // when we're on a pin, because pins always need bends, and junctions are just added to
334 // pins for visual clarity.
335 else if( !foundLine && foundJunction && !foundPin )
336 {
337 // Create a new wire ending at the unselected end
338 foundLine = new SCH_LINE( unselectedEnd, line->GetLayer() );
339 foundLine->SetFlags( IS_NEW );
340 foundLine->SetLastResolvedState( line );
341 cloneWireConnection( foundLine, line, m_frame );
342 m_frame->AddToScreen( foundLine, m_frame->GetScreen() );
343 m_newDragLines.insert( foundLine );
344
345 // We just broke off of the existing items, so replace all of them with our new
346 // end connection.
348 m_lineConnectionCache[line].clear();
349 m_lineConnectionCache[line].emplace_back( foundLine );
350 }
351
352 // We want to drag our found line if it's in the same angle as the move or zero length,
353 // but if the original drag line is also zero and the same original angle we should extend
354 // that one first
355 if( foundLine && !preferOriginalLine )
356 {
357 // Move the connected line found oriented in the direction of our move.
358 //
359 // Make sure we grab the right endpoint, it's not always STARTPOINT since the user can
360 // draw a box of lines. We need to only move one though, and preferably the start point,
361 // in case we have a zero length line that we are extending (we want the foundLine
362 // start point to be attached to the unselected end of our drag line).
363 //
364 // Also, new lines are added already so they'll be in the undo list, skip adding them.
365
366 if( !foundLine->HasFlag( IS_CHANGED ) && !foundLine->HasFlag( IS_NEW ) )
367 {
368 aCommit->Modify( (SCH_ITEM*) foundLine, m_frame->GetScreen() );
369
370 if( !foundLine->IsSelected() )
371 m_changedDragLines.insert( foundLine );
372 }
373
374 if( foundLine->GetStartPoint() == unselectedEnd )
375 foundLine->MoveStart( splitDelta );
376 else if( foundLine->GetEndPoint() == unselectedEnd )
377 foundLine->MoveEnd( splitDelta );
378
379 updateItem( foundLine, true );
380
381 SCH_LINE* bendLine = nullptr;
382
383 if( m_lineConnectionCache.count( foundLine ) == 1
384 && m_lineConnectionCache[foundLine][0]->Type() == SCH_LINE_T )
385 {
386 bendLine = static_cast<SCH_LINE*>( m_lineConnectionCache[foundLine][0] );
387 }
388
389 // Remerge segments we've created if this is a segment that we've added whose only
390 // other connection is also an added segment
391 //
392 // bendLine is first added segment at the original attachment point, foundLine is the
393 // orthogonal line between bendLine and this line
394 if( foundLine->HasFlag( IS_NEW )
395 && foundLine->GetLength() == 0
396 && bendLine && bendLine->HasFlag( IS_NEW ) )
397 {
398 if( line->HasFlag( STARTPOINT ) )
399 line->SetEndPoint( bendLine->GetEndPoint() );
400 else
401 line->SetStartPoint( bendLine->GetEndPoint() );
402
403 // Update our cache of the connected items.
404
405 // Re-attach drag labels from lines being deleted to the surviving line.
406 // This prevents dangling pointers when bendLine/foundLine are deleted below.
407 for( auto& [label, info] : m_specialCaseLabels )
408 {
409 if( info.attachedLine == bendLine || info.attachedLine == foundLine )
410 {
411 info.attachedLine = line;
412 info.originalLineStart = line->GetStartPoint();
413 info.originalLineEnd = line->GetEndPoint();
414 }
415 }
416
418 m_lineConnectionCache[bendLine].clear();
419 m_lineConnectionCache[foundLine].clear();
420
421 m_frame->RemoveFromScreen( bendLine, m_frame->GetScreen() );
422 m_frame->RemoveFromScreen( foundLine, m_frame->GetScreen() );
423
424 m_newDragLines.erase( bendLine );
425 m_newDragLines.erase( foundLine );
426
427 delete bendLine;
428 delete foundLine;
429 }
430 //Ok, move the unselected end of our item
431 else
432 {
433 if( line->HasFlag( STARTPOINT ) )
434 line->MoveEnd( splitDelta );
435 else
436 line->MoveStart( splitDelta );
437 }
438
439 updateItem( line, true );
440 }
441 else if( line->GetLength() == 0 )
442 {
443 // We didn't find another line to shorten/lengthen, (or we did but it's also zero)
444 // so now is a good time to use our existing zero-length original line
445 }
446 // Either no line was at the "right" angle, or this was a junction, pin, sheet, etc. We
447 // need to add segments to keep the soon-to-move unselected end connected to these items.
448 //
449 // To keep our drag selections all the same, we'll move our unselected end point and then
450 // put wires between it and its original endpoint.
451 else if( foundAttachment && line->IsOrthogonal() )
452 {
453 VECTOR2D lineGrid = grid.GetGridSize( grid.GetItemGrid( line ) );
454
455 // The bend counter handles a group of wires all needing their offset one grid movement
456 // further out from each other to not overlap. The absolute value stuff finds the
457 // direction of the line and hence the the bend increment on that axis
458 unsigned int xMoveBit = splitDelta.x != 0;
459 unsigned int yMoveBit = splitDelta.y != 0;
460 int xLength = abs( unselectedEnd.x - selectedEnd.x );
461 int yLength = abs( unselectedEnd.y - selectedEnd.y );
462 int xMove = ( xLength - ( xBendCount * lineGrid.x ) )
463 * sign( selectedEnd.x - unselectedEnd.x );
464 int yMove = ( yLength - ( yBendCount * lineGrid.y ) )
465 * sign( selectedEnd.y - unselectedEnd.y );
466
467 // Create a new wire ending at the unselected end, we'll move the new wire's start
468 // point to the unselected end
469 SCH_LINE* a = new SCH_LINE( unselectedEnd, line->GetLayer() );
470 a->MoveStart( VECTOR2I( xMove, yMove ) );
471 a->SetFlags( IS_NEW );
472 a->SetConnectivityDirty( true );
473 a->SetLastResolvedState( line );
474 cloneWireConnection( a, line, m_frame );
475 m_frame->AddToScreen( a, m_frame->GetScreen() );
476 m_newDragLines.insert( a );
477
478 SCH_LINE* b = new SCH_LINE( a->GetStartPoint(), line->GetLayer() );
479 b->MoveStart( VECTOR2I( splitDelta.x, splitDelta.y ) );
480 b->SetFlags( IS_NEW | STARTPOINT );
481 b->SetConnectivityDirty( true );
482 b->SetLastResolvedState( line );
483 cloneWireConnection( b, line, m_frame );
484 m_frame->AddToScreen( b, m_frame->GetScreen() );
485 m_newDragLines.insert( b );
486
487 xBendCount += yMoveBit;
488 yBendCount += xMoveBit;
489
490 // Ok move the unselected end of our item
491 if( line->HasFlag( STARTPOINT ) )
492 {
493 line->MoveEnd( VECTOR2I( splitDelta.x ? splitDelta.x : xMove,
494 splitDelta.y ? splitDelta.y : yMove ) );
495 }
496 else
497 {
498 line->MoveStart( VECTOR2I( splitDelta.x ? splitDelta.x : xMove,
499 splitDelta.y ? splitDelta.y : yMove ) );
500 }
501
502 // Update our cache of the connected items. First, attach our drag labels to the line
503 // left behind.
504 for( EDA_ITEM* candidate : m_lineConnectionCache[line] )
505 {
506 SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( candidate );
507
508 if( !label || !m_specialCaseLabels.count( label ) )
509 continue;
510
511 if( label->GetPosition() == selectedEnd )
512 {
513 m_specialCaseLabels[label].trackMovingEnd = true;
514 }
515 else
516 {
517 m_specialCaseLabels[label].attachedLine = a;
518 m_specialCaseLabels[label].originalLineStart = a->GetStartPoint();
519 m_specialCaseLabels[label].originalLineEnd = a->GetEndPoint();
520 }
521 }
522
523 // We just broke off of the existing items, so replace all of them with our new end
524 // connection.
526 m_lineConnectionCache[b].emplace_back( a );
527 m_lineConnectionCache[line].clear();
528 m_lineConnectionCache[line].emplace_back( b );
529 }
530 // Original line has no attachments, just move the unselected end
531 else if( !foundAttachment )
532 {
533 if( line->HasFlag( STARTPOINT ) )
534 line->MoveEnd( splitDelta );
535 else
536 line->MoveStart( splitDelta );
537 }
538 }
539}
540
541
542int SCH_MOVE_TOOL::Main( const TOOL_EVENT& aEvent )
543{
544 if( aEvent.IsAction( &SCH_ACTIONS::drag ) )
545 m_mode = DRAG;
546 else if( aEvent.IsAction( &SCH_ACTIONS::breakWire ) )
547 m_mode = BREAK;
548 else if( aEvent.IsAction( &SCH_ACTIONS::slice ) )
549 m_mode = SLICE;
550 else
551 m_mode = MOVE;
552
553 if( SCH_COMMIT* commit = dynamic_cast<SCH_COMMIT*>( aEvent.Commit() ) )
554 {
555 wxCHECK( aEvent.SynchronousState(), 0 );
556 aEvent.SynchronousState()->store( STS_RUNNING );
557
558 if( doMoveSelection( aEvent, commit ) )
559 aEvent.SynchronousState()->store( STS_FINISHED );
560 else
561 aEvent.SynchronousState()->store( STS_CANCELLED );
562 }
563 else
564 {
565 SCH_COMMIT localCommit( m_toolMgr );
566
567 if( doMoveSelection( aEvent, &localCommit ) )
568 {
569 switch( m_mode )
570 {
571 case MOVE: localCommit.Push( _( "Move" ) ); break;
572 case DRAG: localCommit.Push( _( "Drag" ) ); break;
573 case BREAK: localCommit.Push( _( "Break Wire" ) ); break;
574 case SLICE: localCommit.Push( _( "Slice Wire" ) ); break;
575 }
576 }
577 else
578 {
579 localCommit.Revert();
580 }
581 }
582
583 return 0;
584}
585
586
588{
589 if( m_mode != BREAK && m_mode != SLICE )
590 return;
591
592 if( !aCommit )
593 return;
594
596
597 if( !lwbTool )
598 return;
599
600 SCH_SELECTION& selection = m_selectionTool->GetSelection();
601
602 if( selection.Empty() )
603 return;
604
605 std::vector<SCH_LINE*> lines;
606
607 for( EDA_ITEM* item : selection )
608 {
609 if( item->Type() == SCH_LINE_T )
610 {
611 // This function gets called every time segments are broken, which can also be for subsequent
612 // breaks in a loop without leaving the current move tool.
613 // Skip already placed segments (segment keeps IS_BROKEN but will have IS_NEW cleared below)
614 // so that only the actively placed tail segment gets split again.
615 if( item->HasFlag( IS_BROKEN ) && !item->HasFlag( IS_NEW ) )
616 continue;
617
618 lines.push_back( static_cast<SCH_LINE*>( item ) );
619 }
620 }
621
622 if( lines.empty() )
623 return;
624
626 SCH_SCREEN* screen = m_frame->GetScreen();
627 VECTOR2I cursorPos = controls->GetCursorPosition( !aEvent.DisableGridSnapping() );
628
629 bool useCursorForSingleLine = false;
630
631 if( lines.size() == 1 )
632 useCursorForSingleLine = true;
633
634 m_selectionTool->ClearSelection();
635 m_breakPos.reset();
636
637 for( SCH_LINE* line : lines )
638 {
639 VECTOR2I breakPos = useCursorForSingleLine ? cursorPos : line->GetMidPoint();
640
641 if( m_mode == BREAK && !m_breakPos )
642 m_breakPos = breakPos;
643
644 SCH_LINE* newLine = nullptr;
645
646 lwbTool->BreakSegment( aCommit, line, breakPos, &newLine, screen );
647
648 if( !newLine )
649 continue;
650
651 // If this is a second+ round break, we need to get rid of the IS_NEW flag since the new segment
652 // is now an existing segment we are breaking from, this will be checked for in the line selection
653 // gathering above
654 line->ClearFlags( STARTPOINT | IS_NEW );
655 line->SetFlags( ENDPOINT );
656 m_selectionTool->AddItemToSel( line );
657
658 newLine->ClearFlags( ENDPOINT | STARTPOINT );
659
660 if( m_mode == BREAK )
661 {
662 m_selectionTool->AddItemToSel( newLine );
663 newLine->SetFlags( STARTPOINT );
664 }
665 }
666}
667
668
670{
673 bool currentModeIsDragLike = ( m_mode != MOVE );
674 bool wasDragging = m_moveInProgress && currentModeIsDragLike;
675 bool didAtLeastOneBreak = false;
676
677 m_anchorPos.reset();
678
679 // Check if already in progress and handle state transitions
680 if( checkMoveInProgress( aEvent, aCommit, currentModeIsDragLike, wasDragging ) )
681 return false;
682
683 if( m_inMoveTool ) // Must come after m_moveInProgress checks above...
684 return false;
685
687
688 preprocessBreakOrSliceSelection( aCommit, aEvent );
689
690 // Prepare selection (promote pins to symbols, request selection)
691 bool unselect = false;
692 SCH_SELECTION& selection = prepareSelection( unselect );
693
694 // Keep an original copy of the starting points for cleanup after the move
695 std::vector<DANGLING_END_ITEM> internalPoints;
696
697 // Track selection characteristics
698 bool selectionHasSheetPins = false;
699 bool selectionHasGraphicItems = false;
700 bool selectionHasNonGraphicItems = false;
701 bool selectionIsGraphicsOnly = false;
702
703 std::unique_ptr<SCH_DRAG_NET_COLLISION_MONITOR> netCollisionMonitor;
704
705 auto refreshTraits =
706 [&]()
707 {
708 refreshSelectionTraits( selection, selectionHasSheetPins, selectionHasGraphicItems,
709 selectionHasNonGraphicItems, selectionIsGraphicsOnly );
710 };
711
712 refreshTraits();
713
714 if( !selection.Empty() )
715
716 {
717 netCollisionMonitor = std::make_unique<SCH_DRAG_NET_COLLISION_MONITOR>( m_frame, m_view );
718 netCollisionMonitor->Initialize( selection );
719 }
720
721 bool lastCtrlDown = false;
722
723 // When items are pasted via Ctrl+V, the Ctrl key is still held when the move tool
724 // starts. Ctrl disables grid snapping, so the pasted items would track off-grid.
725 // The synthetic move action event carries no modifier bits, so query the live
726 // keyboard state and ignore Ctrl until the user releases and re-presses it.
727 bool pasteHoldingCtrl = false;
728
729 for( EDA_ITEM* item : selection )
730 {
731 if( item->HasFlag( IS_PASTED ) )
732 {
733 pasteHoldingCtrl = wxGetKeyState( WXK_CONTROL );
734 break;
735 }
736 }
737
738 Activate();
739
740 // Must be done after Activate() so that it gets set into the correct context
741 controls->ShowCursor( true );
742
743 m_frame->PushTool( aEvent );
744
745 if( selection.Empty() )
746 {
747 // Note that it's important to go through push/pop even when the selection is empty.
748 // This keeps other tools from having to special-case an empty move.
749 m_frame->PopTool( aEvent );
750 return false;
751 }
752
753 bool restore_state = false;
754 TOOL_EVENT copy = aEvent;
755 TOOL_EVENT* evt = &copy;
756 VECTOR2I prevPos = controls->GetCursorPosition();
758 SCH_SHEET* hoverSheet = nullptr;
759 KICURSOR currentCursor = KICURSOR::MOVING;
760 m_cursor = controls->GetCursorPosition();
761
762 // Axis locking for arrow key movement
763 enum class AXIS_LOCK { NONE, HORIZONTAL, VERTICAL };
764 AXIS_LOCK axisLock = AXIS_LOCK::NONE;
765 long lastArrowKeyAction = 0;
766
767 // Main loop: keep receiving events
768 do
769 {
770 wxLogTrace( traceSchMove, "doMoveSelection: event loop iteration, evt=%s, action=%s",
771 evt->Category() == TC_MOUSE ? "MOUSE" :
772 evt->Category() == TC_KEYBOARD ? "KEYBOARD" :
773 evt->Category() == TC_COMMAND ? "COMMAND" : "OTHER",
774 evt->Format().c_str() );
775
776 m_frame->GetCanvas()->SetCurrentCursor( currentCursor );
777 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
778
779 bool ctrlDown = evt->Modifier( MD_CTRL );
780
781 // Only real input events carry modifier state; the synthetic move action does not.
782 bool hasModifierState = evt->Category() == TC_MOUSE || evt->Category() == TC_KEYBOARD;
783
784 if( pasteHoldingCtrl && hasModifierState && !ctrlDown )
785 pasteHoldingCtrl = false;
786
787 // The paste-held Ctrl only masks grid snapping. Ctrl also forces a graphics-only drop
788 // into a sheet, and that gesture must still honor a physically-held key.
789 bool gridSnapDisabled = ctrlDown && !pasteHoldingCtrl;
790
791 grid.SetUseGrid( getView()->GetGAL()->GetGridSnapping() && !gridSnapDisabled );
792
793 lastCtrlDown = ctrlDown;
794
796 || evt->IsAction( &SCH_ACTIONS::move )
797 || evt->IsAction( &SCH_ACTIONS::drag )
798 || evt->IsMotion()
799 || evt->IsDrag( BUT_LEFT )
801 {
802 refreshTraits();
803
804 if( !m_moveInProgress ) // Prepare to start moving/dragging
805 {
806 initializeMoveOperation( aEvent, selection, aCommit, internalPoints, snapLayer );
807 prevPos = m_cursor;
808 refreshTraits();
809 }
810
811 //------------------------------------------------------------------------
812 // Follow the mouse
813 //
814 m_view->ClearPreview();
815
816 // We need to bypass refreshPreview action here because it is triggered by the move, so we were
817 // getting double-key events that toggled the axis locking if you pressed them in a certain order.
819 {
820 VECTOR2I keyboardPos( controls->GetSettings().m_lastKeyboardCursorPosition );
821 long action = controls->GetSettings().m_lastKeyboardCursorCommand;
822
823 grid.SetSnap( false );
824 m_cursor = grid.Align( keyboardPos, snapLayer );
825
826 // Update axis lock based on arrow key press
827 if( action == ACTIONS::CURSOR_LEFT || action == ACTIONS::CURSOR_RIGHT )
828 {
829 if( axisLock == AXIS_LOCK::HORIZONTAL )
830 {
831 // Check if opposite horizontal key pressed to unlock
832 if( ( lastArrowKeyAction == ACTIONS::CURSOR_LEFT && action == ACTIONS::CURSOR_RIGHT ) ||
833 ( lastArrowKeyAction == ACTIONS::CURSOR_RIGHT && action == ACTIONS::CURSOR_LEFT ) )
834 {
835 axisLock = AXIS_LOCK::NONE;
836 }
837 // Same direction axis, keep locked
838 }
839 else
840 {
841 axisLock = AXIS_LOCK::HORIZONTAL;
842 }
843 }
844 else if( action == ACTIONS::CURSOR_UP || action == ACTIONS::CURSOR_DOWN )
845 {
846 if( axisLock == AXIS_LOCK::VERTICAL )
847 {
848 // Check if opposite vertical key pressed to unlock
849 if( ( lastArrowKeyAction == ACTIONS::CURSOR_UP && action == ACTIONS::CURSOR_DOWN ) ||
850 ( lastArrowKeyAction == ACTIONS::CURSOR_DOWN && action == ACTIONS::CURSOR_UP ) )
851 {
852 axisLock = AXIS_LOCK::NONE;
853 }
854 // Same direction axis, keep locked
855 }
856 else
857 {
858 axisLock = AXIS_LOCK::VERTICAL;
859 }
860 }
861
862 lastArrowKeyAction = action;
863 }
864 else
865 {
866 m_cursor = grid.ResolveSnap( controls->GetCursorPosition( false ), snapLayer, selection, prevPos )
867 .position;
868 }
869
870 if( axisLock == AXIS_LOCK::HORIZONTAL )
871 m_cursor.y = prevPos.y;
872 else if( axisLock == AXIS_LOCK::VERTICAL )
873 m_cursor.x = prevPos.x;
874
875 // Find potential target sheet for dropping. This relocation is only meaningful for a
876 // plain move; drag/break/slice reshape existing connections in place and must never
877 // pull items onto a sub-sheet's screen.
878 SCH_SHEET* sheet = nullptr;
879
880 if( m_mode == MOVE )
881 sheet = findTargetSheet( selection, m_cursor, selectionHasSheetPins, selectionIsGraphicsOnly,
882 ctrlDown );
883
884 if( sheet != hoverSheet )
885 {
886 hoverSheet = sheet;
887
888 if( hoverSheet )
889 {
890 hoverSheet->SetFlags( BRIGHTENED );
891 m_frame->UpdateItem( hoverSheet, false );
892 }
893 }
894
895 currentCursor = hoverSheet ? KICURSOR::PLACE : KICURSOR::MOVING;
896
897 if( netCollisionMonitor )
898 currentCursor = netCollisionMonitor->AdjustCursor( currentCursor );
899
900 VECTOR2I delta( m_cursor - prevPos );
902
903 // Used for tracking how far off a drag end should have its 90 degree elbow added
904 int xBendCount = 1;
905 int yBendCount = 1;
906
907 performItemMove( selection, delta, aCommit, xBendCount, yBendCount, grid );
908 prevPos = m_cursor;
909
910 std::vector<SCH_ITEM*> previewItems;
911
912 for( EDA_ITEM* it : selection )
913 previewItems.push_back( static_cast<SCH_ITEM*>( it ) );
914
915 for( SCH_LINE* line : m_newDragLines )
916 previewItems.push_back( line );
917
918 for( SCH_LINE* line : m_changedDragLines )
919 previewItems.push_back( line );
920
921 std::vector<SCH_JUNCTION*> previewJunctions =
922 JUNCTION_HELPERS::PreviewJunctions( m_frame->GetScreen(), previewItems );
923
924 if( netCollisionMonitor )
925 netCollisionMonitor->Update( previewJunctions, selection );
926
927 for( SCH_JUNCTION* jct : previewJunctions )
928 m_view->AddToPreview( jct, true );
929
931 }
932
933 //------------------------------------------------------------------------
934 // Handle cancel
935 //
936 else if( evt->IsCancelInteractive()
937 || evt->IsActivate()
938 || evt->IsAction( &ACTIONS::undo ) )
939 {
940 if( evt->IsCancelInteractive() )
941 {
942 m_frame->GetInfoBar()->Dismiss();
943
944 // When breaking, the user can cancel after multiple breaks to keep all but the last
945 // break, so exit normally if we have done at least one break
946 if( didAtLeastOneBreak && m_mode == BREAK )
947 break;
948 }
949
950 if( m_moveInProgress )
951 {
952 if( evt->IsActivate() )
953 {
954 // Allowing other tools to activate during a move runs the risk of race
955 // conditions in which we try to spool up both event loops at once.
956
957 switch( m_mode )
958 {
959 case MOVE: m_frame->ShowInfoBarMsg( _( "Press <ESC> to cancel move." ) ); break;
960 case DRAG: m_frame->ShowInfoBarMsg( _( "Press <ESC> to cancel drag." ) ); break;
961 case BREAK: m_frame->ShowInfoBarMsg( _( "Press <ESC> to cancel break." ) ); break;
962 case SLICE: m_frame->ShowInfoBarMsg( _( "Press <ESC> to cancel slice." ) ); break;
963 }
964
965 evt->SetPassEvent( false );
966 continue;
967 }
968
969 evt->SetPassEvent( false );
970 restore_state = true;
971 }
972 else if( m_mode == BREAK || m_mode == SLICE )
973 {
974 // preprocessBreakOrSliceSelection() split the wire before any motion arrived,
975 // so cancel must roll those edits back. Activations still pass through so the
976 // requested tool starts.
977 if( !evt->IsActivate() )
978 evt->SetPassEvent( false );
979
980 restore_state = true;
981 }
982
984
985 m_view->ClearPreview();
986
987 break;
988 }
989 //------------------------------------------------------------------------
990 // Handle TOOL_ACTION special cases
991 //
992 else if( !handleMoveToolActions( evt, aCommit, selection ) )
993 {
994 wxLogTrace( traceSchMove, "doMoveSelection: handleMoveToolActions returned false, exiting" );
995 break; // Exit if told to by handler
996 }
997 //------------------------------------------------------------------------
998 // Handle context menu
999 //
1000 else if( evt->IsClick( BUT_RIGHT ) )
1001 {
1002 m_menu->ShowContextMenu( m_selectionTool->GetSelection() );
1003 }
1004 //------------------------------------------------------------------------
1005 // Handle drop
1006 //
1007 else if( evt->IsMouseUp( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) )
1008 {
1009 if( m_mode != BREAK )
1010 break; // Finish
1011 else
1012 {
1013 didAtLeastOneBreak = true;
1014 preprocessBreakOrSliceSelection( aCommit, *evt );
1015 selection = m_selectionTool->RequestSelection( SCH_COLLECTOR::MovableItems, true );
1016
1017 if( m_breakPos )
1018 {
1021 selection.SetReferencePoint( m_cursor );
1022 m_moveOffset = VECTOR2I( 0, 0 );
1023 m_breakPos.reset();
1024
1025 controls->SetCursorPosition( m_cursor, false );
1026 prevPos = m_cursor;
1027 }
1028 }
1029 }
1030 else if( evt->IsDblClick( BUT_LEFT ) )
1031 {
1032 // Double click always finishes, even breaks
1033 break;
1034 }
1035 // Don't call SetPassEvent() for events we've handled - let them be consumed
1036 else if( evt->IsAction( &SCH_ACTIONS::rotateCW )
1038 || evt->IsAction( &ACTIONS::increment )
1042 || evt->IsAction( &SCH_ACTIONS::toLabel )
1043 || evt->IsAction( &SCH_ACTIONS::toText )
1047 || evt->IsAction( &ACTIONS::duplicate )
1049 || evt->IsAction( &ACTIONS::redo ) )
1050 {
1051 // Event was already handled by handleMoveToolActions, don't pass it on
1052 wxLogTrace( traceSchMove, "doMoveSelection: event handled, not passing" );
1053 }
1054 else
1055 {
1056 evt->SetPassEvent();
1057 }
1058
1059 controls->SetAutoPan( m_moveInProgress );
1060
1061 } while( ( evt = Wait() ) ); //Should be assignment not equality test
1062
1063 SCH_SHEET* targetSheet = hoverSheet;
1064
1065 if( selectionHasSheetPins || ( selectionIsGraphicsOnly && !lastCtrlDown ) )
1066 targetSheet = nullptr;
1067
1068 if( hoverSheet )
1069 {
1070 hoverSheet->ClearFlags( BRIGHTENED );
1071 m_frame->UpdateItem( hoverSheet, false );
1072 }
1073
1074 if( restore_state )
1075 {
1076 m_selectionTool->RemoveItemsFromSel( &m_dragAdditions, QUIET_MODE );
1077
1078 // Clear the split-segment selection that preprocessBreakOrSliceSelection() built
1079 // before the caller's Revert() runs. Revert() rebuilds selection from the screen,
1080 // so leaving the splits selected keeps the restored wire hidden until the next
1081 // selection refresh.
1082 if( m_mode == BREAK || m_mode == SLICE )
1083 m_toolMgr->RunAction( ACTIONS::selectionClear );
1084 }
1085 else
1086 {
1087 // Only drop into a sheet when the move is committed, not when canceled.
1088 if( targetSheet )
1089 {
1090 moveSelectionToSheet( selection, targetSheet, aCommit );
1091 m_toolMgr->RunAction( ACTIONS::selectionClear );
1092 m_newDragLines.clear();
1093 m_changedDragLines.clear();
1094 }
1095
1096 finalizeMoveOperation( selection, aCommit, unselect, internalPoints );
1097 }
1098
1099 m_dragAdditions.clear();
1100 m_lineConnectionCache.clear();
1101 m_moveInProgress = false;
1102 m_breakPos.reset();
1103
1104 m_hiddenJunctions.clear();
1105 m_view->ClearPreview();
1106 m_frame->PopTool( aEvent );
1107
1108 return !restore_state;
1109}
1110
1111
1112bool SCH_MOVE_TOOL::checkMoveInProgress( const TOOL_EVENT& aEvent, SCH_COMMIT* aCommit, bool aCurrentModeIsDragLike,
1113 bool aWasDragging )
1114{
1116
1117 if( !m_moveInProgress )
1118 return false;
1119
1120 if( aCurrentModeIsDragLike != aWasDragging )
1121 {
1122 EDA_ITEM* sel = m_selectionTool->GetSelection().Front();
1123
1124 if( sel && !sel->IsNew() )
1125 {
1126 // Reset the selected items so we can start again with the current drag mode state
1127 aCommit->Revert();
1128
1129 m_selectionTool->RemoveItemsFromSel( &m_dragAdditions, QUIET_MODE );
1131 m_moveInProgress = false;
1132 controls->SetAutoPan( false );
1133
1134 // Give it a kick so it doesn't have to wait for the first mouse movement to refresh
1135 m_toolMgr->PostAction( SCH_ACTIONS::restartMove );
1136 }
1137 }
1138 else
1139 {
1140 // The tool hotkey is interpreted as a click when already dragging/moving
1141 m_toolMgr->PostAction( ACTIONS::cursorClick );
1142 }
1143
1144 return true;
1145}
1146
1147
1149{
1150 SCH_SELECTION& userSelection = m_selectionTool->GetSelection();
1151
1152 // If a single pin is selected, promote the move selection to its parent symbol
1153 if( userSelection.GetSize() == 1 )
1154 {
1155 EDA_ITEM* selItem = userSelection.Front();
1156
1157 if( selItem->Type() == SCH_PIN_T )
1158 {
1159 EDA_ITEM* parent = selItem->GetParent();
1160
1161 if( parent->Type() == SCH_SYMBOL_T )
1162 {
1163 m_selectionTool->ClearSelection();
1164 m_selectionTool->AddItemToSel( parent );
1165 }
1166 }
1167 }
1168
1169 // Be sure that there is at least one item that we can move. If there's no selection try
1170 // looking for the stuff under mouse cursor (i.e. KiCad old-style hover selection).
1171 SCH_SELECTION& selection = m_selectionTool->RequestSelection( SCH_COLLECTOR::MovableItems, true );
1172 aUnselect = selection.IsHover();
1173
1174 m_selectionTool->FilterSelectionForLockedItems();
1175
1176 return selection;
1177}
1178
1179
1180void SCH_MOVE_TOOL::refreshSelectionTraits( const SCH_SELECTION& aSelection, bool& aHasSheetPins,
1181 bool& aHasGraphicItems, bool& aHasNonGraphicItems,
1182 bool& aIsGraphicsOnly )
1183{
1184 aHasSheetPins = false;
1185 aHasGraphicItems = false;
1186 aHasNonGraphicItems = false;
1187
1188 for( EDA_ITEM* edaItem : aSelection )
1189 {
1190 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( edaItem );
1191
1192 if( schItem->Type() == SCH_SHEET_PIN_T )
1193 aHasSheetPins = true;
1194
1195 if( isGraphicItemForDrop( schItem ) )
1196 aHasGraphicItems = true;
1197 else if( schItem->Type() != SCH_SHEET_T )
1198 aHasNonGraphicItems = true;
1199 }
1200
1201 aIsGraphicsOnly = aHasGraphicItems && !aHasNonGraphicItems;
1202}
1203
1204
1206{
1207 // Drag of split items start over top of their other segment, so we want to skip grabbing
1208 // the segments we split from
1209 if( m_mode != DRAG && m_mode != BREAK )
1210 return;
1211
1212 EDA_ITEMS connectedDragItems;
1213
1214 // Add connections to the selection for a drag.
1215 // Do all non-labels/entries first so we don't add junctions to drag when the line will
1216 // eventually be drag selected.
1217 std::vector<SCH_ITEM*> stageTwo;
1218
1219 for( EDA_ITEM* edaItem : aSelection )
1220 {
1221 SCH_ITEM* item = static_cast<SCH_ITEM*>( edaItem );
1222 std::vector<VECTOR2I> connections;
1223
1224 switch( item->Type() )
1225 {
1226 case SCH_LABEL_T:
1227 case SCH_HIER_LABEL_T:
1228 case SCH_GLOBAL_LABEL_T:
1230 stageTwo.emplace_back( item );
1231 break;
1232
1233 case SCH_LINE_T:
1234 static_cast<SCH_LINE*>( item )->GetSelectedPoints( connections );
1235 break;
1236
1237 default:
1238 connections = item->GetConnectionPoints();
1239 }
1240
1241 for( const VECTOR2I& point : connections )
1242 getConnectedDragItems( aCommit, item, point, connectedDragItems );
1243 }
1244
1245 // Go back and get all label connections now that we can test for drag-selected lines
1246 // the labels might be on
1247 for( SCH_ITEM* item : stageTwo )
1248 {
1249 for( const VECTOR2I& point : item->GetConnectionPoints() )
1250 getConnectedDragItems( aCommit, item, point, connectedDragItems );
1251 }
1252
1253 for( EDA_ITEM* item : connectedDragItems )
1254 {
1255 m_dragAdditions.push_back( item->m_Uuid );
1256 m_selectionTool->AddItemToSel( item, QUIET_MODE );
1257 }
1258
1259 // Pre-cache all connections of our selected objects so we can keep track of what they
1260 // were originally connected to as we drag them around
1261 for( EDA_ITEM* edaItem : aSelection )
1262 {
1263 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( edaItem );
1264
1265 if( schItem->Type() == SCH_LINE_T )
1266 {
1267 SCH_LINE* line = static_cast<SCH_LINE*>( schItem );
1268
1269 // Store the original angle of the line; needed later to decide which segment
1270 // to extend when they've become zero length
1271 line->StoreAngle();
1272
1273 for( const VECTOR2I& point : line->GetConnectionPoints() )
1274 getConnectedItems( line, point, m_lineConnectionCache[line] );
1275 }
1276 }
1277}
1278
1279
1280void SCH_MOVE_TOOL::setupItemsForMove( SCH_SELECTION& aSelection, std::vector<DANGLING_END_ITEM>& aInternalPoints )
1281{
1282 // Mark the edges of the block with dangling flags for a move
1283 for( EDA_ITEM* item : aSelection )
1284 static_cast<SCH_ITEM*>( item )->GetEndPoints( aInternalPoints );
1285
1286 std::vector<DANGLING_END_ITEM> endPointsByType = aInternalPoints;
1287 std::vector<DANGLING_END_ITEM> endPointsByPos = endPointsByType;
1288 DANGLING_END_ITEM_HELPER::sort_dangling_end_items( endPointsByType, endPointsByPos );
1289
1290 for( EDA_ITEM* item : aSelection )
1291 static_cast<SCH_ITEM*>( item )->UpdateDanglingState( endPointsByType, endPointsByPos );
1292}
1293
1294
1296 std::vector<DANGLING_END_ITEM>& aInternalPoints,
1297 GRID_HELPER_GRIDS& aSnapLayer )
1298{
1301 SCH_ITEM* sch_item = static_cast<SCH_ITEM*>( aSelection.Front() );
1302 bool placingNewItems = sch_item && sch_item->IsNew();
1303
1304 //------------------------------------------------------------------------
1305 // Setup a drag or a move
1306 //
1307 m_dragAdditions.clear();
1308 m_specialCaseLabels.clear();
1309 m_specialCaseSheetPins.clear();
1310 m_sheetPinDragArc.clear();
1311 aInternalPoints.clear();
1313
1314 for( SCH_ITEM* it : m_frame->GetScreen()->Items() )
1315 {
1316 it->ClearFlags( SELECTED_BY_DRAG );
1317
1318 if( !it->IsSelected() )
1319 it->ClearFlags( STARTPOINT | ENDPOINT );
1320 }
1321
1322 setupItemsForDrag( aSelection, aCommit );
1323 setupItemsForMove( aSelection, aInternalPoints );
1324
1325 // Hide junctions connected to line endpoints that are not selected
1326 m_hiddenJunctions.clear();
1327
1328 for( EDA_ITEM* item : aSelection )
1329 item->SetFlags( STRUCT_DELETED );
1330
1331 for( EDA_ITEM* edaItem : aSelection )
1332 {
1333 if( edaItem->Type() != SCH_LINE_T )
1334 continue;
1335
1336 SCH_LINE* line = static_cast<SCH_LINE*>( edaItem );
1337
1338 for( const VECTOR2I& pt : line->GetConnectionPoints() )
1339 {
1340 SCH_JUNCTION* jct = static_cast<SCH_JUNCTION*>( m_frame->GetScreen()->GetItem( pt, 0, SCH_JUNCTION_T ) );
1341
1342 if( jct && !jct->IsSelected()
1343 && std::find( m_hiddenJunctions.begin(), m_hiddenJunctions.end(), jct ) == m_hiddenJunctions.end() )
1344 {
1346 pt, false );
1347
1348 if( !info.isJunction )
1349 {
1350 jct->SetFlags( STRUCT_DELETED );
1351 m_frame->RemoveFromScreen( jct, m_frame->GetScreen() );
1352 aCommit->Removed( jct, m_frame->GetScreen() );
1353 }
1354 }
1355 }
1356 }
1357
1358 for( EDA_ITEM* item : aSelection )
1359 item->ClearFlags( STRUCT_DELETED );
1360
1361 // Generic setup
1362 aSnapLayer = grid.GetSelectionGrid( aSelection );
1363
1364 for( EDA_ITEM* item : aSelection )
1365 {
1366 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( item );
1367
1368 if( schItem->IsNew() )
1369 {
1370 // Item was added to commit in a previous command
1371
1372 // While SCH_COMMIT::Push() will add any new items to the entered group, we need
1373 // to do it earlier so that the previews while moving are correct.
1374 if( SCH_GROUP* enteredGroup = m_selectionTool->GetEnteredGroup() )
1375 {
1376 if( schItem->IsGroupableType() && !schItem->GetParentGroup() )
1377 {
1378 aCommit->Modify( enteredGroup, m_frame->GetScreen(), RECURSE_MODE::NO_RECURSE );
1379 enteredGroup->AddItem( schItem );
1380 }
1381 }
1382 }
1383 else if( schItem->GetParent() && schItem->GetParent()->IsSelected() )
1384 {
1385 // Item will be (or has been) added to commit by parent
1386 }
1387 else
1388 {
1389 aCommit->Modify( schItem, m_frame->GetScreen(), RECURSE_MODE::RECURSE );
1390 }
1391
1392 schItem->SetFlags( IS_MOVING );
1393
1394 if( SCH_SHAPE* shape = dynamic_cast<SCH_SHAPE*>( schItem ) )
1395 {
1396 shape->SetHatchingDirty();
1397 shape->UpdateHatching();
1398 }
1399
1400 schItem->RunOnChildren(
1401 [&]( SCH_ITEM* aChild )
1402 {
1403 aChild->SetFlags( IS_MOVING );
1404 },
1406
1407 schItem->SetStoredPos( schItem->GetPosition() );
1408
1409 if( schItem->Type() == SCH_SHEET_PIN_T && schItem->GetParent() && !schItem->GetParent()->IsSelected() )
1410 {
1411 SCH_SHEET_PIN* pin = static_cast<SCH_SHEET_PIN*>( schItem );
1412 m_sheetPinDragArc[pin] = sheetBorderArc( pin->GetParent(), pin->GetSide(), pin->GetPosition() );
1413 }
1414 }
1415
1416 // Set up the starting position and move/drag offset
1417 m_cursor = controls->GetCursorPosition();
1418
1419 if( m_mode == BREAK && m_breakPos )
1420 {
1423 aSelection.SetReferencePoint( m_cursor );
1424 m_moveOffset = VECTOR2I( 0, 0 );
1425 m_breakPos.reset();
1426 }
1427
1428 if( aEvent.IsAction( &SCH_ACTIONS::restartMove ) )
1429 {
1430 wxASSERT_MSG( m_anchorPos, "Should be already set from previous cmd" );
1431 }
1432 else if( placingNewItems )
1433 {
1434 m_anchorPos = aSelection.GetReferencePoint();
1435 }
1436
1437 if( m_anchorPos )
1438 {
1439 VECTOR2I delta = m_cursor - ( *m_anchorPos );
1440 bool isPasted = false;
1441
1442 // Drag items to the current cursor position
1443 for( EDA_ITEM* item : aSelection )
1444 {
1445 // Don't double move pins, fields, etc.
1446 if( item->GetParent() && item->GetParent()->IsSelected() )
1447 continue;
1448
1449 moveItem( item, delta );
1450 updateItem( item, false );
1451
1452 isPasted |= ( item->GetFlags() & IS_PASTED ) != 0;
1453 }
1454
1455 // The first time pasted items are moved we need to store the position of the cursor
1456 // so that rotate while moving works as expected (instead of around the original
1457 // anchor point)
1458 if( isPasted )
1459 aSelection.SetReferencePoint( m_cursor );
1460
1462 }
1463 // For some items, moving the cursor to anchor is not good (for instance large
1464 // hierarchical sheets or symbols can have the anchor outside the view)
1465 else if( aSelection.Size() == 1 && !sch_item->IsMovableFromAnchorPoint() )
1466 {
1469 }
1470 else
1471 {
1472 if( m_frame->GetMoveWarpsCursor() )
1473 {
1474 // User wants to warp the mouse
1475 m_cursor = grid.BestDragOrigin( m_cursor, aSnapLayer, aSelection );
1476 aSelection.SetReferencePoint( m_cursor );
1477 }
1478 else
1479 {
1480 // User does not want to warp the mouse
1482 }
1483 }
1484
1485 controls->SetCursorPosition( m_cursor, false );
1486 controls->SetAutoPan( true );
1487 m_moveInProgress = true;
1488}
1489
1490
1492 bool aHasSheetPins, bool aIsGraphicsOnly, bool aCtrlDown )
1493{
1494 // Fields are children of their parent item and must not be dropped into a sheet
1495 for( EDA_ITEM* it : aSelection )
1496 {
1497 if( it->Type() == SCH_FIELD_T )
1498 return nullptr;
1499 }
1500
1501 // Determine potential target sheet
1502 SCH_SHEET* sheet = dynamic_cast<SCH_SHEET*>( m_frame->GetScreen()->GetItem( aCursorPos, 0, SCH_SHEET_T ) );
1503
1504 if( sheet && ( sheet->IsSelected() || sheet->HasFlag( IS_MOVING ) ) )
1505 sheet = nullptr; // Never target a selected sheet
1506
1507 if( !sheet )
1508 {
1509 // Build current selection bounding box in its (already moved) position
1510 BOX2I selBBox;
1511
1512 for( EDA_ITEM* it : aSelection )
1513 {
1514 if( SCH_ITEM* schIt = dynamic_cast<SCH_ITEM*>( it ) )
1515 selBBox.Merge( schIt->GetBoundingBox() );
1516 }
1517
1518 if( selBBox.GetWidth() > 0 && selBBox.GetHeight() > 0 )
1519 {
1520 VECTOR2I selCenter( selBBox.GetX() + selBBox.GetWidth() / 2,
1521 selBBox.GetY() + selBBox.GetHeight() / 2 );
1522
1523 // Find first non-selected sheet whose body fully contains the selection or at
1524 // least contains its center point
1525 for( SCH_ITEM* it : m_frame->GetScreen()->Items().OfType( SCH_SHEET_T ) )
1526 {
1527 SCH_SHEET* candidate = static_cast<SCH_SHEET*>( it );
1528
1529 if( candidate->IsSelected() || candidate->IsTopLevelSheet() || candidate->HasFlag( IS_MOVING ) )
1530 continue;
1531
1532 BOX2I body = candidate->GetBodyBoundingBox();
1533
1534 if( body.Contains( selBBox ) || body.Contains( selCenter ) )
1535 {
1536 sheet = candidate;
1537 break;
1538 }
1539 }
1540 }
1541 }
1542
1543 // Don't drop into a sheet if any connection point of the selection lands on a sheet pin.
1544 // This indicates the user is trying to connect to the pin, not drop into the sheet.
1545 if( sheet )
1546 {
1547 for( EDA_ITEM* it : aSelection )
1548 {
1549 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( it );
1550
1551 if( !schItem )
1552 continue;
1553
1554 for( const VECTOR2I& pt : schItem->GetConnectionPoints() )
1555 {
1556 if( sheet->GetPin( pt ) )
1557 {
1558 sheet = nullptr;
1559 break;
1560 }
1561 }
1562
1563 if( !sheet )
1564 break;
1565 }
1566 }
1567
1568 bool dropAllowedBySelection = !aHasSheetPins;
1569 bool dropAllowedByModifiers = !aIsGraphicsOnly || aCtrlDown;
1570
1571 if( sheet && !( dropAllowedBySelection && dropAllowedByModifiers ) )
1572 sheet = nullptr;
1573
1574 return sheet;
1575}
1576
1577
1579 SCH_COMMIT* aCommit, int& aXBendCount, int& aYBendCount,
1580 const EE_GRID_HELPER& aGrid )
1581{
1582 wxLogTrace( traceSchMove, "performItemMove: delta=(%d,%d), moveOffset=(%d,%d), selection size=%u",
1583 aDelta.x, aDelta.y, m_moveOffset.x, m_moveOffset.y, aSelection.GetSize() );
1584
1585 // We need to check if the movement will change the net offset direction on the X and Y
1586 // axes. This is because we remerge added bend lines in realtime, and we also account for
1587 // the direction of the move when adding bend lines. So, if the move direction changes,
1588 // we need to split it into a move that gets us back to zero, then the rest of the move.
1589 std::vector<VECTOR2I> splitMoves;
1590
1591 if( alg::signbit( m_moveOffset.x ) != alg::signbit( ( m_moveOffset + aDelta ).x ) )
1592 {
1593 splitMoves.emplace_back( VECTOR2I( -1 * m_moveOffset.x, 0 ) );
1594 splitMoves.emplace_back( VECTOR2I( aDelta.x + m_moveOffset.x, 0 ) );
1595 }
1596 else
1597 {
1598 splitMoves.emplace_back( VECTOR2I( aDelta.x, 0 ) );
1599 }
1600
1601 if( alg::signbit( m_moveOffset.y ) != alg::signbit( ( m_moveOffset + aDelta ).y ) )
1602 {
1603 splitMoves.emplace_back( VECTOR2I( 0, -1 * m_moveOffset.y ) );
1604 splitMoves.emplace_back( VECTOR2I( 0, aDelta.y + m_moveOffset.y ) );
1605 }
1606 else
1607 {
1608 splitMoves.emplace_back( VECTOR2I( 0, aDelta.y ) );
1609 }
1610
1611 m_moveOffset += aDelta;
1612
1613 // Split the move into X and Y moves so we can correctly drag orthogonal lines
1614 for( const VECTOR2I& splitDelta : splitMoves )
1615 {
1616 // Skip non-moves
1617 if( splitDelta == VECTOR2I( 0, 0 ) )
1618 continue;
1619
1620 for( EDA_ITEM* item : aSelection.GetItemsSortedByTypeAndXY( ( aDelta.x >= 0 ),
1621 ( aDelta.y >= 0 ) ) )
1622 {
1623 // Don't double move pins, fields, etc.
1624 if( item->GetParent() && item->GetParent()->IsSelected() )
1625 continue;
1626
1627 SCH_LINE* line = dynamic_cast<SCH_LINE*>( item );
1628 bool isLineModeConstrained = false;
1629
1630 if( EESCHEMA_SETTINGS* cfg = GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" ) )
1631 isLineModeConstrained = cfg->m_Drawing.line_mode != LINE_MODE::LINE_MODE_FREE;
1632
1633 // Only partially selected drag lines in orthogonal line mode need special handling.
1634 // Skip newly-created connectivity wires added to maintain connectivity at junctions:
1635 // these are marked with both IS_NEW and SELECTED_BY_DRAG; they already have the
1636 // correct endpoint constraint and don't need orthogonal bending
1637 if( ( m_mode == DRAG ) && isLineModeConstrained && line
1638 && line->HasFlag( STARTPOINT ) != line->HasFlag( ENDPOINT )
1639 && !line->HasFlag( SELECTED_BY_DRAG | IS_NEW ) )
1640 {
1641 orthoLineDrag( aCommit, line, splitDelta, aXBendCount, aYBendCount, aGrid );
1642 }
1643
1644 // Move all other items normally, including the selected end of partially selected
1645 // lines
1646 moveItem( item, splitDelta );
1647 updateItem( item, false );
1648
1649 // Update any lines connected to sheet pins to the sheet pin's location (which may
1650 // not exactly follow the splitDelta as the pins are constrained along the sheet
1651 // edges)
1652 for( const auto& [pin, lineEnd] : m_specialCaseSheetPins )
1653 {
1654 if( lineEnd.second && lineEnd.first->HasFlag( STARTPOINT ) )
1655 lineEnd.first->SetStartPoint( pin->GetPosition() );
1656 else if( !lineEnd.second && lineEnd.first->HasFlag( ENDPOINT ) )
1657 lineEnd.first->SetEndPoint( pin->GetPosition() );
1658 }
1659 }
1660
1661 // Needed to keep labels attached to a line when dragging a sheet/wire combo with a label
1662 // on the line. The label moves by splitDelta for each part of the split move, but the
1663 // line endpoints may not follow splitDelta due to orthogonal drag or sheet pin constraints,
1664 // which can put the label off the line.
1665 for( auto& [label, info] : m_specialCaseLabels )
1666 {
1667 if( !label || !info.attachedLine )
1668 continue;
1669
1670 if( info.trackMovingEnd )
1671 {
1672 label->Move( splitDelta );
1673
1674 VECTOR2I start = info.attachedLine->GetStartPoint();
1675 VECTOR2I end = info.attachedLine->GetEndPoint();
1676
1677 if( info.attachedLine->GetLength() > 0
1678 && info.attachedLine->HitTest( info.originalLabelPos, 1 )
1679 && info.originalLabelPos != start
1680 && info.originalLabelPos != end )
1681 {
1682 info.trackMovingEnd = false;
1683 label->SetPosition( info.originalLabelPos );
1684 info.originalLineStart = start;
1685 info.originalLineEnd = end;
1686 }
1687
1688 updateItem( label, false );
1689 continue;
1690 }
1691
1692 VECTOR2I start = info.attachedLine->GetStartPoint();
1693 VECTOR2I end = info.attachedLine->GetEndPoint();
1694 VECTOR2I deltaStart = start - info.originalLineStart;
1695 VECTOR2I deltaEnd = end - info.originalLineEnd;
1696
1697 // TODO: this could be improved by positioning the label based on the new line geometry,
1698 // bends are involved.
1699 //
1700 // For now, special casing the equal delta case and using splitDelta should work in most
1701 // cases as the user would expect.
1702 if( deltaStart == deltaEnd )
1703 {
1704 label->SetPosition( info.originalLabelPos + deltaStart );
1705 }
1706 else
1707 {
1708 bool startDrags = info.attachedLine->HasFlag( STARTPOINT );
1709 VECTOR2I fixedEndDelta = startDrags ? deltaEnd : deltaStart;
1710
1711 label->SetPosition( info.originalLabelPos + fixedEndDelta );
1712
1713 // If the line shrank while dragging, keep the label on the line,
1714 // otherwise the label can drift off the end of the line, and change connectivity
1715 if( !info.attachedLine->HitTest( label->GetPosition(), 1 ) )
1716 {
1717 SEG seg( start, end );
1718 label->SetPosition( seg.NearestPoint( label->GetPosition() ) );
1719
1720 VECTOR2I movingEnd = startDrags ? start : end;
1721
1722 if( label->GetPosition() == movingEnd )
1723 info.trackMovingEnd = true;
1724 }
1725 }
1726
1727 updateItem( label, false );
1728 }
1729 }
1730
1731 spreadMovingSheetPinGroups( aSelection );
1732
1733 if( aSelection.HasReferencePoint() )
1734 aSelection.SetReferencePoint( aSelection.GetReferencePoint() + aDelta );
1735}
1736
1737
1739{
1740 // Slide pins dragged together by one distance along the border, so they keep their spacing
1741 // and wrap around corners instead of collapsing onto a shared point on a perpendicular edge.
1742 std::map<SCH_SHEET*, std::vector<SCH_SHEET_PIN*>> groups;
1743
1744 for( EDA_ITEM* item : aSelection )
1745 {
1746 if( item->Type() != SCH_SHEET_PIN_T )
1747 continue;
1748
1749 SCH_SHEET_PIN* pin = static_cast<SCH_SHEET_PIN*>( item );
1750
1751 if( SCH_SHEET* sheet = pin->GetParent(); sheet && !sheet->IsSelected() && m_sheetPinDragArc.count( pin ) )
1752 {
1753 groups[sheet].push_back( pin );
1754 }
1755 }
1756
1757 for( auto& [sheet, pins] : groups )
1758 {
1759 if( pins.size() < 2 )
1760 continue;
1761
1762 // Only slide as a group when the pins started on the same edge. A mix of edges would
1763 // move in opposite directions (the border runs one way), so leave those to the normal
1764 // per-pin constraint.
1765 auto startSide = [&]( SCH_SHEET_PIN* aPin )
1766 {
1767 VECTOR2I pos;
1768 SHEET_SIDE side;
1769 sheetBorderPos( sheet, m_sheetPinDragArc[aPin], pos, side );
1770 return side;
1771 };
1772
1773 SCH_SHEET_PIN* ref = pins.front();
1774 SHEET_SIDE refStartSide = startSide( ref );
1775 bool sameEdge = true;
1776
1777 for( SCH_SHEET_PIN* pin : pins )
1778 sameEdge &= ( startSide( pin ) == refStartSide );
1779
1780 if( !sameEdge )
1781 continue;
1782
1783 // The reference pin is already on its edge, its border travel drives the group slide.
1784 long long refArc = sheetBorderArc( sheet, ref->GetSide(), ref->GetPosition() );
1785 long long slide = refArc - m_sheetPinDragArc[ref];
1786
1787 for( SCH_SHEET_PIN* pin : pins )
1788 {
1789 VECTOR2I pos;
1790 SHEET_SIDE side;
1791 sheetBorderPos( sheet, m_sheetPinDragArc[pin] + slide, pos, side );
1792
1793 pin->SetSide( side );
1794
1795 if( side == SHEET_SIDE::LEFT || side == SHEET_SIDE::RIGHT )
1796 pin->SetTextY( pos.y );
1797 else
1798 pin->SetTextX( pos.x );
1799
1800 updateItem( pin, false );
1801 }
1802 }
1803
1804 // Pull attached lines back to the moved pins.
1805 for( const auto& [pin, lineEnd] : m_specialCaseSheetPins )
1806 {
1807 if( lineEnd.second && lineEnd.first->HasFlag( STARTPOINT ) )
1808 lineEnd.first->SetStartPoint( pin->GetPosition() );
1809 else if( !lineEnd.second && lineEnd.first->HasFlag( ENDPOINT ) )
1810 lineEnd.first->SetEndPoint( pin->GetPosition() );
1811 }
1812}
1813
1814
1816 const SCH_SELECTION& aSelection )
1817{
1818 wxLogTrace( traceSchMove, "handleMoveToolActions: received event, action=%s",
1819 aEvent->Format().c_str() );
1820
1821 if( aEvent->IsAction( &ACTIONS::doDelete ) )
1822 {
1823 wxLogTrace( traceSchMove, "handleMoveToolActions: doDelete, exiting move" );
1824 const_cast<TOOL_EVENT*>( aEvent )->SetPassEvent();
1825 return false; // Exit on delete; there will no longer be anything to drag
1826 }
1827 else if( aEvent->IsAction( &ACTIONS::duplicate )
1829 || aEvent->IsAction( &ACTIONS::redo ) )
1830 {
1831 wxBell();
1832 }
1833 else if( aEvent->IsAction( &SCH_ACTIONS::rotateCW ) )
1834 {
1835 wxLogTrace( traceSchMove, "handleMoveToolActions: rotateCW event received, selection size=%u",
1836 aSelection.GetSize() );
1837 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::rotateCW, aCommit );
1838 wxLogTrace( traceSchMove, "handleMoveToolActions: rotateCW RunSynchronousAction completed" );
1839 updateStoredPositions( aSelection );
1840 wxLogTrace( traceSchMove, "handleMoveToolActions: rotateCW updateStoredPositions completed" );
1841 // Note: SCH_EDIT_TOOL::Rotate already posts refreshPreview when moving
1842 }
1843 else if( aEvent->IsAction( &SCH_ACTIONS::rotateCCW ) )
1844 {
1845 wxLogTrace( traceSchMove, "handleMoveToolActions: rotateCCW event received, selection size=%u",
1846 aSelection.GetSize() );
1847 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::rotateCCW, aCommit );
1848 wxLogTrace( traceSchMove, "handleMoveToolActions: rotateCCW RunSynchronousAction completed" );
1849 updateStoredPositions( aSelection );
1850 wxLogTrace( traceSchMove, "handleMoveToolActions: rotateCCW updateStoredPositions completed" );
1851 // Note: SCH_EDIT_TOOL::Rotate already posts refreshPreview when moving
1852 }
1853 else if( aEvent->IsAction( &ACTIONS::increment ) )
1854 {
1855 if( aEvent->HasParameter() )
1856 m_toolMgr->RunSynchronousAction( ACTIONS::increment, aCommit, aEvent->Parameter<ACTIONS::INCREMENT>() );
1857 else
1858 m_toolMgr->RunSynchronousAction( ACTIONS::increment, aCommit, ACTIONS::INCREMENT{ 1, 0 } );
1859
1860 updateStoredPositions( aSelection );
1861 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1862 }
1863 else if( aEvent->IsAction( &SCH_ACTIONS::toDLabel ) )
1864 {
1865 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toDLabel, aCommit );
1866 updateStoredPositions( aSelection );
1867 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1868 }
1869 else if( aEvent->IsAction( &SCH_ACTIONS::toGLabel ) )
1870 {
1871 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toGLabel, aCommit );
1872 updateStoredPositions( aSelection );
1873 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1874 }
1875 else if( aEvent->IsAction( &SCH_ACTIONS::toHLabel ) )
1876 {
1877 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toHLabel, aCommit );
1878 updateStoredPositions( aSelection );
1879 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1880 }
1881 else if( aEvent->IsAction( &SCH_ACTIONS::toLabel ) )
1882 {
1883 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toLabel, aCommit );
1884 updateStoredPositions( aSelection );
1885 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1886 }
1887 else if( aEvent->IsAction( &SCH_ACTIONS::toText ) )
1888 {
1889 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toText, aCommit );
1890 updateStoredPositions( aSelection );
1891 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1892 }
1893 else if( aEvent->IsAction( &SCH_ACTIONS::toTextBox ) )
1894 {
1895 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toTextBox, aCommit );
1896 updateStoredPositions( aSelection );
1897 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1898 }
1899 else if( aEvent->Action() == TA_CHOICE_MENU_CHOICE )
1900 {
1901 if( *aEvent->GetCommandId() >= ID_POPUP_SCH_SELECT_UNIT
1903 {
1904 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( m_selectionTool->GetSelection().Front() );
1905 int unit = *aEvent->GetCommandId() - ID_POPUP_SCH_SELECT_UNIT;
1906
1907 if( symbol )
1908 {
1909 m_frame->SelectUnit( symbol, unit );
1910 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1911 }
1912 }
1913 else if( *aEvent->GetCommandId() >= ID_POPUP_SCH_SELECT_BODY_STYLE
1915 {
1916 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( m_selectionTool->GetSelection().Front() );
1917 int bodyStyle = ( *aEvent->GetCommandId() - ID_POPUP_SCH_SELECT_BODY_STYLE ) + 1;
1918
1919 if( symbol && symbol->GetBodyStyle() != bodyStyle )
1920 {
1921 m_frame->SelectBodyStyle( symbol, bodyStyle );
1922 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1923 }
1924 }
1925 }
1926 else if( aEvent->IsAction( &SCH_ACTIONS::highlightNet )
1927 || aEvent->IsAction( &SCH_ACTIONS::selectOnPCB ) )
1928 {
1929 // These don't make any sense during a move. Eat them.
1930 }
1931 else
1932 {
1933 return true; // Continue processing
1934 }
1935
1936 return true; // Continue processing
1937}
1938
1939
1941{
1942 wxLogTrace( traceSchMove, "updateStoredPositions: start, selection size=%u",
1943 aSelection.GetSize() );
1944
1945 // After transformations like rotation during a move, we need to update the stored
1946 // positions that moveItem() uses, particularly for sheet pins which rely on them
1947 // for constraint calculations.
1948 int itemCount = 0;
1949
1950 for( EDA_ITEM* item : aSelection )
1951 {
1952 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item );
1953
1954 if( !schItem )
1955 continue;
1956
1957 VECTOR2I oldPos = schItem->GetStoredPos();
1958 VECTOR2I newPos = schItem->GetPosition();
1959 schItem->SetStoredPos( newPos );
1960
1961 // Re-baseline the pin's border distance after a transform (e.g. rotation).
1962 if( schItem->Type() == SCH_SHEET_PIN_T )
1963 {
1964 SCH_SHEET_PIN* pin = static_cast<SCH_SHEET_PIN*>( schItem );
1965
1966 if( m_sheetPinDragArc.count( pin ) )
1967 m_sheetPinDragArc[pin] = sheetBorderArc( pin->GetParent(), pin->GetSide(), pin->GetPosition() );
1968 }
1969
1970 wxLogTrace( traceSchMove, " item[%d] type=%d: stored pos updated (%d,%d) -> (%d,%d)",
1971 itemCount++, (int) schItem->Type(), oldPos.x, oldPos.y, newPos.x, newPos.y );
1972
1973 // Also update stored positions for sheet pins
1974 if( schItem->Type() == SCH_SHEET_T )
1975 {
1976 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( schItem );
1977 for( SCH_SHEET_PIN* pin : sheet->GetPins() )
1978 {
1979 VECTOR2I pinOldPos = pin->GetStoredPos();
1980 VECTOR2I pinNewPos = pin->GetPosition();
1981 pin->SetStoredPos( pinNewPos );
1982 wxLogTrace( traceSchMove, " sheet pin: stored pos updated (%d,%d) -> (%d,%d)",
1983 pinOldPos.x, pinOldPos.y, pinNewPos.x, pinNewPos.y );
1984 }
1985 }
1986 }
1987
1988 wxLogTrace( traceSchMove, "updateStoredPositions: complete, updated %d items", itemCount );
1989}
1990
1991
1992void SCH_MOVE_TOOL::finalizeMoveOperation( SCH_SELECTION& aSelection, SCH_COMMIT* aCommit, bool aUnselect,
1993 const std::vector<DANGLING_END_ITEM>& aInternalPoints )
1994{
1996 const bool isSlice = ( m_mode == SLICE );
1997 const bool isDragLike = ( m_mode == DRAG || m_mode == BREAK );
1998
1999 // Save whatever new bend lines and changed lines survived the drag
2000 for( SCH_LINE* newLine : m_newDragLines )
2001 {
2002 newLine->ClearEditFlags();
2003 aCommit->Added( newLine, m_frame->GetScreen() );
2004 }
2005
2006 // These lines have been changed, but aren't selected. We need to manually clear these
2007 // edit flags or they'll stick around.
2008 for( SCH_LINE* oldLine : m_changedDragLines )
2009 oldLine->ClearEditFlags();
2010
2011 controls->ForceCursorPosition( false );
2012 controls->ShowCursor( false );
2013 controls->SetAutoPan( false );
2014
2015 m_moveOffset = { 0, 0 };
2016 m_anchorPos.reset();
2017
2018 // One last update after exiting loop (for slower stuff, such as updating SCREEN's RTree)
2019 for( EDA_ITEM* item : aSelection )
2020 {
2021 updateItem( item, true );
2022
2023 if( SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( item ) )
2024 sch_item->SetConnectivityDirty( true );
2025 }
2026
2027 if( aSelection.GetSize() == 1 && aSelection.Front()->IsNew() )
2028 m_frame->SaveCopyForRepeatItem( static_cast<SCH_ITEM*>( aSelection.Front() ) );
2029
2030 m_selectionTool->RemoveItemsFromSel( &m_dragAdditions, QUIET_MODE );
2031
2033
2034 // If we move items away from a junction, we _may_ want to add a junction there
2035 // to denote the state
2036 for( const DANGLING_END_ITEM& it : aInternalPoints )
2037 {
2038 if( m_frame->GetScreen()->IsExplicitJunctionNeeded( it.GetPosition() ) )
2039 lwbTool->AddJunction( aCommit, m_frame->GetScreen(), it.GetPosition() );
2040 }
2041
2042 // Create a selection of original selection, drag selected/changed items, and new bend
2043 // lines for later before we clear them in the aCommit. We'll need these to check for new
2044 // junctions needed, etc.
2045 SCH_SELECTION selectionCopy( aSelection );
2046
2047 for( SCH_LINE* line : m_newDragLines )
2048 selectionCopy.Add( line );
2049
2050 for( SCH_LINE* line : m_changedDragLines )
2051 selectionCopy.Add( line );
2052
2053 lwbTool->TrimOverLappingWires( aCommit, &selectionCopy );
2054 lwbTool->AddJunctionsIfNeeded( aCommit, &selectionCopy );
2055
2056 // This needs to run prior to `RecalculateConnections` because we need to identify the
2057 // lines that are newly dangling
2058 if( isDragLike && !isSlice )
2059 trimDanglingLines( aCommit );
2060
2061 // Auto-rotate any moved labels
2062 for( EDA_ITEM* item : aSelection )
2063 m_frame->AutoRotateItem( m_frame->GetScreen(), static_cast<SCH_ITEM*>( item ) );
2064
2065 // Clear SELECTED_BY_DRAG and other temp flags before CleanUp so that cleanup can properly
2066 // process all items, including removing zero-length wires and unwanted stubs
2067 for( EDA_ITEM* item : m_frame->GetScreen()->Items() )
2068 item->ClearTempFlags();
2069
2070 for( EDA_ITEM* item : selectionCopy )
2071 item->ClearTempFlags();
2072
2073 m_frame->Schematic().CleanUp( aCommit );
2074
2075 // Mirror the IS_MOVING flag propagation done at the start of the move so that child items
2076 // (e.g. label fields, symbol pins/fields) don't keep their edit flags after the move ends.
2077 auto clearChildEditFlags =
2078 []( SCH_ITEM* aItem )
2079 {
2080 aItem->RunOnChildren(
2081 []( SCH_ITEM* aChild )
2082 {
2083 aChild->ClearEditFlags();
2084 },
2086 };
2087
2088 for( EDA_ITEM* item : m_frame->GetScreen()->Items() )
2089 {
2090 item->ClearEditFlags();
2091
2092 if( SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item ) )
2093 clearChildEditFlags( schItem );
2094 }
2095
2096 // Ensure any selected item not in screen main list (for instance symbol fields) has its
2097 // edit flags cleared
2098 for( EDA_ITEM* item : selectionCopy )
2099 {
2100 item->ClearEditFlags();
2101
2102 if( SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item ) )
2103 clearChildEditFlags( schItem );
2104 }
2105
2106 m_newDragLines.clear();
2107 m_changedDragLines.clear();
2108
2109 if( aUnselect )
2110 m_toolMgr->RunAction( ACTIONS::selectionClear );
2111 else
2112 m_selectionTool->RebuildSelection(); // Schematic cleanup might have merged lines, etc.
2113}
2114
2115
2117 SCH_COMMIT* aCommit )
2118{
2119 SCH_SCREEN* destScreen = aTargetSheet->GetScreen();
2120 SCH_SCREEN* srcScreen = m_frame->GetScreen();
2121
2122 BOX2I bbox;
2123
2124 for( EDA_ITEM* item : aSelection )
2125 bbox.Merge( static_cast<SCH_ITEM*>( item )->GetBoundingBox() );
2126
2127 VECTOR2I offset = VECTOR2I( 0, 0 ) - bbox.GetPosition();
2128 int step = schIUScale.MilsToIU( 50 );
2129 bool overlap = false;
2130
2131 do
2132 {
2133 BOX2I moved = bbox;
2134 moved.Move( offset );
2135 overlap = false;
2136
2137 for( SCH_ITEM* existing : destScreen->Items() )
2138 {
2139 if( moved.Intersects( existing->GetBoundingBox() ) )
2140 {
2141 overlap = true;
2142 break;
2143 }
2144 }
2145
2146 if( overlap )
2147 offset += VECTOR2I( step, step );
2148 } while( overlap );
2149
2150 for( EDA_ITEM* item : aSelection )
2151 {
2152 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( item );
2153
2154 // Remove from current screen and view manually
2155 m_frame->RemoveFromScreen( schItem, srcScreen );
2156
2157 // Move the item
2158 schItem->Move( offset );
2159
2160 // Add to destination screen manually (won't add to view since it's not current)
2161 destScreen->Append( schItem );
2162
2163 // Record in commit with CHT_DONE flag to bypass automatic screen/view operations
2164 aCommit->Stage( schItem, CHT_REMOVE | CHT_DONE, srcScreen );
2165 aCommit->Stage( schItem, CHT_ADD | CHT_DONE, destScreen );
2166 }
2167}
2168
2169
2171{
2172 // Need a local cleanup first to ensure we remove unneeded junctions
2173 m_frame->Schematic().CleanUp( aCommit, m_frame->GetScreen() );
2174
2175 std::set<SCH_ITEM*> danglers;
2176
2177 std::function<void( SCH_ITEM* )> changeHandler =
2178 [&]( SCH_ITEM* aChangedItem ) -> void
2179 {
2180 m_toolMgr->GetView()->Update( aChangedItem, KIGFX::REPAINT );
2181
2182 if( aChangedItem->IsSelected() )
2183 return;
2184
2185 SCH_LINE* line = dynamic_cast<SCH_LINE*>( aChangedItem );
2186
2187 if( !line )
2188 return;
2189
2190 // Split segments that are dangling get trimmed back since they extend
2191 // past the break point.
2192 if( line->HasFlag( IS_BROKEN ) && line->IsDangling() )
2193 {
2194 danglers.insert( aChangedItem );
2195 }
2196 // Drag wires that are completely disconnected (both ends dangling) are
2197 // stubs that should be removed. Wires with only one connected end are
2198 // still providing connectivity and must be preserved.
2199 else if( line->HasFlag( IS_NEW ) && !line->HasFlag( IS_BROKEN )
2200 && line->IsStartDangling() && line->IsEndDangling() )
2201 {
2202 danglers.insert( aChangedItem );
2203 }
2204 };
2205
2206 m_frame->GetScreen()->TestDanglingEnds( nullptr, &changeHandler );
2207
2208 for( SCH_ITEM* line : danglers )
2209 {
2210 line->SetFlags( STRUCT_DELETED );
2211 aCommit->Removed( line, m_frame->GetScreen() );
2212 updateItem( line, false ); // Update any cached visuals before commit processes
2213 m_frame->RemoveFromScreen( line, m_frame->GetScreen() );
2214 }
2215}
2216
2217
2218void SCH_MOVE_TOOL::getConnectedItems( SCH_ITEM* aOriginalItem, const VECTOR2I& aPoint, EDA_ITEMS& aList )
2219{
2220 EE_RTREE& items = m_frame->GetScreen()->Items();
2221 EE_RTREE::EE_TYPE itemsOverlapping = items.Overlapping( aOriginalItem->GetBoundingBox() );
2222 SCH_ITEM* foundJunction = nullptr;
2223 SCH_ITEM* foundSymbol = nullptr;
2224
2225 // If you're connected to a junction, you're only connected to the junction.
2226 //
2227 // But, if you're connected to a junction on a pin, you're only connected to the pin. This
2228 // is because junctions and pins have different logic for how bend lines are generated and
2229 // we need to prioritize the pin version in some cases.
2230 for( SCH_ITEM* item : itemsOverlapping )
2231 {
2232 if( item != aOriginalItem && item->IsConnected( aPoint ) )
2233 {
2234 if( item->Type() == SCH_JUNCTION_T )
2235 foundJunction = item;
2236 else if( item->Type() == SCH_SYMBOL_T )
2237 foundSymbol = item;
2238 }
2239 }
2240
2241 if( foundSymbol && foundJunction )
2242 {
2243 aList.push_back( foundSymbol );
2244 return;
2245 }
2246
2247 if( foundJunction )
2248 {
2249 aList.push_back( foundJunction );
2250 return;
2251 }
2252
2253
2254 for( SCH_ITEM* test : itemsOverlapping )
2255 {
2256 if( test == aOriginalItem || !test->CanConnect( aOriginalItem ) )
2257 continue;
2258
2259 switch( test->Type() )
2260 {
2261 case SCH_LINE_T:
2262 {
2263 SCH_LINE* line = static_cast<SCH_LINE*>( test );
2264
2265 // When getting lines for the connection cache, it's important that we only add
2266 // items at the unselected end, since that is the only end that is handled specially.
2267 // Fully selected lines, and the selected end of a partially selected line, are moved
2268 // around normally and don't care about their connections.
2269 if( ( line->HasFlag( STARTPOINT ) && aPoint == line->GetStartPoint() )
2270 || ( line->HasFlag( ENDPOINT ) && aPoint == line->GetEndPoint() ) )
2271 {
2272 continue;
2273 }
2274
2275 if( test->IsConnected( aPoint ) )
2276 aList.push_back( test );
2277
2278 // Labels can connect to a wire (or bus) anywhere along the length
2279 if( SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( aOriginalItem ) )
2280 {
2281 if( static_cast<SCH_LINE*>( test )->HitTest( label->GetPosition(), 1 ) )
2282 aList.push_back( test );
2283 }
2284
2285 break;
2286 }
2287
2288 case SCH_SHEET_T:
2289 if( aOriginalItem->Type() == SCH_LINE_T )
2290 {
2291 SCH_LINE* line = static_cast<SCH_LINE*>( aOriginalItem );
2292
2293 for( SCH_SHEET_PIN* pin : static_cast<SCH_SHEET*>( test )->GetPins() )
2294 {
2295 if( pin->IsConnected( aPoint ) )
2296 {
2297 if( pin->IsSelected() )
2298 m_specialCaseSheetPins[pin] = { line, line->GetStartPoint() == aPoint };
2299
2300 aList.push_back( pin );
2301 }
2302 }
2303 }
2304
2305 break;
2306
2307 case SCH_SYMBOL_T:
2308 case SCH_JUNCTION_T:
2309 case SCH_NO_CONNECT_T:
2310 if( test->IsConnected( aPoint ) )
2311 aList.push_back( test );
2312
2313 break;
2314
2315 case SCH_LABEL_T:
2316 case SCH_GLOBAL_LABEL_T:
2317 case SCH_HIER_LABEL_T:
2319 // Labels can connect to a wire (or bus) anywhere along the length
2320 if( aOriginalItem->Type() == SCH_LINE_T && test->CanConnect( aOriginalItem ) )
2321 {
2322 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( test );
2323 SCH_LINE* line = static_cast<SCH_LINE*>( aOriginalItem );
2324
2325 if( line->HitTest( label->GetPosition(), 1 ) )
2326 aList.push_back( label );
2327 }
2328
2329 break;
2330
2333 if( aOriginalItem->Type() == SCH_LINE_T && test->CanConnect( aOriginalItem ) )
2334 {
2335 SCH_TEXT* label = static_cast<SCH_TEXT*>( test );
2336 SCH_LINE* line = static_cast<SCH_LINE*>( aOriginalItem );
2337
2338 if( line->HitTest( aPoint, 1 ) )
2339 aList.push_back( label );
2340 }
2341
2342 break;
2343
2344 default:
2345 break;
2346 }
2347 }
2348}
2349
2350
2351void SCH_MOVE_TOOL::getConnectedDragItems( SCH_COMMIT* aCommit, SCH_ITEM* aSelectedItem, const VECTOR2I& aPoint,
2352 EDA_ITEMS& aList )
2353{
2354 EE_RTREE& items = m_frame->GetScreen()->Items();
2355 std::set<SCH_ITEM*> connectableCandidates;
2356 std::vector<SCH_ITEM*> itemsConnectable;
2357 bool ptHasUnselectedJunction = false;
2358
2359 for( SCH_ITEM* item : items.Overlapping( aSelectedItem->GetBoundingBox() ) )
2360 connectableCandidates.insert( item );
2361
2362 // Labels can connect at their anchor even if the label bbox doesn't overlap the target, e.g.
2363 // sheet pins can do this sometimes with just net labels and no wires.
2364 if( dynamic_cast<SCH_LABEL_BASE*>( aSelectedItem ) )
2365 {
2366 for( SCH_ITEM* item : items.Overlapping( aPoint, 1 ) )
2367 connectableCandidates.insert( item );
2368 }
2369
2370 auto makeNewWire =
2371 [this]( SCH_COMMIT* commit, SCH_ITEM* fixed, SCH_ITEM* selected, const VECTOR2I& start,
2372 const VECTOR2I& end )
2373 {
2374 SCH_LINE* newWire;
2375 bool isBusLabel = false;
2376
2377 if( SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( fixed ) )
2378 isBusLabel |= SCH_CONNECTION::IsBusLabel( label->GetText() );
2379
2380 if( SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( selected ) )
2381 isBusLabel |= SCH_CONNECTION::IsBusLabel( label->GetText() );
2382
2383 // Add a new newWire between the fixed item and the selected item so the selected
2384 // item can be dragged.
2385 if( fixed->GetLayer() == LAYER_BUS_JUNCTION || fixed->GetLayer() == LAYER_BUS
2386 || selected->GetLayer() == LAYER_BUS || isBusLabel )
2387 {
2388 newWire = new SCH_LINE( start, LAYER_BUS );
2389 }
2390 else
2391 {
2392 newWire = new SCH_LINE( start, LAYER_WIRE );
2393 }
2394
2395 newWire->SetFlags( IS_NEW );
2396 newWire->SetConnectivityDirty( true );
2397
2398 SCH_LINE* selectedLine = dynamic_cast<SCH_LINE*>( selected );
2399 SCH_LINE* fixedLine = dynamic_cast<SCH_LINE*>( fixed );
2400
2401 if( selectedLine )
2402 {
2403 newWire->SetLastResolvedState( selected );
2404 cloneWireConnection( newWire, selectedLine, m_frame );
2405 }
2406 else if( fixedLine )
2407 {
2408 newWire->SetLastResolvedState( fixed );
2409 cloneWireConnection( newWire, fixedLine, m_frame );
2410 }
2411
2412 newWire->SetEndPoint( end );
2413 m_frame->AddToScreen( newWire, m_frame->GetScreen() );
2414 commit->Added( newWire, m_frame->GetScreen() );
2415
2416 return newWire;
2417 };
2418
2419 auto makeNewJunction =
2420 [this]( SCH_COMMIT* commit, SCH_LINE* line, const VECTOR2I& pt )
2421 {
2422 SCH_JUNCTION* junction = new SCH_JUNCTION( pt );
2423 junction->SetFlags( IS_NEW );
2424 junction->SetConnectivityDirty( true );
2425 junction->SetLastResolvedState( line );
2426
2427 if( line->IsBus() )
2428 junction->SetLayer( LAYER_BUS_JUNCTION );
2429
2430 m_frame->AddToScreen( junction, m_frame->GetScreen() );
2431 commit->Added( junction, m_frame->GetScreen() );
2432
2433 return junction;
2434 };
2435
2436 for( SCH_ITEM* item : connectableCandidates )
2437 {
2438 if( item->Type() == SCH_SHEET_T )
2439 {
2440 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
2441
2442 // A sheet inside a selected group moves with the group, so its pins should not be
2443 // treated as fixed connection anchors.
2444 if( sheet->HasSelectedAncestorGroup() )
2445 continue;
2446
2447 for( SCH_SHEET_PIN* pin : sheet->GetPins() )
2448 {
2449 if( !pin->IsSelected()
2450 && pin->GetPosition() == aPoint
2451 && pin->CanConnect( aSelectedItem ) )
2452 {
2453 itemsConnectable.push_back( pin );
2454 }
2455 }
2456
2457 continue;
2458 }
2459
2460 // Skip ourselves, skip already selected items (but not lines, they need both ends tested)
2461 // and skip unconnectable items. Items inside a selected group are also moving with the
2462 // selection even though they do not carry the SELECTED flag themselves; treating them as
2463 // fixed anchors causes spurious stub wires to be created at the group boundary.
2464 if( item == aSelectedItem
2465 || ( item->Type() != SCH_LINE_T && ( item->IsSelected() || item->HasSelectedAncestorGroup() ) )
2466 || !item->CanConnect( aSelectedItem ) )
2467 {
2468 continue;
2469 }
2470
2471 itemsConnectable.push_back( item );
2472 }
2473
2474 for( SCH_ITEM* item : itemsConnectable )
2475 {
2476 if( item->Type() == SCH_JUNCTION_T && item->IsConnected( aPoint ) && !item->IsSelected() )
2477 {
2478 ptHasUnselectedJunction = true;
2479 break;
2480 }
2481 }
2482
2483 SCH_LINE* newWire = nullptr;
2484
2485 for( SCH_ITEM* test : itemsConnectable )
2486 {
2487 KICAD_T testType = test->Type();
2488
2489 switch( testType )
2490 {
2491 case SCH_LINE_T:
2492 {
2493 // Select the connected end of wires/bus connections that don't have an unselected
2494 // junction isolating them from the drag
2495 if( ptHasUnselectedJunction )
2496 break;
2497
2498 SCH_LINE* line = static_cast<SCH_LINE*>( test );
2499
2500 // A line that is itself a member of a selected group is already moving with that
2501 // group; do not add it as a drag attachment or it will move twice.
2502 bool lineInSelectedGroup = line->HasSelectedAncestorGroup();
2503
2504 if( line->GetStartPoint() == aPoint )
2505 {
2506 // It's possible to manually select one end of a line and get a drag
2507 // connected other end, so we set the flag and then early exit the loop
2508 // later if the other drag items like labels attached to the line have
2509 // already been grabbed during the partial selection process.
2510 if( !lineInSelectedGroup )
2511 line->SetFlags( STARTPOINT );
2512
2513 if( line->HasFlag( SELECTED ) || line->HasFlag( SELECTED_BY_DRAG )
2514 || lineInSelectedGroup )
2515 {
2516 continue;
2517 }
2518 else
2519 {
2520 line->SetFlags( SELECTED_BY_DRAG );
2521 aList.push_back( line );
2522 }
2523 }
2524 else if( line->GetEndPoint() == aPoint )
2525 {
2526 if( !lineInSelectedGroup )
2527 line->SetFlags( ENDPOINT );
2528
2529 if( line->HasFlag( SELECTED ) || line->HasFlag( SELECTED_BY_DRAG )
2530 || lineInSelectedGroup )
2531 {
2532 continue;
2533 }
2534 else
2535 {
2536 line->SetFlags( SELECTED_BY_DRAG );
2537 aList.push_back( line );
2538 }
2539 }
2540 else
2541 {
2542 switch( aSelectedItem->Type() )
2543 {
2544 // These items can connect anywhere along a line
2547 case SCH_LABEL_T:
2548 case SCH_HIER_LABEL_T:
2549 case SCH_GLOBAL_LABEL_T:
2551 // Only add a line if this line is unselected; if the label and line are both
2552 // selected they'll move together
2553 if( line->HitTest( aPoint, 1 ) && !line->HasFlag( SELECTED )
2554 && !line->HasFlag( SELECTED_BY_DRAG ) )
2555 {
2556 newWire = makeNewWire( aCommit, line, aSelectedItem, aPoint, aPoint );
2557 newWire->SetFlags( SELECTED_BY_DRAG | STARTPOINT );
2558 newWire->StoreAngle( ( line->Angle() + ANGLE_90 ).Normalize() );
2559 aList.push_back( newWire );
2560
2561 if( aPoint != line->GetStartPoint() && aPoint != line->GetEndPoint() )
2562 {
2563 // Split line in half
2564 aCommit->Modify( line, m_frame->GetScreen() );
2565
2566 VECTOR2I oldEnd = line->GetEndPoint();
2567 line->SetEndPoint( aPoint );
2568
2569 makeNewWire( aCommit, line, line, aPoint, oldEnd );
2570 makeNewJunction( aCommit, line, aPoint );
2571 }
2572 else
2573 {
2574 m_lineConnectionCache[ newWire ] = { line };
2575 m_lineConnectionCache[ line ] = { newWire };
2576 }
2577 }
2578 break;
2579
2580 default:
2581 break;
2582 }
2583
2584 break;
2585 }
2586
2587 // When only one end moves, keep attached labels tracking the moving end so they stay
2588 // connected to the line.
2589 for( SCH_ITEM* item : items.Overlapping( line->GetBoundingBox() ) )
2590 {
2591 SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( item );
2592
2593 if( !label || label->IsSelected() )
2594 continue; // These will be moved on their own because they're selected
2595
2596 if( label->HasFlag( SELECTED_BY_DRAG ) )
2597 continue;
2598
2599 if( label->CanConnect( line ) && line->HitTest( label->GetPosition(), 1 ) )
2600 {
2601 label->SetFlags( SELECTED_BY_DRAG );
2602 aList.push_back( label );
2603
2605 info.attachedLine = line;
2606 info.originalLabelPos = label->GetPosition();
2607 info.originalLineStart = line->GetStartPoint();
2608 info.originalLineEnd = line->GetEndPoint();
2609 m_specialCaseLabels[label] = info;
2610 }
2611 }
2612
2613 break;
2614 }
2615
2616 case SCH_SHEET_T:
2617 for( SCH_SHEET_PIN* pin : static_cast<SCH_SHEET*>( test )->GetPins() )
2618 {
2619 if( pin->IsConnected( aPoint ) )
2620 {
2621 if( pin->IsSelected() && aSelectedItem->Type() == SCH_LINE_T )
2622 {
2623 SCH_LINE* line = static_cast<SCH_LINE*>( aSelectedItem );
2624 m_specialCaseSheetPins[ pin ] = { line, line->GetStartPoint() == aPoint };
2625 }
2626 else if( !newWire )
2627 {
2628 // Add a new wire between the sheetpin and the selected item so the
2629 // selected item can be dragged.
2630 newWire = makeNewWire( aCommit, pin, aSelectedItem, aPoint, aPoint );
2631 newWire->SetFlags( SELECTED_BY_DRAG | STARTPOINT );
2632 aList.push_back( newWire );
2633 }
2634 }
2635 }
2636
2637 break;
2638
2639 case SCH_SYMBOL_T:
2640 case SCH_JUNCTION_T:
2641 if( test->IsConnected( aPoint ) && !newWire )
2642 {
2643 // Add a new wire between the symbol or junction and the selected item so
2644 // the selected item can be dragged.
2645 newWire = makeNewWire( aCommit, test, aSelectedItem, aPoint, aPoint );
2646 newWire->SetFlags( SELECTED_BY_DRAG | STARTPOINT );
2647 aList.push_back( newWire );
2648 }
2649
2650 break;
2651
2652 case SCH_NO_CONNECT_T:
2653 // Select no-connects that are connected to items being moved.
2654 if( !test->HasFlag( SELECTED_BY_DRAG ) && test->IsConnected( aPoint ) )
2655 {
2656 aList.push_back( test );
2657 test->SetFlags( SELECTED_BY_DRAG );
2658 }
2659
2660 break;
2661
2662 case SCH_LABEL_T:
2663 case SCH_GLOBAL_LABEL_T:
2664 case SCH_HIER_LABEL_T:
2666 case SCH_SHEET_PIN_T:
2667 // Performance optimization:
2668 if( test->HasFlag( SELECTED_BY_DRAG ) )
2669 break;
2670
2671 // Select labels that are connected to a wire (or bus) being moved.
2672 if( aSelectedItem->Type() == SCH_LINE_T && test->CanConnect( aSelectedItem ) )
2673 {
2674 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( test );
2675 SCH_LINE* line = static_cast<SCH_LINE*>( aSelectedItem );
2676
2677 bool oneEndFixed = !line->HasFlag( STARTPOINT ) || !line->HasFlag( ENDPOINT );
2678
2679 if( line->HitTest( label->GetTextPos(), 1 ) )
2680 {
2681 if( ( !line->HasFlag( STARTPOINT ) && label->GetPosition() == line->GetStartPoint() )
2682 || ( !line->HasFlag( ENDPOINT ) && label->GetPosition() == line->GetEndPoint() ) )
2683 {
2684 //If we have a line selected at only one end, don't grab labels
2685 //connected directly to the unselected endpoint
2686 break;
2687 }
2688 else
2689 {
2690 label->SetFlags( SELECTED_BY_DRAG );
2691 aList.push_back( label );
2692
2693 if( oneEndFixed )
2694 {
2696 info.attachedLine = line;
2697 info.originalLabelPos = label->GetPosition();
2698 info.originalLineStart = line->GetStartPoint();
2699 info.originalLineEnd = line->GetEndPoint();
2700 m_specialCaseLabels[label] = info;
2701 }
2702 }
2703 }
2704 }
2705 else if( test->IsConnected( aPoint ) && !newWire )
2706 {
2707 // Add a new wire between the label and the selected item so the selected item
2708 // can be dragged.
2709 newWire = makeNewWire( aCommit, test, aSelectedItem, aPoint, aPoint );
2710 newWire->SetFlags( SELECTED_BY_DRAG | STARTPOINT );
2711 aList.push_back( newWire );
2712 }
2713
2714 break;
2715
2718 // Performance optimization:
2719 if( test->HasFlag( SELECTED_BY_DRAG ) )
2720 break;
2721
2722 // Select bus entries that are connected to a bus being moved.
2723 if( aSelectedItem->Type() == SCH_LINE_T && test->CanConnect( aSelectedItem ) )
2724 {
2725 SCH_LINE* line = static_cast<SCH_LINE*>( aSelectedItem );
2726
2727 if( ( !line->HasFlag( STARTPOINT ) && test->IsConnected( line->GetStartPoint() ) )
2728 || ( !line->HasFlag( ENDPOINT ) && test->IsConnected( line->GetEndPoint() ) ) )
2729 {
2730 // If we have a line selected at only one end, don't grab bus entries
2731 // connected directly to the unselected endpoint
2732 continue;
2733 }
2734
2735 for( VECTOR2I& point : test->GetConnectionPoints() )
2736 {
2737 if( line->HitTest( point, 1 ) )
2738 {
2739 test->SetFlags( SELECTED_BY_DRAG );
2740 aList.push_back( test );
2741
2742 // A bus entry needs its wire & label as well
2743 std::vector<VECTOR2I> ends = test->GetConnectionPoints();
2744 VECTOR2I otherEnd;
2745
2746 if( ends[0] == point )
2747 otherEnd = ends[1];
2748 else
2749 otherEnd = ends[0];
2750
2751 getConnectedDragItems( aCommit, test, otherEnd, aList );
2752
2753 // No need to test the other end of the bus entry
2754 break;
2755 }
2756 }
2757 }
2758
2759 break;
2760
2761 default:
2762 break;
2763 }
2764 }
2765}
2766
2767
2768void SCH_MOVE_TOOL::moveItem( EDA_ITEM* aItem, const VECTOR2I& aDelta )
2769{
2770 static int moveCallCount = 0;
2771 wxLogTrace( traceSchMove, "moveItem[%d]: type=%d, delta=(%d,%d)",
2772 ++moveCallCount, aItem->Type(), aDelta.x, aDelta.y );
2773
2774 switch( aItem->Type() )
2775 {
2776 case SCH_LINE_T:
2777 if( m_mode == MOVE )
2778 {
2779 // In MOVE mode, both endpoints always move
2780 static_cast<SCH_LINE*>( aItem )->Move( aDelta );
2781 }
2782 else
2783 {
2784 // In DRAG mode, only flagged endpoints move - use shared function
2785 MoveSchematicItem( aItem, aDelta );
2786 }
2787
2788 break;
2789
2790 case SCH_PIN_T:
2791 case SCH_FIELD_T:
2792 {
2793 SCH_ITEM* parent = (SCH_ITEM*) aItem->GetParent();
2794 VECTOR2I delta( aDelta );
2795
2796 if( parent && parent->Type() == SCH_SYMBOL_T )
2797 {
2798 SCH_SYMBOL* symbol = (SCH_SYMBOL*) aItem->GetParent();
2799 TRANSFORM transform = symbol->GetTransform().InverseTransform();
2800
2801 delta = transform.TransformCoordinate( delta );
2802 }
2803
2804 static_cast<SCH_ITEM*>( aItem )->Move( delta );
2805
2806 // If we're moving a field with respect to its parent then it's no longer auto-placed
2807 if( aItem->Type() == SCH_FIELD_T && parent && !parent->IsSelected() )
2809
2810 break;
2811 }
2812
2813 case SCH_SHEET_PIN_T:
2814 // Use shared function for sheet pin movement
2815 MoveSchematicItem( aItem, aDelta );
2816 break;
2817
2818 case SCH_LABEL_T:
2820 case SCH_GLOBAL_LABEL_T:
2821 case SCH_HIER_LABEL_T:
2822 {
2823 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( aItem );
2824 if( !m_specialCaseLabels.count( label ) )
2825 label->Move( aDelta );
2826
2827 break;
2828 }
2829
2830 default:
2831 static_cast<SCH_ITEM*>( aItem )->Move( aDelta );
2832 break;
2833 }
2834
2835 aItem->SetFlags( IS_MOVING );
2836}
2837
2838
2840{
2842 SCH_SELECTION& selection = m_selectionTool->RequestSelection( SCH_COLLECTOR::MovableItems );
2843
2844 m_selectionTool->FilterSelectionForLockedItems();
2845
2846 GRID_HELPER_GRIDS selectionGrid = grid.GetSelectionGrid( selection );
2847 SCH_COMMIT commit( m_toolMgr );
2848
2849 auto doMoveItem =
2850 [&]( EDA_ITEM* item, const VECTOR2I& delta )
2851 {
2852 commit.Modify( item, m_frame->GetScreen(), RECURSE_MODE::RECURSE );
2853
2854 // Ensure only one end is moved when calling moveItem
2855 // i.e. we are in drag mode
2856 MOVE_MODE tmpMode = m_mode;
2857 m_mode = DRAG;
2858 moveItem( item, delta );
2859 m_mode = tmpMode;
2860
2861 item->ClearFlags( IS_MOVING );
2862 updateItem( item, true );
2863 };
2864
2865 for( SCH_ITEM* it : m_frame->GetScreen()->Items() )
2866 {
2867 if( !it->IsSelected() )
2868 it->ClearFlags( STARTPOINT | ENDPOINT );
2869
2870 if( !selection.IsHover() && it->IsSelected() )
2871 it->SetFlags( STARTPOINT | ENDPOINT );
2872
2873 it->SetStoredPos( it->GetPosition() );
2874
2875 if( it->Type() == SCH_SHEET_T )
2876 {
2877 for( SCH_SHEET_PIN* pin : static_cast<SCH_SHEET*>( it )->GetPins() )
2878 pin->SetStoredPos( pin->GetPosition() );
2879 }
2880 }
2881
2882 SCH_ALIGNMENT_CALLBACKS callbacks;
2883
2884 callbacks.m_doMoveItem = doMoveItem;
2885
2886 callbacks.m_getConnectedDragItems =
2887 [&]( SCH_ITEM* aItem, const VECTOR2I& aPoint, EDA_ITEMS& aList )
2888 {
2889 getConnectedDragItems( &commit, aItem, aPoint, aList );
2890 };
2891
2892 callbacks.m_updateItem =
2893 [&]( EDA_ITEM* aItem )
2894 {
2895 updateItem( aItem, true );
2896 };
2897
2898 std::vector<EDA_ITEM*> items( selection.begin(), selection.end() );
2899 AlignSchematicItemsToGrid( m_frame->GetScreen(), items, grid, selectionGrid, callbacks );
2900
2902 lwbTool->TrimOverLappingWires( &commit, &selection );
2903 lwbTool->AddJunctionsIfNeeded( &commit, &selection );
2904
2906
2907 m_frame->Schematic().CleanUp( &commit );
2908 commit.Push( _( "Align Items to Grid" ) );
2909 return 0;
2910}
2911
2912
2914{
2915 // Remove new bend lines added during the drag
2916 for( SCH_LINE* newLine : m_newDragLines )
2917 {
2918 m_frame->RemoveFromScreen( newLine, m_frame->GetScreen() );
2919 delete newLine;
2920 }
2921
2922 m_newDragLines.clear();
2923}
2924
2925
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
@ CURSOR_RIGHT
Definition actions.h:309
@ CURSOR_LEFT
Definition actions.h:307
@ CURSOR_UP
Definition actions.h:303
@ CURSOR_DOWN
Definition actions.h:305
static TOOL_ACTION undo
Definition actions.h:71
static TOOL_ACTION duplicate
Definition actions.h:80
static TOOL_ACTION doDelete
Definition actions.h:81
static TOOL_ACTION cursorClick
Definition actions.h:176
static TOOL_ACTION redo
Definition actions.h:72
static TOOL_ACTION increment
Definition actions.h:90
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:220
static TOOL_ACTION refreshPreview
Definition actions.h:155
constexpr const Vec & GetPosition() const
Definition box2.h:207
constexpr coord_type GetY() const
Definition box2.h:204
constexpr size_type GetWidth() const
Definition box2.h:210
constexpr coord_type GetX() const
Definition box2.h:203
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:654
constexpr size_type GetHeight() const
Definition box2.h:211
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:164
COMMIT & Added(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Notify observers that aItem has been added.
Definition commit.h:80
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
COMMIT & Removed(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Definition commit.h:92
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Add a menu entry to run a TOOL_ACTION on selected items.
static void sort_dangling_end_items(std::vector< DANGLING_END_ITEM > &aItemListByType, std::vector< DANGLING_END_ITEM > &aItemListByPos)
Both contain the same information.
Definition sch_item.cpp:980
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition sch_item.h:93
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
virtual VECTOR2I GetPosition() const
Definition eda_item.h:282
virtual void ClearEditFlags()
Definition eda_item.h:166
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:135
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:152
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:114
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition eda_item.h:154
bool IsSelected() const
Definition eda_item.h:132
EDA_ITEM * GetParent() const
Definition eda_item.h:110
bool HasSelectedAncestorGroup() const
Definition eda_item.cpp:106
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition eda_item.h:156
bool IsNew() const
Definition eda_item.h:129
virtual VECTOR2I GetTextPos() const
Definition eda_text.h:294
Implement an R-tree for fast spatial and type indexing of schematic items.
Definition sch_rtree.h:34
EE_TYPE Overlapping(const BOX2I &aRect) const
Definition sch_rtree.h:226
static const TOOL_EVENT SelectedItemsMoved
Used to inform tools that the selection should temporarily be non-editable.
Definition actions.h:353
An interface for classes handling user events controlling the view behavior such as zooming,...
virtual void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0))
Place the cursor immediately at a given point.
virtual void ShowCursor(bool aEnabled)
Enable or disables display of cursor.
VECTOR2D GetCursorPosition() const
Return the current cursor position in world coordinates.
virtual void SetCursorPosition(const VECTOR2D &aPosition, bool aWarpView=true, bool aTriggeredByArrows=false, long aArrowCommand=0)=0
Move cursor to the requested position expressed in world coordinates.
virtual void SetAutoPan(bool aEnabled)
Turn on/off auto panning (this feature is used when there is a tool active (eg.
const VC_SETTINGS & GetSettings() const
Return the current VIEW_CONTROLS settings.
static TOOL_ACTION rotateCCW
static TOOL_ACTION breakWire
static TOOL_ACTION toText
static TOOL_ACTION restartMove
static TOOL_ACTION toHLabel
static TOOL_ACTION rotateCW
static TOOL_ACTION drag
static TOOL_ACTION toLabel
static TOOL_ACTION alignToGrid
static TOOL_ACTION toDLabel
static TOOL_ACTION slice
static TOOL_ACTION toTextBox
static TOOL_ACTION highlightNet
static TOOL_ACTION repeatDrawItem
static TOOL_ACTION toGLabel
static TOOL_ACTION selectOnPCB
static TOOL_ACTION move
static const std::vector< KICAD_T > MovableItems
COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE) override
Add a change of the item aItem of type aChangeType to the change list.
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
virtual void Revert() override
Revert the commit by restoring the modified items state.
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
void Clone(const SCH_CONNECTION &aOther)
Copies connectivity information (but not parent) from another connection.
static bool IsBusLabel(const wxString &aLabel)
Test if aLabel has a bus notation.
Schematic editor (Eeschema) main window.
SCH_SHEET_PATH & GetCurrentSheet() const
A set of SCH_ITEMs (i.e., without duplicates).
Definition sch_group.h:48
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
void SetStoredPos(const VECTOR2I &aPos)
Definition sch_item.h:302
virtual bool CanConnect(const SCH_ITEM *aItem) const
Definition sch_item.h:519
virtual void RunOnChildren(const std::function< void(SCH_ITEM *)> &aFunction, RECURSE_MODE aMode)
Definition sch_item.h:628
int GetBodyStyle() const
Definition sch_item.h:242
SCH_CONNECTION * InitializeConnection(const SCH_SHEET_PATH &aPath, CONNECTION_GRAPH *aGraph)
Create a new connection object associated with this object.
Definition sch_item.cpp:580
virtual void Move(const VECTOR2I &aMoveVector)
Move the item by aMoveVector to a new position.
Definition sch_item.h:396
void SetLayer(SCH_LAYER_ID aLayer)
Definition sch_item.h:339
void SetConnectivityDirty(bool aDirty=true)
Definition sch_item.h:587
void SetFieldsAutoplaced(AUTOPLACE_ALGO aAlgo)
Definition sch_item.h:624
bool IsConnected(const VECTOR2I &aPoint) const
Test the item to see if it is connected to aPoint.
Definition sch_item.cpp:478
virtual bool IsMovableFromAnchorPoint() const
Check if object is movable from the anchor point.
Definition sch_item.h:299
SCH_CONNECTION * Connection(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieve the connection associated with this object in the given sheet.
Definition sch_item.cpp:487
VECTOR2I & GetStoredPos()
Definition sch_item.h:301
bool IsGroupableType() const
Definition sch_item.cpp:113
virtual std::vector< VECTOR2I > GetConnectionPoints() const
Add all the connection points for this item to aPoints.
Definition sch_item.h:539
void SetLastResolvedState(const SCH_ITEM *aItem) override
void Move(const VECTOR2I &aMoveVector) override
Move the item by aMoveVector to a new position.
bool CanConnect(const SCH_ITEM *aItem) const override
Definition sch_label.h:146
Tool responsible for drawing/placing items (symbols, wires, buses, labels, etc.)
int AddJunctionsIfNeeded(SCH_COMMIT *aCommit, SCH_SELECTION *aSelection)
Handle the addition of junctions to a selection of objects.
SCH_JUNCTION * AddJunction(SCH_COMMIT *aCommit, SCH_SCREEN *aScreen, const VECTOR2I &aPos)
int TrimOverLappingWires(SCH_COMMIT *aCommit, SCH_SELECTION *aSelection)
Logic to remove wires when overlapping correct items.
static bool IsDrawingLineWireOrBus(const SELECTION &aSelection)
void BreakSegment(SCH_COMMIT *aCommit, SCH_LINE *aSegment, const VECTOR2I &aPoint, SCH_LINE **aNewSegment, SCH_SCREEN *aScreen)
Break a single segment into two at the specified point.
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:38
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
Definition sch_line.cpp:855
void StoreAngle()
Save the current line angle.
Definition sch_line.h:111
std::vector< VECTOR2I > GetConnectionPoints() const override
Add all the connection points for this item to aPoints.
Definition sch_line.cpp:755
bool IsStartDangling() const
Definition sch_line.h:299
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition sch_line.cpp:272
EDA_ANGLE Angle() const
Get the angle between the start and end lines.
Definition sch_line.h:100
VECTOR2I GetEndPoint() const
Definition sch_line.h:144
VECTOR2I GetStartPoint() const
Definition sch_line.h:135
bool IsEndDangling() const
Definition sch_line.h:300
void MoveEnd(const VECTOR2I &aMoveVector)
Definition sch_line.cpp:217
void SetLastResolvedState(const SCH_ITEM *aItem) override
Definition sch_line.h:159
void MoveStart(const VECTOR2I &aMoveVector)
Definition sch_line.cpp:211
double GetLength() const
Definition sch_line.cpp:288
void SetEndPoint(const VECTOR2I &aPosition)
Definition sch_line.h:145
bool IsDangling() const override
Definition sch_line.h:301
void moveSelectionToSheet(SCH_SELECTION &aSelection, SCH_SHEET *aTarget, SCH_COMMIT *aCommit)
Clears the new drag lines and removes them from the screen.
void refreshSelectionTraits(const SCH_SELECTION &aSelection, bool &aHasSheetPins, bool &aHasGraphicItems, bool &aHasNonGraphicItems, bool &aIsGraphicsOnly)
Initialize the move/drag operation, setting up flags and connections.
bool Init() override
Init() is called once upon a registration of the tool.
VECTOR2I m_cursor
void trimDanglingLines(SCH_COMMIT *aCommit)
Break or slice the current selection before initiating a move, if required.
void orthoLineDrag(SCH_COMMIT *aCommit, SCH_LINE *line, const VECTOR2I &splitDelta, int &xBendCount, int &yBendCount, const EE_GRID_HELPER &grid)
std::unordered_set< SCH_LINE * > m_newDragLines
Lines changed by drag algorithm that weren't selected.
SCH_SHEET * findTargetSheet(const SCH_SELECTION &aSelection, const VECTOR2I &aCursorPos, bool aHasSheetPins, bool aIsGraphicsOnly, bool aCtrlDown)
Perform the actual move of items by delta, handling split moves and orthogonal dragging.
bool handleMoveToolActions(const TOOL_EVENT *aEvent, SCH_COMMIT *aCommit, const SCH_SELECTION &aSelection)
Update stored positions after transformations (rotation, mirroring, etc.) during move.
bool checkMoveInProgress(const TOOL_EVENT &aEvent, SCH_COMMIT *aCommit, bool aCurrentModeIsDragLike, bool aWasDragging)
< Check if a move is already in progress and handle state transitions
void initializeMoveOperation(const TOOL_EVENT &aEvent, SCH_SELECTION &aSelection, SCH_COMMIT *aCommit, std::vector< DANGLING_END_ITEM > &aInternalPoints, GRID_HELPER_GRIDS &aSnapLayer)
Setup items for drag operation, collecting connected items.
OPT_VECTOR2I m_anchorPos
void performItemMove(SCH_SELECTION &aSelection, const VECTOR2I &aDelta, SCH_COMMIT *aCommit, int &aXBendCount, int &aYBendCount, const EE_GRID_HELPER &aGrid)
Slide a group of dragged sheet pins along the sheet border so they keep their spacing.
int Main(const TOOL_EVENT &aEvent)
Run an interactive move of the selected items, or the item under the cursor.
SCH_SELECTION & prepareSelection(bool &aUnselect)
Refresh selection traits (sheet pins, graphic items, etc.)
std::vector< SCH_JUNCTION * > m_hiddenJunctions
void spreadMovingSheetPinGroups(const SCH_SELECTION &aSelection)
Handle tool action events during the move operation.
void setupItemsForMove(SCH_SELECTION &aSelection, std::vector< DANGLING_END_ITEM > &aInternalPoints)
Find the target sheet for dropping items (if any)
bool m_inMoveTool
< Re-entrancy guard
std::vector< KIID > m_dragAdditions
Cache of the line's original connections before dragging started.
void moveItem(EDA_ITEM *aItem, const VECTOR2I &aDelta)
Find additional items for a drag operation.
void setupItemsForDrag(SCH_SELECTION &aSelection, SCH_COMMIT *aCommit)
Setup items for move operation, marking dangling ends.
std::unordered_set< SCH_LINE * > m_changedDragLines
Junctions that were hidden during the move.
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
void finalizeMoveOperation(SCH_SELECTION &aSelection, SCH_COMMIT *aCommit, bool aUnselect, const std::vector< DANGLING_END_ITEM > &aInternalPoints)
void setTransitions() override
Cleanup dangling lines left after a drag.
void getConnectedItems(SCH_ITEM *aOriginalItem, const VECTOR2I &aPoint, EDA_ITEMS &aList)
std::map< SCH_LINE *, EDA_ITEMS > m_lineConnectionCache
Lines added at bend points dynamically during the move.
OPT_VECTOR2I m_breakPos
std::map< SCH_SHEET_PIN *, long long > m_sheetPinDragArc
void updateStoredPositions(const SCH_SELECTION &aSelection)
Finalize the move operation, updating junctions and cleaning up.
bool doMoveSelection(const TOOL_EVENT &aEvent, SCH_COMMIT *aCommit)
void getConnectedDragItems(SCH_COMMIT *aCommit, SCH_ITEM *fixed, const VECTOR2I &selected, EDA_ITEMS &aList)
VECTOR2I m_moveOffset
Last cursor position (needed for getModificationPoint() to avoid changes of edit reference point).
std::map< SCH_LABEL_BASE *, SPECIAL_CASE_LABEL_INFO > m_specialCaseLabels
int AlignToGrid(const TOOL_EVENT &aEvent)
Align selected elements to the grid.
void clearNewDragLines()
Set up handlers for various events.
MOVE_MODE m_mode
Items (such as wires) which were added to the selection for a drag.
void preprocessBreakOrSliceSelection(SCH_COMMIT *aCommit, const TOOL_EVENT &aEvent)
std::map< SCH_SHEET_PIN *, std::pair< SCH_LINE *, bool > > m_specialCaseSheetPins
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:115
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Define a sheet pin (label) used in sheets to create hierarchical schematics.
SHEET_SIDE GetSide() const
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
bool IsTopLevelSheet() const
Check if this sheet is a top-level sheet.
SCH_SHEET_PIN * GetPin(const VECTOR2I &aPosition)
Return the sheet pin item found at aPosition in the sheet.
VECTOR2I GetSize() const
Definition sch_sheet.h:141
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:139
VECTOR2I GetPosition() const override
Definition sch_sheet.h:490
const BOX2I GetBodyBoundingBox() const
Return a bounding box for the sheet body but not the fields.
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition sch_sheet.h:227
Schematic symbol object.
Definition sch_symbol.h:69
VECTOR2I GetPosition() const override
Definition sch_text.h:146
void updateItem(EDA_ITEM *aItem, bool aUpdateRTree) const
bool Init() override
Init() is called once upon a registration of the tool.
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
SCH_TOOL_BASE(const std::string &aName)
SCH_SELECTION_TOOL * m_selectionTool
Definition seg.h:38
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition seg.cpp:629
static SELECTION_CONDITION OnlyTypes(std::vector< KICAD_T > aTypes)
Create a functor that tests if the selected items are only of given types.
virtual void Add(EDA_ITEM *aItem)
A null aItem is ignored; the selection never holds null members.
Definition selection.cpp:38
ITER end()
Definition selection.h:76
ITER begin()
Definition selection.h:75
VECTOR2I GetReferencePoint() const
bool IsHover() const
Definition selection.h:85
virtual unsigned int GetSize() const override
Return the number of stored items.
Definition selection.h:104
EDA_ITEM * Front() const
Definition selection.h:176
int Size() const
Returns the number of selected parts.
Definition selection.h:120
void SetReferencePoint(const VECTOR2I &aP)
bool Empty() const
Checks if there is anything selected.
Definition selection.h:114
std::vector< EDA_ITEM * > GetItemsSortedByTypeAndXY(bool leftBeforeRight=true, bool topBeforeBottom=true) const
Returns a copy of this selection of items sorted by their X then Y position.
bool HasReferencePoint() const
Definition selection.h:215
const TRANSFORM & GetTransform() const
Definition symbol.h:243
KIGFX::VIEW_CONTROLS * getViewControls() const
Definition tool_base.cpp:40
KIGFX::VIEW * getView() const
Definition tool_base.cpp:34
Generic, UI-independent tool event.
Definition tool_event.h:167
bool DisableGridSnapping() const
Definition tool_event.h:367
bool HasParameter() const
Definition tool_event.h:460
bool IsCancelInteractive() const
Indicate the event should restart/end an ongoing interactive tool's event loop (eg esc key,...
TOOL_ACTIONS Action() const
Returns more specific information about the type of an event.
Definition tool_event.h:246
bool IsActivate() const
Definition tool_event.h:341
COMMIT * Commit() const
Definition tool_event.h:279
bool IsClick(int aButtonMask=BUT_ANY) const
TOOL_EVENT_CATEGORY Category() const
Return the category (eg. mouse/keyboard/action) of an event.
Definition tool_event.h:243
bool IsDrag(int aButtonMask=BUT_ANY) const
Definition tool_event.h:311
int Modifier(int aMask=MD_MODIFIER_MASK) const
Return information about key modifiers state (Ctrl, Alt, etc.).
Definition tool_event.h:362
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
T Parameter() const
Return a parameter assigned to the event.
Definition tool_event.h:469
bool IsDblClick(int aButtonMask=BUT_ANY) const
std::atomic< SYNCRONOUS_TOOL_STATE > * SynchronousState() const
Definition tool_event.h:276
std::optional< int > GetCommandId() const
Definition tool_event.h:529
void SetPassEvent(bool aPass=true)
Definition tool_event.h:252
bool IsMouseUp(int aButtonMask=BUT_ANY) const
Definition tool_event.h:321
bool IsMotion() const
Definition tool_event.h:326
const std::string Format() const
Return information about event in form of a human-readable string.
void Go(int(SCH_EDIT_FRAME::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
std::unique_ptr< TOOL_MENU > m_menu
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
for transforming drawing coordinates for a wxDC device context.
Definition transform.h:42
TRANSFORM InverseTransform() const
Calculate the Inverse mirror/rotation transform.
Definition transform.cpp:55
VECTOR2I TransformCoordinate(const VECTOR2I &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition transform.cpp:40
@ CHT_REMOVE
Definition commit.h:39
@ CHT_DONE
Flag to indicate the change is already applied.
Definition commit.h:43
@ CHT_ADD
Definition commit.h:38
KICURSOR
Definition cursors.h:40
@ PLACE
Definition cursors.h:94
@ MOVING
Definition cursors.h:44
#define _(s)
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:413
@ RECURSE
Definition eda_item.h:49
@ NO_RECURSE
Definition eda_item.h:50
#define IS_PASTED
Modifier on IS_NEW which indicates it came from clipboard.
#define IS_CHANGED
Item was edited, and modified.
#define BRIGHTENED
item is drawn with a bright contour
#define IS_NEW
New item, just created.
#define SELECTED
Item was manually selected by the user.
#define SELECTED_BY_DRAG
Item was algorithmically selected as a dragged item.
#define IS_BROKEN
Is a segment just broken by BreakSegment.
#define STRUCT_DELETED
flag indication structures to be erased
#define ENDPOINT
ends. (Used to support dragging.)
#define IS_MOVING
Item being moved.
#define STARTPOINT
When a line is selected, these flags indicate which.
@ NONE
Definition eda_shape.h:72
@ ID_POPUP_SCH_SELECT_UNIT
Definition eeschema_id.h:81
@ ID_POPUP_SCH_SELECT_BODY_STYLE
Definition eeschema_id.h:91
@ ID_POPUP_SCH_SELECT_BODY_STYLE_END
Definition eeschema_id.h:93
@ ID_POPUP_SCH_SELECT_UNIT_END
Definition eeschema_id.h:85
@ LINE_MODE_FREE
GRID_HELPER_GRIDS
Definition grid_helper.h:55
@ GRID_CURRENT
Definition grid_helper.h:57
const wxChar *const traceSchMove
Flag to watch how schematic move tool actions are handled.
@ LAYER_WIRE
Definition layer_ids.h:458
@ LAYER_BUS
Definition layer_ids.h:459
@ LAYER_BUS_JUNCTION
Definition layer_ids.h:504
std::vector< SCH_JUNCTION * > PreviewJunctions(const class SCH_SCREEN *aScreen, const std::vector< class SCH_ITEM * > &aItems)
Determine the points where explicit junctions would be required if the given temporary items were com...
POINT_INFO AnalyzePoint(const EE_RTREE &aItem, const VECTOR2I &aPosition, bool aBreakCrossings)
Check a tree of items for a confluence at a given point and work out what kind of junction it is,...
@ REPAINT
Item needs to be redrawn.
Definition view_item.h:54
bool signbit(T v)
Integral version of std::signbit that works all compilers.
Definition kicad_algo.h:172
@ VERTICAL
A segment (or two points) is vertical.
@ HORIZONTAL
A segment (or two points) is horizontal.
see class PGM_BASE
Class to handle a set of SCH_ITEMs.
@ AUTOPLACE_NONE
Definition sch_item.h:66
void MoveSchematicItem(EDA_ITEM *aItem, const VECTOR2I &aDelta)
Move a schematic item by a delta.
void AlignSchematicItemsToGrid(SCH_SCREEN *aScreen, const std::vector< EDA_ITEM * > &aItems, EE_GRID_HELPER &aGrid, GRID_HELPER_GRIDS aSelectionGrid, const SCH_ALIGNMENT_CALLBACKS &aCallbacks)
Align a set of schematic items to the grid.
std::vector< EDA_ITEM * > EDA_ITEMS
#define QUIET_MODE
static bool isGraphicItemForDrop(const SCH_ITEM *aItem)
static long long sheetBorderArc(const SCH_SHEET *aSheet, SHEET_SIDE aSide, const VECTOR2I &aPos)
static void sheetBorderPos(const SCH_SHEET *aSheet, long long aArc, VECTOR2I &aPos, SHEET_SIDE &aSide)
static void cloneWireConnection(SCH_LINE *aNewLine, SCH_ITEM *aSource, SCH_EDIT_FRAME *aFrame)
SHEET_SIDE
Define the edge of the sheet that the sheet pin is positioned.
T * GetAppSettings(const char *aFilename)
The EE_TYPE struct provides a type-specific auto-range iterator to the RTree.
Definition sch_rtree.h:171
A selection of information about a point in the schematic that might be eligible for turning into a j...
VECTOR2D m_lastKeyboardCursorPosition
Position of the above event.
bool m_lastKeyboardCursorPositionValid
Is last cursor motion event coming from keyboard arrow cursor motion action.
long m_lastKeyboardCursorCommand
ACTIONS::CURSOR_UP, ACTIONS::CURSOR_DOWN, etc.
Callbacks for alignment operations.
std::function< void(SCH_ITEM *aItem, const VECTOR2I &aPoint, EDA_ITEMS &aList)> m_getConnectedDragItems
Callback to get items connected to a given item at a specific point.
std::function< void(EDA_ITEM *aItem, const VECTOR2I &aDelta)> m_doMoveItem
Callback to move an item by a delta.
std::function< void(EDA_ITEM *aItem)> m_updateItem
Optional callback to update an item's display after modification.
bool moved
KIBIS top(path, &reporter)
KIBIS_PIN * pin
VECTOR2I end
int delta
@ TA_CHOICE_MENU_CHOICE
Context menu choice.
Definition tool_event.h:94
@ STS_CANCELLED
Definition tool_event.h:160
@ STS_FINISHED
Definition tool_event.h:159
@ STS_RUNNING
Definition tool_event.h:158
@ MD_CTRL
Definition tool_event.h:140
@ MD_SHIFT
Definition tool_event.h:139
@ TC_COMMAND
Definition tool_event.h:53
@ TC_MOUSE
Definition tool_event.h:51
@ TC_KEYBOARD
Definition tool_event.h:52
@ BUT_LEFT
Definition tool_event.h:128
@ BUT_RIGHT
Definition tool_event.h:129
wxLogTrace helper definitions.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:71
@ SCH_LINE_T
Definition typeinfo.h:160
@ SCH_NO_CONNECT_T
Definition typeinfo.h:157
@ SCH_SYMBOL_T
Definition typeinfo.h:169
@ SCH_FIELD_T
Definition typeinfo.h:147
@ SCH_DIRECTIVE_LABEL_T
Definition typeinfo.h:168
@ SCH_LABEL_T
Definition typeinfo.h:164
@ SCH_SHEET_T
Definition typeinfo.h:172
@ SCH_SHAPE_T
Definition typeinfo.h:146
@ SCH_HIER_LABEL_T
Definition typeinfo.h:166
@ SCH_BUS_BUS_ENTRY_T
Definition typeinfo.h:159
@ SCH_SHEET_PIN_T
Definition typeinfo.h:171
@ SCH_TEXT_T
Definition typeinfo.h:148
@ SCH_BUS_WIRE_ENTRY_T
Definition typeinfo.h:158
@ SCH_BITMAP_T
Definition typeinfo.h:161
@ SCH_TEXTBOX_T
Definition typeinfo.h:149
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:165
@ SCH_JUNCTION_T
Definition typeinfo.h:156
@ SCH_PIN_T
Definition typeinfo.h:150
constexpr int sign(T val)
Definition util.h:141
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682