KiCad PCB EDA Suite
Loading...
Searching...
No Matches
symbol_fields_data_model.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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 <set>
21
22#include <wx/string.h>
23#include <wx/debug.h>
24#include <wx/grid.h>
25#include <wx/settings.h>
26#include <common.h>
27#include <widgets/wx_grid.h>
28#include <sch_reference_list.h>
29#include <sch_commit.h>
30#include <sch_screen.h>
31#include <template_fieldnames.h>
32#include <sch_sheet_path.h>
33#include "string_utils.h"
34
36
37
45{
46 KIID_PATH path = aItem.GetSheetPath().Path();
47 path.push_back( aItem.GetSymbol()->m_Uuid );
48 return path;
49}
50
51
53{
54 return aItem.GetRef() + aItem.GetRefNumber();
55}
56
57
59{
60 return aItem.GetRefSuffix();
61}
62
63
64wxGridCellAttr* SYMBOL_FIELDS_EDITOR_GRID_DATA_MODEL::GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind )
65{
66 wxGridCellAttr* attr = nullptr;
67 bool needsReadOnly = IsCellReadOnly( aRow, aCol );
68 bool needsUrlEditor = cellUsesUrlEditor( aRow, aCol );
69 bool needsVariantHighlight = false;
70 bool needsResolvedTextRenderer = cellUsesResolvedTextRenderer( aRow, aCol );
71 wxColour highlightColor;
72
73 // Check if we need variant highlighting
74 if( !m_currentVariant.IsEmpty() && aRow >= 0 && aRow < (int) m_rows.size() && aCol >= 0
75 && aCol < (int) m_cols.size() )
76 {
77 const wxString& fieldName = m_cols[aCol].m_fieldName;
78
79 // Skip Reference and generated fields (like ${QUANTITY}) for highlighting
80 if( !ColIsReference( aCol ) && !ColIsQuantity( aCol ) && !ColIsItemNumber( aCol ) )
81 {
83
84 // Check if any symbol in this row has a variant-specific value
85 for( const SCH_REFERENCE& ref : row.m_items )
86 {
87 wxString defaultValue = getDefaultFieldValue( ref, fieldName );
88
89 // Get the current value from the data store
90 wxString currentValue;
91
92 if( ref.GetSymbol() )
93 getStoredFieldValue( ref, fieldName, currentValue );
94
95 if( currentValue != defaultValue )
96 {
97 needsVariantHighlight = true;
98
99 bool isPriority2 = false;
100
101 if( const SCH_SYMBOL* sym = ref.GetSymbol() )
102 {
103 auto variantData = sym->GetVariant( ref.GetSheetPath(),
105
106 if( variantData
107 && variantData->m_SymbolOverride
108 && !variantData->m_Fields.count( fieldName ) )
109 {
110 isPriority2 = true;
111 }
112 }
113
114 wxColour bg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
115 bool isDark = ( bg.Red() + bg.Green() + bg.Blue() ) < 384;
116
117 if( isPriority2 )
118 {
121 }
122 else
123 {
126 }
127
128 break;
129 }
130 }
131 }
132 }
133
134 // If we don't need any custom attributes, use the base class behavior
135 if( !needsReadOnly && !needsUrlEditor && !needsVariantHighlight && !needsResolvedTextRenderer )
136 return applyCellDecorations( WX_GRID_TABLE_BASE::GetAttr( aRow, aCol, aKind ), aRow, aCol );
137
138 // URL cells use the Datasheet column's editor. Other cells use their own column attributes.
139 if( needsUrlEditor )
140 {
141 attr = cloneUrlEditorAttr();
142 }
143 else if( m_colAttrs.find( aCol ) != m_colAttrs.end() && m_colAttrs[aCol] )
144 {
145 attr = m_colAttrs[aCol]->Clone();
146 }
147 else
148 {
149 attr = new wxGridCellAttr();
150 }
151
152 if( needsReadOnly )
153 attr->SetReadOnly();
154
155 if( needsVariantHighlight )
156 attr->SetBackgroundColour( highlightColor );
157
158 if( needsResolvedTextRenderer )
159 applyResolvedTextRenderer( attr, !needsVariantHighlight );
160
161 return applyCellDecorations( enhanceAttr( attr, aRow, aCol, aKind ), aRow, aCol );
162}
163
164
165void SYMBOL_FIELDS_EDITOR_GRID_DATA_MODEL::SetValue( int aRow, int aCol, const wxString& aValue )
166{
167 wxCHECK_RET( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), wxS( "Invalid column number" ) );
168
169 if( IsCellReadOnly( aRow, aCol ) )
170 return;
171
172 if( aValue == INDETERMINATE_STATE )
173 return;
174
175 setCellValues( aRow, aCol, std::vector<wxString>( m_rows[aRow].GetCellItems().size(), aValue ) );
176 m_edited = true;
177}
178
179
181{
182 wxCHECK_RET( aRow >= 0 && aRow < static_cast<int>( m_rows.size() ), wxS( "Invalid row number" ) );
183 wxCHECK_RET( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), wxS( "Invalid column number" ) );
184
185 if( !CanClearCell( aRow, aCol ) )
186 return;
187
188 const wxString& fieldName = m_cols[aCol].m_fieldName;
189 std::set<const SCH_SYMBOL*> clearedSymbols;
190
191 for( const SCH_REFERENCE& ref : m_rows[aRow].m_items )
192 clearedSymbols.insert( ref.GetSymbol() );
193
194 // Clearing a field is a symbol-wide operation, even when the table is showing one variant
195 // or one instance of a shared sheet. Clear it from every data-store entry for the symbol so
196 // a later entry cannot recreate it while ApplyData() walks the other instances.
197 for( unsigned ii = 0; ii < m_symbolsList.GetCount(); ++ii )
198 {
199 const SCH_REFERENCE& ref = m_symbolsList[ii];
200
201 if( clearedSymbols.contains( ref.GetSymbol() ) )
202 clearStoredField( ref, fieldName );
203 }
204
205 m_edited = true;
206}
207
208
210{
211 wxCHECK_RET( aRow >= 0 && aRow < static_cast<int>( m_rows.size() ), wxS( "Invalid row number" ) );
212
213 std::set<const SCH_SYMBOL*> rowSymbols;
214
215 for( const SCH_REFERENCE& ref : m_rows[aRow].m_items )
216 rowSymbols.insert( ref.GetSymbol() );
217
218 for( const DATA_MODEL_COL& col : m_cols )
219 {
220 bool revertAllPaths = storageIsSharedAcrossPaths( col.m_fieldName );
221
222 // Field presence belongs to the symbol rather than an individual path. If an edit added
223 // or removed the field, restore every path so a hidden path cannot recreate it or retain
224 // a pending creation when ApplyData() walks the symbol's other instances.
225 if( !revertAllPaths )
226 {
227 for( unsigned ii = 0; ii < m_symbolsList.GetCount(); ++ii )
228 {
229 const SCH_REFERENCE& ref = m_symbolsList[ii];
230
231 if( !rowSymbols.contains( ref.GetSymbol() ) )
232 continue;
233
234 wxString liveValue;
235 wxString storedValue;
236 bool liveFieldPresent = getLiveFieldValue( ref, col.m_fieldName, liveValue );
237 bool storedFieldPresent = getStoredFieldValue( ref, col.m_fieldName, storedValue );
238
239 if( liveFieldPresent != storedFieldPresent )
240 {
241 revertAllPaths = true;
242 break;
243 }
244 }
245 }
246
247 if( !revertAllPaths )
248 continue;
249
250 for( unsigned ii = 0; ii < m_symbolsList.GetCount(); ++ii )
251 {
252 const SCH_REFERENCE& ref = m_symbolsList[ii];
253
254 if( rowSymbols.contains( ref.GetSymbol() ) )
255 updateDataStoreItemFieldFromLive( ref, col.m_fieldName );
256 }
257 }
258
260}
261
262
263void SYMBOL_FIELDS_EDITOR_GRID_DATA_MODEL::setCellValues( int aRow, int aCol, const std::vector<wxString>& aValues )
264{
265 std::span<const SCH_REFERENCE> items = m_rows[aRow].GetCellItems();
266 const wxString& fieldName = m_cols[aCol].m_fieldName;
267
268 std::map<const SCH_SYMBOL*, wxString> editedSymbols;
269
270 for( size_t i = 0; i < items.size(); ++i )
271 editedSymbols[items[i].GetSymbol()] = aValues[i];
272
273 // Field presence is on the symbol object and applies to all instances.
274 // Before editing one path, ensure every instance for that symbol has this field
275 // marked present at least.
276 for( unsigned ii = 0; ii < m_symbolsList.GetCount(); ++ii )
277 {
278 const SCH_REFERENCE& ref = m_symbolsList[ii];
279
280 if( !editedSymbols.contains( ref.GetSymbol() ) )
281 continue;
282
283 wxString unused;
284
285 if( !getStoredFieldValue( ref, fieldName, unused ) )
286 ensureStoredFieldPresent( ref, fieldName );
287 }
288
289 for( size_t i = 0; i < items.size(); ++i )
290 setStoredFieldValue( items[i], fieldName, aValues[i] );
291
292 // ApplyData walks every path a symbol is reachable through, so an edit to storage those
293 // paths have in common must also reach the ones the current scope and filter hide
294 if( storageIsSharedAcrossPaths( fieldName ) )
295 {
296 for( unsigned ii = 0; ii < m_symbolsList.GetCount(); ++ii )
297 {
298 const SCH_REFERENCE& ref = m_symbolsList[ii];
299
300 auto it = editedSymbols.find( ref.GetSymbol() );
301
302 if( it != editedSymbols.end() )
303 setStoredFieldValue( ref, fieldName, it->second );
304 }
305 }
306}
307
308
310{
312 && aFieldName != wxS( "${EXCLUDE_FROM_BOARD}" );
313}
314
315
317{
318 // If items are unannotated then we can't tell if they're units of the same symbol or not
319 if( lhItem.GetRefNumber() == wxT( "?" ) )
320 return false;
321
322 return lhItem.GetRef() == rhItem.GetRef() && lhItem.GetRefNumber() == rhItem.GetRefNumber()
323 && lhItem.GetRefSuffix() == rhItem.GetRefSuffix();
324}
325
326
328 const wxString& aFieldName,
329 wxString& aValue )
330{
331 return getLiveFieldValueForVariant( aRef, aFieldName, m_currentVariant, aValue );
332}
333
334
336{
337 std::vector<SCH_REFERENCE> items;
338
339 for( unsigned i = 0; i < m_symbolsList.GetCount(); ++i )
340 {
341 if( m_symbolsList[i].GetSymbol() )
342 items.push_back( m_symbolsList[i] );
343 }
344
345 return items;
346}
347
348
350 const wxString& aFieldName )
351{
352 SCH_FIELD* field = aRef.GetSymbol()->GetField( aFieldName );
353
354 if( field )
355 {
356 if( field->IsPrivate() )
357 return wxEmptyString;
358 else
359 return field->GetShownText( &aRef.GetSheetPath(), INTERNAL, m_currentVariant );
360 }
361
362 // Handle generated fields with variables as names (e.g. ${QUANTITY}) that are not present in
363 // the symbol by giving them the correct value by resolving against the symbol
364 if( IsGeneratedField( aFieldName ) )
365 {
366 int depth = 0;
367 const SCH_SHEET_PATH& path = aRef.GetSheetPath();
368
369 std::function<bool( wxString* )> symbolResolver =
370 [&]( wxString* token ) -> bool
371 {
372 return aRef.GetSymbol()->ResolveTextVar( &path, token, m_currentVariant, depth + 1 );
373 };
374
375 return ResolveTextVars( aFieldName, &symbolResolver, depth );
376 }
377
378 return wxEmptyString;
379}
380
381
382wxString SYMBOL_FIELDS_EDITOR_GRID_DATA_MODEL::resolveTextVars( const SCH_REFERENCE& aRef, const wxString& aText )
383{
384 int depth = 0;
385
386 std::function<bool( wxString* )> symbolResolver =
387 [&]( wxString* token ) -> bool
388 {
389 if( resolveStoredTextVar( aRef, token ) )
390 return true;
391
392 return aRef.GetSymbol()->ResolveTextVar( &aRef.GetSheetPath(), token, m_currentVariant, depth );
393 };
394
395 return ResolveTextVars( aText, &symbolResolver, depth );
396}
397
398
400{
401 // Variant edits are kept on the symbol instance, but SCH_REFERENCE has no variant form of
402 // the board exclusion so that one always lands on the symbol
403 if( aFieldName == wxS( "${EXCLUDE_FROM_BOARD}" ) )
404 return true;
405
406 return m_currentVariant.IsEmpty();
407}
408
409
411 const wxString& aAttributeName,
412 const wxString& aVariantName )
413{
414 if( aAttributeName == wxS( "${DNP}" ) )
415 return aRef.GetSymbolDNP( aVariantName ) ? wxS( "1" ) : wxS( "0" );
416
417 if( aAttributeName == wxS( "${EXCLUDE_FROM_BOARD}" ) )
418 return aRef.GetSymbolExcludedFromBoard() ? wxS( "1" ) : wxS( "0" );
419
420 if( aAttributeName == wxS( "${EXCLUDE_FROM_BOM}" ) )
421 return aRef.GetSymbolExcludedFromBOM( aVariantName ) ? wxS( "1" ) : wxS( "0" );
422
423 if( aAttributeName == wxS( "${EXCLUDE_FROM_SIM}" ) )
424 return aRef.GetSymbolExcludedFromSim( aVariantName ) ? wxS( "1" ) : wxS( "0" );
425
426 if( aAttributeName == wxS( "${EXCLUDE_FROM_POS_FILES}" ) )
427 return aRef.GetSymbolExcludedFromPosFiles( aVariantName ) ? wxS( "1" ) : wxS( "0" );
428
429 return wxS( "0" );
430}
431
432
434 const wxString& aAttributeName,
435 const wxString& aVariantName ) const
436{
437 const SCH_SHEET_PATH& path = aRef.GetSheetPath();
438
439 if( aAttributeName == wxS( "${DNP}" ) )
440 return path.GetDNP( aVariantName );
441 else if( aAttributeName == wxS( "${EXCLUDE_FROM_BOARD}" ) )
442 return path.GetExcludedFromBoard( aVariantName );
443 else if( aAttributeName == wxS( "${EXCLUDE_FROM_BOM}" ) )
444 return path.GetExcludedFromBOM( aVariantName );
445 else if( aAttributeName == wxS( "${EXCLUDE_FROM_SIM}" ) )
446 return path.GetExcludedFromSim( aVariantName );
447
448 return false;
449}
450
451
453 const wxString& aFieldName,
454 const wxString& aVariantName, wxString& aValue )
455{
456 aValue.clear();
457
458 const SCH_SYMBOL* symbol = aRef.GetSymbol();
459
460 if( !symbol )
461 return false;
462
463 if( fieldIsAttribute( aFieldName ) )
464 {
465 aValue = getAttributeValue( aRef, aFieldName, aVariantName );
466 return true;
467 }
468
469 if( const SCH_FIELD* field = symbol->GetField( aFieldName ) )
470 {
471 if( field->IsPrivate() )
472 return false;
473
474 aValue = symbol->Schematic()->ConvertKIIDsToRefs( field->GetText( &aRef.GetSheetPath(), aVariantName ) );
475 return true;
476 }
477
478 // For generated fields, return the field name itself
479 if( IsGeneratedField( aFieldName ) )
480 {
481 aValue = aFieldName;
482 return true;
483 }
484
485 return false;
486}
487
488
490 const wxString& aFieldName )
491{
492 wxString value;
493 getLiveFieldValueForVariant( aRef, aFieldName, wxEmptyString, value );
494 return value;
495}
496
497
499 const wxString& aAttributeName,
500 const wxString& aValue,
501 const wxString& aVariantName )
502{
503 bool attrChanged = false;
504 bool newValue = aValue == wxS( "1" );
505
506 if( aAttributeName == wxS( "${DNP}" ) )
507 {
508 attrChanged = aRef.GetSymbolDNP( aVariantName ) != newValue;
509
510 if( attrChanged )
511 aRef.SetSymbolDNP( newValue, aVariantName );
512 }
513 else if( aAttributeName == wxS( "${EXCLUDE_FROM_BOARD}" ) )
514 {
515 attrChanged = aRef.GetSymbolExcludedFromBoard() != newValue;
516
517 if( attrChanged )
518 aRef.SetSymbolExcludedFromBoard( newValue );
519 }
520 else if( aAttributeName == wxS( "${EXCLUDE_FROM_BOM}" ) )
521 {
522 attrChanged = aRef.GetSymbolExcludedFromBOM( aVariantName ) != newValue;
523
524 if( attrChanged )
525 aRef.SetSymbolExcludedFromBOM( newValue, aVariantName );
526 }
527 else if( aAttributeName == wxS( "${EXCLUDE_FROM_SIM}" ) )
528 {
529 attrChanged = aRef.GetSymbolExcludedFromSim( aVariantName ) != newValue;
530
531 if( attrChanged )
532 aRef.SetSymbolExcludedFromSim( newValue, aVariantName );
533 }
534 else if( aAttributeName == wxS( "${EXCLUDE_FROM_POS_FILES}" ) )
535 {
536 attrChanged = aRef.GetSymbolExcludedFromPosFiles( aVariantName ) != newValue;
537
538 if( attrChanged )
539 aRef.SetSymbolExcludedFromPosFiles( newValue, aVariantName );
540 }
541
542 return attrChanged;
543}
544
545
547{
548 if( !m_rebuildsEnabled )
549 return;
550
551 if( GetView() )
552 {
553 // Commit any pending in-place edits before the row gets moved out from under
554 // the editor.
555 static_cast<WX_GRID*>( GetView() )->CommitPendingChanges( true );
556
557 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, 0, m_rows.size() );
558 GetView()->ProcessTableMessage( msg );
559 }
560
561 m_rows.clear();
562
563 EDA_COMBINED_MATCHER matcher( m_filter.Lower(), CTX_SEARCH );
564
565 for( unsigned i = 0; i < m_symbolsList.GetCount(); ++i )
566 {
568
569 if( m_scope == SCOPE::SCOPE_SELECTION
570 && !m_selectionItems.contains( getDataStoreKey( ref ) ) )
571 {
572 continue;
573 }
574
575 if( !MatchesFilter( ref, ref.GetFullRef(), matcher ) )
576 continue;
577
578 if( m_excludeDNP )
579 {
580 bool isDNP = false;
581
582 if( !m_variantNames.empty() )
583 {
584 for( const wxString& variantName : m_variantNames )
585 {
586 if( ref.GetSymbol()->ResolveDNP( &ref.GetSheetPath(), variantName )
587 || ref.GetSheetPath().GetDNP( variantName ) )
588 {
589 isDNP = true;
590 break;
591 }
592 }
593 }
594 else
595 {
596 isDNP = ref.GetSymbol()->ResolveDNP( &ref.GetSheetPath(), m_currentVariant )
598 }
599
600 if( isDNP )
601 continue;
602 }
603
604 if( !m_includeExcluded )
605 {
606 bool isExcluded = false;
607
608 if( !m_variantNames.empty() )
609 {
610 for( const wxString& variantName : m_variantNames )
611 {
612 if( ref.GetSymbol()->ResolveExcludedFromBOM( &ref.GetSheetPath(), variantName )
613 || ref.GetSheetPath().GetExcludedFromBOM( variantName ) )
614 {
615 isExcluded = true;
616 break;
617 }
618 }
619 }
620 else
621 {
624 }
625
626 if( isExcluded )
627 continue;
628 }
629
630 // Check if the symbol if on the current sheet or, in the sheet path somewhere
631 // depending on scope
632 if( ( m_scope == SCOPE::SCOPE_SHEET && ref.GetSheetPath() != m_path )
633 || ( m_scope == SCOPE::SCOPE_SHEET_RECURSIVE
634 && !ref.GetSheetPath().IsContainedWithin( m_path ) ) )
635 {
636 continue;
637 }
638
639 bool matchFound = false;
640
641 // Performance optimization for ungrouped case to skip the N^2 for loop
642 if( !m_groupingEnabled && !ref.IsMultiUnit() )
643 {
644 m_rows.emplace_back( SYMBOL_FIELDS_TABLE_DATA_MODEL_ROW( ref, ROW_STATE::NON_EXPANDABLE ) );
645 continue;
646 }
647
648 // See if we already have a row which this symbol fits into
650 {
651 // all group members must have identical refs so just use the first one
652 SCH_REFERENCE rowRef = row.m_items[0];
653
654 if( unitMatch( ref, rowRef ) )
655 {
656 matchFound = true;
657 row.m_items.push_back( ref );
658 break;
659 }
660 else if( m_groupingEnabled && groupMatch( ref, rowRef ) )
661 {
662 matchFound = true;
663 row.m_items.push_back( ref );
664 row.m_state = ROW_STATE::GROUP_COLLAPSED;
665 break;
666 }
667 }
668
669 if( !matchFound )
670 m_rows.emplace_back( SYMBOL_FIELDS_TABLE_DATA_MODEL_ROW( ref, ROW_STATE::NON_EXPANDABLE ) );
671 }
672
673 if( GetView() )
674 {
675 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, m_rows.size() );
676 GetView()->ProcessTableMessage( msg );
677 }
678
679 Sort();
680}
681
682
684{
685 for( const SCH_REFERENCE& ref : m_symbolsList )
687
688 bool symbolModified = false;
689 std::unique_ptr<SCH_SYMBOL> symbolCopy;
690
691 for( size_t i = 0; i < m_symbolsList.GetCount(); i++ )
692 {
693 SCH_SYMBOL* symbol = m_symbolsList[i].GetSymbol();
694 SCH_SYMBOL* nextSymbol = nullptr;
695
696 if( ( i + 1 ) < m_symbolsList.GetCount() )
697 nextSymbol = m_symbolsList[i + 1].GetSymbol();
698
699 if( i == 0 )
700 symbolCopy = std::make_unique<SCH_SYMBOL>( *symbol );
701
702 for( const wxString& variantName : storedVariants( m_symbolsList[i] ) )
703 {
704 const std::map<wxString, wxString> fieldStore = getStoredFields( m_symbolsList[i], variantName );
705
706 for( const auto& [srcName, srcValue] : fieldStore )
707 {
708 // Attributes bypass the field logic, so handle them first
709 if( fieldIsAttribute( srcName ) )
710 {
711 symbolModified |= setAttributeValue( m_symbolsList[i], srcName, srcValue, variantName );
712 continue;
713 }
714
715 // Skip generated fields with variables as names (e.g. ${QUANTITY});
716 // they can't be edited
717 if( IsGeneratedField( srcName ) )
718 continue;
719
720 SCH_FIELD* destField = symbol->GetField( srcName );
721
722 if( destField && destField->IsPrivate() )
723 {
724 if( srcValue.IsEmpty() )
725 continue;
726 else
727 destField->SetPrivate( false );
728 }
729
730 // Reaching this point means the data store field is at least marked present,
731 // so add the field to the symbol even when its stored value is empty.
732 bool createField = !destField;
733
734 if( createField )
735 {
736 destField = symbol->AddField( SCH_FIELD( symbol, FIELD_T::USER, srcName ) );
737 destField->SetTextAngle( symbol->GetField( FIELD_T::REFERENCE )->GetTextAngle() );
738
739 if( const TEMPLATE_FIELDNAME* srcTemplate = aTemplateFieldnames.GetFieldName( srcName ) )
740 destField->SetVisible( srcTemplate->m_Visible );
741 else
742 destField->SetVisible( false );
743
744 destField->SetTextPos( symbol->GetPosition() );
745 symbolModified = true;
746 }
747
748 if( !destField )
749 continue;
750
751 // Reference is not editable from this dialog
752 if( destField->GetId() == FIELD_T::REFERENCE )
753 continue;
754
755 wxString previousValue = destField->GetText( &m_symbolsList[i].GetSheetPath(), variantName );
756
757 destField->SetText( symbol->Schematic()->ConvertRefsToKIIDs( srcValue ),
758 &m_symbolsList[i].GetSheetPath(), variantName );
759
760 if( !createField && ( previousValue != srcValue ) )
761 symbolModified = true;
762 }
763 }
764
765 for( int ii = static_cast<int>( symbol->GetFields().size() ) - 1; ii >= 0; ii-- )
766 {
767 if( symbol->GetFields()[ii].IsMandatory() || symbol->GetFields()[ii].IsPrivate() )
768 continue;
769
770 const wxString& existingName = symbol->GetFields()[ii].GetName();
771
772 if( storedFieldIsRemoved( m_symbolsList[i], existingName ) )
773 {
774 symbol->RemoveField( existingName );
775 symbolModified = true;
776 }
777 }
778
779 if( symbolModified && ( symbol != nextSymbol ) )
780 aCommit.Modified( symbol, symbolCopy.release(), m_symbolsList[i].GetSheetPath().LastScreen() );
781
782 // Only reset the modified flag and next symbol copy if the next symbol is different from the current one.
783 if( symbol != nextSymbol )
784 {
785 if( nextSymbol )
786 symbolCopy = std::make_unique<SCH_SYMBOL>( *nextSymbol );
787 else
788 symbolCopy.reset( nullptr );
789
790 symbolModified = false;
791 }
792 }
793
794 for( const SCH_REFERENCE& ref : m_symbolsList )
795 acceptDataStoreItem( ref );
796
797 m_edited = false;
798}
799
800
802{
803 bool refListChanged = false;
804
805 for( const SCH_REFERENCE& ref : aRefs )
806 {
807 if( !m_symbolsList.Contains( ref ) )
808 {
809 const SCH_REFERENCE* existingRef = nullptr;
810
811 // A field belongs to the symbol object not reference, so an additional sheet path must have
812 // the same field presence as the paths which already reach that symbol. Field values may
813 // still differ by path when editing a variant.
814 for( unsigned ii = 0; ii < m_symbolsList.GetCount(); ++ii )
815 {
816 if( m_symbolsList[ii].GetSymbol() == ref.GetSymbol() )
817 {
818 existingRef = &m_symbolsList[ii];
819 break;
820 }
821 }
822
823 if( existingRef )
824 {
825 const auto& existingFields = m_dataStore[getDataStoreKey( *existingRef )];
826
827 for( const auto& [name, source] : existingFields )
828 {
829 FIELD_STORE_VALUE& field = storedField( ref, name );
830 field.m_present = source.m_present;
831
832 for( const auto& [variant, sourceState] : source.m_variants )
833 {
834 FIELD_VALUE_STATE& state = field.m_variants[variant];
835 getLiveFieldValueForVariant( ref, name, variant, state.m_baseline );
836 state.m_value = variant.IsEmpty() ? sourceState.m_value : state.m_baseline;
837 }
838 }
839 }
840 else
841 {
843 }
844
845 m_symbolsList.AddItem( ref );
846
847 refListChanged = true;
848 }
849 }
850
851 if( refListChanged )
852 m_symbolsList.SortBySymbolPtr();
853}
854
855
857{
858 // The schematic event listener passes us the symbol after it has been removed,
859 // so we can't just work with a SCH_REFERENCE_LIST like the other handlers as the
860 // references are already gone. Instead we need to prune our list.
861
862 // Since we now use full KIID_PATH as keys, we need to find and remove all entries
863 // that correspond to this symbol (their keys end with the symbol's UUID)
864 KIID symbolUuid = aSymbol.m_Uuid;
865 std::vector<KIID_PATH> keysToRemove;
866
867 for( const auto& [key, value] : m_dataStore )
868 {
869 if( !key.empty() && ( key.back() == symbolUuid ) )
870 keysToRemove.push_back( key );
871 }
872
873 for( const KIID_PATH& key : keysToRemove )
874 m_dataStore.erase( key );
875
876 // Remove all refs that match this symbol using remove_if
877 m_symbolsList.erase( std::remove_if( m_symbolsList.begin(), m_symbolsList.end(),
878 [&aSymbol]( const SCH_REFERENCE& ref ) -> bool
879 {
880 return ref.GetSymbol()->m_Uuid == aSymbol.m_Uuid;
881 } ),
882 m_symbolsList.end() );
883}
884
885
887{
888 for( const SCH_REFERENCE& ref : aRefs )
889 {
890 int index = m_symbolsList.FindRefByFullPath( ref.GetFullPath() );
891
892 if( index != -1 )
893 {
894 m_dataStore.erase( getDataStoreKey( ref ) );
895 m_symbolsList.RemoveItem( index );
896 }
897 }
898}
899
900
902{
903 bool refListChanged = false;
904 std::set<SCH_SYMBOL*> updatedSymbols;
905
906 for( const SCH_REFERENCE& incomingRef : aRefs )
907 {
908 if( incomingRef.GetSymbol() )
909 updatedSymbols.insert( incomingRef.GetSymbol() );
910
911 SCH_REFERENCE* cachedRef = m_symbolsList.FindItem( incomingRef );
912
913 // This looks like it might be assigning to itself because it often is
914 if( cachedRef )
915 {
916 // When these things don't happen to be the same pointer, this will update our
917 // cached reference's stuff like reference/unit/etc, but not the pointer to the symbol itself
918 *cachedRef = incomingRef;
919 }
920 else
921 {
922 m_symbolsList.AddItem( incomingRef );
923 refListChanged = true;
924 }
925 }
926
927 if( refListChanged )
928 m_symbolsList.SortBySymbolPtr();
929
930 // Field presence is on the symbol object, so refresh every path/instance which reaches a
931 // changed symbol even if the event supplied only one path.
932 for( unsigned ii = 0; ii < m_symbolsList.GetCount(); ++ii )
933 {
934 const SCH_REFERENCE& ref = m_symbolsList[ii];
935
936 if( !updatedSymbols.contains( ref.GetSymbol() ) )
937 continue;
938
940 }
941
943}
944
945
946bool SYMBOL_FIELDS_EDITOR_GRID_DATA_MODEL::DeleteRows( size_t aPosition, size_t aNumRows )
947{
948 size_t curNumRows = m_rows.size();
949
950 if( aPosition >= curNumRows )
951 {
952 wxFAIL_MSG( wxString::Format( wxT( "Called SYMBOL_FIELDS_EDITOR_GRID_DATA_MODEL::DeleteRows(aPosition=%lu, "
953 "aNumRows=%lu)\nPosition value is invalid for present table with %lu rows" ),
954 (unsigned long) aPosition, (unsigned long) aNumRows,
955 (unsigned long) curNumRows ) );
956
957 return false;
958 }
959
960 if( aNumRows > curNumRows - aPosition )
961 {
962 aNumRows = curNumRows - aPosition;
963 }
964
965 if( aNumRows >= curNumRows )
966 {
967 m_rows.clear();
968 m_dataStore.clear();
969 m_symbolsList.Clear();
970
971 if( GetView() )
972 {
973 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPosition, aNumRows );
974 GetView()->ProcessTableMessage( msg );
975 }
976 }
977 else
978 {
979 // Note: this code is currently dead, as all current usage calls the clear path above,
980 // which is left because it is faster. This code *should* be correct but is untested.
981 auto first = m_rows.begin() + aPosition;
982 auto last = first + aNumRows;
983
984 SCH_REFERENCE_LIST refsToDelete;
985 for( auto it = first; it != last && it != m_rows.end(); ++it )
986 {
987 for( const SCH_REFERENCE& ref : it->m_items )
988 refsToDelete.AddItem( ref );
989 }
990
991 RemoveReferences( refsToDelete );
992 // This will also notify the view
993 RebuildRows();
994 }
995
996 return true;
997}
998
999
1000std::vector<FIELD_CASE_CONFLICT> DetectFieldCaseConflicts( const SCH_REFERENCE_LIST& aSymbols )
1001{
1002 std::vector<FIELD_CASE_CONFLICT> conflicts;
1003
1004 for( unsigned i = 0; i < aSymbols.GetCount(); ++i )
1005 {
1006 SCH_SYMBOL* symbol = aSymbols[i].GetSymbol();
1007
1008 if( !symbol )
1009 continue;
1010
1011 std::map<wxString, std::vector<std::pair<wxString, wxString>>> groups;
1012
1013 for( const SCH_FIELD& field : symbol->GetFields() )
1014 {
1015 if( field.IsMandatory() || field.IsPrivate() )
1016 continue;
1017
1018 groups[field.GetName().Lower()].emplace_back( field.GetName(), field.GetText() );
1019 }
1020
1021 for( const auto& [key, members] : groups )
1022 {
1023 if( members.size() < 2 )
1024 continue;
1025
1027 c.symbol = symbol;
1028 c.sheetPath = aSymbols[i].GetSheetPath();
1029 c.reference = symbol->GetRef( &c.sheetPath );
1030 c.caseFoldedKey = key;
1031 c.variants = members;
1032 conflicts.push_back( std::move( c ) );
1033 }
1034 }
1035
1036 return conflicts;
1037}
int index
const char * name
COMMIT & Modified(EDA_ITEM *aItem, EDA_ITEM *aCopy, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition commit.cpp:224
const KIID m_Uuid
Definition eda_item.h:599
virtual void SetTextPos(const VECTOR2I &aPoint)
Definition eda_text.cpp:557
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:348
virtual EDA_ANGLE GetTextAngle() const
Definition eda_text.h:178
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition eda_text.cpp:269
std::map< KIID_PATH, std::map< wxString, FIELD_STORE_VALUE > > m_dataStore
void applyResolvedTextRenderer(wxGridCellAttr *aAttr, bool aApplyTint)
virtual bool fieldIsAttribute(const wxString &aFieldName) const
wxGridCellAttr * applyCellDecorations(wxGridCellAttr *aAttr, int aRow, int aCol)
std::unordered_set< KIID_PATH > m_selectionItems
void RevertRow(int aRow) override
Go through and revert all the fields in the row to their live values from the item (symbol/footprint/...
void updateDataStoreItemFieldFromLive(const SCH_REFERENCE &aItem, const wxString &aFieldName, bool aAllVariants=false)
bool groupMatch(const SCH_REFERENCE &lhItem, const SCH_REFERENCE &rhItem)
void ensureStoredFieldPresent(const SCH_REFERENCE &aItem, const wxString &aFieldName)
void setStoredFieldValue(const SCH_REFERENCE &aItem, const wxString &aFieldName, const wxString &aValue)
void initializeDataStoreItem(const SCH_REFERENCE &aItem)
bool storedFieldIsRemoved(const SCH_REFERENCE &aItem, const wxString &aName) const
void clearStoredField(const SCH_REFERENCE &aItem, const wxString &aFieldName)
bool getStoredFieldValue(const SCH_REFERENCE &aItem, const wxString &aFieldName, wxString &aValue) const
std::vector< DATA_MODEL_ROW< SCH_REFERENCE > > m_rows
FIELD_STORE_VALUE & storedField(const SCH_REFERENCE &aItem, const wxString &aFieldName)
bool resolveStoredTextVar(const SCH_REFERENCE &aItem, wxString *aToken, bool aCaseSensitive=false) const
bool IsCellReadOnly(int aRow, int aCol) override
std::map< wxString, wxString > getStoredFields(const SCH_REFERENCE &aItem, const wxString &aVariant) const
void refreshDataStoreItem(const SCH_REFERENCE &aItem)
bool MatchesFilter(const SCH_REFERENCE &aItem, const wxString &aReference, EDA_COMBINED_MATCHER &aMatcher)
void acceptDataStoreItem(const SCH_REFERENCE &aItem)
std::set< wxString > storedVariants(const SCH_REFERENCE &aItem) const
Definition kiid.h:46
wxString ConvertKIIDsToRefs(const wxString &aSource) const
wxString ConvertRefsToKIIDs(const wxString &aSource) const
Commit for the schematic and symbol editors.
Definition sch_commit.h:54
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
wxString GetShownText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString, int aDepth=0) const
void SetText(const wxString &aText) override
void SetPrivate(bool aPrivate)
Definition sch_item.h:258
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:302
bool IsPrivate() const
Definition sch_item.h:259
bool ResolveExcludedFromBOM(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const
Definition sch_item.cpp:392
bool ResolveDNP(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const
Definition sch_item.cpp:446
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
void AddItem(const SCH_REFERENCE &aItem)
A helper to define a symbol's reference designator in a schematic.
void SetSymbolExcludedFromBoard(bool aEnable)
const SCH_SHEET_PATH & GetSheetPath() const
void SetSymbolExcludedFromBOM(bool aEnable, const wxString &aVariant=wxEmptyString)
const wxString & GetRefSuffix() const
Return the sheet suffix this reference composes with, or empty while it is unannotated.
wxString GetFullRef() const
Return reference name with unit and sheet suffix altogether, as in U12A_M4.
bool GetSymbolExcludedFromBOM(const wxString &aVariant=wxEmptyString) const
bool GetSymbolDNP(const wxString &aVariant=wxEmptyString) const
SCH_SYMBOL * GetSymbol() const
bool GetSymbolExcludedFromPosFiles(const wxString &aVariant=wxEmptyString) const
void SetSymbolExcludedFromSim(bool aEnable, const wxString &aVariant=wxEmptyString)
wxString GetRef() const
bool GetSymbolExcludedFromBoard() const
bool GetSymbolExcludedFromSim(const wxString &aVariant=wxEmptyString) const
void SetSymbolExcludedFromPosFiles(bool aEnable, const wxString &aVariant=wxEmptyString)
bool IsMultiUnit() const
wxString GetRefNumber() const
void SetSymbolDNP(bool aEnable, const wxString &aVariant=wxEmptyString)
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
bool GetExcludedFromBOM() const
KIID_PATH Path() const
Get the sheet path as an KIID_PATH.
bool IsContainedWithin(const SCH_SHEET_PATH &aSheetPathToTest) const
Check if this path is contained inside aSheetPathToTest.
bool GetDNP() const
Schematic symbol object.
Definition sch_symbol.h:76
void RemoveField(const wxString &aFieldName)
Remove a user field from the symbol.
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
VECTOR2I GetPosition() const override
Definition sch_symbol.h:958
bool ResolveTextVar(const SCH_SHEET_PATH *aPath, wxString *token, int aDepth=0) const
Resolve any references to system tokens supported by the symbol.
SCH_FIELD * AddField(const SCH_FIELD &aField)
Add a field to the symbol.
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
wxString getItemRefSuffix(const SCH_REFERENCE &aItem) const override
Return the sheet suffix shown after the item's identifier, which is a base reference.
void RemoveReferences(const SCH_REFERENCE_LIST &aRefs)
wxString getAttributeValue(const SCH_REFERENCE &aRef, const wxString &aAttributeName, const wxString &aVariantNames)
void setCellValues(int aRow, int aCol, const std::vector< wxString > &aValues) override
Write one value per item represented by the cell. Models may propagate shared storage.
wxString getFieldResolvedLiveValue(const SCH_REFERENCE &aRef, const wxString &aFieldName) override
Explicitly bypasses the data store's field values and retries them from the item's current field valu...
void UpdateReferences(const SCH_REFERENCE_LIST &aRefs)
wxString getItemIdentifier(const SCH_REFERENCE &aItem) const override
bool setAttributeValue(SCH_REFERENCE &aRef, const wxString &aAttributeName, const wxString &aValue, const wxString &aVariantName=wxEmptyString)
Set the attribute value.
SCH_REFERENCE_LIST m_symbolsList
The flattened by hierarchy list of symbols.
void RemoveSymbol(const SCH_SYMBOL &aSymbol)
bool fieldSupportsVariants(const wxString &aFieldName) const override
bool getLiveFieldValue(const SCH_REFERENCE &aRef, const wxString &aFieldName, wxString &aValue) override
Gets the current value of the field from the live item, e.g.
void RevertRow(int aRow) override
Go through and revert all the fields in the row to their live values from the item (symbol/footprint/...
void AddReferences(const SCH_REFERENCE_LIST &aRefs)
void ApplyData(SCH_COMMIT &aCommit, TEMPLATES &aTemplateFieldnames)
wxString resolveTextVars(const SCH_REFERENCE &aRef, const wxString &aText) override
bool unitMatch(const SCH_REFERENCE &lhItem, const SCH_REFERENCE &rhItem) override
void ClearCell(int aRow, int aCol) override
Clears the field from the data store, rather than setting its value to an empty string.
bool getLiveFieldValueForVariant(const SCH_REFERENCE &aRef, const wxString &aFieldName, const wxString &aVariantName, wxString &aValue) override
void SetValue(int aRow, int aCol, const wxString &aValue) override
wxGridCellAttr * GetAttr(int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind) override
KIID_PATH getDataStoreKey(const SCH_REFERENCE &aItem) const override
Create a unique key for the data store by combining the KIID_PATH from the SCH_SHEET_PATH with the sy...
bool attributeForcedOnBySheet(const SCH_REFERENCE &aRef, const wxString &aAttributeName, const wxString &aVariantName) const override
wxString getDefaultFieldValue(const SCH_REFERENCE &aRef, const wxString &aFieldName)
Get the default (non-variant) value for a field.
std::vector< SCH_REFERENCE > getAllItems() const override
bool storageIsSharedAcrossPaths(const wxString &aFieldName) const
Test whether a field's storage is common to every sheet path that reaches a symbol.
bool DeleteRows(size_t aPosition=0, size_t aNumRows=1) override
const TEMPLATE_FIELDNAME * GetFieldName(const wxString &aName)
Search for aName in the template field name list.
wxGridCellAttr * enhanceAttr(wxGridCellAttr *aInputAttr, int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind)
Definition wx_grid.cpp:44
std::map< int, wxGridCellAttr * > m_colAttrs
Definition wx_grid.h:133
wxGridCellAttr * GetAttr(int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind) override
Definition wx_grid.h:96
bool IsGeneratedField(const wxString &aFieldName)
Returns true if the entire string is generated, e.g is a single text var reference.
Definition common.cpp:494
wxString ResolveTextVars(const wxString &aSource, const std::function< bool(wxString *)> *aResolver, int &aDepth)
Multi-pass text variable expansion and math expression evaluation.
Definition common.cpp:333
@ INTERNAL
Definition common.h:96
@ CTX_SEARCH
std::string source
const wxColour VARIANT_FIELD_OVERRIDE_DARK_YELLOW(80, 80, 40)
const wxColour VARIANT_FIELD_OVERRIDE_LIGHT_YELLOW(255, 255, 200)
const wxColour VARIANT_SYMBOL_OVERRIDE_LIGHT_BLUE(220, 235, 255)
const wxColour VARIANT_SYMBOL_OVERRIDE_DARK_BLUE(40, 60, 80)
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
The point of the data model classes is fundamentally to represent three things:
std::vector< ITEM_TYPE > m_items
std::vector< std::pair< wxString, wxString > > variants
std::map< wxString, FIELD_VALUE_STATE > m_variants
Hold a name of a symbol's field, field value, and default visibility.
std::vector< FIELD_CASE_CONFLICT > DetectFieldCaseConflicts(const SCH_REFERENCE_LIST &aSymbols)
DATA_MODEL_ROW< SCH_REFERENCE > SYMBOL_FIELDS_TABLE_DATA_MODEL_ROW
wxString GetDefaultFieldName(FIELD_T aFieldId, TRANSLATION aTranslation)
Return a default symbol field name for a mandatory field type.
@ USER
The field ID hasn't been set yet; field is invalid.
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ UNTRANSLATED
std::string path
#define INDETERMINATE_STATE
Used for holding indeterminate values, such as with multiple selections holding different values or c...
Definition ui_common.h:48