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 {
211 m_menu->ShowContextMenu( selection() );
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(),
231 frame()->GetPcbNewSettings()->m_FlipDirection );
232 view()->Update( &preview );
233 }
234 else if( evt->IsAction( &PCB_ACTIONS::properties ) )
235 {
236 frame()->OnEditItemRequest( newItem.get() );
237
238 // Notify other tools of the changes
240 }
241 else if( evt->IsAction( &ACTIONS::refreshPreview ) )
242 {
243 preview.Clear();
244 newItem.reset();
245
246 makeNewItem( cursorPos );
247 aPlacer->SnapItem( newItem.get() );
248 view()->Update( &preview );
249 }
250 else
251 {
252 evt->SetPassEvent();
253 }
254 }
255 else if( newItem && evt->IsMotion() )
256 {
257 // track the cursor
258 newItem->SetPosition( cursorPos );
259 aPlacer->SnapItem( newItem.get() );
260
261 // Show a preview of the item
262 view()->Update( &preview );
263 }
264 else
265 {
266 evt->SetPassEvent();
267 }
268 }
269
270 view()->Remove( &preview );
271 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
272 controls()->SetAutoPan( false );
273 controls()->CaptureCursor( false );
274 controls()->ForceCursorPosition( false );
275}
276
277
279{
280 // A basic context manu. Many (but not all) tools will choose to override this.
281 CONDITIONAL_MENU& ctxMenu = m_menu->GetMenu();
282
283 // cancel current tool goes in main context menu at the top if present
285 ctxMenu.AddSeparator( 1 );
286
287 // Finally, add the standard zoom/grid items
288 getEditFrame<PCB_BASE_FRAME>()->AddStandardSubMenus( *m_menu.get() );
289
290 return true;
291}
292
293
295{
296}
297
298
300{
301}
302
303
305{
306 return frame()->GetPcbNewSettings()->m_Display;
307}
308
310{
311 return static_cast<PCB_DRAW_PANEL_GAL*>( frame()->GetCanvas() );
312}
313
314
316{
318
319 return selTool->GetSelection();
320}
321
322
324{
326
327 return selTool->GetSelection();
328}
329
330
332{
334
335 if( frame()->IsType( FRAME_PCB_EDITOR ) )
336 return mgr.GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" )->m_Use45DegreeLimit;
337 else
338 return mgr.GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" )->m_Use45Limit;
339}
340
341
343{
344 // Base implementation performs no snapping
345}
346
347
349{
350 aCommit.Add( aItem );
351 return true;
352}
353
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
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:79
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 SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:127
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:129
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:130
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition: actions.h:299
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:402
static TOOL_ACTION properties
Activation of the edit tool.
Definition: pcb_actions.h:180
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
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: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(const wxString &aFilename)
Returns a handle to the a given settings by type If the settings have already been loaded,...
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:218
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
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:1060
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