KiCad PCB EDA Suite
placement_tool.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2014 CERN
5  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Maciej Suminski <[email protected]>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #ifndef ALIGN_DISTRIBUTE_TOOL_H_
28 #define ALIGN_DISTRIBUTE_TOOL_H_
29 
30 #include <tool/tool_interactive.h>
31 #include <tools/pcb_selection.h>
32 #include <board_item.h>
33 #include <pcb_base_frame.h>
34 
35 using ALIGNMENT_RECT = std::pair<BOARD_ITEM*, EDA_RECT>;
36 using ALIGNMENT_RECTS = std::vector<ALIGNMENT_RECT>;
37 
38 class PCB_SELECTION_TOOL;
39 
41 {
42 public:
44  virtual ~ALIGN_DISTRIBUTE_TOOL();
45 
47  void Reset( RESET_REASON aReason ) override {}
48 
50  bool Init() override;
51 
56  int AlignTop( const TOOL_EVENT& aEvent );
57 
62  int AlignBottom( const TOOL_EVENT& aEvent );
63 
68  int AlignLeft( const TOOL_EVENT& aEvent );
69 
74  int AlignRight( const TOOL_EVENT& aEvent );
75 
80  int AlignCenterX( const TOOL_EVENT& aEvent );
81 
86  int AlignCenterY( const TOOL_EVENT& aEvent );
87 
91  int DistributeHorizontally( const TOOL_EVENT& aEvent );
92 
96  int DistributeVertically( const TOOL_EVENT& aEvent );
97 
99  void setTransitions() override;
100 
101 private:
107  template< typename T >
108  size_t GetSelections( ALIGNMENT_RECTS& aItemsToAlign, ALIGNMENT_RECTS& aLockedItems,
109  T aCompare );
110 
111  template< typename T >
112  int selectTarget( ALIGNMENT_RECTS& aItems, ALIGNMENT_RECTS& aLocked, T aGetValue );
113 
121  int doAlignLeft();
122 
129  int doAlignRight();
130 
139  void doDistributeCentersHorizontally( ALIGNMENT_RECTS& itemsToDistribute,
140  BOARD_COMMIT& aCommit ) const;
141 
150  void doDistributeCentersVertically( ALIGNMENT_RECTS& itemsToDistribute,
151  BOARD_COMMIT& aCommit ) const;
152 
159  void doDistributeGapsHorizontally( ALIGNMENT_RECTS& itemsToDistribute, BOARD_COMMIT& aCommit,
160  const BOARD_ITEM* lastItem, int totalGap ) const;
161 
168  void doDistributeGapsVertically( ALIGNMENT_RECTS& itemsToDistribute, BOARD_COMMIT& aCommit,
169  const BOARD_ITEM* lastItem, int totalGap ) const;
170 
171 private:
175 };
176 
177 #endif /* ALIGN_DISTRIBUTE_TOOL_H_ */
int AlignLeft(const TOOL_EVENT &aEvent)
Sets X coordinate of the selected items to the value of the left-most selected item X coordinate.
int selectTarget(ALIGNMENT_RECTS &aItems, ALIGNMENT_RECTS &aLocked, T aGetValue)
int AlignCenterY(const TOOL_EVENT &aEvent)
Set the y coordinate of the midpoint of each of the selected items to the value of the y coordinate o...
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:48
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
PCB_SELECTION_TOOL * m_selectionTool
int AlignCenterX(const TOOL_EVENT &aEvent)
Set the x coordinate of the midpoint of each of the selected items to the value of the x coordinate o...
virtual ~ALIGN_DISTRIBUTE_TOOL()
ACTION_MENU * m_placementMenu
int AlignBottom(const TOOL_EVENT &aEvent)
Sets Y coordinate of the selected items to the value of the bottom-most selected item Y coordinate.
void doDistributeCentersHorizontally(ALIGNMENT_RECTS &itemsToDistribute, BOARD_COMMIT &aCommit) const
Distribute selected items using an even spacing between the centers of their bounding boxes.
Classes used in Pcbnew, CvPcb and GerbView.
int DistributeHorizontally(const TOOL_EVENT &aEvent)
Distribute the selected items along the X axis.
int doAlignRight()
Align selected items using the right edge of their bounding boxes to the right-most item.
void doDistributeGapsHorizontally(ALIGNMENT_RECTS &itemsToDistribute, BOARD_COMMIT &aCommit, const BOARD_ITEM *lastItem, int totalGap) const
Distributes selected items using an even spacing between their bounding boxes.
int doAlignLeft()
Sets X coordinate of the selected items to the value of the left-most selected item X coordinate.
std::vector< ALIGNMENT_RECT > ALIGNMENT_RECTS
int AlignTop(const TOOL_EVENT &aEvent)
Set Y coordinate of the selected items to the value of the top-most selected item Y coordinate.
Generic, UI-independent tool event.
Definition: tool_event.h:152
int DistributeVertically(const TOOL_EVENT &aEvent)
Distribute the selected items along the Y axis.
void doDistributeGapsVertically(ALIGNMENT_RECTS &itemsToDistribute, BOARD_COMMIT &aCommit, const BOARD_ITEM *lastItem, int totalGap) const
Distributes selected items using an even spacing between their bounding boxes.
void doDistributeCentersVertically(ALIGNMENT_RECTS &itemsToDistribute, BOARD_COMMIT &aCommit) const
Distribute selected items using an even spacing between the centers of their bounding boxes.
PCB_BASE_FRAME * m_frame
std::pair< BOARD_ITEM *, EDA_RECT > ALIGNMENT_RECT
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
The selection tool: currently supports:
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:77
int AlignRight(const TOOL_EVENT &aEvent)
Sets X coordinate of the selected items to the value of the right-most selected item X coordinate.
bool Init() override
Init() is called once upon a registration of the tool.
size_t GetSelections(ALIGNMENT_RECTS &aItemsToAlign, ALIGNMENT_RECTS &aLockedItems, T aCompare)
Populate two vectors with the sorted selection and sorted locked items.
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.