KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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
41
44 const wxString& aCommitMessage, int aOptions )
45{
46 using namespace std::placeholders;
47 std::unique_ptr<BOARD_ITEM> newItem;
48
49 frame()->PushTool( aTool );
50
51 BOARD_COMMIT commit( frame() );
52
54
55 Activate();
56 // Must be done after Activate() so that it gets set into the correct context
57 controls()->ShowCursor( true );
58 controls()->ForceCursorPosition( false );
59 // do not capture or auto-pan until we start placing an item
60
61 PCB_GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() );
62
63 // Add a VIEW_GROUP that serves as a preview for the new item
64 PCB_SELECTION preview;
65 view()->Add( &preview );
66
67 aPlacer->m_board = board();
68 aPlacer->m_frame = frame();
69 aPlacer->m_modifiers = 0;
70
71 auto makeNewItem =
72 [&]( const VECTOR2I& aPosition )
73 {
74 if( frame()->GetModel() )
75 newItem = aPlacer->CreateItem();
76
77 if( newItem )
78 {
79 newItem->SetPosition( aPosition );
80 preview.Add( newItem.get() );
81
82 // footprints have more drawable parts
83 if( FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( newItem.get() ) )
84 fp->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Add, &preview, _1 ) );
85 }
86 };
87
88 if( aOptions & IPO_SINGLE_CLICK )
89 makeNewItem( controls()->GetCursorPosition() );
90
91 auto setCursor =
92 [&]()
93 {
94 if( !newItem )
95 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::PENCIL );
96 else
97 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::PLACE );
98 };
99
100 // Set initial cursor
101 setCursor();
102
103 // Main loop: keep receiving events
104 while( TOOL_EVENT* evt = Wait() )
105 {
106 setCursor();
107
108 grid.SetSnap( false ); // Interactive placement tools need to set their own item snaps
109 grid.SetUseGrid( getView()->GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
110 VECTOR2I cursorPos = grid.BestSnapAnchor( controls()->GetMousePosition(), nullptr );
111
112 aPlacer->m_modifiers = evt->Modifier();
113
114 auto cleanup =
115 [&] ()
116 {
117 newItem = nullptr;
118 preview.Clear();
119 view()->Update( &preview );
120 controls()->SetAutoPan( false );
121 controls()->CaptureCursor( false );
122 controls()->ShowCursor( true );
123 controls()->ForceCursorPosition( false );
124 };
125
126 if( evt->IsCancelInteractive() )
127 {
128 if( aOptions & IPO_SINGLE_CLICK )
129 {
130 cleanup();
131 frame()->PopTool( aTool );
132 break;
133 }
134 else if( newItem )
135 {
136 cleanup();
137 }
138 else
139 {
140 frame()->PopTool( aTool );
141 break;
142 }
143 }
144 else if( evt->IsActivate() )
145 {
146 if( newItem )
147 cleanup();
148
149 if( evt->IsPointEditor() )
150 {
151 // don't exit (the point editor runs in the background)
152 }
153 else if( evt->IsMoveTool() )
154 {
155 // leave ourselves on the stack so we come back after the move
156 break;
157 }
158 else
159 {
160 frame()->PopTool( aTool );
161 break;
162 }
163 }
164 else if( evt->IsClick( BUT_LEFT ) || evt->IsDblClick( BUT_LEFT ) )
165 {
166 if( !newItem )
167 {
168 // create the item if possible
169 makeNewItem( cursorPos );
170
171 // no item created, so wait for another click
172 if( !newItem )
173 continue;
174
175 controls()->CaptureCursor( true );
176 controls()->SetAutoPan( true );
177 }
178 else
179 {
180 BOARD_ITEM* newBoardItem = newItem.release();
181 EDA_ITEM_FLAGS oldFlags = newBoardItem->GetFlags();
182
183 newBoardItem->ClearFlags();
184
185 if( !aPlacer->PlaceItem( newBoardItem, commit ) )
186 {
187 newBoardItem->SetFlags( oldFlags );
188 newItem.reset( newBoardItem );
189 continue;
190 }
191
192 preview.Clear();
193 commit.Push( aCommitMessage );
194
195 controls()->CaptureCursor( false );
196 controls()->SetAutoPan( false );
197 controls()->ShowCursor( true );
198
199 if( !( aOptions & IPO_REPEAT ) )
200 break;
201
202 if( aOptions & IPO_SINGLE_CLICK )
203 makeNewItem( controls()->GetCursorPosition() );
204
205 setCursor();
206 }
207 }
208 else if( evt->IsClick( BUT_RIGHT ) )
209 {
211 }
212 else if( evt->IsAction( &PCB_ACTIONS::trackViaSizeChanged ) )
213 {
215 }
216 else if( newItem && evt->Category() == TC_COMMAND )
217 {
218 /*
219 * Handle any events that can affect the item as we move it around
220 */
221 if( TOOL_EVT_UTILS::IsRotateToolEvt( *evt ) && ( aOptions & IPO_ROTATE ) )
222 {
223 EDA_ANGLE rotationAngle = TOOL_EVT_UTILS::GetEventRotationAngle( *frame(), *evt );
224 newItem->Rotate( newItem->GetPosition(), rotationAngle );
225 view()->Update( &preview );
226 }
227 else if( evt->IsAction( &PCB_ACTIONS::flip ) && ( aOptions & IPO_FLIP ) )
228 {
229 newItem->Flip( newItem->GetPosition(), frame()->GetPcbNewSettings()->m_FlipLeftRight );
230 view()->Update( &preview );
231 }
232 else if( evt->IsAction( &PCB_ACTIONS::properties ) )
233 {
234 frame()->OnEditItemRequest( newItem.get() );
235
236 // Notify other tools of the changes
238 }
239 else if( evt->IsAction( &ACTIONS::refreshPreview ) )
240 {
241 preview.Clear();
242 newItem.reset();
243
244 makeNewItem( cursorPos );
245 aPlacer->SnapItem( newItem.get() );
246 view()->Update( &preview );
247 }
248 else
249 {
250 evt->SetPassEvent();
251 }
252 }
253 else if( newItem && evt->IsMotion() )
254 {
255 // track the cursor
256 newItem->SetPosition( cursorPos );
257 aPlacer->SnapItem( newItem.get() );
258
259 // Show a preview of the item
260 view()->Update( &preview );
261 }
262 else
263 {
264 evt->SetPassEvent();
265 }
266 }
267
268 view()->Remove( &preview );
269 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
270 controls()->SetAutoPan( false );
271 controls()->CaptureCursor( false );
272 controls()->ForceCursorPosition( false );
273}
274
275
277{
278 // A basic context manu. Many (but not all) tools will choose to override this.
279 CONDITIONAL_MENU& ctxMenu = m_menu.GetMenu();
280
281 // cancel current tool goes in main context menu at the top if present
283 ctxMenu.AddSeparator( 1 );
284
285 // Finally, add the standard zoom/grid items
286 getEditFrame<PCB_BASE_FRAME>()->AddStandardSubMenus( m_menu );
287
288 return true;
289}
290
291
293{
294}
295
296
298{
299}
300
301
303{
304 return frame()->GetPcbNewSettings()->m_Display;
305}
306
308{
309 return static_cast<PCB_DRAW_PANEL_GAL*>( frame()->GetCanvas() );
310}
311
312
314{
316
317 return selTool->GetSelection();
318}
319
320
322{
324
325 return selTool->GetSelection();
326}
327
328
330{
332
333 if( frame()->IsType( FRAME_PCB_EDITOR ) )
334 return mgr.GetAppSettings<PCBNEW_SETTINGS>()->m_Use45DegreeLimit;
335 else
336 return mgr.GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>()->m_Use45Limit;
337}
338
339
341{
342 // Base implementation performs no snapping
343}
344
345
347{
348 aCommit.Add( aItem );
349 return true;
350}
351
static TOOL_ACTION cancelInteractive
Definition: actions.h:63
static TOOL_ACTION refreshPreview
Definition: actions.h:137
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Revert the commit by restoring the modified items state.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Notify observers that aItem has been added.
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 SetCurrentCursor(KICURSOR aCursor)
Set the current cursor shape for this panel.
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:126
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:128
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:129
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition: actions.h:267
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:75
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:66
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
DISPLAY_OPTIONS m_Display
static TOOL_ACTION trackViaSizeChanged
Definition: pcb_actions.h:387
static TOOL_ACTION properties
Activation of the edit tool.
Definition: pcb_actions.h:174
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:68
static TOOL_ACTION flip
Flipping of selected objects.
Definition: pcb_actions.h:136
virtual void OnEditItemRequest(BOARD_ITEM *aItem)
Install the corresponding dialog editor for the given item.
PCBNEW_SETTINGS * GetPcbNewSettings() const
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
virtual BOARD_ITEM_CONTAINER * GetModel() const =0
The selection tool: currently supports:
PCB_SELECTION & GetSelection()
KIGFX::PCB_VIEW * view() const
virtual bool Is45Limited() const
Should the tool use its 45° mode option?
PCB_BASE_EDIT_FRAME * frame() const
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:142
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:92
T * GetAppSettings()
Returns a handle to the a given settings by type If the settings have already been loaded,...
virtual void PopTool(const TOOL_EVENT &aEvent)
Pops a tool from the stack.
virtual void PushTool(const TOOL_EVENT &aEvent)
NB: the definition of "tool" is different at the user level.
TOOL_MANAGER * GetManager() const
Return the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:145
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:216
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:36
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:167
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:145
bool PostAction(const std::string &aActionName, T aParam)
Run the specified action after the current action (coroutine) ends.
Definition: tool_manager.h:230
CONDITIONAL_MENU & GetMenu()
Definition: tool_menu.cpp:44
void ShowContextMenu(SELECTION &aSelection)
Helper function to set and immediately show a CONDITIONAL_MENU in concert with the given SELECTION.
Definition: tool_menu.cpp:57
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:1059
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:56
@ BUT_LEFT
Definition: tool_event.h:131
@ BUT_RIGHT
Definition: tool_event.h:132