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.

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