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>
35#include <pgm_base.h>
37#include <tool/actions.h>
38#include <tool/tool_manager.h>
40#include <tools/pcb_actions.h>
41
42
44{
45 // Populate the context menu displayed during the tool (primarily the measure tool)
46 auto activeToolCondition =
47 [this] ( const SELECTION& aSel )
48 {
49 return !frame()->ToolStackIsEmpty();
50 };
51
52 CONDITIONAL_MENU& ctxMenu = m_menu->GetMenu();
53
54 // "Cancel" goes at the top of the context menu when a tool is active
55 if( !m_isDefaultTool )
56 {
57 ctxMenu.AddItem( ACTIONS::cancelInteractive, activeToolCondition, 1 );
58 ctxMenu.AddSeparator( 1 );
59 }
60
61 ctxMenu.AddSeparator( activeToolCondition, 2 );
62
63 ctxMenu.AddItem( ACTIONS::copy, activeToolCondition, 3 );
64 ctxMenu.AddSeparator( activeToolCondition, 3 );
65
66 frame()->AddStandardSubMenus( *m_menu.get() );
67
68 return true;
69}
70
71
73{
74}
75
76
78{
79 bool do_reload_board = true; // reload board flag
80
81 // At EDA_3D_VIEWER_FRAME creation, the current board is loaded, so disable loading
82 // the current board if the 3D frame is not yet created
83 if( frame()->Get3DViewerFrame() == nullptr )
84 do_reload_board = false;
85
87
88 if( frame()->IsType( FRAME_FOOTPRINT_VIEWER )
89 || frame()->IsType( FRAME_FOOTPRINT_WIZARD ) )
90 {
91 // A stronger version of Raise() which promotes the window to its parent's level.
92 KIPLATFORM::UI::ReparentModal( draw3DFrame );
93 }
94
95 // And load or update the current board (if needed)
96 if( do_reload_board )
97 frame()->Update3DView( true, true );
98
99 return 0;
100}
101
102
103template<class T> void Flip( T& aValue )
104{
105 aValue = !aValue;
106}
107
108
110{
111 if( frame()->IsType( FRAME_PCB_EDITOR ) )
112 {
113 PCBNEW_SETTINGS* settings = GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" );
114
115 switch( settings->m_AngleSnapMode )
116 {
119 default: settings->m_AngleSnapMode = LEADER_MODE::DIRECT; break;
120 }
121 }
122 else if( frame()->IsType( FRAME_FOOTPRINT_EDITOR ) )
123 {
125
126 switch( settings->m_AngleSnapMode )
127 {
130 default: settings->m_AngleSnapMode = LEADER_MODE::DIRECT; break;
131 }
132 }
133 else
134 {
136
137 switch( mode )
138 {
139 case LEADER_MODE::DIRECT: mode = LEADER_MODE::DEG45; break;
140 case LEADER_MODE::DEG45: mode = LEADER_MODE::DEG90; break;
141 default: mode = LEADER_MODE::DIRECT; break;
142 }
143 }
144
146
147 // Notify other tools/UI (toolbars) that the angle snap mode has changed
149
150 return 0;
151}
152
153
155{
158
159 for( FOOTPRINT* fp : board()->Footprints() )
160 {
161 for( PAD* pad : fp->Pads() )
163 }
164
165 canvas()->Refresh();
166
167 return 0;
168}
169
170
172{
175
176 for( FOOTPRINT* fp : board()->Footprints() )
177 {
178 for( PAD* pad : fp->Pads() )
180 }
181
182 canvas()->Refresh();
183
184 return 0;
185}
186
187
189{
192
193 for( FOOTPRINT* fp : board()->Footprints() )
194 {
195 for( BOARD_ITEM* item : fp->GraphicalItems() )
196 {
197 KICAD_T t = item->Type();
198
199 if( t == PCB_SHAPE_T || BaseType( t ) == PCB_DIMENSION_T )
200 view()->Update( item, KIGFX::REPAINT );
201 }
202 }
203
204 for( BOARD_ITEM* item : board()->Drawings() )
205 {
206 KICAD_T t = item->Type();
207
208 if( t == PCB_SHAPE_T || BaseType( t ) == PCB_DIMENSION_T || t == PCB_TARGET_T )
209 view()->Update( item, KIGFX::REPAINT );
210 }
211
212 canvas()->Refresh();
213
214 return 0;
215}
216
217
219{
222
223 for( FOOTPRINT* fp : board()->Footprints() )
224 {
225 for( PCB_FIELD* field : fp->GetFields() )
226 {
227 wxCHECK2( field, continue );
228
229 view()->Update( field, KIGFX::REPAINT );
230 }
231
232 for( BOARD_ITEM* item : fp->GraphicalItems() )
233 {
234 if( item->Type() == PCB_TEXT_T )
235 view()->Update( item, KIGFX::REPAINT );
236 }
237 }
238
239 for( BOARD_ITEM* item : board()->Drawings() )
240 {
241 KICAD_T t = item->Type();
242
243 if( t == PCB_TEXT_T || t == PCB_TEXTBOX_T || BaseType( t ) == PCB_DIMENSION_T )
244 view()->Update( item, KIGFX::REPAINT );
245 }
246
247 canvas()->Refresh();
248
249 return 0;
250}
251
252
254
255
257{
258 if( IsFootprintFrame() && !frame()->GetModel() )
259 return 0;
260
261 if( frame()->IsCurrentTool( ACTIONS::measureTool ) )
262 return 0;
263
264 auto& view = *getView();
265 auto& controls = *getViewControls();
266
267 frame()->PushTool( aEvent );
268
269 bool invertXAxis = displayOptions().m_DisplayInvertXAxis;
270 bool invertYAxis = displayOptions().m_DisplayInvertYAxis;
271
272 if( IsFootprintFrame() )
273 {
276 }
277
279 PCB_GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() );
280 bool originSet = false;
281 EDA_UNITS units = frame()->GetUserUnits();
282 KIGFX::PREVIEW::RULER_ITEM ruler( twoPtMgr, pcbIUScale, units, invertXAxis, invertYAxis );
283
284 view.Add( &ruler );
285 view.SetVisible( &ruler, false );
286
287 auto setCursor =
288 [&]()
289 {
291 };
292
293 auto cleanup =
294 [&] ()
295 {
296 view.SetVisible( &ruler, false );
297 controls.SetAutoPan( false );
298 controls.CaptureCursor( false );
299 controls.ForceCursorPosition( false );
300 originSet = false;
301 };
302
303 Activate();
304 // Must be done after Activate() so that it gets set into the correct context
305 controls.ShowCursor( true );
306 controls.SetAutoPan( false );
307 controls.CaptureCursor( false );
308 controls.ForceCursorPosition( false );
309
310 // Set initial cursor
311 setCursor();
312
313 while( TOOL_EVENT* evt = Wait() )
314 {
315 setCursor();
316 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
317 grid.SetUseGrid( view.GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
318 VECTOR2I cursorPos = evt->HasPosition() ? evt->Position() : controls.GetMousePosition();
319
320 if( !evt->IsActivate() && !evt->IsCancelInteractive() )
321 {
322 // If we are switching, the canvas may not be valid any more
323 cursorPos = grid.BestSnapAnchor( cursorPos, nullptr );
324 controls.ForceCursorPosition( true, cursorPos );
325 }
326 else
327 {
328 grid.FullReset();
329 }
330
331 if( evt->IsCancelInteractive() )
332 {
333 if( originSet )
334 {
335 cleanup();
336 }
337 else if( m_isDefaultTool )
338 {
339 view.SetVisible( &ruler, false );
340 }
341 else
342 {
343 frame()->PopTool( aEvent );
344 break;
345 }
346 }
347 else if( evt->IsActivate() )
348 {
349 if( originSet )
350 cleanup();
351
352 if( evt->IsMoveTool() )
353 {
354 // leave ourselves on the stack so we come back after the move
355 break;
356 }
357 else
358 {
359 frame()->PopTool( aEvent );
360 break;
361 }
362 }
363 // click or drag starts
364 else if( !originSet && ( evt->IsDrag( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) ) )
365 {
366 twoPtMgr.SetOrigin( cursorPos );
367 twoPtMgr.SetEnd( cursorPos );
368
369 controls.CaptureCursor( true );
370 controls.SetAutoPan( true );
371
372 originSet = true;
373 }
374 // second click or mouse up after drag ends
375 else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
376 {
377 originSet = false;
378
379 controls.SetAutoPan( false );
380 controls.CaptureCursor( false );
381 }
382 // move or drag when origin set updates rules
383 else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
384 {
385 auto snap = LEADER_MODE::DIRECT;
386
387 if( frame()->IsType( FRAME_PCB_EDITOR ) )
388 {
389 snap = GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" )->m_AngleSnapMode;
390 }
391 else if( frame()->IsType( FRAME_FOOTPRINT_EDITOR ) )
392 {
393 snap = GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" )->m_AngleSnapMode;
394 }
395 else
396 {
398 }
399
400 twoPtMgr.SetAngleSnap( snap );
401 twoPtMgr.SetEnd( cursorPos );
402
403 view.SetVisible( &ruler, true );
404 view.Update( &ruler, KIGFX::GEOMETRY );
405 }
406 else if( evt->IsAction( &ACTIONS::updateUnits ) )
407 {
408 if( frame()->GetUserUnits() != units )
409 {
410 units = frame()->GetUserUnits();
411 ruler.SwitchUnits( units );
412 view.Update( &ruler, KIGFX::GEOMETRY );
413 canvas()->Refresh();
414 }
415
416 evt->SetPassEvent();
417 }
418 else if( evt->IsAction( &ACTIONS::updatePreferences ) )
419 {
420 invertXAxis = displayOptions().m_DisplayInvertXAxis;
421 invertYAxis = displayOptions().m_DisplayInvertYAxis;
422
423 if( IsFootprintFrame() )
424 {
427 }
428
429 ruler.UpdateDir( invertXAxis, invertYAxis );
430
431 view.Update( &ruler, KIGFX::GEOMETRY );
432 canvas()->Refresh();
433 evt->SetPassEvent();
434 }
435 else if( evt->IsAction( &ACTIONS::copy ) )
436 {
437 if( originSet )
438 {
439 wxArrayString cursorStrings = ruler.GetDimensionStrings();
440 wxString text = wxJoin( cursorStrings, '\n' );
441
442 if( wxTheClipboard->Open() )
443 {
444 wxTheClipboard->SetData( new wxTextDataObject( text ) );
445 wxTheClipboard->Close();
446 }
447 }
448 }
449 else if( evt->IsClick( BUT_RIGHT ) )
450 {
451 m_menu->ShowContextMenu();
452 }
453 else
454 {
455 evt->SetPassEvent();
456 }
457 }
458
459 view.SetVisible( &ruler, false );
460 view.Remove( &ruler );
461
463 controls.SetAutoPan( false );
464 controls.CaptureCursor( false );
465 controls.ForceCursorPosition( false );
466 return 0;
467}
468
469
471{
473
474 // Toggle the setting
475 if( cfg )
477
478 return 0;
479}
480
481
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:83
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:45
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: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.
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