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>
39#include <tools/pcb_actions.h>
40
41
43{
44 // Populate the context menu displayed during the tool (primarily the measure tool)
45 auto activeToolCondition =
46 [this] ( const SELECTION& aSel )
47 {
48 return !frame()->ToolStackIsEmpty();
49 };
50
51 CONDITIONAL_MENU& ctxMenu = m_menu->GetMenu();
52
53 // "Cancel" goes at the top of the context menu when a tool is active
54 if( !m_isDefaultTool )
55 {
56 ctxMenu.AddItem( ACTIONS::cancelInteractive, activeToolCondition, 1 );
57 ctxMenu.AddSeparator( 1 );
58 }
59
60 ctxMenu.AddCheckItem( PCB_ACTIONS::toggleHV45Mode, activeToolCondition, 2 );
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 return 0;
148}
149
150
152{
155
156 for( FOOTPRINT* fp : board()->Footprints() )
157 {
158 for( PAD* pad : fp->Pads() )
160 }
161
162 canvas()->Refresh();
163
164 return 0;
165}
166
167
169{
172
173 for( FOOTPRINT* fp : board()->Footprints() )
174 {
175 for( PAD* pad : fp->Pads() )
177 }
178
179 canvas()->Refresh();
180
181 return 0;
182}
183
184
186{
189
190 for( FOOTPRINT* fp : board()->Footprints() )
191 {
192 for( BOARD_ITEM* item : fp->GraphicalItems() )
193 {
194 KICAD_T t = item->Type();
195
196 if( t == PCB_SHAPE_T || BaseType( t ) == PCB_DIMENSION_T )
197 view()->Update( item, KIGFX::REPAINT );
198 }
199 }
200
201 for( BOARD_ITEM* item : board()->Drawings() )
202 {
203 KICAD_T t = item->Type();
204
205 if( t == PCB_SHAPE_T || BaseType( t ) == PCB_DIMENSION_T || t == PCB_TARGET_T )
206 view()->Update( item, KIGFX::REPAINT );
207 }
208
209 canvas()->Refresh();
210
211 return 0;
212}
213
214
216{
219
220 for( FOOTPRINT* fp : board()->Footprints() )
221 {
222 for( PCB_FIELD* field : fp->GetFields() )
223 {
224 view()->Update( field, KIGFX::REPAINT );
225 }
226
227 for( BOARD_ITEM* item : fp->GraphicalItems() )
228 {
229 if( item->Type() == PCB_TEXT_T )
230 view()->Update( item, KIGFX::REPAINT );
231 }
232 }
233
234 for( BOARD_ITEM* item : board()->Drawings() )
235 {
236 KICAD_T t = item->Type();
237
238 if( t == PCB_TEXT_T || t == PCB_TEXTBOX_T || BaseType( t ) == PCB_DIMENSION_T )
239 view()->Update( item, KIGFX::REPAINT );
240 }
241
242 canvas()->Refresh();
243
244 return 0;
245}
246
247
249
250
252{
253 if( IsFootprintFrame() && !frame()->GetModel() )
254 return 0;
255
256 if( frame()->IsCurrentTool( ACTIONS::measureTool ) )
257 return 0;
258
259 auto& view = *getView();
260 auto& controls = *getViewControls();
261
262 frame()->PushTool( aEvent );
263
264 bool invertXAxis = displayOptions().m_DisplayInvertXAxis;
265 bool invertYAxis = displayOptions().m_DisplayInvertYAxis;
266
267 if( IsFootprintFrame() )
268 {
271 }
272
274 PCB_GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() );
275 bool originSet = false;
276 EDA_UNITS units = frame()->GetUserUnits();
277 KIGFX::PREVIEW::RULER_ITEM ruler( twoPtMgr, pcbIUScale, units, invertXAxis, invertYAxis );
278
279 view.Add( &ruler );
280 view.SetVisible( &ruler, false );
281
282 auto setCursor =
283 [&]()
284 {
286 };
287
288 auto cleanup =
289 [&] ()
290 {
291 view.SetVisible( &ruler, false );
292 controls.SetAutoPan( false );
293 controls.CaptureCursor( false );
294 controls.ForceCursorPosition( false );
295 originSet = false;
296 };
297
298 Activate();
299 // Must be done after Activate() so that it gets set into the correct context
300 controls.ShowCursor( true );
301 controls.SetAutoPan( false );
302 controls.CaptureCursor( false );
303 controls.ForceCursorPosition( false );
304
305 // Set initial cursor
306 setCursor();
307
308 while( TOOL_EVENT* evt = Wait() )
309 {
310 setCursor();
311 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
312 grid.SetUseGrid( view.GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
313 VECTOR2I cursorPos = evt->HasPosition() ? evt->Position() : controls.GetMousePosition();
314 cursorPos = grid.BestSnapAnchor( cursorPos, nullptr );
315 controls.ForceCursorPosition( true, cursorPos );
316
317 if( evt->IsCancelInteractive() )
318 {
319 if( originSet )
320 {
321 cleanup();
322 }
323 else if( m_isDefaultTool )
324 {
325 view.SetVisible( &ruler, false );
326 }
327 else
328 {
329 frame()->PopTool( aEvent );
330 break;
331 }
332 }
333 else if( evt->IsActivate() )
334 {
335 if( originSet )
336 cleanup();
337
338 if( evt->IsMoveTool() )
339 {
340 // leave ourselves on the stack so we come back after the move
341 break;
342 }
343 else
344 {
345 frame()->PopTool( aEvent );
346 break;
347 }
348 }
349 // click or drag starts
350 else if( !originSet && ( evt->IsDrag( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) ) )
351 {
352 twoPtMgr.SetOrigin( cursorPos );
353 twoPtMgr.SetEnd( cursorPos );
354
355 controls.CaptureCursor( true );
356 controls.SetAutoPan( true );
357
358 originSet = true;
359 }
360 // second click or mouse up after drag ends
361 else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
362 {
363 originSet = false;
364
365 controls.SetAutoPan( false );
366 controls.CaptureCursor( false );
367 }
368 // move or drag when origin set updates rules
369 else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
370 {
371 auto snap = LEADER_MODE::DIRECT;
372
373 if( frame()->IsType( FRAME_PCB_EDITOR ) )
374 {
375 snap = GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" )->m_AngleSnapMode;
376 }
377 else if( frame()->IsType( FRAME_FOOTPRINT_EDITOR ) )
378 {
379 snap = GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" )->m_AngleSnapMode;
380 }
381 else
382 {
384 }
385
386 twoPtMgr.SetAngleSnap( snap );
387 twoPtMgr.SetEnd( cursorPos );
388
389 view.SetVisible( &ruler, true );
390 view.Update( &ruler, KIGFX::GEOMETRY );
391 }
392 else if( evt->IsAction( &ACTIONS::updateUnits ) )
393 {
394 if( frame()->GetUserUnits() != units )
395 {
396 units = frame()->GetUserUnits();
397 ruler.SwitchUnits( units );
398 view.Update( &ruler, KIGFX::GEOMETRY );
399 canvas()->Refresh();
400 }
401
402 evt->SetPassEvent();
403 }
404 else if( evt->IsAction( &ACTIONS::updatePreferences ) )
405 {
406 invertXAxis = displayOptions().m_DisplayInvertXAxis;
407 invertYAxis = displayOptions().m_DisplayInvertYAxis;
408
409 if( IsFootprintFrame() )
410 {
413 }
414
415 ruler.UpdateDir( invertXAxis, invertYAxis );
416
417 view.Update( &ruler, KIGFX::GEOMETRY );
418 canvas()->Refresh();
419 evt->SetPassEvent();
420 }
421 else if( evt->IsAction( &ACTIONS::copy ) )
422 {
423 if( originSet )
424 {
425 wxArrayString cursorStrings = ruler.GetDimensionStrings();
426 wxString text = wxJoin( cursorStrings, '\n' );
427
428 if( wxTheClipboard->Open() )
429 {
430 wxTheClipboard->SetData( new wxTextDataObject( text ) );
431 wxTheClipboard->Close();
432 }
433 }
434 }
435 else if( evt->IsClick( BUT_RIGHT ) )
436 {
437 m_menu->ShowContextMenu();
438 }
439 else
440 {
441 evt->SetPassEvent();
442 }
443 }
444
445 view.SetVisible( &ruler, false );
446 view.Remove( &ruler );
447
449 controls.SetAutoPan( false );
450 controls.CaptureCursor( false );
451 controls.ForceCursorPosition( false );
452 return 0;
453}
454
455
457{
459
460 // Toggle the setting
461 if( cfg )
463
464 return 0;
465}
466
467
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:257
static TOOL_ACTION updatePreferences
Definition actions.h:277
static TOOL_ACTION copy
Definition actions.h:78
static TOOL_ACTION updateUnits
Definition actions.h:206
static TOOL_ACTION measureTool
Definition actions.h:251
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:79
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.
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:54
LEADER_MODE m_AngleSnapMode
static TOOL_ACTION toggleHV45Mode
static TOOL_ACTION padDisplayMode
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)
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 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:66
@ 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:151
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:252
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
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695