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.

141 lines
4.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 Kicad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <preview_items/arc_assistant.h>
  24. #include <preview_items/draw_context.h>
  25. #include <preview_items/preview_utils.h>
  26. #include <gal/graphics_abstraction_layer.h>
  27. #include <view/view.h>
  28. #include <common.h>
  29. #include <base_units.h>
  30. #include <trigo.h>
  31. using namespace KIGFX::PREVIEW;
  32. ARC_ASSISTANT::ARC_ASSISTANT( const ARC_GEOM_MANAGER& aManager, EDA_UNITS aUnits ) :
  33. EDA_ITEM( NOT_USED ),
  34. m_constructMan( aManager ),
  35. m_units( aUnits )
  36. {
  37. }
  38. const BOX2I ARC_ASSISTANT::ViewBBox() const
  39. {
  40. BOX2I tmp;
  41. // no bounding box when no graphic shown
  42. if( m_constructMan.IsReset() )
  43. return tmp;
  44. // this is an edit-time artefact; no reason to try and be smart with the bounding box
  45. // (besides, we can't tell the text extents without a view to know what the scale is)
  46. tmp.SetMaximum();
  47. return tmp;
  48. }
  49. /**
  50. * Get deci-degrees from radians, normalised to +/- 360.
  51. *
  52. * The normalisation is such that a negative angle will stay
  53. * negative.
  54. */
  55. double getNormDeciDegFromRad( double aRadians )
  56. {
  57. double degs = RAD2DECIDEG( aRadians );
  58. // normalise to +/- 360
  59. while( degs < -3600.0 )
  60. degs += 3600.0;
  61. while( degs > 3600.0 )
  62. degs -= 3600.0;
  63. return degs;
  64. }
  65. void ARC_ASSISTANT::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
  66. {
  67. KIGFX::GAL& gal = *aView->GetGAL();
  68. // not in a position to draw anything
  69. if( m_constructMan.IsReset() )
  70. return;
  71. gal.ResetTextAttributes();
  72. const VECTOR2I origin = m_constructMan.GetOrigin();
  73. KIGFX::PREVIEW::DRAW_CONTEXT preview_ctx( *aView );
  74. // draw first radius line
  75. bool dimFirstLine = m_constructMan.GetStep() > ARC_GEOM_MANAGER::SET_START;
  76. preview_ctx.DrawLineWithAngleHighlight( origin, m_constructMan.GetStartRadiusEnd(),
  77. dimFirstLine );
  78. std::vector<wxString> cursorStrings;
  79. if( m_constructMan.GetStep() == ARC_GEOM_MANAGER::SET_START )
  80. {
  81. // haven't started the angle selection phase yet
  82. double initAngle = m_constructMan.GetStartAngle();
  83. // draw the radius guide circle
  84. preview_ctx.DrawCircle( origin, m_constructMan.GetRadius(), true );
  85. double degs = getNormDeciDegFromRad( initAngle );
  86. cursorStrings.push_back( DimensionLabel( "r", m_constructMan.GetRadius(), m_units ) );
  87. cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "θ" ), degs,
  88. EDA_UNITS::DEGREES ) );
  89. }
  90. else
  91. {
  92. preview_ctx.DrawLineWithAngleHighlight( origin, m_constructMan.GetEndRadiusEnd(), false );
  93. double start = m_constructMan.GetStartAngle();
  94. double subtended = m_constructMan.GetSubtended();
  95. double subtendedDeg = getNormDeciDegFromRad( subtended );
  96. double endAngleDeg = getNormDeciDegFromRad( start + subtended );
  97. // draw dimmed extender line to cursor
  98. preview_ctx.DrawLineWithAngleHighlight( origin, m_constructMan.GetLastPoint(), true );
  99. cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "Δθ" ), subtendedDeg,
  100. EDA_UNITS::DEGREES ) );
  101. cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "θ" ), endAngleDeg,
  102. EDA_UNITS::DEGREES ) );
  103. }
  104. // place the text next to cursor, on opposite side from radius
  105. DrawTextNextToCursor( aView, m_constructMan.GetLastPoint(),
  106. origin - m_constructMan.GetLastPoint(), cursorStrings,
  107. aLayer == LAYER_SELECT_OVERLAY );
  108. }