You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

386 lines
12 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef SCH_LABEL_H
  25. #define SCH_LABEL_H
  26. #include <sch_text.h>
  27. #include <sch_item.h>
  28. #include <sch_field.h>
  29. #include <sch_connection.h> // for CONNECTION_TYPE
  30. extern const char* SheetLabelType[]; /* names of types of labels */
  31. class SCH_LABEL_BASE : public SCH_TEXT
  32. {
  33. public:
  34. SCH_LABEL_BASE( const VECTOR2I& aPos, const wxString& aText, KICAD_T aType );
  35. SCH_LABEL_BASE( const SCH_LABEL_BASE& aLabel );
  36. // Abstract class
  37. virtual wxString GetClass() const override = 0;
  38. bool IsType( const KICAD_T aScanTypes[] ) const override;
  39. void SwapData( SCH_ITEM* aItem ) override;
  40. LABEL_FLAG_SHAPE GetShape() const override { return m_shape; }
  41. void SetShape( LABEL_FLAG_SHAPE aShape ) override { m_shape = aShape; }
  42. static const wxString GetDefaultFieldName( const wxString& aName, bool aUseDefaultName );
  43. virtual int GetMandatoryFieldCount() { return 0; }
  44. std::vector<SCH_FIELD>& GetFields() { return m_fields; }
  45. const std::vector<SCH_FIELD>& GetFields() const { return m_fields; }
  46. /**
  47. * Set multiple schematic fields.
  48. *
  49. * @param aFields are the fields to set in this symbol.
  50. */
  51. void SetFields( const std::vector<SCH_FIELD>& aFields )
  52. {
  53. m_fields = aFields; // vector copying, length is changed possibly
  54. }
  55. /**
  56. * Increment the label text, if it ends with a number.
  57. *
  58. * @param aIncrement = the increment value to add to the number ending the text.
  59. */
  60. bool IncrementLabel( int aIncrement );
  61. void Move( const VECTOR2I& aMoveVector ) override
  62. {
  63. SCH_TEXT::Move( aMoveVector );
  64. for( SCH_FIELD& field : m_fields )
  65. field.Offset( aMoveVector );
  66. }
  67. void Rotate( const VECTOR2I& aCenter ) override;
  68. void Rotate90( bool aClockwise ) override;
  69. void AutoplaceFields( SCH_SCREEN* aScreen, bool aManual ) override;
  70. virtual bool ResolveTextVar( wxString* token, int aDepth ) const;
  71. wxString GetShownText( int aDepth = 0 ) const override;
  72. void RunOnChildren( const std::function<void( SCH_ITEM* )>& aFunction ) override;
  73. SEARCH_RESULT Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] ) override;
  74. VECTOR2I GetSchematicTextOffset( const RENDER_SETTINGS* aSettings ) const override;
  75. /**
  76. * Calculate the graphic shape (a polygon) associated to the text.
  77. *
  78. * @param aPoints A buffer to fill with polygon corners coordinates
  79. * @param Pos Position of the shape, for texts and labels: do nothing
  80. */
  81. virtual void CreateGraphicShape( const RENDER_SETTINGS* aSettings,
  82. std::vector<VECTOR2I>& aPoints, const VECTOR2I& Pos ) const
  83. {
  84. aPoints.clear();
  85. }
  86. int GetLabelBoxExpansion( const RENDER_SETTINGS* aSettings = nullptr ) const;
  87. /**
  88. * Return the bounding box of the label only, without taking in account its fields.
  89. */
  90. virtual const EDA_RECT GetBodyBoundingBox() const;
  91. /**
  92. * Return the bounding box of the label including its fields.
  93. */
  94. const EDA_RECT GetBoundingBox() const override;
  95. bool HitTest( const VECTOR2I& aPosition, int aAccuracy = 0 ) const override;
  96. bool HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy = 0 ) const override;
  97. std::vector<VECTOR2I> GetConnectionPoints() const override;
  98. void GetEndPoints( std::vector< DANGLING_END_ITEM >& aItemList ) override;
  99. bool UpdateDanglingState( std::vector<DANGLING_END_ITEM>& aItemList,
  100. const SCH_SHEET_PATH* aPath = nullptr ) override;
  101. bool IsDangling() const override { return m_isDangling; }
  102. void SetIsDangling( bool aIsDangling ) { m_isDangling = aIsDangling; }
  103. void ViewGetLayers( int aLayers[], int& aCount ) const override;
  104. void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
  105. void Plot( PLOTTER* aPlotter, bool aBackground ) const override;
  106. void Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& offset ) override;
  107. protected:
  108. std::vector<SCH_FIELD> m_fields;
  109. LABEL_FLAG_SHAPE m_shape;
  110. CONNECTION_TYPE m_connectionType;
  111. bool m_isDangling;
  112. };
  113. class SCH_LABEL : public SCH_LABEL_BASE
  114. {
  115. public:
  116. SCH_LABEL( const VECTOR2I& aPos = VECTOR2I( 0, 0 ), const wxString& aText = wxEmptyString );
  117. // Do not create a copy constructor. The one generated by the compiler is adequate.
  118. ~SCH_LABEL() { }
  119. static inline bool ClassOf( const EDA_ITEM* aItem )
  120. {
  121. return aItem && SCH_LABEL_T == aItem->Type();
  122. }
  123. wxString GetClass() const override
  124. {
  125. return wxT( "SCH_LABEL" );
  126. }
  127. const EDA_RECT GetBodyBoundingBox() const override;
  128. bool IsConnectable() const override { return true; }
  129. bool CanConnect( const SCH_ITEM* aItem ) const override
  130. {
  131. return aItem->Type() == SCH_LINE_T &&
  132. ( aItem->GetLayer() == LAYER_WIRE || aItem->GetLayer() == LAYER_BUS );
  133. }
  134. wxString GetSelectMenuText( EDA_UNITS aUnits ) const override;
  135. BITMAPS GetMenuImage() const override;
  136. bool IsReplaceable() const override { return true; }
  137. EDA_ITEM* Clone() const override
  138. {
  139. return new SCH_LABEL( *this );
  140. }
  141. bool IsPointClickableAnchor( const VECTOR2I& aPos ) const override
  142. {
  143. return m_isDangling && GetPosition() == aPos;
  144. }
  145. private:
  146. bool doIsConnected( const VECTOR2I& aPosition ) const override
  147. {
  148. return EDA_TEXT::GetTextPos() == aPosition;
  149. }
  150. };
  151. class SCH_DIRECTIVE_LABEL : public SCH_LABEL_BASE
  152. {
  153. public:
  154. SCH_DIRECTIVE_LABEL( const VECTOR2I& aPos = VECTOR2I( 0, 0 ) );
  155. SCH_DIRECTIVE_LABEL( const SCH_DIRECTIVE_LABEL& aClassLabel );
  156. ~SCH_DIRECTIVE_LABEL() { }
  157. static inline bool ClassOf( const EDA_ITEM* aItem )
  158. {
  159. return aItem && SCH_DIRECTIVE_LABEL_T == aItem->Type();
  160. }
  161. wxString GetClass() const override
  162. {
  163. return wxT( "SCH_DIRECTIVE_LABEL" );
  164. }
  165. EDA_ITEM* Clone() const override
  166. {
  167. return new SCH_DIRECTIVE_LABEL( *this );
  168. }
  169. void SwapData( SCH_ITEM* aItem ) override;
  170. int GetPinLength() const { return m_pinLength; }
  171. void SetPinLength( int aLength ) { m_pinLength = aLength; }
  172. void CreateGraphicShape( const RENDER_SETTINGS* aSettings, std::vector<VECTOR2I>& aPoints,
  173. const VECTOR2I& aPos ) const override;
  174. void AutoplaceFields( SCH_SCREEN* aScreen, bool aManual ) override;
  175. wxString GetSelectMenuText( EDA_UNITS aUnits ) const override;
  176. bool IsConnectable() const override { return true; }
  177. bool CanConnect( const SCH_ITEM* aItem ) const override
  178. {
  179. return aItem->Type() == SCH_LINE_T &&
  180. ( aItem->GetLayer() == LAYER_WIRE || aItem->GetLayer() == LAYER_BUS );
  181. }
  182. private:
  183. int m_pinLength;
  184. int m_symbolSize;
  185. };
  186. class SCH_GLOBALLABEL : public SCH_LABEL_BASE
  187. {
  188. public:
  189. SCH_GLOBALLABEL( const VECTOR2I& aPos = VECTOR2I( 0, 0 ), const wxString& aText = wxEmptyString );
  190. SCH_GLOBALLABEL( const SCH_GLOBALLABEL& aGlobalLabel );
  191. ~SCH_GLOBALLABEL() { }
  192. static inline bool ClassOf( const EDA_ITEM* aItem )
  193. {
  194. return aItem && SCH_GLOBAL_LABEL_T == aItem->Type();
  195. }
  196. wxString GetClass() const override
  197. {
  198. return wxT( "SCH_GLOBALLABEL" );
  199. }
  200. EDA_ITEM* Clone() const override
  201. {
  202. return new SCH_GLOBALLABEL( *this );
  203. }
  204. int GetMandatoryFieldCount() override { return 1; }
  205. void MirrorSpinStyle( bool aLeftRight ) override;
  206. void MirrorHorizontally( int aCenter ) override;
  207. void MirrorVertically( int aCenter ) override;
  208. void SetTextSpinStyle( TEXT_SPIN_STYLE aSpinStyle ) override;
  209. VECTOR2I GetSchematicTextOffset( const RENDER_SETTINGS* aSettings ) const override;
  210. void CreateGraphicShape( const RENDER_SETTINGS* aRenderSettings, std::vector<VECTOR2I>& aPoints,
  211. const VECTOR2I& aPos ) const override;
  212. bool ResolveTextVar( wxString* token, int aDepth ) const override;
  213. bool IsConnectable() const override { return true; }
  214. bool CanConnect( const SCH_ITEM* aItem ) const override
  215. {
  216. return aItem->Type() == SCH_LINE_T &&
  217. ( aItem->GetLayer() == LAYER_WIRE || aItem->GetLayer() == LAYER_BUS );
  218. }
  219. void ViewGetLayers( int aLayers[], int& aCount ) const override;
  220. wxString GetSelectMenuText( EDA_UNITS aUnits ) const override;
  221. BITMAPS GetMenuImage() const override;
  222. bool IsPointClickableAnchor( const VECTOR2I& aPos ) const override
  223. {
  224. return m_isDangling && GetPosition() == aPos;
  225. }
  226. private:
  227. bool doIsConnected( const VECTOR2I& aPosition ) const override
  228. {
  229. return EDA_TEXT::GetTextPos() == aPosition;
  230. }
  231. };
  232. class SCH_HIERLABEL : public SCH_LABEL_BASE
  233. {
  234. public:
  235. SCH_HIERLABEL( const VECTOR2I& aPos = VECTOR2I( 0, 0 ), const wxString& aText = wxEmptyString,
  236. KICAD_T aType = SCH_HIER_LABEL_T );
  237. // Do not create a copy constructor. The one generated by the compiler is adequate.
  238. ~SCH_HIERLABEL() { }
  239. static inline bool ClassOf( const EDA_ITEM* aItem )
  240. {
  241. return aItem && SCH_HIER_LABEL_T == aItem->Type();
  242. }
  243. wxString GetClass() const override
  244. {
  245. return wxT( "SCH_HIERLABEL" );
  246. }
  247. void SetTextSpinStyle( TEXT_SPIN_STYLE aSpinStyle ) override;
  248. VECTOR2I GetSchematicTextOffset( const RENDER_SETTINGS* aSettings ) const override;
  249. void CreateGraphicShape( const RENDER_SETTINGS* aSettings, std::vector<VECTOR2I>& aPoints,
  250. const VECTOR2I& aPos ) const override;
  251. void CreateGraphicShape( const RENDER_SETTINGS* aSettings, std::vector<VECTOR2I>& aPoints,
  252. const VECTOR2I& aPos, LABEL_FLAG_SHAPE aShape ) const;
  253. const EDA_RECT GetBodyBoundingBox() const override;
  254. bool IsConnectable() const override { return true; }
  255. bool CanConnect( const SCH_ITEM* aItem ) const override
  256. {
  257. return aItem->Type() == SCH_LINE_T &&
  258. ( aItem->GetLayer() == LAYER_WIRE || aItem->GetLayer() == LAYER_BUS );
  259. }
  260. wxString GetSelectMenuText( EDA_UNITS aUnits ) const override;
  261. BITMAPS GetMenuImage() const override;
  262. EDA_ITEM* Clone() const override
  263. {
  264. return new SCH_HIERLABEL( *this );
  265. }
  266. bool IsPointClickableAnchor( const VECTOR2I& aPos ) const override
  267. {
  268. return m_isDangling && GetPosition() == aPos;
  269. }
  270. private:
  271. bool doIsConnected( const VECTOR2I& aPosition ) const override
  272. {
  273. return EDA_TEXT::GetTextPos() == aPosition;
  274. }
  275. };
  276. #endif /* SCH_LABEL_H */