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.

111 lines
4.1 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 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 <class_drawpanel.h>
  25. #include <draw_frame.h>
  26. #include <origin_viewitem.h>
  27. #include <gal/graphics_abstraction_layer.h>
  28. using namespace KIGFX;
  29. ORIGIN_VIEWITEM::ORIGIN_VIEWITEM( const COLOR4D& aColor, MARKER_STYLE aStyle, int aSize, const VECTOR2D& aPosition ) :
  30. BOARD_ITEM( nullptr, NOT_USED ), // this item is never added to a BOARD so it needs no type
  31. m_position( aPosition ), m_size( aSize ), m_color( aColor ), m_style( aStyle ), m_drawAtZero( false )
  32. {
  33. }
  34. ORIGIN_VIEWITEM::ORIGIN_VIEWITEM( const VECTOR2D& aPosition, STATUS_FLAGS flags ) :
  35. BOARD_ITEM( nullptr, NOT_USED ), // this item is never added to a BOARD so it needs no type
  36. m_position( aPosition ), m_size( NOT_USED ), m_color( UNSPECIFIED_COLOR ), m_style( NONE ), m_drawAtZero( false )
  37. {
  38. SetFlags( flags );
  39. }
  40. ORIGIN_VIEWITEM* ORIGIN_VIEWITEM::Clone() const
  41. {
  42. return new ORIGIN_VIEWITEM( m_color, m_style, m_size, m_position );
  43. }
  44. const BOX2I ORIGIN_VIEWITEM::ViewBBox() const
  45. {
  46. BOX2I bbox;
  47. // The origin item doesn't have a fixed size. It is constant on the screen but
  48. // changes the effective BBox size based on the zoom level.
  49. // But we can't simply set it to the maximum size as this causes a splitting degeneracy
  50. // when compiling for Debian i386. By modestly adjusting the bbox, we avoid the degeneracy
  51. // while keeping the origin visible at all zoom levels
  52. bbox.SetSize( VECTOR2I( INT_MAX - 2, INT_MAX - 2 ) );
  53. bbox.SetOrigin( VECTOR2I( INT_MIN / 2 + 1, INT_MIN / 2 + 1 ) );
  54. return bbox;
  55. }
  56. void ORIGIN_VIEWITEM::ViewDraw( int, VIEW* aView ) const
  57. {
  58. auto gal = aView->GetGAL();
  59. // Nothing to do if the target shouldn't be drawn at 0,0 and that's where the target is. This
  60. // mimics the Legacy canvas that doesn't display most targets at 0,0
  61. if( !m_drawAtZero && ( m_position.x == 0 ) && ( m_position.y == 0 ) )
  62. return;
  63. gal->SetIsStroke( true );
  64. gal->SetIsFill( false );
  65. gal->SetLineWidth( 1 );
  66. gal->SetStrokeColor( m_color );
  67. VECTOR2D scaledSize = aView->ToWorld( VECTOR2D( m_size, m_size ), false );
  68. // Draw a circle around the marker's centre point if the style demands it
  69. if( ( m_style == CIRCLE_CROSS ) || ( m_style == CIRCLE_DOT ) || ( m_style == CIRCLE_X ) )
  70. gal->DrawCircle( m_position, fabs( scaledSize.x ) );
  71. switch( m_style )
  72. {
  73. case NONE:
  74. break;
  75. case CROSS:
  76. case CIRCLE_CROSS:
  77. gal->DrawLine( m_position - VECTOR2D( scaledSize.x, 0 ),
  78. m_position + VECTOR2D( scaledSize.x, 0 ) );
  79. gal->DrawLine( m_position - VECTOR2D( 0, scaledSize.y ),
  80. m_position + VECTOR2D( 0, scaledSize.y ) );
  81. break;
  82. case X:
  83. case CIRCLE_X:
  84. gal->DrawLine( m_position - scaledSize, m_position + scaledSize );
  85. scaledSize.y = -scaledSize.y;
  86. gal->DrawLine( m_position - scaledSize, m_position + scaledSize );
  87. break;
  88. case DOT:
  89. case CIRCLE_DOT:
  90. gal->DrawCircle( m_position, scaledSize.x / 4 );
  91. break;
  92. }
  93. }