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.

379 lines
8.9 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2006 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 1992-2017 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. /**
  25. * @file sch_sheet_pin.cpp
  26. * @brief Implementation of the SCH_SHEET_PIN class.
  27. */
  28. #include <fctsys.h>
  29. #include <gr_basic.h>
  30. #include <sch_draw_panel.h>
  31. #include <draw_graphic_text.h>
  32. #include <plotter.h>
  33. #include <trigo.h>
  34. #include <richio.h>
  35. #include <sch_edit_frame.h>
  36. #include <bitmaps.h>
  37. #include <general.h>
  38. #include <sch_sheet.h>
  39. #include <kicad_string.h>
  40. #include <trace_helpers.h>
  41. SCH_SHEET_PIN::SCH_SHEET_PIN( SCH_SHEET* parent, const wxPoint& pos, const wxString& text ) :
  42. SCH_HIERLABEL( pos, text, SCH_SHEET_PIN_T )
  43. {
  44. SetParent( parent );
  45. wxASSERT( parent );
  46. m_Layer = LAYER_SHEETLABEL;
  47. SetTextPos( pos );
  48. if( parent->IsVerticalOrientation() )
  49. SetEdge( SHEET_TOP_SIDE );
  50. else
  51. SetEdge( SHEET_LEFT_SIDE );
  52. m_shape = NET_INPUT;
  53. m_isDangling = true;
  54. m_number = 2;
  55. }
  56. EDA_ITEM* SCH_SHEET_PIN::Clone() const
  57. {
  58. return new SCH_SHEET_PIN( *this );
  59. }
  60. void SCH_SHEET_PIN::Draw( EDA_DRAW_PANEL* aPanel,
  61. wxDC* aDC,
  62. const wxPoint& aOffset,
  63. GR_DRAWMODE aDraw_mode,
  64. COLOR4D aColor )
  65. {
  66. // The icon selection is handle by the virtual method CreateGraphicShape
  67. // called by ::Draw
  68. SCH_HIERLABEL::Draw( aPanel, aDC, aOffset, aDraw_mode, aColor );
  69. }
  70. void SCH_SHEET_PIN::SwapData( SCH_ITEM* aItem )
  71. {
  72. wxCHECK_RET( aItem->Type() == SCH_SHEET_PIN_T,
  73. wxString::Format( wxT( "SCH_SHEET_PIN object cannot swap data with %s object." ),
  74. GetChars( aItem->GetClass() ) ) );
  75. SCH_SHEET_PIN* pin = ( SCH_SHEET_PIN* ) aItem;
  76. SCH_TEXT::SwapData( (SCH_TEXT*) pin );
  77. int tmp = pin->GetNumber();
  78. pin->SetNumber( GetNumber() );
  79. SetNumber( tmp );
  80. SHEET_SIDE stmp = pin->GetEdge();
  81. pin->SetEdge( GetEdge() );
  82. SetEdge( stmp );
  83. }
  84. bool SCH_SHEET_PIN::operator==( const SCH_SHEET_PIN* aPin ) const
  85. {
  86. return aPin == this;
  87. }
  88. int SCH_SHEET_PIN::GetPenSize() const
  89. {
  90. return GetDefaultLineThickness();
  91. }
  92. void SCH_SHEET_PIN::SetNumber( int aNumber )
  93. {
  94. wxASSERT( aNumber >= 2 );
  95. m_number = aNumber;
  96. }
  97. void SCH_SHEET_PIN::SetEdge( SCH_SHEET_PIN::SHEET_SIDE aEdge )
  98. {
  99. SCH_SHEET* Sheet = GetParent();
  100. // use SHEET_UNDEFINED_SIDE to adjust text orientation without changing edge
  101. switch( aEdge )
  102. {
  103. case SHEET_LEFT_SIDE:
  104. m_edge = aEdge;
  105. SetTextX( Sheet->m_pos.x );
  106. SetLabelSpinStyle( 2 ); // Orientation horiz inverse
  107. break;
  108. case SHEET_RIGHT_SIDE:
  109. m_edge = aEdge;
  110. SetTextX( Sheet->m_pos.x + Sheet->m_size.x );
  111. SetLabelSpinStyle( 0 ); // Orientation horiz normal
  112. break;
  113. case SHEET_TOP_SIDE:
  114. m_edge = aEdge;
  115. SetTextY( Sheet->m_pos.y );
  116. SetLabelSpinStyle( 3 ); // Orientation vert BOTTOM
  117. break;
  118. case SHEET_BOTTOM_SIDE:
  119. m_edge = aEdge;
  120. SetTextY( Sheet->m_pos.y + Sheet->m_size.y );
  121. SetLabelSpinStyle( 1 ); // Orientation vert UP
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. enum SCH_SHEET_PIN::SHEET_SIDE SCH_SHEET_PIN::GetEdge() const
  128. {
  129. return m_edge;
  130. }
  131. void SCH_SHEET_PIN::ConstrainOnEdge( wxPoint Pos )
  132. {
  133. SCH_SHEET* sheet = GetParent();
  134. if( sheet == NULL )
  135. return;
  136. wxPoint center = sheet->m_pos + ( sheet->m_size / 2 );
  137. if( m_edge == SHEET_LEFT_SIDE || m_edge == SHEET_RIGHT_SIDE )
  138. {
  139. if( Pos.x > center.x )
  140. {
  141. SetEdge( SHEET_RIGHT_SIDE );
  142. }
  143. else
  144. {
  145. SetEdge( SHEET_LEFT_SIDE );
  146. }
  147. SetTextY( Pos.y );
  148. if( GetTextPos().y < sheet->m_pos.y )
  149. SetTextY( sheet->m_pos.y );
  150. if( GetTextPos().y > (sheet->m_pos.y + sheet->m_size.y) )
  151. SetTextY( sheet->m_pos.y + sheet->m_size.y );
  152. }
  153. else
  154. {
  155. if( Pos.y > center.y )
  156. {
  157. SetEdge( SHEET_BOTTOM_SIDE );
  158. }
  159. else
  160. {
  161. SetEdge( SHEET_TOP_SIDE );
  162. }
  163. SetTextX( Pos.x );
  164. if( GetTextPos().x < sheet->m_pos.x )
  165. SetTextX( sheet->m_pos.x );
  166. if( GetTextPos().x > (sheet->m_pos.x + sheet->m_size.x) )
  167. SetTextX( sheet->m_pos.x + sheet->m_size.x );
  168. }
  169. }
  170. bool SCH_SHEET_PIN::Matches( wxFindReplaceData& aSearchData,
  171. void* aAuxData, wxPoint* aFindLocation )
  172. {
  173. wxCHECK_MSG( GetParent() != NULL, false,
  174. wxT( "Sheet pin " ) + m_Text + wxT( " does not have a parent sheet!" ) );
  175. wxLogTrace( traceFindItem, wxT( " child item " ) + GetSelectMenuText( MILLIMETRES ) );
  176. if( SCH_ITEM::Matches( m_Text, aSearchData ) )
  177. {
  178. if( aFindLocation )
  179. *aFindLocation = GetBoundingBox().Centre();
  180. return true;
  181. }
  182. return false;
  183. }
  184. void SCH_SHEET_PIN::MirrorX( int aXaxis_position )
  185. {
  186. int p = GetTextPos().y - aXaxis_position;
  187. SetTextY( aXaxis_position - p );
  188. switch( m_edge )
  189. {
  190. case SHEET_TOP_SIDE:
  191. SetEdge( SHEET_BOTTOM_SIDE );
  192. break;
  193. case SHEET_BOTTOM_SIDE:
  194. SetEdge( SHEET_TOP_SIDE );
  195. break;
  196. default:
  197. break;
  198. }
  199. }
  200. void SCH_SHEET_PIN::MirrorY( int aYaxis_position )
  201. {
  202. int p = GetTextPos().x - aYaxis_position;
  203. SetTextX( aYaxis_position - p );
  204. switch( m_edge )
  205. {
  206. case SHEET_LEFT_SIDE:
  207. SetEdge( SHEET_RIGHT_SIDE );
  208. break;
  209. case SHEET_RIGHT_SIDE:
  210. SetEdge( SHEET_LEFT_SIDE );
  211. break;
  212. default:
  213. break;
  214. }
  215. }
  216. void SCH_SHEET_PIN::Rotate( wxPoint aPosition )
  217. {
  218. wxPoint pt = GetTextPos();
  219. RotatePoint( &pt, aPosition, 900 );
  220. SetTextPos( pt );
  221. switch( m_edge )
  222. {
  223. case SHEET_LEFT_SIDE: //pin on left side
  224. SetEdge( SHEET_BOTTOM_SIDE );
  225. break;
  226. case SHEET_RIGHT_SIDE: //pin on right side
  227. SetEdge( SHEET_TOP_SIDE );
  228. break;
  229. case SHEET_TOP_SIDE: //pin on top side
  230. SetEdge( SHEET_LEFT_SIDE );
  231. break;
  232. case SHEET_BOTTOM_SIDE: //pin on bottom side
  233. SetEdge( SHEET_RIGHT_SIDE );
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. void SCH_SHEET_PIN::CreateGraphicShape( std::vector <wxPoint>& aPoints, const wxPoint& aPos )
  240. {
  241. /* This is the same icon shapes as SCH_HIERLABEL
  242. * but the graphic icon is slightly different in 2 cases:
  243. * for INPUT type the icon is the OUTPUT shape of SCH_HIERLABEL
  244. * for OUTPUT type the icon is the INPUT shape of SCH_HIERLABEL
  245. */
  246. PINSHEETLABEL_SHAPE tmp = m_shape;
  247. switch( m_shape )
  248. {
  249. case NET_INPUT:
  250. m_shape = NET_OUTPUT;
  251. break;
  252. case NET_OUTPUT:
  253. m_shape = NET_INPUT;
  254. break;
  255. default:
  256. break;
  257. }
  258. SCH_HIERLABEL::CreateGraphicShape( aPoints, aPos );
  259. m_shape = tmp;
  260. }
  261. void SCH_SHEET_PIN::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
  262. {
  263. DANGLING_END_ITEM item( SHEET_LABEL_END, this, GetTextPos() );
  264. aItemList.push_back( item );
  265. }
  266. wxString SCH_SHEET_PIN::GetSelectMenuText( EDA_UNITS_T aUnits ) const
  267. {
  268. return wxString::Format( _( "Hierarchical Sheet Pin %s" ), ShortenedShownText() );
  269. }
  270. BITMAP_DEF SCH_SHEET_PIN::GetMenuImage() const
  271. {
  272. return add_hierar_pin_xpm;
  273. }
  274. bool SCH_SHEET_PIN::HitTest( const wxPoint& aPoint, int aAccuracy ) const
  275. {
  276. EDA_RECT rect = GetBoundingBox();
  277. rect.Inflate( aAccuracy );
  278. return rect.Contains( aPoint );
  279. }
  280. #if defined(DEBUG)
  281. void SCH_SHEET_PIN::Show( int nestLevel, std::ostream& os ) const
  282. {
  283. // XML output:
  284. wxString s = GetClass();
  285. NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << ">"
  286. << " pin_name=\"" << TO_UTF8( m_Text )
  287. << '"' << "/>\n" << std::flush;
  288. // NestedSpace( nestLevel, os ) << "</" << s.Lower().mb_str() << ">\n";
  289. }
  290. #endif