KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 The 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>
29#include <footprint.h>
30#include <pcb_draw_panel_gal.h>
31#include <pgm_base.h>
33#include <pcbnew_settings.h>
37#include <tools/pcb_actions.h>
40#include <view/view_controls.h>
41
42
45 const wxString& aCommitMessage, int aOptions )
46{
47 using namespace std::placeholders;
48 std::unique_ptr<BOARD_ITEM> newItem;
49
50 frame()->PushTool( aTool );
51
52 BOARD_COMMIT commit( frame() );
53
55
56 Activate();
57 // Must be done after Activate() so that it gets set into the correct context
58 controls()->ShowCursor( true );
59 controls()->ForceCursorPosition( false );
60 // do not capture or auto-pan until we start placing an item
61
62 PCB_GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() );
63
64 // Add a VIEW_GROUP that serves as a preview for the new item
65 PCB_SELECTION preview;
66 view()->Add( &preview );
67
68 aPlacer->m_board = board();
69 aPlacer->m_frame = frame();
70 aPlacer->m_modifiers = 0;
71
72 auto makeNewItem =
73 [&]( const VECTOR2I& aPosition )
74 {
75 if( frame()->GetModel() )
76 newItem = aPlacer->CreateItem();
77
78 if( newItem )
79 {
80 newItem->SetPosition( aPosition );
81 preview.Add( newItem.get() );
82
83 // footprints have more drawable parts
84 if( FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( newItem.get() ) )
85 fp->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Add, &preview, _1 ),
86 RECURSE_MODE::NO_RECURSE );
87 }
88 };
89
90 if( aOptions & IPO_SINGLE_CLICK )
91 makeNewItem( controls()->GetCursorPosition() );
92
93 auto setCursor =
94 [&]()
95 {
96 if( !newItem )
97 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::PENCIL );
98 else
99 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::PLACE );
100 };
101
102 // Set initial cursor
103 setCursor();
104
105 // Main loop: keep receiving events
106 while( TOOL_EVENT* evt = Wait() )
107 {
108 setCursor();
109
110 grid.SetSnap( false ); // Interactive placement tools need to set their own item snaps
111 grid.SetUseGrid( getView()->GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
112 VECTOR2I cursorPos = grid.BestSnapAnchor( controls()->GetMousePosition(), nullptr );
113
114 aPlacer->m_modifiers = evt->Modifier();
115
116 auto cleanup =
117 [&] ()
118 {
119 newItem = nullptr;
120 preview.Clear();
121 view()->Update( &preview );
122 controls()->SetAutoPan( false );
123 controls()->CaptureCursor( false );
124 controls()->ShowCursor( true );
125 controls()->ForceCursorPosition( false );
126 };
127
128 if( evt->IsCancelInteractive() )
129 {
130 if( aOptions & IPO_SINGLE_CLICK )
131 {
132 cleanup();
133 frame()->PopTool( aTool );
134 break;
135 }
136 else if( newItem )
137 {
138 cleanup();
139 }
140 else
141 {
142 frame()->PopTool( aTool );
143 break;
144 }
145 }
146 else if( evt->IsActivate() )
147 {
148 if( newItem )
149 cleanup();
150
151 if( evt->IsPointEditor() )
152 {
153 // don't exit (the point editor runs in the background)
154 }
155 else if( evt->IsMoveTool() )
156 {
157 // leave ourselves on the stack so we come back after the move
158 break;
159 }
160 else
161 {
162 frame()->PopTool( aTool );
163 break;
164 }
165 }
166 else if( evt->IsClick( BUT_LEFT ) || evt->IsDblClick( BUT_LEFT ) )
167 {
168 if( !newItem )
169 {
170 // create the item if possible
171 makeNewItem( cursorPos );
172
173 // no item created, so wait for another click
174 if( !newItem )
175 continue;
176
177 controls()->CaptureCursor( true );
178 controls()->SetAutoPan( true );
179 }
180 else
181 {
182 BOARD_ITEM* newBoardItem = newItem.release();
183 EDA_ITEM_FLAGS oldFlags = newBoardItem->GetFlags();
184
185 newBoardItem->ClearFlags();
186
187 if( !aPlacer->PlaceItem( newBoardItem, commit ) )
188 {
189 newBoardItem->SetFlags( oldFlags );
190 newItem.reset( newBoardItem );
191 continue;
192 }
193
194 preview.Clear();
195 commit.Push( aCommitMessage );
196
197 controls()->CaptureCursor( false );
198 controls()->SetAutoPan( false );
199 controls()->ShowCursor( true );
200
201 if( !( aOptions & IPO_REPEAT ) )
202 break;
203
204 if( aOptions & IPO_SINGLE_CLICK )
205 makeNewItem( controls()->GetCursorPosition() );
206
207 setCursor();
208 }
209 }
210 else if( evt->IsClick( BUT_RIGHT ) )
211 {
212 m_menu->ShowContextMenu( selection() );
213 }
214 else if( evt->IsAction( &PCB_ACTIONS::trackViaSizeChanged ) )
215 {
217 }
218 else if( newItem && evt->Category() == TC_COMMAND )
219 {
220 /*
221 * Handle any events that can affect the item as we move it around
222 */
223 if( TOOL_EVT_UTILS::IsRotateToolEvt( *evt ) && ( aOptions & IPO_ROTATE ) )
224 {
225 EDA_ANGLE rotationAngle = TOOL_EVT_UTILS::GetEventRotationAngle( *frame(), *evt );
226 newItem->Rotate( newItem->GetPosition(), rotationAngle );
227 view()->Update( &preview );
228 }
229 else if( evt->IsAction( &PCB_ACTIONS::flip ) && ( aOptions & IPO_FLIP ) )
230 {
231 newItem->Flip( newItem->GetPosition(),
232 frame()->GetPcbNewSettings()->m_FlipDirection );
233 view()->Update( &preview );
234 }
235 else if( evt->IsAction( &PCB_ACTIONS::properties ) )
236 {
237 frame()->OnEditItemRequest( newItem.get() );
238
239 // Notify other tools of the changes
241 }
242 else if( evt->IsAction( &ACTIONS::refreshPreview ) )
243 {
244 preview.Clear();
245 newItem.reset();
246
247 makeNewItem( cursorPos );
248 aPlacer->SnapItem( newItem.get() );
249 view()->Update( &preview );
250 }
251 else
252 {
253 evt->SetPassEvent();
254 }
255 }
256 else if( newItem && evt->IsMotion() )
257 {
258 // track the cursor
259 newItem->SetPosition( cursorPos );
260 aPlacer->SnapItem( newItem.get() );
261
262 // Show a preview of the item
263 view()->Update( &preview );
264 }
265 else
266 {
267 evt->SetPassEvent();
268 }
269 }
270
271 view()->Remove( &preview );
272 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
273 controls()->SetAutoPan( false );
274 controls()->CaptureCursor( false );
275 controls()->ForceCursorPosition( false );
276}
277
278
280{
281 // A basic context manu. Many (but not all) tools will choose to override this.
282 CONDITIONAL_MENU& ctxMenu = m_menu->GetMenu();
283
284 // cancel current tool goes in main context menu at the top if present
286 ctxMenu.AddSeparator( 1 );
287
288 // Finally, add the standard zoom/grid items
289 getEditFrame<PCB_BASE_FRAME>()->AddStandardSubMenus( *m_menu.get() );
290
291 return true;
292}
293
294
296{
297}
298
299
301{
302}
303
304
306{
307 return frame()->GetPcbNewSettings()->m_Display;
308}
309
311{
312 return static_cast<PCB_DRAW_PANEL_GAL*>( frame()->GetCanvas() );
313}
314
315
317{
319
320 return selTool->GetSelection();
321}
322
323
325{
327
328 return selTool->GetSelection();
329}
330
331
333{
335
336 if( frame()->IsType( FRAME_PCB_EDITOR ) )
337 return mgr.GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" )->m_Use45DegreeLimit;
338 else
339 return mgr.GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" )->m_Use45Limit;
340}
341
342
344{
345 // Base implementation performs no snapping
346}
347
348
350{
351 aCommit.Add( aItem );
352 return true;
353}
354
static TOOL_ACTION cancelInteractive
Definition: actions.h:65
static TOOL_ACTION refreshPreview
Definition: actions.h:149
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:78
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition: commit.h:80
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 AddSeparator(int aOrder=ANY_ORDER)
Add a separator to the menu.
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:131
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:133
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:134
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition: actions.h:303
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:91
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Add a VIEW_ITEM to the view.
Definition: pcb_view.cpp:57
virtual void Remove(VIEW_ITEM *aItem) override
Remove a VIEW_ITEM from the view.
Definition: pcb_view.cpp:74
virtual void CaptureCursor(bool aEnabled)
Force the cursor to stay within the drawing panel area.
virtual void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0))
Place the cursor immediately at a given point.
virtual void ShowCursor(bool aEnabled)
Enable or disables display of cursor.
virtual void SetAutoPan(bool aEnabled)
Turn on/off auto panning (this feature is used when there is a tool active (eg.
virtual void Add(VIEW_ITEM *aItem)
Add an item to the group.
Definition: view_group.cpp:58
static TOOL_ACTION trackViaSizeChanged
Definition: pcb_actions.h:406
static TOOL_ACTION properties
Activation of the edit tool.
Definition: pcb_actions.h:181
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:69
static TOOL_ACTION flip
Flipping of selected objects.
Definition: pcb_actions.h:137
The selection tool: currently supports:
PCB_SELECTION & GetSelection()
T * frame() const
KIGFX::PCB_VIEW * view() const
virtual bool Is45Limited() const
Should the tool use its 45° mode option?
KIGFX::VIEW_CONTROLS * controls() const
BOARD * board() const
PCB_DRAW_PANEL_GAL * canvas() const
PCBNEW_SETTINGS::DISPLAY_OPTIONS & displayOptions() const
virtual void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
@ IPO_FLIP
Handle flip action in the loop by calling the item's flip method.
@ IPO_ROTATE
Handle the rotate action in the loop by calling the item's rotate method.
@ IPO_SINGLE_CLICK
Create an item immediately on placement starting, otherwise show the pencil cursor until the item is ...
@ IPO_REPEAT
Allow repeat placement of the item.
virtual bool Init() override
Init() is called once upon a registration of the tool.
virtual void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
void doInteractiveItemPlacement(const TOOL_EVENT &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...
const PCB_SELECTION & selection() const
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:125
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
virtual void Add(EDA_ITEM *aItem)
Definition: selection.cpp:42
virtual void Clear() override
Remove all the stored items from the group.
Definition: selection.h:93
T * GetAppSettings(const wxString &aFilename)
Return a handle to the a given settings by type.
TOOL_MANAGER * GetManager() const
Return the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:146
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:220
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:38
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:168
std::unique_ptr< TOOL_MENU > m_menu
The functions below are not yet implemented - their interface may change.
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.
void Activate()
Run the tool.
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagate an event to tools that requested events of matching type(s).
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:150
bool PostAction(const std::string &aActionName, T aParam)
Run the specified action after the current action (coroutine) ends.
Definition: tool_manager.h:235
std::uint32_t EDA_ITEM_FLAGS
@ FRAME_PCB_EDITOR
Definition: frame_type.h:42
EDA_ANGLE GetEventRotationAngle(const PCB_BASE_EDIT_FRAME &aFrame, const TOOL_EVENT &aEvent)
Function getEventRotationAngle()
bool IsRotateToolEvt(const TOOL_EVENT &aEvt)
Function isRotateToolEvt()
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:1071
see class PGM_BASE
virtual void SnapItem(BOARD_ITEM *aItem)
PCB_BASE_EDIT_FRAME * m_frame
Definition: pcb_tool_base.h:64
virtual std::unique_ptr< BOARD_ITEM > CreateItem()=0
virtual bool PlaceItem(BOARD_ITEM *aItem, BOARD_COMMIT &aCommit)
@ TC_COMMAND
Definition: tool_event.h:57
@ BUT_LEFT
Definition: tool_event.h:132
@ BUT_RIGHT
Definition: tool_event.h:133