-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
sch_label.cpp
2273 lines (1741 loc) · 68 KB
/
sch_label.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) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2015 Wayne Stambaugh <[email protected]>
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <advanced_config.h>
#include <base_units.h>
#include <increment.h>
#include <pgm_base.h>
#include <sch_edit_frame.h>
#include <sch_plotter.h>
#include <widgets/msgpanel.h>
#include <bitmaps.h>
#include <string_utils.h>
#include <schematic.h>
#include <settings/color_settings.h>
#include <sch_painter.h>
#include <default_values.h>
#include <wx/debug.h>
#include <wx/log.h>
#include <dialogs/html_message_box.h>
#include <project/project_file.h>
#include <project/net_settings.h>
#include <core/kicad_algo.h>
#include <core/mirror.h>
#include <trigo.h>
#include <sch_label.h>
#include <magic_enum.hpp>
#include <api/api_utils.h>
#include <api/schematic/schematic_types.pb.h>
/* Coding polygons for global symbol graphic shapes.
* the first parml is the number of corners
* others are the corners coordinates in reduced units
* the real coordinate is the reduced coordinate * text half size
*/
static int TemplateIN_HN[] = { 6, 0, 0, -1, -1, -2, -1, -2, 1, -1, 1, 0, 0 };
static int TemplateIN_HI[] = { 6, 0, 0, 1, 1, 2, 1, 2, -1, 1, -1, 0, 0 };
static int TemplateIN_UP[] = { 6, 0, 0, 1, -1, 1, -2, -1, -2, -1, -1, 0, 0 };
static int TemplateIN_BOTTOM[] = { 6, 0, 0, 1, 1, 1, 2, -1, 2, -1, 1, 0, 0 };
static int TemplateOUT_HN[] = { 6, -2, 0, -1, 1, 0, 1, 0, -1, -1, -1, -2, 0 };
static int TemplateOUT_HI[] = { 6, 2, 0, 1, -1, 0, -1, 0, 1, 1, 1, 2, 0 };
static int TemplateOUT_UP[] = { 6, 0, -2, 1, -1, 1, 0, -1, 0, -1, -1, 0, -2 };
static int TemplateOUT_BOTTOM[] = { 6, 0, 2, 1, 1, 1, 0, -1, 0, -1, 1, 0, 2 };
static int TemplateUNSPC_HN[] = { 5, 0, -1, -2, -1, -2, 1, 0, 1, 0, -1 };
static int TemplateUNSPC_HI[] = { 5, 0, -1, 2, -1, 2, 1, 0, 1, 0, -1 };
static int TemplateUNSPC_UP[] = { 5, 1, 0, 1, -2, -1, -2, -1, 0, 1, 0 };
static int TemplateUNSPC_BOTTOM[] = { 5, 1, 0, 1, 2, -1, 2, -1, 0, 1, 0 };
static int TemplateBIDI_HN[] = { 5, 0, 0, -1, -1, -2, 0, -1, 1, 0, 0 };
static int TemplateBIDI_HI[] = { 5, 0, 0, 1, -1, 2, 0, 1, 1, 0, 0 };
static int TemplateBIDI_UP[] = { 5, 0, 0, -1, -1, 0, -2, 1, -1, 0, 0 };
static int TemplateBIDI_BOTTOM[] = { 5, 0, 0, -1, 1, 0, 2, 1, 1, 0, 0 };
static int Template3STATE_HN[] = { 5, 0, 0, -1, -1, -2, 0, -1, 1, 0, 0 };
static int Template3STATE_HI[] = { 5, 0, 0, 1, -1, 2, 0, 1, 1, 0, 0 };
static int Template3STATE_UP[] = { 5, 0, 0, -1, -1, 0, -2, 1, -1, 0, 0 };
static int Template3STATE_BOTTOM[] = { 5, 0, 0, -1, 1, 0, 2, 1, 1, 0, 0 };
static int* TemplateShape[5][4] =
{
{ TemplateIN_HN, TemplateIN_UP, TemplateIN_HI, TemplateIN_BOTTOM },
{ TemplateOUT_HN, TemplateOUT_UP, TemplateOUT_HI, TemplateOUT_BOTTOM },
{ TemplateBIDI_HN, TemplateBIDI_UP, TemplateBIDI_HI, TemplateBIDI_BOTTOM },
{ Template3STATE_HN, Template3STATE_UP, Template3STATE_HI, Template3STATE_BOTTOM },
{ TemplateUNSPC_HN, TemplateUNSPC_UP, TemplateUNSPC_HI, TemplateUNSPC_BOTTOM }
};
wxString getElectricalTypeLabel( LABEL_FLAG_SHAPE aType )
{
switch( aType )
{
case LABEL_FLAG_SHAPE::L_INPUT: return _( "Input" );
case LABEL_FLAG_SHAPE::L_OUTPUT: return _( "Output" );
case LABEL_FLAG_SHAPE::L_BIDI: return _( "Bidirectional" );
case LABEL_FLAG_SHAPE::L_TRISTATE: return _( "Tri-State" );
case LABEL_FLAG_SHAPE::L_UNSPECIFIED: return _( "Passive" );
default: return wxT( "???" );
}
}
SPIN_STYLE SPIN_STYLE::RotateCCW()
{
SPIN newSpin = m_spin;
switch( m_spin )
{
case SPIN_STYLE::LEFT: newSpin = SPIN_STYLE::BOTTOM; break;
case SPIN_STYLE::BOTTOM: newSpin = SPIN_STYLE::RIGHT; break;
case SPIN_STYLE::RIGHT: newSpin = SPIN_STYLE::UP; break;
case SPIN_STYLE::UP: newSpin = SPIN_STYLE::LEFT; break;
}
return SPIN_STYLE( newSpin );
}
SPIN_STYLE SPIN_STYLE::MirrorX()
{
SPIN newSpin = m_spin;
switch( m_spin )
{
case SPIN_STYLE::UP: newSpin = SPIN_STYLE::BOTTOM; break;
case SPIN_STYLE::BOTTOM: newSpin = SPIN_STYLE::UP; break;
case SPIN_STYLE::LEFT: break;
case SPIN_STYLE::RIGHT: break;
}
return SPIN_STYLE( newSpin );
}
SPIN_STYLE SPIN_STYLE::MirrorY()
{
SPIN newSpin = m_spin;
switch( m_spin )
{
case SPIN_STYLE::LEFT: newSpin = SPIN_STYLE::RIGHT; break;
case SPIN_STYLE::RIGHT: newSpin = SPIN_STYLE::LEFT; break;
case SPIN_STYLE::UP: break;
case SPIN_STYLE::BOTTOM: break;
}
return SPIN_STYLE( newSpin );
}
unsigned SPIN_STYLE::CCWRotationsTo( const SPIN_STYLE& aOther ) const
{
return ( ( (int) m_spin - (int) aOther.m_spin ) % 4 + 4 ) % 4;
}
SCH_LABEL_BASE::SCH_LABEL_BASE( const VECTOR2I& aPos, const wxString& aText, KICAD_T aType ) :
SCH_TEXT( aPos, aText, LAYER_NOTES, aType ),
m_shape( L_UNSPECIFIED ),
m_connectionType( CONNECTION_TYPE::NONE ),
m_isDangling( true ),
m_lastResolvedColor( COLOR4D::UNSPECIFIED )
{
SetMultilineAllowed( false );
if( !HasTextVars() )
m_cached_driver_name = EscapeString( EDA_TEXT::GetShownText( true, 0 ), CTX_NETNAME );
}
SCH_LABEL_BASE::SCH_LABEL_BASE( const SCH_LABEL_BASE& aLabel ) :
SCH_TEXT( aLabel ),
m_shape( aLabel.m_shape ),
m_connectionType( aLabel.m_connectionType ),
m_isDangling( aLabel.m_isDangling ),
m_lastResolvedColor( aLabel.m_lastResolvedColor ),
m_cached_driver_name( aLabel.m_cached_driver_name )
{
SetMultilineAllowed( false );
m_fields = aLabel.m_fields;
for( SCH_FIELD& field : m_fields )
field.SetParent( this );
}
SCH_LABEL_BASE& SCH_LABEL_BASE::operator=( const SCH_LABEL_BASE& aLabel )
{
SCH_TEXT::operator=( aLabel );
m_cached_driver_name = aLabel.m_cached_driver_name;
return *this;
}
const wxString SCH_LABEL_BASE::GetDefaultFieldName( const wxString& aName, bool aUseDefaultName )
{
if( aName == wxT( "Intersheetrefs" ) )
return _( "Sheet References" );
else if( aName == wxT( "Netclass" ) )
return _( "Net Class" );
else if( aName.IsEmpty() && aUseDefaultName )
return _( "Field" );
else
return aName;
}
bool SCH_LABEL_BASE::IsType( const std::vector<KICAD_T>& aScanTypes ) const
{
static const std::vector<KICAD_T> wireAndPinTypes = { SCH_ITEM_LOCATE_WIRE_T, SCH_PIN_T };
static const std::vector<KICAD_T> busTypes = { SCH_ITEM_LOCATE_BUS_T };
if( SCH_TEXT::IsType( aScanTypes ) )
return true;
for( KICAD_T scanType : aScanTypes )
{
if( scanType == SCH_LABEL_LOCATE_ANY_T )
return true;
}
wxCHECK_MSG( Schematic(), false, wxT( "No parent SCHEMATIC set for SCH_LABEL!" ) );
// Ensure m_connected_items for Schematic()->CurrentSheet() exists.
// Can be not the case when "this" is living in clipboard
if( m_connected_items.find( Schematic()->CurrentSheet() ) == m_connected_items.end() )
return false;
const SCH_ITEM_VEC& item_set = m_connected_items.at( Schematic()->CurrentSheet() );
for( KICAD_T scanType : aScanTypes )
{
if( scanType == SCH_LABEL_LOCATE_WIRE_T )
{
for( SCH_ITEM* connection : item_set )
{
if( connection->IsType( wireAndPinTypes ) )
return true;
}
}
if ( scanType == SCH_LABEL_LOCATE_BUS_T )
{
for( SCH_ITEM* connection : item_set )
{
if( connection->IsType( busTypes ) )
return true;
}
}
}
return false;
}
void SCH_LABEL_BASE::SwapData( SCH_ITEM* aItem )
{
SCH_TEXT::SwapData( aItem );
SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( aItem );
m_fields.swap( label->m_fields );
std::swap( m_fieldsAutoplaced, label->m_fieldsAutoplaced );
for( SCH_FIELD& field : m_fields )
field.SetParent( this );
for( SCH_FIELD& field : label->m_fields )
field.SetParent( label );
std::swap( m_shape, label->m_shape );
std::swap( m_connectionType, label->m_connectionType );
std::swap( m_isDangling, label->m_isDangling );
std::swap( m_lastResolvedColor, label->m_lastResolvedColor );
}
COLOR4D SCH_LABEL_BASE::GetLabelColor() const
{
if( GetTextColor() != COLOR4D::UNSPECIFIED )
m_lastResolvedColor = GetTextColor();
else if( !IsConnectivityDirty() )
m_lastResolvedColor = GetEffectiveNetClass()->GetSchematicColor();
return m_lastResolvedColor;
}
void SCH_LABEL_BASE::SetSpinStyle( SPIN_STYLE aSpinStyle )
{
// Assume "Right" and Left" mean which side of the anchor the text will be on
// Thus we want to left justify text up against the anchor if we are on the right
switch( aSpinStyle )
{
default:
wxFAIL_MSG( "Bad spin style" );
KI_FALLTHROUGH;
case SPIN_STYLE::RIGHT: // Horiz Normal Orientation
SetTextAngle( ANGLE_HORIZONTAL );
SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
break;
case SPIN_STYLE::UP: // Vert Orientation UP
SetTextAngle( ANGLE_VERTICAL );
SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
break;
case SPIN_STYLE::LEFT: // Horiz Orientation - Right justified
SetTextAngle( ANGLE_HORIZONTAL );
SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
break;
case SPIN_STYLE::BOTTOM: // Vert Orientation BOTTOM
SetTextAngle( ANGLE_VERTICAL );
SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
break;
}
SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
}
SPIN_STYLE SCH_LABEL_BASE::GetSpinStyle() const
{
if( GetTextAngle() == ANGLE_VERTICAL )
{
if( GetHorizJustify() == GR_TEXT_H_ALIGN_RIGHT )
return SPIN_STYLE::BOTTOM;
else
return SPIN_STYLE::UP;
}
else
{
if( GetHorizJustify() == GR_TEXT_H_ALIGN_RIGHT )
return SPIN_STYLE::LEFT;
else
return SPIN_STYLE::RIGHT;
}
}
VECTOR2I SCH_LABEL_BASE::GetSchematicTextOffset( const RENDER_SETTINGS* aSettings ) const
{
VECTOR2I text_offset;
// add an offset to x (or y) position to aid readability of text on a wire or line
int dist = GetTextOffset( aSettings ) + GetPenWidth();
switch( GetSpinStyle() )
{
case SPIN_STYLE::UP:
case SPIN_STYLE::BOTTOM: text_offset.x = -dist; break; // Vert Orientation
default:
case SPIN_STYLE::LEFT:
case SPIN_STYLE::RIGHT: text_offset.y = -dist; break; // Horiz Orientation
}
return text_offset;
}
void SCH_LABEL_BASE::SetPosition( const VECTOR2I& aPosition )
{
VECTOR2I offset = aPosition - GetTextPos();
Move( offset );
}
void SCH_LABEL_BASE::Move( const VECTOR2I& aMoveVector )
{
SCH_TEXT::Move( aMoveVector );
for( SCH_FIELD& field : m_fields )
field.Offset( aMoveVector );
}
void SCH_LABEL_BASE::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
{
VECTOR2I pt = GetTextPos();
RotatePoint( pt, aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
VECTOR2I offset = pt - GetTextPos();
Rotate90( !aRotateCCW );
SetTextPos( GetTextPos() + offset );
for( SCH_FIELD& field : m_fields )
field.SetTextPos( field.GetTextPos() + offset );
}
void SCH_LABEL_BASE::Rotate90( bool aClockwise )
{
SCH_TEXT::Rotate90( aClockwise );
if( m_fieldsAutoplaced == AUTOPLACE_AUTO || m_fieldsAutoplaced == AUTOPLACE_MANUAL )
{
AutoplaceFields( nullptr, m_fieldsAutoplaced );
}
else
{
for( SCH_FIELD& field : m_fields )
field.Rotate( GetPosition(), !aClockwise );
}
}
void SCH_LABEL_BASE::MirrorSpinStyle( bool aLeftRight )
{
SCH_TEXT::MirrorSpinStyle( aLeftRight );
for( SCH_FIELD& field : m_fields )
{
if( ( aLeftRight && field.GetTextAngle().IsHorizontal() )
|| ( !aLeftRight && field.GetTextAngle().IsVertical() ) )
{
if( field.GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT )
field.SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
else
field.SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
}
VECTOR2I pos = field.GetTextPos();
VECTOR2I delta = (VECTOR2I)GetPosition() - pos;
if( aLeftRight )
pos.x = GetPosition().x + delta.x;
else
pos.y = GetPosition().y + delta.y;
field.SetTextPos( pos );
}
}
void SCH_LABEL_BASE::MirrorHorizontally( int aCenter )
{
VECTOR2I old_pos = GetPosition();
SCH_TEXT::MirrorHorizontally( aCenter );
for( SCH_FIELD& field : m_fields )
{
if( field.GetTextAngle() == ANGLE_HORIZONTAL )
field.FlipHJustify();
VECTOR2I pos = field.GetTextPos();
VECTOR2I delta = old_pos - pos;
pos.x = GetPosition().x + delta.x;
field.SetPosition( pos );
}
}
void SCH_LABEL_BASE::MirrorVertically( int aCenter )
{
VECTOR2I old_pos = GetPosition();
SCH_TEXT::MirrorVertically( aCenter );
for( SCH_FIELD& field : m_fields )
{
if( field.GetTextAngle() == ANGLE_VERTICAL )
field.FlipHJustify();
VECTOR2I pos = field.GetTextPos();
VECTOR2I delta = old_pos - pos;
pos.y = GetPosition().y + delta.y;
field.SetPosition( pos );
}
}
bool SCH_LABEL_BASE::IncrementLabel( int aIncrement )
{
wxString text = GetText();
if( IncrementString( text, aIncrement ) )
{
SetText( text );
return true;
}
return false;
}
bool SCH_LABEL_BASE::operator==( const SCH_ITEM& aOther ) const
{
const SCH_LABEL_BASE* other = dynamic_cast<const SCH_LABEL_BASE*>( &aOther );
if( !other )
return false;
if( m_shape != other->m_shape )
return false;
if( m_connectionType != other->m_connectionType )
return false;
if( m_fields.size() != other->m_fields.size() )
return false;
for( size_t ii = 0; ii < m_fields.size(); ++ii )
{
if( !( m_fields[ii] == other->m_fields[ii] ) )
return false;
}
return SCH_TEXT::operator==( aOther );
}
double SCH_LABEL_BASE::Similarity( const SCH_ITEM& aOther ) const
{
const SCH_LABEL_BASE* other = dynamic_cast<const SCH_LABEL_BASE*>( &aOther );
if( !other )
return 0.0;
if( m_Uuid == other->m_Uuid )
return 1.0;
double similarity = SCH_TEXT::Similarity( aOther );
if( typeid( *this ) != typeid( aOther ) )
similarity *= 0.9;
if( m_shape == other->m_shape )
similarity *= 0.9;
if( m_connectionType == other->m_connectionType )
similarity *= 0.9;
for( size_t ii = 0; ii < m_fields.size(); ++ii )
{
if( ii >= other->m_fields.size() )
break;
similarity *= m_fields[ii].Similarity( other->m_fields[ii] );
}
int diff = std::abs( int( m_fields.size() ) - int( other->m_fields.size() ) );
similarity *= std::pow( 0.9, diff );
return similarity;
}
void SCH_LABEL_BASE::AutoplaceFields( SCH_SCREEN* aScreen, AUTOPLACE_ALGO aAlgo )
{
int margin = GetTextOffset() * 2;
int labelLen = GetBodyBoundingBox().GetSizeMax();
int accumulated = GetTextHeight() / 2;
if( Type() == SCH_GLOBAL_LABEL_T )
accumulated += margin + GetPenWidth() + margin;
for( SCH_FIELD& field : m_fields )
{
VECTOR2I offset( 0, 0 );
switch( GetSpinStyle() )
{
default:
case SPIN_STYLE::LEFT:
field.SetTextAngle( ANGLE_HORIZONTAL );
field.SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
if( field.GetCanonicalName() == wxT( "Intersheetrefs" ) )
offset.x = - ( labelLen + margin );
else
offset.y = accumulated + field.GetTextHeight() / 2;
break;
case SPIN_STYLE::UP:
field.SetTextAngle( ANGLE_VERTICAL );
field.SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
if( field.GetCanonicalName() == wxT( "Intersheetrefs" ) )
offset.y = - ( labelLen + margin );
else
offset.x = accumulated + field.GetTextHeight() / 2;
break;
case SPIN_STYLE::RIGHT:
field.SetTextAngle( ANGLE_HORIZONTAL );
field.SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
if( field.GetCanonicalName() == wxT( "Intersheetrefs" ) )
offset.x = labelLen + margin;
else
offset.y = accumulated + field.GetTextHeight() / 2;
break;
case SPIN_STYLE::BOTTOM:
field.SetTextAngle( ANGLE_VERTICAL );
field.SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
if( field.GetCanonicalName() == wxT( "Intersheetrefs" ) )
offset.y = labelLen + margin;
else
offset.x = accumulated + field.GetTextHeight() / 2;
break;
}
field.SetTextPos( GetTextPos() + offset );
if( field.GetCanonicalName() != wxT( "Intersheetrefs" ) )
accumulated += field.GetTextHeight() + margin;
}
if( aAlgo == AUTOPLACE_AUTO || aAlgo == AUTOPLACE_MANUAL )
m_fieldsAutoplaced = aAlgo;
}
void SCH_LABEL_BASE::GetIntersheetRefs( const SCH_SHEET_PATH* aPath,
std::vector<std::pair<wxString, wxString>>* pages )
{
wxCHECK( pages, /* void */ );
if( Schematic() )
{
wxString resolvedLabel = GetShownText( &Schematic()->CurrentSheet(), false );
auto it = Schematic()->GetPageRefsMap().find( resolvedLabel );
if( it != Schematic()->GetPageRefsMap().end() )
{
std::vector<int> pageListCopy;
pageListCopy.insert( pageListCopy.end(), it->second.begin(), it->second.end() );
if( !Schematic()->Settings().m_IntersheetRefsListOwnPage )
{
int currentPage = Schematic()->CurrentSheet().GetVirtualPageNumber();
alg::delete_matching( pageListCopy, currentPage );
if( pageListCopy.empty() )
return;
}
std::sort( pageListCopy.begin(), pageListCopy.end() );
std::map<int, wxString> sheetPages = Schematic()->GetVirtualPageToSheetPagesMap();
std::map<int, wxString> sheetNames = Schematic()->GetVirtualPageToSheetNamesMap();
for( int pageNum : pageListCopy )
pages->push_back( { sheetPages[ pageNum ], sheetNames[ pageNum ] } );
}
}
}
void SCH_LABEL_BASE::GetContextualTextVars( wxArrayString* aVars ) const
{
for( const SCH_FIELD& field : m_fields )
aVars->push_back( field.GetCanonicalName().Upper() );
aVars->push_back( wxT( "OP" ) );
aVars->push_back( wxT( "CONNECTION_TYPE" ) );
aVars->push_back( wxT( "SHORT_NET_NAME" ) );
aVars->push_back( wxT( "NET_NAME" ) );
aVars->push_back( wxT( "NET_CLASS" ) );
}
bool SCH_LABEL_BASE::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token,
int aDepth ) const
{
static wxRegEx operatingPoint( wxT( "^"
"OP"
"(.([0-9])?([a-zA-Z]*))?"
"$" ) );
wxCHECK( aPath, false );
SCHEMATIC* schematic = Schematic();
if( !schematic )
return false;
if( operatingPoint.Matches( *token ) )
{
int precision = 3;
wxString precisionStr( operatingPoint.GetMatch( *token, 2 ) );
wxString range( operatingPoint.GetMatch( *token, 3 ) );
if( !precisionStr.IsEmpty() )
precision = precisionStr[0] - '0';
if( range.IsEmpty() )
range = wxS( "~V" );
const SCH_CONNECTION* connection = Connection();
*token = wxS( "?" );
if( connection )
*token = schematic->GetOperatingPoint( connection->Name( false ), precision, range );
return true;
}
if( token->Contains( ':' ) )
{
if( schematic->ResolveCrossReference( token, aDepth + 1 ) )
return true;
}
if( ( Type() == SCH_GLOBAL_LABEL_T || Type() == SCH_HIER_LABEL_T || Type() == SCH_SHEET_PIN_T )
&& token->IsSameAs( wxT( "CONNECTION_TYPE" ) ) )
{
const SCH_LABEL_BASE* label = static_cast<const SCH_LABEL_BASE*>( this );
*token = getElectricalTypeLabel( label->GetShape() );
return true;
}
else if( token->IsSameAs( wxT( "SHORT_NET_NAME" ) ) )
{
const SCH_CONNECTION* connection = Connection();
*token = wxEmptyString;
if( connection )
*token = connection->LocalName();
return true;
}
else if( token->IsSameAs( wxT( "NET_NAME" ) ) )
{
const SCH_CONNECTION* connection = Connection();
*token = wxEmptyString;
if( connection )
*token = connection->Name();
return true;
}
else if( token->IsSameAs( wxT( "NET_CLASS" ) ) )
{
const SCH_CONNECTION* connection = Connection();
*token = wxEmptyString;
if( connection )
*token = GetEffectiveNetClass()->GetVariableSubstitutionName();
return true;
}
for( const SCH_FIELD& field : m_fields)
{
if( token->IsSameAs( field.GetName() ) )
{
*token = field.GetShownText( false, aDepth + 1 );
return true;
}
}
// See if parent can resolve it (these will recurse to ancestors)
if( Type() == SCH_SHEET_PIN_T && m_parent )
{
SCH_SHEET* sheet = static_cast<SCH_SHEET*>( m_parent );
SCH_SHEET_PATH path = *aPath;
path.push_back( sheet );
if( sheet->ResolveTextVar( &path, token, aDepth + 1 ) )
return true;
}
else
{
if( aPath->Last()->ResolveTextVar( aPath, token, aDepth + 1 ) )
return true;
}
return false;
}
bool SCH_LABEL_BASE::HasCachedDriverName() const
{
return !HasTextVars();
}
const wxString& SCH_LABEL_BASE::GetCachedDriverName() const
{
return m_cached_driver_name;
}
void SCH_LABEL_BASE::cacheShownText()
{
EDA_TEXT::cacheShownText();
if( !HasTextVars() )
m_cached_driver_name = EscapeString( EDA_TEXT::GetShownText( true, 0 ), CTX_NETNAME );
}
wxString SCH_LABEL_BASE::GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraText,
int aDepth ) const
{
std::function<bool( wxString* )> textResolver =
[&]( wxString* token ) -> bool
{
return ResolveTextVar( aPath, token, aDepth + 1 );
};
wxString text = EDA_TEXT::GetShownText( aAllowExtraText, aDepth );
if( text == wxS( "~" ) ) // Legacy placeholder for empty string
{
text = wxS( "" );
}
else if( HasTextVars() )
{
if( aDepth < ADVANCED_CFG::GetCfg().m_ResolveTextRecursionDepth )
text = ExpandTextVars( text, &textResolver );
}
return text;
}
void SCH_LABEL_BASE::RunOnChildren( const std::function<void( SCH_ITEM* )>& aFunction )
{
for( SCH_FIELD& field : m_fields )
aFunction( &field );
}
bool SCH_LABEL_BASE::Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) const
{
return SCH_ITEM::Matches( UnescapeString( GetText() ), aSearchData );
}
bool SCH_LABEL_BASE::Replace( const EDA_SEARCH_DATA& aSearchData, void* aAuxData )
{
EDA_SEARCH_DATA localSearchData( aSearchData );
localSearchData.findString = EscapeString( aSearchData.findString, CTX_NETNAME );
localSearchData.replaceString = EscapeString( aSearchData.replaceString, CTX_NETNAME );
return EDA_TEXT::Replace( localSearchData );
}
INSPECT_RESULT SCH_LABEL_BASE::Visit( INSPECTOR aInspector, void* testData,
const std::vector<KICAD_T>& aScanTypes )
{
if( IsType( aScanTypes ) )
{
if( INSPECT_RESULT::QUIT == aInspector( this, nullptr ) )
return INSPECT_RESULT::QUIT;
}
for( KICAD_T scanType : aScanTypes )
{
if( scanType == SCH_LOCATE_ANY_T || scanType == SCH_FIELD_T )
{
for( SCH_FIELD& field : m_fields )
{
if( INSPECT_RESULT::QUIT == aInspector( &field, this ) )
return INSPECT_RESULT::QUIT;
}
}
}
return INSPECT_RESULT::CONTINUE;
}
void SCH_LABEL_BASE::GetEndPoints( std::vector<DANGLING_END_ITEM>& aItemList )
{
DANGLING_END_ITEM item( LABEL_END, this, GetTextPos() );
aItemList.push_back( item );
}
std::vector<VECTOR2I> SCH_LABEL_BASE::GetConnectionPoints() const
{
return { GetTextPos() };
}
std::vector<int> SCH_LABEL_BASE::ViewGetLayers() const
{
return { LAYER_DANGLING, LAYER_DEVICE, LAYER_NETCLASS_REFS, LAYER_FIELDS,
LAYER_SELECTION_SHADOWS };
}
int SCH_LABEL_BASE::GetLabelBoxExpansion( const RENDER_SETTINGS* aSettings ) const
{
double ratio;
if( aSettings )
ratio = static_cast<const SCH_RENDER_SETTINGS*>( aSettings )->m_LabelSizeRatio;
else if( Schematic() )
ratio = Schematic()->Settings().m_LabelSizeRatio;
else
ratio = DEFAULT_LABEL_SIZE_RATIO; // For previews (such as in Preferences), etc.
return KiROUND( ratio * GetTextSize().y );
}
const BOX2I SCH_LABEL_BASE::GetBodyBoundingBox() const
{
// build the bounding box of the label only, without taking into account its fields
BOX2I box;
std::vector<VECTOR2I> pts;
CreateGraphicShape( nullptr, pts, GetTextPos() );
for( const VECTOR2I& pt : pts )
box.Merge( pt );
box.Inflate( GetEffectiveTextPenWidth() / 2 );
box.Normalize();
return box;
}
const BOX2I SCH_LABEL_BASE::GetBoundingBox() const
{
// build the bounding box of the entire label, including its fields
BOX2I box = GetBodyBoundingBox();
for( const SCH_FIELD& field : m_fields )
{
if( field.IsVisible() && field.GetText() != wxEmptyString )
{
BOX2I fieldBBox = field.GetBoundingBox();
if( Type() == SCH_LABEL_T || Type() == SCH_GLOBAL_LABEL_T )
fieldBBox.Offset( GetSchematicTextOffset( nullptr ) );
box.Merge( fieldBBox );
}
}
box.Normalize();
return box;
}
bool SCH_LABEL_BASE::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
{
BOX2I bbox = GetBodyBoundingBox();
bbox.Inflate( aAccuracy );
if( bbox.Contains( aPosition ) )
return true;
for( const SCH_FIELD& field : m_fields )
{
if( field.IsVisible() )
{
BOX2I fieldBBox = field.GetBoundingBox();
fieldBBox.Inflate( aAccuracy );
if( Type() == SCH_LABEL_T || Type() == SCH_GLOBAL_LABEL_T )
fieldBBox.Offset( GetSchematicTextOffset( nullptr ) );
if( fieldBBox.Contains( aPosition ) )
return true;
}
}
return false;
}
bool SCH_LABEL_BASE::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
{
BOX2I rect = aRect;
rect.Inflate( aAccuracy );
if( aContained )
{
return rect.Contains( GetBoundingBox() );