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 SCOPED_TOOL_PUSHER raii( m_frame, aEvent );
744
745 // Note that it's important to go through push/pop even when the selection is empty.
746 // This keeps other tools from having to special-case an empty move.
747 if( selection.Empty() )
748 return false;
749
750 bool restore_state = false;
751 TOOL_EVENT copy = aEvent;
752 TOOL_EVENT* evt = &copy;
753 VECTOR2I prevPos = controls->GetCursorPosition();
755 SCH_SHEET* hoverSheet = nullptr;
756 KICURSOR currentCursor = KICURSOR::MOVING;
757 m_cursor = controls->GetCursorPosition();
758
759 // Axis locking for arrow key movement
760 enum class AXIS_LOCK { NONE, HORIZONTAL, VERTICAL };
761 AXIS_LOCK axisLock = AXIS_LOCK::NONE;
762 long lastArrowKeyAction = 0;
763
764 // Main loop: keep receiving events
765 do
766 {
767 wxLogTrace( traceSchMove, "doMoveSelection: event loop iteration, evt=%s, action=%s",
768 evt->Category() == TC_MOUSE ? "MOUSE" :
769 evt->Category() == TC_KEYBOARD ? "KEYBOARD" :
770 evt->Category() == TC_COMMAND ? "COMMAND" : "OTHER",
771 evt->Format().c_str() );
772
773 m_frame->GetCanvas()->SetCurrentCursor( currentCursor );
774 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
775
776 bool ctrlDown = evt->Modifier( MD_CTRL );
777
778 // Only real input events carry modifier state; the synthetic move action does not.
779 bool hasModifierState = evt->Category() == TC_MOUSE || evt->Category() == TC_KEYBOARD;
780
781 if( pasteHoldingCtrl && hasModifierState && !ctrlDown )
782 pasteHoldingCtrl = false;
783
784 // The paste-held Ctrl only masks grid snapping. Ctrl also forces a graphics-only drop
785 // into a sheet, and that gesture must still honor a physically-held key.
786 bool gridSnapDisabled = ctrlDown && !pasteHoldingCtrl;
787
788 grid.SetUseGrid( getView()->GetGAL()->GetGridSnapping() && !gridSnapDisabled );
789
790 lastCtrlDown = ctrlDown;
791
793 || evt->IsAction( &SCH_ACTIONS::move )
794 || evt->IsAction( &SCH_ACTIONS::drag )
795 || evt->IsMotion()
796 || evt->IsDrag( BUT_LEFT )
798 {
799 refreshTraits();
800
801 if( !m_moveInProgress ) // Prepare to start moving/dragging
802 {
803 initializeMoveOperation( aEvent, selection, aCommit, internalPoints, snapLayer );
804 prevPos = m_cursor;
805 refreshTraits();
806 }
807
808 //------------------------------------------------------------------------
809 // Follow the mouse
810 //
811 m_view->ClearPreview();
812
813 // We need to bypass refreshPreview action here because it is triggered by the move, so we were
814 // getting double-key events that toggled the axis locking if you pressed them in a certain order.
816 {
817 VECTOR2I keyboardPos( controls->GetSettings().m_lastKeyboardCursorPosition );
818 long action = controls->GetSettings().m_lastKeyboardCursorCommand;
819
820 grid.SetSnap( false );
821 m_cursor = grid.Align( keyboardPos, snapLayer );
822
823 // Update axis lock based on arrow key press
824 if( action == ACTIONS::CURSOR_LEFT || action == ACTIONS::CURSOR_RIGHT )
825 {
826 if( axisLock == AXIS_LOCK::HORIZONTAL )
827 {
828 // Check if opposite horizontal key pressed to unlock
829 if( ( lastArrowKeyAction == ACTIONS::CURSOR_LEFT && action == ACTIONS::CURSOR_RIGHT ) ||
830 ( lastArrowKeyAction == ACTIONS::CURSOR_RIGHT && action == ACTIONS::CURSOR_LEFT ) )
831 {
832 axisLock = AXIS_LOCK::NONE;
833 }
834 // Same direction axis, keep locked
835 }
836 else
837 {
838 axisLock = AXIS_LOCK::HORIZONTAL;
839 }
840 }
841 else if( action == ACTIONS::CURSOR_UP || action == ACTIONS::CURSOR_DOWN )
842 {
843 if( axisLock == AXIS_LOCK::VERTICAL )
844 {
845 // Check if opposite vertical key pressed to unlock
846 if( ( lastArrowKeyAction == ACTIONS::CURSOR_UP && action == ACTIONS::CURSOR_DOWN ) ||
847 ( lastArrowKeyAction == ACTIONS::CURSOR_DOWN && action == ACTIONS::CURSOR_UP ) )
848 {
849 axisLock = AXIS_LOCK::NONE;
850 }
851 // Same direction axis, keep locked
852 }
853 else
854 {
855 axisLock = AXIS_LOCK::VERTICAL;
856 }
857 }
858
859 lastArrowKeyAction = action;
860 }
861 else
862 {
863 m_cursor = grid.ResolveSnap( controls->GetCursorPosition( false ), snapLayer, selection, prevPos )
864 .position;
865 }
866
867 if( axisLock == AXIS_LOCK::HORIZONTAL )
868 m_cursor.y = prevPos.y;
869 else if( axisLock == AXIS_LOCK::VERTICAL )
870 m_cursor.x = prevPos.x;
871
872 // Find potential target sheet for dropping. This relocation is only meaningful for a
873 // plain move; drag/break/slice reshape existing connections in place and must never
874 // pull items onto a sub-sheet's screen.
875 SCH_SHEET* sheet = nullptr;
876
877 if( m_mode == MOVE )
878 sheet = findTargetSheet( selection, m_cursor, selectionHasSheetPins, selectionIsGraphicsOnly,
879 ctrlDown );
880
881 if( sheet != hoverSheet )
882 {
883 if( hoverSheet )
884 {
885 hoverSheet->ClearFlags( BRIGHTENED );
886 m_frame->UpdateItem( hoverSheet, false );
887 }
888
889 hoverSheet = sheet;
890
891 if( hoverSheet )
892 {
893 hoverSheet->SetFlags( BRIGHTENED );
894 m_frame->UpdateItem( hoverSheet, false );
895 }
896 }
897
898 currentCursor = hoverSheet ? KICURSOR::PLACE : KICURSOR::MOVING;
899
900 if( netCollisionMonitor )
901 currentCursor = netCollisionMonitor->AdjustCursor( currentCursor );
902
903 VECTOR2I delta( m_cursor - prevPos );
905
906 // Used for tracking how far off a drag end should have its 90 degree elbow added
907 int xBendCount = 1;
908 int yBendCount = 1;
909
910 performItemMove( selection, delta, aCommit, xBendCount, yBendCount, grid );
911 prevPos = m_cursor;
912
913 std::vector<SCH_ITEM*> previewItems;
914
915 for( EDA_ITEM* it : selection )
916 previewItems.push_back( static_cast<SCH_ITEM*>( it ) );
917
918 for( SCH_LINE* line : m_newDragLines )
919 previewItems.push_back( line );
920
921 for( SCH_LINE* line : m_changedDragLines )
922 previewItems.push_back( line );
923
924 std::vector<SCH_JUNCTION*> previewJunctions =
925 JUNCTION_HELPERS::PreviewJunctions( m_frame->GetScreen(), previewItems );
926
927 if( netCollisionMonitor )
928 netCollisionMonitor->Update( previewJunctions, selection );
929
930 for( SCH_JUNCTION* jct : previewJunctions )
931 m_view->AddToPreview( jct, true );
932
934 }
935
936 //------------------------------------------------------------------------
937 // Handle cancel
938 //
939 else if( evt->IsCancelInteractive()
940 || evt->IsActivate()
941 || evt->IsAction( &ACTIONS::undo ) )
942 {
943 if( evt->IsCancelInteractive() )
944 {
945 m_frame->GetInfoBar()->Dismiss();
946
947 // When breaking, the user can cancel after multiple breaks to keep all but the last
948 // break, so exit normally if we have done at least one break
949 if( didAtLeastOneBreak && m_mode == BREAK )
950 break;
951 }
952
953 if( m_moveInProgress )
954 {
955 if( evt->IsActivate() )
956 {
957 // Allowing other tools to activate during a move runs the risk of race
958 // conditions in which we try to spool up both event loops at once.
959
960 switch( m_mode )
961 {
962 case MOVE: m_frame->ShowInfoBarMsg( _( "Press <ESC> to cancel move." ) ); break;
963 case DRAG: m_frame->ShowInfoBarMsg( _( "Press <ESC> to cancel drag." ) ); break;
964 case BREAK: m_frame->ShowInfoBarMsg( _( "Press <ESC> to cancel break." ) ); break;
965 case SLICE: m_frame->ShowInfoBarMsg( _( "Press <ESC> to cancel slice." ) ); break;
966 }
967
968 evt->SetPassEvent( false );
969 continue;
970 }
971
972 evt->SetPassEvent( false );
973 restore_state = true;
974 }
975 else if( m_mode == BREAK || m_mode == SLICE )
976 {
977 // preprocessBreakOrSliceSelection() split the wire before any motion arrived,
978 // so cancel must roll those edits back. Activations still pass through so the
979 // requested tool starts.
980 if( !evt->IsActivate() )
981 evt->SetPassEvent( false );
982
983 restore_state = true;
984 }
985
987
988 m_view->ClearPreview();
989
990 break;
991 }
992 //------------------------------------------------------------------------
993 // Handle TOOL_ACTION special cases
994 //
995 else if( !handleMoveToolActions( evt, aCommit, selection ) )
996 {
997 wxLogTrace( traceSchMove, "doMoveSelection: handleMoveToolActions returned false, exiting" );
998 break; // Exit if told to by handler
999 }
1000 //------------------------------------------------------------------------
1001 // Handle context menu
1002 //
1003 else if( evt->IsClick( BUT_RIGHT ) )
1004 {
1005 m_menu->ShowContextMenu( m_selectionTool->GetSelection() );
1006 }
1007 //------------------------------------------------------------------------
1008 // Handle drop
1009 //
1010 else if( evt->IsMouseUp( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) )
1011 {
1012 if( m_mode != BREAK )
1013 {
1014 break; // Finish
1015 }
1016 else
1017 {
1018 didAtLeastOneBreak = true;
1019 preprocessBreakOrSliceSelection( aCommit, *evt );
1020 selection = m_selectionTool->RequestSelection( SCH_COLLECTOR::MovableItems, true );
1021
1022 if( m_breakPos )
1023 {
1026 selection.SetReferencePoint( m_cursor );
1027 m_moveOffset = VECTOR2I( 0, 0 );
1028 m_breakPos.reset();
1029
1030 controls->SetCursorPosition( m_cursor, false );
1031 prevPos = m_cursor;
1032 }
1033 }
1034 }
1035 else if( evt->IsDblClick( BUT_LEFT ) )
1036 {
1037 // Double click always finishes, even breaks
1038 break;
1039 }
1040 // Don't call SetPassEvent() for events we've handled - let them be consumed
1041 else if( evt->IsAction( &SCH_ACTIONS::rotateCW )
1043 || evt->IsAction( &ACTIONS::increment )
1047 || evt->IsAction( &SCH_ACTIONS::toLabel )
1048 || evt->IsAction( &SCH_ACTIONS::toText )
1052 || evt->IsAction( &ACTIONS::duplicate )
1054 || evt->IsAction( &ACTIONS::redo ) )
1055 {
1056 // Event was already handled by handleMoveToolActions, don't pass it on
1057 wxLogTrace( traceSchMove, "doMoveSelection: event handled, not passing" );
1058 }
1059 else
1060 {
1061 evt->SetPassEvent();
1062 }
1063
1064 controls->SetAutoPan( m_moveInProgress );
1065
1066 } while( ( evt = Wait() ) ); //Should be assignment not equality test
1067
1068 SCH_SHEET* targetSheet = hoverSheet;
1069
1070 if( selectionHasSheetPins || ( selectionIsGraphicsOnly && !lastCtrlDown ) )
1071 targetSheet = nullptr;
1072
1073 if( hoverSheet )
1074 {
1075 hoverSheet->ClearFlags( BRIGHTENED );
1076 m_frame->UpdateItem( hoverSheet, false );
1077 }
1078
1079 if( restore_state )
1080 {
1081 for( const HIDDEN_JUNCTION& hidden : m_hiddenJunctions )
1082 m_view->Hide( hidden.m_junction, false );
1083
1084 m_selectionTool->RemoveItemsFromSel( &m_dragAdditions, QUIET_MODE );
1085
1086 // Clear the split-segment selection that preprocessBreakOrSliceSelection() built
1087 // before the caller's Revert() runs. Revert() rebuilds selection from the screen,
1088 // so leaving the splits selected keeps the restored wire hidden until the next
1089 // selection refresh.
1090 if( m_mode == BREAK || m_mode == SLICE )
1091 m_toolMgr->RunAction( ACTIONS::selectionClear );
1092 }
1093 else
1094 {
1095 // Only drop into a sheet when the move is committed, not when canceled.
1096 if( targetSheet )
1097 {
1098 moveSelectionToSheet( selection, targetSheet, aCommit );
1099 m_toolMgr->RunAction( ACTIONS::selectionClear );
1100 m_newDragLines.clear();
1101 m_changedDragLines.clear();
1102 }
1103
1104 finalizeMoveOperation( selection, aCommit, unselect, internalPoints );
1105 }
1106
1107 m_dragAdditions.clear();
1108 m_lineConnectionCache.clear();
1109 m_moveInProgress = false;
1110 m_breakPos.reset();
1111
1112 m_hiddenJunctions.clear();
1113 m_view->ClearPreview();
1114
1115 return !restore_state;
1116}
1117
1118
1119bool SCH_MOVE_TOOL::checkMoveInProgress( const TOOL_EVENT& aEvent, SCH_COMMIT* aCommit, bool aCurrentModeIsDragLike,
1120 bool aWasDragging )
1121{
1123
1124 if( !m_moveInProgress )
1125 return false;
1126
1127 if( aCurrentModeIsDragLike != aWasDragging )
1128 {
1129 EDA_ITEM* sel = m_selectionTool->GetSelection().Front();
1130
1131 if( sel && !sel->IsNew() )
1132 {
1133 // Reset the selected items so we can start again with the current drag mode state
1134 aCommit->Revert();
1135
1136 m_selectionTool->RemoveItemsFromSel( &m_dragAdditions, QUIET_MODE );
1138 m_moveInProgress = false;
1139 controls->SetAutoPan( false );
1140
1141 // Give it a kick so it doesn't have to wait for the first mouse movement to refresh
1142 m_toolMgr->PostAction( SCH_ACTIONS::restartMove );
1143 }
1144 }
1145 else
1146 {
1147 // The tool hotkey is interpreted as a click when already dragging/moving
1148 m_toolMgr->PostAction( ACTIONS::cursorClick );
1149 }
1150
1151 return true;
1152}
1153
1154
1156{
1157 SCH_SELECTION& userSelection = m_selectionTool->GetSelection();
1158
1159 // If a single pin is selected, promote the move selection to its parent symbol
1160 if( userSelection.GetSize() == 1 )
1161 {
1162 EDA_ITEM* selItem = userSelection.Front();
1163
1164 if( selItem->Type() == SCH_PIN_T )
1165 {
1166 EDA_ITEM* parent = selItem->GetParent();
1167
1168 if( parent->Type() == SCH_SYMBOL_T )
1169 {
1170 m_selectionTool->ClearSelection();
1171 m_selectionTool->AddItemToSel( parent );
1172 }
1173 }
1174 }
1175
1176 // Be sure that there is at least one item that we can move. If there's no selection try
1177 // looking for the stuff under mouse cursor (i.e. KiCad old-style hover selection).
1178 SCH_SELECTION& selection = m_selectionTool->RequestSelection( SCH_COLLECTOR::MovableItems, true );
1179 aUnselect = selection.IsHover();
1180
1181 m_selectionTool->FilterSelectionForLockedItems();
1182
1183 return selection;
1184}
1185
1186
1187void SCH_MOVE_TOOL::refreshSelectionTraits( const SCH_SELECTION& aSelection, bool& aHasSheetPins,
1188 bool& aHasGraphicItems, bool& aHasNonGraphicItems,
1189 bool& aIsGraphicsOnly )
1190{
1191 aHasSheetPins = false;
1192 aHasGraphicItems = false;
1193 aHasNonGraphicItems = false;
1194
1195 for( EDA_ITEM* edaItem : aSelection )
1196 {
1197 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( edaItem );
1198
1199 if( schItem->Type() == SCH_SHEET_PIN_T )
1200 aHasSheetPins = true;
1201
1202 if( isGraphicItemForDrop( schItem ) )
1203 aHasGraphicItems = true;
1204 else if( schItem->Type() != SCH_SHEET_T )
1205 aHasNonGraphicItems = true;
1206 }
1207
1208 aIsGraphicsOnly = aHasGraphicItems && !aHasNonGraphicItems;
1209}
1210
1211
1213{
1214 // Drag of split items start over top of their other segment, so we want to skip grabbing
1215 // the segments we split from
1216 if( m_mode != DRAG && m_mode != BREAK )
1217 return;
1218
1219 EDA_ITEMS connectedDragItems;
1220
1221 // Add connections to the selection for a drag.
1222 // Do all non-labels/entries first so we don't add junctions to drag when the line will
1223 // eventually be drag selected.
1224 std::vector<SCH_ITEM*> stageTwo;
1225
1226 for( EDA_ITEM* edaItem : aSelection )
1227 {
1228 SCH_ITEM* item = static_cast<SCH_ITEM*>( edaItem );
1229 std::vector<VECTOR2I> connections;
1230
1231 switch( item->Type() )
1232 {
1233 case SCH_LABEL_T:
1234 case SCH_HIER_LABEL_T:
1235 case SCH_GLOBAL_LABEL_T:
1237 stageTwo.emplace_back( item );
1238 break;
1239
1240 case SCH_LINE_T:
1241 static_cast<SCH_LINE*>( item )->GetSelectedPoints( connections );
1242 break;
1243
1244 default:
1245 connections = item->GetConnectionPoints();
1246 }
1247
1248 for( const VECTOR2I& point : connections )
1249 getConnectedDragItems( aCommit, item, point, connectedDragItems );
1250 }
1251
1252 // Go back and get all label connections now that we can test for drag-selected lines
1253 // the labels might be on
1254 for( SCH_ITEM* item : stageTwo )
1255 {
1256 for( const VECTOR2I& point : item->GetConnectionPoints() )
1257 getConnectedDragItems( aCommit, item, point, connectedDragItems );
1258 }
1259
1260 for( EDA_ITEM* item : connectedDragItems )
1261 {
1262 m_dragAdditions.push_back( item->m_Uuid );
1263 m_selectionTool->AddItemToSel( item, QUIET_MODE );
1264 }
1265
1266 // Pre-cache all connections of our selected objects so we can keep track of what they
1267 // were originally connected to as we drag them around
1268 for( EDA_ITEM* edaItem : aSelection )
1269 {
1270 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( edaItem );
1271
1272 if( schItem->Type() == SCH_LINE_T )
1273 {
1274 SCH_LINE* line = static_cast<SCH_LINE*>( schItem );
1275
1276 // Store the original angle of the line; needed later to decide which segment
1277 // to extend when they've become zero length
1278 line->StoreAngle();
1279
1280 for( const VECTOR2I& point : line->GetConnectionPoints() )
1281 getConnectedItems( line, point, m_lineConnectionCache[line] );
1282 }
1283 }
1284}
1285
1286
1287void SCH_MOVE_TOOL::setupItemsForMove( SCH_SELECTION& aSelection, std::vector<DANGLING_END_ITEM>& aInternalPoints )
1288{
1289 // Mark the edges of the block with dangling flags for a move
1290 for( EDA_ITEM* item : aSelection )
1291 static_cast<SCH_ITEM*>( item )->GetEndPoints( aInternalPoints );
1292
1293 std::vector<DANGLING_END_ITEM> endPointsByType = aInternalPoints;
1294 std::vector<DANGLING_END_ITEM> endPointsByPos = endPointsByType;
1295 DANGLING_END_ITEM_HELPER::sort_dangling_end_items( endPointsByType, endPointsByPos );
1296
1297 for( EDA_ITEM* item : aSelection )
1298 static_cast<SCH_ITEM*>( item )->UpdateDanglingState( endPointsByType, endPointsByPos );
1299}
1300
1301
1303 std::vector<DANGLING_END_ITEM>& aInternalPoints,
1304 GRID_HELPER_GRIDS& aSnapLayer )
1305{
1308 SCH_ITEM* sch_item = static_cast<SCH_ITEM*>( aSelection.Front() );
1309 bool placingNewItems = sch_item && sch_item->IsNew();
1310
1311 //------------------------------------------------------------------------
1312 // Setup a drag or a move
1313 //
1314 m_dragAdditions.clear();
1315 m_specialCaseLabels.clear();
1316 m_specialCaseSheetPins.clear();
1317 m_sheetPinDragArc.clear();
1318 aInternalPoints.clear();
1320
1321 for( SCH_ITEM* it : m_frame->GetScreen()->Items() )
1322 {
1323 it->ClearFlags( SELECTED_BY_DRAG );
1324
1325 if( !it->IsSelected() )
1326 it->ClearFlags( STARTPOINT | ENDPOINT );
1327 }
1328
1329 setupItemsForDrag( aSelection, aCommit );
1330 setupItemsForMove( aSelection, aInternalPoints );
1331
1332 recordRedundantJunctions( aSelection );
1333
1334 // Generic setup
1335 aSnapLayer = grid.GetSelectionGrid( aSelection );
1336
1337 for( EDA_ITEM* item : aSelection )
1338 {
1339 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( item );
1340
1341 if( schItem->IsNew() )
1342 {
1343 // Item was added to commit in a previous command
1344
1345 // While SCH_COMMIT::Push() will add any new items to the entered group, we need
1346 // to do it earlier so that the previews while moving are correct.
1347 if( SCH_GROUP* enteredGroup = m_selectionTool->GetEnteredGroup() )
1348 {
1349 if( schItem->IsGroupableType() && !schItem->GetParentGroup() )
1350 {
1351 aCommit->Modify( enteredGroup, m_frame->GetScreen(), RECURSE_MODE::NO_RECURSE );
1352 enteredGroup->AddItem( schItem );
1353 }
1354 }
1355 }
1356 else if( schItem->GetParent() && schItem->GetParent()->IsSelected() )
1357 {
1358 // Item will be (or has been) added to commit by parent
1359 }
1360 else
1361 {
1362 aCommit->Modify( schItem, m_frame->GetScreen(), RECURSE_MODE::RECURSE );
1363 }
1364
1365 schItem->SetFlags( IS_MOVING );
1366
1367 if( SCH_SHAPE* shape = dynamic_cast<SCH_SHAPE*>( schItem ) )
1368 {
1369 shape->SetHatchingDirty();
1370 shape->UpdateHatching();
1371 }
1372
1373 schItem->RunOnChildren(
1374 [&]( SCH_ITEM* aChild )
1375 {
1376 aChild->SetFlags( IS_MOVING );
1377 },
1379
1380 schItem->SetStoredPos( schItem->GetPosition() );
1381
1382 if( schItem->Type() == SCH_SHEET_PIN_T && schItem->GetParent() && !schItem->GetParent()->IsSelected() )
1383 {
1384 SCH_SHEET_PIN* pin = static_cast<SCH_SHEET_PIN*>( schItem );
1385 m_sheetPinDragArc[pin] = sheetBorderArc( pin->GetParent(), pin->GetSide(), pin->GetPosition() );
1386 }
1387 }
1388
1389 // Set up the starting position and move/drag offset
1390 m_cursor = controls->GetCursorPosition();
1391
1392 if( m_mode == BREAK && m_breakPos )
1393 {
1396 aSelection.SetReferencePoint( m_cursor );
1397 m_moveOffset = VECTOR2I( 0, 0 );
1398 m_breakPos.reset();
1399 }
1400
1401 if( aEvent.IsAction( &SCH_ACTIONS::restartMove ) )
1402 {
1403 wxASSERT_MSG( m_anchorPos, "Should be already set from previous cmd" );
1404 }
1405 else if( placingNewItems )
1406 {
1407 m_anchorPos = aSelection.GetReferencePoint();
1408 }
1409
1410 if( m_anchorPos )
1411 {
1412 VECTOR2I delta = m_cursor - ( *m_anchorPos );
1413 bool isPasted = false;
1414
1415 // Drag items to the current cursor position
1416 for( EDA_ITEM* item : aSelection )
1417 {
1418 // Don't double move pins, fields, etc.
1419 if( item->GetParent() && item->GetParent()->IsSelected() )
1420 continue;
1421
1422 moveItem( item, delta );
1423 updateItem( item, false );
1424
1425 isPasted |= ( item->GetFlags() & IS_PASTED ) != 0;
1426 }
1427
1428 // The first time pasted items are moved we need to store the position of the cursor
1429 // so that rotate while moving works as expected (instead of around the original
1430 // anchor point)
1431 if( isPasted )
1432 aSelection.SetReferencePoint( m_cursor );
1433
1435 }
1436 // For some items, moving the cursor to anchor is not good (for instance large
1437 // hierarchical sheets or symbols can have the anchor outside the view)
1438 else if( aSelection.Size() == 1 && !sch_item->IsMovableFromAnchorPoint() )
1439 {
1442 }
1443 else
1444 {
1445 if( m_frame->GetMoveWarpsCursor() )
1446 {
1447 // User wants to warp the mouse
1448 m_cursor = grid.BestDragOrigin( m_cursor, aSnapLayer, aSelection );
1449 aSelection.SetReferencePoint( m_cursor );
1450 }
1451 else
1452 {
1453 // User does not want to warp the mouse
1455 }
1456 }
1457
1458 controls->SetCursorPosition( m_cursor, false );
1459 controls->SetAutoPan( true );
1460 m_moveInProgress = true;
1461}
1462
1463
1465 bool aHasSheetPins, bool aIsGraphicsOnly, bool aCtrlDown )
1466{
1467 // Fields are children of their parent item and must not be dropped into a sheet
1468 for( EDA_ITEM* it : aSelection )
1469 {
1470 if( it->Type() == SCH_FIELD_T )
1471 return nullptr;
1472 }
1473
1474 // Determine potential target sheet
1475 SCH_SHEET* sheet = dynamic_cast<SCH_SHEET*>( m_frame->GetScreen()->GetItem( aCursorPos, 0, SCH_SHEET_T ) );
1476
1477 if( sheet && ( sheet->IsSelected() || sheet->HasFlag( IS_MOVING ) ) )
1478 sheet = nullptr; // Never target a selected sheet
1479
1480 if( !sheet )
1481 {
1482 // Build current selection bounding box in its (already moved) position
1483 BOX2I selBBox;
1484
1485 for( EDA_ITEM* it : aSelection )
1486 {
1487 if( SCH_ITEM* schIt = dynamic_cast<SCH_ITEM*>( it ) )
1488 selBBox.Merge( schIt->GetBoundingBox() );
1489 }
1490
1491 if( selBBox.GetWidth() > 0 && selBBox.GetHeight() > 0 )
1492 {
1493 VECTOR2I selCenter( selBBox.GetX() + selBBox.GetWidth() / 2,
1494 selBBox.GetY() + selBBox.GetHeight() / 2 );
1495
1496 // Find first non-selected sheet whose body fully contains the selection or at
1497 // least contains its center point
1498 for( SCH_ITEM* it : m_frame->GetScreen()->Items().OfType( SCH_SHEET_T ) )
1499 {
1500 SCH_SHEET* candidate = static_cast<SCH_SHEET*>( it );
1501
1502 if( candidate->IsSelected() || candidate->IsTopLevelSheet() || candidate->HasFlag( IS_MOVING ) )
1503 continue;
1504
1505 BOX2I body = candidate->GetBodyBoundingBox();
1506
1507 if( body.Contains( selBBox ) || body.Contains( selCenter ) )
1508 {
1509 sheet = candidate;
1510 break;
1511 }
1512 }
1513 }
1514 }
1515
1516 // Don't drop into a sheet if any connection point of the selection lands on a sheet pin.
1517 // This indicates the user is trying to connect to the pin, not drop into the sheet.
1518 if( sheet )
1519 {
1520 for( EDA_ITEM* it : aSelection )
1521 {
1522 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( it );
1523
1524 if( !schItem )
1525 continue;
1526
1527 for( const VECTOR2I& pt : schItem->GetConnectionPoints() )
1528 {
1529 if( sheet->GetPin( pt ) )
1530 {
1531 sheet = nullptr;
1532 break;
1533 }
1534 }
1535
1536 if( !sheet )
1537 break;
1538 }
1539 }
1540
1541 if( sheet && dropWouldRecurse( aSelection, sheet ) )
1542 sheet = nullptr;
1543
1544 bool dropAllowedBySelection = !aHasSheetPins;
1545 bool dropAllowedByModifiers = !aIsGraphicsOnly || aCtrlDown;
1546
1547 if( sheet && !( dropAllowedBySelection && dropAllowedByModifiers ) )
1548 sheet = nullptr;
1549
1550 return sheet;
1551}
1552
1553
1554bool SCH_MOVE_TOOL::dropWouldRecurse( const SCH_SELECTION& aSelection, const SCH_SHEET* aTargetSheet )
1555{
1556 SCH_SCREEN* destScreen = aTargetSheet->GetScreen();
1557
1558 if( !destScreen || destScreen->GetFileName().IsEmpty() )
1559 return false;
1560
1561 std::vector<SCH_SHEET*> movedSheets;
1562
1563 for( EDA_ITEM* item : aSelection )
1564 {
1565 if( item->Type() == SCH_SHEET_T )
1566 movedSheets.push_back( static_cast<SCH_SHEET*>( item ) );
1567 }
1568
1569 if( movedSheets.empty() )
1570 return false;
1571
1572 SCH_SHEET_LIST hierarchy = m_frame->Schematic().Hierarchy();
1573
1574 for( SCH_SHEET* movedSheet : movedSheets )
1575 {
1576 SCH_SHEET_LIST movedHierarchy( movedSheet );
1577
1578 if( hierarchy.TestForRecursion( movedHierarchy, destScreen->GetFileName() ) )
1579 return true;
1580 }
1581
1582 return false;
1583}
1584
1585
1587 SCH_COMMIT* aCommit, int& aXBendCount, int& aYBendCount,
1588 const EE_GRID_HELPER& aGrid )
1589{
1590 wxLogTrace( traceSchMove, "performItemMove: delta=(%d,%d), moveOffset=(%d,%d), selection size=%u",
1591 aDelta.x, aDelta.y, m_moveOffset.x, m_moveOffset.y, aSelection.GetSize() );
1592
1593 // We need to check if the movement will change the net offset direction on the X and Y
1594 // axes. This is because we remerge added bend lines in realtime, and we also account for
1595 // the direction of the move when adding bend lines. So, if the move direction changes,
1596 // we need to split it into a move that gets us back to zero, then the rest of the move.
1597 std::vector<VECTOR2I> splitMoves;
1598
1599 if( alg::signbit( m_moveOffset.x ) != alg::signbit( ( m_moveOffset + aDelta ).x ) )
1600 {
1601 splitMoves.emplace_back( VECTOR2I( -1 * m_moveOffset.x, 0 ) );
1602 splitMoves.emplace_back( VECTOR2I( aDelta.x + m_moveOffset.x, 0 ) );
1603 }
1604 else
1605 {
1606 splitMoves.emplace_back( VECTOR2I( aDelta.x, 0 ) );
1607 }
1608
1609 if( alg::signbit( m_moveOffset.y ) != alg::signbit( ( m_moveOffset + aDelta ).y ) )
1610 {
1611 splitMoves.emplace_back( VECTOR2I( 0, -1 * m_moveOffset.y ) );
1612 splitMoves.emplace_back( VECTOR2I( 0, aDelta.y + m_moveOffset.y ) );
1613 }
1614 else
1615 {
1616 splitMoves.emplace_back( VECTOR2I( 0, aDelta.y ) );
1617 }
1618
1619 m_moveOffset += aDelta;
1620
1621 // Split the move into X and Y moves so we can correctly drag orthogonal lines
1622 for( const VECTOR2I& splitDelta : splitMoves )
1623 {
1624 // Skip non-moves
1625 if( splitDelta == VECTOR2I( 0, 0 ) )
1626 continue;
1627
1628 for( EDA_ITEM* item : aSelection.GetItemsSortedByTypeAndXY( ( aDelta.x >= 0 ),
1629 ( aDelta.y >= 0 ) ) )
1630 {
1631 // Don't double move pins, fields, etc.
1632 if( item->GetParent() && item->GetParent()->IsSelected() )
1633 continue;
1634
1635 SCH_LINE* line = dynamic_cast<SCH_LINE*>( item );
1636 bool isLineModeConstrained = false;
1637
1638 if( EESCHEMA_SETTINGS* cfg = GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" ) )
1639 isLineModeConstrained = cfg->m_Drawing.line_mode != LINE_MODE::LINE_MODE_FREE;
1640
1641 // Only partially selected drag lines in orthogonal line mode need special handling.
1642 // Skip newly-created connectivity wires added to maintain connectivity at junctions:
1643 // these are marked with both IS_NEW and SELECTED_BY_DRAG; they already have the
1644 // correct endpoint constraint and don't need orthogonal bending
1645 if( ( m_mode == DRAG ) && isLineModeConstrained && line
1646 && line->HasFlag( STARTPOINT ) != line->HasFlag( ENDPOINT )
1647 && !line->HasFlag( SELECTED_BY_DRAG | IS_NEW ) )
1648 {
1649 orthoLineDrag( aCommit, line, splitDelta, aXBendCount, aYBendCount, aGrid );
1650 }
1651
1652 // Move all other items normally, including the selected end of partially selected
1653 // lines
1654 moveItem( item, splitDelta );
1655 updateItem( item, false );
1656
1657 // Update any lines connected to sheet pins to the sheet pin's location (which may
1658 // not exactly follow the splitDelta as the pins are constrained along the sheet
1659 // edges)
1660 for( const auto& [pin, lineEnd] : m_specialCaseSheetPins )
1661 {
1662 if( lineEnd.second && lineEnd.first->HasFlag( STARTPOINT ) )
1663 lineEnd.first->SetStartPoint( pin->GetPosition() );
1664 else if( !lineEnd.second && lineEnd.first->HasFlag( ENDPOINT ) )
1665 lineEnd.first->SetEndPoint( pin->GetPosition() );
1666 }
1667 }
1668
1669 // Needed to keep labels attached to a line when dragging a sheet/wire combo with a label
1670 // on the line. The label moves by splitDelta for each part of the split move, but the
1671 // line endpoints may not follow splitDelta due to orthogonal drag or sheet pin constraints,
1672 // which can put the label off the line.
1673 for( auto& [label, info] : m_specialCaseLabels )
1674 {
1675 if( !label || !info.attachedLine )
1676 continue;
1677
1678 if( info.trackMovingEnd )
1679 {
1680 label->Move( splitDelta );
1681
1682 VECTOR2I start = info.attachedLine->GetStartPoint();
1683 VECTOR2I end = info.attachedLine->GetEndPoint();
1684
1685 if( info.attachedLine->GetLength() > 0
1686 && info.attachedLine->HitTest( info.originalLabelPos, 1 )
1687 && info.originalLabelPos != start
1688 && info.originalLabelPos != end )
1689 {
1690 info.trackMovingEnd = false;
1691 label->SetPosition( info.originalLabelPos );
1692 info.originalLineStart = start;
1693 info.originalLineEnd = end;
1694 }
1695
1696 updateItem( label, false );
1697 continue;
1698 }
1699
1700 VECTOR2I start = info.attachedLine->GetStartPoint();
1701 VECTOR2I end = info.attachedLine->GetEndPoint();
1702 VECTOR2I deltaStart = start - info.originalLineStart;
1703 VECTOR2I deltaEnd = end - info.originalLineEnd;
1704
1705 // TODO: this could be improved by positioning the label based on the new line geometry,
1706 // bends are involved.
1707 //
1708 // For now, special casing the equal delta case and using splitDelta should work in most
1709 // cases as the user would expect.
1710 if( deltaStart == deltaEnd )
1711 {
1712 label->SetPosition( info.originalLabelPos + deltaStart );
1713 }
1714 else
1715 {
1716 bool startDrags = info.attachedLine->HasFlag( STARTPOINT );
1717 VECTOR2I fixedEndDelta = startDrags ? deltaEnd : deltaStart;
1718
1719 label->SetPosition( info.originalLabelPos + fixedEndDelta );
1720
1721 // If the line shrank while dragging, keep the label on the line,
1722 // otherwise the label can drift off the end of the line, and change connectivity
1723 if( !info.attachedLine->HitTest( label->GetPosition(), 1 ) )
1724 {
1725 SEG seg( start, end );
1726 label->SetPosition( seg.NearestPoint( label->GetPosition() ) );
1727
1728 VECTOR2I movingEnd = startDrags ? start : end;
1729
1730 if( label->GetPosition() == movingEnd )
1731 info.trackMovingEnd = true;
1732 }
1733 }
1734
1735 updateItem( label, false );
1736 }
1737 }
1738
1739 spreadMovingSheetPinGroups( aSelection );
1740
1741 if( aSelection.HasReferencePoint() )
1742 aSelection.SetReferencePoint( aSelection.GetReferencePoint() + aDelta );
1743}
1744
1745
1747{
1748 // Slide pins dragged together by one distance along the border, so they keep their spacing
1749 // and wrap around corners instead of collapsing onto a shared point on a perpendicular edge.
1750 std::map<SCH_SHEET*, std::vector<SCH_SHEET_PIN*>> groups;
1751
1752 for( EDA_ITEM* item : aSelection )
1753 {
1754 if( item->Type() != SCH_SHEET_PIN_T )
1755 continue;
1756
1757 SCH_SHEET_PIN* pin = static_cast<SCH_SHEET_PIN*>( item );
1758
1759 if( SCH_SHEET* sheet = pin->GetParent(); sheet && !sheet->IsSelected() && m_sheetPinDragArc.count( pin ) )
1760 {
1761 groups[sheet].push_back( pin );
1762 }
1763 }
1764
1765 for( auto& [sheet, pins] : groups )
1766 {
1767 if( pins.size() < 2 )
1768 continue;
1769
1770 // Only slide as a group when the pins started on the same edge. A mix of edges would
1771 // move in opposite directions (the border runs one way), so leave those to the normal
1772 // per-pin constraint.
1773 auto startSide = [&]( SCH_SHEET_PIN* aPin )
1774 {
1775 VECTOR2I pos;
1776 SHEET_SIDE side;
1777 sheetBorderPos( sheet, m_sheetPinDragArc[aPin], pos, side );
1778 return side;
1779 };
1780
1781 SCH_SHEET_PIN* ref = pins.front();
1782 SHEET_SIDE refStartSide = startSide( ref );
1783 bool sameEdge = true;
1784
1785 for( SCH_SHEET_PIN* pin : pins )
1786 sameEdge &= ( startSide( pin ) == refStartSide );
1787
1788 if( !sameEdge )
1789 continue;
1790
1791 // The reference pin is already on its edge, its border travel drives the group slide.
1792 long long refArc = sheetBorderArc( sheet, ref->GetSide(), ref->GetPosition() );
1793 long long slide = refArc - m_sheetPinDragArc[ref];
1794
1795 for( SCH_SHEET_PIN* pin : pins )
1796 {
1797 VECTOR2I pos;
1798 SHEET_SIDE side;
1799 sheetBorderPos( sheet, m_sheetPinDragArc[pin] + slide, pos, side );
1800
1801 pin->SetSide( side );
1802
1803 if( side == SHEET_SIDE::LEFT || side == SHEET_SIDE::RIGHT )
1804 pin->SetTextY( pos.y );
1805 else
1806 pin->SetTextX( pos.x );
1807
1808 updateItem( pin, false );
1809 }
1810 }
1811
1812 // Pull attached lines back to the moved pins.
1813 for( const auto& [pin, lineEnd] : m_specialCaseSheetPins )
1814 {
1815 if( lineEnd.second && lineEnd.first->HasFlag( STARTPOINT ) )
1816 lineEnd.first->SetStartPoint( pin->GetPosition() );
1817 else if( !lineEnd.second && lineEnd.first->HasFlag( ENDPOINT ) )
1818 lineEnd.first->SetEndPoint( pin->GetPosition() );
1819 }
1820}
1821
1822
1824 const SCH_SELECTION& aSelection )
1825{
1826 wxLogTrace( traceSchMove, "handleMoveToolActions: received event, action=%s",
1827 aEvent->Format().c_str() );
1828
1829 if( aEvent->IsAction( &ACTIONS::doDelete ) )
1830 {
1831 wxLogTrace( traceSchMove, "handleMoveToolActions: doDelete, exiting move" );
1832 const_cast<TOOL_EVENT*>( aEvent )->SetPassEvent();
1833 return false; // Exit on delete; there will no longer be anything to drag
1834 }
1835 else if( aEvent->IsAction( &ACTIONS::duplicate )
1837 || aEvent->IsAction( &ACTIONS::redo ) )
1838 {
1839 wxBell();
1840 }
1841 else if( aEvent->IsAction( &SCH_ACTIONS::rotateCW ) )
1842 {
1843 wxLogTrace( traceSchMove, "handleMoveToolActions: rotateCW event received, selection size=%u",
1844 aSelection.GetSize() );
1845 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::rotateCW, aCommit );
1846 wxLogTrace( traceSchMove, "handleMoveToolActions: rotateCW RunSynchronousAction completed" );
1847 updateStoredPositions( aSelection );
1848 wxLogTrace( traceSchMove, "handleMoveToolActions: rotateCW updateStoredPositions completed" );
1849 // Note: SCH_EDIT_TOOL::Rotate already posts refreshPreview when moving
1850 }
1851 else if( aEvent->IsAction( &SCH_ACTIONS::rotateCCW ) )
1852 {
1853 wxLogTrace( traceSchMove, "handleMoveToolActions: rotateCCW event received, selection size=%u",
1854 aSelection.GetSize() );
1855 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::rotateCCW, aCommit );
1856 wxLogTrace( traceSchMove, "handleMoveToolActions: rotateCCW RunSynchronousAction completed" );
1857 updateStoredPositions( aSelection );
1858 wxLogTrace( traceSchMove, "handleMoveToolActions: rotateCCW updateStoredPositions completed" );
1859 // Note: SCH_EDIT_TOOL::Rotate already posts refreshPreview when moving
1860 }
1861 else if( aEvent->IsAction( &ACTIONS::increment ) )
1862 {
1863 if( aEvent->HasParameter() )
1864 m_toolMgr->RunSynchronousAction( ACTIONS::increment, aCommit, aEvent->Parameter<ACTIONS::INCREMENT>() );
1865 else
1866 m_toolMgr->RunSynchronousAction( ACTIONS::increment, aCommit, ACTIONS::INCREMENT{ 1, 0 } );
1867
1868 updateStoredPositions( aSelection );
1869 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1870 }
1871 else if( aEvent->IsAction( &SCH_ACTIONS::toDLabel ) )
1872 {
1873 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toDLabel, aCommit );
1874 updateStoredPositions( aSelection );
1875 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1876 }
1877 else if( aEvent->IsAction( &SCH_ACTIONS::toGLabel ) )
1878 {
1879 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toGLabel, aCommit );
1880 updateStoredPositions( aSelection );
1881 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1882 }
1883 else if( aEvent->IsAction( &SCH_ACTIONS::toHLabel ) )
1884 {
1885 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toHLabel, aCommit );
1886 updateStoredPositions( aSelection );
1887 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1888 }
1889 else if( aEvent->IsAction( &SCH_ACTIONS::toLabel ) )
1890 {
1891 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toLabel, aCommit );
1892 updateStoredPositions( aSelection );
1893 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1894 }
1895 else if( aEvent->IsAction( &SCH_ACTIONS::toText ) )
1896 {
1897 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toText, aCommit );
1898 updateStoredPositions( aSelection );
1899 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1900 }
1901 else if( aEvent->IsAction( &SCH_ACTIONS::toTextBox ) )
1902 {
1903 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toTextBox, aCommit );
1904 updateStoredPositions( aSelection );
1905 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1906 }
1907 else if( aEvent->Action() == TA_CHOICE_MENU_CHOICE )
1908 {
1909 if( *aEvent->GetCommandId() >= ID_POPUP_SCH_SELECT_UNIT
1911 {
1912 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( m_selectionTool->GetSelection().Front() );
1913 int unit = *aEvent->GetCommandId() - ID_POPUP_SCH_SELECT_UNIT;
1914
1915 if( symbol )
1916 {
1917 m_frame->SelectUnit( symbol, unit );
1918 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1919 }
1920 }
1921 else if( *aEvent->GetCommandId() >= ID_POPUP_SCH_SELECT_BODY_STYLE
1923 {
1924 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( m_selectionTool->GetSelection().Front() );
1925 int bodyStyle = ( *aEvent->GetCommandId() - ID_POPUP_SCH_SELECT_BODY_STYLE ) + 1;
1926
1927 if( symbol && symbol->GetBodyStyle() != bodyStyle )
1928 {
1929 m_frame->SelectBodyStyle( symbol, bodyStyle );
1930 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1931 }
1932 }
1933 }
1934 else if( aEvent->IsAction( &SCH_ACTIONS::highlightNet )
1935 || aEvent->IsAction( &SCH_ACTIONS::selectOnPCB ) )
1936 {
1937 // These don't make any sense during a move. Eat them.
1938 }
1939 else
1940 {
1941 return true; // Continue processing
1942 }
1943
1944 return true; // Continue processing
1945}
1946
1947
1949{
1950 wxLogTrace( traceSchMove, "updateStoredPositions: start, selection size=%u",
1951 aSelection.GetSize() );
1952
1953 // After transformations like rotation during a move, we need to update the stored
1954 // positions that moveItem() uses, particularly for sheet pins which rely on them
1955 // for constraint calculations.
1956 int itemCount = 0;
1957
1958 for( EDA_ITEM* item : aSelection )
1959 {
1960 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item );
1961
1962 if( !schItem )
1963 continue;
1964
1965 VECTOR2I oldPos = schItem->GetStoredPos();
1966 VECTOR2I newPos = schItem->GetPosition();
1967 schItem->SetStoredPos( newPos );
1968
1969 // Re-baseline the pin's border distance after a transform (e.g. rotation).
1970 if( schItem->Type() == SCH_SHEET_PIN_T )
1971 {
1972 SCH_SHEET_PIN* pin = static_cast<SCH_SHEET_PIN*>( schItem );
1973
1974 if( m_sheetPinDragArc.count( pin ) )
1975 m_sheetPinDragArc[pin] = sheetBorderArc( pin->GetParent(), pin->GetSide(), pin->GetPosition() );
1976 }
1977
1978 wxLogTrace( traceSchMove, " item[%d] type=%d: stored pos updated (%d,%d) -> (%d,%d)",
1979 itemCount++, (int) schItem->Type(), oldPos.x, oldPos.y, newPos.x, newPos.y );
1980
1981 // Also update stored positions for sheet pins
1982 if( schItem->Type() == SCH_SHEET_T )
1983 {
1984 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( schItem );
1985 for( SCH_SHEET_PIN* pin : sheet->GetPins() )
1986 {
1987 VECTOR2I pinOldPos = pin->GetStoredPos();
1988 VECTOR2I pinNewPos = pin->GetPosition();
1989 pin->SetStoredPos( pinNewPos );
1990 wxLogTrace( traceSchMove, " sheet pin: stored pos updated (%d,%d) -> (%d,%d)",
1991 pinOldPos.x, pinOldPos.y, pinNewPos.x, pinNewPos.y );
1992 }
1993 }
1994 }
1995
1996 wxLogTrace( traceSchMove, "updateStoredPositions: complete, updated %d items", itemCount );
1997}
1998
1999
2001{
2002 m_hiddenJunctions.clear();
2003
2004 for( EDA_ITEM* item : aSelection )
2005 item->SetFlags( STRUCT_DELETED );
2006
2007 for( EDA_ITEM* edaItem : aSelection )
2008 {
2009 if( edaItem->Type() != SCH_LINE_T )
2010 continue;
2011
2012 SCH_LINE* line = static_cast<SCH_LINE*>( edaItem );
2013
2014 for( const VECTOR2I& pt : line->GetConnectionPoints() )
2015 {
2016 SCH_JUNCTION* jct = static_cast<SCH_JUNCTION*>( m_frame->GetScreen()->GetItem( pt, 0, SCH_JUNCTION_T ) );
2017
2018 if( jct && !jct->IsSelected()
2019 && std::none_of( m_hiddenJunctions.begin(), m_hiddenJunctions.end(),
2020 [jct]( const HIDDEN_JUNCTION& aHidden )
2021 {
2022 return aHidden.m_junction == jct;
2023 } ) )
2024 {
2026 JUNCTION_HELPERS::AnalyzePoint( m_frame->GetScreen()->Items(), pt, false );
2027
2028 if( !info.isJunction )
2029 {
2030 m_hiddenJunctions.push_back( { jct, line->m_Uuid, pt == line->GetStartPoint() } );
2031 m_view->Hide( jct, true );
2032 }
2033 }
2034 }
2035 }
2036
2037 for( EDA_ITEM* item : aSelection )
2038 item->ClearFlags( STRUCT_DELETED );
2039}
2040
2041
2043{
2044 SCH_SCREEN* screen = m_frame->GetScreen();
2045
2046 for( const HIDDEN_JUNCTION& hidden : m_hiddenJunctions )
2047 {
2048 m_view->Hide( hidden.m_junction, false );
2049
2050 SCH_LINE* line = dynamic_cast<SCH_LINE*>( m_frame->Schematic().ResolveItem( hidden.m_lineId, nullptr, true ) );
2051
2052 if( !line )
2053 continue;
2054
2055 VECTOR2I newPos = hidden.m_atLineStart ? line->GetStartPoint() : line->GetEndPoint();
2056
2057 if( newPos != hidden.m_junction->GetPosition()
2058 && !screen->IsExplicitJunction( hidden.m_junction->GetPosition() )
2059 && screen->IsExplicitJunctionNeeded( newPos ) )
2060 {
2061 aCommit->Modify( hidden.m_junction, screen );
2062 hidden.m_junction->SetPosition( newPos );
2063 m_frame->UpdateItem( hidden.m_junction, false, true );
2064 }
2065 }
2066}
2067
2068
2069void SCH_MOVE_TOOL::finalizeMoveOperation( SCH_SELECTION& aSelection, SCH_COMMIT* aCommit, bool aUnselect,
2070 const std::vector<DANGLING_END_ITEM>& aInternalPoints )
2071{
2073 const bool isSlice = ( m_mode == SLICE );
2074 const bool isDragLike = ( m_mode == DRAG || m_mode == BREAK );
2075
2076 // Save whatever new bend lines and changed lines survived the drag
2077 for( SCH_LINE* newLine : m_newDragLines )
2078 {
2079 newLine->ClearEditFlags();
2080 aCommit->Added( newLine, m_frame->GetScreen() );
2081 }
2082
2083 // These lines have been changed, but aren't selected. We need to manually clear these
2084 // edit flags or they'll stick around.
2085 for( SCH_LINE* oldLine : m_changedDragLines )
2086 oldLine->ClearEditFlags();
2087
2088 controls->ForceCursorPosition( false );
2089 controls->ShowCursor( false );
2090 controls->SetAutoPan( false );
2091
2092 m_moveOffset = { 0, 0 };
2093 m_anchorPos.reset();
2094
2095 // One last update after exiting loop (for slower stuff, such as updating SCREEN's RTree)
2096 for( EDA_ITEM* item : aSelection )
2097 {
2098 updateItem( item, true );
2099
2100 if( SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( item ) )
2101 sch_item->SetConnectivityDirty( true );
2102 }
2103
2104 if( aSelection.GetSize() == 1 && aSelection.Front()->IsNew() )
2105 m_frame->SaveCopyForRepeatItem( static_cast<SCH_ITEM*>( aSelection.Front() ) );
2106
2107 m_selectionTool->RemoveItemsFromSel( &m_dragAdditions, QUIET_MODE );
2108
2110
2111 // If we move items away from a junction, we _may_ want to add a junction there
2112 // to denote the state
2113 for( const DANGLING_END_ITEM& it : aInternalPoints )
2114 {
2115 if( m_frame->GetScreen()->IsExplicitJunctionNeeded( it.GetPosition() ) )
2116 lwbTool->AddJunction( aCommit, m_frame->GetScreen(), it.GetPosition() );
2117 }
2118
2119 // Create a selection of original selection, drag selected/changed items, and new bend
2120 // lines for later before we clear them in the aCommit. We'll need these to check for new
2121 // junctions needed, etc.
2122 SCH_SELECTION selectionCopy( aSelection );
2123
2124 for( SCH_LINE* line : m_newDragLines )
2125 selectionCopy.Add( line );
2126
2127 for( SCH_LINE* line : m_changedDragLines )
2128 selectionCopy.Add( line );
2129
2130 lwbTool->TrimOverLappingWires( aCommit, &selectionCopy );
2131
2132 migrateHiddenJunctions( aCommit );
2133
2134 lwbTool->AddJunctionsIfNeeded( aCommit, &selectionCopy );
2135
2136 // This needs to run prior to `RecalculateConnections` because we need to identify the
2137 // lines that are newly dangling
2138 if( isDragLike && !isSlice )
2139 trimDanglingLines( aCommit );
2140
2141 // Auto-rotate any moved labels
2142 for( EDA_ITEM* item : aSelection )
2143 m_frame->AutoRotateItem( m_frame->GetScreen(), static_cast<SCH_ITEM*>( item ) );
2144
2145 // Clear SELECTED_BY_DRAG and other temp flags before CleanUp so that cleanup can properly
2146 // process all items, including removing zero-length wires and unwanted stubs
2147 for( EDA_ITEM* item : m_frame->GetScreen()->Items() )
2148 item->ClearTempFlags();
2149
2150 for( EDA_ITEM* item : selectionCopy )
2151 item->ClearTempFlags();
2152
2153 m_frame->Schematic().CleanUp( aCommit );
2154
2155 // Mirror the IS_MOVING flag propagation done at the start of the move so that child items
2156 // (e.g. label fields, symbol pins/fields) don't keep their edit flags after the move ends.
2157 auto clearChildEditFlags =
2158 []( SCH_ITEM* aItem )
2159 {
2160 aItem->RunOnChildren(
2161 []( SCH_ITEM* aChild )
2162 {
2163 aChild->ClearEditFlags();
2164 },
2166 };
2167
2168 for( EDA_ITEM* item : m_frame->GetScreen()->Items() )
2169 {
2170 item->ClearEditFlags();
2171
2172 if( SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item ) )
2173 clearChildEditFlags( schItem );
2174 }
2175
2176 // Ensure any selected item not in screen main list (for instance symbol fields) has its
2177 // edit flags cleared
2178 for( EDA_ITEM* item : selectionCopy )
2179 {
2180 item->ClearEditFlags();
2181
2182 if( SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item ) )
2183 clearChildEditFlags( schItem );
2184 }
2185
2186 m_newDragLines.clear();
2187 m_changedDragLines.clear();
2188
2189 if( aUnselect )
2190 m_toolMgr->RunAction( ACTIONS::selectionClear );
2191 else
2192 m_selectionTool->RebuildSelection(); // Schematic cleanup might have merged lines, etc.
2193}
2194
2195
2197 SCH_COMMIT* aCommit )
2198{
2199 SCH_SCREEN* destScreen = aTargetSheet->GetScreen();
2200 SCH_SCREEN* srcScreen = m_frame->GetScreen();
2201
2202 BOX2I bbox;
2203
2204 for( EDA_ITEM* item : aSelection )
2205 bbox.Merge( static_cast<SCH_ITEM*>( item )->GetBoundingBox() );
2206
2207 VECTOR2I offset = VECTOR2I( 0, 0 ) - bbox.GetPosition();
2208 int step = schIUScale.MilsToIU( 50 );
2209 bool overlap = false;
2210
2211 do
2212 {
2213 BOX2I moved = bbox;
2214 moved.Move( offset );
2215 overlap = false;
2216
2217 for( SCH_ITEM* existing : destScreen->Items() )
2218 {
2219 if( moved.Intersects( existing->GetBoundingBox() ) )
2220 {
2221 overlap = true;
2222 break;
2223 }
2224 }
2225
2226 if( overlap )
2227 offset += VECTOR2I( step, step );
2228 } while( overlap );
2229
2230 for( EDA_ITEM* item : aSelection )
2231 {
2232 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( item );
2233
2234 // Remove from current screen and view manually
2235 m_frame->RemoveFromScreen( schItem, srcScreen );
2236
2237 // Move the item
2238 schItem->Move( offset );
2239
2240 // Add to destination screen manually (won't add to view since it's not current)
2241 destScreen->Append( schItem );
2242
2243 // Record in commit with CHT_DONE flag to bypass automatic screen/view operations
2244 aCommit->Stage( schItem, CHT_REMOVE | CHT_DONE, srcScreen );
2245 aCommit->Stage( schItem, CHT_ADD | CHT_DONE, destScreen );
2246 }
2247}
2248
2249
2251{
2252 // Need a local cleanup first to ensure we remove unneeded junctions
2253 m_frame->Schematic().CleanUp( aCommit, m_frame->GetScreen() );
2254
2255 std::set<SCH_ITEM*> danglers;
2256
2257 std::function<void( SCH_ITEM* )> changeHandler =
2258 [&]( SCH_ITEM* aChangedItem ) -> void
2259 {
2260 m_toolMgr->GetView()->Update( aChangedItem, KIGFX::REPAINT );
2261
2262 if( aChangedItem->IsSelected() )
2263 return;
2264
2265 SCH_LINE* line = dynamic_cast<SCH_LINE*>( aChangedItem );
2266
2267 if( !line )
2268 return;
2269
2270 // Split segments that are dangling get trimmed back since they extend
2271 // past the break point.
2272 if( line->HasFlag( IS_BROKEN ) && line->IsDangling() )
2273 {
2274 danglers.insert( aChangedItem );
2275 }
2276 // Drag wires that are completely disconnected (both ends dangling) are
2277 // stubs that should be removed. Wires with only one connected end are
2278 // still providing connectivity and must be preserved.
2279 else if( line->HasFlag( IS_NEW ) && !line->HasFlag( IS_BROKEN )
2280 && line->IsStartDangling() && line->IsEndDangling() )
2281 {
2282 danglers.insert( aChangedItem );
2283 }
2284 };
2285
2286 m_frame->GetScreen()->TestDanglingEnds( nullptr, &changeHandler );
2287
2288 for( SCH_ITEM* line : danglers )
2289 {
2290 line->SetFlags( STRUCT_DELETED );
2291 aCommit->Removed( line, m_frame->GetScreen() );
2292 updateItem( line, false ); // Update any cached visuals before commit processes
2293 m_frame->RemoveFromScreen( line, m_frame->GetScreen() );
2294 }
2295}
2296
2297
2298void SCH_MOVE_TOOL::getConnectedItems( SCH_ITEM* aOriginalItem, const VECTOR2I& aPoint, EDA_ITEMS& aList )
2299{
2300 EE_RTREE& items = m_frame->GetScreen()->Items();
2301 EE_RTREE::EE_TYPE itemsOverlapping = items.Overlapping( aOriginalItem->GetBoundingBox() );
2302 SCH_ITEM* foundJunction = nullptr;
2303 SCH_ITEM* foundSymbol = nullptr;
2304
2305 // If you're connected to a junction, you're only connected to the junction.
2306 //
2307 // But, if you're connected to a junction on a pin, you're only connected to the pin. This
2308 // is because junctions and pins have different logic for how bend lines are generated and
2309 // we need to prioritize the pin version in some cases.
2310 for( SCH_ITEM* item : itemsOverlapping )
2311 {
2312 if( item != aOriginalItem && item->IsConnected( aPoint ) )
2313 {
2314 if( item->Type() == SCH_JUNCTION_T )
2315 foundJunction = item;
2316 else if( item->Type() == SCH_SYMBOL_T )
2317 foundSymbol = item;
2318 }
2319 }
2320
2321 if( foundSymbol && foundJunction )
2322 {
2323 aList.push_back( foundSymbol );
2324 return;
2325 }
2326
2327 if( foundJunction )
2328 {
2329 aList.push_back( foundJunction );
2330 return;
2331 }
2332
2333
2334 for( SCH_ITEM* test : itemsOverlapping )
2335 {
2336 if( test == aOriginalItem || !test->CanConnect( aOriginalItem ) )
2337 continue;
2338
2339 switch( test->Type() )
2340 {
2341 case SCH_LINE_T:
2342 {
2343 SCH_LINE* line = static_cast<SCH_LINE*>( test );
2344
2345 // When getting lines for the connection cache, it's important that we only add
2346 // items at the unselected end, since that is the only end that is handled specially.
2347 // Fully selected lines, and the selected end of a partially selected line, are moved
2348 // around normally and don't care about their connections.
2349 if( ( line->HasFlag( STARTPOINT ) && aPoint == line->GetStartPoint() )
2350 || ( line->HasFlag( ENDPOINT ) && aPoint == line->GetEndPoint() ) )
2351 {
2352 continue;
2353 }
2354
2355 if( test->IsConnected( aPoint ) )
2356 aList.push_back( test );
2357
2358 // Labels can connect to a wire (or bus) anywhere along the length
2359 if( SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( aOriginalItem ) )
2360 {
2361 if( static_cast<SCH_LINE*>( test )->HitTest( label->GetPosition(), 1 ) )
2362 aList.push_back( test );
2363 }
2364
2365 break;
2366 }
2367
2368 case SCH_SHEET_T:
2369 if( aOriginalItem->Type() == SCH_LINE_T )
2370 {
2371 SCH_LINE* line = static_cast<SCH_LINE*>( aOriginalItem );
2372
2373 for( SCH_SHEET_PIN* pin : static_cast<SCH_SHEET*>( test )->GetPins() )
2374 {
2375 if( pin->IsConnected( aPoint ) )
2376 {
2377 if( pin->IsSelected() )
2378 m_specialCaseSheetPins[pin] = { line, line->GetStartPoint() == aPoint };
2379
2380 aList.push_back( pin );
2381 }
2382 }
2383 }
2384
2385 break;
2386
2387 case SCH_SYMBOL_T:
2388 case SCH_JUNCTION_T:
2389 case SCH_NO_CONNECT_T:
2390 if( test->IsConnected( aPoint ) )
2391 aList.push_back( test );
2392
2393 break;
2394
2395 case SCH_LABEL_T:
2396 case SCH_GLOBAL_LABEL_T:
2397 case SCH_HIER_LABEL_T:
2399 // Labels can connect to a wire (or bus) anywhere along the length
2400 if( aOriginalItem->Type() == SCH_LINE_T && test->CanConnect( aOriginalItem ) )
2401 {
2402 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( test );
2403 SCH_LINE* line = static_cast<SCH_LINE*>( aOriginalItem );
2404
2405 if( line->HitTest( label->GetPosition(), 1 ) )
2406 aList.push_back( label );
2407 }
2408
2409 break;
2410
2413 if( aOriginalItem->Type() == SCH_LINE_T && test->CanConnect( aOriginalItem ) )
2414 {
2415 SCH_TEXT* label = static_cast<SCH_TEXT*>( test );
2416 SCH_LINE* line = static_cast<SCH_LINE*>( aOriginalItem );
2417
2418 if( line->HitTest( aPoint, 1 ) )
2419 aList.push_back( label );
2420 }
2421
2422 break;
2423
2424 default:
2425 break;
2426 }
2427 }
2428}
2429
2430
2431void SCH_MOVE_TOOL::getConnectedDragItems( SCH_COMMIT* aCommit, SCH_ITEM* aSelectedItem, const VECTOR2I& aPoint,
2432 EDA_ITEMS& aList )
2433{
2434 EE_RTREE& items = m_frame->GetScreen()->Items();
2435 std::set<SCH_ITEM*> connectableCandidates;
2436 std::vector<SCH_ITEM*> itemsConnectable;
2437 bool ptHasUnselectedJunction = false;
2438
2439 for( SCH_ITEM* item : items.Overlapping( aSelectedItem->GetBoundingBox() ) )
2440 connectableCandidates.insert( item );
2441
2442 // Labels can connect at their anchor even if the label bbox doesn't overlap the target, e.g.
2443 // sheet pins can do this sometimes with just net labels and no wires.
2444 if( dynamic_cast<SCH_LABEL_BASE*>( aSelectedItem ) )
2445 {
2446 for( SCH_ITEM* item : items.Overlapping( aPoint, 1 ) )
2447 connectableCandidates.insert( item );
2448 }
2449
2450 auto makeNewWire =
2451 [this]( SCH_COMMIT* commit, SCH_ITEM* fixed, SCH_ITEM* selected, const VECTOR2I& start,
2452 const VECTOR2I& end )
2453 {
2454 SCH_LINE* newWire;
2455 bool isBusLabel = false;
2456
2457 if( SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( fixed ) )
2458 isBusLabel |= SCH_CONNECTION::IsBusLabel( label->GetText() );
2459
2460 if( SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( selected ) )
2461 isBusLabel |= SCH_CONNECTION::IsBusLabel( label->GetText() );
2462
2463 // Add a new newWire between the fixed item and the selected item so the selected
2464 // item can be dragged.
2465 if( fixed->GetLayer() == LAYER_BUS_JUNCTION || fixed->GetLayer() == LAYER_BUS
2466 || selected->GetLayer() == LAYER_BUS || isBusLabel )
2467 {
2468 newWire = new SCH_LINE( start, LAYER_BUS );
2469 }
2470 else
2471 {
2472 newWire = new SCH_LINE( start, LAYER_WIRE );
2473 }
2474
2475 newWire->SetFlags( IS_NEW );
2476 newWire->SetConnectivityDirty( true );
2477
2478 SCH_LINE* selectedLine = dynamic_cast<SCH_LINE*>( selected );
2479 SCH_LINE* fixedLine = dynamic_cast<SCH_LINE*>( fixed );
2480
2481 if( selectedLine )
2482 {
2483 newWire->SetLastResolvedState( selected );
2484 cloneWireConnection( newWire, selectedLine, m_frame );
2485 }
2486 else if( fixedLine )
2487 {
2488 newWire->SetLastResolvedState( fixed );
2489 cloneWireConnection( newWire, fixedLine, m_frame );
2490 }
2491
2492 newWire->SetEndPoint( end );
2493 m_frame->AddToScreen( newWire, m_frame->GetScreen() );
2494 commit->Added( newWire, m_frame->GetScreen() );
2495
2496 return newWire;
2497 };
2498
2499 auto makeNewJunction =
2500 [this]( SCH_COMMIT* commit, SCH_LINE* line, const VECTOR2I& pt )
2501 {
2502 SCH_JUNCTION* junction = new SCH_JUNCTION( pt );
2503 junction->SetFlags( IS_NEW );
2504 junction->SetConnectivityDirty( true );
2505 junction->SetLastResolvedState( line );
2506
2507 if( line->IsBus() )
2508 junction->SetLayer( LAYER_BUS_JUNCTION );
2509
2510 m_frame->AddToScreen( junction, m_frame->GetScreen() );
2511 commit->Added( junction, m_frame->GetScreen() );
2512
2513 return junction;
2514 };
2515
2516 for( SCH_ITEM* item : connectableCandidates )
2517 {
2518 if( item->Type() == SCH_SHEET_T )
2519 {
2520 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
2521
2522 // A sheet inside a selected group moves with the group, so its pins should not be
2523 // treated as fixed connection anchors.
2524 if( sheet->HasSelectedAncestorGroup() )
2525 continue;
2526
2527 for( SCH_SHEET_PIN* pin : sheet->GetPins() )
2528 {
2529 if( !pin->IsSelected()
2530 && pin->GetPosition() == aPoint
2531 && pin->CanConnect( aSelectedItem ) )
2532 {
2533 itemsConnectable.push_back( pin );
2534 }
2535 }
2536
2537 continue;
2538 }
2539
2540 // Skip ourselves, skip already selected items (but not lines, they need both ends tested)
2541 // and skip unconnectable items. Items inside a selected group are also moving with the
2542 // selection even though they do not carry the SELECTED flag themselves; treating them as
2543 // fixed anchors causes spurious stub wires to be created at the group boundary.
2544 if( item == aSelectedItem
2545 || ( item->Type() != SCH_LINE_T && ( item->IsSelected() || item->HasSelectedAncestorGroup() ) )
2546 || !item->CanConnect( aSelectedItem ) )
2547 {
2548 continue;
2549 }
2550
2551 itemsConnectable.push_back( item );
2552 }
2553
2554 for( SCH_ITEM* item : itemsConnectable )
2555 {
2556 if( item->Type() == SCH_JUNCTION_T && item->IsConnected( aPoint ) && !item->IsSelected() )
2557 {
2558 ptHasUnselectedJunction = true;
2559 break;
2560 }
2561 }
2562
2563 SCH_LINE* newWire = nullptr;
2564
2565 for( SCH_ITEM* test : itemsConnectable )
2566 {
2567 KICAD_T testType = test->Type();
2568
2569 switch( testType )
2570 {
2571 case SCH_LINE_T:
2572 {
2573 // Select the connected end of wires/bus connections that don't have an unselected
2574 // junction isolating them from the drag
2575 if( ptHasUnselectedJunction )
2576 break;
2577
2578 SCH_LINE* line = static_cast<SCH_LINE*>( test );
2579
2580 // A line that is itself a member of a selected group is already moving with that
2581 // group; do not add it as a drag attachment or it will move twice.
2582 bool lineInSelectedGroup = line->HasSelectedAncestorGroup();
2583
2584 if( line->GetStartPoint() == aPoint )
2585 {
2586 // It's possible to manually select one end of a line and get a drag
2587 // connected other end, so we set the flag and then early exit the loop
2588 // later if the other drag items like labels attached to the line have
2589 // already been grabbed during the partial selection process.
2590 if( !lineInSelectedGroup )
2591 line->SetFlags( STARTPOINT );
2592
2593 if( line->HasFlag( SELECTED ) || line->HasFlag( SELECTED_BY_DRAG )
2594 || lineInSelectedGroup )
2595 {
2596 continue;
2597 }
2598 else
2599 {
2600 line->SetFlags( SELECTED_BY_DRAG );
2601 aList.push_back( line );
2602 }
2603 }
2604 else if( line->GetEndPoint() == aPoint )
2605 {
2606 if( !lineInSelectedGroup )
2607 line->SetFlags( ENDPOINT );
2608
2609 if( line->HasFlag( SELECTED ) || line->HasFlag( SELECTED_BY_DRAG )
2610 || lineInSelectedGroup )
2611 {
2612 continue;
2613 }
2614 else
2615 {
2616 line->SetFlags( SELECTED_BY_DRAG );
2617 aList.push_back( line );
2618 }
2619 }
2620 else
2621 {
2622 switch( aSelectedItem->Type() )
2623 {
2624 // These items can connect anywhere along a line
2627 case SCH_LABEL_T:
2628 case SCH_HIER_LABEL_T:
2629 case SCH_GLOBAL_LABEL_T:
2631 // Only add a line if this line is unselected; if the label and line are both
2632 // selected they'll move together
2633 if( line->HitTest( aPoint, 1 ) && !line->HasFlag( SELECTED )
2634 && !line->HasFlag( SELECTED_BY_DRAG ) )
2635 {
2636 newWire = makeNewWire( aCommit, line, aSelectedItem, aPoint, aPoint );
2637 newWire->SetFlags( SELECTED_BY_DRAG | STARTPOINT );
2638 newWire->StoreAngle( ( line->Angle() + ANGLE_90 ).Normalize() );
2639 aList.push_back( newWire );
2640
2641 if( aPoint != line->GetStartPoint() && aPoint != line->GetEndPoint() )
2642 {
2643 // Split line in half
2644 aCommit->Modify( line, m_frame->GetScreen() );
2645
2646 VECTOR2I oldEnd = line->GetEndPoint();
2647 line->SetEndPoint( aPoint );
2648
2649 makeNewWire( aCommit, line, line, aPoint, oldEnd );
2650 makeNewJunction( aCommit, line, aPoint );
2651 }
2652 else
2653 {
2654 m_lineConnectionCache[ newWire ] = { line };
2655 m_lineConnectionCache[ line ] = { newWire };
2656 }
2657 }
2658 break;
2659
2660 default:
2661 break;
2662 }
2663
2664 break;
2665 }
2666
2667 // When only one end moves, keep attached labels tracking the moving end so they stay
2668 // connected to the line.
2669 for( SCH_ITEM* item : items.Overlapping( line->GetBoundingBox() ) )
2670 {
2671 SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( item );
2672
2673 if( !label || label->IsSelected() )
2674 continue; // These will be moved on their own because they're selected
2675
2676 if( label->HasFlag( SELECTED_BY_DRAG ) )
2677 continue;
2678
2679 if( label->CanConnect( line ) && line->HitTest( label->GetPosition(), 1 ) )
2680 {
2681 label->SetFlags( SELECTED_BY_DRAG );
2682 aList.push_back( label );
2683
2685 info.attachedLine = line;
2686 info.originalLabelPos = label->GetPosition();
2687 info.originalLineStart = line->GetStartPoint();
2688 info.originalLineEnd = line->GetEndPoint();
2689 m_specialCaseLabels[label] = info;
2690 }
2691 }
2692
2693 break;
2694 }
2695
2696 case SCH_SHEET_T:
2697 for( SCH_SHEET_PIN* pin : static_cast<SCH_SHEET*>( test )->GetPins() )
2698 {
2699 if( pin->IsConnected( aPoint ) )
2700 {
2701 if( pin->IsSelected() && aSelectedItem->Type() == SCH_LINE_T )
2702 {
2703 SCH_LINE* line = static_cast<SCH_LINE*>( aSelectedItem );
2704 m_specialCaseSheetPins[ pin ] = { line, line->GetStartPoint() == aPoint };
2705 }
2706 else if( !newWire )
2707 {
2708 // Add a new wire between the sheetpin and the selected item so the
2709 // selected item can be dragged.
2710 newWire = makeNewWire( aCommit, pin, aSelectedItem, aPoint, aPoint );
2711 newWire->SetFlags( SELECTED_BY_DRAG | STARTPOINT );
2712 aList.push_back( newWire );
2713 }
2714 }
2715 }
2716
2717 break;
2718
2719 case SCH_SYMBOL_T:
2720 case SCH_JUNCTION_T:
2721 if( test->IsConnected( aPoint ) && !newWire )
2722 {
2723 // Add a new wire between the symbol or junction and the selected item so
2724 // the selected item can be dragged.
2725 newWire = makeNewWire( aCommit, test, aSelectedItem, aPoint, aPoint );
2726 newWire->SetFlags( SELECTED_BY_DRAG | STARTPOINT );
2727 aList.push_back( newWire );
2728 }
2729
2730 break;
2731
2732 case SCH_NO_CONNECT_T:
2733 // Select no-connects that are connected to items being moved.
2734 if( !test->HasFlag( SELECTED_BY_DRAG ) && test->IsConnected( aPoint ) )
2735 {
2736 aList.push_back( test );
2737 test->SetFlags( SELECTED_BY_DRAG );
2738 }
2739
2740 break;
2741
2742 case SCH_LABEL_T:
2743 case SCH_GLOBAL_LABEL_T:
2744 case SCH_HIER_LABEL_T:
2746 case SCH_SHEET_PIN_T:
2747 // Performance optimization:
2748 if( test->HasFlag( SELECTED_BY_DRAG ) )
2749 break;
2750
2751 // Select labels that are connected to a wire (or bus) being moved.
2752 if( aSelectedItem->Type() == SCH_LINE_T && test->CanConnect( aSelectedItem ) )
2753 {
2754 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( test );
2755 SCH_LINE* line = static_cast<SCH_LINE*>( aSelectedItem );
2756
2757 bool oneEndFixed = !line->HasFlag( STARTPOINT ) || !line->HasFlag( ENDPOINT );
2758
2759 if( line->HitTest( label->GetTextPos(), 1 ) )
2760 {
2761 if( ( !line->HasFlag( STARTPOINT ) && label->GetPosition() == line->GetStartPoint() )
2762 || ( !line->HasFlag( ENDPOINT ) && label->GetPosition() == line->GetEndPoint() ) )
2763 {
2764 //If we have a line selected at only one end, don't grab labels
2765 //connected directly to the unselected endpoint
2766 break;
2767 }
2768 else
2769 {
2770 label->SetFlags( SELECTED_BY_DRAG );
2771 aList.push_back( label );
2772
2773 if( oneEndFixed )
2774 {
2776 info.attachedLine = line;
2777 info.originalLabelPos = label->GetPosition();
2778 info.originalLineStart = line->GetStartPoint();
2779 info.originalLineEnd = line->GetEndPoint();
2780 m_specialCaseLabels[label] = info;
2781 }
2782 }
2783 }
2784 }
2785 else if( test->IsConnected( aPoint ) && !newWire )
2786 {
2787 // Add a new wire between the label and the selected item so the selected item
2788 // can be dragged.
2789 newWire = makeNewWire( aCommit, test, aSelectedItem, aPoint, aPoint );
2790 newWire->SetFlags( SELECTED_BY_DRAG | STARTPOINT );
2791 aList.push_back( newWire );
2792 }
2793
2794 break;
2795
2798 // Performance optimization:
2799 if( test->HasFlag( SELECTED_BY_DRAG ) )
2800 break;
2801
2802 // Select bus entries that are connected to a bus being moved.
2803 if( aSelectedItem->Type() == SCH_LINE_T && test->CanConnect( aSelectedItem ) )
2804 {
2805 SCH_LINE* line = static_cast<SCH_LINE*>( aSelectedItem );
2806
2807 if( ( !line->HasFlag( STARTPOINT ) && test->IsConnected( line->GetStartPoint() ) )
2808 || ( !line->HasFlag( ENDPOINT ) && test->IsConnected( line->GetEndPoint() ) ) )
2809 {
2810 // If we have a line selected at only one end, don't grab bus entries
2811 // connected directly to the unselected endpoint
2812 continue;
2813 }
2814
2815 for( VECTOR2I& point : test->GetConnectionPoints() )
2816 {
2817 if( line->HitTest( point, 1 ) )
2818 {
2819 test->SetFlags( SELECTED_BY_DRAG );
2820 aList.push_back( test );
2821
2822 // A bus entry needs its wire & label as well
2823 std::vector<VECTOR2I> ends = test->GetConnectionPoints();
2824 VECTOR2I otherEnd;
2825
2826 if( ends[0] == point )
2827 otherEnd = ends[1];
2828 else
2829 otherEnd = ends[0];
2830
2831 getConnectedDragItems( aCommit, test, otherEnd, aList );
2832
2833 // No need to test the other end of the bus entry
2834 break;
2835 }
2836 }
2837 }
2838
2839 break;
2840
2841 default:
2842 break;
2843 }
2844 }
2845}
2846
2847
2848void SCH_MOVE_TOOL::moveItem( EDA_ITEM* aItem, const VECTOR2I& aDelta )
2849{
2850 static int moveCallCount = 0;
2851 wxLogTrace( traceSchMove, "moveItem[%d]: type=%d, delta=(%d,%d)",
2852 ++moveCallCount, aItem->Type(), aDelta.x, aDelta.y );
2853
2854 switch( aItem->Type() )
2855 {
2856 case SCH_LINE_T:
2857 if( m_mode == MOVE )
2858 {
2859 // In MOVE mode, both endpoints always move
2860 static_cast<SCH_LINE*>( aItem )->Move( aDelta );
2861 }
2862 else
2863 {
2864 // In DRAG mode, only flagged endpoints move - use shared function
2865 MoveSchematicItem( aItem, aDelta );
2866 }
2867
2868 break;
2869
2870 case SCH_PIN_T:
2871 case SCH_FIELD_T:
2872 {
2873 SCH_ITEM* parent = (SCH_ITEM*) aItem->GetParent();
2874 VECTOR2I delta( aDelta );
2875
2876 if( parent && parent->Type() == SCH_SYMBOL_T )
2877 {
2878 SCH_SYMBOL* symbol = (SCH_SYMBOL*) aItem->GetParent();
2879 TRANSFORM transform = symbol->GetTransform().InverseTransform();
2880
2881 delta = transform.TransformCoordinate( delta );
2882 }
2883
2884 static_cast<SCH_ITEM*>( aItem )->Move( delta );
2885
2886 // If we're moving a field with respect to its parent then it's no longer auto-placed
2887 if( aItem->Type() == SCH_FIELD_T && parent && !parent->IsSelected() )
2889
2890 break;
2891 }
2892
2893 case SCH_SHEET_PIN_T:
2894 // Use shared function for sheet pin movement
2895 MoveSchematicItem( aItem, aDelta );
2896 break;
2897
2898 case SCH_LABEL_T:
2900 case SCH_GLOBAL_LABEL_T:
2901 case SCH_HIER_LABEL_T:
2902 {
2903 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( aItem );
2904 if( !m_specialCaseLabels.count( label ) )
2905 label->Move( aDelta );
2906
2907 break;
2908 }
2909
2910 default:
2911 static_cast<SCH_ITEM*>( aItem )->Move( aDelta );
2912 break;
2913 }
2914
2915 aItem->SetFlags( IS_MOVING );
2916}
2917
2918
2920{
2922 SCH_SELECTION& selection = m_selectionTool->RequestSelection( SCH_COLLECTOR::MovableItems );
2923
2924 m_selectionTool->FilterSelectionForLockedItems();
2925
2926 GRID_HELPER_GRIDS selectionGrid = grid.GetSelectionGrid( selection );
2927 SCH_COMMIT commit( m_toolMgr );
2928
2929 auto doMoveItem =
2930 [&]( EDA_ITEM* item, const VECTOR2I& delta )
2931 {
2932 commit.Modify( item, m_frame->GetScreen(), RECURSE_MODE::RECURSE );
2933
2934 // Ensure only one end is moved when calling moveItem
2935 // i.e. we are in drag mode
2936 MOVE_MODE tmpMode = m_mode;
2937 m_mode = DRAG;
2938 moveItem( item, delta );
2939 m_mode = tmpMode;
2940
2941 item->ClearFlags( IS_MOVING );
2942 updateItem( item, true );
2943 };
2944
2945 for( SCH_ITEM* it : m_frame->GetScreen()->Items() )
2946 {
2947 if( !it->IsSelected() )
2948 it->ClearFlags( STARTPOINT | ENDPOINT );
2949
2950 if( !selection.IsHover() && it->IsSelected() )
2951 it->SetFlags( STARTPOINT | ENDPOINT );
2952
2953 it->SetStoredPos( it->GetPosition() );
2954
2955 if( it->Type() == SCH_SHEET_T )
2956 {
2957 for( SCH_SHEET_PIN* pin : static_cast<SCH_SHEET*>( it )->GetPins() )
2958 pin->SetStoredPos( pin->GetPosition() );
2959 }
2960 }
2961
2962 SCH_ALIGNMENT_CALLBACKS callbacks;
2963
2964 callbacks.m_doMoveItem = doMoveItem;
2965
2966 callbacks.m_getConnectedDragItems =
2967 [&]( SCH_ITEM* aItem, const VECTOR2I& aPoint, EDA_ITEMS& aList )
2968 {
2969 getConnectedDragItems( &commit, aItem, aPoint, aList );
2970 };
2971
2972 callbacks.m_updateItem =
2973 [&]( EDA_ITEM* aItem )
2974 {
2975 updateItem( aItem, true );
2976 };
2977
2978 recordRedundantJunctions( selection );
2979
2980 std::vector<EDA_ITEM*> items( selection.begin(), selection.end() );
2981 AlignSchematicItemsToGrid( m_frame->GetScreen(), items, grid, selectionGrid, callbacks );
2982
2984 lwbTool->TrimOverLappingWires( &commit, &selection );
2985 migrateHiddenJunctions( &commit );
2986 lwbTool->AddJunctionsIfNeeded( &commit, &selection );
2987
2989
2990 m_frame->Schematic().CleanUp( &commit );
2991 commit.Push( _( "Align Items to Grid" ) );
2992 return 0;
2993}
2994
2995
2997{
2998 // Remove new bend lines added during the drag
2999 for( SCH_LINE* newLine : m_newDragLines )
3000 {
3001 m_frame->RemoveFromScreen( newLine, m_frame->GetScreen() );
3002 delete newLine;
3003 }
3004
3005 m_newDragLines.clear();
3006}
3007
3008
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
BOX2< VECTOR2I > BOX2I
Definition box2.h:927
@ 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:208
constexpr coord_type GetY() const
Definition box2.h:205
constexpr size_type GetWidth() const
Definition box2.h:211
constexpr coord_type GetX() const
Definition box2.h:204
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:653
constexpr size_type GetHeight() const
Definition box2.h:212
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:165
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.
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition sch_item.h:96
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
virtual VECTOR2I GetPosition() const
Definition eda_item.h:348
virtual void ClearEditFlags()
Definition eda_item.h:178
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:270
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:158
const KIID m_Uuid
Definition eda_item.h:597
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:116
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition eda_item.h:160
bool IsSelected() const
Definition eda_item.h:134
EDA_ITEM * GetParent() const
Definition eda_item.h:112
bool HasSelectedAncestorGroup() const
Definition eda_item.cpp:241
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition eda_item.h:168
bool IsNew() const
Definition eda_item.h:131
virtual VECTOR2I GetTextPos() const
Definition eda_text.h:313
Implement an R-tree for fast spatial and type indexing of schematic items.
Definition sch_rtree.h:37
EE_TYPE Overlapping(const BOX2I &aRect) const
Definition sch_rtree.h:253
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:165
void SetStoredPos(const VECTOR2I &aPos)
Definition sch_item.h:309
virtual bool CanConnect(const SCH_ITEM *aItem) const
Definition sch_item.h:526
virtual void RunOnChildren(const std::function< void(SCH_ITEM *)> &aFunction, RECURSE_MODE aMode)
Definition sch_item.h:641
int GetBodyStyle() const
Definition sch_item.h:247
SCH_CONNECTION * InitializeConnection(const SCH_SHEET_PATH &aPath, CONNECTION_GRAPH *aGraph)
Create a new connection object associated with this object.
Definition sch_item.cpp:613
virtual void Move(const VECTOR2I &aMoveVector)
Move the item by aMoveVector to a new position.
Definition sch_item.h:403
void SetLayer(SCH_LAYER_ID aLayer)
Definition sch_item.h:346
void SetConnectivityDirty(bool aDirty=true)
Definition sch_item.h:600
void SetFieldsAutoplaced(AUTOPLACE_ALGO aAlgo)
Definition sch_item.h:637
bool IsConnected(const VECTOR2I &aPoint) const
Test the item to see if it is connected to aPoint.
Definition sch_item.cpp:494
virtual bool IsMovableFromAnchorPoint() const
Check if object is movable from the anchor point.
Definition sch_item.h:306
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:503
VECTOR2I & GetStoredPos()
Definition sch_item.h:308
bool IsGroupableType() const
Definition sch_item.cpp:123
virtual std::vector< VECTOR2I > GetConnectionPoints() const
Add all the connection points for this item to aPoints.
Definition sch_item.h:546
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:39
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:906
void StoreAngle()
Save the current line angle.
Definition sch_line.h:112
std::vector< VECTOR2I > GetConnectionPoints() const override
Add all the connection points for this item to aPoints.
Definition sch_line.cpp:806
bool IsStartDangling() const
Definition sch_line.h:327
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition sch_line.cpp:315
EDA_ANGLE Angle() const
Get the angle between the start and end lines.
Definition sch_line.h:101
VECTOR2I GetEndPoint() const
Definition sch_line.h:145
VECTOR2I GetStartPoint() const
Definition sch_line.h:136
bool IsEndDangling() const
Definition sch_line.h:328
void MoveEnd(const VECTOR2I &aMoveVector)
Definition sch_line.cpp:260
void SetLastResolvedState(const SCH_ITEM *aItem) override
Definition sch_line.h:160
void MoveStart(const VECTOR2I &aMoveVector)
Definition sch_line.cpp:254
double GetLength() const
Definition sch_line.cpp:340
void SetEndPoint(const VECTOR2I &aPosition)
Definition sch_line.h:146
bool IsDangling() const override
Definition sch_line.h:329
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)
True when dropping the selection into aTargetSheet would make a sheet its own descendant.
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.)
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)
Hide the junction dots that the pending edit will make redundant, noting the line end each one marks.
std::vector< HIDDEN_JUNCTION > m_hiddenJunctions
bool doMoveSelection(const TOOL_EVENT &aEvent, SCH_COMMIT *aCommit)
void recordRedundantJunctions(SCH_SELECTION &aSelection)
Move those junction dots to wherever the line end they marked has ended up.
bool dropWouldRecurse(const SCH_SELECTION &aSelection, const SCH_SHEET *aTargetSheet)
Perform the actual move of items by delta, handling split moves and orthogonal dragging.
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
void migrateHiddenJunctions(SCH_COMMIT *aCommit)
Finalize the move operation, updating junctions and cleaning up.
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)
bool IsExplicitJunction(const VECTOR2I &aPosition) const
Indicate that a junction dot is necessary at the given location.
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:118
const wxString & GetFileName() const
Definition sch_screen.h:153
bool IsExplicitJunctionNeeded(const VECTOR2I &aPosition) const
Indicate that a junction dot is necessary at the given location, and does not yet exist.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
bool TestForRecursion(const SCH_SHEET_LIST &aSrcSheetHierarchy, const wxString &aDestFileName)
Test every SCH_SHEET_PATH in this SCH_SHEET_LIST to verify if adding the sheets stored in aSrcSheetHi...
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:48
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:147
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:145
VECTOR2I GetPosition() const override
Definition sch_sheet.h:504
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:241
Schematic symbol object.
Definition sch_symbol.h:75
VECTOR2I GetPosition() const override
Definition sch_text.h:143
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:640
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:424
@ NONE
Definition eda_fill.h:42
@ RECURSE
Definition eda_item.h:51
@ NO_RECURSE
Definition eda_item.h:52
#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.
@ 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:474
@ LAYER_BUS
Definition layer_ids.h:475
@ LAYER_BUS_JUNCTION
Definition layer_ids.h:520
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:69
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:198
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:70
@ SCH_LINE_T
Definition typeinfo.h:159
@ SCH_NO_CONNECT_T
Definition typeinfo.h:156
@ SCH_SYMBOL_T
Definition typeinfo.h:168
@ SCH_FIELD_T
Definition typeinfo.h:146
@ SCH_DIRECTIVE_LABEL_T
Definition typeinfo.h:167
@ SCH_LABEL_T
Definition typeinfo.h:163
@ SCH_SHEET_T
Definition typeinfo.h:171
@ SCH_SHAPE_T
Definition typeinfo.h:145
@ SCH_HIER_LABEL_T
Definition typeinfo.h:165
@ SCH_BUS_BUS_ENTRY_T
Definition typeinfo.h:158
@ SCH_SHEET_PIN_T
Definition typeinfo.h:170
@ SCH_TEXT_T
Definition typeinfo.h:147
@ SCH_BUS_WIRE_ENTRY_T
Definition typeinfo.h:157
@ SCH_BITMAP_T
Definition typeinfo.h:160
@ SCH_TEXTBOX_T
Definition typeinfo.h:148
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:164
@ SCH_JUNCTION_T
Definition typeinfo.h:155
@ SCH_PIN_T
Definition typeinfo.h:149
constexpr int sign(T val)
Definition util.h:141
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682