-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
sch_commit.cpp
659 lines (504 loc) · 18.8 KB
/
sch_commit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <macros.h>
#include <tool/tool_manager.h>
#include <tools/ee_tool_base.h>
#include <lib_symbol.h>
#include <sch_screen.h>
#include <schematic.h>
#include <view/view.h>
#include <sch_commit.h>
#include <connection_graph.h>
#include <functional>
#include <wx/log.h>
SCH_COMMIT::SCH_COMMIT( TOOL_MANAGER* aToolMgr ) :
m_toolMgr( aToolMgr ),
m_isLibEditor( false )
{
SCH_BASE_FRAME* frame = static_cast<SCH_BASE_FRAME*>( m_toolMgr->GetToolHolder() );
m_isLibEditor = frame && frame->IsType( FRAME_SCH_SYMBOL_EDITOR );
}
SCH_COMMIT::SCH_COMMIT( EE_TOOL_BASE<SCH_BASE_FRAME>* aTool )
{
m_toolMgr = aTool->GetManager();
m_isLibEditor = aTool->IsSymbolEditor();
}
SCH_COMMIT::SCH_COMMIT( EDA_DRAW_FRAME* aFrame )
{
m_toolMgr = aFrame->GetToolManager();
m_isLibEditor = aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR );
}
SCH_COMMIT::~SCH_COMMIT()
{
}
COMMIT& SCH_COMMIT::Stage( EDA_ITEM *aItem, CHANGE_TYPE aChangeType, BASE_SCREEN *aScreen )
{
wxCHECK( aItem, *this );
// If aItem belongs a symbol, sheet or label, the full parent will be saved because undo/redo
// does not handle "sub items" modifications.
if( aItem->Type() != SCH_SHEET_T
&& aItem->GetParent() && aItem->GetParent()->IsType( { SCH_SYMBOL_T, LIB_SYMBOL_T,
SCH_SHEET_T,
SCH_LABEL_LOCATE_ANY_T } ) )
{
aItem = aItem->GetParent();
aChangeType = CHT_MODIFY;
}
// IS_SELECTED flag should not be set on undo items which were added for
// a drag operation.
if( aItem->IsSelected() && aItem->HasFlag( SELECTED_BY_DRAG ) )
{
aItem->ClearSelected();
COMMIT::Stage( aItem, aChangeType, aScreen );
aItem->SetSelected();
}
else
{
COMMIT::Stage( aItem, aChangeType, aScreen );
}
return *this;
}
COMMIT& SCH_COMMIT::Stage( std::vector<EDA_ITEM*> &container, CHANGE_TYPE aChangeType,
BASE_SCREEN *aScreen )
{
for( EDA_ITEM* item : container )
Stage( item, aChangeType, aScreen );
return *this;
}
COMMIT& SCH_COMMIT::Stage( const PICKED_ITEMS_LIST &aItems, UNDO_REDO aModFlag,
BASE_SCREEN *aScreen )
{
return COMMIT::Stage( aItems, aModFlag, aScreen );
}
void SCH_COMMIT::pushLibEdit( const wxString& aMessage, int aCommitFlags )
{
KIGFX::VIEW* view = m_toolMgr->GetView();
SYMBOL_EDIT_FRAME* frame = static_cast<SYMBOL_EDIT_FRAME*>( m_toolMgr->GetToolHolder() );
if( Empty() )
return;
// Symbol editor just saves copies of the whole symbol, so grab the first and discard the rest
LIB_SYMBOL* symbol = dynamic_cast<LIB_SYMBOL*>( m_changes.front().m_item );
LIB_SYMBOL* copy = dynamic_cast<LIB_SYMBOL*>( m_changes.front().m_copy );
if( symbol )
{
if( view )
{
view->Update( symbol );
symbol->RunOnChildren(
[&]( SCH_ITEM* aChild )
{
view->Update( aChild );
} );
}
if( !( aCommitFlags & SKIP_UNDO ) )
{
if( frame && copy )
{
frame->PushSymbolToUndoList( aMessage, copy );
copy = nullptr; // we've transferred ownership to the undo stack
}
}
if( copy )
{
// if no undo entry was needed, the copy would create a memory leak
delete copy;
copy = nullptr;
}
}
m_toolMgr->PostEvent( { TC_MESSAGE, TA_MODEL_CHANGE, AS_GLOBAL } );
m_toolMgr->ProcessEvent( EVENTS::SelectedItemsModified );
if( !( aCommitFlags & SKIP_SET_DIRTY ) )
{
if( frame )
frame->OnModify();
}
for( size_t ii = 1; ii < m_changes.size(); ++ii )
delete m_changes[ii].m_copy;
clear();
}
void SCH_COMMIT::pushSchEdit( const wxString& aMessage, int aCommitFlags )
{
// Objects potentially interested in changes:
PICKED_ITEMS_LIST undoList;
KIGFX::VIEW* view = m_toolMgr->GetView();
SCH_EDIT_FRAME* frame = static_cast<SCH_EDIT_FRAME*>( m_toolMgr->GetToolHolder() );
EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
bool itemsDeselected = false;
bool selectedModified = false;
bool dirtyConnectivity = false;
bool refreshHierarchy = false;
SCH_CLEANUP_FLAGS connectivityCleanUp = NO_CLEANUP;
if( Empty() )
return;
undoList.SetDescription( aMessage );
SCHEMATIC* schematic = nullptr;
std::vector<SCH_ITEM*> bulkAddedItems;
std::vector<SCH_ITEM*> bulkRemovedItems;
std::vector<SCH_ITEM*> itemsChanged;
for( COMMIT_LINE& ent : m_changes )
{
int changeType = ent.m_type & CHT_TYPE;
int changeFlags = ent.m_type & CHT_FLAGS;
SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( ent.m_item );
SCH_SCREEN* screen = dynamic_cast<SCH_SCREEN*>( ent.m_screen );
wxCHECK2( schItem && screen, continue );
if( !schematic )
schematic = schItem->Schematic();
if( schItem->IsSelected() )
{
selectedModified = true;
}
else
{
schItem->RunOnChildren(
[&selectedModified]( SCH_ITEM* aChild )
{
if( aChild->IsSelected() )
selectedModified = true;
} );
}
auto updateConnectivityFlag = [&]()
{
if( schItem->IsConnectable() || ( schItem->Type() == SCH_RULE_AREA_T ) )
{
dirtyConnectivity = true;
// Do a local clean up if there are any connectable objects in the commit.
if( connectivityCleanUp == NO_CLEANUP )
connectivityCleanUp = LOCAL_CLEANUP;
// Do a full rebauild of the connectivity if there is a sheet in the commit.
if( schItem->Type() == SCH_SHEET_T )
connectivityCleanUp = GLOBAL_CLEANUP;
}
};
switch( changeType )
{
case CHT_ADD:
{
updateConnectivityFlag();
if( !( aCommitFlags & SKIP_UNDO ) )
undoList.PushItem( ITEM_PICKER( screen, schItem, UNDO_REDO::NEWITEM ) );
if( !( changeFlags & CHT_DONE ) )
{
if( !screen->CheckIfOnDrawList( schItem ) ) // don't want a loop!
screen->Append( schItem );
if( view )
view->Add( schItem );
}
if( frame )
frame->UpdateItem( schItem, true, true );
bulkAddedItems.push_back( schItem );
if( schItem->Type() == SCH_SHEET_T )
refreshHierarchy = true;
break;
}
case CHT_REMOVE:
{
updateConnectivityFlag();
if( !( aCommitFlags & SKIP_UNDO ) )
undoList.PushItem( ITEM_PICKER( screen, schItem, UNDO_REDO::DELETED ) );
if( schItem->IsSelected() )
{
if( selTool )
selTool->RemoveItemFromSel( schItem, true /* quiet mode */ );
itemsDeselected = true;
}
if( schItem->Type() == SCH_FIELD_T )
{
static_cast<SCH_FIELD*>( schItem )->SetVisible( false );
break;
}
if( !( changeFlags & CHT_DONE ) )
{
screen->Remove( schItem );
if( view )
view->Remove( schItem );
}
if( frame )
frame->UpdateItem( schItem, true, true );
bulkRemovedItems.push_back( schItem );
if( schItem->Type() == SCH_SHEET_T )
refreshHierarchy = true;
break;
}
case CHT_MODIFY:
{
if( !( aCommitFlags & SKIP_UNDO ) )
{
ITEM_PICKER itemWrapper( screen, schItem, UNDO_REDO::CHANGED );
wxASSERT( ent.m_copy );
itemWrapper.SetLink( ent.m_copy );
const SCH_ITEM* itemCopy = static_cast<const SCH_ITEM*>( ent.m_copy );
wxCHECK2( itemCopy, continue );
SCH_SHEET_PATH currentSheet;
if( frame )
currentSheet = frame->GetCurrentSheet();
if( itemCopy->HasConnectivityChanges( schItem, ¤tSheet )
|| ( itemCopy->Type() == SCH_RULE_AREA_T ) )
{
updateConnectivityFlag();
}
if( schItem->Type() == SCH_SHEET_T )
{
const SCH_SHEET* modifiedSheet = static_cast<const SCH_SHEET*>( schItem );
const SCH_SHEET* originalSheet = static_cast<const SCH_SHEET*>( itemCopy );
wxCHECK2( modifiedSheet && originalSheet, continue );
if( originalSheet->HasPageNumberChanges( *modifiedSheet ) )
refreshHierarchy = true;
}
undoList.PushItem( itemWrapper );
ent.m_copy = nullptr; // We've transferred ownership to the undo list
}
if( frame )
frame->UpdateItem( schItem, false, true );
itemsChanged.push_back( schItem );
if( ent.m_copy )
{
// if no undo entry is needed, the copy would create a memory leak
delete ent.m_copy;
ent.m_copy = nullptr;
}
break;
}
default:
wxASSERT( false );
break;
}
// Clear all flags but SELECTED and others used to move and rotate commands,
// after edition (selected items must keep their selection flag).
const int selected_mask = ( SELECTED | STARTPOINT | ENDPOINT );
schItem->ClearFlags( EDA_ITEM_ALL_FLAGS - selected_mask );
}
if( schematic )
{
if( bulkAddedItems.size() > 0 )
schematic->OnItemsAdded( bulkAddedItems );
if( bulkRemovedItems.size() > 0 )
schematic->OnItemsRemoved( bulkRemovedItems );
if( itemsChanged.size() > 0 )
schematic->OnItemsChanged( itemsChanged );
if( refreshHierarchy )
{
schematic->RefreshHierarchy();
if( frame )
frame->UpdateHierarchyNavigator();
}
}
if( !( aCommitFlags & SKIP_UNDO ) )
{
if( frame )
{
frame->SaveCopyInUndoList( undoList, UNDO_REDO::UNSPECIFIED, false, dirtyConnectivity );
if( dirtyConnectivity )
{
wxLogTrace( wxS( "CONN_PROFILE" ),
wxS( "SCH_COMMIT::pushSchEdit() %s clean up connectivity rebuild." ),
( connectivityCleanUp == LOCAL_CLEANUP ) ? wxS( "local" ) : wxS( "global" ) );
frame->RecalculateConnections( this, connectivityCleanUp );
}
}
}
m_toolMgr->PostEvent( { TC_MESSAGE, TA_MODEL_CHANGE, AS_GLOBAL } );
if( itemsDeselected )
m_toolMgr->PostEvent( EVENTS::UnselectedEvent );
if( selectedModified )
m_toolMgr->ProcessEvent( EVENTS::SelectedItemsModified );
if( frame && frame->GetCanvas() )
frame->GetCanvas()->Refresh();
if( !( aCommitFlags & SKIP_SET_DIRTY ) )
{
if( frame )
frame->OnModify();
}
clear();
}
void SCH_COMMIT::Push( const wxString& aMessage, int aCommitFlags )
{
if( m_isLibEditor )
pushLibEdit( aMessage, aCommitFlags );
else
pushSchEdit( aMessage, aCommitFlags );
}
EDA_ITEM* SCH_COMMIT::parentObject( EDA_ITEM* aItem ) const
{
EDA_ITEM* parent = aItem->GetParent();
if( parent && ( parent->Type() == SCH_SYMBOL_T || parent->Type() == LIB_SYMBOL_T ) )
return parent;
if( m_isLibEditor )
return static_cast<SYMBOL_EDIT_FRAME*>( m_toolMgr->GetToolHolder() )->GetCurSymbol();
return aItem;
}
EDA_ITEM* SCH_COMMIT::makeImage( EDA_ITEM* aItem ) const
{
if( m_isLibEditor )
{
SYMBOL_EDIT_FRAME* frame = static_cast<SYMBOL_EDIT_FRAME*>( m_toolMgr->GetToolHolder() );
LIB_SYMBOL* symbol = frame->GetCurSymbol();
std::vector<KIID> selected;
for( const SCH_ITEM& item : symbol->GetDrawItems() )
{
if( item.IsSelected() )
selected.push_back( item.m_Uuid );
}
symbol = new LIB_SYMBOL( *symbol );
for( SCH_ITEM& item : symbol->GetDrawItems() )
{
if( alg::contains( selected, item.m_Uuid ) )
item.SetSelected();
}
return symbol;
}
return aItem->Clone();
}
void SCH_COMMIT::revertLibEdit()
{
if( Empty() )
return;
// Symbol editor just saves copies of the whole symbol, so grab the first and discard the rest
SYMBOL_EDIT_FRAME* frame = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_toolMgr->GetToolHolder() );
LIB_SYMBOL* copy = dynamic_cast<LIB_SYMBOL*>( m_changes.front().m_copy );
EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
if( frame && copy )
{
frame->SetCurSymbol( copy, false );
m_toolMgr->ResetTools( TOOL_BASE::MODEL_RELOAD );
}
for( size_t ii = 1; ii < m_changes.size(); ++ii )
delete m_changes[ii].m_copy;
if( selTool )
selTool->RebuildSelection();
clear();
}
void SCH_COMMIT::Revert()
{
KIGFX::VIEW* view = m_toolMgr->GetView();
SCH_EDIT_FRAME* frame = dynamic_cast<SCH_EDIT_FRAME*>( m_toolMgr->GetToolHolder() );
EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
SCH_SHEET_LIST sheets;
if( m_changes.empty() )
return;
if( m_isLibEditor )
{
revertLibEdit();
return;
}
SCHEMATIC* schematic = nullptr;
std::vector<SCH_ITEM*> bulkAddedItems;
std::vector<SCH_ITEM*> bulkRemovedItems;
std::vector<SCH_ITEM*> itemsChanged;
for( COMMIT_LINE& ent : m_changes )
{
int changeType = ent.m_type & CHT_TYPE;
int changeFlags = ent.m_type & CHT_FLAGS;
SCH_ITEM* item = dynamic_cast<SCH_ITEM*>( ent.m_item );
SCH_ITEM* copy = dynamic_cast<SCH_ITEM*>( ent.m_copy );
SCH_SCREEN* screen = dynamic_cast<SCH_SCREEN*>( ent.m_screen );
wxCHECK2( item && screen, continue );
if( !schematic )
schematic = item->Schematic();
switch( changeType )
{
case CHT_ADD:
if( !( changeFlags & CHT_DONE ) )
break;
if( view )
view->Remove( item );
screen->Remove( item );
bulkRemovedItems.push_back( item );
break;
case CHT_REMOVE:
item->SetConnectivityDirty();
if( !( changeFlags & CHT_DONE ) )
break;
if( view )
view->Add( item );
screen->Append( item );
bulkAddedItems.push_back( item );
break;
case CHT_MODIFY:
{
if( view )
view->Remove( item );
bool unselect = !item->IsSelected();
item->SwapData( copy );
if( unselect )
{
item->ClearSelected();
item->RunOnChildren( []( SCH_ITEM* aChild ) { aChild->ClearSelected(); } );
}
// Special cases for items which have instance data
if( item->GetParent() && item->GetParent()->Type() == SCH_SYMBOL_T
&& item->Type() == SCH_FIELD_T )
{
SCH_FIELD* field = static_cast<SCH_FIELD*>( item );
SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item->GetParent() );
if( field->GetId() == REFERENCE_FIELD )
{
// Lazy eval of sheet list; this is expensive even when unsorted
if( sheets.empty() )
sheets = schematic->Hierarchy();
SCH_SHEET_PATH sheet = sheets.FindSheetForScreen( screen );
symbol->SetRef( &sheet, field->GetText() );
}
}
// This must be called before any calls that require stable object pointers.
screen->Update( item );
// This hack is to prevent incorrectly parented symbol pins from breaking the
// connectivity algorithm.
if( item->Type() == SCH_SYMBOL_T )
{
SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
symbol->UpdatePins();
CONNECTION_GRAPH* graph = schematic->ConnectionGraph();
SCH_SYMBOL* symbolCopy = static_cast<SCH_SYMBOL*>( copy );
graph->RemoveItem( symbolCopy );
for( SCH_PIN* pin : symbolCopy->GetPins() )
graph->RemoveItem( pin );
}
item->SetConnectivityDirty();
if( view )
view->Add( item );
delete copy;
break;
}
default:
wxASSERT( false );
break;
}
}
if( schematic )
{
if( bulkAddedItems.size() > 0 )
schematic->OnItemsAdded( bulkAddedItems );
if( bulkRemovedItems.size() > 0 )
schematic->OnItemsRemoved( bulkRemovedItems );
if( itemsChanged.size() > 0 )
schematic->OnItemsChanged( itemsChanged );
}
if( selTool )
selTool->RebuildSelection();
if( frame )
{
frame->RecalculateConnections( nullptr, NO_CLEANUP );
}
clear();
}