KiCad PCB EDA Suite
pcb_tool.cpp
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) 2017 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 
24 
25 #include "pcb_tool.h"
26 
27 #include <view/view_controls.h>
28 #include <view/view.h>
29 #include <tool/tool_manager.h>
30 #include <board_commit.h>
31 
32 #include <class_module.h>
33 #include <pcb_draw_panel_gal.h>
34 
35 #include "selection_tool.h"
36 #include "pcb_actions.h"
37 #include "tool_event_utils.h"
38 
40  const wxString& aCommitMessage,
41  int aOptions )
42 {
43  using namespace std::placeholders;
44  std::unique_ptr<BOARD_ITEM> newItem;
45 
46  Activate();
47 
48  BOARD_COMMIT commit( frame() );
49 
51 
52  // do not capture or auto-pan until we start placing an item
53  controls()->ShowCursor( true );
54  controls()->SetSnapping( true );
55 
56  // Add a VIEW_GROUP that serves as a preview for the new item
57  SELECTION preview;
58  view()->Add( &preview );
59 
60  aPlacer->m_board = board();
61  aPlacer->m_frame = frame();
62  aPlacer->m_modifiers = 0;
63 
64  if( aOptions & IPO_SINGLE_CLICK )
65  {
66  VECTOR2I cursorPos = controls()->GetCursorPosition();
67 
68  newItem = aPlacer->CreateItem();
69  newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
70 
71  preview.Add( newItem.get() );
72  }
73 
74  // Main loop: keep receiving events
75  while( OPT_TOOL_EVENT evt = Wait() )
76  {
77  VECTOR2I cursorPos = controls()->GetCursorPosition();
78  aPlacer->m_modifiers = evt->Modifier();
79 
81  {
82  if( newItem )
83  {
84  // Delete the old item and have another try
85  newItem = nullptr;
86 
87  preview.Clear();
88 
89  if( aOptions & IPO_SINGLE_CLICK )
90  break;
91 
92  controls()->SetAutoPan( false );
93  controls()->CaptureCursor( false );
94  controls()->ShowCursor( true );
95  }
96  else
97  {
98  break;
99  }
100 
101  if( evt->IsActivate() ) // now finish unconditionally
102  break;
103  }
104 
105  else if( evt->IsClick( BUT_LEFT ) )
106  {
107  if( !newItem )
108  {
109  // create the item if possible
110  newItem = aPlacer->CreateItem();
111 
112  // no item created, so wait for another click
113  if( !newItem )
114  continue;
115 
116  controls()->CaptureCursor( true );
117  controls()->SetAutoPan( true );
118 
119  newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
120 
121  preview.Add( newItem.get() );
122 
123  if( newItem->Type() == PCB_MODULE_T )
124  {
125  auto module = dyn_cast<MODULE*>( newItem.get() );
126 
127  // modules have more drawable parts
128  module->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Add, &preview, _1 ) );
129  }
130  }
131  else
132  {
133  newItem->ClearFlags();
134  preview.Remove( newItem.get() );
135 
136  aPlacer->PlaceItem( newItem.get(), commit );
137 
138  if( newItem->Type() == PCB_MODULE_T )
139  {
140  auto module = dyn_cast<MODULE*>( newItem.get() );
141  module->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Remove, &preview, _1 ) );
142  }
143 
144  newItem.release();
145  commit.Push( aCommitMessage );
146 
147  controls()->CaptureCursor( false );
148  controls()->SetAutoPan( false );
149  controls()->ShowCursor( true );
150 
151  if( !( aOptions & IPO_REPEAT ) )
152  break;
153 
154  if( aOptions & IPO_SINGLE_CLICK )
155  {
157 
158  newItem = aPlacer->CreateItem();
159  newItem->SetPosition( wxPoint( pos.x, pos.y ) );
160 
161  preview.Add( newItem.get() );
162  }
163  }
164  }
165 
166  else if( newItem && evt->Category() == TC_COMMAND )
167  {
168  /*
169  * Handle any events that can affect the item as we move
170  * it around, eg rotate and flip
171  */
172 
173  if( TOOL_EVT_UTILS::IsRotateToolEvt( *evt ) && ( aOptions & IPO_ROTATE ) )
174  {
175  const auto rotationAngle = TOOL_EVT_UTILS::GetEventRotationAngle(
176  *frame(), *evt );
177  newItem->Rotate( newItem->GetPosition(), rotationAngle );
178  view()->Update( &preview );
179  }
180  else if( evt->IsAction( &PCB_ACTIONS::flip ) && ( aOptions & IPO_FLIP ) )
181  {
182  newItem->Flip( newItem->GetPosition() );
183  view()->Update( &preview );
184  }
185  }
186 
187  else if( newItem && evt->IsMotion() )
188  {
189  // track the cursor
190  newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
191  aPlacer->SnapItem( newItem.get() );
192 
193  // Show a preview of the item
194  view()->Update( &preview );
195  }
196  }
197 
198  view()->Remove( &preview );
199 }
200 
202 {
203 
204 }
205 
207 {
208 
209 }
210 
212 {
213  return static_cast<PCB_DISPLAY_OPTIONS*>( frame()->GetDisplayOptions() );
214 }
215 
217 {
218  return static_cast<PCB_DRAW_PANEL_GAL*>( frame()->GetGalCanvas() );
219 }
220 
222 {
223  auto selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
224  const auto& selection = selTool->GetSelection();
225  return selection;
226 }
227 
229 {
230  auto selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
231  auto& selection = selTool->GetSelection();
232  return selection;
233 }
234 
235 
237 {
238  // Base implementation performs no snapping
239 }
240 
242 {
243  aCommit.Add( aItem );
244 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:53
virtual void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
Definition: pcb_tool.cpp:206
virtual void ShowCursor(bool aEnabled)
Function ShowCursor() Enables or disables display of cursor.
virtual void PlaceItem(BOARD_ITEM *aItem, BOARD_COMMIT &aCommit)
Definition: pcb_tool.cpp:241
virtual void Clear() override
Function Clear() Removes all the stored items from the group.
Definition: selection.h:84
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction)
Function RunOnChildren.
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags) override
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: pcb_view.cpp:81
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
COMMIT & Add(EDA_ITEM *aItem)
Adds a new item to the model
Definition: commit.h:78
virtual std::unique_ptr< BOARD_ITEM > CreateItem()=0
VIEW_CONTROLS class definition.
Class SELECTION_TOOL.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:215
OPT_TOOL_EVENT Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
virtual void SetSnapping(bool aEnabled)
Function SetSnapping() Enables/disables snapping cursor to grid.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:125
PCB_EDIT_FRAME * m_frame
Definition: pcb_tool.h:58
bool IsRotateToolEvt(const TOOL_EVENT &aEvt)
Function isRotateToolEvt()
Casted dyn_cast(From aObject)
Function dyn_cast()
Definition: typeinfo.h:61
virtual void Remove(VIEW_ITEM *aItem) override
Function Remove() Removes a VIEW_ITEM from the view.
Definition: pcb_view.cpp:64
void * GetDisplayOptions() override
Function GetDisplayOptions returns the display options current in use Display options are relative to...
virtual void Add(EDA_ITEM *aItem)
Definition: selection.h:74
virtual void SnapItem(BOARD_ITEM *aItem)
Definition: pcb_tool.cpp:236
PCB_DRAW_PANEL_GAL * canvas() const
Definition: pcb_tool.cpp:216
PCB_DISPLAY_OPTIONS * displayOptions() const
Definition: pcb_tool.cpp:211
KIGFX::VIEW_CONTROLS * controls() const
Definition: pcb_tool.h:135
class MODULE, a footprint
Definition: typeinfo.h:89
Class PCB_DISPLAY_OPTIONS handles display options like enable/disable some optional drawings...
virtual void CaptureCursor(bool aEnabled)
Function CaptureCursor() Forces the cursor to stay within the drawing panel area. ...
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.
PCB_EDIT_FRAME * frame() const
Definition: pcb_tool.h:136
virtual void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
Definition: pcb_tool.cpp:201
MODULE * module() const
Definition: pcb_tool.h:138
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes an item from the group.
Definition: view_group.cpp:61
const SELECTION & selection() const
Definition: pcb_tool.cpp:221
bool IsCancelInteractive(const TOOL_EVENT &aEvt)
Function IsCancelInteractive()
virtual void Add(VIEW_ITEM *aItem)
Function Add() Adds an item to the group.
Definition: view_group.cpp:55
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg...
static TOOL_ACTION flip
Flipping of selected objects.
Definition: pcb_actions.h:102
KIGFX::PCB_VIEW * view() const
Definition: pcb_tool.h:134
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:80
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool...
Definition: tool_base.h:144
void Activate()
Function Activate() Runs the tool.
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Function GetGalCanvas returns a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:903
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Function Add() Adds a VIEW_ITEM to the view.
Definition: pcb_view.cpp:48
Module description (excepted pads)
void doInteractiveItemPlacement(INTERACTIVE_PLACER_BASE *aPlacer, const wxString &aCommitMessage, int aOptions=IPO_ROTATE|IPO_FLIP|IPO_REPEAT)
Helper function for performing a common interactive idiom: wait for a left click, place an item there...
Definition: pcb_tool.cpp:39
int GetEventRotationAngle(const PCB_BASE_EDIT_FRAME &aFrame, const TOOL_EVENT &aEvt)
Function getEventRotationAngle()
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:465
BOARD * board() const
Definition: pcb_tool.h:137
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.h:79