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 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, 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
25
26#include <wx/clipbrd.h>
27
31#include <kiplatform/ui.h>
32#include <pcb_base_frame.h>
34#include <pgm_base.h>
36#include <tool/actions.h>
38#include <tools/pcb_actions.h>
39
40
42{
43 // Populate the context menu displayed during the tool (primarily the measure tool)
44 auto activeToolCondition =
45 [ this ] ( const SELECTION& aSel )
46 {
47 return !frame()->ToolStackIsEmpty();
48 };
49
50 CONDITIONAL_MENU& ctxMenu = m_menu->GetMenu();
51
52 // "Cancel" goes at the top of the context menu when a tool is active
53 if( !m_isDefaultTool )
54 {
55 ctxMenu.AddItem( ACTIONS::cancelInteractive, activeToolCondition, 1 );
56 ctxMenu.AddSeparator( 1 );
57 }
58
59 ctxMenu.AddCheckItem( PCB_ACTIONS::toggleHV45Mode, activeToolCondition, 2 );
60 ctxMenu.AddSeparator( activeToolCondition, 2 );
61
62 ctxMenu.AddItem( ACTIONS::copy, activeToolCondition, 3 );
63 ctxMenu.AddSeparator( activeToolCondition, 3 );
64
65 frame()->AddStandardSubMenus( *m_menu.get() );
66
67 return true;
68}
69
70
72{
73}
74
75
77{
78 bool do_reload_board = true; // reload board flag
79
80 // At EDA_3D_VIEWER_FRAME creation, the current board is loaded, so disable loading
81 // the current board if the 3D frame is not yet created
82 if( frame()->Get3DViewerFrame() == nullptr )
83 do_reload_board = false;
84
86
87 if( frame()->IsType( FRAME_FOOTPRINT_VIEWER )
88 || frame()->IsType( FRAME_FOOTPRINT_WIZARD ) )
89 {
90 // A stronger version of Raise() which promotes the window to its parent's level.
91 KIPLATFORM::UI::ReparentModal( draw3DFrame );
92 }
93
94 // And load or update the current board (if needed)
95 if( do_reload_board )
96 frame()->Update3DView( true, true );
97
98 return 0;
99}
100
101
102template<class T> void Flip( T& aValue )
103{
104 aValue = !aValue;
105}
106
107
109{
111
112 if( frame()->IsType( FRAME_PCB_EDITOR ) )
114 else if( frame()->IsType( FRAME_FOOTPRINT_EDITOR ) )
116 else
117 Flip( frame()->GetViewerSettingsBase()->m_ViewersDisplay.m_Use45Limit );
118
120
121 return 0;
122}
123
124
126{
129
130 for( FOOTPRINT* fp : board()->Footprints() )
131 {
132 for( PAD* pad : fp->Pads() )
134 }
135
136 canvas()->Refresh();
137
138 return 0;
139}
140
141
143{
146
147 for( FOOTPRINT* fp : board()->Footprints() )
148 {
149 for( PAD* pad : fp->Pads() )
151 }
152
153 canvas()->Refresh();
154
155 return 0;
156}
157
158
160{
163
164 for( FOOTPRINT* fp : board()->Footprints() )
165 {
166 for( BOARD_ITEM* item : fp->GraphicalItems() )
167 {
168 KICAD_T t = item->Type();
169
170 if( t == PCB_SHAPE_T || BaseType( t ) == PCB_DIMENSION_T )
171 view()->Update( item, KIGFX::REPAINT );
172 }
173 }
174
175 for( BOARD_ITEM* item : board()->Drawings() )
176 {
177 KICAD_T t = item->Type();
178
179 if( t == PCB_SHAPE_T || BaseType( t ) == PCB_DIMENSION_T || t == PCB_TARGET_T )
180 view()->Update( item, KIGFX::REPAINT );
181 }
182
183 canvas()->Refresh();
184
185 return 0;
186}
187
188
190{
193
194 for( FOOTPRINT* fp : board()->Footprints() )
195 {
196 for( PCB_FIELD* field : fp->GetFields() )
197 {
198 view()->Update( field, KIGFX::REPAINT );
199 }
200
201 for( BOARD_ITEM* item : fp->GraphicalItems() )
202 {
203 if( item->Type() == PCB_TEXT_T )
204 view()->Update( item, KIGFX::REPAINT );
205 }
206 }
207
208 for( BOARD_ITEM* item : board()->Drawings() )
209 {
210 KICAD_T t = item->Type();
211
212 if( t == PCB_TEXT_T || t == PCB_TEXTBOX_T || BaseType( t ) == PCB_DIMENSION_T )
213 view()->Update( item, KIGFX::REPAINT );
214 }
215
216 canvas()->Refresh();
217
218 return 0;
219}
220
221
223
224
226{
227 if( IsFootprintFrame() && !frame()->GetModel() )
228 return 0;
229
230 if( frame()->IsCurrentTool( ACTIONS::measureTool ) )
231 return 0;
232
233 auto& view = *getView();
234 auto& controls = *getViewControls();
235
236 frame()->PushTool( aEvent );
237
238 bool invertXAxis = displayOptions().m_DisplayInvertXAxis;
239 bool invertYAxis = displayOptions().m_DisplayInvertYAxis;
240
241 if( IsFootprintFrame() )
242 {
245 }
246
248 PCB_GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() );
249 bool originSet = false;
250 EDA_UNITS units = frame()->GetUserUnits();
251 KIGFX::PREVIEW::RULER_ITEM ruler( twoPtMgr, pcbIUScale, units, invertXAxis, invertYAxis );
252
253 view.Add( &ruler );
254 view.SetVisible( &ruler, false );
255
256 auto setCursor =
257 [&]()
258 {
259 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::MEASURE );
260 };
261
262 auto cleanup =
263 [&] ()
264 {
265 view.SetVisible( &ruler, false );
266 controls.SetAutoPan( false );
267 controls.CaptureCursor( false );
268 controls.ForceCursorPosition( false );
269 originSet = false;
270 };
271
272 Activate();
273 // Must be done after Activate() so that it gets set into the correct context
274 controls.ShowCursor( true );
275 controls.SetAutoPan( false );
276 controls.CaptureCursor( false );
277 controls.ForceCursorPosition( false );
278
279 // Set initial cursor
280 setCursor();
281
282 while( TOOL_EVENT* evt = Wait() )
283 {
284 setCursor();
285 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
286 grid.SetUseGrid( view.GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
287 VECTOR2I cursorPos = evt->HasPosition() ? evt->Position() : controls.GetMousePosition();
288 cursorPos = grid.BestSnapAnchor( cursorPos, nullptr );
289 controls.ForceCursorPosition( true, cursorPos );
290
291 if( evt->IsCancelInteractive() )
292 {
293 if( originSet )
294 {
295 cleanup();
296 }
297 else if( m_isDefaultTool )
298 {
299 view.SetVisible( &ruler, false );
300 view.Remove( &ruler );
301 }
302 else
303 {
304 frame()->PopTool( aEvent );
305 break;
306 }
307 }
308 else if( evt->IsActivate() )
309 {
310 if( originSet )
311 cleanup();
312
313 if( evt->IsMoveTool() )
314 {
315 // leave ourselves on the stack so we come back after the move
316 break;
317 }
318 else
319 {
320 frame()->PopTool( aEvent );
321 break;
322 }
323 }
324 // click or drag starts
325 else if( !originSet && ( evt->IsDrag( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) ) )
326 {
327 twoPtMgr.SetOrigin( cursorPos );
328 twoPtMgr.SetEnd( cursorPos );
329
330 controls.CaptureCursor( true );
331 controls.SetAutoPan( true );
332
333 originSet = true;
334 }
335 // second click or mouse up after drag ends
336 else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
337 {
338 originSet = false;
339
340 controls.SetAutoPan( false );
341 controls.CaptureCursor( false );
342 }
343 // move or drag when origin set updates rules
344 else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
345 {
347 bool force45Deg;
348
349 if( frame()->IsType( FRAME_PCB_EDITOR ) )
350 force45Deg = mgr.GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" )->m_Use45DegreeLimit;
351 else if( frame()->IsType( FRAME_FOOTPRINT_EDITOR ) )
352 force45Deg = mgr.GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" )->m_Use45Limit;
353 else
355
356 twoPtMgr.SetAngleSnap( force45Deg );
357 twoPtMgr.SetEnd( cursorPos );
358
359 view.SetVisible( &ruler, true );
360 view.Update( &ruler, KIGFX::GEOMETRY );
361 }
362 else if( evt->IsAction( &ACTIONS::updateUnits ) )
363 {
364 if( frame()->GetUserUnits() != units )
365 {
366 units = frame()->GetUserUnits();
367 ruler.SwitchUnits( units );
368 view.Update( &ruler, KIGFX::GEOMETRY );
369 canvas()->Refresh();
370 }
371
372 evt->SetPassEvent();
373 }
374 else if( evt->IsAction( &ACTIONS::updatePreferences ) )
375 {
376 invertXAxis = displayOptions().m_DisplayInvertXAxis;
377 invertYAxis = displayOptions().m_DisplayInvertYAxis;
378
379 if( IsFootprintFrame() )
380 {
383 }
384
385 ruler.UpdateDir( invertXAxis, invertYAxis );
386
387 view.Update( &ruler, KIGFX::GEOMETRY );
388 canvas()->Refresh();
389 evt->SetPassEvent();
390 }
391 else if( evt->IsAction( &ACTIONS::copy ) )
392 {
393 if( originSet )
394 {
395 wxArrayString cursorStrings = ruler.GetDimensionStrings();
396 wxString text = wxJoin( cursorStrings, '\n' );
397
398 if( wxTheClipboard->Open() )
399 {
400 wxTheClipboard->SetData( new wxTextDataObject( text ) );
401 wxTheClipboard->Close();
402 }
403 }
404 }
405 else if( evt->IsClick( BUT_RIGHT ) )
406 {
407 m_menu->ShowContextMenu();
408 }
409 else
410 {
411 evt->SetPassEvent();
412 }
413 }
414
415 view.SetVisible( &ruler, false );
416 view.Remove( &ruler );
417
418 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
419 controls.SetAutoPan( false );
420 controls.CaptureCursor( false );
421 controls.ForceCursorPosition( false );
422 return 0;
423}
424
425
427{
429
430 // Toggle the setting
431 if( cfg )
433
434 return 0;
435}
436
437
439{
440 // clang-format off
442
444
445 // Display modes
450
452
454 // clang-format on
455}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:110
static TOOL_ACTION cancelInteractive
Definition: actions.h:72
static TOOL_ACTION show3DViewer
Definition: actions.h:253
static TOOL_ACTION updatePreferences
Definition: actions.h:268
static TOOL_ACTION copy
Definition: actions.h:78
static TOOL_ACTION updateUnits
Definition: actions.h:207
static TOOL_ACTION measureTool
Definition: actions.h:247
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:78
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.
virtual APP_SETTINGS_BASE * config() const
Return the settings object used in SaveSettings(), and is overloaded in KICAD_MANAGER_FRAME.
bool IsType(FRAME_T aType) const
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: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
A drawn ruler item for showing the distance between two points.
Definition: ruler_item.h:45
wxArrayString GetDimensionStrings() const
Get the strings for the dimensions of the ruler.
Definition: ruler_item.cpp:375
void SwitchUnits(EDA_UNITS aUnits)
Switch the ruler units.
Definition: ruler_item.h:91
void UpdateDir(bool aFlipX, bool aFlipY)
Definition: ruler_item.h:93
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:198
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1600
Definition: pad.h:54
static TOOL_ACTION toggleHV45Mode
Definition: pcb_actions.h:522
static TOOL_ACTION padDisplayMode
Definition: pcb_actions.h:317
static TOOL_ACTION graphicsOutlines
Display footprint graphics as outlines.
Definition: pcb_actions.h:494
static TOOL_ACTION fpAutoZoom
Definition: pcb_actions.h:325
static TOOL_ACTION textOutlines
Display texts as lines.
Definition: pcb_actions.h:497
static TOOL_ACTION showPadNumbers
Definition: pcb_actions.h:324
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()
Show 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.
virtual void UpdateStatusBar() override
Update the status bar information.
bool m_FootprintViewerAutoZoomOnSelect
true to use automatic zoom on fp selection
VIEWERS_DISPLAY_OPTIONS m_ViewersDisplay
bool Init() override
Init() is called once upon a registration of the tool.
bool m_isDefaultTool
Indicates no selection tool is present in the current toolset.
int FootprintAutoZoom(const TOOL_EVENT &aEvent)
Automatically zoom to fit on footprints.
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)
int ToggleHV45Mode(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:125
T * GetAppSettings(const char *aFilename)
Return a handle to the a given settings by type.
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:44
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:220
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:38
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:168
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).
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.
EDA_UNITS GetUserUnits() const
Declaration of the eda_3d_viewer class.
EDA_UNITS
Definition: eda_units.h:48
@ FRAME_PCB_EDITOR
Definition: frame_type.h:42
@ FRAME_FOOTPRINT_VIEWER
Definition: frame_type.h:45
@ FRAME_FOOTPRINT_WIZARD
Definition: frame_type.h:46
@ FRAME_FOOTPRINT_EDITOR
Definition: frame_type.h:43
@ REPAINT
Item needs to be redrawn.
Definition: view_item.h:58
@ GEOMETRY
Position or shape has changed.
Definition: view_item.h:55
void ReparentModal(wxNonOwnedWindow *aWindow)
Move a window's parent to be the top-level window and force the window to be on top.
Definition: wxgtk/ui.cpp:145
void Flip(T &aValue)
void Flip(T &aValue)
int GetUserUnits()
Return the currently selected user unit value for the interface.
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:1071
see class PGM_BASE
@ MD_SHIFT
Definition: tool_event.h:143
@ BUT_LEFT
Definition: tool_event.h:132
@ BUT_RIGHT
Definition: tool_event.h:133
constexpr KICAD_T BaseType(const KICAD_T aType)
Return the underlying type of the given type.
Definition: typeinfo.h:250
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