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 
38 
40  const wxString& aCommitMessage )
41 {
42  using namespace std::placeholders;
43 
44  KIGFX::VIEW& view = *getView();
46  auto& frame = *getEditFrame<PCB_EDIT_FRAME>();
47 
48  std::unique_ptr<BOARD_ITEM> newItem;
49 
50  Activate();
51 
52  BOARD_COMMIT commit( &frame );
53 
55 
56  // do not capture or auto-pan until we start placing an item
57  controls.ShowCursor( true );
58  controls.SetSnapping( true );
59 
60  // Add a VIEW_GROUP that serves as a preview for the new item
61  SELECTION preview;
62  view.Add( &preview );
63 
64  // Main loop: keep receiving events
65  while( OPT_TOOL_EVENT evt = Wait() )
66  {
67  VECTOR2I cursorPos = controls.GetCursorPosition();
68 
70  {
71  if( newItem )
72  {
73  // Delete the old item and have another try
74  newItem = nullptr;
75 
76  preview.Clear();
77 
78  controls.SetAutoPan( false );
79  controls.CaptureCursor( false );
80  controls.ShowCursor( true );
81  }
82  else
83  {
84  break;
85  }
86 
87  if( evt->IsActivate() ) // now finish unconditionally
88  break;
89  }
90 
91  else if( evt->IsClick( BUT_LEFT ) )
92  {
93  if( !newItem )
94  {
95  // create the item if possible
96  newItem = aItemCreator( *evt );
97 
98  // no item created, so wait for another click
99  if( !newItem )
100  continue;
101 
102  controls.CaptureCursor( true );
103  controls.SetAutoPan( true );
104 
105  newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
106 
107  preview.Add( newItem.get() );
108 
109  if( newItem->Type() == PCB_MODULE_T )
110  {
111  auto module = dyn_cast<MODULE*>( newItem.get() );
112 
113  // modules have more drawable parts
114  module->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Add, &preview, _1 ) );
115  }
116  }
117  else
118  {
119  newItem->ClearFlags();
120  preview.Remove( newItem.get() );
121 
122  if( newItem->Type() == PCB_MODULE_T )
123  {
124  auto module = dyn_cast<MODULE*>( newItem.get() );
125  module->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Remove, &preview, _1 ) );
126  }
127 
128  commit.Add( newItem.release() );
129  commit.Push( aCommitMessage );
130 
131  controls.CaptureCursor( false );
132  controls.SetAutoPan( false );
133  controls.ShowCursor( true );
134  }
135  }
136 
137  else if( newItem && evt->Category() == TC_COMMAND )
138  {
139  /*
140  * Handle any events that can affect the item as we move
141  * it around, eg rotate and flip
142  */
143 
144  if( TOOL_EVT_UTILS::IsRotateToolEvt( *evt ) )
145  {
146  const auto rotationAngle = TOOL_EVT_UTILS::GetEventRotationAngle(
147  frame, *evt );
148  newItem->Rotate( newItem->GetPosition(), rotationAngle );
149  view.Update( &preview );
150  }
151  else if( evt->IsAction( &PCB_ACTIONS::flip ) )
152  {
153  newItem->Flip( newItem->GetPosition() );
154  view.Update( &preview );
155  }
156  }
157 
158  else if( newItem && evt->IsMotion() )
159  {
160  // track the cursor
161  newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
162 
163  // Show a preview of the item
164  view.Update( &preview );
165  }
166  }
167 
168  view.Remove( &preview );
169 }
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:108
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
COMMIT & Add(EDA_ITEM *aItem)
Adds a new item to the model
Definition: commit.h:78
VIEW_CONTROLS class definition.
void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:340
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
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
class MODULE, a footprint
Definition: typeinfo.h:101
virtual VECTOR2D GetCursorPosition() const =0
Function GetCursorPosition() Returns the current cursor position in world coordinates.
virtual void CaptureCursor(bool aEnabled)
Function CaptureCursor() Forces the cursor to stay within the drawing panel area. ...
PCB_EDIT_FRAME * frame() const
Definition: pcb_tool.h:109
Class VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (...
Definition: view_controls.h:94
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)
Function Update() For dynamic VIEWs, informs the associated VIEW that the graphical representation of...
Definition: view.cpp:1378
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg...
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:36
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:30
static TOOL_ACTION flip
Flipping of selected objects.
Definition: pcb_actions.h:90
void doInteractiveItemPlacement(ITEM_CREATOR aItemCreator, const wxString &aCommitMessage)
Helper function for performing a common interactive idiom: wait for a left click, place an item there...
Definition: pcb_tool.cpp:39
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:310
Module description (excepted pads)
Class VIEW.
Definition: view.h:58
std::function< std::unique_ptr< BOARD_ITEM >(const TOOL_EVENT &aEvt) > ITEM_CREATOR
Callable that returns a new board item.
Definition: pcb_tool.h:91
int GetEventRotationAngle(const PCB_BASE_EDIT_FRAME &aFrame, const TOOL_EVENT &aEvt)
Function getEventRotationAngle()
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.h:76