-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
sch_field.cpp
1793 lines (1401 loc) · 54.3 KB
/
sch_field.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
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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
* 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
*/
/*
* Fields are texts attached to a symbol, some of which have a special meaning.
* Fields 0 and 1 are very important: reference and value.
* Field 2 is used as default footprint name.
* Field 3 is used to point to a datasheet (usually a URL).
* Fields 4+ are user fields. They can be renamed and can appear in reports.
*/
#include <wx/log.h>
#include <wx/menu.h>
#include <advanced_config.h>
#include <base_units.h>
#include <common.h> // for ExpandTextVars
#include <sch_edit_frame.h>
#include <plotters/plotter.h>
#include <bitmaps.h>
#include <core/mirror.h>
#include <kiway.h>
#include <symbol_library.h>
#include <sch_symbol.h>
#include <sch_field.h>
#include <sch_label.h>
#include <schematic.h>
#include <settings/color_settings.h>
#include <string_utils.h>
#include <trace_helpers.h>
#include <tool/tool_manager.h>
#include <tools/sch_navigate_tool.h>
#include <font/outline_font.h>
#include "sim/sim_lib_mgr.h"
static const std::vector<KICAD_T> labelTypes = { SCH_LABEL_LOCATE_ANY_T };
SCH_FIELD::SCH_FIELD( const VECTOR2I& aPos, int aFieldId, SCH_ITEM* aParent,
const wxString& aName ) :
SCH_ITEM( aParent, SCH_FIELD_T ),
EDA_TEXT( schIUScale, wxEmptyString ),
m_id( 0 ),
m_showName( false ),
m_allowAutoPlace( true ),
m_autoAdded( false ),
m_showInChooser( true ),
m_renderCacheValid( false ),
m_lastResolvedColor( COLOR4D::UNSPECIFIED )
{
if( aName.IsEmpty() )
SetName( TEMPLATE_FIELDNAME::GetDefaultFieldName( aFieldId ) );
else
SetName( aName );
SetTextPos( aPos );
SetId( aFieldId ); // will also set the layer
SetVisible( true );
}
SCH_FIELD::SCH_FIELD( SCH_ITEM* aParent, int aFieldId, const wxString& aName) :
SCH_FIELD( VECTOR2I(), aFieldId, aParent, aName )
{
}
SCH_FIELD::SCH_FIELD( const SCH_FIELD& aField ) :
SCH_ITEM( aField ),
EDA_TEXT( aField )
{
m_private = aField.m_private;
m_id = aField.m_id;
m_name = aField.m_name;
m_showName = aField.m_showName;
m_allowAutoPlace = aField.m_allowAutoPlace;
m_isNamedVariable = aField.m_isNamedVariable;
m_autoAdded = aField.m_autoAdded;
m_showInChooser = aField.m_showInChooser;
m_renderCache.clear();
for( const std::unique_ptr<KIFONT::GLYPH>& glyph : aField.m_renderCache )
{
if( KIFONT::OUTLINE_GLYPH* outline = dynamic_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() ) )
m_renderCache.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( *outline ) );
else if( KIFONT::STROKE_GLYPH* stroke = dynamic_cast<KIFONT::STROKE_GLYPH*>( glyph.get() ) )
m_renderCache.emplace_back( std::make_unique<KIFONT::STROKE_GLYPH>( *stroke ) );
}
m_renderCacheValid = aField.m_renderCacheValid;
m_renderCachePos = aField.m_renderCachePos;
m_lastResolvedColor = aField.m_lastResolvedColor;
}
SCH_FIELD& SCH_FIELD::operator=( const SCH_FIELD& aField )
{
EDA_TEXT::operator=( aField );
m_private = aField.m_private;
m_id = aField.m_id;
m_name = aField.m_name;
m_showName = aField.m_showName;
m_allowAutoPlace = aField.m_allowAutoPlace;
m_isNamedVariable = aField.m_isNamedVariable;
m_renderCache.clear();
for( const std::unique_ptr<KIFONT::GLYPH>& glyph : aField.m_renderCache )
{
if( KIFONT::OUTLINE_GLYPH* outline = dynamic_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() ) )
m_renderCache.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( *outline ) );
else if( KIFONT::STROKE_GLYPH* stroke = dynamic_cast<KIFONT::STROKE_GLYPH*>( glyph.get() ) )
m_renderCache.emplace_back( std::make_unique<KIFONT::STROKE_GLYPH>( *stroke ) );
}
m_renderCacheValid = aField.m_renderCacheValid;
m_renderCachePos = aField.m_renderCachePos;
m_lastResolvedColor = aField.m_lastResolvedColor;
return *this;
}
EDA_ITEM* SCH_FIELD::Clone() const
{
return new SCH_FIELD( *this );
}
void SCH_FIELD::Copy( SCH_FIELD* aTarget ) const
{
*aTarget = *this;
}
void SCH_FIELD::SetId( int aId )
{
m_id = aId;
if( m_parent && m_parent->Type() == SCH_SHEET_T )
{
switch( m_id )
{
case SHEETNAME: SetLayer( LAYER_SHEETNAME ); break;
case SHEETFILENAME: SetLayer( LAYER_SHEETFILENAME ); break;
default: SetLayer( LAYER_SHEETFIELDS ); break;
}
}
else if( m_parent && ( m_parent->Type() == SCH_SYMBOL_T || m_parent->Type() == LIB_SYMBOL_T ) )
{
switch( m_id )
{
case REFERENCE_FIELD: SetLayer( LAYER_REFERENCEPART ); break;
case VALUE_FIELD: SetLayer( LAYER_VALUEPART ); break;
default: SetLayer( LAYER_FIELDS ); break;
}
}
else if( m_parent && m_parent->IsType( labelTypes ) )
{
// We can't use defined IDs for labels because there can be multiple net class
// assignments.
if( GetCanonicalName() == wxT( "Netclass" )
|| GetCanonicalName() == wxT( "Component Class" ) )
{
SetLayer( LAYER_NETCLASS_REFS );
}
else if( GetCanonicalName() == wxT( "Intersheetrefs" ) )
{
SetLayer( LAYER_INTERSHEET_REFS );
}
else
{
SetLayer( LAYER_FIELDS );
}
}
}
wxString SCH_FIELD::GetShownName() const
{
return m_isNamedVariable ? GetTextVars( GetName() ) : GetName();
}
wxString SCH_FIELD::GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraText,
int aDepth ) const
{
std::function<bool( wxString* )> libSymbolResolver =
[&]( wxString* token ) -> bool
{
LIB_SYMBOL* symbol = static_cast<LIB_SYMBOL*>( m_parent );
return symbol->ResolveTextVar( token, aDepth + 1 );
};
std::function<bool( wxString* )> symbolResolver =
[&]( wxString* token ) -> bool
{
SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( m_parent );
return symbol->ResolveTextVar( aPath, token, aDepth + 1 );
};
std::function<bool( wxString* )> schematicResolver =
[&]( wxString* token ) -> bool
{
if( !aPath )
return false;
if( SCHEMATIC* schematic = Schematic() )
return schematic->ResolveTextVar( aPath, token, aDepth + 1 );
return false;
};
std::function<bool( wxString* )> sheetResolver =
[&]( wxString* token ) -> bool
{
if( !aPath )
return false;
SCH_SHEET* sheet = static_cast<SCH_SHEET*>( m_parent );
SCHEMATIC* schematic = Schematic();
SCH_SHEET_PATH path = *aPath;
path.push_back( sheet );
bool retval = sheet->ResolveTextVar( &path, token, aDepth + 1 );
if( schematic )
retval |= schematic->ResolveTextVar( &path, token, aDepth + 1 );
return retval;
};
std::function<bool( wxString* )> labelResolver =
[&]( wxString* token ) -> bool
{
if( !aPath )
return false;
SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( m_parent );
return label->ResolveTextVar( aPath, token, aDepth + 1 );
};
wxString text = EDA_TEXT::GetShownText( aAllowExtraText, aDepth );
if( IsNameShown() && aAllowExtraText )
text = GetShownName() << wxS( ": " ) << text;
if( text == wxS( "~" ) ) // Legacy placeholder for empty string
text = wxS( "" );
// The iteration here it to allow for nested variables in the
// text strings (e.g. ${${VAR}}). Although the symbols and sheets
// and labels recurse, text that is none of those types such as text
// boxes and labels do not. This only loops if there is still a
// variable to resolve.
for( int ii = 0; ii < 10 && text.Contains( wxT( "${" ) ); ++ii )
{
if( aDepth < ADVANCED_CFG::GetCfg().m_ResolveTextRecursionDepth )
{
if( m_parent && m_parent->Type() == LIB_SYMBOL_T )
text = ExpandTextVars( text, &libSymbolResolver );
else if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
text = ExpandTextVars( text, &symbolResolver );
else if( m_parent && m_parent->Type() == SCH_SHEET_T )
text = ExpandTextVars( text, &sheetResolver );
else if( m_parent && m_parent->IsType( labelTypes ) )
text = ExpandTextVars( text, &labelResolver );
else if( Schematic() )
{
text = ExpandTextVars( text, &Schematic()->Prj() );
text = ExpandTextVars( text, &schematicResolver );
}
}
}
// WARNING: the IDs of FIELDS and SHEETS overlap, so one must check *both* the
// id and the parent's type.
if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
{
SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
if( m_id == REFERENCE_FIELD && aPath )
{
// For more than one part per package, we must add the part selection
// A, B, ... or 1, 2, .. to the reference.
if( parentSymbol->GetUnitCount() > 1 )
text << parentSymbol->SubReference( parentSymbol->GetUnitSelection( aPath ) );
}
}
else if( m_parent && m_parent->Type() == SCH_SHEET_T )
{
if( m_id == SHEETFILENAME && aAllowExtraText && !IsNameShown() )
text = _( "File:" ) + wxS( " " ) + text;
}
return text;
}
wxString SCH_FIELD::GetShownText( bool aAllowExtraText, int aDepth ) const
{
if( SCHEMATIC* schematic = Schematic() )
return GetShownText( &schematic->CurrentSheet(), aAllowExtraText, aDepth );
else
return GetShownText( nullptr, aAllowExtraText, aDepth );
}
wxString SCH_FIELD::GetFullText( int unit ) const
{
if( GetId() != REFERENCE_FIELD )
return GetText();
wxString text = GetText();
text << wxT( "?" );
if( GetParentSymbol() && GetParentSymbol()->IsMulti() )
text << LIB_SYMBOL::LetterSubReference( unit, 'A' );
return text;
}
int SCH_FIELD::GetPenWidth() const
{
return GetEffectiveTextPenWidth();
}
KIFONT::FONT* SCH_FIELD::getDrawFont() const
{
KIFONT::FONT* font = EDA_TEXT::GetFont();
if( !font )
font = KIFONT::FONT::GetFont( GetDefaultFont(), IsBold(), IsItalic() );
return font;
}
void SCH_FIELD::ClearCaches()
{
ClearRenderCache();
EDA_TEXT::ClearBoundingBoxCache();
}
void SCH_FIELD::ClearRenderCache()
{
EDA_TEXT::ClearRenderCache();
m_renderCacheValid = false;
}
std::vector<std::unique_ptr<KIFONT::GLYPH>>*
SCH_FIELD::GetRenderCache( const wxString& forResolvedText, const VECTOR2I& forPosition,
TEXT_ATTRIBUTES& aAttrs ) const
{
KIFONT::FONT* font = GetFont();
if( !font )
font = KIFONT::FONT::GetFont( GetDefaultFont(), IsBold(), IsItalic() );
if( font->IsOutline() )
{
KIFONT::OUTLINE_FONT* outlineFont = static_cast<KIFONT::OUTLINE_FONT*>( font );
if( m_renderCache.empty() || !m_renderCacheValid )
{
m_renderCache.clear();
outlineFont->GetLinesAsGlyphs( &m_renderCache, forResolvedText, forPosition, aAttrs,
GetFontMetrics() );
m_renderCachePos = forPosition;
m_renderCacheValid = true;
}
if( m_renderCachePos != forPosition )
{
VECTOR2I delta = forPosition - m_renderCachePos;
for( std::unique_ptr<KIFONT::GLYPH>& glyph : m_renderCache )
{
if( glyph->IsOutline() )
static_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() )->Move( delta );
else
static_cast<KIFONT::STROKE_GLYPH*>( glyph.get() )->Move( delta );
}
m_renderCachePos = forPosition;
}
return &m_renderCache;
}
return nullptr;
}
void SCH_FIELD::Print( const SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBodyStyle,
const VECTOR2I& aOffset, bool aForceNoFill, bool aDimmed )
{
wxString text;
if( Schematic() )
text = GetShownText( &Schematic()->CurrentSheet(), true );
else
text = GetShownText( true );
if( ( !IsVisible() && !IsForceVisible() ) || text.IsEmpty() )
return;
wxDC* DC = aSettings->GetPrintDC();
COLOR4D color = aSettings->GetLayerColor( IsForceVisible() ? LAYER_HIDDEN : m_layer );
bool blackAndWhiteMode = GetGRForceBlackPenState();
int penWidth = GetEffectiveTextPenWidth( aSettings->GetDefaultPenWidth() );
COLOR4D bg = aSettings->GetBackgroundColor();
if( bg == COLOR4D::UNSPECIFIED || GetGRForceBlackPenState() )
bg = COLOR4D::WHITE;
if( IsForceVisible() )
bg = aSettings->GetLayerColor( LAYER_HIDDEN );
if( !blackAndWhiteMode && GetTextColor() != COLOR4D::UNSPECIFIED )
color = GetTextColor();
if( aDimmed )
{
color.Desaturate( );
color = color.Mix( bg, 0.5f );
}
// Calculate the text orientation according to the symbol orientation.
EDA_ANGLE orient = GetTextAngle();
VECTOR2I textpos = GetTextPos();
GR_TEXT_H_ALIGN_T hjustify = GetHorizJustify();
GR_TEXT_V_ALIGN_T vjustify = GetVertJustify();
KIFONT::FONT* font = GetFont();
if( !font )
font = KIFONT::FONT::GetFont( aSettings->GetDefaultFont(), IsBold(), IsItalic() );
if( m_parent && m_parent->Type() == LIB_SYMBOL_T )
{
textpos = aSettings->TransformCoordinate( GetTextPos() ) + aOffset;
}
else if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
{
/*
* Calculate the text justification, according to the symbol orientation/mirror.
* This is a bit complicated due to cumulative calculations:
* - numerous cases (mirrored or not, rotation)
* - the GRText function will also recalculate H and V justifications according to the
* text orientation.
* - when symbol is mirrored, the text is not mirrored and justifications are complicated
* to calculate so the easier way is to use no justifications (centered text) and use
* GetBoundingBox to know the text coordinate considered as centered
*/
hjustify = GR_TEXT_H_ALIGN_CENTER;
vjustify = GR_TEXT_V_ALIGN_CENTER;
textpos = GetBoundingBox().Centre() + aOffset;
if( aSettings->m_Transform.y1 ) // Rotate symbol 90 degrees.
{
if( orient == ANGLE_HORIZONTAL )
orient = ANGLE_VERTICAL;
else
orient = ANGLE_HORIZONTAL;
}
}
else if( m_parent && m_parent->Type() == SCH_GLOBAL_LABEL_T )
{
SCH_GLOBALLABEL* label = static_cast<SCH_GLOBALLABEL*>( GetParent() );
textpos += label->GetSchematicTextOffset( aSettings );
}
GRPrintText( DC, textpos, color, text, orient, GetTextSize(), hjustify, vjustify, penWidth,
IsItalic(), IsBold(), font, GetFontMetrics() );
}
void SCH_FIELD::ImportValues( const SCH_FIELD& aSource )
{
SetAttributes( aSource );
SetNameShown( aSource.IsNameShown() );
SetCanAutoplace( aSource.CanAutoplace() );
}
void SCH_FIELD::SwapData( SCH_ITEM* aItem )
{
wxCHECK_RET( aItem && aItem->Type() == SCH_FIELD_T, wxT( "Cannot swap with invalid item." ) );
SCH_ITEM::SwapFlags( aItem );
SCH_FIELD* item = static_cast<SCH_FIELD*>( aItem );
std::swap( m_layer, item->m_layer );
std::swap( m_showName, item->m_showName );
std::swap( m_allowAutoPlace, item->m_allowAutoPlace );
std::swap( m_isNamedVariable, item->m_isNamedVariable );
std::swap( m_private, item->m_private );
SwapText( *item );
SwapAttributes( *item );
std::swap( m_lastResolvedColor, item->m_lastResolvedColor );
}
COLOR4D SCH_FIELD::GetFieldColor() const
{
if( GetTextColor() != COLOR4D::UNSPECIFIED )
{
m_lastResolvedColor = GetTextColor();
}
else
{
SCH_LABEL_BASE* parentLabel = dynamic_cast<SCH_LABEL_BASE*>( GetParent() );
if( parentLabel && !parentLabel->IsConnectivityDirty() )
m_lastResolvedColor = parentLabel->GetEffectiveNetClass()->GetSchematicColor();
else
m_lastResolvedColor = GetTextColor();
}
return m_lastResolvedColor;
}
std::vector<int> SCH_FIELD::ViewGetLayers() const
{
return { GetDefaultLayer(), LAYER_SELECTION_SHADOWS };
}
SCH_LAYER_ID SCH_FIELD::GetDefaultLayer() const
{
if( m_parent && ( m_parent->Type() == LIB_SYMBOL_T || m_parent->Type() == SCH_SYMBOL_T ) )
{
if( m_id == REFERENCE_FIELD )
return LAYER_REFERENCEPART;
else if( m_id == VALUE_FIELD )
return LAYER_VALUEPART;
}
else if( m_parent && m_parent->Type() == SCH_SHEET_T )
{
if( m_id == SHEETNAME )
return LAYER_SHEETNAME;
else if( m_id == SHEETFILENAME )
return LAYER_SHEETFILENAME;
else
return LAYER_SHEETFIELDS;
}
else if( m_parent && m_parent->Type() == SCH_LABEL_T )
{
if( GetCanonicalName() == wxT( "Netclass" ) )
return LAYER_NETCLASS_REFS;
}
return LAYER_FIELDS;
}
EDA_ANGLE SCH_FIELD::GetDrawRotation() const
{
// Calculate the text orientation according to the symbol orientation.
EDA_ANGLE orient = GetTextAngle();
if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
{
SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
if( parentSymbol && parentSymbol->GetTransform().y1 ) // Rotate symbol 90 degrees.
{
if( orient.IsHorizontal() )
orient = ANGLE_VERTICAL;
else
orient = ANGLE_HORIZONTAL;
}
}
return orient;
}
const BOX2I SCH_FIELD::GetBoundingBox() const
{
BOX2I bbox = GetTextBox();
// Calculate the bounding box position relative to the parent:
VECTOR2I origin = GetParentPosition();
VECTOR2I pos = GetTextPos() - origin;
VECTOR2I begin = bbox.GetOrigin() - origin;
VECTOR2I end = bbox.GetEnd() - origin;
RotatePoint( begin, pos, GetTextAngle() );
RotatePoint( end, pos, GetTextAngle() );
// Now, apply the symbol transform (mirror/rot)
TRANSFORM transform;
if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
transform = static_cast<SCH_SYMBOL*>( m_parent )->GetTransform();
bbox.SetOrigin( transform.TransformCoordinate( begin ) );
bbox.SetEnd( transform.TransformCoordinate( end ) );
bbox.Move( origin );
bbox.Normalize();
return bbox;
}
bool SCH_FIELD::IsHorizJustifyFlipped() const
{
VECTOR2I render_center = GetBoundingBox().Centre();
VECTOR2I pos = GetPosition();
switch( GetHorizJustify() )
{
case GR_TEXT_H_ALIGN_LEFT:
if( GetDrawRotation().IsVertical() )
return render_center.y > pos.y;
else
return render_center.x < pos.x;
case GR_TEXT_H_ALIGN_RIGHT:
if( GetDrawRotation().IsVertical() )
return render_center.y < pos.y;
else
return render_center.x > pos.x;
default:
return false;
}
}
void SCH_FIELD::SetEffectiveHorizJustify( GR_TEXT_H_ALIGN_T aJustify )
{
GR_TEXT_H_ALIGN_T actualJustify;
switch( aJustify )
{
case GR_TEXT_H_ALIGN_LEFT:
actualJustify = IsHorizJustifyFlipped() ? GR_TEXT_H_ALIGN_RIGHT : GR_TEXT_H_ALIGN_LEFT;
break;
case GR_TEXT_H_ALIGN_RIGHT:
actualJustify = IsHorizJustifyFlipped() ? GR_TEXT_H_ALIGN_LEFT : GR_TEXT_H_ALIGN_RIGHT;
break;
default:
actualJustify = aJustify;
}
SetHorizJustify( actualJustify );
}
GR_TEXT_H_ALIGN_T SCH_FIELD::GetEffectiveHorizJustify() const
{
switch( GetHorizJustify() )
{
case GR_TEXT_H_ALIGN_LEFT:
return IsHorizJustifyFlipped() ? GR_TEXT_H_ALIGN_RIGHT : GR_TEXT_H_ALIGN_LEFT;
case GR_TEXT_H_ALIGN_RIGHT:
return IsHorizJustifyFlipped() ? GR_TEXT_H_ALIGN_LEFT : GR_TEXT_H_ALIGN_RIGHT;
default:
return GR_TEXT_H_ALIGN_CENTER;
}
}
bool SCH_FIELD::IsVertJustifyFlipped() const
{
VECTOR2I render_center = GetBoundingBox().Centre();
VECTOR2I pos = GetPosition();
switch( GetVertJustify() )
{
case GR_TEXT_V_ALIGN_TOP:
if( GetDrawRotation().IsVertical() )
return render_center.x < pos.x;
else
return render_center.y < pos.y;
case GR_TEXT_V_ALIGN_BOTTOM:
if( GetDrawRotation().IsVertical() )
return render_center.x > pos.x;
else
return render_center.y > pos.y;
default:
return false;
}
}
void SCH_FIELD::SetEffectiveVertJustify( GR_TEXT_V_ALIGN_T aJustify )
{
GR_TEXT_V_ALIGN_T actualJustify;
switch( aJustify )
{
case GR_TEXT_V_ALIGN_TOP:
actualJustify = IsVertJustifyFlipped() ? GR_TEXT_V_ALIGN_BOTTOM : GR_TEXT_V_ALIGN_TOP;
break;
case GR_TEXT_V_ALIGN_BOTTOM:
actualJustify = IsVertJustifyFlipped() ? GR_TEXT_V_ALIGN_TOP : GR_TEXT_V_ALIGN_BOTTOM;
break;
default:
actualJustify = aJustify;
}
SetVertJustify( actualJustify );
}
GR_TEXT_V_ALIGN_T SCH_FIELD::GetEffectiveVertJustify() const
{
switch( GetVertJustify() )
{
case GR_TEXT_V_ALIGN_TOP:
return IsVertJustifyFlipped() ? GR_TEXT_V_ALIGN_BOTTOM : GR_TEXT_V_ALIGN_TOP;
case GR_TEXT_V_ALIGN_BOTTOM:
return IsVertJustifyFlipped() ? GR_TEXT_V_ALIGN_TOP : GR_TEXT_V_ALIGN_BOTTOM;
default:
return GR_TEXT_V_ALIGN_CENTER;
}
}
bool SCH_FIELD::Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) const
{
bool searchHiddenFields = false;
bool searchAndReplace = false;
bool replaceReferences = false;
try
{
const SCH_SEARCH_DATA& schSearchData = dynamic_cast<const SCH_SEARCH_DATA&>( aSearchData ); // downcast
searchHiddenFields = schSearchData.searchAllFields;
searchAndReplace = schSearchData.searchAndReplace;
replaceReferences = schSearchData.replaceReferences;
}
catch( const std::bad_cast& )
{
}
wxString text = UnescapeString( GetText() );
if( !IsVisible() && !searchHiddenFields )
return false;
if( m_parent && m_parent->Type() == SCH_SYMBOL_T && m_id == REFERENCE_FIELD )
{
if( searchAndReplace && !replaceReferences )
return false;
SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
wxASSERT( aAuxData );
// Take sheet path into account which effects the reference field and the unit for
// symbols with multiple parts.
if( aAuxData )
{
SCH_SHEET_PATH* sheet = (SCH_SHEET_PATH*) aAuxData;
text = parentSymbol->GetRef( sheet );
if( SCH_ITEM::Matches( text, aSearchData ) )
return true;
if( parentSymbol->GetUnitCount() > 1 )
text << parentSymbol->SubReference( parentSymbol->GetUnitSelection( sheet ) );
}
}
return SCH_ITEM::Matches( text, aSearchData );
}
void SCH_FIELD::OnScintillaCharAdded( SCINTILLA_TRICKS* aScintillaTricks,
wxStyledTextEvent &aEvent ) const
{
SCH_ITEM* parent = dynamic_cast<SCH_ITEM*>( GetParent() );
SCHEMATIC* schematic = parent ? parent->Schematic() : nullptr;
if( !schematic )
return;
wxStyledTextCtrl* scintilla = aScintillaTricks->Scintilla();
int key = aEvent.GetKey();
wxArrayString autocompleteTokens;
int pos = scintilla->GetCurrentPos();
int start = scintilla->WordStartPosition( pos, true );
wxString partial;
// Multi-line fields are not allowed. So remove '\n' if entered.
if( key == '\n' )
{
wxString text = scintilla->GetText();
int currpos = scintilla->GetCurrentPos();
text.Replace( wxS( "\n" ), wxS( "" ) );
scintilla->SetText( text );
scintilla->GotoPos( currpos-1 );
return;
}
auto textVarRef =
[&]( int pt )
{
return pt >= 2
&& scintilla->GetCharAt( pt - 2 ) == '$'
&& scintilla->GetCharAt( pt - 1 ) == '{';
};
// Check for cross-reference
if( start > 1 && scintilla->GetCharAt( start - 1 ) == ':' )
{
int refStart = scintilla->WordStartPosition( start - 1, true );
if( textVarRef( refStart ) )
{
partial = scintilla->GetRange( start, pos );
wxString ref = scintilla->GetRange( refStart, start - 1 );
if( ref == wxS( "OP" ) )
{
// SPICE operating points use ':' syntax for ports
if( SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( parent ) )
{
NULL_REPORTER devnull;
SCH_SHEET_PATH& sheet = schematic->CurrentSheet();
SIM_LIB_MGR mgr( &schematic->Prj() );
SIM_MODEL& model = mgr.CreateModel( &sheet, *symbol, devnull ).model;
for( wxString pin : model.GetPinNames() )
{
if( pin.StartsWith( '<' ) && pin.EndsWith( '>' ) )
autocompleteTokens.push_back( pin.Mid( 1, pin.Length() - 2 ) );
else
autocompleteTokens.push_back( pin );
}
}
}
else
{
SCH_REFERENCE_LIST refs;
SCH_SYMBOL* refSymbol = nullptr;
schematic->Hierarchy().GetSymbols( refs );
for( size_t jj = 0; jj < refs.GetCount(); jj++ )
{
if( refs[ jj ].GetSymbol()->GetRef( &refs[ jj ].GetSheetPath(), true ) == ref )
{
refSymbol = refs[ jj ].GetSymbol();
break;
}
}
if( refSymbol )
refSymbol->GetContextualTextVars( &autocompleteTokens );
}
}
}
else if( textVarRef( start ) )
{
partial = scintilla->GetTextRange( start, pos );
SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( parent );
SCH_SHEET* sheet = dynamic_cast<SCH_SHEET*>( parent );
SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( parent );
if( symbol )
{
symbol->GetContextualTextVars( &autocompleteTokens );
if( schematic->CurrentSheet().Last() )
schematic->CurrentSheet().Last()->GetContextualTextVars( &autocompleteTokens );
}
if( sheet )
sheet->GetContextualTextVars( &autocompleteTokens );
if( label )
label->GetContextualTextVars( &autocompleteTokens );
for( std::pair<wxString, wxString> entry : schematic->Prj().GetTextVars() )
autocompleteTokens.push_back( entry.first );
}
aScintillaTricks->DoAutocomplete( partial, autocompleteTokens );
scintilla->SetFocus();
}
bool SCH_FIELD::IsReplaceable() const
{
if( m_parent && m_parent->Type() == SCH_SHEET_T )
{
// See comments in SCH_FIELD::Replace(), below.
if( m_id == SHEETFILENAME )
return false;
}
else if( m_parent && m_parent->Type() == SCH_GLOBAL_LABEL_T )
{
if( m_id == 0 /* IntersheetRefs */ )
return false;
}
return true;
}
bool SCH_FIELD::Replace( const EDA_SEARCH_DATA& aSearchData, void* aAuxData )
{
bool replaceReferences = false;
try
{
const SCH_SEARCH_DATA& schSearchData = dynamic_cast<const SCH_SEARCH_DATA&>( aSearchData );
replaceReferences = schSearchData.replaceReferences;
}
catch( const std::bad_cast& )
{
}
wxString text;
bool isReplaced = false;
if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
{
SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
switch( m_id )
{
case REFERENCE_FIELD:
wxCHECK_MSG( aAuxData, false, wxT( "Need sheetpath to replace in refdes." ) );
if( !replaceReferences )
return false;
text = parentSymbol->GetRef( (SCH_SHEET_PATH*) aAuxData );
isReplaced = EDA_ITEM::Replace( aSearchData, text );
if( isReplaced )
parentSymbol->SetRef( (SCH_SHEET_PATH*) aAuxData, text );
break;
case VALUE_FIELD:
wxCHECK_MSG( aAuxData, false, wxT( "Need sheetpath to replace in value field." ) );
text = parentSymbol->GetField( VALUE_FIELD )->GetText();
isReplaced = EDA_ITEM::Replace( aSearchData, text );
if( isReplaced )
parentSymbol->SetValueFieldText( text );
break;
case FOOTPRINT_FIELD:
wxCHECK_MSG( aAuxData, false, wxT( "Need sheetpath to replace in footprint field." ) );
text = parentSymbol->GetField( FOOTPRINT_FIELD )->GetText();
isReplaced = EDA_ITEM::Replace( aSearchData, text );
if( isReplaced )
parentSymbol->SetFootprintFieldText( text );
break;