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 view()->Update( field, KIGFX::REPAINT );
228 }
229
230 for( BOARD_ITEM* item : fp->GraphicalItems() )
231 {
232 if( item->Type() == PCB_TEXT_T )
233 view()->Update( item, KIGFX::REPAINT );
234 }
235 }
236
237 for( BOARD_ITEM* item : board()->Drawings() )
238 {
239 KICAD_T t = item->Type();
240
241 if( t == PCB_TEXT_T || t == PCB_TEXTBOX_T || BaseType( t ) == PCB_DIMENSION_T )
242 view()->Update( item, KIGFX::REPAINT );
243 }
244
245 canvas()->Refresh();
246
247 return 0;
248}
249
250
252
253
255{
256 if( IsFootprintFrame() && !frame()->GetModel() )
257 return 0;
258
259 if( frame()->IsCurrentTool( ACTIONS::measureTool ) )
260 return 0;
261
262 auto& view = *getView();
263 auto& controls = *getViewControls();
264
265 frame()->PushTool( aEvent );
266
267 bool invertXAxis = displayOptions().m_DisplayInvertXAxis;
268 bool invertYAxis = displayOptions().m_DisplayInvertYAxis;
269
270 if( IsFootprintFrame() )
271 {
274 }
275
277 PCB_GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() );
278 bool originSet = false;
279 EDA_UNITS units = frame()->GetUserUnits();
280 KIGFX::PREVIEW::RULER_ITEM ruler( twoPtMgr, pcbIUScale, units, invertXAxis, invertYAxis );
281
282 view.Add( &ruler );
283 view.SetVisible( &ruler, false );
284
285 auto setCursor =
286 [&]()
287 {
289 };
290
291 auto cleanup =
292 [&] ()
293 {
294 view.SetVisible( &ruler, false );
295 controls.SetAutoPan( false );
296 controls.CaptureCursor( false );
297 controls.ForceCursorPosition( false );
298 originSet = false;
299 };
300
301 Activate();
302 // Must be done after Activate() so that it gets set into the correct context
303 controls.ShowCursor( true );
304 controls.SetAutoPan( false );
305 controls.CaptureCursor( false );
306 controls.ForceCursorPosition( false );
307
308 // Set initial cursor
309 setCursor();
310
311 while( TOOL_EVENT* evt = Wait() )
312 {
313 setCursor();
314 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
315 grid.SetUseGrid( view.GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
316 VECTOR2I cursorPos = evt->HasPosition() ? evt->Position() : controls.GetMousePosition();
317 cursorPos = grid.BestSnapAnchor( cursorPos, nullptr );
318 controls.ForceCursorPosition( true, cursorPos );
319
320 if( evt->IsCancelInteractive() )
321 {
322 if( originSet )
323 {
324 cleanup();
325 }
326 else if( m_isDefaultTool )
327 {
328 view.SetVisible( &ruler, false );
329 }
330 else
331 {
332 frame()->PopTool( aEvent );
333 break;
334 }
335 }
336 else if( evt->IsActivate() )
337 {
338 if( originSet )
339 cleanup();
340
341 if( evt->IsMoveTool() )
342 {
343 // leave ourselves on the stack so we come back after the move
344 break;
345 }
346 else
347 {
348 frame()->PopTool( aEvent );
349 break;
350 }
351 }
352 // click or drag starts
353 else if( !originSet && ( evt->IsDrag( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) ) )
354 {
355 twoPtMgr.SetOrigin( cursorPos );
356 twoPtMgr.SetEnd( cursorPos );
357
358 controls.CaptureCursor( true );
359 controls.SetAutoPan( true );
360
361 originSet = true;
362 }
363 // second click or mouse up after drag ends
364 else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
365 {
366 originSet = false;
367
368 controls.SetAutoPan( false );
369 controls.CaptureCursor( false );
370 }
371 // move or drag when origin set updates rules
372 else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
373 {
374 auto snap = LEADER_MODE::DIRECT;
375
376 if( frame()->IsType( FRAME_PCB_EDITOR ) )
377 {
378 snap = GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" )->m_AngleSnapMode;
379 }
380 else if( frame()->IsType( FRAME_FOOTPRINT_EDITOR ) )
381 {
382 snap = GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" )->m_AngleSnapMode;
383 }
384 else
385 {
387 }
388
389 twoPtMgr.SetAngleSnap( snap );
390 twoPtMgr.SetEnd( cursorPos );
391
392 view.SetVisible( &ruler, true );
393 view.Update( &ruler, KIGFX::GEOMETRY );
394 }
395 else if( evt->IsAction( &ACTIONS::updateUnits ) )
396 {
397 if( frame()->GetUserUnits() != units )
398 {
399 units = frame()->GetUserUnits();
400 ruler.SwitchUnits( units );
401 view.Update( &ruler, KIGFX::GEOMETRY );
402 canvas()->Refresh();
403 }
404
405 evt->SetPassEvent();
406 }
407 else if( evt->IsAction( &ACTIONS::updatePreferences ) )
408 {
409 invertXAxis = displayOptions().m_DisplayInvertXAxis;
410 invertYAxis = displayOptions().m_DisplayInvertYAxis;
411
412 if( IsFootprintFrame() )
413 {
416 }
417
418 ruler.UpdateDir( invertXAxis, invertYAxis );
419
420 view.Update( &ruler, KIGFX::GEOMETRY );
421 canvas()->Refresh();
422 evt->SetPassEvent();
423 }
424 else if( evt->IsAction( &ACTIONS::copy ) )
425 {
426 if( originSet )
427 {
428 wxArrayString cursorStrings = ruler.GetDimensionStrings();
429 wxString text = wxJoin( cursorStrings, '\n' );
430
431 if( wxTheClipboard->Open() )
432 {
433 wxTheClipboard->SetData( new wxTextDataObject( text ) );
434 wxTheClipboard->Close();
435 }
436 }
437 }
438 else if( evt->IsClick( BUT_RIGHT ) )
439 {
440 m_menu->ShowContextMenu();
441 }
442 else
443 {
444 evt->SetPassEvent();
445 }
446 }
447
448 view.SetVisible( &ruler, false );
449 view.Remove( &ruler );
450
452 controls.SetAutoPan( false );
453 controls.CaptureCursor( false );
454 controls.ForceCursorPosition( false );
455 return 0;
456}
457
458
460{
462
463 // Toggle the setting
464 if( cfg )
466
467 return 0;
468}
469
470
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.
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 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: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: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