forked from KiCad/kicad-source-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfields_data_model.cpp
More file actions
1632 lines (1288 loc) · 51 KB
/
fields_data_model.cpp
File metadata and controls
1632 lines (1288 loc) · 51 KB
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2023 <author>
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <wx/string.h>
#include <wx/debug.h>
#include <wx/grid.h>
#include <wx/settings.h>
#include <common.h>
#include <widgets/wx_grid.h>
#include <sch_reference_list.h>
#include <sch_commit.h>
#include <sch_screen.h>
#include "string_utils.h"
#include "fields_data_model.h"
/**
* Create a unique key for the data store by combining the #KIID_PATH from the
* #SCH_SHEET_PATH with the symbol's UUID.
*
* @param aSheetPath The sheet path containing the symbol
* @param aSymbol The symbol to create a key for
* @return A KIID_PATH representing the full #SCH_SHEET_PATH + symbol UUID.
*/
static KIID_PATH makeDataStoreKey( const SCH_SHEET_PATH& aSheetPath, const SCH_SYMBOL& aSymbol )
{
KIID_PATH path = aSheetPath.Path();
path.push_back( aSymbol.m_Uuid );
return path;
}
wxString VIEW_CONTROLS_GRID_DATA_MODEL::GetColLabelValue( int aCol )
{
switch( aCol )
{
case DISPLAY_NAME_COLUMN: return _( "Field" );
case LABEL_COLUMN: return m_forBOM ? _( "BOM Name" ) : _( "Label" );
case SHOW_FIELD_COLUMN: return _( "Include" );
case GROUP_BY_COLUMN: return _( "Group By" );
default: return wxT( "unknown column" );
};
}
wxString VIEW_CONTROLS_GRID_DATA_MODEL::GetValue( int aRow, int aCol )
{
wxCHECK( aRow < GetNumberRows(), wxT( "bad row!" ) );
BOM_FIELD& rowData = m_fields[aRow];
switch( aCol )
{
case DISPLAY_NAME_COLUMN:
for( FIELD_T fieldId : MANDATORY_FIELDS )
{
if( GetDefaultFieldName( fieldId, !DO_TRANSLATE ) == rowData.name )
return GetDefaultFieldName( fieldId, DO_TRANSLATE );
}
return rowData.name;
case LABEL_COLUMN:
return rowData.label;
default:
// we can't assert here because wxWidgets sometimes calls this without checking
// the column type when trying to see if there's an overflow
return wxT( "bad wxWidgets!" );
}
}
bool VIEW_CONTROLS_GRID_DATA_MODEL::GetValueAsBool( int aRow, int aCol )
{
wxCHECK( aRow < GetNumberRows(), false );
BOM_FIELD& rowData = m_fields[aRow];
switch( aCol )
{
case SHOW_FIELD_COLUMN: return rowData.show;
case GROUP_BY_COLUMN: return rowData.groupBy;
default:
wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a bool value" ), aCol ) );
return false;
}
}
void VIEW_CONTROLS_GRID_DATA_MODEL::SetValue( int aRow, int aCol, const wxString &aValue )
{
wxCHECK( aRow < GetNumberRows(), /*void*/ );
BOM_FIELD& rowData = m_fields[aRow];
switch( aCol )
{
case DISPLAY_NAME_COLUMN:
// Not editable
break;
case LABEL_COLUMN:
rowData.label = aValue;
break;
default:
wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a string value" ), aCol ) );
}
GetView()->Refresh();
}
void VIEW_CONTROLS_GRID_DATA_MODEL::SetValueAsBool( int aRow, int aCol, bool aValue )
{
wxCHECK( aRow < GetNumberRows(), /*void*/ );
BOM_FIELD& rowData = m_fields[aRow];
switch( aCol )
{
case SHOW_FIELD_COLUMN: rowData.show = aValue; break;
case GROUP_BY_COLUMN: rowData.groupBy = aValue; break;
default:
wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a bool value" ), aCol ) );
}
}
void VIEW_CONTROLS_GRID_DATA_MODEL::AppendRow( const wxString& aFieldName, const wxString& aBOMName,
bool aShow, bool aGroupBy )
{
m_fields.emplace_back( BOM_FIELD{ aFieldName, aBOMName, aShow, aGroupBy } );
if( wxGrid* grid = GetView() )
{
wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
grid->ProcessTableMessage( msg );
}
}
void VIEW_CONTROLS_GRID_DATA_MODEL::DeleteRow( int aRow )
{
wxCHECK( aRow >= 0 && aRow < GetNumberRows(), /* void */ );
m_fields.erase( m_fields.begin() + aRow );
if( wxGrid* grid = GetView() )
{
wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aRow, 1 );
grid->ProcessTableMessage( msg );
}
}
wxString VIEW_CONTROLS_GRID_DATA_MODEL::GetCanonicalFieldName( int aRow )
{
wxCHECK( aRow >= 0 && aRow < GetNumberRows(), wxEmptyString );
BOM_FIELD& rowData = m_fields[aRow];
return rowData.name;
}
void VIEW_CONTROLS_GRID_DATA_MODEL::SetCanonicalFieldName( int aRow, const wxString& aName )
{
wxCHECK( aRow >= 0 && aRow < GetNumberRows(), /* void */ );
BOM_FIELD& rowData = m_fields[aRow];
rowData.name = aName;
}
const wxString FIELDS_EDITOR_GRID_DATA_MODEL::QUANTITY_VARIABLE = wxS( "${QUANTITY}" );
const wxString FIELDS_EDITOR_GRID_DATA_MODEL::ITEM_NUMBER_VARIABLE = wxS( "${ITEM_NUMBER}" );
void FIELDS_EDITOR_GRID_DATA_MODEL::AddColumn( const wxString& aFieldName, const wxString& aLabel,
bool aAddedByUser, const wxString& aVariantName )
{
// Don't add a field twice
if( GetFieldNameCol( aFieldName ) != -1 )
return;
m_cols.push_back( { aFieldName, aLabel, aAddedByUser, false, false } );
for( unsigned i = 0; i < m_symbolsList.GetCount(); ++i )
updateDataStoreSymbolField( m_symbolsList[i], aFieldName, aVariantName );
}
void FIELDS_EDITOR_GRID_DATA_MODEL::updateDataStoreSymbolField( const SCH_REFERENCE& aSymbolRef,
const wxString& aFieldName,
const wxString& aVariantName )
{
const SCH_SYMBOL* symbol = aSymbolRef.GetSymbol();
if( !symbol )
return;
KIID_PATH key = makeDataStoreKey( aSymbolRef.GetSheetPath(), *symbol );
if( isAttribute( aFieldName ) )
{
m_dataStore[key][aFieldName] = getAttributeValue( aSymbolRef, aFieldName, aVariantName );
}
else if( const SCH_FIELD* field = symbol->GetField( aFieldName ) )
{
if( field->IsPrivate() )
{
m_dataStore[key][aFieldName] = wxEmptyString;
return;
}
wxString value = symbol->Schematic()->ConvertKIIDsToRefs( field->GetText( &aSymbolRef.GetSheetPath(),
aVariantName ) );
m_dataStore[key][aFieldName] = value;
}
else if( IsGeneratedField( aFieldName ) )
{
// Handle generated fields with variables as names (e.g. ${QUANTITY}) that are not present in
// the symbol by giving them the correct value
m_dataStore[key][aFieldName] = aFieldName;
}
else
{
m_dataStore[key][aFieldName] = wxEmptyString;
}
}
void FIELDS_EDITOR_GRID_DATA_MODEL::RemoveColumn( int aCol )
{
for( unsigned i = 0; i < m_symbolsList.GetCount(); ++i )
{
if( SCH_SYMBOL* symbol = m_symbolsList[i].GetSymbol() )
{
KIID_PATH key = makeDataStoreKey( m_symbolsList[i].GetSheetPath(), *symbol );
m_dataStore[key].erase( m_cols[aCol].m_fieldName );
}
}
m_cols.erase( m_cols.begin() + aCol );
if( wxGrid* grid = GetView() )
{
wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_COLS_DELETED, aCol, 1 );
grid->ProcessTableMessage( msg );
}
}
void FIELDS_EDITOR_GRID_DATA_MODEL::RenameColumn( int aCol, const wxString& newName )
{
for( unsigned i = 0; i < m_symbolsList.GetCount(); ++i )
{
SCH_SYMBOL* symbol = m_symbolsList[i].GetSymbol();
KIID_PATH key = makeDataStoreKey( m_symbolsList[i].GetSheetPath(), *symbol );
// Careful; field may have already been renamed from another sheet instance
if( auto node = m_dataStore[key].extract( m_cols[aCol].m_fieldName ) )
{
node.key() = newName;
m_dataStore[key].insert( std::move( node ) );
}
}
m_cols[aCol].m_fieldName = newName;
m_cols[aCol].m_label = newName;
}
int FIELDS_EDITOR_GRID_DATA_MODEL::GetFieldNameCol( const wxString& aFieldName ) const
{
for( size_t i = 0; i < m_cols.size(); i++ )
{
if( m_cols[i].m_fieldName == aFieldName )
return static_cast<int>( i );
}
return -1;
}
std::vector<BOM_FIELD> FIELDS_EDITOR_GRID_DATA_MODEL::GetFieldsOrdered()
{
std::vector<BOM_FIELD> fields;
for( const DATA_MODEL_COL& col : m_cols )
fields.push_back( { col.m_fieldName, col.m_label, col.m_show, col.m_group } );
return fields;
}
void FIELDS_EDITOR_GRID_DATA_MODEL::SetFieldsOrder( const std::vector<wxString>& aNewOrder )
{
size_t foundCount = 0;
for( const wxString& newField : aNewOrder )
{
if( foundCount >= m_cols.size() )
break;
for( DATA_MODEL_COL& col : m_cols )
{
if( col.m_fieldName == newField )
{
std::swap( m_cols[foundCount], col );
foundCount++;
break;
}
}
}
}
bool FIELDS_EDITOR_GRID_DATA_MODEL::IsExpanderColumn( int aCol ) const
{
// Check if aCol is the first visible column
for( int col = 0; col < aCol; ++col )
{
if( m_cols[col].m_show )
return false;
}
return true;
}
wxString FIELDS_EDITOR_GRID_DATA_MODEL::GetValue( int aRow, int aCol )
{
GetView()->SetReadOnly( aRow, aCol, IsExpanderColumn( aCol ) );
return GetValue( m_rows[aRow], aCol );
}
wxGridCellAttr* FIELDS_EDITOR_GRID_DATA_MODEL::GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind )
{
wxGridCellAttr* attr = nullptr;
bool needsUrlEditor = false;
bool needsVariantHighlight = false;
wxColour highlightColor;
// Check if we need URL editor
if( GetColFieldName( aCol ) == GetCanonicalFieldName( FIELD_T::DATASHEET )
|| IsURL( GetValue( m_rows[aRow], aCol ) ) )
{
if( m_urlEditor )
needsUrlEditor = true;
}
// Check if we need variant highlighting
if( !m_currentVariant.IsEmpty() && aRow >= 0 && aRow < (int) m_rows.size()
&& aCol >= 0 && aCol < (int) m_cols.size() )
{
const wxString& fieldName = m_cols[aCol].m_fieldName;
// Skip Reference and generated fields (like ${QUANTITY}) for highlighting
if( !ColIsReference( aCol ) && !ColIsQuantity( aCol ) && !ColIsItemNumber( aCol ) )
{
const DATA_MODEL_ROW& row = m_rows[aRow];
// Check if any symbol in this row has a variant-specific value
for( const SCH_REFERENCE& ref : row.m_Refs )
{
wxString defaultValue = getDefaultFieldValue( ref, fieldName );
KIID_PATH symbolKey = KIID_PATH();
if( const SCH_SYMBOL* symbol = ref.GetSymbol() )
{
symbolKey = ref.GetSheetPath().Path();
symbolKey.push_back( symbol->m_Uuid );
}
// Get the current value from the data store
wxString currentValue;
if( m_dataStore.contains( symbolKey ) && m_dataStore[symbolKey].contains( fieldName ) )
currentValue = m_dataStore[symbolKey][fieldName];
if( currentValue != defaultValue )
{
needsVariantHighlight = true;
// Use a subtle highlight color that works in both light and dark themes
wxColour bg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
bool isDark = ( bg.Red() + bg.Green() + bg.Blue() ) < 384;
if( isDark )
highlightColor = wxColour( 80, 80, 40 ); // Dark gold/brown
else
highlightColor = wxColour( 255, 255, 200 ); // Light yellow
break;
}
}
}
}
// If we don't need any custom attributes, use the base class behavior
if( !needsUrlEditor && !needsVariantHighlight )
return WX_GRID_TABLE_BASE::GetAttr( aRow, aCol, aKind );
// URL cells: use m_urlEditor as base, potentially with variant highlight overlay
if( needsUrlEditor )
{
if( needsVariantHighlight )
{
// Clone the URL editor attribute and add highlight color
attr = m_urlEditor->Clone();
attr->SetBackgroundColour( highlightColor );
}
else
{
// Just use the URL editor attribute directly
m_urlEditor->IncRef();
attr = m_urlEditor;
}
return enhanceAttr( attr, aRow, aCol, aKind );
}
// Non-URL cells with variant highlighting: start with column attributes if they exist.
// This preserves checkbox renderers and other column-specific settings.
if( m_colAttrs.find( aCol ) != m_colAttrs.end() && m_colAttrs[aCol] )
{
attr = m_colAttrs[aCol]->Clone();
}
else
{
attr = new wxGridCellAttr();
}
attr->SetBackgroundColour( highlightColor );
return enhanceAttr( attr, aRow, aCol, aKind );
}
wxString FIELDS_EDITOR_GRID_DATA_MODEL::GetValue( const DATA_MODEL_ROW& group, int aCol,
const wxString& refDelimiter,
const wxString& refRangeDelimiter,
bool resolveVars,
bool listMixedValues )
{
std::vector<SCH_REFERENCE> references;
std::set<wxString> mixedValues;
wxString fieldValue;
for( const SCH_REFERENCE& ref : group.m_Refs )
{
if( ColIsReference( aCol ) || ColIsQuantity( aCol ) || ColIsItemNumber( aCol ) )
{
references.push_back( ref );
}
else // Other columns are either a single value or ROW_MULTI_ITEMS
{
KIID_PATH symbolKey = makeDataStoreKey( ref.GetSheetPath(), *ref.GetSymbol() );
if( !m_dataStore.contains( symbolKey ) || !m_dataStore[symbolKey].contains( m_cols[aCol].m_fieldName ) )
return INDETERMINATE_STATE;
wxString refFieldValue = m_dataStore[symbolKey][m_cols[aCol].m_fieldName];
if( resolveVars )
{
if( IsGeneratedField( m_cols[aCol].m_fieldName ) )
{
// Generated fields (e.g. ${QUANTITY}) can't have un-applied values as they're
// read-only. Resolve them against the field.
refFieldValue = getFieldShownText( ref, m_cols[aCol].m_fieldName );
}
else if( refFieldValue.Contains( wxT( "${" ) ) )
{
// Resolve variables in the un-applied value using the parent symbol and instance
// data.
std::function<bool( wxString* )> symbolResolver =
[&]( wxString* token ) -> bool
{
return ref.GetSymbol()->ResolveTextVar( &ref.GetSheetPath(), token );
};
refFieldValue = ExpandTextVars( refFieldValue, & symbolResolver );
}
}
if( listMixedValues )
mixedValues.insert( refFieldValue );
else if( &ref == &group.m_Refs.front() )
fieldValue = refFieldValue;
else if( fieldValue != refFieldValue )
return INDETERMINATE_STATE;
}
}
if( listMixedValues )
{
fieldValue = wxEmptyString;
for( const wxString& value : mixedValues )
{
if( value.IsEmpty() )
continue;
else if( fieldValue.IsEmpty() )
fieldValue = value;
else
fieldValue += "," + value;
}
}
if( ColIsReference( aCol ) || ColIsQuantity( aCol ) || ColIsItemNumber( aCol ) )
{
// Remove duplicates (other units of multi-unit parts)
std::sort( references.begin(), references.end(),
[]( const SCH_REFERENCE& l, const SCH_REFERENCE& r ) -> bool
{
wxString l_ref( l.GetRef() << l.GetRefNumber() );
wxString r_ref( r.GetRef() << r.GetRefNumber() );
return StrNumCmp( l_ref, r_ref, true ) < 0;
} );
auto logicalEnd = std::unique( references.begin(), references.end(),
[]( const SCH_REFERENCE& l, const SCH_REFERENCE& r ) -> bool
{
// If unannotated then we can't tell what units belong together
// so we have to leave them all
if( l.GetRefNumber() == wxT( "?" ) )
return false;
wxString l_ref( l.GetRef() << l.GetRefNumber() );
wxString r_ref( r.GetRef() << r.GetRefNumber() );
return l_ref == r_ref;
} );
references.erase( logicalEnd, references.end() );
}
if( ColIsReference( aCol ) )
fieldValue = SCH_REFERENCE_LIST::Shorthand( references, refDelimiter, refRangeDelimiter );
else if( ColIsQuantity( aCol ) )
fieldValue = wxString::Format( wxT( "%d" ), (int) references.size() );
else if( ColIsItemNumber( aCol ) && group.m_Flag != CHILD_ITEM )
fieldValue = wxString::Format( wxT( "%d" ), group.m_ItemNumber );
return fieldValue;
}
void FIELDS_EDITOR_GRID_DATA_MODEL::SetValue( int aRow, int aCol, const wxString& aValue )
{
wxCHECK_RET( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), wxS( "Invalid column number" ) );
// Can't modify references or generated fields (e.g. ${QUANTITY})
if( ColIsReference( aCol )
|| ( IsGeneratedField( m_cols[aCol].m_fieldName ) && !ColIsAttribute( aCol ) ) )
{
return;
}
DATA_MODEL_ROW& rowGroup = m_rows[aRow];
const SCH_SYMBOL* sharedSymbol = nullptr;
bool isSharedInstance = false;
for( const SCH_REFERENCE& ref : rowGroup.m_Refs )
{
const SCH_SCREEN* screen = nullptr;
// Check to see if the symbol associated with this row has more than one instance.
if( const SCH_SYMBOL* symbol = ref.GetSymbol() )
{
screen = static_cast<const SCH_SCREEN*>( symbol->GetParent() );
isSharedInstance = ( screen && ( screen->GetRefCount() > 1 ) );
sharedSymbol = symbol;
}
KIID_PATH key = makeDataStoreKey( ref.GetSheetPath(), *ref.GetSymbol() );
m_dataStore[key][m_cols[aCol].m_fieldName] = aValue;
}
// Update all of the other instances for the shared symbol as required.
if( isSharedInstance
&& ( ( rowGroup.m_Flag == GROUP_SINGLETON ) || ( rowGroup.m_Flag == CHILD_ITEM ) ) )
{
for( DATA_MODEL_ROW& row : m_rows )
{
if( row.m_ItemNumber == aRow + 1 )
continue;
for( const SCH_REFERENCE& ref : row.m_Refs )
{
if( ref.GetSymbol() != sharedSymbol )
continue;
KIID_PATH key = makeDataStoreKey( ref.GetSheetPath(), *ref.GetSymbol() );
m_dataStore[key][m_cols[aCol].m_fieldName] = aValue;
}
}
}
m_edited = true;
}
bool FIELDS_EDITOR_GRID_DATA_MODEL::ColIsReference( int aCol )
{
wxCHECK( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), false );
return m_cols[aCol].m_fieldName == GetCanonicalFieldName( FIELD_T::REFERENCE );
}
bool FIELDS_EDITOR_GRID_DATA_MODEL::ColIsValue( int aCol )
{
wxCHECK( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), false );
return m_cols[aCol].m_fieldName == GetCanonicalFieldName( FIELD_T::VALUE );
}
bool FIELDS_EDITOR_GRID_DATA_MODEL::ColIsQuantity( int aCol )
{
wxCHECK( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), false );
return m_cols[aCol].m_fieldName == QUANTITY_VARIABLE;
}
bool FIELDS_EDITOR_GRID_DATA_MODEL::ColIsItemNumber( int aCol )
{
wxCHECK( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), false );
return m_cols[aCol].m_fieldName == ITEM_NUMBER_VARIABLE;
}
bool FIELDS_EDITOR_GRID_DATA_MODEL::ColIsAttribute( int aCol )
{
wxCHECK( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), false );
return isAttribute( m_cols[aCol].m_fieldName );
}
bool FIELDS_EDITOR_GRID_DATA_MODEL::cmp( const DATA_MODEL_ROW& lhGroup,
const DATA_MODEL_ROW& rhGroup,
FIELDS_EDITOR_GRID_DATA_MODEL* dataModel, int sortCol,
bool ascending )
{
// Empty rows always go to the bottom, whether ascending or descending
if( lhGroup.m_Refs.size() == 0 )
return true;
else if( rhGroup.m_Refs.size() == 0 )
return false;
// N.B. To meet the iterator sort conditions, we cannot simply invert the truth
// to get the opposite sort. i.e. ~(a<b) != (a>b)
auto local_cmp =
[ ascending ]( const auto a, const auto b )
{
if( ascending )
return a < b;
else
return a > b;
};
// Primary sort key is sortCol; secondary is always REFERENCE (column 0)
if( sortCol < 0 || sortCol >= dataModel->GetNumberCols() )
sortCol = 0;
wxString lhs = dataModel->GetValue( lhGroup, sortCol ).Trim( true ).Trim( false );
wxString rhs = dataModel->GetValue( rhGroup, sortCol ).Trim( true ).Trim( false );
if( lhs == rhs || dataModel->ColIsReference( sortCol ) )
{
wxString lhRef = lhGroup.m_Refs[0].GetRef() + lhGroup.m_Refs[0].GetRefNumber();
wxString rhRef = rhGroup.m_Refs[0].GetRef() + rhGroup.m_Refs[0].GetRefNumber();
return local_cmp( StrNumCmp( lhRef, rhRef, true ), 0 );
}
else
{
return local_cmp( ValueStringCompare( lhs, rhs ), 0 );
}
}
void FIELDS_EDITOR_GRID_DATA_MODEL::Sort()
{
CollapseForSort();
// We're going to sort the rows based on their first reference, so the first reference
// had better be the lowest one.
for( DATA_MODEL_ROW& row : m_rows )
{
std::sort( row.m_Refs.begin(), row.m_Refs.end(),
[]( const SCH_REFERENCE& lhs, const SCH_REFERENCE& rhs )
{
wxString lhs_ref( lhs.GetRef() << lhs.GetRefNumber() );
wxString rhs_ref( rhs.GetRef() << rhs.GetRefNumber() );
return StrNumCmp( lhs_ref, rhs_ref, true ) < 0;
} );
}
std::sort( m_rows.begin(), m_rows.end(),
[this]( const DATA_MODEL_ROW& lhs, const DATA_MODEL_ROW& rhs ) -> bool
{
return cmp( lhs, rhs, this, m_sortColumn, m_sortAscending );
} );
// Time to renumber the item numbers
int itemNumber = 1;
for( DATA_MODEL_ROW& row : m_rows )
{
row.m_ItemNumber = itemNumber++;
}
ExpandAfterSort();
}
bool FIELDS_EDITOR_GRID_DATA_MODEL::unitMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef )
{
// If items are unannotated then we can't tell if they're units of the same symbol or not
if( lhRef.GetRefNumber() == wxT( "?" ) )
return false;
return ( lhRef.GetRef() == rhRef.GetRef() && lhRef.GetRefNumber() == rhRef.GetRefNumber() );
}
bool FIELDS_EDITOR_GRID_DATA_MODEL::groupMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef )
{
int refCol = GetFieldNameCol( GetCanonicalFieldName( FIELD_T::REFERENCE ) );
bool matchFound = false;
if( refCol == -1 )
return false;
// First check the reference column. This can be done directly out of the
// SCH_REFERENCEs as the references can't be edited in the grid.
if( m_cols[refCol].m_group )
{
// if we're grouping by reference, then only the prefix must match
if( lhRef.GetRef() != rhRef.GetRef() )
return false;
matchFound = true;
}
KIID_PATH lhRefKey = makeDataStoreKey( lhRef.GetSheetPath(), *lhRef.GetSymbol() );
KIID_PATH rhRefKey = makeDataStoreKey( rhRef.GetSheetPath(), *rhRef.GetSymbol() );
// Now check all the other columns.
for( size_t i = 0; i < m_cols.size(); ++i )
{
//Handled already
if( static_cast<int>( i ) == refCol )
continue;
if( !m_cols[i].m_group )
continue;
// If the field is generated (e.g. ${QUANTITY}), we need to resolve it through the symbol
// to get the actual current value; otherwise we need to pull it out of the store so the
// refresh can regroup based on values that haven't been applied to the schematic yet.
wxString lh, rh;
if( IsGeneratedField( m_cols[i].m_fieldName )
|| IsGeneratedField( m_dataStore[lhRefKey][m_cols[i].m_fieldName] ) )
{
lh = getFieldShownText( lhRef, m_cols[i].m_fieldName );
}
else
{
lh = m_dataStore[lhRefKey][m_cols[i].m_fieldName];
}
if( IsGeneratedField( m_cols[i].m_fieldName )
|| IsGeneratedField( m_dataStore[rhRefKey][m_cols[i].m_fieldName] ) )
{
rh = getFieldShownText( rhRef, m_cols[i].m_fieldName );
}
else
{
rh = m_dataStore[rhRefKey][m_cols[i].m_fieldName];
}
if( lh != rh )
return false;
matchFound = true;
}
return matchFound;
}
wxString FIELDS_EDITOR_GRID_DATA_MODEL::getFieldShownText( const SCH_REFERENCE& aRef,
const wxString& aFieldName )
{
SCH_FIELD* field = aRef.GetSymbol()->GetField( aFieldName );
if( field )
{
if( field->IsPrivate() )
return wxEmptyString;
else
return field->GetShownText( &aRef.GetSheetPath(), false );
}
// Handle generated fields with variables as names (e.g. ${QUANTITY}) that are not present in
// the symbol by giving them the correct value by resolving against the symbol
if( IsGeneratedField( aFieldName ) )
{
int depth = 0;
const SCH_SHEET_PATH& path = aRef.GetSheetPath();
std::function<bool( wxString* )> symbolResolver =
[&]( wxString* token ) -> bool
{
return aRef.GetSymbol()->ResolveTextVar( &path, token, depth + 1 );
};
return ExpandTextVars( aFieldName, &symbolResolver );
}
return wxEmptyString;
}
bool FIELDS_EDITOR_GRID_DATA_MODEL::isAttribute( const wxString& aFieldName )
{
return aFieldName == wxS( "${DNP}" )
|| aFieldName == wxS( "${EXCLUDE_FROM_BOARD}" )
|| aFieldName == wxS( "${EXCLUDE_FROM_BOM}" )
|| aFieldName == wxS( "${EXCLUDE_FROM_SIM}" );
}
wxString FIELDS_EDITOR_GRID_DATA_MODEL::getAttributeValue( const SCH_REFERENCE& aRef, const wxString& aAttributeName,
const wxString& aVariantName )
{
if( aAttributeName == wxS( "${DNP}" ) )
return aRef.GetSymbolDNP( aVariantName ) ? wxS( "1" ) : wxS( "0" );
if( aAttributeName == wxS( "${EXCLUDE_FROM_BOARD}" ) )
return aRef.GetSymbolExcludedFromBoard() ? wxS( "1" ) : wxS( "0" );
if( aAttributeName == wxS( "${EXCLUDE_FROM_BOM}" ) )
return aRef.GetSymbolExcludedFromBOM( aVariantName ) ? wxS( "1" ) : wxS( "0" );
if( aAttributeName == wxS( "${EXCLUDE_FROM_SIM}" ) )
return aRef.GetSymbolExcludedFromSim( aVariantName ) ? wxS( "1" ) : wxS( "0" );
return wxS( "0" );
}
wxString FIELDS_EDITOR_GRID_DATA_MODEL::getDefaultFieldValue( const SCH_REFERENCE& aRef,
const wxString& aFieldName )
{
const SCH_SYMBOL* symbol = aRef.GetSymbol();
if( !symbol )
return wxEmptyString;
// For attributes, get the default (non-variant) value
if( isAttribute( aFieldName ) )
return getAttributeValue( aRef, aFieldName, wxEmptyString );
// For regular fields, get the text without variant override
if( const SCH_FIELD* field = symbol->GetField( aFieldName ) )
{
if( field->IsPrivate() )
return wxEmptyString;
// Get the field text with empty variant name (default value)
wxString value = symbol->Schematic()->ConvertKIIDsToRefs(
field->GetText( &aRef.GetSheetPath(), wxEmptyString ) );
return value;
}
// For generated fields, return the field name itself
if( IsGeneratedField( aFieldName ) )
return aFieldName;
return wxEmptyString;
}
bool FIELDS_EDITOR_GRID_DATA_MODEL::setAttributeValue( SCH_REFERENCE& aRef,
const wxString& aAttributeName,
const wxString& aValue,
const wxString& aVariantName )
{
bool attrChanged = false;
bool newValue = aValue == wxS( "1" );
if( aAttributeName == wxS( "${DNP}" ) )
{
attrChanged = aRef.GetSymbolDNP( aVariantName ) != newValue;
aRef.SetSymbolDNP( newValue, aVariantName );
}
else if( aAttributeName == wxS( "${EXCLUDE_FROM_BOARD}" ) )
{
attrChanged = aRef.GetSymbolExcludedFromBoard() != newValue;
aRef.SetSymbolExcludedFromBoard( newValue );
}
else if( aAttributeName == wxS( "${EXCLUDE_FROM_BOM}" ) )
{
attrChanged = aRef.GetSymbolExcludedFromBOM( aVariantName ) != newValue;
aRef.SetSymbolExcludedFromBOM( newValue, aVariantName );
}
else if( aAttributeName == wxS( "${EXCLUDE_FROM_SIM}" ) )
{
attrChanged = aRef.GetSymbolExcludedFromSim( aVariantName ) != newValue;
aRef.SetSymbolExcludedFromSim( newValue, aVariantName );
}
return attrChanged;
}
void FIELDS_EDITOR_GRID_DATA_MODEL::EnableRebuilds()
{
m_rebuildsEnabled = true;
}
void FIELDS_EDITOR_GRID_DATA_MODEL::DisableRebuilds()
{
m_rebuildsEnabled = false;
}
void FIELDS_EDITOR_GRID_DATA_MODEL::RebuildRows()
{
if( !m_rebuildsEnabled )
return;
if( GetView() )
{
// Commit any pending in-place edits before the row gets moved out from under
// the editor.
static_cast<WX_GRID*>( GetView() )->CommitPendingChanges( true );
wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, 0, m_rows.size() );
GetView()->ProcessTableMessage( msg );
}
m_rows.clear();
EDA_COMBINED_MATCHER matcher( m_filter.Lower(), CTX_SEARCH );
for( unsigned i = 0; i < m_symbolsList.GetCount(); ++i )
{
SCH_REFERENCE ref = m_symbolsList[i];
if( !m_filter.IsEmpty() && !matcher.Find( ref.GetFullRef().Lower() ) )
continue;
if( m_excludeDNP )
{
bool isDNP = false;
if( !m_variantNames.empty() )
{
for( const wxString& variantName : m_variantNames )
{
if( ref.GetSymbol()->GetDNP( &ref.GetSheetPath(), variantName )
|| ref.GetSheetPath().GetDNP( variantName ) )
{
isDNP = true;
break;
}
}
}