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.

189 lines
5.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2021 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. #ifndef GRID_HELPER_H
  24. #define GRID_HELPER_H
  25. #include "tool/selection.h"
  26. #include <tool/tool_manager.h>
  27. #include <vector>
  28. #include <math/vector2d.h>
  29. #include <origin_viewitem.h>
  30. class TOOL_MANAGER;
  31. class EDA_ITEM;
  32. enum GRID_HELPER_GRIDS : int
  33. {
  34. // When the item doesn't match an override, use the current user grid
  35. GRID_CURRENT,
  36. GRID_CONNECTABLE,
  37. GRID_WIRES,
  38. GRID_VIAS,
  39. GRID_TEXT,
  40. GRID_GRAPHICS
  41. };
  42. class GRID_HELPER
  43. {
  44. public:
  45. GRID_HELPER( TOOL_MANAGER* aToolMgr );
  46. virtual ~GRID_HELPER();
  47. VECTOR2I GetGrid() const;
  48. VECTOR2D GetVisibleGrid() const;
  49. VECTOR2I GetOrigin() const;
  50. void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) );
  51. virtual VECTOR2I Align( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
  52. {
  53. return Align( aPoint, GetGridSize( aGrid ), GetOrigin() );
  54. }
  55. virtual VECTOR2I AlignGrid( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
  56. {
  57. return AlignGrid( aPoint, GetGridSize( aGrid ), GetOrigin() );
  58. }
  59. virtual VECTOR2I Align( const VECTOR2I& aPoint ) const;
  60. virtual VECTOR2I Align( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
  61. const VECTOR2D& aOffset ) const;
  62. VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const;
  63. VECTOR2I AlignGrid( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
  64. const VECTOR2D& aOffset ) const;
  65. /**
  66. * Gets the coarsest grid that applies to a selecion of items.
  67. */
  68. virtual GRID_HELPER_GRIDS GetSelectionGrid( const SELECTION& aSelection ) const;
  69. /**
  70. * Gets the coarsest grid that applies to an item.
  71. */
  72. virtual GRID_HELPER_GRIDS GetItemGrid( const EDA_ITEM* aItem ) const { return GRID_CURRENT; }
  73. /**
  74. * Return the size of the specified grid
  75. */
  76. virtual VECTOR2D GetGridSize( GRID_HELPER_GRIDS aGrid ) const;
  77. void SetSkipPoint( const VECTOR2I& aPoint )
  78. {
  79. m_skipPoint = aPoint;
  80. }
  81. /**
  82. * We clear the skip point by setting it to an unreachable position, thereby preventing matching
  83. */
  84. void ClearSkipPoint()
  85. {
  86. m_skipPoint = VECTOR2I( std::numeric_limits<int>::min(), std::numeric_limits<int>::min() );
  87. }
  88. void SetSnap( bool aSnap ) { m_enableSnap = aSnap; }
  89. bool GetSnap() const { return m_enableSnap; }
  90. void SetUseGrid( bool aSnapToGrid ) { m_enableGrid = aSnapToGrid; }
  91. bool GetUseGrid() const { return m_enableGrid; }
  92. void SetSnapLine( bool aSnap ) { m_enableSnapLine = aSnap; }
  93. void SetMask( int aMask ) { m_maskTypes = aMask; }
  94. void SetMaskFlag( int aFlag ) { m_maskTypes |= aFlag; }
  95. void ClearMaskFlag( int aFlag ) { m_maskTypes = m_maskTypes & ~aFlag; }
  96. enum ANCHOR_FLAGS {
  97. CORNER = 1,
  98. OUTLINE = 2,
  99. SNAPPABLE = 4,
  100. ORIGIN = 8,
  101. VERTICAL = 16,
  102. HORIZONTAL = 32,
  103. ALL = CORNER | OUTLINE | SNAPPABLE | ORIGIN | VERTICAL | HORIZONTAL
  104. };
  105. protected:
  106. struct ANCHOR
  107. {
  108. ANCHOR( const VECTOR2I& aPos, int aFlags = CORNER | SNAPPABLE, EDA_ITEM* aItem = nullptr ) :
  109. pos( aPos ),
  110. flags( aFlags ),
  111. item( aItem )
  112. { };
  113. VECTOR2I pos;
  114. int flags;
  115. EDA_ITEM* item;
  116. double Distance( const VECTOR2I& aP ) const
  117. {
  118. return ( aP - pos ).EuclideanNorm();
  119. }
  120. };
  121. void addAnchor( const VECTOR2I& aPos, int aFlags, EDA_ITEM* aItem )
  122. {
  123. if( ( aFlags & m_maskTypes ) == aFlags )
  124. m_anchors.emplace_back( ANCHOR( aPos, aFlags, aItem ) );
  125. }
  126. void clearAnchors()
  127. {
  128. m_anchors.clear();
  129. m_snapItem = nullptr;
  130. }
  131. /**
  132. * Check whether it is possible to use the grid -- this depends both on local grid helper
  133. * settings and global (tool manager) KiCad settings.
  134. */
  135. bool canUseGrid() const;
  136. VECTOR2I computeNearest( const VECTOR2I& aPoint, const VECTOR2I& aGrid,
  137. const VECTOR2I& aOffset ) const;
  138. protected:
  139. std::vector<ANCHOR> m_anchors;
  140. TOOL_MANAGER* m_toolMgr;
  141. std::optional<VECTOR2I> m_auxAxis;
  142. int m_maskTypes; // Mask of allowed snap types
  143. bool m_enableSnap; // Allow snapping to other items on the layers
  144. bool m_enableGrid; // If true, allow snapping to grid
  145. bool m_enableSnapLine; // Allow drawing lines from snap points
  146. ANCHOR* m_snapItem; // Pointer to the currently snapped item in m_anchors
  147. // (NULL if not snapped)
  148. VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the
  149. // source point
  150. KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint;
  151. KIGFX::ORIGIN_VIEWITEM m_viewSnapLine;
  152. KIGFX::ORIGIN_VIEWITEM m_viewAxis;
  153. };
  154. #endif