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 
63  if( aOptions & IPO_SINGLE_CLICK )
64  {
65  VECTOR2I cursorPos = controls()->GetCursorPosition();
66 
67  newItem = aPlacer->CreateItem();
68  newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
69 
70  preview.Add( newItem.get() );
71  }
72 
73  // Main loop: keep receiving events
74  while( OPT_TOOL_EVENT evt = Wait() )
75  {
76  VECTOR2I cursorPos = controls()->GetCursorPosition();
77 
79  {
80  if( newItem )
81  {
82  // Delete the old item and have another try
83  newItem = nullptr;
84 
85  preview.Clear();
86 
87  if( aOptions & IPO_SINGLE_CLICK )
88  break;
89 
90  controls()->SetAutoPan( false );
91  controls()->CaptureCursor( false );
92  controls()->ShowCursor( true );
93  }
94  else
95  {
96  break;
97  }
98 
99  if( evt->IsActivate() ) // now finish unconditionally
100  break;
101  }
102 
103  else if( evt->IsClick( BUT_LEFT ) )
104  {
105  if( !newItem )
106  {
107  // create the item if possible
108  newItem = aPlacer->CreateItem();
109 
110  // no item created, so wait for another click
111  if( !newItem )
112  continue;
113 
114  controls()->CaptureCursor( true );
115  controls()->SetAutoPan( true );
116 
117  newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
118 
119  preview.Add( newItem.get() );
120 
121  if( newItem->Type() == PCB_MODULE_T )
122  {
123  auto module = dyn_cast<MODULE*>( newItem.get() );
124 
125  // modules have more drawable parts
126  module->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Add, &preview, _1 ) );
127  }
128  }
129  else
130  {
131  newItem->ClearFlags();
132  preview.Remove( newItem.get() );
133 
134  aPlacer->PlaceItem( newItem.get() );
135 
136  if( newItem->Type() == PCB_MODULE_T )
137  {
138  auto module = dyn_cast<MODULE*>( newItem.get() );
139  module->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Remove, &preview, _1 ) );
140  }
141 
142  commit.Add( newItem.release() );
143  commit.Push( aCommitMessage );
144 
145  controls()->CaptureCursor( false );
146  controls()->SetAutoPan( false );
147  controls()->ShowCursor( true );
148 
149  if( !( aOptions & IPO_REPEAT ) )
150  break;
151 
152  if( aOptions & IPO_SINGLE_CLICK )
153  {
155 
156  newItem = aPlacer->CreateItem();
157  newItem->SetPosition( wxPoint( pos.x, pos.y ) );
158 
159  preview.Add( newItem.get() );
160  }
161  }
162  }
163 
164  else if( newItem && evt->Category() == TC_COMMAND )
165  {
166  /*
167  * Handle any events that can affect the item as we move
168  * it around, eg rotate and flip
169  */
170 
171  if( TOOL_EVT_UTILS::IsRotateToolEvt( *evt ) && ( aOptions & IPO_ROTATE ) )
172  {
173  const auto rotationAngle = TOOL_EVT_UTILS::GetEventRotationAngle(
174  *frame(), *evt );
175  newItem->Rotate( newItem->GetPosition(), rotationAngle );
176  view()->Update( &preview );
177  }
178  else if( evt->IsAction( &PCB_ACTIONS::flip ) && ( aOptions & IPO_FLIP ) )
179  {
180  newItem->Flip( newItem->GetPosition() );
181  view()->Update( &preview );
182  }
183  }
184 
185  else if( newItem && evt->IsMotion() )
186  {
187  // track the cursor
188  newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
189 
190  // Show a preview of the item
191  view()->Update( &preview );
192  }
193  }
194 
195  view()->Remove( &preview );
196 }
197 
199 {
200 
201 }
202 
204 {
205 
206 }
207 
209 {
210  return static_cast<PCB_DISPLAY_OPTIONS*>( frame()->GetDisplayOptions() );
211 }
212 
214 {
215  return static_cast<PCB_DRAW_PANEL_GAL*>( frame()->GetGalCanvas() );
216 }
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:203
virtual void ShowCursor(bool aEnabled)
Function ShowCursor() Enables or disables display of cursor.
void RunOnChildren(std::function< void(BOARD_ITEM *)> aFunction)
Function RunOnChildren.
virtual void Clear() override
Function Clear() Removes all the stored items from the group.
Definition: selection.h:81
virtual bool PlaceItem(BOARD_ITEM *aItem)
Definition: pcb_tool.h:54
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
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.
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:56
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
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true) override
Executes the changes.
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:71
PCB_DRAW_PANEL_GAL * canvas() const
Definition: pcb_tool.cpp:213
PCB_DISPLAY_OPTIONS * displayOptions() const
Definition: pcb_tool.cpp:208
KIGFX::VIEW_CONTROLS * controls() const
Definition: pcb_tool.h:132
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:133
virtual void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
Definition: pcb_tool.cpp:198
MODULE * module() const
Definition: pcb_tool.h:135
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes an item from the group.
Definition: view_group.cpp:61
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:99
KIGFX::PCB_VIEW * view() const
Definition: pcb_tool.h:131
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:870
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:460
BOARD * board() const
Definition: pcb_tool.h:134
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.h:76