KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_commit.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <sch_edit_frame.h>
21#include <symbol_edit_frame.h>
23#include <sch_view.h>
24#include <macros.h>
25#include <tool/actions.h>
26#include <tool/tool_manager.h>
27#include <tools/sch_tool_base.h>
28
29#include <lib_symbol.h>
30#include <advanced_config.h>
31
32#include <sch_group.h>
33#include <sch_screen.h>
34#include <schematic.h>
35
36#include <view/view.h>
37#include <sch_commit.h>
38#include <connection_graph.h>
39
40#include <functional>
41#include <memory>
42#include <set>
43#include <wx/log.h>
44
45
47 COMMIT(),
48 m_toolMgr( aToolMgr ),
49 m_isLibEditor( false )
50{
51 SCH_BASE_FRAME* frame = static_cast<SCH_BASE_FRAME*>( m_toolMgr->GetToolHolder() );
52 m_isLibEditor = frame && frame->IsType( FRAME_SCH_SYMBOL_EDITOR );
53}
54
55
56SCH_COMMIT::SCH_COMMIT( TOOL_MANAGER* aToolMgr, bool aIsLibEditor ) :
57 COMMIT(),
58 m_toolMgr( aToolMgr ),
59 m_isLibEditor( aIsLibEditor )
60{
61}
62
63
69
70
76
77
81
82
83COMMIT& SCH_COMMIT::Stage( EDA_ITEM *aItem, CHANGE_TYPE aChangeType, BASE_SCREEN *aScreen, RECURSE_MODE aRecurse )
84{
85 wxCHECK( aItem, *this );
86
87 if( !m_isLibEditor )
88 {
89 SCH_ITEM* item = dynamic_cast<SCH_ITEM*>( aItem );
90 SCH_SCREEN* screen = dynamic_cast<SCH_SCREEN*>( aScreen );
91
92 if( !screen && item )
93 screen = item->GetParentScreen();
94
95 if( screen && item && SCH_SCREEN::IsConnectivitySource( item ) )
96 screen->BumpConnectivityRevision( item->Type() );
97 }
98
99 if( aRecurse == RECURSE_MODE::RECURSE )
100 {
101 if( SCH_GROUP* group = dynamic_cast<SCH_GROUP*>( aItem ) )
102 {
103 for( EDA_ITEM* member : group->GetItems() )
104 Stage( member, aChangeType, aScreen, aRecurse );
105 }
106 }
107
108 // IS_SELECTED flag should not be set on undo items which were added for a drag operation.
109 if( aItem->IsSelected() && aItem->HasFlag( SELECTED_BY_DRAG ) )
110 {
111 aItem->ClearSelected();
112 COMMIT::Stage( aItem, aChangeType, aScreen );
113 aItem->SetSelected();
114 }
115 else
116 {
117 COMMIT::Stage( aItem, aChangeType, aScreen );
118 }
119
120 return *this;
121}
122
123
124COMMIT& SCH_COMMIT::Stage( std::vector<EDA_ITEM*> &container, CHANGE_TYPE aChangeType, BASE_SCREEN *aScreen )
125{
126 for( EDA_ITEM* item : container )
127 Stage( item, aChangeType, aScreen );
128
129 return *this;
130}
131
132
134{
135 wxASSERT( !aScreen->CheckIfOnDrawList( aItem ) );
136
137 if( COMMIT_LINE* entry = findEntry( aItem, aScreen ); entry && ( entry->m_type & CHT_TYPE ) == CHT_MODIFY )
138 {
139 // Staging REMOVE discards MODIFY, so preserve the original geometry on the removed item
140 // before its image is discarded. Cleanup must still skip it until the commit is processed.
141 aItem->SwapItemData( static_cast<SCH_ITEM*>( entry->m_copy ) );
142 aItem->SetFlags( STRUCT_DELETED );
143 }
144
145 Removed( aItem, aScreen );
146}
147
148
149void SCH_COMMIT::pushLibEdit( const wxString& aMessage, int aCommitFlags )
150{
151 // Symbol editor just saves copies of the whole symbol, so grab the first and discard the rest
152 LIB_SYMBOL* symbol = dynamic_cast<LIB_SYMBOL*>( m_entries.front().m_item );
153 LIB_SYMBOL* copy = dynamic_cast<LIB_SYMBOL*>( m_entries.front().m_copy );
154
155 if( symbol )
156 {
157 if( KIGFX::VIEW* view = m_toolMgr->GetView() )
158 {
159 view->Update( symbol );
160
161 symbol->RunOnChildren(
162 [&]( SCH_ITEM* aChild )
163 {
164 view->Update( aChild );
165 },
167 }
168
169 if( SYMBOL_EDIT_FRAME* frame = static_cast<SYMBOL_EDIT_FRAME*>( m_toolMgr->GetToolHolder() ) )
170 {
171 if( !( aCommitFlags & SKIP_UNDO ) )
172 {
173 if( copy )
174 {
175 frame->PushSymbolToUndoList( aMessage, copy );
176 copy = nullptr; // we've transferred ownership to the undo stack
177 }
178 }
179 }
180
181 if( copy )
182 {
183 // if no undo entry was needed, the copy would create a memory leak
184 delete copy;
185 copy = nullptr;
186 }
187 }
188
189 m_toolMgr->PostEvent( { TC_MESSAGE, TA_MODEL_CHANGE, AS_GLOBAL } );
191}
192
193
194void SCH_COMMIT::pushSchEdit( const wxString& aMessage, int aCommitFlags )
195{
196 // Objects potentially interested in changes:
197 PICKED_ITEMS_LIST undoList;
198 KIGFX::VIEW* view = m_toolMgr->GetView();
199
200 SCH_EDIT_FRAME* frame = static_cast<SCH_EDIT_FRAME*>( m_toolMgr->GetToolHolder() );
201 SCH_SCREEN* currentScreen = frame ? frame->GetScreen() : nullptr;
202 SCH_SELECTION_TOOL* selTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
203 SCH_GROUP* enteredGroup = selTool ? selTool->GetEnteredGroup() : nullptr;
204 bool itemsDeselected = false;
205 bool selectedModified = false;
206 bool dirtyConnectivity = false;
207 bool refreshConnectivity = false;
208 bool refreshHierarchy = false;
209 bool refSuffixChanged = false;
210 SCH_CLEANUP_FLAGS connectivityCleanUp = NO_CLEANUP;
211
212 if( Empty() )
213 {
214 return;
215 }
216
217 if( !frame )
218 aCommitFlags |= SKIP_UNDO;
219
220 undoList.SetDescription( aMessage );
221
222 SCHEMATIC* schematic = nullptr;
223 std::vector<SCH_ITEM*> bulkAddedItems;
224 std::vector<SCH_ITEM*> bulkRemovedItems;
225 std::vector<SCH_ITEM*> itemsChanged;
226 std::vector<std::unique_ptr<SCH_ITEM>> cleanupRemovedItems;
227 std::set<SCH_SCREEN*> connectivityScreens;
228
229 auto notifyModel =
230 [&]()
231 {
232 if( !schematic )
233 return;
234
235 if( !bulkAddedItems.empty() )
236 schematic->OnItemsAdded( bulkAddedItems );
237
238 if( !bulkRemovedItems.empty() )
239 schematic->OnItemsRemoved( bulkRemovedItems );
240
241 if( !itemsChanged.empty() )
242 schematic->OnItemsChanged( itemsChanged );
243
244 if( refreshHierarchy )
245 schematic->RefreshHierarchy();
246
247 bulkAddedItems.clear();
248 bulkRemovedItems.clear();
249 itemsChanged.clear();
250 };
251
252 auto updateConnectivityFlag =
253 [&]( SCH_ITEM* schItem, SCH_SCREEN* screen, bool fullSheetUpdate = true )
254 {
255 if( schItem->IsConnectable() || ( schItem->Type() == SCH_RULE_AREA_T ) )
256 {
257 dirtyConnectivity = true;
258 connectivityScreens.insert( screen );
259
260 // Do a local clean up if there are any connectable objects in the commit.
261 if( connectivityCleanUp == NO_CLEANUP )
262 connectivityCleanUp = LOCAL_CLEANUP;
263
264 if( schItem->Type() == SCH_SHEET_T && fullSheetUpdate )
265 connectivityCleanUp = GLOBAL_CLEANUP;
266 }
267 };
268
269 // We don't know that anything will be added to the entered group, but it does no harm to
270 // add it to the commit anyway (well, as long as we're not trying to remove it).
271 if( enteredGroup && GetStatus( enteredGroup ) == 0 )
272 Modify( enteredGroup, frame->GetScreen() );
273
274
275 // Handle wires with Hop Over shapes (view update only; skipped headless):
276 if( frame )
277 {
278 for( COMMIT_LINE& entry : m_entries )
279 {
280 SCH_ITEM* schCopyItem = dynamic_cast<SCH_ITEM*>( entry.m_copy );
281 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( entry.m_item );
282
283 if( schCopyItem && schCopyItem->Type() == SCH_LINE_T )
284 frame->UpdateHopOveredWires( schCopyItem );
285
286 if( schItem && schItem->Type() == SCH_LINE_T )
287 frame->UpdateHopOveredWires( schItem );
288 }
289 }
290
291 auto stageRemovedGroups =
292 [&]()
293 {
294 // Modify() appends to m_entries, so collect before staging group changes.
295 std::vector<std::pair<EDA_GROUP*, BASE_SCREEN*>> removedItemGroups;
296
297 for( COMMIT_LINE& entry : m_entries )
298 {
299 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( entry.m_item );
300 int changeType = entry.m_type & CHT_TYPE;
301
302 wxCHECK2( schItem, continue );
303
304 if( changeType == CHT_REMOVE && schItem->GetParentGroup() )
305 removedItemGroups.emplace_back( schItem->GetParentGroup(), entry.m_screen );
306 }
307
308 for( const auto& [group, screen] : removedItemGroups )
309 Modify( group->AsEdaItem(), screen );
310 };
311
312 stageRemovedGroups();
313 bool cleanupPending = true;
314 std::set<SCH_SCREEN*> touchedScreens;
315
316 for( size_t index = 0; ; )
317 {
318 if( index == m_entries.size() )
319 {
320 if( !cleanupPending || !dirtyConnectivity || ( aCommitFlags & SKIP_CONNECTIVITY ) || !schematic )
321 break;
322
323 cleanupPending = false;
324 notifyModel();
325
326 // Cleanup can replace a MODIFY with REMOVE. Keep its staging separate from processed
327 // entries while retaining their undo records, so both phases form one user action.
328 clear();
329 // Legacy cleanup stays schematic-wide; only the engine scopes it to the staged screens
330 schematic->CleanUpConnections( this, connectivityCleanUp,
331 ADVANCED_CFG::GetCfg().m_ConnectivityEngine ? connectivityScreens
332 : std::set<SCH_SCREEN*>() );
333
334 stageRemovedGroups();
335 index = 0;
336 continue;
337 }
338
339 COMMIT_LINE& entry = m_entries[index++];
340 int changeType = entry.m_type & CHT_TYPE;
341 int changeFlags = entry.m_type & CHT_FLAGS;
342 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( entry.m_item );
343 SCH_SCREEN* screen = dynamic_cast<SCH_SCREEN*>( entry.m_screen );
344
345 wxCHECK2( schItem, continue );
346 wxCHECK2( screen, continue );
347
348 touchedScreens.insert( screen );
349
350 if( !schematic )
351 schematic = schItem->Schematic();
352
353 // Staging invalidates captured sources even when the final connectivity is unchanged
354 if( ADVANCED_CFG::GetCfg().m_ConnectivityEngine && SCH_SCREEN::IsConnectivitySource( schItem ) )
355 refreshConnectivity = true;
356
357 if( schItem->IsSelected() )
358 {
359 selectedModified = true;
360 }
361 else
362 {
363 schItem->RunOnChildren(
364 [&selectedModified]( SCH_ITEM* aChild )
365 {
366 if( aChild->IsSelected() )
367 selectedModified = true;
368 },
370 }
371
372 switch( changeType )
373 {
374 case CHT_ADD:
375 {
376 if( enteredGroup && schItem->IsGroupableType() && !schItem->GetParentGroup() )
377 selTool->GetEnteredGroup()->AddItem( schItem );
378
379 updateConnectivityFlag( schItem, screen );
380
381 if( !( aCommitFlags & SKIP_UNDO ) )
382 undoList.PushItem( ITEM_PICKER( screen, schItem, UNDO_REDO::NEWITEM ) );
383
384 if( !( changeFlags & CHT_DONE ) )
385 {
386 if( !screen->CheckIfOnDrawList( schItem ) ) // don't want a loop!
387 screen->Append( schItem );
388
389 if( view && screen == currentScreen )
390 view->Add( schItem );
391 }
392
393 if( frame && screen == currentScreen )
394 frame->UpdateItem( schItem, true, true );
395 else if( screen )
396 screen->Update( schItem );
397
398 bulkAddedItems.push_back( schItem );
399
400 if( schItem->Type() == SCH_SHEET_T )
401 refreshHierarchy = true;
402
403 break;
404 }
405
406 case CHT_REMOVE:
407 {
408 updateConnectivityFlag( schItem, screen );
409
410 if( !( aCommitFlags & SKIP_UNDO ) )
411 {
412 ITEM_PICKER itemWrapper( screen, schItem, UNDO_REDO::DELETED );
413 itemWrapper.SetLink( entry.m_copy );
414 entry.m_copy = nullptr; // We've transferred ownership to the undo list
415 undoList.PushItem( itemWrapper );
416 }
417
418 if( schItem->IsSelected() )
419 {
420 if( selTool )
421 selTool->RemoveItemFromSel( schItem, true /* quiet mode */ );
422
423 // In an undo we're going to re-create from the item, not the image. That seems wrong, but
424 // it would be a big risky change to make it use the image, so let's just reset the selection
425 // flag for now (so that on undo it comes back selected).
426 schItem->SetFlags( SELECTED );
427
428 itemsDeselected = true;
429 }
430
431 if( schItem->Type() == SCH_FIELD_T )
432 {
433 static_cast<SCH_FIELD*>( schItem )->SetVisible( false );
434 break;
435 }
436
437 if( ( aCommitFlags & SKIP_UNDO ) && ( !cleanupPending || ( aCommitFlags & DELETE_REMOVED_ITEMS ) ) )
438 cleanupRemovedItems.emplace_back( schItem );
439
440 if( EDA_GROUP* group = schItem->GetParentGroup() )
441 group->RemoveItem( schItem );
442
443 if( !( changeFlags & CHT_DONE ) )
444 {
445 screen->Remove( schItem );
446
447 if( view && screen == currentScreen )
448 view->Remove( schItem );
449 }
450
451 if( frame && screen == currentScreen )
452 frame->UpdateItem( schItem, true, true );
453 else if( screen )
454 screen->Update( schItem );
455
456 if( schItem->Type() == SCH_SHEET_T )
457 refreshHierarchy = true;
458
459 bulkRemovedItems.push_back( schItem );
460 break;
461 }
462
463 case CHT_MODIFY:
464 {
465 const SCH_ITEM* itemCopy = static_cast<const SCH_ITEM*>( entry.m_copy );
466 SCH_SHEET_PATH currentSheet;
467 bool fullSheetUpdate = true;
468
469 if( frame )
470 currentSheet = frame->GetCurrentSheet();
471
472 if( schItem->Type() == SCH_SHEET_T )
473 {
474 const SCH_SHEET* modifiedSheet = static_cast<const SCH_SHEET*>( schItem );
475 const SCH_SHEET* originalSheet = static_cast<const SCH_SHEET*>( itemCopy );
476 const bool hierarchyChanged = originalSheet->HasHierarchyChanges( *modifiedSheet );
477
478 refreshHierarchy |= hierarchyChanged;
479
480 if( hierarchyChanged && originalSheet->HasRefSuffixChanges( *modifiedSheet ) )
481 {
482 static_cast<SCH_SHEET*>( schItem )->SetDescendantsModified();
483 refSuffixChanged = true;
484 }
485
486 // Stable ports are invalidated through their containing screen, including shared instances
487 fullSheetUpdate = !ADVANCED_CFG::GetCfg().m_ConnectivityEngine
488 || hierarchyChanged
489 || originalSheet->HasPinIdentityChanges( *modifiedSheet );
490 }
491
492 if( itemCopy->HasConnectivityChanges( schItem, &currentSheet )
493 || ( itemCopy->Type() == SCH_RULE_AREA_T ) )
494 {
495 updateConnectivityFlag( schItem, screen, fullSheetUpdate );
496 }
497 else if( schItem->IsConnectable() && schItem->IsConnectivityDirty() )
498 {
499 // Move previews can change dangling flags without changing the final geometry.
500 refreshConnectivity = true;
501 }
502
503 if( schItem->Type() == SCH_SYMBOL_T )
504 {
505 const SCH_SYMBOL* origSymbol = static_cast<const SCH_SYMBOL*>( itemCopy );
506 const SCH_SYMBOL* modSymbol = static_cast<const SCH_SYMBOL*>( schItem );
507
508 if( origSymbol->GetPins( &currentSheet ).size() != modSymbol->GetPins( &currentSheet ).size() )
509 connectivityCleanUp = GLOBAL_CLEANUP;
510
511 if( origSymbol->GetSchSymbolLibraryName() != modSymbol->GetSchSymbolLibraryName() )
512 screen->PruneUnusedLibSymbol( origSymbol->GetSchSymbolLibraryName() );
513 }
514
515 if( !( aCommitFlags & SKIP_UNDO ) )
516 {
517#if 0
518 // While this keeps us from marking documents modified when someone OK's a dialog with
519 // no changes, it depends on our various SCH_ITEM::operator=='s being bullet-proof. They
520 // currently aren't.
521 if( *itemCopy == *schItem )
522 {
523 // No actual changes made; short-circuit undo
524 delete entry.m_copy;
525 entry.m_copy = nullptr;
526 break;
527 }
528#endif
529
530 ITEM_PICKER itemWrapper( screen, schItem, UNDO_REDO::CHANGED );
531 itemWrapper.SetLink( entry.m_copy );
532 entry.m_copy = nullptr; // We've transferred ownership to the undo list
533 undoList.PushItem( itemWrapper );
534 }
535
536 if( frame && screen == currentScreen )
537 frame->UpdateItem( schItem, false, true );
538 else if( screen )
539 screen->Update( schItem );
540
541 itemsChanged.push_back( schItem );
542 break;
543 }
544
545 default:
546 wxASSERT( false );
547 break;
548 }
549
550 // Delete any copies we still have ownership of
551 delete entry.m_copy;
552 entry.m_copy = nullptr;
553
554 // Clear all flags but SELECTED and others used to move and rotate commands,
555 // after edition (selected items must keep their selection flag).
556 const int selected_mask = ( SELECTED | STARTPOINT | ENDPOINT );
557 schItem->ClearFlags( EDA_ITEM_ALL_FLAGS - selected_mask );
558
559 if( schItem->Type() == SCH_SHEET_T || schItem->Type() == SCH_SYMBOL_T )
560 {
561 schItem->RunOnChildren(
562 [&]( SCH_ITEM* child )
563 {
564 child->ClearFlags( EDA_ITEM_ALL_FLAGS - selected_mask );
565 },
567 }
568 }
569
570 notifyModel();
571
572 // Mark the touched screens dirty so that the state is tracked even in headless API server mode
573 if( !( aCommitFlags & SKIP_SET_DIRTY ) )
574 {
575 for( SCH_SCREEN* screen : touchedScreens )
576 screen->SetContentModified();
577 }
578
579 if( !( aCommitFlags & SKIP_UNDO ) && frame && undoList.GetCount() > 0 )
580 frame->SaveCopyInUndoList( undoList, UNDO_REDO::UNSPECIFIED, false );
581
582 cleanupRemovedItems.clear();
583
584 if( ( dirtyConnectivity || refreshConnectivity ) && !( aCommitFlags & SKIP_CONNECTIVITY ) )
585 {
586 wxLogTrace( wxS( "CONN_PROFILE" ), wxS( "SCH_COMMIT::pushSchEdit() connectivity refresh, cleanup=%d." ),
587 static_cast<int>( connectivityCleanUp ) );
588
589 if( frame )
590 {
591 frame->RecalculateConnections( this, connectivityCleanUp, nullptr, true );
592 }
593 else if( schematic )
594 {
595 schematic->RecalculateConnections( this, connectivityCleanUp, m_toolMgr, nullptr, nullptr, nullptr,
596 nullptr, true );
597 }
598 }
599
600 if( refreshHierarchy && frame )
602
603 // An API edit can change a suffix while one of its child sheets is on screen
604 if( refSuffixChanged && frame )
606
607 m_toolMgr->PostEvent( { TC_MESSAGE, TA_MODEL_CHANGE, AS_GLOBAL } );
608
609 if( itemsDeselected )
611
612 if( selectedModified )
614}
615
616
617void SCH_COMMIT::Push( const wxString& aMessage, int aCommitFlags )
618{
619 if( Empty() )
620 return;
621
622 if( m_isLibEditor )
623 pushLibEdit( aMessage, aCommitFlags );
624 else
625 pushSchEdit( aMessage, aCommitFlags );
626
627 if( SCH_BASE_FRAME* frame = static_cast<SCH_BASE_FRAME*>( m_toolMgr->GetToolHolder() ) )
628 {
629 if( !( aCommitFlags & SKIP_SET_DIRTY ) )
630 frame->OnModify();
631
632 if( frame && frame->GetCanvas() )
633 frame->GetCanvas()->Refresh();
634 }
635
636 clear();
637}
638
639
641{
642 EDA_ITEM* parent = aItem->GetParent();
643
644 if( m_isLibEditor )
645 {
646 // Headless commits operate directly on the staged item
647 if( SYMBOL_EDIT_FRAME* frame = static_cast<SYMBOL_EDIT_FRAME*>( m_toolMgr->GetToolHolder() ) )
648 return frame->GetCurSymbol();
649
650 return aItem;
651 }
652
653 if( parent && parent->IsType( { SCH_SYMBOL_T, SCH_TABLE_T, SCH_SHEET_T, SCH_LABEL_LOCATE_ANY_T } ) )
654 return parent;
655
656 return aItem;
657}
658
659
661{
662 if( m_isLibEditor )
663 {
664 SYMBOL_EDIT_FRAME* frame = static_cast<SYMBOL_EDIT_FRAME*>( m_toolMgr->GetToolHolder() );
665
666 if( frame && frame->GetCurSymbol() )
667 {
668 LIB_SYMBOL* symbol = frame->GetCurSymbol();
669 std::vector<KIID> selected;
670
671 // Cloning will clear the selected flags, but we want to keep them.
672 for( const SCH_ITEM& item : symbol->GetDrawItems() )
673 {
674 if( item.IsSelected() )
675 selected.push_back( item.m_Uuid );
676 }
677
678 symbol = new LIB_SYMBOL( *symbol );
679
680 // Restore selected flags.
681 for( SCH_ITEM& item : symbol->GetDrawItems() )
682 {
683 if( alg::contains( selected, item.m_Uuid ) )
684 item.SetSelected();
685 }
686
687 return symbol;
688 }
689 }
690
691 return aItem->Clone();
692}
693
694
696{
697 if( Empty() )
698 return;
699
700 // Symbol editor just saves copies of the whole symbol, so grab the first and discard the rest
701 SYMBOL_EDIT_FRAME* frame = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_toolMgr->GetToolHolder() );
702 LIB_SYMBOL* copy = dynamic_cast<LIB_SYMBOL*>( m_entries.front().m_copy );
703 SCH_SELECTION_TOOL* selTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
704
705 if( frame && copy )
706 {
707 frame->SetCurSymbol( copy, false );
708 m_toolMgr->ResetTools( TOOL_BASE::MODEL_RELOAD );
709 }
710
711 if( selTool )
712 selTool->RebuildSelection();
713
714 clear();
715}
716
717
719{
721}
722
723
724void SCH_COMMIT::RevertToCheckpoint( int aCheckpoint )
725{
726 KIGFX::VIEW* view = m_toolMgr->GetView();
727 SCH_EDIT_FRAME* frame = dynamic_cast<SCH_EDIT_FRAME*>( m_toolMgr->GetToolHolder() );
728 SCH_SELECTION_TOOL* selTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
729 SCH_SHEET_LIST sheets;
730
731 if( m_entries.empty() )
732 return;
733
734 if( m_isLibEditor )
735 {
737 return;
738 }
739
740 SCHEMATIC* schematic = nullptr;
741 std::vector<SCH_ITEM*> bulkAddedItems;
742 std::vector<SCH_ITEM*> bulkRemovedItems;
743 std::vector<SCH_ITEM*> itemsChanged;
744
745 // Detach added groups before restoring any saved group memberships.
746 for( int ii = aCheckpoint; ii < (int) m_entries.size(); ++ii )
747 {
748 COMMIT_LINE& entry = m_entries[ii];
749
750 if( ( entry.m_type & CHT_TYPE ) == CHT_ADD )
751 {
752 if( SCH_GROUP* group = dynamic_cast<SCH_GROUP*>( entry.m_item ) )
753 group->RemoveAll();
754 }
755 }
756
757 for( int ii = aCheckpoint; ii < (int) m_entries.size(); ++ii )
758 {
759 COMMIT_LINE& entry = m_entries[ii];
760 int changeType = entry.m_type & CHT_TYPE;
761 int changeFlags = entry.m_type & CHT_FLAGS;
762 SCH_ITEM* item = dynamic_cast<SCH_ITEM*>( entry.m_item );
763 SCH_ITEM* copy = dynamic_cast<SCH_ITEM*>( entry.m_copy );
764 SCH_SCREEN* screen = dynamic_cast<SCH_SCREEN*>( entry.m_screen );
765
766 wxCHECK2( item && screen, continue );
767
768 if( !schematic )
769 schematic = item->Schematic();
770
771 switch( changeType )
772 {
773 case CHT_ADD:
774 if( !( changeFlags & CHT_DONE ) )
775 break;
776
777 if( view )
778 view->Remove( item );
779
780 screen->Remove( item );
781 bulkRemovedItems.push_back( item );
782 break;
783
784 case CHT_REMOVE:
785 item->SetConnectivityDirty();
786
787 if( !( changeFlags & CHT_DONE ) )
788 break;
789
790 item->ClearFlags( STRUCT_DELETED );
791
792 if( view )
793 view->Add( item );
794
795 screen->Append( item );
796 bulkAddedItems.push_back( item );
797 break;
798
799 case CHT_MODIFY:
800 {
801 wxCHECK2( copy, break );
802
803 if( view )
804 view->Remove( item );
805
806 bool unselect = !item->IsSelected();
807
808 item->SwapItemData( copy );
809
810 if( unselect )
811 {
812 item->ClearSelected();
813 item->RunOnChildren( []( SCH_ITEM* aChild )
814 {
815 aChild->ClearSelected();
816 },
818 }
819
820 // Special cases for items which have instance data
821 if( item->GetParent() && item->GetParent()->Type() == SCH_SYMBOL_T && item->Type() == SCH_FIELD_T )
822 {
823 SCH_FIELD* field = static_cast<SCH_FIELD*>( item );
824 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item->GetParent() );
825
826 if( field->GetId() == FIELD_T::REFERENCE )
827 {
828 // Lazy eval of sheet list; this is expensive even when unsorted
829 if( sheets.empty() )
830 sheets = schematic->Hierarchy();
831
832 SCH_SHEET_PATH sheet = sheets.FindSheetForScreen( screen );
833 symbol->SetRef( &sheet, field->GetText() );
834 }
835 }
836
837 // This must be called before any calls that require stable object pointers.
838 screen->Update( item );
839
840 // This hack is to prevent incorrectly parented symbol pins from breaking the
841 // connectivity algorithm.
842 if( item->Type() == SCH_SYMBOL_T )
843 {
844 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
845 SCH_SYMBOL* symbolCopy = static_cast<SCH_SYMBOL*>( copy );
846
847 symbol->UpdatePins();
848
849 if( symbol->GetSchSymbolLibraryName() != symbolCopy->GetSchSymbolLibraryName() )
850 screen->PruneUnusedLibSymbol( symbolCopy->GetSchSymbolLibraryName() );
851
852 CONNECTION_GRAPH* graph = schematic->ConnectionGraph();
853
854 graph->RemoveItem( symbolCopy );
855
856 // Lazy eval of sheet list; this is expensive even when unsorted
857 if( sheets.empty() )
858 sheets = schematic->Hierarchy();
859
860 SCH_SHEET_PATH sheet = sheets.FindSheetForScreen( screen );
861
862 for( SCH_PIN* pin : symbolCopy->GetPins( &sheet ) )
863 graph->RemoveItem( pin );
864 }
865
866 item->SetConnectivityDirty();
867
868 if( view )
869 view->Add( item );
870
871 itemsChanged.push_back( item );
872 break;
873 }
874
875 default:
876 wxASSERT( false );
877 break;
878 }
879
880 delete copy;
881 entry.m_copy = nullptr;
882 }
883
884 if( schematic )
885 {
886 if( bulkAddedItems.size() > 0 )
887 schematic->OnItemsAdded( bulkAddedItems );
888
889 if( bulkRemovedItems.size() > 0 )
890 schematic->OnItemsRemoved( bulkRemovedItems );
891
892 if( itemsChanged.size() > 0 )
893 schematic->OnItemsChanged( itemsChanged );
894 }
895
896 if( selTool )
897 selTool->RebuildSelection();
898
899 if( frame )
900 frame->RecalculateConnections( nullptr, NO_CLEANUP );
901
902 dropEntriesAfter( aCheckpoint );
903}
904
905
907{
908 if( aID == niluuid )
909 return nullptr;
910
911 for( COMMIT_LINE& entry : m_entries )
912 {
913 if( entry.m_item && entry.m_item->IsSCH_ITEM() && entry.m_item->m_Uuid == aID )
914 return entry.m_item;
915 }
916
917 return nullptr;
918}
int index
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
Handles how to draw a screen (a board, a schematic ...)
Definition base_screen.h:37
bool Empty() const
Definition commit.h:145
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()
Definition commit.cpp:30
COMMIT_LINE * findEntry(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Search for an entry describing change for a particular item.
Definition commit.cpp:267
std::vector< COMMIT_LINE > m_entries
Definition commit.h:225
COMMIT & Removed(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Notify observers that aItem has been removed.
Definition commit.h:92
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
Definition commit.cpp:235
void dropEntriesAfter(int aCheckpoint)
Definition commit.h:184
void clear()
Should be called in Push() & Revert() methods.
Definition commit.h:176
virtual COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Add a change of the item aItem of type aChangeType to the change list.
Definition commit.cpp:42
Calculate the connectivity of a schematic and generate netlists.
void RemoveItem(SCH_ITEM *aItem)
bool IsType(FRAME_T aType) const
The base class for create windows for drawing purpose.
A set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:43
void AddItem(EDA_ITEM *aItem)
Add item to group.
Definition eda_group.cpp:58
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:158
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 ClearSelected()
Definition eda_item.h:153
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition eda_item.h:160
bool IsSelected() const
Definition eda_item.h:134
void SetSelected()
Definition eda_item.h:150
virtual bool IsType(const std::vector< KICAD_T > &aScanTypes) const
Check whether the item is one of the listed types.
Definition eda_item.h:214
EDA_ITEM * GetParent() const
Definition eda_item.h:112
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition eda_item.cpp:288
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition eda_item.h:168
static const TOOL_EVENT SelectedItemsModified
Selected item had a property changed (except movement)
Definition actions.h:354
static const TOOL_EVENT UnselectedEvent
Definition actions.h:348
void SetLink(EDA_ITEM *aItem)
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:68
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:365
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition view.cpp:488
Definition kiid.h:46
Define a library symbol object.
Definition lib_symbol.h:114
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
Definition lib_symbol.h:845
void RunOnChildren(const std::function< void(SCH_ITEM *)> &aFunction, RECURSE_MODE aMode) override
A holder to handle information on schematic or board items.
void PushItem(const ITEM_PICKER &aItem)
Push aItem to the top of the list.
void SetDescription(const wxString &aDescription)
unsigned GetCount() const
Holds all the data relating to one schematic.
Definition schematic.h:155
void OnItemsAdded(std::vector< SCH_ITEM * > &aNewItems)
Must be used if Add() is used using a BULK_x ADD_MODE to generate a change event for listeners.
void OnItemsRemoved(std::vector< SCH_ITEM * > &aRemovedItems)
Must be used if Remove() is used using a BULK_x REMOVE_MODE to generate a change event for listeners.
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
void CleanUpConnections(SCH_COMMIT *aCommit, SCH_CLEANUP_FLAGS aCleanupFlags, const std::set< SCH_SCREEN * > &aLocalScreens={})
CONNECTION_GRAPH * ConnectionGraph() const
Definition schematic.h:414
void RecalculateConnections(SCH_COMMIT *aCommit, SCH_CLEANUP_FLAGS aCleanupFlags, TOOL_MANAGER *aToolManager, PROGRESS_REPORTER *aProgressReporter=nullptr, KIGFX::SCH_VIEW *aSchView=nullptr, std::function< void(SCH_ITEM *)> *aChangedItemHandler=nullptr, PICKED_ITEMS_LIST *aLastChangeList=nullptr, bool aCleanupDone=false)
Generate the connection data for the entire schematic hierarchy.
void RefreshHierarchy()
void OnItemsChanged(std::vector< SCH_ITEM * > &aItems)
Notify the schematic and its listeners that an item on the schematic has been modified in some way.
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
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.
bool m_isLibEditor
Definition sch_commit.h:91
EDA_ITEM * undoLevelItem(EDA_ITEM *aItem) const override
void Revert() override
Revert the commit by restoring the modified items state.
TOOL_MANAGER * m_toolMgr
Definition sch_commit.h:90
EDA_ITEM * ResolveItem(KIID &aID) override
Search for an item in this commit that matches the provided KIID.
void RemovedForCleanup(SCH_ITEM *aItem, SCH_SCREEN *aScreen)
Retain the pre-edit state of an item already removed from its screen by cleanup.
SCH_COMMIT(TOOL_MANAGER *aToolMgr)
virtual ~SCH_COMMIT()
void pushLibEdit(const wxString &aMessage, int aCommitFlags)
void revertLibEdit()
EDA_ITEM * makeImage(EDA_ITEM *aItem) const override
void pushSchEdit(const wxString &aMessage, int aCommitFlags)
void RevertToCheckpoint(int aCheckpoint) override
void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
Schematic editor (Eeschema) main window.
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend)
Create a copy of the current schematic item, and put it in the undo list.
bool RecalculateConnections(SCH_COMMIT *aCommit, SCH_CLEANUP_FLAGS aCleanupFlags, PROGRESS_REPORTER *aProgressReporter=nullptr, bool aCleanupDone=false)
Generate the connection data for the entire schematic hierarchy.
SCH_SHEET_PATH & GetCurrentSheet() const
void UpdateHierarchyNavigator(bool aRefreshNetNavigator=true, bool aClear=false)
Update the hierarchy navigation tree and history.
void RefreshComposedReferences()
Redraw the displayed symbols after a sheet suffix change made on another screen.
void UpdateItem(EDA_ITEM *aItem, bool isAddOrDelete=false, bool aUpdateRtree=false) override
Mark an item for refresh.
void UpdateHopOveredWires(SCH_ITEM *aItem)
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:139
FIELD_T GetId() const
Definition sch_field.h:143
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:170
virtual bool IsConnectable() const
Definition sch_item.h:550
virtual void RunOnChildren(const std::function< void(SCH_ITEM *)> &aFunction, RECURSE_MODE aMode)
Definition sch_item.h:683
SCH_SCREEN * GetParentScreen() const
Definition sch_item.cpp:308
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:302
void SetConnectivityDirty(bool aDirty=true)
Set the dirty flag.
Definition sch_item.cpp:314
bool IsConnectivityDirty() const
Definition sch_item.h:635
virtual bool HasConnectivityChanges(const SCH_ITEM *aItem, const SCH_SHEET_PATH *aInstance=nullptr) const
Check if aItem has connectivity changes against this object.
Definition sch_item.h:659
bool IsGroupableType() const
Definition sch_item.cpp:144
void SwapItemData(SCH_ITEM *aImage)
Swap data between aItem and aImage.
Definition sch_item.cpp:840
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
static bool IsConnectivitySource(const SCH_ITEM *aItem)
Markers, bitmaps, plain shapes, graphic lines and groups carry nothing captured by connectivity.
void BumpConnectivityRevision(KICAD_T aChangedType=TYPE_NOT_INIT)
Increment the connectivity revisions of this screen.
bool Remove(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Remove aItem from the schematic associated with this screen.
void Update(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Refresh a changed item in the tree and invalidate connectivity.
bool CheckIfOnDrawList(const SCH_ITEM *aItem) const
bool PruneUnusedLibSymbol(const wxString &aName)
Remove the library symbol cached under aName if no symbol on this screen still references that name.
void RebuildSelection()
Rebuild the selection from the EDA_ITEMs' selection flags.
SCH_GROUP * GetEnteredGroup()
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:50
bool HasPinIdentityChanges(const SCH_SHEET &aOther) const
bool HasHierarchyChanges(const SCH_SHEET &aOther) const
bool HasRefSuffixChanges(const SCH_SHEET &aOther) const
True when an edit may change the reference suffix this sheet adds to the paths below it.
Schematic symbol object.
Definition sch_symbol.h:76
wxString GetSchSymbolLibraryName() const
std::vector< const SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
void UpdatePins()
Updates the cache of SCH_PIN objects for each pin.
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the base reference for the given sheet path for this symbol.
A foundation class for a tool operating on a schematic or symbol.
bool IsSymbolEditor() const
Returns true if the tool is running in the symbol editor.
int RemoveItemFromSel(const TOOL_EVENT &aEvent)
The symbol library editor main window.
void SetCurSymbol(LIB_SYMBOL *aSymbol, bool aUpdateZoom)
Take ownership of aSymbol and notes that it is the one currently being edited.
LIB_SYMBOL * GetCurSymbol() const
Return the current symbol being edited or NULL if none selected.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
TOOL_MANAGER * GetManager() const
Return the instance of TOOL_MANAGER that takes care of the tool.
Definition tool_base.h:142
@ MODEL_RELOAD
Model changes (the sheet for a schematic)
Definition tool_base.h:76
Master controller class:
CHANGE_TYPE
Types of changes.
Definition commit.h:37
@ CHT_MODIFY
Definition commit.h:40
@ CHT_REMOVE
Definition commit.h:39
@ CHT_DONE
Flag to indicate the change is already applied.
Definition commit.h:43
@ CHT_TYPE
Definition commit.h:41
@ CHT_ADD
Definition commit.h:38
@ CHT_FLAGS
Definition commit.h:44
RECURSE_MODE
Definition eda_item.h:50
@ RECURSE
Definition eda_item.h:51
@ NO_RECURSE
Definition eda_item.h:52
#define SELECTED
Item was manually selected by the user.
#define SELECTED_BY_DRAG
Item was algorithmically selected as a dragged item.
#define EDA_ITEM_ALL_FLAGS
#define STRUCT_DELETED
flag indication structures to be erased
#define ENDPOINT
ends. (Used to support dragging.)
#define STARTPOINT
When a line is selected, these flags indicate which.
@ FRAME_SCH_SYMBOL_EDITOR
Definition frame_type.h:31
bool m_ConnectivityEngine
Use the schematic connectivity engine instead of CONNECTION_GRAPH.
KIID niluuid(0)
This file contains miscellaneous commonly used macros and functions.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:96
#define SKIP_CONNECTIVITY
Definition sch_commit.h:41
#define SKIP_SET_DIRTY
Definition sch_commit.h:40
#define DELETE_REMOVED_ITEMS
Definition sch_commit.h:43
#define SKIP_UNDO
Definition sch_commit.h:38
Class to handle a set of SCH_ITEMs.
SCH_CLEANUP_FLAGS
Definition schematic.h:98
@ LOCAL_CLEANUP
Definition schematic.h:100
@ NO_CLEANUP
Definition schematic.h:99
@ GLOBAL_CLEANUP
Definition schematic.h:101
EDA_ITEM * m_copy
Optional copy of the item.
Definition commit.h:170
CHANGE_TYPE m_type
Modification type.
Definition commit.h:171
EDA_ITEM * m_item
Main item that is added/deleted/modified.
Definition commit.h:169
BASE_SCREEN * m_screen
Definition commit.h:172
@ REFERENCE
Field Reference of part, i.e. "IC21".
KIBIS_PIN * pin
@ AS_GLOBAL
Global action (toolbar/main menu event, global shortcut)
Definition tool_action.h:45
@ TA_MODEL_CHANGE
Model has changed (partial update).
Definition tool_event.h:117
@ TC_MESSAGE
Definition tool_event.h:54
@ SCH_LINE_T
Definition typeinfo.h:159
@ SCH_SYMBOL_T
Definition typeinfo.h:168
@ SCH_FIELD_T
Definition typeinfo.h:146
@ SCH_SHEET_T
Definition typeinfo.h:171
@ SCH_RULE_AREA_T
Definition typeinfo.h:166