KiCad PCB EDA Suite
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-2022 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>
29#include <kiplatform/ui.h>
30#include <pcb_base_frame.h>
32#include <tool/actions.h>
34#include <tools/pcb_actions.h>
36
37
39{
40 // Populate the context menu displayed during the tool (primarily the measure tool)
41 auto activeToolCondition =
42 [ this ] ( const SELECTION& aSel )
43 {
44 return !frame()->ToolStackIsEmpty();
45 };
46
47 CONDITIONAL_MENU& ctxMenu = m_menu.GetMenu();
48
49 // "Cancel" goes at the top of the context menu when a tool is active
50 ctxMenu.AddItem( ACTIONS::cancelInteractive, activeToolCondition, 1 );
51 ctxMenu.AddSeparator( 1 );
52
54
55 return true;
56}
57
58
60{
61}
62
63
65{
67
68 if( frame()->IsType( FRAME_FOOTPRINT_VIEWER )
70 || frame()->IsType( FRAME_FOOTPRINT_WIZARD ) )
71 {
72 // A stronger version of Raise() which promotes the window to its parent's level.
74 }
75
76 // And load or update the current board
77 frame()->Update3DView( true, true );
78
79 return 0;
80}
81
82
83template<class T> void Flip( T& aValue )
84{
85 aValue = !aValue;
86}
87
88
90{
93
94 for( FOOTPRINT* fp : board()->Footprints() )
95 {
96 for( PAD* pad : fp->Pads() )
98 }
99
100 canvas()->Refresh();
101
102 return 0;
103}
104
105
107{
110
111 for( FOOTPRINT* fp : board()->Footprints() )
112 {
113 for( PAD* pad : fp->Pads() )
115 }
116
117 canvas()->Refresh();
118
119 return 0;
120}
121
122
124{
127
128 for( FOOTPRINT* fp : board()->Footprints() )
129 {
130 for( BOARD_ITEM* item : fp->GraphicalItems() )
131 {
132 KICAD_T t = item->Type();
133
134 if( t == PCB_FP_SHAPE_T || BaseType( t ) == PCB_DIMENSION_T )
135 view()->Update( item, KIGFX::REPAINT );
136 }
137 }
138
139 for( BOARD_ITEM* item : board()->Drawings() )
140 {
141 KICAD_T t = item->Type();
142
143 if( t == PCB_SHAPE_T || BaseType( t ) == PCB_DIMENSION_T || t == PCB_TARGET_T )
144 view()->Update( item, KIGFX::REPAINT );
145 }
146
147 canvas()->Refresh();
148
149 return 0;
150}
151
152
154{
157
158 for( FOOTPRINT* fp : board()->Footprints() )
159 {
160 view()->Update( &fp->Reference(), KIGFX::REPAINT );
161 view()->Update( &fp->Value(), KIGFX::REPAINT );
162
163 for( BOARD_ITEM* item : fp->GraphicalItems() )
164 {
165 if( item->Type() == PCB_FP_TEXT_T )
166 view()->Update( item, KIGFX::REPAINT );
167 }
168 }
169
170 for( BOARD_ITEM* item : board()->Drawings() )
171 {
172 KICAD_T t = item->Type();
173
174 if( t == PCB_TEXT_T || t == PCB_TEXTBOX_T || BaseType( t ) == PCB_DIMENSION_T )
175 view()->Update( item, KIGFX::REPAINT );
176 }
177
178 canvas()->Refresh();
179
180 return 0;
181}
182
183
185
186
188{
189 if( IsFootprintFrame() && !frame()->GetModel() )
190 return 0;
191
192 if( frame()->IsCurrentTool( ACTIONS::measureTool ) )
193 return 0;
194
195 auto& view = *getView();
196 auto& controls = *getViewControls();
197
198 frame()->PushTool( aEvent );
199
201 PCB_GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() );
202 bool originSet = false;
203 EDA_UNITS units = frame()->GetUserUnits();
204 KIGFX::PREVIEW::RULER_ITEM ruler( twoPtMgr, pcbIUScale, units,
205 displayOptions().m_DisplayInvertXAxis,
206 displayOptions().m_DisplayInvertYAxis );
207
208 view.Add( &ruler );
209 view.SetVisible( &ruler, false );
210
211 auto setCursor =
212 [&]()
213 {
215 };
216
217 auto cleanup =
218 [&] ()
219 {
220 view.SetVisible( &ruler, false );
221 controls.SetAutoPan( false );
222 controls.CaptureCursor( false );
223 controls.ForceCursorPosition( false );
224 originSet = false;
225 };
226
227 Activate();
228 // Must be done after Activate() so that it gets set into the correct context
229 controls.ShowCursor( true );
230 controls.SetAutoPan( false );
231 controls.CaptureCursor( false );
232 controls.ForceCursorPosition( false );
233
234 // Set initial cursor
235 setCursor();
236
237 while( TOOL_EVENT* evt = Wait() )
238 {
239 setCursor();
240 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
241 grid.SetUseGrid( view.GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
242 const VECTOR2I cursorPos = grid.BestSnapAnchor( controls.GetMousePosition(), nullptr );
243 controls.ForceCursorPosition( true, cursorPos );
244
245 if( evt->IsCancelInteractive() )
246 {
247 if( originSet )
248 {
249 cleanup();
250 }
251 else
252 {
253 frame()->PopTool( aEvent );
254 break;
255 }
256 }
257 else if( evt->IsActivate() )
258 {
259 if( originSet )
260 cleanup();
261
262 if( evt->IsMoveTool() )
263 {
264 // leave ourselves on the stack so we come back after the move
265 break;
266 }
267 else
268 {
269 frame()->PopTool( aEvent );
270 break;
271 }
272 }
273 // click or drag starts
274 else if( !originSet && ( evt->IsDrag( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) ) )
275 {
276 twoPtMgr.SetOrigin( cursorPos );
277 twoPtMgr.SetEnd( cursorPos );
278
279 controls.CaptureCursor( true );
280 controls.SetAutoPan( true );
281
282 originSet = true;
283 }
284 // second click or mouse up after drag ends
285 else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
286 {
287 originSet = false;
288
289 controls.SetAutoPan( false );
290 controls.CaptureCursor( false );
291 }
292 // move or drag when origin set updates rules
293 else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
294 {
295 SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
296 bool force45Deg;
297
298 if( frame()->IsType( FRAME_PCB_EDITOR ) )
299 force45Deg = mgr.GetAppSettings<PCBNEW_SETTINGS>()->m_Use45DegreeLimit;
300 else
301 force45Deg = mgr.GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>()->m_Use45Limit;
302
303 twoPtMgr.SetAngleSnap( force45Deg );
304 twoPtMgr.SetEnd( cursorPos );
305
306 view.SetVisible( &ruler, true );
307 view.Update( &ruler, KIGFX::GEOMETRY );
308 }
309 else if( evt->IsAction( &ACTIONS::updateUnits ) )
310 {
311 if( frame()->GetUserUnits() != units )
312 {
313 units = frame()->GetUserUnits();
314 ruler.SwitchUnits( units );
315 view.Update( &ruler, KIGFX::GEOMETRY );
316 canvas()->Refresh();
317 }
318
319 evt->SetPassEvent();
320 }
321 else if( evt->IsAction( &ACTIONS::updatePreferences ) )
322 {
323 ruler.UpdateDir( displayOptions().m_DisplayInvertXAxis,
324 displayOptions().m_DisplayInvertYAxis );
325
326 view.Update( &ruler, KIGFX::GEOMETRY );
327 canvas()->Refresh();
328 evt->SetPassEvent();
329 }
330 else if( evt->IsClick( BUT_RIGHT ) )
331 {
333 }
334 else
335 {
336 evt->SetPassEvent();
337 }
338 }
339
340 view.SetVisible( &ruler, false );
341 view.Remove( &ruler );
342
344 controls.SetAutoPan( false );
345 controls.CaptureCursor( false );
346 controls.ForceCursorPosition( false );
347 return 0;
348}
349
350
352{
354
355 // Display modes
360
362}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
static TOOL_ACTION cancelInteractive
Definition: actions.h:63
static TOOL_ACTION show3DViewer
Definition: actions.h:163
static TOOL_ACTION updatePreferences
Definition: actions.h:175
static TOOL_ACTION updateUnits
Definition: actions.h:151
static TOOL_ACTION measureTool
Definition: actions.h:158
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:70
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.
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
Update the board display after modifying it by a python script (note: it is automatically called by a...
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:92
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Add a VIEW_ITEM to the view.
Definition: pcb_view.cpp:58
virtual void Remove(VIEW_ITEM *aItem) override
Remove a VIEW_ITEM from the view.
Definition: pcb_view.cpp:75
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:195
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1529
Definition: pad.h:60
static TOOL_ACTION padDisplayMode
Definition: pcb_actions.h:280
static TOOL_ACTION graphicsOutlines
Display footprint graphics as outlines.
Definition: pcb_actions.h:438
static TOOL_ACTION textOutlines
Display texts as lines.
Definition: pcb_actions.h:441
static TOOL_ACTION showPadNumbers
Definition: pcb_actions.h:287
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.
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)
T * GetAppSettings(bool aLoadNow=true)
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:128
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:215
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:156
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
EDA_UNITS
Definition: eda_units.h:43
@ FRAME_PCB_EDITOR
Definition: frame_type.h:40
@ FRAME_FOOTPRINT_VIEWER_MODAL
Definition: frame_type.h:43
@ FRAME_FOOTPRINT_VIEWER
Definition: frame_type.h:42
@ FRAME_FOOTPRINT_WIZARD
Definition: frame_type.h:44
@ REPAINT
Item needs to be redrawn.
Definition: view_item.h:52
@ GEOMETRY
Position or shape has changed.
Definition: view_item.h:49
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:65
SGLIB_API S3DMODEL * GetModel(SCENEGRAPH *aNode)
Function GetModel creates an S3DMODEL representation of aNode (raw data, no transforms)
Definition: ifsg_api.cpp:338
void Flip(T &aValue)
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:111
@ MD_SHIFT
Definition: tool_event.h:138
@ BUT_LEFT
Definition: tool_event.h:127
@ BUT_RIGHT
Definition: tool_event.h:128
constexpr KICAD_T BaseType(const KICAD_T aType)
Return the underlying type of the given type.
Definition: typeinfo.h:253
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_FP_SHAPE_T
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:94
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:91
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:90
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:111
@ PCB_FP_TEXT_T
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition: typeinfo.h:105