KiCad PCB EDA Suite
pcb_tool_base.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-2019 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 #include "pcb_tool_base.h"
25 
26 #include <tool/tool_manager.h>
27 #include <board_commit.h>
28 #include <footprint.h>
29 #include <pcb_draw_panel_gal.h>
30 #include <pcbnew_settings.h>
31 #include "pcb_selection_tool.h"
32 #include "pcb_actions.h"
33 #include "tool_event_utils.h"
34 
35 void PCB_TOOL_BASE::doInteractiveItemPlacement( const std::string& aTool,
36  INTERACTIVE_PLACER_BASE* aPlacer,
37  const wxString& aCommitMessage, int aOptions )
38 {
39  using namespace std::placeholders;
40  std::unique_ptr<BOARD_ITEM> newItem;
41 
42  frame()->PushTool( aTool );
43  Activate();
44 
45  BOARD_COMMIT commit( frame() );
46 
48 
49  // do not capture or auto-pan until we start placing an item
50  controls()->ShowCursor( true );
51 
52  // Add a VIEW_GROUP that serves as a preview for the new item
53  PCB_SELECTION preview;
54  view()->Add( &preview );
55 
56  aPlacer->m_board = board();
57  aPlacer->m_frame = frame();
58  aPlacer->m_modifiers = 0;
59 
60  auto makeNewItem =
61  [&]( VECTOR2I aPosition )
62  {
63  if( frame()->GetModel() )
64  newItem = aPlacer->CreateItem();
65 
66  if( newItem )
67  {
68  newItem->SetPosition( (wxPoint) aPosition );
69  preview.Add( newItem.get() );
70 
71  if( newItem->Type() == PCB_FOOTPRINT_T )
72  {
73  FOOTPRINT* fp = dyn_cast<FOOTPRINT*>( newItem.get() );
74 
75  // footprints have more drawable parts
76  fp->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Add, &preview, _1 ) );
77  }
78  }
79  };
80 
81  if( aOptions & IPO_SINGLE_CLICK )
82  makeNewItem( controls()->GetCursorPosition() );
83 
84  auto setCursor =
85  [&]()
86  {
87  if( !newItem )
89  else
91  };
92 
93  // Set initial cursor
94  setCursor();
95 
96  // Main loop: keep receiving events
97  while( TOOL_EVENT* evt = Wait() )
98  {
99  setCursor();
100 
101  VECTOR2I cursorPos = controls()->GetCursorPosition();
102  aPlacer->m_modifiers = evt->Modifier();
103 
104  auto cleanup =
105  [&] ()
106  {
107  newItem = nullptr;
108  preview.Clear();
109  view()->Update( &preview );
110  controls()->SetAutoPan( false );
111  controls()->CaptureCursor( false );
112  controls()->ShowCursor( true );
113  };
114 
115  if( evt->IsCancelInteractive() )
116  {
117  if( aOptions & IPO_SINGLE_CLICK )
118  {
119  cleanup();
120  frame()->PopTool( aTool );
121  break;
122  }
123  else if( newItem )
124  {
125  cleanup();
126  }
127  else
128  {
129  frame()->PopTool( aTool );
130  break;
131  }
132  }
133  else if( evt->IsActivate() )
134  {
135  if( newItem )
136  cleanup();
137 
138  if( evt->IsPointEditor() )
139  {
140  // don't exit (the point editor runs in the background)
141  }
142  else if( evt->IsMoveTool() )
143  {
144  // leave ourselves on the stack so we come back after the move
145  break;
146  }
147  else
148  {
149  frame()->PopTool( aTool );
150  break;
151  }
152  }
153  else if( evt->IsClick( BUT_LEFT ) || evt->IsDblClick( BUT_LEFT ) )
154  {
155  if( !newItem )
156  {
157  // create the item if possible
158  makeNewItem( cursorPos );
159 
160  // no item created, so wait for another click
161  if( !newItem )
162  continue;
163 
164  controls()->CaptureCursor( true );
165  controls()->SetAutoPan( true );
166  }
167  else
168  {
169  auto oldFlags = newItem->GetFlags();
170  newItem->ClearFlags();
171 
172  if( !aPlacer->PlaceItem( newItem.get(), commit ) )
173  {
174  newItem->SetFlags( oldFlags );
175  continue;
176  }
177 
178  preview.Clear();
179  newItem.release();
180  commit.Push( aCommitMessage );
181 
182  controls()->CaptureCursor( false );
183  controls()->SetAutoPan( false );
184  controls()->ShowCursor( true );
185 
186  if( !( aOptions & IPO_REPEAT ) )
187  break;
188 
189  if( aOptions & IPO_SINGLE_CLICK )
190  makeNewItem( controls()->GetCursorPosition() );
191 
192  setCursor();
193  }
194  }
195  else if( evt->IsClick( BUT_RIGHT ) )
196  {
198  }
199  else if( evt->IsAction( &PCB_ACTIONS::trackViaSizeChanged ) )
200  {
202  }
203  else if( newItem && evt->Category() == TC_COMMAND )
204  {
205  /*
206  * Handle any events that can affect the item as we move it around
207  */
208  if( TOOL_EVT_UTILS::IsRotateToolEvt( *evt ) && ( aOptions & IPO_ROTATE ) )
209  {
210  const int rotationAngle = TOOL_EVT_UTILS::GetEventRotationAngle( *frame(), *evt );
211  newItem->Rotate( newItem->GetPosition(), rotationAngle );
212  view()->Update( &preview );
213  }
214  else if( evt->IsAction( &PCB_ACTIONS::flip ) && ( aOptions & IPO_FLIP ) )
215  {
216  newItem->Flip( newItem->GetPosition(), frame()->Settings().m_FlipLeftRight );
217  view()->Update( &preview );
218  }
219  else if( evt->IsAction( &PCB_ACTIONS::viaSizeInc )
220  || evt->IsAction( &PCB_ACTIONS::viaSizeDec ) )
221  {
222  // Refresh preview after event runs
224  }
225  else if( evt->IsAction( &PCB_ACTIONS::properties ) )
226  {
227  frame()->OnEditItemRequest( newItem.get() );
228 
229  // Notify other tools of the changes
231  }
232  else if( evt->IsAction( &ACTIONS::refreshPreview ) )
233  {
234  preview.Clear();
235  newItem.release();
236 
237  makeNewItem( (wxPoint) cursorPos );
238  aPlacer->SnapItem( newItem.get() );
239  view()->Update( &preview );
240  }
241  else
242  {
243  evt->SetPassEvent();
244  }
245  }
246  else if( newItem && evt->IsMotion() )
247  {
248  // track the cursor
249  newItem->SetPosition( (wxPoint) cursorPos );
250  aPlacer->SnapItem( newItem.get() );
251 
252  // Show a preview of the item
253  view()->Update( &preview );
254  }
255  else
256  {
257  evt->SetPassEvent();
258  }
259  }
260 
261  view()->Remove( &preview );
263  controls()->SetAutoPan( false );
264  controls()->CaptureCursor( false );
265 }
266 
267 
269 {
270  // A basic context manu. Many (but not all) tools will choose to override this.
271  CONDITIONAL_MENU& ctxMenu = m_menu.GetMenu();
272 
273  // cancel current tool goes in main context menu at the top if present
275  ctxMenu.AddSeparator( 1 );
276 
277  // Finally, add the standard zoom/grid items
278  getEditFrame<PCB_BASE_FRAME>()->AddStandardSubMenus( m_menu );
279 
280  return true;
281 }
282 
283 
285 {
286 }
287 
288 
290 {
291 }
292 
293 
295 {
296  return frame()->GetDisplayOptions();
297 }
298 
300 {
301  return static_cast<PCB_DRAW_PANEL_GAL*>( frame()->GetCanvas() );
302 }
303 
304 
306 {
308 
309  return selTool->GetSelection();
310 }
311 
312 
314 {
316 
317  return selTool->GetSelection();
318 }
319 
320 
322 {
323  // Base implementation performs no snapping
324 }
325 
326 
328 {
329  aCommit.Add( aItem );
330  return true;
331 }
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:59
virtual void ShowCursor(bool aEnabled)
Enable or disables display of cursor.
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
TOOL_MENU m_menu
The functions below are not yet implemented - their interface may change.
virtual void Clear() override
Remove all the stored items from the group.
Definition: selection.h:82
void SetCurrentCursor(KICURSOR aCursor)
Set the current cursor shape for this panel.
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Suspend execution of the tool until an event specified in aEventList arrives.
virtual bool PlaceItem(BOARD_ITEM *aItem, BOARD_COMMIT &aCommit)
Handle flip action in the loop by calling the item's flip method.
BOARD * board() const
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
virtual void Add(EDA_ITEM *aItem)
Definition: selection.cpp:31
static TOOL_ACTION viaSizeInc
Definition: pcb_actions.h:299
COMMIT & Add(EDA_ITEM *aItem)
Notify observers that aItem has been added.
Definition: commit.h:78
virtual std::unique_ptr< BOARD_ITEM > CreateItem()=0
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
CONDITIONAL_MENU & GetMenu()
Definition: tool_menu.cpp:46
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:214
virtual void OnEditItemRequest(BOARD_ITEM *aItem)=0
Install the corresponding dialog editor for the given item.
static TOOL_ACTION cancelInteractive
Definition: actions.h:62
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
static TOOL_ACTION properties
Activation of the edit tool.
Definition: pcb_actions.h:117
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:143
static TOOL_ACTION trackViaSizeChanged
Definition: pcb_actions.h:302
virtual void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
bool IsRotateToolEvt(const TOOL_EVENT &aEvt)
Function isRotateToolEvt()
const PCB_DISPLAY_OPTIONS & GetDisplayOptions() const
Display options control the way tracks, vias, outlines and other things are shown (for instance solid...
virtual void Remove(VIEW_ITEM *aItem) override
Remove a VIEW_ITEM from the view.
Definition: pcb_view.cpp:75
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition: actions.h:204
TOOL_MANAGER * GetManager() const
Return the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:143
PCB_BASE_EDIT_FRAME * frame() const
virtual void SnapItem(BOARD_ITEM *aItem)
PCB_SELECTION & GetSelection()
Return the set of currently selected items.
Container for display options like enable/disable some optional drawings.
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const override
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: pcb_view.cpp:92
const PCB_SELECTION & selection() const
virtual void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
Allow repeat placement of the item.
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagate an event to tools that requested events of matching type(s).
virtual void PopTool(const std::string &actionName)
virtual void CaptureCursor(bool aEnabled)
Force the cursor to stay within the drawing panel area.
Generic, UI-independent tool event.
Definition: tool_event.h:152
KIGFX::PCB_VIEW * view() const
Create an item immediately on placement starting, otherwise show the pencil cursor until the item is ...
virtual void Add(VIEW_ITEM *aItem)
Add an item to the group.
Definition: view_group.cpp:56
virtual void SetAutoPan(bool aEnabled)
Turn on/off auto panning (this feature is used when there is a tool active (eg.
const PCB_DISPLAY_OPTIONS & displayOptions() const
class FOOTPRINT, a footprint
Definition: typeinfo.h:88
void AddSeparator(int aOrder=ANY_ORDER)
Add a separator to the menu.
virtual BOARD_ITEM_CONTAINER * GetModel() const =0
static TOOL_ACTION flip
Flipping of selected objects.
Definition: pcb_actions.h:105
Handle the rotate action in the loop by calling the item's rotate method.
KIGFX::VIEW_CONTROLS * controls() const
virtual bool Init() override
Init() is called once upon a registration of the tool.
PCBNEW_SETTINGS & Settings()
The selection tool: currently supports:
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Revert the commit by restoring the modified items state.
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:77
void Activate()
Run the tool.
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction) const
Invoke a function on all BOARD_ITEMs that belong to the footprint (pads, drawings,...
Definition: footprint.cpp:1189
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Add a VIEW_ITEM to the view.
Definition: pcb_view.cpp:58
void doInteractiveItemPlacement(const std::string &aTool, 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...
PCB_BASE_EDIT_FRAME * m_frame
Definition: pcb_tool_base.h:64
PCB_DRAW_PANEL_GAL * canvas() const
int GetEventRotationAngle(const PCB_BASE_EDIT_FRAME &aFrame, const TOOL_EVENT &aEvt)
Function getEventRotationAngle()
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Add a menu entry to run a TOOL_ACTION on selected items.
void ShowContextMenu(SELECTION &aSelection)
Helper function to set and immediately show a CONDITIONAL_MENU in concert with the given SELECTION.
Definition: tool_menu.cpp:59
static TOOL_ACTION viaSizeDec
Definition: pcb_actions.h:300
static TOOL_ACTION refreshPreview
Definition: actions.h:106
VECTOR2D GetCursorPosition() const
Return the current cursor position in world coordinates.