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
29#include <footprint.h>
32#include <kiplatform/ui.h>
33#include <pad.h>
34#include <pcb_base_frame.h>
37#include <pgm_base.h>
39#include <tool/actions.h>
40#include <tool/tool_manager.h>
42#include <tools/pcb_actions.h>
43
44
46{
47 // Populate the context menu displayed during the tool (primarily the measure tool)
48 auto activeToolCondition =
49 [this] ( const SELECTION& aSel )
50 {
51 return !frame()->ToolStackIsEmpty();
52 };
53
54 CONDITIONAL_MENU& ctxMenu = m_menu->GetMenu();
55
56 // "Cancel" goes at the top of the context menu when a tool is active
57 if( !m_isDefaultTool )
58 {
59 ctxMenu.AddItem( ACTIONS::cancelInteractive, activeToolCondition, 1 );
60 ctxMenu.AddSeparator( 1 );
61 }
62
63 ctxMenu.AddSeparator( activeToolCondition, 2 );
64
65 ctxMenu.AddItem( ACTIONS::copy, activeToolCondition, 3 );
66 ctxMenu.AddSeparator( activeToolCondition, 3 );
67
68 frame()->AddStandardSubMenus( *m_menu.get() );
69
70 return true;
71}
72
73
75{
76}
77
78
80{
81 bool do_reload_board = true; // reload board flag
82
83 // At EDA_3D_VIEWER_FRAME creation, the current board is loaded, so disable loading
84 // the current board if the 3D frame is not yet created
85 if( frame()->Get3DViewerFrame() == nullptr )
86 do_reload_board = false;
87
89
90 if( frame()->IsType( FRAME_FOOTPRINT_VIEWER )
91 || frame()->IsType( FRAME_FOOTPRINT_WIZARD ) )
92 {
93 // A stronger version of Raise() which promotes the window to its parent's level.
94 KIPLATFORM::UI::ReparentModal( draw3DFrame );
95 }
96
97 // And load or update the current board (if needed)
98 if( do_reload_board )
99 frame()->Update3DView( true, true );
100
101 return 0;
102}
103
104
105template<class T> void Flip( T& aValue )
106{
107 aValue = !aValue;
108}
109
110
112{
113 if( frame()->IsType( FRAME_PCB_EDITOR ) )
114 {
115 PCBNEW_SETTINGS* settings = GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" );
116
117 switch( settings->m_AngleSnapMode )
118 {
121 default: settings->m_AngleSnapMode = LEADER_MODE::DIRECT; break;
122 }
123 }
124 else if( frame()->IsType( FRAME_FOOTPRINT_EDITOR ) )
125 {
127
128 switch( settings->m_AngleSnapMode )
129 {
132 default: settings->m_AngleSnapMode = LEADER_MODE::DIRECT; break;
133 }
134 }
135 else
136 {
138
139 switch( mode )
140 {
141 case LEADER_MODE::DIRECT: mode = LEADER_MODE::DEG45; break;
142 case LEADER_MODE::DEG45: mode = LEADER_MODE::DEG90; break;
143 default: mode = LEADER_MODE::DIRECT; break;
144 }
145 }
146
148
149 // Notify other tools/UI (toolbars) that the angle snap mode has changed
151
152 return 0;
153}
154
155
157{
160
161 for( FOOTPRINT* fp : board()->Footprints() )
162 {
163 for( PAD* pad : fp->Pads() )
165 }
166
167 canvas()->Refresh();
168
169 return 0;
170}
171
172
174{
177
178 for( FOOTPRINT* fp : board()->Footprints() )
179 {
180 for( PAD* pad : fp->Pads() )
182 }
183
184 canvas()->Refresh();
185
186 return 0;
187}
188
189
191{
194
195 for( FOOTPRINT* fp : board()->Footprints() )
196 {
197 for( BOARD_ITEM* item : fp->GraphicalItems() )
198 {
199 KICAD_T t = item->Type();
200
201 if( t == PCB_SHAPE_T || BaseType( t ) == PCB_DIMENSION_T )
202 view()->Update( item, KIGFX::REPAINT );
203 }
204 }
205
206 for( BOARD_ITEM* item : board()->Drawings() )
207 {
208 KICAD_T t = item->Type();
209
210 if( t == PCB_SHAPE_T || BaseType( t ) == PCB_DIMENSION_T || t == PCB_TARGET_T )
211 view()->Update( item, KIGFX::REPAINT );
212 }
213
214 canvas()->Refresh();
215
216 return 0;
217}
218
219
221{
224
225 for( FOOTPRINT* fp : board()->Footprints() )
226 {
227 for( PCB_FIELD* field : fp->GetFields() )
228 {
229 wxCHECK2( field, continue );
230
231 view()->Update( field, KIGFX::REPAINT );
232 }
233
234 for( BOARD_ITEM* item : fp->GraphicalItems() )
235 {
236 if( item->Type() == PCB_TEXT_T )
237 view()->Update( item, KIGFX::REPAINT );
238 }
239 }
240
241 for( BOARD_ITEM* item : board()->Drawings() )
242 {
243 KICAD_T t = item->Type();
244
245 if( t == PCB_TEXT_T || t == PCB_TEXTBOX_T || BaseType( t ) == PCB_DIMENSION_T )
246 view()->Update( item, KIGFX::REPAINT );
247 }
248
249 canvas()->Refresh();
250
251 return 0;
252}
253
254
256
257
259{
260 if( IsFootprintFrame() && !frame()->GetModel() )
261 return 0;
262
263 if( frame()->IsCurrentTool( ACTIONS::measureTool ) )
264 return 0;
265
266 auto& view = *getView();
267 auto& controls = *getViewControls();
268
269 frame()->PushTool( aEvent );
270
271 bool invertXAxis = displayOptions().m_DisplayInvertXAxis;
272 bool invertYAxis = displayOptions().m_DisplayInvertYAxis;
273
274 if( IsFootprintFrame() )
275 {
278 }
279
281 PCB_GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() );
282 bool originSet = false;
283 EDA_UNITS units = frame()->GetUserUnits();
284 KIGFX::PREVIEW::RULER_ITEM ruler( twoPtMgr, pcbIUScale, units, invertXAxis, invertYAxis );
285
286 view.Add( &ruler );
287 view.SetVisible( &ruler, false );
288
289 auto setCursor =
290 [&]()
291 {
293 };
294
295 auto cleanup =
296 [&] ()
297 {
298 view.SetVisible( &ruler, false );
299 controls.SetAutoPan( false );
300 controls.CaptureCursor( false );
301 controls.ForceCursorPosition( false );
302 originSet = false;
303 };
304
305 Activate();
306 // Must be done after Activate() so that it gets set into the correct context
307 controls.ShowCursor( true );
308 controls.SetAutoPan( false );
309 controls.CaptureCursor( false );
310 controls.ForceCursorPosition( false );
311
312 // Set initial cursor
313 setCursor();
314
315 while( TOOL_EVENT* evt = Wait() )
316 {
317 setCursor();
318 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
319 grid.SetUseGrid( view.GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
320 VECTOR2I cursorPos = evt->HasPosition() ? evt->Position() : controls.GetMousePosition();
321
322 if( !evt->IsActivate() && !evt->IsCancelInteractive() )
323 {
324 // If we are switching, the canvas may not be valid any more
325 cursorPos = grid.BestSnapAnchor( cursorPos, nullptr );
326 controls.ForceCursorPosition( true, cursorPos );
327 }
328 else
329 {
330 grid.FullReset();
331 }
332
333 if( evt->IsCancelInteractive() )
334 {
335 if( originSet )
336 {
337 cleanup();
338 }
339 else if( m_isDefaultTool )
340 {
341 view.SetVisible( &ruler, false );
342 }
343 else
344 {
345 frame()->PopTool( aEvent );
346 break;
347 }
348 }
349 else if( evt->IsActivate() )
350 {
351 if( originSet )
352 cleanup();
353
354 if( evt->IsMoveTool() )
355 {
356 // leave ourselves on the stack so we come back after the move
357 break;
358 }
359 else
360 {
361 frame()->PopTool( aEvent );
362 break;
363 }
364 }
365 // click or drag starts
366 else if( !originSet && ( evt->IsDrag( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) ) )
367 {
368 twoPtMgr.SetOrigin( cursorPos );
369 twoPtMgr.SetEnd( cursorPos );
370
371 controls.CaptureCursor( true );
372 controls.SetAutoPan( true );
373
374 originSet = true;
375 }
376 // second click or mouse up after drag ends
377 else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
378 {
379 originSet = false;
380
381 controls.SetAutoPan( false );
382 controls.CaptureCursor( false );
383 }
384 // move or drag when origin set updates rules
385 else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
386 {
387 // The measurement tool always measures in a direct line; holding Shift
388 // constrains to 45° increments for convenience.
389 twoPtMgr.SetAngleSnap( evt->Modifier( MD_SHIFT ) ? LEADER_MODE::DEG45
391 twoPtMgr.SetEnd( cursorPos );
392
393 view.SetVisible( &ruler, true );
394 view.Update( &ruler, KIGFX::GEOMETRY );
395 }
396 else if( evt->IsAction( &ACTIONS::updateUnits ) )
397 {
398 if( frame()->GetUserUnits() != units )
399 {
400 units = frame()->GetUserUnits();
401 ruler.SwitchUnits( units );
402 view.Update( &ruler, KIGFX::GEOMETRY );
403 canvas()->Refresh();
404 }
405
406 evt->SetPassEvent();
407 }
408 else if( evt->IsAction( &ACTIONS::updatePreferences ) )
409 {
410 invertXAxis = displayOptions().m_DisplayInvertXAxis;
411 invertYAxis = displayOptions().m_DisplayInvertYAxis;
412
413 if( IsFootprintFrame() )
414 {
417 }
418
419 ruler.UpdateDir( invertXAxis, invertYAxis );
420
421 view.Update( &ruler, KIGFX::GEOMETRY );
422 canvas()->Refresh();
423 evt->SetPassEvent();
424 }
425 else if( evt->IsAction( &ACTIONS::copy ) )
426 {
427 if( originSet )
428 {
429 wxArrayString cursorStrings = ruler.GetDimensionStrings();
430 wxString text = wxJoin( cursorStrings, '\n' );
431
432 if( wxTheClipboard->Open() )
433 {
434 wxTheClipboard->SetData( new wxTextDataObject( text ) );
435 wxTheClipboard->Close();
436 }
437 }
438 }
439 else if( evt->IsClick( BUT_RIGHT ) )
440 {
441 m_menu->ShowContextMenu();
442 }
443 else
444 {
445 evt->SetPassEvent();
446 }
447 }
448
449 view.SetVisible( &ruler, false );
450 view.Remove( &ruler );
451
453 controls.SetAutoPan( false );
454 controls.CaptureCursor( false );
455 controls.ForceCursorPosition( false );
456 return 0;
457}
458
459
461{
463
464 // Toggle the setting
465 if( cfg )
467
468 return 0;
469}
470
471
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
static TOOL_ACTION cancelInteractive
Definition actions.h:72
static TOOL_ACTION show3DViewer
Definition actions.h:258
static TOOL_ACTION updatePreferences
Definition actions.h:276
static TOOL_ACTION copy
Definition actions.h:78
static TOOL_ACTION updateUnits
Definition actions.h:207
static TOOL_ACTION measureTool
Definition actions.h:252
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
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.
virtual APP_SETTINGS_BASE * config() const
Return the settings object used in SaveSettings(), and is overloaded in KICAD_MANAGER_FRAME.
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
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
A drawn ruler item for showing the distance between two points.
Definition ruler_item.h:46
wxArrayString GetDimensionStrings() const
Get the strings for the dimensions of the ruler.
void SwitchUnits(EDA_UNITS aUnits)
Switch the ruler units.
Definition ruler_item.h:92
void UpdateDir(bool aFlipX, bool aFlipY)
Definition ruler_item.h:94
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.
Definition pad.h:55
LEADER_MODE m_AngleSnapMode
static TOOL_ACTION padDisplayMode
static TOOL_ACTION angleSnapModeChanged
Notification event when angle mode changes.
static TOOL_ACTION lineModeNext
Cycle through angle modes.
static TOOL_ACTION graphicsOutlines
Display footprint graphics as outlines.
static TOOL_ACTION fpAutoZoom
static TOOL_ACTION textOutlines
Display texts as lines.
static TOOL_ACTION showPadNumbers
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)
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 NextLineMode(const TOOL_EVENT &aEvent)
int GraphicOutlines(const TOOL_EVENT &aEvent)
int ShowPadNumbers(const TOOL_EVENT &aEvent)
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()
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:171
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
@ MEASURE
Definition cursors.h:68
@ ARROW
Definition cursors.h:46
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
LEADER_MODE
The kind of the leader line.
@ DEG45
45 Degree only
@ DIRECT
Unconstrained point-to-point.
@ DEG90
90 Degree only
@ 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:168
void Flip(T &aValue)
void Flip(T &aValue)
int GetUserUnits()
Return the currently selected user unit value for the interface.
see class PGM_BASE
T * GetAppSettings(const char *aFilename)
@ 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:254
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:107
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition typeinfo.h:100
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695