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 
34 #include "selection_tool.h"
35 #include "pcb_actions.h"
36 #include "tool_event_utils.h"
37 
39  const wxString& aCommitMessage,
40  int aOptions )
41 {
42  using namespace std::placeholders;
43  std::unique_ptr<BOARD_ITEM> newItem;
44 
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  SELECTION preview;
57  view()->Add( &preview );
58 
59  aPlacer->m_board = board();
60  aPlacer->m_frame = frame();
61 
62  if( aOptions & IPO_SINGLE_CLICK )
63  {
64  VECTOR2I cursorPos = controls()->GetCursorPosition();
65 
66  newItem = aPlacer->CreateItem();
67  newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
68 
69  preview.Add( newItem.get() );
70  }
71 
72  // Main loop: keep receiving events
73  while( OPT_TOOL_EVENT evt = Wait() )
74  {
75  VECTOR2I cursorPos = controls()->GetCursorPosition();
76 
78  {
79  if( newItem )
80  {
81  // Delete the old item and have another try
82  newItem = nullptr;
83 
84  preview.Clear();
85 
86  if( aOptions & IPO_SINGLE_CLICK )
87  break;
88 
89  controls()->SetAutoPan( false );
90  controls()->CaptureCursor( false );
91  controls()->ShowCursor( true );
92  }
93  else
94  {
95  break;
96  }
97 
98  if( evt->IsActivate() ) // now finish unconditionally
99  break;
100  }
101 
102  else if( evt->IsClick( BUT_LEFT ) )
103  {
104  if( !newItem )
105  {
106  // create the item if possible
107  newItem = aPlacer->CreateItem();
108 
109  // no item created, so wait for another click
110  if( !newItem )
111  continue;
112 
113  controls()->CaptureCursor( true );
114  controls()->SetAutoPan( true );
115 
116  newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
117 
118  preview.Add( newItem.get() );
119 
120  if( newItem->Type() == PCB_MODULE_T )
121  {
122  auto module = dyn_cast<MODULE*>( newItem.get() );
123 
124  // modules have more drawable parts
125  module->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Add, &preview, _1 ) );
126  }
127  }
128  else
129  {
130  newItem->ClearFlags();
131  preview.Remove( newItem.get() );
132 
133  aPlacer->PlaceItem( newItem.get() );
134 
135  if( newItem->Type() == PCB_MODULE_T )
136  {
137  auto module = dyn_cast<MODULE*>( newItem.get() );
138  module->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Remove, &preview, _1 ) );
139  }
140 
141  commit.Add( newItem.release() );
142  commit.Push( aCommitMessage );
143 
144  controls()->CaptureCursor( false );
145  controls()->SetAutoPan( false );
146  controls()->ShowCursor( true );
147 
148  if( !( aOptions & IPO_REPEAT ) )
149  break;
150 
151  if( aOptions & IPO_SINGLE_CLICK )
152  {
154 
155  newItem = aPlacer->CreateItem();
156  newItem->SetPosition( wxPoint( pos.x, pos.y ) );
157 
158  preview.Add( newItem.get() );
159  }
160  }
161  }
162 
163  else if( newItem && evt->Category() == TC_COMMAND )
164  {
165  /*
166  * Handle any events that can affect the item as we move
167  * it around, eg rotate and flip
168  */
169 
170  if( TOOL_EVT_UTILS::IsRotateToolEvt( *evt ) && ( aOptions & IPO_ROTATE ) )
171  {
172  const auto rotationAngle = TOOL_EVT_UTILS::GetEventRotationAngle(
173  *frame(), *evt );
174  newItem->Rotate( newItem->GetPosition(), rotationAngle );
175  view()->Update( &preview );
176  }
177  else if( evt->IsAction( &PCB_ACTIONS::flip ) && ( aOptions & IPO_FLIP ) )
178  {
179  newItem->Flip( newItem->GetPosition() );
180  view()->Update( &preview );
181  }
182  }
183 
184  else if( newItem && evt->IsMotion() )
185  {
186  // track the cursor
187  newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
188 
189  // Show a preview of the item
190  view()->Update( &preview );
191  }
192  }
193 
194  view()->Remove( &preview );
195 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:53
virtual void ShowCursor(bool aEnabled)
Function ShowCursor() Enables or disables display of cursor.
KIGFX::VIEW * view() const
Definition: pcb_tool.h:123
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:51
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.
void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:341
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:53
bool IsRotateToolEvt(const TOOL_EVENT &aEvt)
Function isRotateToolEvt()
Casted dyn_cast(From aObject)
Function dyn_cast()
Definition: typeinfo.h:73
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true) override
Executes the changes.
virtual void Add(EDA_ITEM *aItem)
Definition: selection.h:71
KIGFX::VIEW_CONTROLS * controls() const
Definition: pcb_tool.h:124
class MODULE, a footprint
Definition: typeinfo.h:101
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:125
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
void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1385
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:96
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.
void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:311
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:38
int GetEventRotationAngle(const PCB_BASE_EDIT_FRAME &aFrame, const TOOL_EVENT &aEvt)
Function getEventRotationAngle()
BOARD * board() const
Definition: pcb_tool.h:126
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.h:76