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#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 }
87 };
88
89 if( aOptions & IPO_SINGLE_CLICK )
90 makeNewItem( controls()->GetCursorPosition() );
91
92 auto setCursor =
93 [&]()
94 {
95 if( !newItem )
96 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::PENCIL );
97 else
98 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::PLACE );
99 };
100
101 // Set initial cursor
102 setCursor();
103
104 // Main loop: keep receiving events
105 while( TOOL_EVENT* evt = Wait() )
106 {
107 setCursor();
108
109 grid.SetSnap( false ); // Interactive placement tools need to set their own item snaps
110 grid.SetUseGrid( getView()->GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
111 VECTOR2I cursorPos = grid.BestSnapAnchor( controls()->GetMousePosition(), nullptr );
112
113 aPlacer->m_modifiers = evt->Modifier();
114
115 auto cleanup =
116 [&] ()
117 {
118 newItem = nullptr;
119 preview.Clear();
120 view()->Update( &preview );
121 controls()->SetAutoPan( false );
122 controls()->CaptureCursor( false );
123 controls()->ShowCursor( true );
124 controls()->ForceCursorPosition( false );
125 };
126
127 if( evt->IsCancelInteractive() )
128 {
129 if( aOptions & IPO_SINGLE_CLICK )
130 {
131 cleanup();
132 frame()->PopTool( aTool );
133 break;
134 }
135 else if( newItem )
136 {
137 cleanup();
138 }
139 else
140 {
141 frame()->PopTool( aTool );
142 break;
143 }
144 }
145 else if( evt->IsActivate() )
146 {
147 if( newItem )
148 cleanup();
149
150 if( evt->IsPointEditor() )
151 {
152 // don't exit (the point editor runs in the background)
153 }
154 else if( evt->IsMoveTool() )
155 {
156 // leave ourselves on the stack so we come back after the move
157 break;
158 }
159 else
160 {
161 frame()->PopTool( aTool );
162 break;
163 }
164 }
165 else if( evt->IsClick( BUT_LEFT ) || evt->IsDblClick( BUT_LEFT ) )
166 {
167 if( !newItem )
168 {
169 // create the item if possible
170 makeNewItem( cursorPos );
171
172 // no item created, so wait for another click
173 if( !newItem )
174 continue;
175
176 controls()->CaptureCursor( true );
177 controls()->SetAutoPan( true );
178 }
179 else
180 {
181 BOARD_ITEM* newBoardItem = newItem.release();
182 EDA_ITEM_FLAGS oldFlags = newBoardItem->GetFlags();
183
184 newBoardItem->ClearFlags();
185
186 if( !aPlacer->PlaceItem( newBoardItem, commit ) )
187 {
188 newBoardItem->SetFlags( oldFlags );
189 newItem.reset( newBoardItem );
190 continue;
191 }
192
193 preview.Clear();
194 commit.Push( aCommitMessage );
195
196 controls()->CaptureCursor( false );
197 controls()->SetAutoPan( false );
198 controls()->ShowCursor( true );
199
200 if( !( aOptions & IPO_REPEAT ) )
201 break;
202
203 if( aOptions & IPO_SINGLE_CLICK )
204 makeNewItem( controls()->GetCursorPosition() );
205
206 setCursor();
207 }
208 }
209 else if( evt->IsClick( BUT_RIGHT ) )
210 {
212 }
213 else if( evt->IsAction( &PCB_ACTIONS::trackViaSizeChanged ) )
214 {
216 }
217 else if( newItem && evt->Category() == TC_COMMAND )
218 {
219 /*
220 * Handle any events that can affect the item as we move it around
221 */
222 if( TOOL_EVT_UTILS::IsRotateToolEvt( *evt ) && ( aOptions & IPO_ROTATE ) )
223 {
224 EDA_ANGLE rotationAngle = TOOL_EVT_UTILS::GetEventRotationAngle( *frame(), *evt );
225 newItem->Rotate( newItem->GetPosition(), rotationAngle );
226 view()->Update( &preview );
227 }
228 else if( evt->IsAction( &PCB_ACTIONS::flip ) && ( aOptions & IPO_FLIP ) )
229 {
230 newItem->Flip( newItem->GetPosition(), frame()->GetPcbNewSettings()->m_FlipLeftRight );
231 view()->Update( &preview );
232 }
233 else if( evt->IsAction( &PCB_ACTIONS::properties ) )
234 {
235 frame()->OnEditItemRequest( newItem.get() );
236
237 // Notify other tools of the changes
239 }
240 else if( evt->IsAction( &ACTIONS::refreshPreview ) )
241 {
242 preview.Clear();
243 newItem.reset();
244
245 makeNewItem( cursorPos );
246 aPlacer->SnapItem( newItem.get() );
247 view()->Update( &preview );
248 }
249 else
250 {
251 evt->SetPassEvent();
252 }
253 }
254 else if( newItem && evt->IsMotion() )
255 {
256 // track the cursor
257 newItem->SetPosition( cursorPos );
258 aPlacer->SnapItem( newItem.get() );
259
260 // Show a preview of the item
261 view()->Update( &preview );
262 }
263 else
264 {
265 evt->SetPassEvent();
266 }
267 }
268
269 view()->Remove( &preview );
270 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
271 controls()->SetAutoPan( false );
272 controls()->CaptureCursor( false );
273 controls()->ForceCursorPosition( false );
274}
275
276
278{
279 // A basic context manu. Many (but not all) tools will choose to override this.
280 CONDITIONAL_MENU& ctxMenu = m_menu.GetMenu();
281
282 // cancel current tool goes in main context menu at the top if present
284 ctxMenu.AddSeparator( 1 );
285
286 // Finally, add the standard zoom/grid items
287 getEditFrame<PCB_BASE_FRAME>()->AddStandardSubMenus( m_menu );
288
289 return true;
290}
291
292
294{
295}
296
297
299{
300}
301
302
304{
305 return frame()->GetPcbNewSettings()->m_Display;
306}
307
309{
310 return static_cast<PCB_DRAW_PANEL_GAL*>( frame()->GetCanvas() );
311}
312
313
315{
317
318 return selTool->GetSelection();
319}
320
321
323{
325
326 return selTool->GetSelection();
327}
328
329
331{
333
334 if( frame()->IsType( FRAME_PCB_EDITOR ) )
335 return mgr.GetAppSettings<PCBNEW_SETTINGS>()->m_Use45DegreeLimit;
336 else
337 return mgr.GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>()->m_Use45Limit;
338}
339
340
342{
343 // Base implementation performs no snapping
344}
345
346
348{
349 aCommit.Add( aItem );
350 return true;
351}
352
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:391
static TOOL_ACTION properties
Activation of the edit tool.
Definition: pcb_actions.h:176
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:93
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:217
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:150
bool PostAction(const std::string &aActionName, T aParam)
Run the specified action after the current action (coroutine) ends.
Definition: tool_manager.h:235
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