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.

363 lines
9.1 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2004-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_bus_entry.cpp
  26. *
  27. */
  28. #include <fctsys.h>
  29. #include <gr_basic.h>
  30. #include <macros.h>
  31. #include <class_drawpanel.h>
  32. #include <trigo.h>
  33. #include <common.h>
  34. #include <richio.h>
  35. #include <class_plotter.h>
  36. #include <bitmaps.h>
  37. #include <eeschema_config.h>
  38. #include <general.h>
  39. #include <sch_bus_entry.h>
  40. SCH_BUS_ENTRY_BASE::SCH_BUS_ENTRY_BASE( KICAD_T aType, const wxPoint& pos, char shape ) :
  41. SCH_ITEM( NULL, aType )
  42. {
  43. m_pos = pos;
  44. m_size.x = 100;
  45. m_size.y = 100;
  46. if( shape == '/' )
  47. m_size.y = -100;
  48. m_isDanglingStart = m_isDanglingEnd = true;
  49. }
  50. SCH_BUS_WIRE_ENTRY::SCH_BUS_WIRE_ENTRY( const wxPoint& pos, char shape ) :
  51. SCH_BUS_ENTRY_BASE( SCH_BUS_WIRE_ENTRY_T, pos, shape )
  52. {
  53. m_Layer = LAYER_WIRE;
  54. }
  55. SCH_BUS_BUS_ENTRY::SCH_BUS_BUS_ENTRY( const wxPoint& pos, char shape ) :
  56. SCH_BUS_ENTRY_BASE( SCH_BUS_BUS_ENTRY_T, pos, shape )
  57. {
  58. m_Layer = LAYER_BUS;
  59. }
  60. EDA_ITEM* SCH_BUS_WIRE_ENTRY::Clone() const
  61. {
  62. return new SCH_BUS_WIRE_ENTRY( *this );
  63. }
  64. EDA_ITEM* SCH_BUS_BUS_ENTRY::Clone() const
  65. {
  66. return new SCH_BUS_BUS_ENTRY( *this );
  67. }
  68. bool SCH_BUS_ENTRY_BASE::doIsConnected( const wxPoint& aPosition ) const
  69. {
  70. return ( m_pos == aPosition || m_End() == aPosition );
  71. }
  72. wxPoint SCH_BUS_ENTRY_BASE::m_End() const
  73. {
  74. return wxPoint( m_pos.x + m_size.x, m_pos.y + m_size.y );
  75. }
  76. void SCH_BUS_ENTRY_BASE::SwapData( SCH_ITEM* aItem )
  77. {
  78. SCH_BUS_ENTRY_BASE* item = dynamic_cast<SCH_BUS_ENTRY_BASE*>( aItem );
  79. wxCHECK_RET( item, wxT( "Cannot swap bus entry data with invalid item." ) );
  80. std::swap( m_pos, item->m_pos );
  81. std::swap( m_size, item->m_size );
  82. }
  83. const EDA_RECT SCH_BUS_ENTRY_BASE::GetBoundingBox() const
  84. {
  85. EDA_RECT box;
  86. box.SetOrigin( m_pos );
  87. box.SetEnd( m_End() );
  88. box.Normalize();
  89. box.Inflate( GetPenSize() / 2 );
  90. return box;
  91. }
  92. int SCH_BUS_WIRE_ENTRY::GetPenSize() const
  93. {
  94. return GetDefaultLineThickness();
  95. }
  96. int SCH_BUS_BUS_ENTRY::GetPenSize() const
  97. {
  98. return GetDefaultBusThickness();
  99. }
  100. void SCH_BUS_WIRE_ENTRY::GetEndPoints( std::vector< DANGLING_END_ITEM >& aItemList )
  101. {
  102. DANGLING_END_ITEM item( WIRE_ENTRY_END, this, m_pos );
  103. aItemList.push_back( item );
  104. DANGLING_END_ITEM item1( WIRE_ENTRY_END, this, m_End() );
  105. aItemList.push_back( item1 );
  106. }
  107. void SCH_BUS_BUS_ENTRY::GetEndPoints( std::vector< DANGLING_END_ITEM >& aItemList )
  108. {
  109. DANGLING_END_ITEM item( BUS_ENTRY_END, this, m_pos );
  110. aItemList.push_back( item );
  111. DANGLING_END_ITEM item1( BUS_ENTRY_END, this, m_End() );
  112. aItemList.push_back( item1 );
  113. }
  114. void SCH_BUS_ENTRY_BASE::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
  115. GR_DRAWMODE aDrawMode, COLOR4D aColor )
  116. {
  117. COLOR4D color;
  118. EDA_RECT* clipbox = aPanel->GetClipBox();
  119. if( aColor != COLOR4D::UNSPECIFIED )
  120. color = aColor;
  121. else
  122. color = GetLayerColor( GetState( BRIGHTENED ) ? LAYER_BRIGHTENED : m_Layer );
  123. GRSetDrawMode( aDC, aDrawMode );
  124. GRLine( clipbox, aDC, m_pos.x + aOffset.x, m_pos.y + aOffset.y,
  125. m_End().x + aOffset.x, m_End().y + aOffset.y, GetPenSize(), color );
  126. // Draw pin targets if part is being dragged
  127. bool dragging = aPanel->GetScreen()->GetCurItem() == this && aPanel->IsMouseCaptured();
  128. if( m_isDanglingStart || dragging )
  129. {
  130. GRCircle( clipbox, aDC, m_pos.x + aOffset.x, m_pos.y + aOffset.y,
  131. TARGET_BUSENTRY_RADIUS, 0, color );
  132. }
  133. if( m_isDanglingEnd || dragging )
  134. {
  135. GRCircle( clipbox, aDC, m_End().x + aOffset.x, m_End().y + aOffset.y,
  136. TARGET_BUSENTRY_RADIUS, 0, color );
  137. }
  138. }
  139. void SCH_BUS_ENTRY_BASE::MirrorX( int aXaxis_position )
  140. {
  141. MIRROR( m_pos.y, aXaxis_position );
  142. m_size.y = -m_size.y;
  143. }
  144. void SCH_BUS_ENTRY_BASE::MirrorY( int aYaxis_position )
  145. {
  146. MIRROR( m_pos.x, aYaxis_position );
  147. m_size.x = -m_size.x;
  148. }
  149. void SCH_BUS_ENTRY_BASE::Rotate( wxPoint aPosition )
  150. {
  151. RotatePoint( &m_pos, aPosition, 900 );
  152. RotatePoint( &m_size.x, &m_size.y, 900 );
  153. }
  154. bool SCH_BUS_ENTRY_BASE::IsDanglingStateChanged( std::vector<DANGLING_END_ITEM>& aItemList )
  155. {
  156. bool previousStateStart = m_isDanglingStart;
  157. bool previousStateEnd = m_isDanglingEnd;
  158. m_isDanglingStart = m_isDanglingEnd = true;
  159. // Wires and buses are stored in the list as a pair, start and end. This
  160. // variable holds the start position from one iteration so it can be used
  161. // when the end position is found.
  162. wxPoint seg_start;
  163. // Special case: if both items are wires, show as dangling. This is because
  164. // a bus entry between two wires will look like a connection, but does NOT
  165. // actually represent one. We need to clarify this for the user.
  166. bool start_is_wire = false;
  167. bool end_is_wire = false;
  168. for( DANGLING_END_ITEM& each_item : aItemList )
  169. {
  170. if( each_item.GetItem() == this )
  171. continue;
  172. switch( each_item.GetType() )
  173. {
  174. case WIRE_START_END:
  175. case BUS_START_END:
  176. seg_start = each_item.GetPosition();
  177. break;
  178. case WIRE_END_END:
  179. if( IsPointOnSegment( seg_start, each_item.GetPosition(), m_pos ) )
  180. start_is_wire = true;
  181. if( IsPointOnSegment( seg_start, each_item.GetPosition(), m_End() ) )
  182. end_is_wire = true;
  183. // Fall through
  184. case BUS_END_END:
  185. if( IsPointOnSegment( seg_start, each_item.GetPosition(), m_pos ) )
  186. m_isDanglingStart = false;
  187. if( IsPointOnSegment( seg_start, each_item.GetPosition(), m_End() ) )
  188. m_isDanglingEnd = false;
  189. break;
  190. default:
  191. break;
  192. }
  193. }
  194. // See above: show as dangling if joining two wires
  195. if( start_is_wire && end_is_wire )
  196. m_isDanglingStart = m_isDanglingEnd = true;
  197. return (previousStateStart != m_isDanglingStart) || (previousStateEnd != m_isDanglingEnd);
  198. }
  199. bool SCH_BUS_ENTRY_BASE::IsDangling() const
  200. {
  201. return m_isDanglingStart || m_isDanglingEnd;
  202. }
  203. bool SCH_BUS_ENTRY_BASE::IsSelectStateChanged( const wxRect& aRect )
  204. {
  205. bool previousState = IsSelected();
  206. // If either end of the bus entry is inside the selection rectangle, the entire
  207. // bus entry is selected. Bus entries have a fixed length and angle.
  208. if( aRect.Contains( m_pos ) || aRect.Contains( m_End() ) )
  209. SetFlags( SELECTED );
  210. else
  211. ClearFlags( SELECTED );
  212. return previousState != IsSelected();
  213. }
  214. void SCH_BUS_ENTRY_BASE::GetConnectionPoints( std::vector< wxPoint >& aPoints ) const
  215. {
  216. aPoints.push_back( m_pos );
  217. aPoints.push_back( m_End() );
  218. }
  219. wxString SCH_BUS_WIRE_ENTRY::GetSelectMenuText() const
  220. {
  221. return wxString( _( "Bus to Wire Entry" ) );
  222. }
  223. wxString SCH_BUS_BUS_ENTRY::GetSelectMenuText() const
  224. {
  225. return wxString( _( "Bus to Bus Entry" ) );
  226. }
  227. BITMAP_DEF SCH_BUS_WIRE_ENTRY::GetMenuImage() const
  228. {
  229. return add_line2bus_xpm;
  230. }
  231. BITMAP_DEF SCH_BUS_BUS_ENTRY::GetMenuImage() const
  232. {
  233. return add_bus2bus_xpm;
  234. }
  235. bool SCH_BUS_ENTRY_BASE::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  236. {
  237. return TestSegmentHit( aPosition, m_pos, m_End(), aAccuracy );
  238. }
  239. bool SCH_BUS_ENTRY_BASE::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  240. {
  241. EDA_RECT rect = aRect;
  242. rect.Inflate( aAccuracy );
  243. if( aContained )
  244. return rect.Contains( GetBoundingBox() );
  245. return rect.Intersects( GetBoundingBox() );
  246. }
  247. void SCH_BUS_ENTRY_BASE::Plot( PLOTTER* aPlotter )
  248. {
  249. aPlotter->SetCurrentLineWidth( GetPenSize() );
  250. aPlotter->SetColor( GetLayerColor( GetLayer() ) );
  251. aPlotter->MoveTo( m_pos );
  252. aPlotter->FinishTo( m_End() );
  253. }
  254. void SCH_BUS_ENTRY_BASE::SetBusEntryShape( char aShape )
  255. {
  256. switch( aShape )
  257. {
  258. case '\\':
  259. if( m_size.y < 0 )
  260. m_size.y = -m_size.y;
  261. break;
  262. case '/':
  263. if( m_size.y > 0 )
  264. m_size.y = -m_size.y;
  265. break;
  266. }
  267. }
  268. char SCH_BUS_ENTRY_BASE::GetBusEntryShape() const
  269. {
  270. if( GetSize().y < 0 )
  271. return '/';
  272. else
  273. return '\\';
  274. }