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 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "pcb_tool_base.h"
21
22#include <tool/tool_manager.h>
23#include <board_commit.h>
26#include <footprint.h>
27#include <pcb_draw_panel_gal.h>
28#include <pgm_base.h>
30#include <pcbnew_settings.h>
34#include <tools/pcb_actions.h>
37#include <view/view_controls.h>
38
39#include "pcb_track.h"
41
42
44 const wxString& aCommitMessage, int aOptions )
45{
46 using namespace std::placeholders;
47 std::unique_ptr<BOARD_ITEM> newItem;
48
49 TOOL_EVENT originalEvent = aTool; // This can change out from under us when the event loop runs
50 SCOPED_TOOL_PUSHER raii( frame(), originalEvent );
51
52 BOARD_COMMIT commit( frame() );
53
54 LEADER_MODE* angleSnapMode = nullptr;
55 LEADER_MODE savedAngleSnapMode = LEADER_MODE::DIRECT;
56 bool restoreAngleSnapMode = false;
57
58 if( frame()->IsType( FRAME_PCB_EDITOR ) )
59 {
60 angleSnapMode = &GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" )->m_AngleSnapMode;
61 }
62 else if( frame()->IsType( FRAME_FOOTPRINT_EDITOR ) )
63 {
64 angleSnapMode = &GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" )->m_AngleSnapMode;
65 }
66
67 if( !angleSnapMode )
68 {
69 PCB_VIEWERS_SETTINGS_BASE* viewerSettings = frame()->GetViewerSettingsBase();
70
71 if( viewerSettings )
72 angleSnapMode = &viewerSettings->m_ViewersDisplay.m_AngleSnapMode;
73 }
74
75 if( angleSnapMode && *angleSnapMode != LEADER_MODE::DIRECT )
76 {
77 savedAngleSnapMode = *angleSnapMode;
78 *angleSnapMode = LEADER_MODE::DIRECT;
79 restoreAngleSnapMode = true;
81 }
82
84
85 Activate();
86 // Must be done after Activate() so that it gets set into the correct context
87 controls()->ShowCursor( true );
88 controls()->ForceCursorPosition( false );
89 // do not capture or auto-pan until we start placing an item
90
91 PCB_GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() );
92
93 // Add a VIEW_GROUP that serves as a preview for the new item
94 PCB_SELECTION preview;
95 view()->Add( &preview );
96
97 aPlacer->m_board = board();
98 aPlacer->m_frame = frame();
99 aPlacer->m_modifiers = 0;
100
101 auto makeNewItem =
102 [&]( const VECTOR2I& aPosition )
103 {
104 if( frame()->GetModel() )
105 newItem = aPlacer->CreateItem();
106
107 if( newItem )
108 {
109 newItem->SetPosition( aPosition );
110 preview.Add( newItem.get() );
111
112 // footprints have more drawable parts
113 if( FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( newItem.get() ) )
114 {
115 fp->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Add, &preview, _1 ),
117 }
118 }
119 };
120
121 if( aOptions & IPO_SINGLE_CLICK )
122 makeNewItem( controls()->GetCursorPosition() );
123
124 auto setCursor =
125 [&]()
126 {
127 if( !newItem )
128 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::PENCIL );
129 else
130 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::PLACE );
131 };
132
133 auto ensureVisibility =
134 [&]( BOARD_ITEM* aItem )
135 {
136 if( APPEARANCE_CONTROLS* controls = frame()->GetAppearancePanel() )
137 {
138 auto ensureLayerVisible =
139 [&]( int aLayer )
140 {
142 {
143 if( !view()->IsLayerVisible( aLayer ) )
144 {
145 controls->SetLayerVisible( aLayer, true );
146 frame()->GetCanvas()->Refresh();
147 }
148 }
149 else
150 {
151 if( !board()->IsElementVisible( (GAL_LAYER_ID) aLayer ) )
152 {
153 controls->SetObjectVisible( (GAL_LAYER_ID) aLayer, true );
154 frame()->GetCanvas()->Refresh();
155 }
156 }
157 };
158
159 if( aItem->Type() == PCB_POINT_T )
160 {
161 ensureLayerVisible( LAYER_POINTS );
162 }
163 else if( aItem->Type() == PCB_VIA_T )
164 {
165 ensureLayerVisible( LAYER_VIAS );
166
167 switch( static_cast<PCB_VIA*>( aItem )->GetViaType() )
168 {
169 case VIATYPE::BURIED: ensureLayerVisible( LAYER_VIA_BURIED ); break;
170 case VIATYPE::BLIND: ensureLayerVisible( LAYER_VIA_BLIND ); break;
171 case VIATYPE::MICROVIA: ensureLayerVisible( LAYER_VIA_MICROVIA ); break;
172 default: break;
173 }
174 }
175 else if( aItem->Type() == PCB_PAD_T )
176 {
177 ensureLayerVisible( LAYER_PADS );
178 }
179 else
180 {
181 ensureLayerVisible( aItem->GetLayer() );
182 }
183 }
184 };
185
186 // Set initial cursor
187 setCursor();
188
189 // Main loop: keep receiving events
190 while( TOOL_EVENT* evt = Wait() )
191 {
192 setCursor();
193
194 grid.SetSnap( false ); // Interactive placement tools need to set their own item snaps
195 grid.SetUseGrid( getView()->GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
196 VECTOR2I cursorPos = controls()->GetMousePosition();
197
198 if( !evt->IsActivate() && !evt->IsCancelInteractive() )
199 cursorPos = grid.ResolveSnap( cursorPos, nullptr ).position;
200 else
201 grid.FullReset();
202
203 aPlacer->m_modifiers = evt->Modifier();
204
205 auto cleanup =
206 [&] ()
207 {
208 newItem = nullptr;
209 preview.Clear();
210 view()->Update( &preview );
211 controls()->SetAutoPan( false );
212 controls()->CaptureCursor( false );
213 controls()->ShowCursor( true );
214 controls()->ForceCursorPosition( false );
215 };
216
217 if( evt->IsCancelInteractive() )
218 {
219 if( aOptions & IPO_SINGLE_CLICK )
220 {
221 cleanup();
222 break;
223 }
224 else if( newItem )
225 {
226 cleanup();
227 }
228 else
229 {
230 break;
231 }
232 }
233 else if( evt->IsActivate() )
234 {
235 if( newItem )
236 cleanup();
237
238 if( evt->IsPointEditor() )
239 {
240 // don't exit (the point editor runs in the background)
241 continue;
242 }
243
244 if( evt->IsMoveTool() )
245 {
246 // Make sure we come back after the move tool is done
247 frame()->PushTool( originalEvent );
248 }
249
250 break;
251 }
252 else if( evt->IsClick( BUT_LEFT ) || evt->IsDblClick( BUT_LEFT )
253 || evt->IsAction( &ACTIONS::cursorClick ) || evt->IsAction( &ACTIONS::cursorDblClick ) )
254 {
255 if( !newItem )
256 {
257 // create the item if possible
258 makeNewItem( cursorPos );
259
260 // no item created, so wait for another click
261 if( !newItem )
262 continue;
263
264 ensureVisibility( newItem.get() );
265 controls()->CaptureCursor( true );
266 controls()->SetAutoPan( true );
267 }
268 else
269 {
270 BOARD_ITEM* newBoardItem = newItem.release();
271 EDA_ITEM_FLAGS oldFlags = newBoardItem->GetFlags();
272
273 ensureVisibility( newBoardItem );
274 newBoardItem->ClearFlags();
275
276 if( !aPlacer->PlaceItem( newBoardItem, commit ) )
277 {
278 newBoardItem->SetFlags( oldFlags );
279 newItem.reset( newBoardItem );
280 continue;
281 }
282
283 preview.Clear();
284 commit.Push( aCommitMessage );
285
286 controls()->CaptureCursor( false );
287 controls()->SetAutoPan( false );
288 controls()->ShowCursor( true );
289
290 if( !( aOptions & IPO_REPEAT ) )
291 break;
292
293 if( aOptions & IPO_SINGLE_CLICK )
294 makeNewItem( controls()->GetCursorPosition() );
295
296 setCursor();
297 }
298 }
299 else if( evt->IsClick( BUT_RIGHT ) )
300 {
301 m_menu->ShowContextMenu( selection() );
302 }
303 else if( evt->IsAction( &PCB_ACTIONS::trackViaSizeChanged ) )
304 {
305 m_toolMgr->PostAction( ACTIONS::refreshPreview );
306 }
307 else if( newItem && evt->Category() == TC_COMMAND )
308 {
309 /*
310 * Handle any events that can affect the item as we move it around
311 */
312 if( TOOL_EVT_UTILS::IsRotateToolEvt( *evt ) && ( aOptions & IPO_ROTATE ) )
313 {
314 EDA_ANGLE rotationAngle = TOOL_EVT_UTILS::GetEventRotationAngle( *frame(), *evt );
315 newItem->Rotate( newItem->GetPosition(), rotationAngle );
316 view()->Update( &preview );
317 }
318 else if( evt->IsAction( &PCB_ACTIONS::flip ) && ( aOptions & IPO_FLIP ) )
319 {
320 newItem->Flip( newItem->GetPosition(), frame()->GetPcbNewSettings()->m_FlipDirection );
321 view()->Update( &preview );
322 }
323 else if( evt->IsAction( &PCB_ACTIONS::properties ) )
324 {
325 frame()->OnEditItemRequest( newItem.get() );
326
327 // Notify other tools of the changes
329 }
330 else if( evt->IsAction( &ACTIONS::refreshPreview ) )
331 {
332 preview.Clear();
333 newItem.reset();
334
335 makeNewItem( cursorPos );
336 aPlacer->SnapItem( newItem.get() );
337 view()->Update( &preview );
338 }
339 else
340 {
341 evt->SetPassEvent();
342 }
343 }
344 else if( newItem && ( evt->IsMotion()
345 || evt->IsAction( &ACTIONS::refreshPreview ) ) )
346 {
347 // track the cursor
348 newItem->SetPosition( cursorPos );
349 aPlacer->SnapItem( newItem.get() );
350
351 // Show a preview of the item
352 view()->Update( &preview );
353 }
354 else
355 {
356 evt->SetPassEvent();
357 }
358 }
359
360 view()->Remove( &preview );
361 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
362 controls()->SetAutoPan( false );
363 controls()->CaptureCursor( false );
364 controls()->ForceCursorPosition( false );
365
366 if( restoreAngleSnapMode )
367 {
368 *angleSnapMode = savedAngleSnapMode;
370 }
371}
372
373
375{
376 // A basic context manu. Many (but not all) tools will choose to override this.
377 CONDITIONAL_MENU& ctxMenu = m_menu->GetMenu();
378
379 // cancel current tool goes in main context menu at the top if present
381 ctxMenu.AddSeparator( 1 );
382
383 // Finally, add the standard zoom/grid items
384 getEditFrame<PCB_BASE_FRAME>()->AddStandardSubMenus( *m_menu.get() );
385
386 return true;
387}
388
389
391{
392}
393
394
398
399
401{
402 return frame<PCB_BASE_FRAME>()->GetPcbNewSettings()->m_Display;
403}
404
406{
407 return static_cast<PCB_DRAW_PANEL_GAL*>( frame<PCB_BASE_FRAME>()->GetCanvas() );
408}
409
410
412{
413 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
414
415 return selTool->GetSelection();
416}
417
418
420{
421 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
422
423 return selTool->GetSelection();
424}
425
426
431
433{
435}
436
438{
439 if( frame<PCB_BASE_FRAME>()->IsType( FRAME_PCB_EDITOR ) )
440 return GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" )->m_AngleSnapMode;
441 else
442 return GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" )->m_AngleSnapMode;
443}
444
445
447{
448 if( frame<PCB_BASE_FRAME>()->IsType( FRAME_PCB_EDITOR ) )
449 return GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" )->m_AutoConstraints;
450 else
451 return GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" )->m_AutoConstraints;
452}
453
454
456{
457 // Base implementation performs no snapping
458}
459
460
462{
463 aCommit.Add( aItem );
464 return true;
465}
static TOOL_ACTION cancelInteractive
Definition actions.h:69
static TOOL_ACTION cursorDblClick
Definition actions.h:178
static TOOL_ACTION cursorClick
Definition actions.h:177
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:221
static TOOL_ACTION refreshPreview
Definition actions.h:156
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:99
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition commit.h:74
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:158
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition eda_item.h:160
EDA_ITEM_FLAGS GetFlags() const
Definition eda_item.h:167
static const TOOL_EVENT SelectedItemsModified
Selected item had a property changed (except movement)
Definition actions.h:354
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:87
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Add a VIEW_ITEM to the view.
Definition pcb_view.cpp:53
virtual void Remove(VIEW_ITEM *aItem) override
Remove a VIEW_ITEM from the view.
Definition pcb_view.cpp:70
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 VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const =0
Return the current mouse pointer position.
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.
static TOOL_ACTION trackViaSizeChanged
static TOOL_ACTION properties
Activation of the edit tool.
static TOOL_ACTION angleSnapModeChanged
Notification event when angle mode changes.
static TOOL_ACTION flip
Flipping of selected objects.
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?
LEADER_MODE GetAngleSnapMode() const
Get the current angle snapping mode.
bool GetAutoConstraints() const
Should drawing tools author constraints automatically?
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...
virtual bool Is90Limited() const
Should the tool limit drawing to horizontal and vertical only?
const PCB_SELECTION & selection() const
VIEWERS_DISPLAY_OPTIONS m_ViewersDisplay
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
virtual void Add(EDA_ITEM *aItem)
A null aItem is ignored; the selection never holds null members.
Definition selection.cpp:38
virtual void Clear() override
Remove all the stored items from the group.
Definition selection.h:97
TOOL_MANAGER * GetManager() const
Return the instance of TOOL_MANAGER that takes care of the tool.
Definition tool_base.h:142
T * getEditFrame() const
Return the application window object, casted to requested user type.
Definition tool_base.h:182
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
KIGFX::VIEW * getView() const
Return the instance of KIGFX::VIEW object used in the application.
Definition tool_base.cpp:34
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:74
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 RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
@ PLACE
Definition cursors.h:95
@ ARROW
Definition cursors.h:43
@ PENCIL
Definition cursors.h:49
@ NO_RECURSE
Definition eda_item.h:52
std::uint32_t EDA_ITEM_FLAGS
@ FRAME_PCB_EDITOR
Definition frame_type.h:38
@ FRAME_FOOTPRINT_EDITOR
Definition frame_type.h:39
LEADER_MODE
The kind of the leader line.
@ DIRECT
Unconstrained point-to-point.
@ DEG90
90 Degree only
GAL_LAYER_ID
GAL layers are "virtual" layers, i.e.
Definition layer_ids.h:224
@ LAYER_POINTS
PCB reference/manual snap points visibility.
Definition layer_ids.h:317
@ LAYER_PADS
Meta control for all pads opacity/visibility (color ignored).
Definition layer_ids.h:288
@ LAYER_VIA_BURIED
Draw blind vias.
Definition layer_ids.h:231
@ LAYER_VIA_BLIND
Draw micro vias.
Definition layer_ids.h:230
@ LAYER_VIA_MICROVIA
Definition layer_ids.h:229
@ LAYER_VIAS
Meta control for all vias opacity/visibility.
Definition layer_ids.h:228
@ PCB_LAYER_ID_COUNT
Definition layer_ids.h:167
EDA_ANGLE GetEventRotationAngle(const PCB_BASE_EDIT_FRAME &aFrame, const TOOL_EVENT &aEvent)
Function getEventRotationAngle()
bool IsRotateToolEvt(const TOOL_EVENT &aEvt)
Function isRotateToolEvt()
see class PGM_BASE
T * GetAppSettings(const char *aFilename)
virtual void SnapItem(BOARD_ITEM *aItem)
PCB_BASE_EDIT_FRAME * m_frame
virtual std::unique_ptr< BOARD_ITEM > CreateItem()=0
virtual bool PlaceItem(BOARD_ITEM *aItem, BOARD_COMMIT &aCommit)
@ TC_COMMAND
Definition tool_event.h:53
@ BUT_LEFT
Definition tool_event.h:128
@ BUT_RIGHT
Definition tool_event.h:129
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:79
@ PCB_POINT_T
class PCB_POINT, a 0-dimensional point
Definition typeinfo.h:105
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708