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.

264 lines
6.3 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014-2017 CERN
  5. * @author Maciej Suminski <maciej.suminski@cern.ch>
  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. #include <gal/graphics_abstraction_layer.h>
  25. #include "edit_points.h"
  26. bool EDIT_POINT::WithinPoint( const VECTOR2I& aPoint, unsigned int aSize ) const
  27. {
  28. // Corners of the EDIT_POINT square
  29. VECTOR2I topLeft = GetPosition() - aSize;
  30. VECTOR2I bottomRight = GetPosition() + aSize;
  31. return ( aPoint.x > topLeft.x && aPoint.y > topLeft.y &&
  32. aPoint.x < bottomRight.x && aPoint.y < bottomRight.y );
  33. }
  34. EDIT_POINTS::EDIT_POINTS( EDA_ITEM* aParent ) :
  35. EDA_ITEM( NOT_USED ), m_parent( aParent )
  36. {
  37. }
  38. EDIT_POINT* EDIT_POINTS::FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView ) // fixme: ugly
  39. {
  40. float size = aView->ToWorld( EDIT_POINT::POINT_SIZE );
  41. std::deque<EDIT_POINT>::iterator pit, pitEnd;
  42. for( pit = m_points.begin(), pitEnd = m_points.end(); pit != pitEnd; ++pit )
  43. {
  44. EDIT_POINT& point = *pit;
  45. if( point.WithinPoint( aLocation, size ) )
  46. return &point;
  47. }
  48. std::deque<EDIT_LINE>::iterator lit, litEnd;
  49. for( lit = m_lines.begin(), litEnd = m_lines.end(); lit != litEnd; ++lit )
  50. {
  51. EDIT_LINE& line = *lit;
  52. if( line.WithinPoint( aLocation, size ) )
  53. return &line;
  54. }
  55. return NULL;
  56. }
  57. int EDIT_POINTS::GetContourStartIdx( int aPointIdx ) const
  58. {
  59. int lastIdx = 0;
  60. for( int idx : m_contours )
  61. {
  62. if( idx >= aPointIdx )
  63. return lastIdx;
  64. lastIdx = idx + 1;
  65. }
  66. return lastIdx;
  67. }
  68. int EDIT_POINTS::GetContourEndIdx( int aPointIdx ) const
  69. {
  70. for( int idx : m_contours )
  71. {
  72. if( idx >= aPointIdx )
  73. return idx;
  74. }
  75. return m_points.size() - 1;
  76. }
  77. bool EDIT_POINTS::IsContourStart( int aPointIdx ) const
  78. {
  79. for( int idx : m_contours )
  80. {
  81. if( idx + 1 == aPointIdx )
  82. return true;
  83. // the list is sorted, so we cannot expect it any further
  84. if( idx > aPointIdx )
  85. break;
  86. }
  87. return ( aPointIdx == 0 );
  88. }
  89. bool EDIT_POINTS::IsContourEnd( int aPointIdx ) const
  90. {
  91. for( int idx : m_contours )
  92. {
  93. if( idx == aPointIdx )
  94. return true;
  95. // the list is sorted, so we cannot expect it any further
  96. if( idx > aPointIdx )
  97. break;
  98. }
  99. // the end of the list surely is the end of a contour
  100. return ( aPointIdx == (int) m_points.size() - 1 );
  101. }
  102. EDIT_POINT* EDIT_POINTS::Previous( const EDIT_POINT& aPoint, bool aTraverseContours )
  103. {
  104. for( unsigned int i = 0; i < m_points.size(); ++i )
  105. {
  106. if( m_points[i] == aPoint )
  107. {
  108. if( !aTraverseContours && IsContourStart( i ) )
  109. return &m_points[GetContourEndIdx( i )];
  110. if( i == 0 )
  111. return &m_points[m_points.size() - 1];
  112. else
  113. return &m_points[i - 1];
  114. }
  115. }
  116. return NULL;
  117. }
  118. EDIT_LINE* EDIT_POINTS::Previous( const EDIT_LINE& aLine )
  119. {
  120. for( unsigned int i = 0; i < m_lines.size(); ++i )
  121. {
  122. if( m_lines[i] == aLine )
  123. {
  124. if( i == 0 )
  125. return &m_lines[m_lines.size() - 1];
  126. else
  127. return &m_lines[i - 1];
  128. }
  129. }
  130. return NULL;
  131. }
  132. EDIT_POINT* EDIT_POINTS::Next( const EDIT_POINT& aPoint, bool aTraverseContours )
  133. {
  134. for( unsigned int i = 0; i < m_points.size(); ++i )
  135. {
  136. if( m_points[i] == aPoint )
  137. {
  138. if( !aTraverseContours && IsContourEnd( i ) )
  139. return &m_points[GetContourStartIdx( i )];
  140. if( i == m_points.size() - 1 )
  141. return &m_points[0];
  142. else
  143. return &m_points[i + 1];
  144. }
  145. }
  146. return NULL;
  147. }
  148. EDIT_LINE* EDIT_POINTS::Next( const EDIT_LINE& aLine )
  149. {
  150. for( unsigned int i = 0; i < m_lines.size(); ++i )
  151. {
  152. if( m_lines[i] == aLine )
  153. {
  154. if( i == m_lines.size() - 1 )
  155. return &m_lines[0];
  156. else
  157. return &m_lines[i + 1];
  158. }
  159. }
  160. return NULL;
  161. }
  162. const BOX2I EDIT_POINTS::ViewBBox() const
  163. {
  164. BOX2I box;
  165. bool empty = true;
  166. for( const auto& point : m_points )
  167. {
  168. if( empty )
  169. {
  170. box.SetOrigin( point.GetPosition() );
  171. empty = false;
  172. }
  173. else
  174. {
  175. box.Merge( point.GetPosition() );
  176. }
  177. }
  178. for( const auto& line : m_lines )
  179. {
  180. if( empty )
  181. {
  182. box.SetOrigin( line.GetOrigin().GetPosition() );
  183. box.SetEnd( line.GetEnd().GetPosition() );
  184. empty = false;
  185. }
  186. else
  187. {
  188. box.Merge( line.GetOrigin().GetPosition() );
  189. box.Merge( line.GetEnd().GetPosition() );
  190. }
  191. }
  192. return box;
  193. }
  194. void EDIT_POINTS::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
  195. {
  196. auto gal = aView->GetGAL();
  197. gal->SetFillColor( KIGFX::COLOR4D( 1.0, 1.0, 1.0, 1.0 ) );
  198. gal->SetIsFill( true );
  199. gal->SetIsStroke( false );
  200. gal->PushDepth();
  201. gal->SetLayerDepth( gal->GetMinDepth() );
  202. float size = aView->ToWorld( EDIT_POINT::POINT_SIZE );
  203. for( const EDIT_POINT& point : m_points )
  204. gal->DrawRectangle( point.GetPosition() - size / 2, point.GetPosition() + size / 2 );
  205. for( const EDIT_LINE& line : m_lines )
  206. {
  207. gal->DrawCircle( line.GetPosition(), size / 2 );
  208. }
  209. gal->PopDepth();
  210. }