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.

410 lines
10 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 <sch_draw_panel.h>
  32. #include <trigo.h>
  33. #include <common.h>
  34. #include <richio.h>
  35. #include <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_WIRE_ENTRY::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. // Store the connection type and state for the start (0) and end (1)
  164. bool has_wire[2] = { false };
  165. bool has_bus[2] = { false };
  166. for( DANGLING_END_ITEM& each_item : aItemList )
  167. {
  168. if( each_item.GetItem() == this )
  169. continue;
  170. switch( each_item.GetType() )
  171. {
  172. case WIRE_START_END:
  173. case BUS_START_END:
  174. seg_start = each_item.GetPosition();
  175. break;
  176. case WIRE_END_END:
  177. if( IsPointOnSegment( seg_start, each_item.GetPosition(), m_pos ) )
  178. has_wire[0] = true;
  179. if( IsPointOnSegment( seg_start, each_item.GetPosition(), m_End() ) )
  180. has_wire[1] = true;
  181. break;
  182. case BUS_END_END:
  183. if( IsPointOnSegment( seg_start, each_item.GetPosition(), m_pos ) )
  184. has_bus[0] = true;
  185. if( IsPointOnSegment( seg_start, each_item.GetPosition(), m_End() ) )
  186. has_bus[1] = true;
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. /**
  193. * A bus-wire entry is connected at both ends if it has a bus and a wire on its
  194. * ends. Otherwise, we connect only one end (in the case of a wire-wire or bus-bus)
  195. */
  196. if( ( has_wire[0] && has_bus[1] ) || ( has_wire[1] && has_bus[0] ) )
  197. m_isDanglingEnd = m_isDanglingStart = false;
  198. else if( has_wire[0] || has_bus[0] )
  199. m_isDanglingStart = false;
  200. else if( has_wire[1] || has_bus[1] )
  201. m_isDanglingEnd = false;
  202. return (previousStateStart != m_isDanglingStart) || (previousStateEnd != m_isDanglingEnd);
  203. }
  204. bool SCH_BUS_BUS_ENTRY::IsDanglingStateChanged( std::vector<DANGLING_END_ITEM>& aItemList )
  205. {
  206. bool previousStateStart = m_isDanglingStart;
  207. bool previousStateEnd = m_isDanglingEnd;
  208. m_isDanglingStart = m_isDanglingEnd = true;
  209. // Wires and buses are stored in the list as a pair, start and end. This
  210. // variable holds the start position from one iteration so it can be used
  211. // when the end position is found.
  212. wxPoint seg_start;
  213. for( DANGLING_END_ITEM& each_item : aItemList )
  214. {
  215. if( each_item.GetItem() == this )
  216. continue;
  217. switch( each_item.GetType() )
  218. {
  219. case BUS_START_END:
  220. seg_start = each_item.GetPosition();
  221. break;
  222. case BUS_END_END:
  223. if( IsPointOnSegment( seg_start, each_item.GetPosition(), m_pos ) )
  224. m_isDanglingStart = false;
  225. if( IsPointOnSegment( seg_start, each_item.GetPosition(), m_End() ) )
  226. m_isDanglingEnd = false;
  227. break;
  228. default:
  229. break;
  230. }
  231. }
  232. return (previousStateStart != m_isDanglingStart) || (previousStateEnd != m_isDanglingEnd);
  233. }
  234. bool SCH_BUS_ENTRY_BASE::IsDangling() const
  235. {
  236. return m_isDanglingStart || m_isDanglingEnd;
  237. }
  238. bool SCH_BUS_ENTRY_BASE::IsSelectStateChanged( const wxRect& aRect )
  239. {
  240. bool previousState = IsSelected();
  241. // If either end of the bus entry is inside the selection rectangle, the entire
  242. // bus entry is selected. Bus entries have a fixed length and angle.
  243. if( aRect.Contains( m_pos ) || aRect.Contains( m_End() ) )
  244. SetFlags( SELECTED );
  245. else
  246. ClearFlags( SELECTED );
  247. return previousState != IsSelected();
  248. }
  249. void SCH_BUS_ENTRY_BASE::GetConnectionPoints( std::vector< wxPoint >& aPoints ) const
  250. {
  251. aPoints.push_back( m_pos );
  252. aPoints.push_back( m_End() );
  253. }
  254. wxString SCH_BUS_WIRE_ENTRY::GetSelectMenuText( EDA_UNITS_T aUnits ) const
  255. {
  256. return wxString( _( "Bus to Wire Entry" ) );
  257. }
  258. wxString SCH_BUS_BUS_ENTRY::GetSelectMenuText( EDA_UNITS_T aUnits ) const
  259. {
  260. return wxString( _( "Bus to Bus Entry" ) );
  261. }
  262. BITMAP_DEF SCH_BUS_WIRE_ENTRY::GetMenuImage() const
  263. {
  264. return add_line2bus_xpm;
  265. }
  266. BITMAP_DEF SCH_BUS_BUS_ENTRY::GetMenuImage() const
  267. {
  268. return add_bus2bus_xpm;
  269. }
  270. bool SCH_BUS_ENTRY_BASE::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  271. {
  272. return TestSegmentHit( aPosition, m_pos, m_End(), aAccuracy );
  273. }
  274. bool SCH_BUS_ENTRY_BASE::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  275. {
  276. EDA_RECT rect = aRect;
  277. rect.Inflate( aAccuracy );
  278. if( aContained )
  279. return rect.Contains( GetBoundingBox() );
  280. return rect.Intersects( GetBoundingBox() );
  281. }
  282. void SCH_BUS_ENTRY_BASE::Plot( PLOTTER* aPlotter )
  283. {
  284. aPlotter->SetCurrentLineWidth( GetPenSize() );
  285. aPlotter->SetColor( GetLayerColor( GetLayer() ) );
  286. aPlotter->MoveTo( m_pos );
  287. aPlotter->FinishTo( m_End() );
  288. }
  289. void SCH_BUS_ENTRY_BASE::SetBusEntryShape( char aShape )
  290. {
  291. switch( aShape )
  292. {
  293. case '\\':
  294. if( m_size.y < 0 )
  295. m_size.y = -m_size.y;
  296. break;
  297. case '/':
  298. if( m_size.y > 0 )
  299. m_size.y = -m_size.y;
  300. break;
  301. }
  302. }
  303. char SCH_BUS_ENTRY_BASE::GetBusEntryShape() const
  304. {
  305. if( GetSize().y < 0 )
  306. return '/';
  307. else
  308. return '\\';
  309. }