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 <view/view_controls.h>
27 #include <view/view.h>
28 #include <tool/tool_manager.h>
29 #include <board_commit.h>
30 #include <class_module.h>
31 #include <pcb_draw_panel_gal.h>
32 #include <pcbnew_settings.h>
33 #include "selection_tool.h"
34 #include "pcb_actions.h"
35 #include "tool_event_utils.h"
36 
37 void PCB_TOOL_BASE::doInteractiveItemPlacement( const std::string& aTool,
38  INTERACTIVE_PLACER_BASE* aPlacer,
39  const wxString& aCommitMessage, int aOptions )
40 {
41  using namespace std::placeholders;
42  std::unique_ptr<BOARD_ITEM> newItem;
43 
44  frame()->PushTool( aTool );
45  Activate();
46 
47  BOARD_COMMIT commit( frame() );
48 
50 
51  // do not capture or auto-pan until we start placing an item
52  controls()->ShowCursor( true );
53  controls()->SetSnapping( true );
54 
55  // Add a VIEW_GROUP that serves as a preview for the new item
56  PCBNEW_SELECTION preview;
57  view()->Add( &preview );
58 
59  aPlacer->m_board = board();
60  aPlacer->m_frame = frame();
61  aPlacer->m_modifiers = 0;
62 
63  auto makeNewItem = [&] ( VECTOR2I aPosition )
64  {
65  if( frame()->GetModel() )
66  newItem = aPlacer->CreateItem();
67 
68  if( newItem )
69  {
70  newItem->SetPosition( (wxPoint) aPosition );
71  preview.Add( newItem.get() );
72 
73  if( newItem->Type() == PCB_MODULE_T )
74  {
75  auto module = dyn_cast<MODULE*>( newItem.get() );
76 
77  // modules have more drawable parts
78  module->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Add, &preview, _1 ) );
79  }
80  }
81  };
82 
83  if( aOptions & IPO_SINGLE_CLICK )
84  makeNewItem( controls()->GetCursorPosition() );
85 
86  // Main loop: keep receiving events
87  while( TOOL_EVENT* evt = Wait() )
88  {
89  if( !newItem )
90  frame()->GetCanvas()->SetCurrentCursor( wxCURSOR_PENCIL );
91  else
92  frame()->GetCanvas()->SetCurrentCursor( wxCURSOR_ARROW );
93 
94  VECTOR2I cursorPos = controls()->GetCursorPosition();
95  aPlacer->m_modifiers = evt->Modifier();
96 
97  auto cleanup = [&] ()
98  {
99  newItem = nullptr;
100  preview.Clear();
101  view()->Update( &preview );
102  controls()->SetAutoPan( false );
103  controls()->CaptureCursor( false );
104  controls()->ShowCursor( true );
105  };
106 
107  if( evt->IsCancelInteractive() )
108  {
109  if( aOptions & IPO_SINGLE_CLICK )
110  {
111  cleanup();
112  frame()->PopTool( aTool );
113  break;
114  }
115  else if( newItem )
116  cleanup();
117  else
118  {
119  frame()->PopTool( aTool );
120  break;
121  }
122  }
123  else if( evt->IsActivate() )
124  {
125  if( newItem )
126  cleanup();
127 
128  if( evt->IsPointEditor() )
129  {
130  // don't exit (the point editor runs in the background)
131  }
132  else if( evt->IsMoveTool() )
133  {
134  // leave ourselves on the stack so we come back after the move
135  break;
136  }
137  else
138  {
139  frame()->PopTool( aTool );
140  break;
141  }
142  }
143  else if( evt->IsClick( BUT_LEFT ) )
144  {
145  if( !newItem )
146  {
147  // create the item if possible
148  makeNewItem( cursorPos );
149 
150  // no item created, so wait for another click
151  if( !newItem )
152  continue;
153 
154  controls()->CaptureCursor( true );
155  controls()->SetAutoPan( true );
156  }
157  else
158  {
159  auto oldFlags = newItem->GetFlags();
160  newItem->ClearFlags();
161 
162  if( !aPlacer->PlaceItem( newItem.get(), commit ) )
163  {
164  newItem->SetFlags( oldFlags );
165  continue;
166  }
167 
168  preview.Clear();
169  newItem.release();
170  commit.Push( aCommitMessage );
171 
172  controls()->CaptureCursor( false );
173  controls()->SetAutoPan( false );
174  controls()->ShowCursor( true );
175 
176  if( !( aOptions & IPO_REPEAT ) )
177  break;
178 
179  if( aOptions & IPO_SINGLE_CLICK )
180  makeNewItem( controls()->GetCursorPosition() );
181  }
182  }
183  else if( evt->IsClick( BUT_RIGHT ) )
184  {
186  }
187  else if( newItem && evt->Category() == TC_COMMAND )
188  {
189  /*
190  * Handle any events that can affect the item as we move it around
191  */
192  if( TOOL_EVT_UTILS::IsRotateToolEvt( *evt ) && ( aOptions & IPO_ROTATE ) )
193  {
194  const int rotationAngle = TOOL_EVT_UTILS::GetEventRotationAngle( *frame(), *evt );
195  newItem->Rotate( newItem->GetPosition(), rotationAngle );
196  view()->Update( &preview );
197  }
198  else if( evt->IsAction( &PCB_ACTIONS::flip ) && ( aOptions & IPO_FLIP ) )
199  {
200  newItem->Flip( newItem->GetPosition(), frame()->Settings().m_FlipLeftRight );
201  view()->Update( &preview );
202  }
203  else if( evt->IsAction( &PCB_ACTIONS::viaSizeInc )
204  || evt->IsAction( &PCB_ACTIONS::viaSizeDec ) )
205  {
206  // Refresh preview after event runs
208  }
209  else if( evt->IsAction( &ACTIONS::refreshPreview ) )
210  {
211  preview.Clear();
212  newItem.release();
213 
214  makeNewItem( controls()->GetCursorPosition() );
215  view()->Update( &preview );
216  }
217  }
218 
219  else if( newItem && evt->IsMotion() )
220  {
221  // track the cursor
222  newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
223  aPlacer->SnapItem( newItem.get() );
224 
225  // Show a preview of the item
226  view()->Update( &preview );
227  }
228 
229  else
230  evt->SetPassEvent();
231  }
232 
233  view()->Remove( &preview );
234 }
235 
236 
238 {
239  // A basic context manu. Many (but not all) tools will choose to override this.
240 
241  auto& ctxMenu = m_menu.GetMenu();
242 
243  // cancel current tool goes in main context menu at the top if present
245  ctxMenu.AddSeparator( 1 );
246 
247  // Finally, add the standard zoom/grid items
248  getEditFrame<PCB_BASE_FRAME>()->AddStandardSubMenus( m_menu );
249 
250  return true;
251 }
252 
253 
255 {
256 }
257 
258 
260 {
261 }
262 
263 
265 {
266  return frame()->GetDisplayOptions();
267 }
268 
270 {
271  return static_cast<PCB_DRAW_PANEL_GAL*>( frame()->GetCanvas() );
272 }
273 
274 
276 {
277  auto selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
278  const auto& selection = selTool->GetSelection();
279  return selection;
280 }
281 
282 
284 {
285  auto selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
286  auto& selection = selTool->GetSelection();
287  return selection;
288 }
289 
290 
292 {
293  // Base implementation performs no snapping
294 }
295 
296 
298 {
299  aCommit.Add( aItem );
300  return true;
301 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:62
virtual void ShowCursor(bool aEnabled)
Function ShowCursor() Enables or disables display of cursor.
static bool ShowAlways(const SELECTION &aSelection)
Function ShowAlways The default condition function (always returns true).
TOOL_MENU m_menu
functions below are not yet implemented - their interface may change
virtual void Clear() override
Function Clear() Removes all the stored items from the group.
Definition: selection.h:95
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction)
Function RunOnChildren.
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
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
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:91
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
static TOOL_ACTION viaSizeInc
Definition: pcb_actions.h:279
COMMIT & Add(EDA_ITEM *aItem)
Adds a new item to the model
Definition: commit.h:78
virtual std::unique_ptr< BOARD_ITEM > CreateItem()=0
void SetCurrentCursor(wxStockCursor aStockCursorID)
Function SetCurrentCursor Set the current cursor shape for this panel.
VIEW_CONTROLS class definition.
SELECTION_TOOL.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:46
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
static TOOL_ACTION cancelInteractive
Definition: actions.h:65
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
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:140
virtual void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
bool IsRotateToolEvt(const TOOL_EVENT &aEvt)
Function isRotateToolEvt()
const PCB_DISPLAY_OPTIONS & GetDisplayOptions() const
Function GetDisplayOptions Display options control the way tracks, vias, outlines and other things ar...
virtual void Remove(VIEW_ITEM *aItem) override
Function Remove() Removes a VIEW_ITEM from the view.
Definition: pcb_view.cpp:74
virtual void Add(EDA_ITEM *aItem)
Definition: selection.h:76
TOOL_MANAGER * GetManager() const
Function GetManager() Returns 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)
class MODULE, a footprint
Definition: typeinfo.h:89
PCB_DISPLAY_OPTIONS handles display options like enable/disable some optional drawings.
const PCBNEW_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.
virtual void PopTool(const std::string &actionName)
virtual void CaptureCursor(bool aEnabled)
Function CaptureCursor() Forces the cursor to stay within the drawing panel area.
TOOL_EVENT.
Definition: tool_event.h:171
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)
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.
const PCB_DISPLAY_OPTIONS & displayOptions() const
virtual BOARD_ITEM_CONTAINER * GetModel() const =0
Function GetModel()
static TOOL_ACTION flip
Flipping of selected objects.
Definition: pcb_actions.h:100
Handle the rotate action in the loop by calling the item's rotate method.
KIGFX::VIEW_CONTROLS * controls() const
MODULE * module() const
virtual bool Init() override
Function Init() Init() is called once upon a registration of the tool.
PCBNEW_SETTINGS & Settings()
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:79
void Activate()
Function Activate() Runs the tool.
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Function Add() Adds 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:62
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)
Function AddItem()
void ShowContextMenu(SELECTION &aSelection)
Function ShowContextMenu.
Definition: tool_menu.cpp:59
static TOOL_ACTION viaSizeDec
Definition: pcb_actions.h:280
static TOOL_ACTION refreshPreview
Definition: actions.h:101
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.