-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
sch_label.h
631 lines (464 loc) · 18.8 KB
/
sch_label.h
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
/*
* 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
*/
#ifndef SCH_LABEL_H
#define SCH_LABEL_H
#include <sch_text.h>
#include <sch_item.h>
#include <sch_field.h>
#include <sch_connection.h> // for CONNECTION_TYPE
class SCH_RULE_AREA;
/*
* Spin style for labels of all kinds on schematics
* Basically a higher level abstraction of rotation and justification of text
*/
class SPIN_STYLE
{
public:
enum SPIN : int
{
LEFT = 0,
UP = 1,
RIGHT = 2,
BOTTOM = 3
};
SPIN_STYLE() = default;
constexpr SPIN_STYLE( SPIN aSpin ) : m_spin( aSpin )
{
}
constexpr bool operator==( SPIN a ) const
{
return m_spin == a;
}
constexpr bool operator!=( SPIN a ) const
{
return m_spin != a;
}
operator int() const
{
return static_cast<int>( m_spin );
}
SPIN_STYLE RotateCCW();
/**
* Mirror the label spin style across the X axis or simply swaps up and bottom.
*/
SPIN_STYLE MirrorX();
/**
* Mirror the label spin style across the Y axis or simply swaps left and right.
*/
SPIN_STYLE MirrorY();
/**
* Get CCW rotation needed to get to the given spin style.
*/
unsigned CCWRotationsTo( const SPIN_STYLE& aOther ) const;
private:
SPIN m_spin;
};
/*
* Label and flag shapes used with text objects.
*/
enum LABEL_FLAG_SHAPE : unsigned int
{
L_INPUT,
L_OUTPUT,
L_BIDI,
L_TRISTATE,
L_UNSPECIFIED,
F_FIRST,
F_DOT = F_FIRST,
F_ROUND,
F_DIAMOND,
F_RECTANGLE
};
/*
* Specific enums for property manager (not used elsewhere)
*/
enum LABEL_SHAPE : unsigned int
{
LABEL_INPUT = L_INPUT,
LABEL_OUTPUT = L_OUTPUT,
LABEL_BIDI = L_BIDI,
LABEL_TRISTATE = L_TRISTATE,
LABEL_PASSIVE = L_UNSPECIFIED
};
enum FLAG_SHAPE : unsigned int
{
FLAG_DOT = F_DOT,
FLAG_CIRCLE = F_ROUND,
FLAG_DIAMOND = F_DIAMOND,
FLAG_RECTANGLE = F_RECTANGLE
};
class SCH_LABEL_BASE : public SCH_TEXT
{
public:
SCH_LABEL_BASE( const VECTOR2I& aPos, const wxString& aText, KICAD_T aType );
SCH_LABEL_BASE( const SCH_LABEL_BASE& aLabel );
SCH_LABEL_BASE& operator=( const SCH_LABEL_BASE& aLabel );
// Abstract class
virtual wxString GetClass() const override = 0;
bool IsType( const std::vector<KICAD_T>& aScanTypes ) const override;
void SwapData( SCH_ITEM* aItem ) override;
bool CanConnect( const SCH_ITEM* aItem ) const override
{
switch( aItem->Type() )
{
case SCH_LINE_T:
return aItem->GetLayer() == LAYER_WIRE || aItem->GetLayer() == LAYER_BUS;
case SCH_BUS_WIRE_ENTRY_T:
return true;
case SCH_SYMBOL_T:
return true;
case SCH_LABEL_T:
case SCH_GLOBAL_LABEL_T:
case SCH_HIER_LABEL_T:
case SCH_DIRECTIVE_LABEL_T:
case SCH_SHEET_PIN_T:
return true;
default:
return false;
}
}
bool HasConnectivityChanges( const SCH_ITEM* aItem,
const SCH_SHEET_PATH* aInstance = nullptr ) const override;
LABEL_FLAG_SHAPE GetShape() const { return m_shape; }
void SetShape( LABEL_FLAG_SHAPE aShape ) { m_shape = aShape; }
// Type-specific versions for property manager
LABEL_SHAPE GetLabelShape() const { return (LABEL_SHAPE) m_shape; }
void SetLabelShape( LABEL_SHAPE aShape ) { m_shape = (LABEL_FLAG_SHAPE) aShape; }
COLOR4D GetLabelColor() const;
virtual void SetSpinStyle( SPIN_STYLE aSpinStyle );
SPIN_STYLE GetSpinStyle() const;
void SetLastResolvedState( const SCH_ITEM* aItem ) override
{
const SCH_LABEL_BASE* aLabel = dynamic_cast<const SCH_LABEL_BASE*>( aItem );
if( aLabel )
m_lastResolvedColor = aLabel->m_lastResolvedColor;
}
static const wxString GetDefaultFieldName( const wxString& aName, bool aUseDefaultName );
virtual int GetMandatoryFieldCount() { return 0; }
std::vector<SCH_FIELD>& GetFields() { return m_fields; }
const std::vector<SCH_FIELD>& GetFields() const { return m_fields; }
/**
* Set multiple schematic fields.
*
* @param aFields are the fields to set in this symbol.
*/
void SetFields( const std::vector<SCH_FIELD>& aFields )
{
m_fields = aFields; // vector copying, length is changed possibly
}
void AddFields( const std::vector<SCH_FIELD>& aFields )
{
m_fields.insert( m_fields.end(), aFields.begin(), aFields.end() );
}
void AddField( const SCH_FIELD& aField )
{
m_fields.push_back( aField );
}
/**
* Increment the label text, if it ends with a number.
*
* @param aIncrement = the increment value to add to the number ending the text.
*/
bool IncrementLabel( int aIncrement );
void Move( const VECTOR2I& aMoveVector ) override;
void Rotate( const VECTOR2I& aCenter, bool aRotateCCW ) override;
void Rotate90( bool aClockwise ) override;
void MirrorSpinStyle( bool aLeftRight ) override;
void MirrorHorizontally( int aCenter ) override;
void MirrorVertically( int aCenter ) override;
void SetPosition( const VECTOR2I& aPosition ) override;
void AutoplaceFields( SCH_SCREEN* aScreen, AUTOPLACE_ALGO aAlgo ) override;
/**
* Builds an array of { pageNumber, pageName } pairs.
* @param pages [out] Array of { pageNumber, pageName } pairs.
*/
void GetIntersheetRefs( const SCH_SHEET_PATH* aPath,
std::vector<std::pair<wxString, wxString>>* pages );
/**
* Return the list of system text vars & fields for this label.
*/
void GetContextualTextVars( wxArrayString* aVars ) const;
/**
* Resolve any references to system tokens supported by the label.
*
* @param aDepth a counter to limit recursion and circular references.
*/
virtual bool ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token, int aDepth ) const;
wxString GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraText,
int aDepth = 0 ) const override;
wxString GetShownText( bool aAllowExtraText, int aDepth = 0 ) const override
{
SCHEMATIC* schematic = Schematic();
if( schematic )
return GetShownText( &schematic->CurrentSheet(), aAllowExtraText, aDepth );
else
return GetText();
}
bool HasCachedDriverName() const override;
const wxString& GetCachedDriverName() const override;
void RunOnChildren( const std::function<void( SCH_ITEM* )>& aFunction ) override;
INSPECT_RESULT Visit( INSPECTOR inspector, void* testData,
const std::vector<KICAD_T>& scanTypes ) override;
bool Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) const override;
bool Replace( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) override;
VECTOR2I GetSchematicTextOffset( const RENDER_SETTINGS* aSettings ) const override;
/**
* Calculate the graphic shape (a polygon) associated to the text.
*
* @param aPoints A buffer to fill with polygon corners coordinates
* @param Pos Position of the shape, for texts and labels: do nothing
*/
virtual void CreateGraphicShape( const RENDER_SETTINGS* aSettings,
std::vector<VECTOR2I>& aPoints, const VECTOR2I& Pos ) const
{
aPoints.clear();
}
int GetLabelBoxExpansion( const RENDER_SETTINGS* aSettings = nullptr ) const;
/**
* Return the bounding box of the label only, without taking in account its fields.
*/
virtual const BOX2I GetBodyBoundingBox() const;
/**
* Return the bounding box of the label including its fields.
*/
const BOX2I GetBoundingBox() const override;
bool HitTest( const VECTOR2I& aPosition, int aAccuracy = 0 ) const override;
bool HitTest( const BOX2I& aRect, bool aContained, int aAccuracy = 0 ) const override;
std::vector<VECTOR2I> GetConnectionPoints() const override;
void GetEndPoints( std::vector< DANGLING_END_ITEM >& aItemList ) override;
bool UpdateDanglingState( std::vector<DANGLING_END_ITEM>& aItemListByType,
std::vector<DANGLING_END_ITEM>& aItemListByPos,
const SCH_SHEET_PATH* aPath = nullptr ) override;
bool IsDangling() const override { return m_isDangling; }
void SetIsDangling( bool aIsDangling ) { m_isDangling = aIsDangling; }
std::vector<int> ViewGetLayers() const override;
void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
void Print( const SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBodyStyle,
const VECTOR2I& offset, bool aForceNoFill, bool aDimmed ) override;
void PrintBackground( const SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBodyStyle,
const VECTOR2I& aOffset, bool aDimmed ) override {}
void Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed ) override;
/**
* @brief autoRotateOnPlacement
* @return Returns true if the label rotation will be automatically set on the placement
*/
bool AutoRotateOnPlacement() const;
/**
* @brief setAutoRotateOnPlacement
* @param autoRotate If set to true when the label is placed in the connection to a
* pin/net the direction will be automatically set according to the positioning of the net/pin
*/
void SetAutoRotateOnPlacement( bool autoRotate = true );
/**
* @brief AutoRotateOnPlacementSupported
* @return true if the automated rotation of the label is supported after the placement
* At the moment it is supported for global and hierarchial labels
*/
virtual bool AutoRotateOnPlacementSupported() const = 0;
double Similarity( const SCH_ITEM& aItem ) const override;
bool operator==( const SCH_ITEM& aItem ) const override;
protected:
void cacheShownText() override;
protected:
std::vector<SCH_FIELD> m_fields;
LABEL_FLAG_SHAPE m_shape;
CONNECTION_TYPE m_connectionType;
bool m_isDangling;
bool m_autoRotateOnPlacement = false;
mutable COLOR4D m_lastResolvedColor;
wxString m_cached_driver_name;
};
class SCH_LABEL : public SCH_LABEL_BASE
{
public:
SCH_LABEL( const VECTOR2I& aPos = VECTOR2I( 0, 0 ), const wxString& aText = wxEmptyString );
// Do not create a copy constructor. The one generated by the compiler is adequate.
~SCH_LABEL() { }
void Serialize( google::protobuf::Any &aContainer ) const override;
bool Deserialize( const google::protobuf::Any &aContainer ) override;
static inline bool ClassOf( const EDA_ITEM* aItem )
{
return aItem && SCH_LABEL_T == aItem->Type();
}
wxString GetClass() const override
{
return wxT( "SCH_LABEL" );
}
const BOX2I GetBodyBoundingBox() const override;
bool IsConnectable() const override { return true; }
wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const override;
BITMAPS GetMenuImage() const override;
bool IsReplaceable() const override { return true; }
EDA_ITEM* Clone() const override
{
return new SCH_LABEL( *this );
}
bool IsPointClickableAnchor( const VECTOR2I& aPos ) const override
{
return m_isDangling && GetPosition() == aPos;
}
bool AutoRotateOnPlacementSupported() const override { return false; }
private:
bool doIsConnected( const VECTOR2I& aPosition ) const override
{
return EDA_TEXT::GetTextPos() == aPosition;
}
};
class SCH_DIRECTIVE_LABEL : public SCH_LABEL_BASE
{
public:
SCH_DIRECTIVE_LABEL( const VECTOR2I& aPos = VECTOR2I( 0, 0 ) );
SCH_DIRECTIVE_LABEL( const SCH_DIRECTIVE_LABEL& aClassLabel );
~SCH_DIRECTIVE_LABEL() { }
void Serialize( google::protobuf::Any &aContainer ) const override;
bool Deserialize( const google::protobuf::Any &aContainer ) override;
static inline bool ClassOf( const EDA_ITEM* aItem )
{
return aItem && SCH_DIRECTIVE_LABEL_T == aItem->Type();
}
wxString GetClass() const override
{
return wxT( "SCH_DIRECTIVE_LABEL" );
}
EDA_ITEM* Clone() const override
{
return new SCH_DIRECTIVE_LABEL( *this );
}
void SwapData( SCH_ITEM* aItem ) override;
FLAG_SHAPE GetFlagShape() const { return (FLAG_SHAPE) m_shape; }
void SetFlagShape( FLAG_SHAPE aShape ) { m_shape = (LABEL_FLAG_SHAPE) aShape; }
int GetPinLength() const { return m_pinLength; }
void SetPinLength( int aLength ) { m_pinLength = aLength; }
int GetPenWidth() const override;
void CreateGraphicShape( const RENDER_SETTINGS* aSettings,
std::vector<VECTOR2I>& aPoints,
const VECTOR2I& aPos ) const override;
void AutoplaceFields( SCH_SCREEN* aScreen, AUTOPLACE_ALGO aAlgo ) override;
wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const override;
bool IsConnectable() const override { return true; }
bool AutoRotateOnPlacementSupported() const override { return false; }
void MirrorSpinStyle( bool aLeftRight ) override;
void MirrorHorizontally( int aCenter ) override;
void MirrorVertically( int aCenter ) override;
/// @brief Adds an entry to the connected rule area cache
void AddConnectedRuleArea( SCH_RULE_AREA* aRuleArea );
/// @brief Removes all rule areas from the cache
void ClearConnectedRuleAreas();
/// @brief Removes a specific rule area from the cache
void RemoveConnectedRuleArea( SCH_RULE_AREA* aRuleArea );
/// @brief Determines dangling state from connectivity and cached connected rule areas
virtual bool IsDangling() const override;
private:
int m_pinLength;
int m_symbolSize;
/// Cache of any rule areas with borders which this label connects to
std::unordered_set<SCH_RULE_AREA*> m_connected_rule_areas;
};
class SCH_GLOBALLABEL : public SCH_LABEL_BASE
{
public:
SCH_GLOBALLABEL( const VECTOR2I& aPos = VECTOR2I( 0, 0 ),
const wxString& aText = wxEmptyString );
SCH_GLOBALLABEL( const SCH_GLOBALLABEL& aGlobalLabel );
~SCH_GLOBALLABEL() { }
void Serialize( google::protobuf::Any &aContainer ) const override;
bool Deserialize( const google::protobuf::Any &aContainer ) override;
static inline bool ClassOf( const EDA_ITEM* aItem )
{
return aItem && SCH_GLOBAL_LABEL_T == aItem->Type();
}
wxString GetClass() const override
{
return wxT( "SCH_GLOBALLABEL" );
}
EDA_ITEM* Clone() const override
{
return new SCH_GLOBALLABEL( *this );
}
int GetMandatoryFieldCount() override { return 1; }
void SetSpinStyle( SPIN_STYLE aSpinStyle ) override;
VECTOR2I GetSchematicTextOffset( const RENDER_SETTINGS* aSettings ) const override;
void CreateGraphicShape( const RENDER_SETTINGS* aRenderSettings,
std::vector<VECTOR2I>& aPoints,
const VECTOR2I& aPos ) const override;
bool ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token, int aDepth ) const override;
bool IsConnectable() const override { return true; }
std::vector<int> ViewGetLayers() const override;
wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const override;
BITMAPS GetMenuImage() const override;
bool IsPointClickableAnchor( const VECTOR2I& aPos ) const override
{
return m_isDangling && GetPosition() == aPos;
}
bool AutoRotateOnPlacementSupported() const override { return true; }
private:
bool doIsConnected( const VECTOR2I& aPosition ) const override
{
return EDA_TEXT::GetTextPos() == aPosition;
}
};
class SCH_HIERLABEL : public SCH_LABEL_BASE
{
public:
SCH_HIERLABEL( const VECTOR2I& aPos = VECTOR2I( 0, 0 ), const wxString& aText = wxEmptyString,
KICAD_T aType = SCH_HIER_LABEL_T );
// Do not create a copy constructor. The one generated by the compiler is adequate.
~SCH_HIERLABEL() { }
void Serialize( google::protobuf::Any &aContainer ) const override;
bool Deserialize( const google::protobuf::Any &aContainer ) override;
static inline bool ClassOf( const EDA_ITEM* aItem )
{
return aItem && SCH_HIER_LABEL_T == aItem->Type();
}
wxString GetClass() const override
{
return wxT( "SCH_HIERLABEL" );
}
void SetSpinStyle( SPIN_STYLE aSpinStyle ) override;
VECTOR2I GetSchematicTextOffset( const RENDER_SETTINGS* aSettings ) const override;
void CreateGraphicShape( const RENDER_SETTINGS* aSettings, std::vector<VECTOR2I>& aPoints,
const VECTOR2I& aPos ) const override;
void CreateGraphicShape( const RENDER_SETTINGS* aSettings, std::vector<VECTOR2I>& aPoints,
const VECTOR2I& aPos, LABEL_FLAG_SHAPE aShape ) const;
const BOX2I GetBodyBoundingBox() const override;
bool IsConnectable() const override { return true; }
wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const override;
BITMAPS GetMenuImage() const override;
EDA_ITEM* Clone() const override
{
return new SCH_HIERLABEL( *this );
}
bool IsPointClickableAnchor( const VECTOR2I& aPos ) const override
{
return m_isDangling && GetPosition() == aPos;
}
bool AutoRotateOnPlacementSupported() const override { return true; }
private:
bool doIsConnected( const VECTOR2I& aPosition ) const override
{
return EDA_TEXT::GetTextPos() == aPosition;
}
};
#endif /* SCH_LABEL_H */