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.

292 lines
7.7 KiB

5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2004-2021 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. #include <eda_draw_frame.h>
  25. #include <sch_draw_panel.h>
  26. #include <plotters/plotter.h>
  27. #include <trigo.h>
  28. #include <base_units.h>
  29. #include <widgets/msgpanel.h>
  30. #include <bitmaps.h>
  31. #include <general.h>
  32. #include <lib_rectangle.h>
  33. #include <settings/color_settings.h>
  34. #include <transform.h>
  35. LIB_RECTANGLE::LIB_RECTANGLE( LIB_SYMBOL* aParent ) : LIB_ITEM( LIB_RECTANGLE_T, aParent )
  36. {
  37. m_Width = 0;
  38. m_fill = FILL_TYPE::NO_FILL;
  39. m_isFillable = true;
  40. }
  41. EDA_ITEM* LIB_RECTANGLE::Clone() const
  42. {
  43. return new LIB_RECTANGLE( *this );
  44. }
  45. int LIB_RECTANGLE::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
  46. {
  47. wxASSERT( aOther.Type() == LIB_RECTANGLE_T );
  48. int retv = LIB_ITEM::compare( aOther );
  49. if( retv )
  50. return retv;
  51. const LIB_RECTANGLE* tmp = ( LIB_RECTANGLE* ) &aOther;
  52. if( m_Pos.x != tmp->m_Pos.x )
  53. return m_Pos.x - tmp->m_Pos.x;
  54. if( m_Pos.y != tmp->m_Pos.y )
  55. return m_Pos.y - tmp->m_Pos.y;
  56. if( m_End.x != tmp->m_End.x )
  57. return m_End.x - tmp->m_End.x;
  58. if( m_End.y != tmp->m_End.y )
  59. return m_End.y - tmp->m_End.y;
  60. return 0;
  61. }
  62. void LIB_RECTANGLE::Offset( const wxPoint& aOffset )
  63. {
  64. m_Pos += aOffset;
  65. m_End += aOffset;
  66. }
  67. void LIB_RECTANGLE::MoveTo( const wxPoint& aPosition )
  68. {
  69. wxPoint size = m_End - m_Pos;
  70. m_Pos = aPosition;
  71. m_End = aPosition + size;
  72. }
  73. void LIB_RECTANGLE::MirrorHorizontal( const wxPoint& aCenter )
  74. {
  75. m_Pos.x -= aCenter.x;
  76. m_Pos.x *= -1;
  77. m_Pos.x += aCenter.x;
  78. m_End.x -= aCenter.x;
  79. m_End.x *= -1;
  80. m_End.x += aCenter.x;
  81. }
  82. void LIB_RECTANGLE::MirrorVertical( const wxPoint& aCenter )
  83. {
  84. m_Pos.y -= aCenter.y;
  85. m_Pos.y *= -1;
  86. m_Pos.y += aCenter.y;
  87. m_End.y -= aCenter.y;
  88. m_End.y *= -1;
  89. m_End.y += aCenter.y;
  90. }
  91. void LIB_RECTANGLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
  92. {
  93. int rot_angle = aRotateCCW ? -900 : 900;
  94. RotatePoint( &m_Pos, aCenter, rot_angle );
  95. RotatePoint( &m_End, aCenter, rot_angle );
  96. }
  97. void LIB_RECTANGLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
  98. const TRANSFORM& aTransform ) const
  99. {
  100. wxASSERT( aPlotter != nullptr );
  101. wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
  102. wxPoint end = aTransform.TransformCoordinate( m_End ) + aOffset;
  103. if( aFill && m_fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR )
  104. {
  105. aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE_BACKGROUND ) );
  106. aPlotter->Rect( pos, end, FILL_TYPE::FILLED_WITH_BG_BODYCOLOR, 0 );
  107. }
  108. bool already_filled = m_fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR;
  109. int pen_size = GetEffectivePenWidth( aPlotter->RenderSettings() );
  110. if( !already_filled || pen_size > 0 )
  111. {
  112. aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE ) );
  113. aPlotter->Rect( pos, end, already_filled ? FILL_TYPE::NO_FILL : m_fill, pen_size );
  114. }
  115. }
  116. int LIB_RECTANGLE::GetPenWidth() const
  117. {
  118. return m_Width;
  119. }
  120. void LIB_RECTANGLE::print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset,
  121. void* aData, const TRANSFORM& aTransform )
  122. {
  123. bool forceNoFill = static_cast<bool>( aData );
  124. int penWidth = GetEffectivePenWidth( aSettings );
  125. if( forceNoFill && m_fill != FILL_TYPE::NO_FILL && penWidth == 0 )
  126. return;
  127. wxDC* DC = aSettings->GetPrintDC();
  128. COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
  129. wxPoint pt1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
  130. wxPoint pt2 = aTransform.TransformCoordinate( m_End ) + aOffset;
  131. if( forceNoFill || m_fill == FILL_TYPE::NO_FILL )
  132. {
  133. GRRect( nullptr, DC, pt1.x, pt1.y, pt2.x, pt2.y, penWidth, color );
  134. }
  135. else
  136. {
  137. if( m_fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR )
  138. color = aSettings->GetLayerColor( LAYER_DEVICE_BACKGROUND );
  139. GRFilledRect( nullptr, DC, pt1.x, pt1.y, pt2.x, pt2.y, penWidth, color, color );
  140. }
  141. }
  142. void LIB_RECTANGLE::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  143. {
  144. LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
  145. aList.emplace_back( _( "Line Width" ), MessageTextFromValue( aFrame->GetUserUnits(), m_Width ) );
  146. }
  147. const EDA_RECT LIB_RECTANGLE::GetBoundingBox() const
  148. {
  149. EDA_RECT rect;
  150. rect.SetOrigin( m_Pos );
  151. rect.SetEnd( m_End );
  152. rect.Inflate( ( GetPenWidth() / 2 ) + 1 );
  153. rect.RevertYAxis();
  154. return rect;
  155. }
  156. bool LIB_RECTANGLE::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  157. {
  158. int mindist = std::max( aAccuracy + GetPenWidth() / 2,
  159. Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
  160. wxPoint actualStart = DefaultTransform.TransformCoordinate( m_Pos );
  161. wxPoint actualEnd = DefaultTransform.TransformCoordinate( m_End );
  162. // locate lower segment
  163. wxPoint start, end;
  164. start = actualStart;
  165. end.x = actualEnd.x;
  166. end.y = actualStart.y;
  167. if( TestSegmentHit( aPosition, start, end, mindist ) )
  168. return true;
  169. // locate right segment
  170. start.x = actualEnd.x;
  171. end.y = actualEnd.y;
  172. if( TestSegmentHit( aPosition, start, end, mindist ) )
  173. return true;
  174. // locate upper segment
  175. start.y = actualEnd.y;
  176. end.x = actualStart.x;
  177. if( TestSegmentHit( aPosition, start, end, mindist ) )
  178. return true;
  179. // locate left segment
  180. start = actualStart;
  181. end.x = actualStart.x;
  182. end.y = actualEnd.y;
  183. if( TestSegmentHit( aPosition, start, end, mindist ) )
  184. return true;
  185. if( m_fill == FILL_TYPE::NO_FILL )
  186. return false;
  187. return GetBoundingBox().Contains( aPosition );
  188. }
  189. bool LIB_RECTANGLE::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  190. {
  191. if( m_flags & (STRUCT_DELETED | SKIP_STRUCT ) )
  192. return false;
  193. EDA_RECT sel = aRect;
  194. if ( aAccuracy )
  195. sel.Inflate( aAccuracy );
  196. if( aContained )
  197. return sel.Contains( GetBoundingBox() );
  198. return sel.Intersects( GetBoundingBox() );
  199. }
  200. wxString LIB_RECTANGLE::GetSelectMenuText( EDA_UNITS aUnits ) const
  201. {
  202. return wxString::Format( _( "Rectangle, width %s height %s" ),
  203. MessageTextFromValue( aUnits, std::abs( m_Pos.x - m_End.x ) ),
  204. MessageTextFromValue( aUnits, std::abs( m_Pos.y - m_End.y ) ) );
  205. }
  206. BITMAPS LIB_RECTANGLE::GetMenuImage() const
  207. {
  208. return BITMAPS::add_rectangle;
  209. }
  210. void LIB_RECTANGLE::BeginEdit( const wxPoint& aPosition )
  211. {
  212. m_Pos = m_End = aPosition;
  213. }
  214. void LIB_RECTANGLE::CalcEdit( const wxPoint& aPosition )
  215. {
  216. m_End = aPosition;
  217. }