KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_viewer_tools.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) 2020-2024 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 <pgm_base.h>
26#include <pcbnew_settings.h>
30#include <kiplatform/ui.h>
31#include <pcb_base_frame.h>
33#include <tool/actions.h>
35#include <tools/pcb_actions.h>
37
38
40{
41 // Populate the context menu displayed during the tool (primarily the measure tool)
42 auto activeToolCondition =
43 [ this ] ( const SELECTION& aSel )
44 {
45 return !frame()->ToolStackIsEmpty();
46 };
47
48 CONDITIONAL_MENU& ctxMenu = m_menu.GetMenu();
49
50 // "Cancel" goes at the top of the context menu when a tool is active
51 ctxMenu.AddItem( ACTIONS::cancelInteractive, activeToolCondition, 1 );
52 ctxMenu.AddSeparator( 1 );
53
54 ctxMenu.AddCheckItem( PCB_ACTIONS::toggleHV45Mode, activeToolCondition, 2 );
55 ctxMenu.AddSeparator( activeToolCondition, 2 );
56
58
59 return true;
60}
61
62
64{
65}
66
67
69{
70 bool do_reload_board = true; // reload board flag
71
72 // At EDA_3D_VIEWER_FRAME creation, the current board is loaded, so disable loading
73 // the current board if the 3D frame is not yet created
74 if( frame()->Get3DViewerFrame() == nullptr )
75 do_reload_board = false;
76
78
79 if( frame()->IsType( FRAME_FOOTPRINT_VIEWER )
80 || frame()->IsType( FRAME_FOOTPRINT_WIZARD ) )
81 {
82 // A stronger version of Raise() which promotes the window to its parent's level.
84 }
85
86 // And load or update the current board (if needed)
87 if( do_reload_board )
88 frame()->Update3DView( true, true );
89
90 return 0;
91}
92
93
94template<class T> void Flip( T& aValue )
95{
96 aValue = !aValue;
97}
98
99
101{
104
105 for( FOOTPRINT* fp : board()->Footprints() )
106 {
107 for( PAD* pad : fp->Pads() )
109 }
110
111 canvas()->Refresh();
112
113 return 0;
114}
115
116
118{
121
122 for( FOOTPRINT* fp : board()->Footprints() )
123 {
124 for( PAD* pad : fp->Pads() )
126 }
127
128 canvas()->Refresh();
129
130 return 0;
131}
132
133
135{
138
139 for( FOOTPRINT* fp : board()->Footprints() )
140 {
141 for( BOARD_ITEM* item : fp->GraphicalItems() )
142 {
143 KICAD_T t = item->Type();
144
145 if( t == PCB_SHAPE_T || BaseType( t ) == PCB_DIMENSION_T )
146 view()->Update( item, KIGFX::REPAINT );
147 }
148 }
149
150 for( BOARD_ITEM* item : board()->Drawings() )
151 {
152 KICAD_T t = item->Type();
153
154 if( t == PCB_SHAPE_T || BaseType( t ) == PCB_DIMENSION_T || t == PCB_TARGET_T )
155 view()->Update( item, KIGFX::REPAINT );
156 }
157
158 canvas()->Refresh();
159
160 return 0;
161}
162
163
165{
168
169 for( FOOTPRINT* fp : board()->Footprints() )
170 {
171 for( PCB_FIELD* field : fp->Fields() )
172 {
173 view()->Update( field, KIGFX::REPAINT );
174 }
175
176 for( BOARD_ITEM* item : fp->GraphicalItems() )
177 {
178 if( item->Type() == PCB_TEXT_T )
179 view()->Update( item, KIGFX::REPAINT );
180 }
181 }
182
183 for( BOARD_ITEM* item : board()->Drawings() )
184 {
185 KICAD_T t = item->Type();
186
187 if( t == PCB_TEXT_T || t == PCB_TEXTBOX_T || BaseType( t ) == PCB_DIMENSION_T )
188 view()->Update( item, KIGFX::REPAINT );
189 }
190
191 canvas()->Refresh();
192
193 return 0;
194}
195
196
198
199
201{
202 if( IsFootprintFrame() && !frame()->GetModel() )
203 return 0;
204
205 if( frame()->IsCurrentTool( ACTIONS::measureTool ) )
206 return 0;
207
208 auto& view = *getView();
209 auto& controls = *getViewControls();
210
211 frame()->PushTool( aEvent );
212
213 bool invertXAxis = displayOptions().m_DisplayInvertXAxis;
214 bool invertYAxis = displayOptions().m_DisplayInvertYAxis;
215
216 if( IsFootprintFrame() )
217 {
220 }
221
223 PCB_GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() );
224 bool originSet = false;
225 EDA_UNITS units = frame()->GetUserUnits();
226 KIGFX::PREVIEW::RULER_ITEM ruler( twoPtMgr, pcbIUScale, units, invertXAxis, invertYAxis );
227
228 view.Add( &ruler );
229 view.SetVisible( &ruler, false );
230
231 auto setCursor =
232 [&]()
233 {
234 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::MEASURE );
235 };
236
237 auto cleanup =
238 [&] ()
239 {
240 view.SetVisible( &ruler, false );
241 controls.SetAutoPan( false );
242 controls.CaptureCursor( false );
243 controls.ForceCursorPosition( false );
244 originSet = false;
245 };
246
247 Activate();
248 // Must be done after Activate() so that it gets set into the correct context
249 controls.ShowCursor( true );
250 controls.SetAutoPan( false );
251 controls.CaptureCursor( false );
252 controls.ForceCursorPosition( false );
253
254 // Set initial cursor
255 setCursor();
256
257 while( TOOL_EVENT* evt = Wait() )
258 {
259 setCursor();
260 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
261 grid.SetUseGrid( view.GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
262 VECTOR2I cursorPos = evt->HasPosition() ? evt->Position() : controls.GetMousePosition();
263 cursorPos = grid.BestSnapAnchor( cursorPos, nullptr );
264 controls.ForceCursorPosition( true, cursorPos );
265
266 if( evt->IsCancelInteractive() )
267 {
268 if( originSet )
269 {
270 cleanup();
271 }
272 else
273 {
274 frame()->PopTool( aEvent );
275 break;
276 }
277 }
278 else if( evt->IsActivate() )
279 {
280 if( originSet )
281 cleanup();
282
283 if( evt->IsMoveTool() )
284 {
285 // leave ourselves on the stack so we come back after the move
286 break;
287 }
288 else
289 {
290 frame()->PopTool( aEvent );
291 break;
292 }
293 }
294 // click or drag starts
295 else if( !originSet && ( evt->IsDrag( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) ) )
296 {
297 twoPtMgr.SetOrigin( cursorPos );
298 twoPtMgr.SetEnd( cursorPos );
299
300 controls.CaptureCursor( true );
301 controls.SetAutoPan( true );
302
303 originSet = true;
304 }
305 // second click or mouse up after drag ends
306 else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
307 {
308 originSet = false;
309
310 controls.SetAutoPan( false );
311 controls.CaptureCursor( false );
312 }
313 // move or drag when origin set updates rules
314 else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
315 {
317 bool force45Deg;
318
319 if( frame()->IsType( FRAME_PCB_EDITOR ) )
320 force45Deg = mgr.GetAppSettings<PCBNEW_SETTINGS>()->m_Use45DegreeLimit;
321 else
322 force45Deg = mgr.GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>()->m_Use45Limit;
323
324 twoPtMgr.SetAngleSnap( force45Deg );
325 twoPtMgr.SetEnd( cursorPos );
326
327 view.SetVisible( &ruler, true );
328 view.Update( &ruler, KIGFX::GEOMETRY );
329 }
330 else if( evt->IsAction( &ACTIONS::updateUnits ) )
331 {
332 if( frame()->GetUserUnits() != units )
333 {
334 units = frame()->GetUserUnits();
335 ruler.SwitchUnits( units );
336 view.Update( &ruler, KIGFX::GEOMETRY );
337 canvas()->Refresh();
338 }
339
340 evt->SetPassEvent();
341 }
342 else if( evt->IsAction( &ACTIONS::updatePreferences ) )
343 {
344 invertXAxis = displayOptions().m_DisplayInvertXAxis;
345 invertYAxis = displayOptions().m_DisplayInvertYAxis;
346
347 if( IsFootprintFrame() )
348 {
351 }
352
353 ruler.UpdateDir( invertXAxis, invertYAxis );
354
355 view.Update( &ruler, KIGFX::GEOMETRY );
356 canvas()->Refresh();
357 evt->SetPassEvent();
358 }
359 else if( evt->IsClick( BUT_RIGHT ) )
360 {
362 }
363 else
364 {
365 evt->SetPassEvent();
366 }
367 }
368
369 view.SetVisible( &ruler, false );
370 view.Remove( &ruler );
371
372 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
373 controls.SetAutoPan( false );
374 controls.CaptureCursor( false );
375 controls.ForceCursorPosition( false );
376 return 0;
377}
378
379
381{
383
384 // Display modes
389
391}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
static TOOL_ACTION cancelInteractive
Definition: actions.h:63
static TOOL_ACTION show3DViewer
Definition: actions.h:194
static TOOL_ACTION updatePreferences
Definition: actions.h:207
static TOOL_ACTION updateUnits
Definition: actions.h:181
static TOOL_ACTION measureTool
Definition: actions.h:188
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
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 AddCheckItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Add a checked menu entry to run a TOOL_ACTION on selected items.
Create and handle a window for the 3d viewer connected to a Kiway and a pcbboard.
void AddStandardSubMenus(TOOL_MENU &aMenu)
Construct a "basic" menu for a tool, containing only items that apply to all tools (e....
void SetCurrentCursor(KICURSOR aCursor)
Set the current cursor shape for this panel.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
bool GetGridSnapping() const
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
A drawn ruler item for showing the distance between two points.
Definition: ruler_item.h:42
void SwitchUnits(EDA_UNITS aUnits)
Switch the ruler units.
Definition: ruler_item.h:77
void UpdateDir(bool aFlipX, bool aFlipY)
Definition: ruler_item.h:79
Represent a very simple geometry manager for items that have a start and end point.
void SetOrigin(const VECTOR2I &aOrigin)
< Set the origin of the ruler (the fixed end)
void SetEnd(const VECTOR2I &aEnd)
Set the current end of the rectangle (the end that moves with the cursor.
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:197
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1558
Definition: pad.h:59
static TOOL_ACTION toggleHV45Mode
Definition: pcb_actions.h:512
static TOOL_ACTION padDisplayMode
Definition: pcb_actions.h:326
static TOOL_ACTION graphicsOutlines
Display footprint graphics as outlines.
Definition: pcb_actions.h:484
static TOOL_ACTION textOutlines
Display texts as lines.
Definition: pcb_actions.h:487
static TOOL_ACTION showPadNumbers
Definition: pcb_actions.h:333
virtual PCB_VIEWERS_SETTINGS_BASE * GetViewerSettingsBase() const
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
FOOTPRINT_EDITOR_SETTINGS * GetFootprintEditorSettings() const
EDA_3D_VIEWER_FRAME * CreateAndShow3D_Frame()
Shows the 3D view frame.
virtual void Update3DView(bool aMarkDirty, bool aRefresh, const wxString *aTitle=nullptr)
Update the 3D view, if the viewer is opened by this frame.
VIEWERS_DISPLAY_OPTIONS m_ViewersDisplay
bool Init() override
Init() is called once upon a registration of the tool.
PCBNEW_SETTINGS::DISPLAY_OPTIONS & displayOptions() const
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
PCB_BASE_FRAME * frame() const
int PadDisplayMode(const TOOL_EVENT &aEvent)
BOARD * board() const
KIGFX::PCB_VIEW * view() const
int MeasureTool(const TOOL_EVENT &aEvent)
int TextOutlines(const TOOL_EVENT &aEvent)
PCB_DRAW_PANEL_GAL * canvas() const
bool IsFootprintFrame() const
int Show3DViewer(const TOOL_EVENT &aEvent)
Show the 3D viewer.
int GraphicOutlines(const TOOL_EVENT &aEvent)
int ShowPadNumbers(const TOOL_EVENT &aEvent)
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:142
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.
bool ToolStackIsEmpty()
Definition: tools_holder.h:125
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition: tool_base.cpp:42
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
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
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.
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
EDA_UNITS GetUserUnits() const
Declaration of the eda_3d_viewer class.
EDA_UNITS
Definition: eda_units.h:46
@ FRAME_PCB_EDITOR
Definition: frame_type.h:42
@ FRAME_FOOTPRINT_VIEWER
Definition: frame_type.h:45
@ FRAME_FOOTPRINT_WIZARD
Definition: frame_type.h:46
@ REPAINT
Item needs to be redrawn.
Definition: view_item.h:57
@ GEOMETRY
Position or shape has changed.
Definition: view_item.h:54
void ReparentQuasiModal(wxNonOwnedWindow *aWindow)
Move a window's parent to be the top-level window and force the window to be on top.
Definition: gtk/ui.cpp:88
void Flip(T &aValue)
void Flip(T &aValue)
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1059
see class PGM_BASE
@ MD_SHIFT
Definition: tool_event.h:142
@ BUT_LEFT
Definition: tool_event.h:131
@ BUT_RIGHT
Definition: tool_event.h:132
constexpr KICAD_T BaseType(const KICAD_T aType)
Return the underlying type of the given type.
Definition: typeinfo.h:248
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:93
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:106
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition: typeinfo.h:100