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.

97 lines
3.2 KiB

5 years ago
5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 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/two_point_assistant.h>
  24. #include <preview_items/draw_context.h>
  25. #include <preview_items/preview_utils.h>
  26. #include <view/view.h>
  27. using namespace KIGFX::PREVIEW;
  28. TWO_POINT_ASSISTANT::TWO_POINT_ASSISTANT( const TWO_POINT_GEOMETRY_MANAGER& aManager,
  29. EDA_UNITS aUnits, GEOM_SHAPE aShape ) :
  30. EDA_ITEM( NOT_USED ),
  31. m_constructMan( aManager ),
  32. m_units( aUnits ),
  33. m_shape( aShape )
  34. {
  35. }
  36. const BOX2I TWO_POINT_ASSISTANT::ViewBBox() const
  37. {
  38. BOX2I tmp;
  39. // no bounding box when no graphic shown
  40. if( m_constructMan.IsReset() )
  41. return tmp;
  42. // this is an edit-time artefact; no reason to try and be smart with the bounding box
  43. // (besides, we can't tell the text extents without a view to know what the scale is)
  44. tmp.SetMaximum();
  45. return tmp;
  46. }
  47. void TWO_POINT_ASSISTANT::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
  48. {
  49. KIGFX::GAL& gal = *aView->GetGAL();
  50. // not in a position to draw anything
  51. if( m_constructMan.IsReset() )
  52. return;
  53. const VECTOR2I origin = m_constructMan.GetOrigin();
  54. const VECTOR2I end = m_constructMan.GetEnd();
  55. const VECTOR2I radVec = end - origin;
  56. if( radVec.x == 0 && radVec.y == 0 )
  57. {
  58. return; // text next to cursor jumps around a lot in this corner case
  59. }
  60. gal.ResetTextAttributes();
  61. std::vector<wxString> cursorStrings;
  62. if( m_shape == GEOM_SHAPE::SEGMENT )
  63. {
  64. cursorStrings.push_back( DimensionLabel( "l", radVec.EuclideanNorm(), m_units ) );
  65. }
  66. else if( m_shape == GEOM_SHAPE::RECT )
  67. {
  68. cursorStrings.push_back( DimensionLabel( "x", std::abs( radVec.x ), m_units ) );
  69. cursorStrings.push_back( DimensionLabel( "y", std::abs( radVec.y ), m_units ) );
  70. }
  71. else if( m_shape == GEOM_SHAPE::CIRCLE )
  72. {
  73. KIGFX::PREVIEW::DRAW_CONTEXT preview_ctx( *aView );
  74. preview_ctx.DrawLine( origin, end, false );
  75. cursorStrings.push_back( DimensionLabel( "r", radVec.EuclideanNorm(), m_units ) );
  76. }
  77. // place the text next to cursor, on opposite side from drawing
  78. DrawTextNextToCursor( aView, end, origin - end, cursorStrings, aLayer == LAYER_SELECT_OVERLAY );
  79. }