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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <cmath>
26#include <wx/log.h>
27#include <trigo.h>
29#include <tool/tool_manager.h>
33#include <sch_actions.h>
34#include <sch_commit.h>
35#include <eda_item.h>
36#include <sch_group.h>
37#include <sch_item.h>
38#include <sch_symbol.h>
39#include <sch_sheet.h>
40#include <sch_sheet_pin.h>
41#include <sch_line.h>
42#include <sch_junction.h>
43#include <junction_helpers.h>
44#include <sch_edit_frame.h>
45#include <eeschema_id.h>
46#include <pgm_base.h>
47#include <view/view_controls.h>
49#include <math/box2.h>
50#include <base_units.h>
51#include <sch_screen.h>
52#include "sch_move_tool.h"
53
54
55// For adding to or removing from selections
56#define QUIET_MODE true
57
58
60 SCH_TOOL_BASE<SCH_EDIT_FRAME>( "eeschema.InteractiveMove" ),
61 m_inMoveTool( false ),
62 m_moveInProgress( false ),
63 m_isDrag( false ),
64 m_moveOffset( 0, 0 )
65{
66}
67
68
70{
72
73 auto moveCondition =
74 []( const SELECTION& aSel )
75 {
76 if( aSel.Empty() || SELECTION_CONDITIONS::OnlyTypes( { SCH_MARKER_T } )( aSel ) )
77 return false;
78
80 return false;
81
82 return true;
83 };
84
85 // Add move actions to the selection tool menu
86 //
87 CONDITIONAL_MENU& selToolMenu = m_selectionTool->GetToolMenu().GetMenu();
88
89 selToolMenu.AddItem( SCH_ACTIONS::move, moveCondition, 150 );
90 selToolMenu.AddItem( SCH_ACTIONS::drag, moveCondition, 150 );
91 selToolMenu.AddItem( SCH_ACTIONS::alignToGrid, moveCondition, 150 );
92
93 return true;
94}
95
96
97void SCH_MOVE_TOOL::orthoLineDrag( SCH_COMMIT* aCommit, SCH_LINE* line, const VECTOR2I& splitDelta,
98 int& xBendCount, int& yBendCount, const EE_GRID_HELPER& grid )
99{
100 // If the move is not the same angle as this move, then we need to do something special with
101 // the unselected end to maintain orthogonality. Either drag some connected line that is the
102 // same angle as the move or add two lines to make a 90 degree connection
103 if( !EDA_ANGLE( splitDelta ).IsParallelTo( line->Angle() ) || line->GetLength() == 0 )
104 {
105 VECTOR2I unselectedEnd = line->HasFlag( STARTPOINT ) ? line->GetEndPoint()
106 : line->GetStartPoint();
107 VECTOR2I selectedEnd = line->HasFlag( STARTPOINT ) ? line->GetStartPoint()
108 : line->GetEndPoint();
109
110 // Look for pre-existing lines we can drag with us instead of creating new ones
111 bool foundAttachment = false;
112 bool foundJunction = false;
113 bool foundPin = false;
114 SCH_LINE* foundLine = nullptr;
115
116 for( EDA_ITEM* cItem : m_lineConnectionCache[line] )
117 {
118 foundAttachment = true;
119
120 // If the move is the same angle as a connected line, we can shrink/extend that line
121 // endpoint
122 switch( cItem->Type() )
123 {
124 case SCH_LINE_T:
125 {
126 SCH_LINE* cLine = static_cast<SCH_LINE*>( cItem );
127
128 // A matching angle on a non-zero-length line means lengthen/shorten will work
129 if( EDA_ANGLE( splitDelta ).IsParallelTo( cLine->Angle() )
130 && cLine->GetLength() != 0 )
131 {
132 foundLine = cLine;
133 }
134
135 // Zero length lines are lines that this algorithm has shortened to 0 so they also
136 // work but we should prefer using a segment with length and angle matching when
137 // we can (otherwise the zero length line will draw overlapping segments on them)
138 if( !foundLine && cLine->GetLength() == 0 )
139 foundLine = cLine;
140
141 break;
142 }
143 case SCH_JUNCTION_T:
144 foundJunction = true;
145 break;
146
147 case SCH_PIN_T:
148 foundPin = true;
149 break;
150
151 case SCH_SHEET_T:
152 for( const auto& pair : m_specialCaseSheetPins )
153 {
154 if( pair.first->IsConnected( selectedEnd ) )
155 {
156 foundPin = true;
157 break;
158 }
159 }
160
161 break;
162
163 default:
164 break;
165 }
166 }
167
168 // Ok... what if our original line is length zero from moving in its direction, and the
169 // last added segment of the 90 bend we are connected to is zero from moving it in its
170 // direction after it was added?
171 //
172 // If we are moving in original direction, we should lengthen the original drag wire.
173 // Otherwise we should lengthen the new wire.
174 bool preferOriginalLine = false;
175
176 if( foundLine
177 && foundLine->GetLength() == 0
178 && line->GetLength() == 0
179 && EDA_ANGLE( splitDelta ).IsParallelTo( line->GetStoredAngle() ) )
180 {
181 preferOriginalLine = true;
182 }
183 // If we have found an attachment, but not a line, we want to check if it's a junction.
184 // These are special-cased and get a single line added instead of a 90-degree bend. Except
185 // when we're on a pin, because pins always need bends, and junctions are just added to
186 // pins for visual clarity.
187 else if( !foundLine && foundJunction && !foundPin )
188 {
189 // Create a new wire ending at the unselected end
190 foundLine = new SCH_LINE( unselectedEnd, line->GetLayer() );
191 foundLine->SetFlags( IS_NEW );
192 foundLine->SetLastResolvedState( line );
193 m_frame->AddToScreen( foundLine, m_frame->GetScreen() );
194 m_newDragLines.insert( foundLine );
195
196 // We just broke off of the existing items, so replace all of them with our new
197 // end connection.
199 m_lineConnectionCache[line].clear();
200 m_lineConnectionCache[line].emplace_back( foundLine );
201 }
202
203 // We want to drag our found line if it's in the same angle as the move or zero length,
204 // but if the original drag line is also zero and the same original angle we should extend
205 // that one first
206 if( foundLine && !preferOriginalLine )
207 {
208 // Move the connected line found oriented in the direction of our move.
209 //
210 // Make sure we grab the right endpoint, it's not always STARTPOINT since the user can
211 // draw a box of lines. We need to only move one though, and preferably the start point,
212 // in case we have a zero length line that we are extending (we want the foundLine
213 // start point to be attached to the unselected end of our drag line).
214 //
215 // Also, new lines are added already so they'll be in the undo list, skip adding them.
216
217 if( !foundLine->HasFlag( IS_CHANGED ) && !foundLine->HasFlag( IS_NEW ) )
218 {
219 aCommit->Modify( (SCH_ITEM*) foundLine, m_frame->GetScreen() );
220
221 if( !foundLine->IsSelected() )
222 m_changedDragLines.insert( foundLine );
223 }
224
225 if( foundLine->GetStartPoint() == unselectedEnd )
226 foundLine->MoveStart( splitDelta );
227 else if( foundLine->GetEndPoint() == unselectedEnd )
228 foundLine->MoveEnd( splitDelta );
229
230 updateItem( foundLine, true );
231
232 SCH_LINE* bendLine = nullptr;
233
234 if( m_lineConnectionCache.count( foundLine ) == 1
235 && m_lineConnectionCache[foundLine][0]->Type() == SCH_LINE_T )
236 {
237 bendLine = static_cast<SCH_LINE*>( m_lineConnectionCache[foundLine][0] );
238 }
239
240 // Remerge segments we've created if this is a segment that we've added whose only
241 // other connection is also an added segment
242 //
243 // bendLine is first added segment at the original attachment point, foundLine is the
244 // orthogonal line between bendLine and this line
245 if( foundLine->HasFlag( IS_NEW )
246 && foundLine->GetLength() == 0
247 && bendLine && bendLine->HasFlag( IS_NEW ) )
248 {
249 if( line->HasFlag( STARTPOINT ) )
250 line->SetEndPoint( bendLine->GetEndPoint() );
251 else
252 line->SetStartPoint( bendLine->GetEndPoint() );
253
254 // Update our cache of the connected items.
255
256 // First, re-attach our drag labels to the original line being re-merged.
257 for( EDA_ITEM* candidate : m_lineConnectionCache[bendLine] )
258 {
259 SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( candidate );
260
261 if( label && m_specialCaseLabels.count( label ) )
262 m_specialCaseLabels[label].attachedLine = line;
263 }
264
266 m_lineConnectionCache[bendLine].clear();
267 m_lineConnectionCache[foundLine].clear();
268
269 m_frame->RemoveFromScreen( bendLine, m_frame->GetScreen() );
270 m_frame->RemoveFromScreen( foundLine, m_frame->GetScreen() );
271
272 m_newDragLines.erase( bendLine );
273 m_newDragLines.erase( foundLine );
274
275 delete bendLine;
276 delete foundLine;
277 }
278 //Ok, move the unselected end of our item
279 else
280 {
281 if( line->HasFlag( STARTPOINT ) )
282 line->MoveEnd( splitDelta );
283 else
284 line->MoveStart( splitDelta );
285 }
286
287 updateItem( line, true );
288 }
289 else if( line->GetLength() == 0 )
290 {
291 // We didn't find another line to shorten/lengthen, (or we did but it's also zero)
292 // so now is a good time to use our existing zero-length original line
293 }
294 // Either no line was at the "right" angle, or this was a junction, pin, sheet, etc. We
295 // need to add segments to keep the soon-to-move unselected end connected to these items.
296 //
297 // To keep our drag selections all the same, we'll move our unselected end point and then
298 // put wires between it and its original endpoint.
299 else if( foundAttachment && line->IsOrthogonal() )
300 {
301 VECTOR2D lineGrid = grid.GetGridSize( grid.GetItemGrid( line ) );
302
303 // The bend counter handles a group of wires all needing their offset one grid movement
304 // further out from each other to not overlap. The absolute value stuff finds the
305 // direction of the line and hence the the bend increment on that axis
306 unsigned int xMoveBit = splitDelta.x != 0;
307 unsigned int yMoveBit = splitDelta.y != 0;
308 int xLength = abs( unselectedEnd.x - selectedEnd.x );
309 int yLength = abs( unselectedEnd.y - selectedEnd.y );
310 int xMove = ( xLength - ( xBendCount * lineGrid.x ) )
311 * sign( selectedEnd.x - unselectedEnd.x );
312 int yMove = ( yLength - ( yBendCount * lineGrid.y ) )
313 * sign( selectedEnd.y - unselectedEnd.y );
314
315 // Create a new wire ending at the unselected end, we'll move the new wire's start
316 // point to the unselected end
317 SCH_LINE* a = new SCH_LINE( unselectedEnd, line->GetLayer() );
318 a->MoveStart( VECTOR2I( xMove, yMove ) );
319 a->SetFlags( IS_NEW );
320 a->SetConnectivityDirty( true );
321 a->SetLastResolvedState( line );
322 m_frame->AddToScreen( a, m_frame->GetScreen() );
323 m_newDragLines.insert( a );
324
325 SCH_LINE* b = new SCH_LINE( a->GetStartPoint(), line->GetLayer() );
326 b->MoveStart( VECTOR2I( splitDelta.x, splitDelta.y ) );
327 b->SetFlags( IS_NEW | STARTPOINT );
328 b->SetConnectivityDirty( true );
329 b->SetLastResolvedState( line );
330 m_frame->AddToScreen( b, m_frame->GetScreen() );
331 m_newDragLines.insert( b );
332
333 xBendCount += yMoveBit;
334 yBendCount += xMoveBit;
335
336 // Ok move the unselected end of our item
337 if( line->HasFlag( STARTPOINT ) )
338 {
339 line->MoveEnd( VECTOR2I( splitDelta.x ? splitDelta.x : xMove,
340 splitDelta.y ? splitDelta.y : yMove ) );
341 }
342 else
343 {
344 line->MoveStart( VECTOR2I( splitDelta.x ? splitDelta.x : xMove,
345 splitDelta.y ? splitDelta.y : yMove ) );
346 }
347
348 // Update our cache of the connected items. First, attach our drag labels to the line
349 // left behind.
350 for( EDA_ITEM* candidate : m_lineConnectionCache[line] )
351 {
352 SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( candidate );
353
354 if( label && m_specialCaseLabels.count( label ) )
355 m_specialCaseLabels[label].attachedLine = a;
356 }
357
358 // We just broke off of the existing items, so replace all of them with our new end
359 // connection.
361 m_lineConnectionCache[b].emplace_back( a );
362 m_lineConnectionCache[line].clear();
363 m_lineConnectionCache[line].emplace_back( b );
364 }
365 // Original line has no attachments, just move the unselected end
366 else if( !foundAttachment )
367 {
368 if( line->HasFlag( STARTPOINT ) )
369 line->MoveEnd( splitDelta );
370 else
371 line->MoveStart( splitDelta );
372 }
373 }
374}
375
376
377int SCH_MOVE_TOOL::Main( const TOOL_EVENT& aEvent )
378{
380
381 if( SCH_COMMIT* commit = dynamic_cast<SCH_COMMIT*>( aEvent.Commit() ) )
382 {
383 bool isSlice = false;
384
385 if( m_isDrag )
386 isSlice = aEvent.Parameter<bool>();
387
388 wxCHECK( aEvent.SynchronousState(), 0 );
389 aEvent.SynchronousState()->store( STS_RUNNING );
390
391 if( doMoveSelection( aEvent, commit, isSlice ) )
392 aEvent.SynchronousState()->store( STS_FINISHED );
393 else
394 aEvent.SynchronousState()->store( STS_CANCELLED );
395 }
396 else
397 {
398 SCH_COMMIT localCommit( m_toolMgr );
399
400 if( doMoveSelection( aEvent, &localCommit, false ) )
401 localCommit.Push( m_isDrag ? _( "Drag" ) : _( "Move" ) );
402 else
403 localCommit.Revert();
404 }
405
406 return 0;
407}
408
409
410bool SCH_MOVE_TOOL::doMoveSelection( const TOOL_EVENT& aEvent, SCH_COMMIT* aCommit, bool aIsSlice )
411{
414 bool wasDragging = m_moveInProgress && m_isDrag;
415 bool isLineModeConstrained = false;
416
418 isLineModeConstrained = cfg->m_Drawing.line_mode != LINE_MODE::LINE_MODE_FREE;
419
420 m_anchorPos.reset();
421
422 if( m_moveInProgress )
423 {
424 if( m_isDrag != wasDragging )
425 {
426 EDA_ITEM* sel = m_selectionTool->GetSelection().Front();
427
428 if( sel && !sel->IsNew() )
429 {
430 // Reset the selected items so we can start again with the current m_isDrag
431 // state.
432 aCommit->Revert();
433
434 m_selectionTool->RemoveItemsFromSel( &m_dragAdditions, QUIET_MODE );
436 m_moveInProgress = false;
437 controls->SetAutoPan( false );
438
439 // And give it a kick so it doesn't have to wait for the first mouse movement
440 // to refresh.
441 m_toolMgr->PostAction( SCH_ACTIONS::restartMove );
442 }
443 }
444 else
445 {
446 // The tool hotkey is interpreted as a click when already dragging/moving
447 m_toolMgr->PostAction( ACTIONS::cursorClick );
448 }
449
450 return false;
451 }
452
453 if( m_inMoveTool ) // Must come after m_moveInProgress checks above...
454 return false;
455
457
458 SCH_SELECTION& userSelection = m_selectionTool->GetSelection();
459
460 // If a single pin is selected, promote the move selection to its parent symbol
461 if( userSelection.GetSize() == 1 )
462 {
463 EDA_ITEM* selItem = userSelection.Front();
464
465 if( selItem->Type() == SCH_PIN_T )
466 {
467 EDA_ITEM* parent = selItem->GetParent();
468
469 if( parent->Type() == SCH_SYMBOL_T )
470 {
471 m_selectionTool->ClearSelection();
472 m_selectionTool->AddItemToSel( parent );
473 }
474 }
475 }
476
477 // Be sure that there is at least one item that we can move. If there's no selection try
478 // looking for the stuff under mouse cursor (i.e. Kicad old-style hover selection).
479 SCH_SELECTION& selection = m_selectionTool->RequestSelection( SCH_COLLECTOR::MovableItems,
480 true );
481 bool unselect = selection.IsHover();
482
483 // Keep an original copy of the starting points for cleanup after the move
484 std::vector<DANGLING_END_ITEM> internalPoints;
485
486 Activate();
487
488 // Must be done after Activate() so that it gets set into the correct context
489 controls->ShowCursor( true );
490
491 m_frame->PushTool( aEvent );
492
493 if( selection.Empty() )
494 {
495 // Note that it's important to go through push/pop even when the selection is empty.
496 // This keeps other tools from having to special-case an empty move.
497 m_frame->PopTool( aEvent );
498 return false;
499 }
500
501 bool restore_state = false;
502 TOOL_EVENT copy = aEvent;
503 TOOL_EVENT* evt = &copy;
504 VECTOR2I prevPos;
506 SCH_SHEET* hoverSheet = nullptr;
507
508 m_cursor = controls->GetCursorPosition();
509
510 // Main loop: keep receiving events
511 do
512 {
513 m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::MOVING );
514 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
515 grid.SetUseGrid( getView()->GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
516
518 || evt->IsAction( &SCH_ACTIONS::move )
519 || evt->IsAction( &SCH_ACTIONS::drag )
520 || evt->IsMotion()
521 || evt->IsDrag( BUT_LEFT )
523 {
524 if( !m_moveInProgress ) // Prepare to start moving/dragging
525 {
526 SCH_ITEM* sch_item = (SCH_ITEM*) selection.Front();
527 bool placingNewItems = sch_item && sch_item->IsNew();
528
529 //------------------------------------------------------------------------
530 // Setup a drag or a move
531 //
532 m_dragAdditions.clear();
533 m_specialCaseLabels.clear();
535 internalPoints.clear();
537
538 for( SCH_ITEM* it : m_frame->GetScreen()->Items() )
539 {
540 it->ClearFlags( SELECTED_BY_DRAG );
541
542 if( !it->IsSelected() )
543 it->ClearFlags( STARTPOINT | ENDPOINT );
544 }
545
546 // Drag of split items start over top of their other segment so
547 // we want to skip grabbing the segments we split from
548 if( m_isDrag && !aIsSlice )
549 {
550 EDA_ITEMS connectedDragItems;
551
552 // Add connections to the selection for a drag.
553 // Do all non-labels/entries first so we don't add junctions to drag
554 // when the line will eventually be drag selected.
555 std::vector<SCH_ITEM*> stageTwo;
556
557 for( EDA_ITEM* edaItem : selection )
558 {
559 SCH_ITEM* item = static_cast<SCH_ITEM*>( edaItem );
560 std::vector<VECTOR2I> connections;
561
562 switch( item->Type() )
563 {
564 case SCH_LABEL_T:
565 case SCH_HIER_LABEL_T:
568 stageTwo.emplace_back(item);
569 break;
570
571 case SCH_LINE_T:
572 static_cast<SCH_LINE*>( item )->GetSelectedPoints( connections );
573 break;
574 default:
575 connections = item->GetConnectionPoints();
576 }
577
578 for( const VECTOR2I& point : connections )
579 getConnectedDragItems( aCommit, item, point, connectedDragItems );
580 }
581
582 // Go back and get all label connections now that we can test for drag-selected
583 // lines the labels might be on
584 for( SCH_ITEM* item : stageTwo )
585 {
586 for( const VECTOR2I& point : item->GetConnectionPoints() )
587 getConnectedDragItems( aCommit, item, point, connectedDragItems );
588 }
589
590 for( EDA_ITEM* item : connectedDragItems )
591 {
592 m_dragAdditions.push_back( item->m_Uuid );
593 m_selectionTool->AddItemToSel( item, QUIET_MODE );
594 }
595
596 // Pre-cache all connections of our selected objects so we can keep track of
597 // what they were originally connected to as we drag them around
598 for( EDA_ITEM* edaItem : selection )
599 {
600 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( edaItem );
601
602 if( schItem->Type() == SCH_LINE_T )
603 {
604 SCH_LINE* line = static_cast<SCH_LINE*>( schItem );
605
606 //Also store the original angle of the line, is needed later to decide
607 //which segment to extend when they've become zero length
608 line->StoreAngle();
609
610 for( const VECTOR2I& point : line->GetConnectionPoints() )
611 getConnectedItems( line, point, m_lineConnectionCache[line] );
612 }
613 }
614 }
615 else
616 {
617 // Mark the edges of the block with dangling flags for a move.
618 for( EDA_ITEM* item : selection )
619 static_cast<SCH_ITEM*>( item )->GetEndPoints( internalPoints );
620
621 std::vector<DANGLING_END_ITEM> endPointsByType = internalPoints;
622 std::vector<DANGLING_END_ITEM> endPointsByPos = endPointsByType;
623 DANGLING_END_ITEM_HELPER::sort_dangling_end_items( endPointsByType, endPointsByPos );
624
625 for( EDA_ITEM* item : selection )
626 static_cast<SCH_ITEM*>( item )->UpdateDanglingState( endPointsByType, endPointsByPos );
627 }
628
629 // Hide junctions connected to line endpoints that are not selected
630 m_hiddenJunctions.clear();
631
632 for( EDA_ITEM* edaItem : selection )
633 {
634 if( edaItem->Type() != SCH_LINE_T )
635 continue;
636
637 SCH_LINE* line = static_cast<SCH_LINE*>( edaItem );
638
639 for( const VECTOR2I& pt : line->GetConnectionPoints() )
640 {
641 SCH_JUNCTION* jct = static_cast<SCH_JUNCTION*>( m_frame->GetScreen()->GetItem( pt, 0, SCH_JUNCTION_T ) );
642
643 if( jct && !jct->IsSelected()
644 && std::find( m_hiddenJunctions.begin(), m_hiddenJunctions.end(), jct ) == m_hiddenJunctions.end() )
645 {
646 jct->SetFlags( STRUCT_DELETED );
647 m_frame->RemoveFromScreen( jct, m_frame->GetScreen() );
648 aCommit->Removed( jct, m_frame->GetScreen() );
649 }
650 }
651 }
652
653 // Generic setup
654 snapLayer = grid.GetSelectionGrid( selection );
655
656 for( EDA_ITEM* item : selection )
657 {
658 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( item );
659
660 if( schItem->IsNew() )
661 {
662 // Item was added to commit in a previous command
663
664 // While SCH_COMMIT::Push() will add any new items to the entered group,
665 // we need to do it earlier so that the previews while moving are correct.
666 if( SCH_GROUP* enteredGroup = m_selectionTool->GetEnteredGroup() )
667 {
668 if( schItem->IsGroupableType() && !schItem->GetParentGroup() )
669 {
670 aCommit->Modify( enteredGroup, m_frame->GetScreen(), RECURSE_MODE::NO_RECURSE );
671 enteredGroup->AddItem( schItem );
672 }
673 }
674 }
675 else if( schItem->GetParent() && schItem->GetParent()->IsSelected() )
676 {
677 // Item will be (or has been) added to commit by parent
678 }
679 else
680 {
681 aCommit->Modify( schItem, m_frame->GetScreen(), RECURSE_MODE::RECURSE );
682 }
683
684 schItem->SetFlags( IS_MOVING );
685
686 if( SCH_SHAPE* shape = dynamic_cast<SCH_SHAPE*>( schItem ) )
687 {
688 shape->SetHatchingDirty();
689 shape->UpdateHatching();
690 }
691
692 schItem->RunOnChildren(
693 [&]( SCH_ITEM* schItem )
694 {
695 item->SetFlags( IS_MOVING );
696 },
698
699 schItem->SetStoredPos( schItem->GetPosition() );
700 }
701
702 // Set up the starting position and move/drag offset
703 //
704 m_cursor = controls->GetCursorPosition();
705
707 {
708 wxASSERT_MSG( m_anchorPos, "Should be already set from previous cmd" );
709 }
710 else if( placingNewItems )
711 {
712 m_anchorPos = selection.GetReferencePoint();
713 }
714
715 if( m_anchorPos )
716 {
717 VECTOR2I delta = m_cursor - (*m_anchorPos);
718 bool isPasted = false;
719
720 // Drag items to the current cursor position
721 for( EDA_ITEM* item : selection )
722 {
723 // Don't double move pins, fields, etc.
724 if( item->GetParent() && item->GetParent()->IsSelected() )
725 continue;
726
727 moveItem( item, delta );
728 updateItem( item, false );
729
730 isPasted |= ( item->GetFlags() & IS_PASTED ) != 0;
731 item->ClearFlags( IS_PASTED );
732 }
733
734 // The first time pasted items are moved we need to store the position of the
735 // cursor so that rotate while moving works as expected (instead of around the
736 // original anchor point
737 if( isPasted )
738 selection.SetReferencePoint( m_cursor );
739
741 }
742 // For some items, moving the cursor to anchor is not good (for instance large
743 // hierarchical sheets or symbols can have the anchor outside the view)
744 else if( selection.Size() == 1 && !sch_item->IsMovableFromAnchorPoint() )
745 {
748 }
749 else
750 {
751 if( m_frame->GetMoveWarpsCursor() )
752 {
753 // User wants to warp the mouse
754 m_cursor = grid.BestDragOrigin( m_cursor, snapLayer, selection );
755 selection.SetReferencePoint( m_cursor );
756 }
757 else
758 {
759 // User does not want to warp the mouse
761 }
762 }
763
764 controls->SetCursorPosition( m_cursor, false );
765
766 prevPos = m_cursor;
767 controls->SetAutoPan( true );
768 m_moveInProgress = true;
769 }
770
771 //------------------------------------------------------------------------
772 // Follow the mouse
773 //
774 m_view->ClearPreview();
775
776 m_cursor = grid.BestSnapAnchor( controls->GetCursorPosition( false ),
777 snapLayer, selection );
778 // Determine potential target sheet.
779 SCH_SHEET* sheet = dynamic_cast<SCH_SHEET*>( m_frame->GetScreen()->GetItem( m_cursor, 0,
780 SCH_SHEET_T ) );
781 if( sheet && sheet->IsSelected() )
782 sheet = nullptr; // Never target a selected sheet
783
784 if( !sheet )
785 {
786 // Build current selection bounding box in its (already moved) position.
787 BOX2I selBBox;
788 for( EDA_ITEM* it : selection )
789 {
790 if( SCH_ITEM* schIt = dynamic_cast<SCH_ITEM*>( it ) )
791 selBBox.Merge( schIt->GetBoundingBox() );
792 }
793
794 if( selBBox.GetWidth() > 0 && selBBox.GetHeight() > 0 )
795 {
796 VECTOR2I selCenter( selBBox.GetX() + selBBox.GetWidth() / 2,
797 selBBox.GetY() + selBBox.GetHeight() / 2 );
798
799 // Find first non-selected sheet whose body fully contains the selection
800 // or at least contains its center point.
801 for( SCH_ITEM* it : m_frame->GetScreen()->Items().OfType( SCH_SHEET_T ) )
802 {
803 SCH_SHEET* candidate = static_cast<SCH_SHEET*>( it );
804 if( candidate->IsSelected() || candidate->IsRootSheet() )
805 continue;
806
807 BOX2I body = candidate->GetBodyBoundingBox();
808
809 if( body.Contains( selBBox ) || body.Contains( selCenter ) )
810 {
811 sheet = candidate;
812 break;
813 }
814 }
815 }
816 }
817
818 if( sheet != hoverSheet )
819 {
820 if( hoverSheet )
821 {
822 hoverSheet->ClearFlags( BRIGHTENED );
823 m_frame->UpdateItem( hoverSheet, false );
824 }
825
826 hoverSheet = sheet;
827
828 if( hoverSheet )
829 {
830 hoverSheet->SetFlags( BRIGHTENED );
831 m_frame->UpdateItem( hoverSheet, false );
832 }
833 }
834
835 m_frame->GetCanvas()->SetCurrentCursor( hoverSheet ? KICURSOR::PLACE
837 VECTOR2I delta( m_cursor - prevPos );
839
840 // We need to check if the movement will change the net offset direction on the
841 // X an Y axes. This is because we remerge added bend lines in realtime, and we
842 // also account for the direction of the move when adding bend lines. So, if the
843 // move direction changes, we need to split it into a move that gets us back to
844 // zero, then the rest of the move.
845 std::vector<VECTOR2I> splitMoves;
846
848 {
849 splitMoves.emplace_back( VECTOR2I( -1 * m_moveOffset.x, 0 ) );
850 splitMoves.emplace_back( VECTOR2I( delta.x + m_moveOffset.x, 0 ) );
851 }
852 else
853 {
854 splitMoves.emplace_back( VECTOR2I( delta.x, 0 ) );
855 }
856
858 {
859 splitMoves.emplace_back( VECTOR2I( 0, -1 * m_moveOffset.y ) );
860 splitMoves.emplace_back( VECTOR2I( 0, delta.y + m_moveOffset.y ) );
861 }
862 else
863 {
864 splitMoves.emplace_back( VECTOR2I( 0, delta.y ) );
865 }
866
867
869 prevPos = m_cursor;
870
871 // Used for tracking how far off a drag end should have its 90 degree elbow added
872 int xBendCount = 1;
873 int yBendCount = 1;
874
875 // Split the move into X and Y moves so we can correctly drag orthogonal lines
876 for( const VECTOR2I& splitDelta : splitMoves )
877 {
878 // Skip non-moves
879 if( splitDelta == VECTOR2I( 0, 0 ) )
880 continue;
881
882 for( EDA_ITEM* item : selection.GetItemsSortedByTypeAndXY( ( delta.x >= 0 ),
883 ( delta.y >= 0 ) ) )
884 {
885 // Don't double move pins, fields, etc.
886 if( item->GetParent() && item->GetParent()->IsSelected() )
887 continue;
888
889 SCH_LINE* line = dynamic_cast<SCH_LINE*>( item );
890
891 // Only partially selected drag lines in orthogonal line mode need special
892 // handling
893 if( m_isDrag && isLineModeConstrained
894 && line && line->HasFlag( STARTPOINT ) != line->HasFlag( ENDPOINT ) )
895 {
896 orthoLineDrag( aCommit, line, splitDelta, xBendCount, yBendCount, grid );
897 }
898
899 // Move all other items normally, including the selected end of partially
900 // selected lines
901 moveItem( item, splitDelta );
902 updateItem( item, false );
903
904 // Update any lines connected to sheet pins to the sheet pin's location
905 // (which may not exactly follow the splitDelta as the pins are constrained
906 // along the sheet edges.
907 for( const auto& [pin, lineEnd] : m_specialCaseSheetPins )
908 {
909 if( lineEnd.second && lineEnd.first->HasFlag( STARTPOINT ) )
910 lineEnd.first->SetStartPoint( pin->GetPosition() );
911 else if( !lineEnd.second && lineEnd.first->HasFlag( ENDPOINT ) )
912 lineEnd.first->SetEndPoint( pin->GetPosition() );
913 }
914 }
915 }
916
917 if( selection.HasReferencePoint() )
918 selection.SetReferencePoint( selection.GetReferencePoint() + delta );
919
920 std::vector<SCH_ITEM*> previewItems;
921
922 for( EDA_ITEM* it : selection )
923 previewItems.push_back( static_cast<SCH_ITEM*>( it ) );
924
925 for( SCH_LINE* line : m_newDragLines )
926 previewItems.push_back( line );
927
928 for( SCH_LINE* line : m_changedDragLines )
929 previewItems.push_back( line );
930
932 previewItems ) )
933 {
934 m_view->AddToPreview( jct, true );
935 }
936
938 }
939
940 //------------------------------------------------------------------------
941 // Handle cancel
942 //
943 else if( evt->IsCancelInteractive()
944 || evt->IsActivate()
945 || evt->IsAction( &ACTIONS::undo ) )
946 {
947 if( evt->IsCancelInteractive() )
948 m_frame->GetInfoBar()->Dismiss();
949
950 if( m_moveInProgress )
951 {
952 if( evt->IsActivate() )
953 {
954 // Allowing other tools to activate during a move runs the risk of race
955 // conditions in which we try to spool up both event loops at once.
956
957 if( m_isDrag )
958 m_frame->ShowInfoBarMsg( _( "Press <ESC> to cancel drag." ) );
959 else
960 m_frame->ShowInfoBarMsg( _( "Press <ESC> to cancel move." ) );
961
962 evt->SetPassEvent( false );
963 continue;
964 }
965
966 evt->SetPassEvent( false );
967 restore_state = true;
968 }
969
971
972 m_view->ClearPreview();
973
974 break;
975 }
976 //------------------------------------------------------------------------
977 // Handle TOOL_ACTION special cases
978 //
979 else if( evt->IsAction( &ACTIONS::doDelete ) )
980 {
981 evt->SetPassEvent();
982 // Exit on a delete; there will no longer be anything to drag.
983 break;
984 }
985 else if( evt->IsAction( &ACTIONS::duplicate )
987 || evt->IsAction( &ACTIONS::redo ) )
988 {
989 wxBell();
990 }
991 else if( evt->IsAction( &SCH_ACTIONS::rotateCW ) )
992 {
993 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::rotateCW, aCommit );
994 m_toolMgr->PostAction( ACTIONS::refreshPreview );
995 }
996 else if( evt->IsAction( &SCH_ACTIONS::rotateCCW ) )
997 {
998 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::rotateCCW, aCommit );
999 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1000 }
1001 else if( evt->IsAction( &ACTIONS::increment ) )
1002 {
1003 if( evt->HasParameter() )
1004 m_toolMgr->RunSynchronousAction( ACTIONS::increment, aCommit, evt->Parameter<ACTIONS::INCREMENT>() );
1005 else
1006 m_toolMgr->RunSynchronousAction( ACTIONS::increment, aCommit, ACTIONS::INCREMENT { 1, 0 } );
1007
1008 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1009 }
1010 else if( evt->IsAction( &SCH_ACTIONS::toDLabel ) )
1011 {
1012 m_toolMgr->RunSynchronousAction(SCH_ACTIONS::toDLabel, aCommit );
1013 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1014 }
1015 else if( evt->IsAction( &SCH_ACTIONS::toGLabel ) )
1016 {
1017 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toGLabel, aCommit );
1018 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1019 }
1020 else if( evt->IsAction( &SCH_ACTIONS::toHLabel ) )
1021 {
1022 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toHLabel, aCommit );
1023 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1024 }
1025 else if( evt->IsAction( &SCH_ACTIONS::toLabel ) )
1026 {
1027 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toLabel, aCommit );
1028 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1029 }
1030 else if( evt->IsAction( &SCH_ACTIONS::toText ) )
1031 {
1032 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toText, aCommit );
1033 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1034 }
1035 else if( evt->IsAction( &SCH_ACTIONS::toTextBox ) )
1036 {
1037 m_toolMgr->RunSynchronousAction( SCH_ACTIONS::toTextBox, aCommit );
1038 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1039 }
1040 else if( evt->Action() == TA_CHOICE_MENU_CHOICE )
1041 {
1044 {
1045 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( selection.Front() );
1046 int unit = *evt->GetCommandId() - ID_POPUP_SCH_SELECT_UNIT;
1047
1048 if( symbol )
1049 {
1050 m_frame->SelectUnit( symbol, unit );
1051 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1052 }
1053 }
1056 {
1057 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( selection.Front() );
1058 int bodyStyle = ( *evt->GetCommandId() - ID_POPUP_SCH_SELECT_BODY_STYLE ) + 1;
1059
1060 if( symbol && symbol->GetBodyStyle() != bodyStyle )
1061 {
1062 m_frame->SelectBodyStyle( symbol, bodyStyle );
1063 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1064 }
1065 }
1066 }
1067 else if( evt->IsAction( &SCH_ACTIONS::highlightNet )
1069 {
1070 // These don't make any sense during a move. Eat them.
1071 }
1072 //------------------------------------------------------------------------
1073 // Handle context menu
1074 //
1075 else if( evt->IsClick( BUT_RIGHT ) )
1076 {
1077 m_menu->ShowContextMenu( m_selectionTool->GetSelection() );
1078 }
1079 //------------------------------------------------------------------------
1080 // Handle drop
1081 //
1082 else if( evt->IsMouseUp( BUT_LEFT )
1083 || evt->IsClick( BUT_LEFT )
1084 || evt->IsDblClick( BUT_LEFT ) )
1085 {
1086 break; // Finish
1087 }
1088 else
1089 {
1090 evt->SetPassEvent();
1091 }
1092
1093 controls->SetAutoPan( m_moveInProgress );
1094
1095 } while( ( evt = Wait() ) ); //Should be assignment not equality test
1096
1097 SCH_SHEET* targetSheet = hoverSheet;
1098
1099 if( hoverSheet )
1100 {
1101 hoverSheet->ClearFlags( BRIGHTENED );
1102 m_frame->UpdateItem( hoverSheet, false );
1103 }
1104
1105 if( targetSheet )
1106 {
1107 moveSelectionToSheet( selection, targetSheet, aCommit );
1108 m_toolMgr->RunAction( ACTIONS::selectionClear );
1109 m_newDragLines.clear();
1110 m_changedDragLines.clear();
1111 }
1112
1113 // Create a selection of original selection, drag selected/changed items, and new
1114 // bend lines for later before we clear them in the aCommit. We'll need these
1115 // to check for new junctions needed, etc.
1116 SCH_SELECTION selectionCopy( selection );
1117
1118 for( SCH_LINE* line : m_newDragLines )
1119 selectionCopy.Add( line );
1120
1121 for( SCH_LINE* line : m_changedDragLines )
1122 selectionCopy.Add( line );
1123
1124 // Save whatever new bend lines and changed lines survived the drag
1125 for( SCH_LINE* newLine : m_newDragLines )
1126 {
1127 newLine->ClearEditFlags();
1128 aCommit->Added( newLine, m_frame->GetScreen() );
1129 }
1130
1131 // These lines have been changed, but aren't selected. We need
1132 // to manually clear these edit flags or they'll stick around.
1133 for( SCH_LINE* oldLine : m_changedDragLines )
1134 oldLine->ClearEditFlags();
1135
1136 m_newDragLines.clear();
1137 m_changedDragLines.clear();
1138
1139 controls->ForceCursorPosition( false );
1140 controls->ShowCursor( false );
1141 controls->SetAutoPan( false );
1142
1143 m_moveOffset = { 0, 0 };
1144 m_anchorPos.reset();
1145
1146 if( restore_state )
1147 {
1148 m_selectionTool->RemoveItemsFromSel( &m_dragAdditions, QUIET_MODE );
1149 }
1150 else
1151 {
1152 // One last update after exiting loop (for slower stuff, such as updating SCREEN's RTree).
1153 for( EDA_ITEM* item : selection )
1154 {
1155 updateItem( item, true );
1156
1157 if( SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( item ) )
1158 sch_item->SetConnectivityDirty( true );
1159 }
1160
1161 if( selection.GetSize() == 1 && selection.Front()->IsNew() )
1162 m_frame->SaveCopyForRepeatItem( static_cast<SCH_ITEM*>( selection.Front() ) );
1163
1164 m_selectionTool->RemoveItemsFromSel( &m_dragAdditions, QUIET_MODE );
1165
1167
1168 // If we move items away from a junction, we _may_ want to add a junction there
1169 // to denote the state.
1170 for( const DANGLING_END_ITEM& it : internalPoints )
1171 {
1172 if( m_frame->GetScreen()->IsExplicitJunctionNeeded( it.GetPosition()) )
1173 lwbTool->AddJunction( aCommit, m_frame->GetScreen(), it.GetPosition() );
1174 }
1175
1176 lwbTool->TrimOverLappingWires( aCommit, &selectionCopy );
1177 lwbTool->AddJunctionsIfNeeded( aCommit, &selectionCopy );
1178
1179 // This needs to run prior to `RecalculateConnections` because we need to identify
1180 // the lines that are newly dangling
1181 if( m_isDrag && !aIsSlice )
1182 trimDanglingLines( aCommit );
1183
1184 // Auto-rotate any moved labels
1185 for( EDA_ITEM* item : selection )
1186 m_frame->AutoRotateItem( m_frame->GetScreen(), static_cast<SCH_ITEM*>( item ) );
1187
1188 m_frame->Schematic().CleanUp( aCommit );
1189 }
1190
1191 for( EDA_ITEM* item : m_frame->GetScreen()->Items() )
1192 item->ClearEditFlags();
1193
1194 // ensure any selected item not in screen main list (for instance symbol fields)
1195 // has its edit flags cleared
1196 for( EDA_ITEM* item : selectionCopy )
1197 item->ClearEditFlags();
1198
1199 if( unselect )
1200 m_toolMgr->RunAction( ACTIONS::selectionClear );
1201 else
1202 m_selectionTool->RebuildSelection(); // Schematic cleanup might have merged lines, etc.
1203
1204 m_dragAdditions.clear();
1205 m_lineConnectionCache.clear();
1206 m_moveInProgress = false;
1207
1208 m_hiddenJunctions.clear();
1209 m_view->ClearPreview();
1210 m_frame->PopTool( aEvent );
1211
1212 return !restore_state;
1213}
1214
1215
1217 SCH_COMMIT* aCommit )
1218{
1219 SCH_SCREEN* destScreen = aTargetSheet->GetScreen();
1220 SCH_SCREEN* srcScreen = m_frame->GetScreen();
1221
1222 BOX2I bbox;
1223
1224 for( EDA_ITEM* item : aSelection )
1225 bbox.Merge( static_cast<SCH_ITEM*>( item )->GetBoundingBox() );
1226
1227 VECTOR2I offset = VECTOR2I( 0, 0 ) - bbox.GetPosition();
1228 int step = schIUScale.MilsToIU( 50 );
1229 bool overlap = false;
1230
1231 do
1232 {
1233 BOX2I moved = bbox;
1234 moved.Move( offset );
1235 overlap = false;
1236
1237 for( SCH_ITEM* existing : destScreen->Items() )
1238 {
1239 if( moved.Intersects( existing->GetBoundingBox() ) )
1240 {
1241 overlap = true;
1242 break;
1243 }
1244 }
1245
1246 if( overlap )
1247 offset += VECTOR2I( step, step );
1248 } while( overlap );
1249
1250 for( EDA_ITEM* item : aSelection )
1251 {
1252 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( item );
1253
1254 // Remove from current screen and view manually
1255 m_frame->RemoveFromScreen( schItem, srcScreen );
1256
1257 // Move the item
1258 schItem->Move( offset );
1259
1260 // Add to destination screen manually (won't add to view since it's not current)
1261 destScreen->Append( schItem );
1262
1263 // Record in commit with CHT_DONE flag to bypass automatic screen/view operations
1264 aCommit->Stage( schItem, CHT_REMOVE | CHT_DONE, srcScreen );
1265 aCommit->Stage( schItem, CHT_ADD | CHT_DONE, destScreen );
1266 }
1267}
1268
1269
1271{
1272 // Need a local cleanup first to ensure we remove unneeded junctions
1273 m_frame->Schematic().CleanUp( aCommit, m_frame->GetScreen() );
1274
1275 std::set<SCH_ITEM*> danglers;
1276
1277 std::function<void( SCH_ITEM* )> changeHandler =
1278 [&]( SCH_ITEM* aChangedItem ) -> void
1279 {
1280 m_toolMgr->GetView()->Update( aChangedItem, KIGFX::REPAINT );
1281
1282 // Delete newly dangling lines:
1283 // Find split segments (one segment is new, the other is changed) that
1284 // we aren't dragging and don't have selected
1285 if( aChangedItem->HasFlag( IS_BROKEN) && aChangedItem->IsDangling()
1286 && !aChangedItem->IsSelected() )
1287 {
1288 danglers.insert( aChangedItem );
1289 }
1290 };
1291
1292 m_frame->GetScreen()->TestDanglingEnds( nullptr, &changeHandler );
1293
1294 for( SCH_ITEM* line : danglers )
1295 {
1296 line->SetFlags( STRUCT_DELETED );
1297 aCommit->Removed( line, m_frame->GetScreen() );
1298 updateItem( line, false ); // Update any cached visuals before commit processes
1299 m_frame->RemoveFromScreen( line, m_frame->GetScreen() );
1300 }
1301}
1302
1303
1304void SCH_MOVE_TOOL::getConnectedItems( SCH_ITEM* aOriginalItem, const VECTOR2I& aPoint,
1305 EDA_ITEMS& aList )
1306{
1307 EE_RTREE& items = m_frame->GetScreen()->Items();
1308 EE_RTREE::EE_TYPE itemsOverlapping = items.Overlapping( aOriginalItem->GetBoundingBox() );
1309 SCH_ITEM* foundJunction = nullptr;
1310 SCH_ITEM* foundSymbol = nullptr;
1311
1312 // If you're connected to a junction, you're only connected to the junction.
1313 //
1314 // But, if you're connected to a junction on a pin, you're only connected to the pin. This
1315 // is because junctions and pins have different logic for how bend lines are generated and
1316 // we need to prioritize the pin version in some cases.
1317 for( SCH_ITEM* item : itemsOverlapping )
1318 {
1319 if( item != aOriginalItem && item->IsConnected( aPoint ) )
1320 {
1321 if( item->Type() == SCH_JUNCTION_T )
1322 foundJunction = item;
1323 else if( item->Type() == SCH_SYMBOL_T )
1324 foundSymbol = item;
1325 }
1326 }
1327
1328 if( foundSymbol && foundJunction )
1329 {
1330 aList.push_back( foundSymbol );
1331 return;
1332 }
1333
1334 if( foundJunction )
1335 {
1336 aList.push_back( foundJunction );
1337 return;
1338 }
1339
1340
1341 for( SCH_ITEM* test : itemsOverlapping )
1342 {
1343 if( test == aOriginalItem || !test->CanConnect( aOriginalItem ) )
1344 continue;
1345
1346 switch( test->Type() )
1347 {
1348 case SCH_LINE_T:
1349 {
1350 SCH_LINE* line = static_cast<SCH_LINE*>( test );
1351
1352 // When getting lines for the connection cache, it's important that we only add
1353 // items at the unselected end, since that is the only end that is handled specially.
1354 // Fully selected lines, and the selected end of a partially selected line, are moved
1355 // around normally and don't care about their connections.
1356 if( ( line->HasFlag( STARTPOINT ) && aPoint == line->GetStartPoint() )
1357 || ( line->HasFlag( ENDPOINT ) && aPoint == line->GetEndPoint() ) )
1358 {
1359 continue;
1360 }
1361
1362 if( test->IsConnected( aPoint ) )
1363 aList.push_back( test );
1364
1365 // Labels can connect to a wire (or bus) anywhere along the length
1366 if( SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( aOriginalItem ) )
1367 {
1368 if( static_cast<SCH_LINE*>( test )->HitTest( label->GetPosition(), 1 ) )
1369 aList.push_back( test );
1370 }
1371
1372 break;
1373 }
1374
1375 case SCH_SHEET_T:
1376 if( aOriginalItem->Type() == SCH_LINE_T )
1377 {
1378 SCH_LINE* line = static_cast<SCH_LINE*>( aOriginalItem );
1379
1380 for( SCH_SHEET_PIN* pin : static_cast<SCH_SHEET*>( test )->GetPins() )
1381 {
1382 if( pin->IsConnected( aPoint ) )
1383 {
1384 if( pin->IsSelected() )
1385 m_specialCaseSheetPins[pin] = { line, line->GetStartPoint() == aPoint };
1386
1387 aList.push_back( pin );
1388 }
1389 }
1390 }
1391
1392 break;
1393
1394 case SCH_SYMBOL_T:
1395 case SCH_JUNCTION_T:
1396 case SCH_NO_CONNECT_T:
1397 if( test->IsConnected( aPoint ) )
1398 aList.push_back( test );
1399
1400 break;
1401
1402 case SCH_LABEL_T:
1403 case SCH_GLOBAL_LABEL_T:
1404 case SCH_HIER_LABEL_T:
1406 // Labels can connect to a wire (or bus) anywhere along the length
1407 if( aOriginalItem->Type() == SCH_LINE_T && test->CanConnect( aOriginalItem ) )
1408 {
1409 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( test );
1410 SCH_LINE* line = static_cast<SCH_LINE*>( aOriginalItem );
1411
1412 if( line->HitTest( label->GetPosition(), 1 ) )
1413 aList.push_back( label );
1414 }
1415
1416 break;
1417
1420 if( aOriginalItem->Type() == SCH_LINE_T && test->CanConnect( aOriginalItem ) )
1421 {
1422 SCH_TEXT* label = static_cast<SCH_TEXT*>( test );
1423 SCH_LINE* line = static_cast<SCH_LINE*>( aOriginalItem );
1424
1425 if( line->HitTest( aPoint, 1 ) )
1426 aList.push_back( label );
1427 }
1428
1429 break;
1430
1431 default:
1432 break;
1433 }
1434 }
1435}
1436
1437
1439 const VECTOR2I& aPoint, EDA_ITEMS& aList )
1440{
1441 EE_RTREE& items = m_frame->GetScreen()->Items();
1442 EE_RTREE::EE_TYPE itemsOverlappingRTree = items.Overlapping( aSelectedItem->GetBoundingBox() );
1443 std::vector<SCH_ITEM*> itemsConnectable;
1444 bool ptHasUnselectedJunction = false;
1445
1446 auto makeNewWire =
1447 [this]( SCH_COMMIT* commit, SCH_ITEM* fixed, SCH_ITEM* selected, const VECTOR2I& start,
1448 const VECTOR2I& end )
1449 {
1450 SCH_LINE* newWire;
1451
1452 // Add a new newWire between the fixed item and the selected item so the selected
1453 // item can be dragged.
1454 if( fixed->GetLayer() == LAYER_BUS_JUNCTION || fixed->GetLayer() == LAYER_BUS
1455 || selected->GetLayer() == LAYER_BUS )
1456 {
1457 newWire = new SCH_LINE( start, LAYER_BUS );
1458 }
1459 else
1460 {
1461 newWire = new SCH_LINE( start, LAYER_WIRE );
1462 }
1463
1464 newWire->SetFlags( IS_NEW );
1465 newWire->SetConnectivityDirty( true );
1466
1467 if( dynamic_cast<const SCH_LINE*>( selected ) )
1468 newWire->SetLastResolvedState( selected );
1469 else if( dynamic_cast<const SCH_LINE*>( fixed ) )
1470 newWire->SetLastResolvedState( fixed );
1471
1472 newWire->SetEndPoint( end );
1473 m_frame->AddToScreen( newWire, m_frame->GetScreen() );
1474 commit->Added( newWire, m_frame->GetScreen() );
1475
1476 return newWire;
1477 };
1478
1479 auto makeNewJunction =
1480 [this]( SCH_COMMIT* commit, SCH_LINE* line, const VECTOR2I& pt )
1481 {
1482 SCH_JUNCTION* junction = new SCH_JUNCTION( pt );
1483 junction->SetFlags( IS_NEW );
1484 junction->SetConnectivityDirty( true );
1485 junction->SetLastResolvedState( line );
1486
1487 if( line->IsBus() )
1488 junction->SetLayer( LAYER_BUS_JUNCTION );
1489
1490 m_frame->AddToScreen( junction, m_frame->GetScreen() );
1491 commit->Added( junction, m_frame->GetScreen() );
1492
1493 return junction;
1494 };
1495
1496 for( SCH_ITEM* item : itemsOverlappingRTree )
1497 {
1498 if( item->Type() == SCH_SHEET_T )
1499 {
1500 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
1501
1502 for( SCH_SHEET_PIN* pin : sheet->GetPins() )
1503 {
1504 if( !pin->IsSelected()
1505 && pin->GetPosition() == aSelectedItem->GetPosition()
1506 && pin->CanConnect( aSelectedItem ) )
1507 {
1508 itemsConnectable.push_back( pin );
1509 }
1510 }
1511
1512 continue;
1513 }
1514
1515 // Skip ourselves, skip already selected items (but not lines, they need both ends tested)
1516 // and skip unconnectable items
1517 if( item == aSelectedItem
1518 || ( item->Type() != SCH_LINE_T && item->IsSelected() )
1519 || !item->CanConnect( aSelectedItem ) )
1520 {
1521 continue;
1522 }
1523
1524 itemsConnectable.push_back( item );
1525 }
1526
1527 for( SCH_ITEM* item : itemsConnectable )
1528 {
1529 if( item->Type() == SCH_JUNCTION_T && item->IsConnected( aPoint ) && !item->IsSelected() )
1530 {
1531 ptHasUnselectedJunction = true;
1532 break;
1533 }
1534 }
1535
1536 SCH_LINE* newWire = nullptr;
1537
1538 for( SCH_ITEM* test : itemsConnectable )
1539 {
1540 KICAD_T testType = test->Type();
1541
1542 switch( testType )
1543 {
1544 case SCH_LINE_T:
1545 {
1546 // Select the connected end of wires/bus connections that don't have an unselected
1547 // junction isolating them from the drag
1548 if( ptHasUnselectedJunction )
1549 break;
1550
1551 SCH_LINE* line = static_cast<SCH_LINE*>( test );
1552
1553 if( line->GetStartPoint() == aPoint )
1554 {
1555 // It's possible to manually select one end of a line and get a drag
1556 // connected other end, so we set the flag and then early exit the loop
1557 // later if the other drag items like labels attached to the line have
1558 // already been grabbed during the partial selection process.
1559 line->SetFlags( STARTPOINT );
1560
1561 if( line->HasFlag( SELECTED ) || line->HasFlag( SELECTED_BY_DRAG ) )
1562 {
1563 continue;
1564 }
1565 else
1566 {
1567 line->SetFlags( SELECTED_BY_DRAG );
1568 aList.push_back( line );
1569 }
1570 }
1571 else if( line->GetEndPoint() == aPoint )
1572 {
1573 line->SetFlags( ENDPOINT );
1574
1575 if( line->HasFlag( SELECTED ) || line->HasFlag( SELECTED_BY_DRAG ) )
1576 {
1577 continue;
1578 }
1579 else
1580 {
1581 line->SetFlags( SELECTED_BY_DRAG );
1582 aList.push_back( line );
1583 }
1584 }
1585 else
1586 {
1587 switch( aSelectedItem->Type() )
1588 {
1589 // These items can connect anywhere along a line
1592 case SCH_LABEL_T:
1593 case SCH_HIER_LABEL_T:
1594 case SCH_GLOBAL_LABEL_T:
1596 // Only add a line if this line is unselected; if the label and line are both
1597 // selected they'll move together
1598 if( line->HitTest( aPoint, 1 ) && !line->HasFlag( SELECTED )
1599 && !line->HasFlag( SELECTED_BY_DRAG ) )
1600 {
1601 newWire = makeNewWire( aCommit, line, aSelectedItem, aPoint, aPoint );
1602 newWire->SetFlags( SELECTED_BY_DRAG | STARTPOINT );
1603 newWire->StoreAngle( ( line->Angle() + ANGLE_90 ).Normalize() );
1604 aList.push_back( newWire );
1605
1606 if( aPoint != line->GetStartPoint() && aPoint != line->GetEndPoint() )
1607 {
1608 // Split line in half
1609 aCommit->Modify( line, m_frame->GetScreen() );
1610
1611 VECTOR2I oldEnd = line->GetEndPoint();
1612 line->SetEndPoint( aPoint );
1613
1614 makeNewWire( aCommit, line, line, aPoint, oldEnd );
1615 makeNewJunction( aCommit, line, aPoint );
1616 }
1617 else
1618 {
1619 m_lineConnectionCache[ newWire ] = { line };
1620 m_lineConnectionCache[ line ] = { newWire };
1621 }
1622 }
1623 break;
1624
1625 default:
1626 break;
1627 }
1628
1629 break;
1630 }
1631
1632 // Since only one end is going to move, the movement vector of any labels attached to
1633 // it is scaled by the proportion of the line length the label is from the moving end.
1634 for( SCH_ITEM* item : items.Overlapping( line->GetBoundingBox() ) )
1635 {
1636 SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( item );
1637
1638 if( !label || label->IsSelected() )
1639 continue; // These will be moved on their own because they're selected
1640
1641 if( label->HasFlag( SELECTED_BY_DRAG ) )
1642 continue;
1643
1644 if( label->CanConnect( line ) && line->HitTest( label->GetPosition(), 1 ) )
1645 {
1646 label->SetFlags( SELECTED_BY_DRAG );
1647 aList.push_back( label );
1648
1650 info.attachedLine = line;
1651 info.originalLabelPos = label->GetPosition();
1652 m_specialCaseLabels[label] = info;
1653 }
1654 }
1655
1656 break;
1657 }
1658
1659 case SCH_SHEET_T:
1660 for( SCH_SHEET_PIN* pin : static_cast<SCH_SHEET*>( test )->GetPins() )
1661 {
1662 if( pin->IsConnected( aPoint ) )
1663 {
1664 if( pin->IsSelected() && aSelectedItem->Type() == SCH_LINE_T )
1665 {
1666 SCH_LINE* line = static_cast<SCH_LINE*>( aSelectedItem );
1667 m_specialCaseSheetPins[ pin ] = { line, line->GetStartPoint() == aPoint };
1668 }
1669 else if( !newWire )
1670 {
1671 // Add a new wire between the sheetpin and the selected item so the
1672 // selected item can be dragged.
1673 newWire = makeNewWire( aCommit, pin, aSelectedItem, aPoint, aPoint );
1674 newWire->SetFlags( SELECTED_BY_DRAG | STARTPOINT );
1675 aList.push_back( newWire );
1676 }
1677 }
1678 }
1679
1680 break;
1681
1682 case SCH_SYMBOL_T:
1683 case SCH_JUNCTION_T:
1684 if( test->IsConnected( aPoint ) && !newWire )
1685 {
1686 // Add a new wire between the symbol or junction and the selected item so
1687 // the selected item can be dragged.
1688 newWire = makeNewWire( aCommit, test, aSelectedItem, aPoint, aPoint );
1689 newWire->SetFlags( SELECTED_BY_DRAG | STARTPOINT );
1690 aList.push_back( newWire );
1691 }
1692
1693 break;
1694
1695 case SCH_NO_CONNECT_T:
1696 // Select no-connects that are connected to items being moved.
1697 if( !test->HasFlag( SELECTED_BY_DRAG ) && test->IsConnected( aPoint ) )
1698 {
1699 aList.push_back( test );
1700 test->SetFlags( SELECTED_BY_DRAG );
1701 }
1702
1703 break;
1704
1705 case SCH_LABEL_T:
1706 case SCH_GLOBAL_LABEL_T:
1707 case SCH_HIER_LABEL_T:
1709 case SCH_SHEET_PIN_T:
1710 // Performance optimization:
1711 if( test->HasFlag( SELECTED_BY_DRAG ) )
1712 break;
1713
1714 // Select labels that are connected to a wire (or bus) being moved.
1715 if( aSelectedItem->Type() == SCH_LINE_T && test->CanConnect( aSelectedItem ) )
1716 {
1717 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( test );
1718 SCH_LINE* line = static_cast<SCH_LINE*>( aSelectedItem );
1719
1720 bool oneEndFixed = !line->HasFlag( STARTPOINT ) || !line->HasFlag( ENDPOINT );
1721
1722 if( line->HitTest( label->GetTextPos(), 1 ) )
1723 {
1724 if( ( !line->HasFlag( STARTPOINT ) && label->GetPosition() == line->GetStartPoint() )
1725 || ( !line->HasFlag( ENDPOINT ) && label->GetPosition() == line->GetEndPoint() ) )
1726 {
1727 //If we have a line selected at only one end, don't grab labels
1728 //connected directly to the unselected endpoint
1729 break;
1730 }
1731 else
1732 {
1733 label->SetFlags( SELECTED_BY_DRAG );
1734 aList.push_back( label );
1735
1736 if( oneEndFixed )
1737 {
1739 info.attachedLine = line;
1740 info.originalLabelPos = label->GetPosition();
1741 m_specialCaseLabels[label] = info;
1742 }
1743 }
1744 }
1745 }
1746 else if( test->IsConnected( aPoint ) && !newWire )
1747 {
1748 // Add a new wire between the label and the selected item so the selected item
1749 // can be dragged.
1750 newWire = makeNewWire( aCommit, test, aSelectedItem, aPoint, aPoint );
1751 newWire->SetFlags( SELECTED_BY_DRAG | STARTPOINT );
1752 aList.push_back( newWire );
1753 }
1754
1755 break;
1756
1759 // Performance optimization:
1760 if( test->HasFlag( SELECTED_BY_DRAG ) )
1761 break;
1762
1763 // Select bus entries that are connected to a bus being moved.
1764 if( aSelectedItem->Type() == SCH_LINE_T && test->CanConnect( aSelectedItem ) )
1765 {
1766 SCH_LINE* line = static_cast<SCH_LINE*>( aSelectedItem );
1767
1768 if( ( !line->HasFlag( STARTPOINT ) && test->IsConnected( line->GetStartPoint() ) )
1769 || ( !line->HasFlag( ENDPOINT ) && test->IsConnected( line->GetEndPoint() ) ) )
1770 {
1771 // If we have a line selected at only one end, don't grab bus entries
1772 // connected directly to the unselected endpoint
1773 continue;
1774 }
1775
1776 for( VECTOR2I& point : test->GetConnectionPoints() )
1777 {
1778 if( line->HitTest( point, 1 ) )
1779 {
1780 test->SetFlags( SELECTED_BY_DRAG );
1781 aList.push_back( test );
1782
1783 // A bus entry needs its wire & label as well
1784 std::vector<VECTOR2I> ends = test->GetConnectionPoints();
1785 VECTOR2I otherEnd;
1786
1787 if( ends[0] == point )
1788 otherEnd = ends[1];
1789 else
1790 otherEnd = ends[0];
1791
1792 getConnectedDragItems( aCommit, test, otherEnd, aList );
1793
1794 // No need to test the other end of the bus entry
1795 break;
1796 }
1797 }
1798 }
1799
1800 break;
1801
1802 default:
1803 break;
1804 }
1805 }
1806}
1807
1808
1809void SCH_MOVE_TOOL::moveItem( EDA_ITEM* aItem, const VECTOR2I& aDelta )
1810{
1811 switch( aItem->Type() )
1812 {
1813 case SCH_LINE_T:
1814 {
1815 SCH_LINE* line = static_cast<SCH_LINE*>( aItem );
1816
1817 if( aItem->HasFlag( STARTPOINT ) || !m_isDrag )
1818 line->MoveStart( aDelta );
1819
1820 if( aItem->HasFlag( ENDPOINT ) || !m_isDrag )
1821 line->MoveEnd( aDelta );
1822
1823 break;
1824 }
1825
1826 case SCH_PIN_T:
1827 case SCH_FIELD_T:
1828 {
1829 SCH_ITEM* parent = (SCH_ITEM*) aItem->GetParent();
1830 VECTOR2I delta( aDelta );
1831
1832 if( parent && parent->Type() == SCH_SYMBOL_T )
1833 {
1834 SCH_SYMBOL* symbol = (SCH_SYMBOL*) aItem->GetParent();
1835 TRANSFORM transform = symbol->GetTransform().InverseTransform();
1836
1837 delta = transform.TransformCoordinate( delta );
1838 }
1839
1840 static_cast<SCH_ITEM*>( aItem )->Move( delta );
1841
1842 // If we're moving a field with respect to its parent then it's no longer auto-placed
1843 if( aItem->Type() == SCH_FIELD_T && parent && !parent->IsSelected() )
1845
1846 break;
1847 }
1848
1849 case SCH_SHEET_PIN_T:
1850 {
1851 SCH_SHEET_PIN* pin = (SCH_SHEET_PIN*) aItem;
1852
1853 pin->SetStoredPos( pin->GetStoredPos() + aDelta );
1854 pin->ConstrainOnEdge( pin->GetStoredPos(), true );
1855 break;
1856 }
1857
1858 case SCH_LABEL_T:
1860 case SCH_GLOBAL_LABEL_T:
1861 case SCH_HIER_LABEL_T:
1862 {
1863 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( aItem );
1864
1865 if( m_specialCaseLabels.count( label ) )
1866 {
1868 SEG currentLine( info.attachedLine->GetStartPoint(), info.attachedLine->GetEndPoint() );
1869 label->SetPosition( currentLine.NearestPoint( info.originalLabelPos ) );
1870 }
1871 else
1872 {
1873 label->Move( aDelta );
1874 }
1875
1876 break;
1877 }
1878
1879 default:
1880 static_cast<SCH_ITEM*>( aItem )->Move( aDelta );
1881 break;
1882 }
1883
1884 aItem->SetFlags( IS_MOVING );
1885}
1886
1887
1889{
1891 SCH_SELECTION& selection = m_selectionTool->RequestSelection( SCH_COLLECTOR::MovableItems );
1892 GRID_HELPER_GRIDS selectionGrid = grid.GetSelectionGrid( selection );
1893 SCH_COMMIT commit( m_toolMgr );
1894
1895 auto doMoveItem =
1896 [&]( EDA_ITEM* item, const VECTOR2I& delta )
1897 {
1898 commit.Modify( item, m_frame->GetScreen(), RECURSE_MODE::RECURSE );
1899
1900 // Ensure only one end is moved when calling moveItem
1901 // i.e. we are in drag mode
1902 bool tmp_isDrag = m_isDrag;
1903 m_isDrag = true;
1904 moveItem( item, delta );
1905 m_isDrag = tmp_isDrag;
1906
1907 item->ClearFlags( IS_MOVING );
1908 updateItem( item, true );
1909 };
1910
1911 for( SCH_ITEM* it : m_frame->GetScreen()->Items() )
1912 {
1913 if( !it->IsSelected() )
1914 it->ClearFlags( STARTPOINT | ENDPOINT );
1915
1916 if( !selection.IsHover() && it->IsSelected() )
1917 it->SetFlags( STARTPOINT | ENDPOINT );
1918
1919 it->SetStoredPos( it->GetPosition() );
1920
1921 if( it->Type() == SCH_SHEET_T )
1922 {
1923 for( SCH_SHEET_PIN* pin : static_cast<SCH_SHEET*>( it )->GetPins() )
1924 pin->SetStoredPos( pin->GetPosition() );
1925 }
1926 }
1927
1928 for( EDA_ITEM* item : selection )
1929 {
1930 if( item->Type() == SCH_LINE_T )
1931 {
1932 SCH_LINE* line = static_cast<SCH_LINE*>( item );
1933 std::vector<int> flags{ STARTPOINT, ENDPOINT };
1934 std::vector<VECTOR2I> pts{ line->GetStartPoint(), line->GetEndPoint() };
1935
1936 for( int ii = 0; ii < 2; ++ii )
1937 {
1938 EDA_ITEMS drag_items{ item };
1939 line->ClearFlags();
1940 line->SetFlags( SELECTED );
1941 line->SetFlags( flags[ii] );
1942 getConnectedDragItems( &commit, line, pts[ii], drag_items );
1943 std::set<EDA_ITEM*> unique_items( drag_items.begin(), drag_items.end() );
1944
1945 VECTOR2I delta = grid.AlignGrid( pts[ii], selectionGrid ) - pts[ii];
1946
1947 if( delta != VECTOR2I( 0, 0 ) )
1948 {
1949 for( EDA_ITEM* dragItem : unique_items )
1950 {
1951 if( dragItem->GetParent() && dragItem->GetParent()->IsSelected() )
1952 continue;
1953
1954 doMoveItem( dragItem, delta );
1955 }
1956 }
1957 }
1958 }
1959 else if( item->Type() == SCH_FIELD_T || item->Type() == SCH_TEXT_T )
1960 {
1961 VECTOR2I delta = grid.AlignGrid( item->GetPosition(), selectionGrid ) - item->GetPosition();
1962
1963 if( delta != VECTOR2I( 0, 0 ) )
1964 doMoveItem( item, delta );
1965 }
1966 else if( item->Type() == SCH_SHEET_T )
1967 {
1968 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
1969 VECTOR2I topLeft = sheet->GetPosition();
1970 VECTOR2I bottomRight = topLeft + sheet->GetSize();
1971 VECTOR2I tl_delta = grid.AlignGrid( topLeft, selectionGrid ) - topLeft;
1972 VECTOR2I br_delta = grid.AlignGrid( bottomRight, selectionGrid ) - bottomRight;
1973
1974 if( tl_delta != VECTOR2I( 0, 0 ) || br_delta != VECTOR2I( 0, 0 ) )
1975 {
1976 doMoveItem( sheet, tl_delta );
1977
1978 VECTOR2I newSize = (VECTOR2I) sheet->GetSize() - tl_delta + br_delta;
1979 sheet->SetSize( VECTOR2I( newSize.x, newSize.y ) );
1980 updateItem( sheet, true );
1981 }
1982
1983 for( SCH_SHEET_PIN* pin : sheet->GetPins() )
1984 {
1985 VECTOR2I newPos;
1986
1987 if( pin->GetSide() == SHEET_SIDE::TOP || pin->GetSide() == SHEET_SIDE::LEFT )
1988 newPos = pin->GetPosition() + tl_delta;
1989 else
1990 newPos = pin->GetPosition() + br_delta;
1991
1992 VECTOR2I delta = grid.AlignGrid( newPos - pin->GetPosition(), selectionGrid );
1993
1994 if( delta != VECTOR2I( 0, 0 ) )
1995 {
1996 EDA_ITEMS drag_items;
1997 getConnectedDragItems( &commit, pin, pin->GetConnectionPoints()[0],
1998 drag_items );
1999
2000 doMoveItem( pin, delta );
2001
2002 for( EDA_ITEM* dragItem : drag_items )
2003 {
2004 if( dragItem->GetParent() && dragItem->GetParent()->IsSelected() )
2005 continue;
2006
2007 doMoveItem( dragItem, delta );
2008 }
2009 }
2010 }
2011 }
2012 else
2013 {
2014 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( item );
2015 std::vector<VECTOR2I> connections = schItem->GetConnectionPoints();
2016 EDA_ITEMS drag_items;
2017
2018 for( const VECTOR2I& point : connections )
2019 getConnectedDragItems( &commit, schItem, point, drag_items );
2020
2021 std::map<VECTOR2I, int> shifts;
2022 VECTOR2I most_common( 0, 0 );
2023 int max_count = 0;
2024
2025 for( const VECTOR2I& conn : connections )
2026 {
2027 VECTOR2I gridpt = grid.AlignGrid( conn, selectionGrid ) - conn;
2028
2029 shifts[gridpt]++;
2030
2031 if( shifts[gridpt] > max_count )
2032 {
2033 most_common = gridpt;
2034 max_count = shifts[most_common];
2035 }
2036 }
2037
2038 if( most_common != VECTOR2I( 0, 0 ) )
2039 {
2040 doMoveItem( item, most_common );
2041
2042 for( EDA_ITEM* dragItem : drag_items )
2043 {
2044 if( dragItem->GetParent() && dragItem->GetParent()->IsSelected() )
2045 continue;
2046
2047 doMoveItem( dragItem, most_common );
2048 }
2049 }
2050 }
2051 }
2052
2054 lwbTool->TrimOverLappingWires( &commit, &selection );
2055 lwbTool->AddJunctionsIfNeeded( &commit, &selection );
2056
2058
2059 m_frame->Schematic().CleanUp( &commit );
2060 commit.Push( _( "Align Items to Grid" ) );
2061 return 0;
2062}
2063
2064
2066{
2067 // Remove new bend lines added during the drag
2068 for( SCH_LINE* newLine : m_newDragLines )
2069 {
2070 m_frame->RemoveFromScreen( newLine, m_frame->GetScreen() );
2071 delete newLine;
2072 }
2073
2074 m_newDragLines.clear();
2075}
2076
2077
2084
2085
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:114
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
static TOOL_ACTION undo
Definition actions.h:75
static TOOL_ACTION duplicate
Definition actions.h:84
static TOOL_ACTION doDelete
Definition actions.h:85
static TOOL_ACTION cursorClick
Definition actions.h:179
static TOOL_ACTION redo
Definition actions.h:76
static TOOL_ACTION increment
Definition actions.h:94
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:223
static TOOL_ACTION refreshPreview
Definition actions.h:158
constexpr const Vec & GetPosition() const
Definition box2.h:211
constexpr coord_type GetY() const
Definition box2.h:208
constexpr size_type GetWidth() const
Definition box2.h:214
constexpr coord_type GetX() const
Definition box2.h:207
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:658
constexpr size_type GetHeight() const
Definition box2.h:215
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:168
COMMIT & Added(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Notify observers that aItem has been added.
Definition commit.h:84
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:106
COMMIT & Removed(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Definition commit.h:96
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Add a menu entry to run a TOOL_ACTION on selected items.
static void sort_dangling_end_items(std::vector< DANGLING_END_ITEM > &aItemListByType, std::vector< DANGLING_END_ITEM > &aItemListByPos)
Both contain the same information.
Definition sch_item.cpp:796
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:272
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:110
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:142
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:144
bool IsSelected() const
Definition eda_item.h:127
EDA_ITEM * GetParent() const
Definition eda_item.h:112
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition eda_item.h:146
bool IsNew() const
Definition eda_item.h:124
const VECTOR2I & GetTextPos() const
Definition eda_text.h:272
Implement an R-tree for fast spatial and type indexing of schematic items.
Definition sch_rtree.h:40
EE_TYPE Overlapping(const BOX2I &aRect) const
Definition sch_rtree.h:246
static const TOOL_EVENT SelectedItemsMoved
Used to inform tools that the selection should temporarily be non-editable.
Definition actions.h:356
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.
static TOOL_ACTION rotateCCW
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 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.
Schematic editor (Eeschema) main window.
A set of SCH_ITEMs (i.e., without duplicates).
Definition sch_group.h:52
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:167
void SetStoredPos(const VECTOR2I &aPos)
Definition sch_item.h:280
virtual bool CanConnect(const SCH_ITEM *aItem) const
Definition sch_item.h:488
virtual void RunOnChildren(const std::function< void(SCH_ITEM *)> &aFunction, RECURSE_MODE aMode)
Definition sch_item.h:597
int GetBodyStyle() const
Definition sch_item.h:244
virtual void Move(const VECTOR2I &aMoveVector)
Move the item by aMoveVector to a new position.
Definition sch_item.h:365
void SetLayer(SCH_LAYER_ID aLayer)
Definition sch_item.h:310
void SetConnectivityDirty(bool aDirty=true)
Definition sch_item.h:556
void SetFieldsAutoplaced(AUTOPLACE_ALGO aAlgo)
Definition sch_item.h:593
bool IsConnected(const VECTOR2I &aPoint) const
Test the item to see if it is connected to aPoint.
Definition sch_item.cpp:314
virtual bool IsMovableFromAnchorPoint() const
Check if object is movable from the anchor point.
Definition sch_item.h:277
bool IsGroupableType() const
Definition sch_item.cpp:105
virtual std::vector< VECTOR2I > GetConnectionPoints() const
Add all the connection points for this item to aPoints.
Definition sch_item.h:508
void SetLastResolvedState(const SCH_ITEM *aItem) override
void Move(const VECTOR2I &aMoveVector) override
Move the item by aMoveVector to a new position.
void SetPosition(const VECTOR2I &aPosition) override
bool CanConnect(const SCH_ITEM *aItem) const override
Definition sch_label.h:148
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)
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:42
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:789
void StoreAngle()
Save the current line angle.
Definition sch_line.h:115
std::vector< VECTOR2I > GetConnectionPoints() const override
Add all the connection points for this item to aPoints.
Definition sch_line.cpp:689
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition sch_line.cpp:231
EDA_ANGLE Angle() const
Get the angle between the start and end lines.
Definition sch_line.h:104
VECTOR2I GetEndPoint() const
Definition sch_line.h:144
VECTOR2I GetStartPoint() const
Definition sch_line.h:139
void MoveEnd(const VECTOR2I &aMoveVector)
Definition sch_line.cpp:176
void SetLastResolvedState(const SCH_ITEM *aItem) override
Definition sch_line.h:155
void MoveStart(const VECTOR2I &aMoveVector)
Definition sch_line.cpp:170
double GetLength() const
Definition sch_line.cpp:247
void SetEndPoint(const VECTOR2I &aPosition)
Definition sch_line.h:145
void moveSelectionToSheet(SCH_SELECTION &aSelection, SCH_SHEET *aTarget, SCH_COMMIT *aCommit)
Clears the new drag lines and removes them from the screen.
bool Init() override
Init() is called once upon a registration of the tool.
VECTOR2I m_cursor
void trimDanglingLines(SCH_COMMIT *aCommit)
bool m_isDrag
Items (such as wires) which were added to the selection for a drag.
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.
bool doMoveSelection(const TOOL_EVENT &aEvent, SCH_COMMIT *aCommit, bool aIsSlice)
OPT_VECTOR2I m_anchorPos
int Main(const TOOL_EVENT &aEvent)
Run an interactive move of the selected items, or the item under the cursor.
std::vector< SCH_JUNCTION * > m_hiddenJunctions
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.
std::unordered_set< SCH_LINE * > m_changedDragLines
Junctions that were hidden during the move.
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.
void getConnectedDragItems(SCH_COMMIT *aCommit, SCH_ITEM *fixed, const VECTOR2I &selected, EDA_ITEMS &aList)
VECTOR2I m_moveOffset
Last cursor position (needed for getModificationPoint() to avoid changes of edit reference point).
std::map< SCH_LABEL_BASE *, SPECIAL_CASE_LABEL_INFO > m_specialCaseLabels
int AlignToGrid(const TOOL_EVENT &aEvent)
Align selected elements to the grid.
void clearNewDragLines()
Set up handlers for various events.
std::map< SCH_SHEET_PIN *, std::pair< SCH_LINE *, bool > > m_specialCaseSheetPins
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:117
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:47
bool IsRootSheet() const
void SetSize(const VECTOR2I &aSize)
Definition sch_sheet.h:119
VECTOR2I GetSize() const
Definition sch_sheet.h:118
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:116
VECTOR2I GetPosition() const override
Definition sch_sheet.h:415
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:187
Schematic symbol object.
Definition sch_symbol.h:75
VECTOR2I GetPosition() const override
Definition sch_text.h:141
void updateItem(EDA_ITEM *aItem, bool aUpdateRTree) const
bool Init() override
Init() is called once upon a registration of the tool.
SCH_TOOL_BASE(const std::string &aName)
SCH_SELECTION_TOOL * m_selectionTool
Definition seg.h:42
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition seg.cpp:604
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)
Definition selection.cpp:42
VECTOR2I GetReferencePoint() const
bool IsHover() const
Definition selection.h:89
virtual unsigned int GetSize() const override
Return the number of stored items.
Definition selection.h:105
EDA_ITEM * Front() const
Definition selection.h:177
void SetReferencePoint(const VECTOR2I &aP)
bool Empty() const
Checks if there is anything selected.
Definition selection.h:115
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:216
const TRANSFORM & GetTransform() const
Definition symbol.h:202
KIGFX::VIEW_CONTROLS * getViewControls() const
Definition tool_base.cpp:44
KIGFX::VIEW * getView() const
Definition tool_base.cpp:38
Generic, UI-independent tool event.
Definition tool_event.h:171
bool DisableGridSnapping() const
Definition tool_event.h:371
bool HasParameter() const
Definition tool_event.h:464
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:250
bool IsActivate() const
Definition tool_event.h:345
COMMIT * Commit() const
Definition tool_event.h:283
bool IsClick(int aButtonMask=BUT_ANY) const
bool IsDrag(int aButtonMask=BUT_ANY) const
Definition tool_event.h:315
int Modifier(int aMask=MD_MODIFIER_MASK) const
Return information about key modifiers state (Ctrl, Alt, etc.).
Definition tool_event.h:366
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:473
bool IsDblClick(int aButtonMask=BUT_ANY) const
std::atomic< SYNCRONOUS_TOOL_STATE > * SynchronousState() const
Definition tool_event.h:280
std::optional< int > GetCommandId() const
Definition tool_event.h:533
void SetPassEvent(bool aPass=true)
Definition tool_event.h:256
bool IsMouseUp(int aButtonMask=BUT_ANY) const
Definition tool_event.h:325
bool IsMotion() const
Definition tool_event.h:330
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:46
TRANSFORM InverseTransform() const
Calculate the Inverse mirror/rotation transform.
Definition transform.cpp:59
VECTOR2I TransformCoordinate(const VECTOR2I &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition transform.cpp:44
@ CHT_REMOVE
Definition commit.h:43
@ CHT_DONE
Flag to indicate the change is already applied.
Definition commit.h:47
@ CHT_ADD
Definition commit.h:42
@ PLACE
Definition cursors.h:96
@ MOVING
Definition cursors.h:48
#define _(s)
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:413
@ RECURSE
Definition eda_item.h:51
@ NO_RECURSE
Definition eda_item.h:52
std::vector< EDA_ITEM * > EDA_ITEMS
Define list of drawing items for screens.
Definition eda_item.h:566
#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:89
@ ID_POPUP_SCH_SELECT_BODY_STYLE
Definition eeschema_id.h:99
@ ID_POPUP_SCH_SELECT_BODY_STYLE_END
@ ID_POPUP_SCH_SELECT_UNIT_END
Definition eeschema_id.h:93
@ LINE_MODE_FREE
GRID_HELPER_GRIDS
Definition grid_helper.h:43
@ GRID_CURRENT
Definition grid_helper.h:45
@ LAYER_WIRE
Definition layer_ids.h:451
@ LAYER_BUS
Definition layer_ids.h:452
@ LAYER_BUS_JUNCTION
Definition layer_ids.h:496
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...
@ REPAINT
Item needs to be redrawn.
Definition view_item.h:58
bool signbit(T v)
Integral version of std::signbit that works all compilers.
Definition kicad_algo.h:176
see class PGM_BASE
Class to handle a set of SCH_ITEMs.
@ AUTOPLACE_NONE
Definition sch_item.h:69
#define QUIET_MODE
T * GetAppSettings(const char *aFilename)
The EE_TYPE struct provides a type-specific auto-range iterator to the RTree.
Definition sch_rtree.h:195
bool moved
VECTOR2I end
int delta
@ TA_CHOICE_MENU_CHOICE
Context menu choice.
Definition tool_event.h:98
@ MD_SHIFT
Definition tool_event.h:143
@ STS_CANCELLED
Definition tool_event.h:164
@ STS_FINISHED
Definition tool_event.h:163
@ STS_RUNNING
Definition tool_event.h:162
@ BUT_LEFT
Definition tool_event.h:132
@ BUT_RIGHT
Definition tool_event.h:133
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:78
@ SCH_LINE_T
Definition typeinfo.h:165
@ SCH_NO_CONNECT_T
Definition typeinfo.h:162
@ SCH_SYMBOL_T
Definition typeinfo.h:174
@ SCH_FIELD_T
Definition typeinfo.h:152
@ SCH_DIRECTIVE_LABEL_T
Definition typeinfo.h:173
@ SCH_LABEL_T
Definition typeinfo.h:169
@ SCH_SHEET_T
Definition typeinfo.h:177
@ SCH_HIER_LABEL_T
Definition typeinfo.h:171
@ SCH_BUS_BUS_ENTRY_T
Definition typeinfo.h:164
@ SCH_SHEET_PIN_T
Definition typeinfo.h:176
@ SCH_TEXT_T
Definition typeinfo.h:153
@ SCH_BUS_WIRE_ENTRY_T
Definition typeinfo.h:163
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:170
@ SCH_JUNCTION_T
Definition typeinfo.h:161
@ SCH_PIN_T
Definition typeinfo.h:155
constexpr int sign(T val)
Definition util.h:145
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694