KiCad PCB EDA Suite
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 (C) 2020 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 #include <kiplatform/ui.h>
26 #include <pcb_base_frame.h>
27 #include <pcbnew_settings.h>
29 #include <tool/actions.h>
30 #include <tools/grid_helper.h>
31 #include <tools/pcb_actions.h>
32 #include <tools/pcb_viewer_tools.h>
33 #include <view/view_controls.h>
34 #include <wx/debug.h>
35 
36 
38 {
39  // Populate the context menu displayed during the tool (primarily the measure tool)
40  auto activeToolCondition =
41  [ this ] ( const SELECTION& aSel )
42  {
43  return !frame()->ToolStackIsEmpty();
44  };
45 
46  auto& ctxMenu = m_menu.GetMenu();
47 
48  // "Cancel" goes at the top of the context menu when a tool is active
49  ctxMenu.AddItem( ACTIONS::cancelInteractive, activeToolCondition, 1 );
50  ctxMenu.AddSeparator( 1 );
51 
53 
54  return true;
55 }
56 
57 
59 {
60 }
61 
62 
64 {
65  EDA_3D_VIEWER* draw3DFrame = frame()->CreateAndShow3D_Frame();
66 
67  if( frame()->IsType( FRAME_FOOTPRINT_VIEWER )
68  || frame()->IsType( FRAME_FOOTPRINT_VIEWER_MODAL )
69  || frame()->IsType( FRAME_FOOTPRINT_WIZARD ) )
70  {
71  frame()->Update3DView( true );
72 
73  // A stronger version of Raise() which promotes the window to its parent's level.
75  }
76  return 0;
77 }
78 
79 
80 template<class T> void Flip( T& aValue )
81 {
82  aValue = !aValue;
83 }
84 
85 
87 {
88  frame()->SetAutoZoom( !frame()->GetAutoZoom() );
89 
90  return 0;
91 }
92 
93 
95 {
96  auto opts = displayOptions();
97 
98  Flip( opts.m_DisplayPadNum );
99  frame()->SetDisplayOptions( opts );
100  view()->UpdateDisplayOptions( opts );
101 
102  for( auto module : board()->Modules() )
103  {
104  for( auto pad : module->Pads() )
105  view()->Update( pad, KIGFX::GEOMETRY );
106  }
107 
108  canvas()->Refresh();
109 
110  return 0;
111 }
112 
113 
115 {
116  auto opts = displayOptions();
117 
118  Flip( opts.m_DisplayPadFill );
119  frame()->SetDisplayOptions( opts );
120  view()->UpdateDisplayOptions( opts );
121 
122  for( auto module : board()->Modules() )
123  {
124  for( auto pad : module->Pads() )
125  view()->Update( pad, KIGFX::GEOMETRY );
126  }
127 
128  canvas()->Refresh();
129 
130  return 0;
131 }
132 
133 
135 {
137 
138  Flip( opts.m_DisplayGraphicsFill );
139  frame()->SetDisplayOptions( opts );
140  view()->UpdateDisplayOptions( opts );
141 
142  for( MODULE* module : board()->Modules() )
143  {
144  for( BOARD_ITEM* item : module->GraphicalItems() )
145  {
146  if( item->Type() == PCB_MODULE_EDGE_T )
147  view()->Update( item, KIGFX::GEOMETRY );
148  }
149  }
150 
151  for( BOARD_ITEM* item : board()->Drawings() )
152  {
153  KICAD_T t = item->Type();
154 
155  if( t == PCB_LINE_T || t == PCB_DIMENSION_T || t == PCB_TARGET_T )
156  view()->Update( item, KIGFX::GEOMETRY );
157  }
158 
159  canvas()->Refresh();
160 
161  return 0;
162 }
163 
164 
166 {
168 
169  Flip( opts.m_DisplayTextFill );
170  frame()->SetDisplayOptions( opts );
171  view()->UpdateDisplayOptions( opts );
172 
173  for( MODULE* module : board()->Modules() )
174  {
177 
178  for( BOARD_ITEM* item : module->GraphicalItems() )
179  {
180  if( item->Type() == PCB_MODULE_TEXT_T )
181  view()->Update( item, KIGFX::GEOMETRY );
182  }
183  }
184 
185  for( BOARD_ITEM* item : board()->Drawings() )
186  {
187  KICAD_T t = item->Type();
188 
189  if( t == PCB_TEXT_T || t == PCB_DIMENSION_T )
190  view()->Update( item, KIGFX::GEOMETRY );
191  }
192 
193  canvas()->Refresh();
194 
195  return 0;
196 }
197 
198 
200 {
201  if( IsFootprintFrame() && !frame()->GetModel() )
202  return 0;
203 
204  auto& view = *getView();
205  auto& controls = *getViewControls();
206 
207  std::string tool = aEvent.GetCommandStr().get();
208  frame()->PushTool( tool );
209  Activate();
210 
212 
213  EDA_UNITS units = frame()->GetUserUnits();
214  KIGFX::PREVIEW::RULER_ITEM ruler( twoPtMgr, units );
215 
216  view.Add( &ruler );
217  view.SetVisible( &ruler, false );
218 
219  GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() );
220 
221  bool originSet = false;
222 
223  controls.ShowCursor( true );
224  controls.SetAutoPan( false );
225  controls.CaptureCursor( false );
226 
227  while( auto evt = Wait() )
228  {
229  frame()->GetCanvas()->SetCurrentCursor( wxCURSOR_ARROW );
230  grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
231  grid.SetUseGrid( !evt->Modifier( MD_ALT ) );
232  controls.SetSnapping( !evt->Modifier( MD_ALT ) );
233  const VECTOR2I cursorPos = grid.BestSnapAnchor( controls.GetMousePosition(), nullptr );
234  controls.ForceCursorPosition(true, cursorPos );
235 
236  auto clearRuler =
237  [&] ()
238  {
239  view.SetVisible( &ruler, false );
240  controls.SetAutoPan( false );
241  controls.CaptureCursor( false );
242  originSet = false;
243  };
244 
245  if( evt->IsCancelInteractive() )
246  {
247  if( originSet )
248  clearRuler();
249  else
250  {
251  frame()->PopTool( tool );
252  break;
253  }
254  }
255 
256  else if( evt->IsActivate() )
257  {
258  if( originSet )
259  clearRuler();
260 
261  if( evt->IsMoveTool() )
262  {
263  // leave ourselves on the stack so we come back after the move
264  break;
265  }
266  else
267  {
268  frame()->PopTool( tool );
269  break;
270  }
271  }
272 
273  // click or drag starts
274  else if( !originSet && ( evt->IsDrag( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) ) )
275  {
276  twoPtMgr.SetOrigin( cursorPos );
277  twoPtMgr.SetEnd( cursorPos );
278 
279  controls.CaptureCursor( true );
280  controls.SetAutoPan( true );
281 
282  originSet = true;
283  }
284 
285  // second click or mouse up after drag ends
286  else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
287  {
288  originSet = false;
289 
290  controls.SetAutoPan( false );
291  controls.CaptureCursor( false );
292  }
293 
294  // move or drag when origin set updates rules
295  else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
296  {
297  twoPtMgr.SetAngleSnap( evt->Modifier( MD_CTRL ) );
298  twoPtMgr.SetEnd( cursorPos );
299 
300  view.SetVisible( &ruler, true );
301  view.Update( &ruler, KIGFX::GEOMETRY );
302  }
303 
304  else if( evt->IsAction( &ACTIONS::toggleUnits )
305  || evt->IsAction( &PCB_ACTIONS::updateUnits ) )
306  {
307  if( frame()->GetUserUnits() != units )
308  {
309  units = frame()->GetUserUnits();
310  ruler.SwitchUnits();
311  view.Update( &ruler, KIGFX::GEOMETRY );
312  canvas()->Refresh();
313  }
314  }
315 
316  else if( evt->IsClick( BUT_RIGHT ) )
317  {
319  }
320 
321  else
322  evt->SetPassEvent();
323  }
324 
325  view.SetVisible( &ruler, false );
326  view.Remove( &ruler );
327  return 0;
328 }
329 
330 
332 {
334 
335  // Display modes
341 
343 }
EDA_UNITS
Definition: common.h:198
int ShowPadNumbers(const TOOL_EVENT &aEvent)
static TOOL_ACTION show3DViewer
Definition: actions.h:152
void AddStandardSubMenus(TOOL_MENU &aMenu)
Function CreateBasicMenu.
TOOL_MENU m_menu
functions below are not yet implemented - their interface may change
TEXTE_MODULE & Reference()
Definition: class_module.h:485
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags) override
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: pcb_view.cpp:92
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
void SetCurrentCursor(wxStockCursor aStockCursorID)
Function SetCurrentCursor Set the current cursor shape for this panel.
VIEW_CONTROLS class definition.
int MeasureTool(const TOOL_EVENT &aEvent)
Launches a tool to measure between points
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:46
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
static TOOL_ACTION cancelInteractive
Definition: actions.h:65
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
PADS & Pads()
Definition: class_module.h:173
class TEXTE_PCB, text on a layer
Definition: typeinfo.h:92
PCB_BASE_FRAME * frame() const
void SetDisplayOptions(const PCB_DISPLAY_OPTIONS &aOptions)
VECTOR2I BestSnapAnchor(const VECTOR2I &aOrigin, BOARD_ITEM *aDraggedItem)
static TOOL_ACTION updateUnits
Definition: pcb_actions.h:404
T
enum T contains all this lexer's tokens.
DRAWINGS & GraphicalItems()
Definition: class_module.h:183
static TOOL_ACTION zoomFootprintAutomatically
Definition: pcb_actions.h:231
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
virtual void Remove(VIEW_ITEM *aItem) override
Function Remove() Removes a VIEW_ITEM from the view.
Definition: pcb_view.cpp:75
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
KIGFX::PCB_VIEW * view() const
Classes used in Pcbnew, CvPcb and GerbView.
static TOOL_ACTION measureTool
Definition: actions.h:148
PCB_DISPLAY_OPTIONS handles display options like enable/disable some optional drawings.
void UpdateDisplayOptions(const PCB_DISPLAY_OPTIONS &aOptions)
Definition: pcb_view.cpp:116
int Show3DViewer(const TOOL_EVENT &aEvent)
Show the 3D viewer.
EDA_3D_VIEWER * CreateAndShow3D_Frame()
Shows the 3D view frame.
virtual void PopTool(const std::string &actionName)
static TOOL_ACTION showPadNumbers
Definition: pcb_actions.h:230
TEXTE_MODULE & Value()
read/write accessors:
Definition: class_module.h:484
void setTransitions() override
Sets up handlers for various events.
TOOL_EVENT.
Definition: tool_event.h:171
void SetOrigin(const VECTOR2I &aOrigin)
Set the origin of the ruler (the fixed end)
bool ToolStackIsEmpty()
Definition: tools_holder.h:84
class DIMENSION, a dimension (graphic item)
Definition: typeinfo.h:100
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:36
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:101
void SetSnap(bool aSnap)
Definition: grid_helper.h:83
static TOOL_ACTION padDisplayMode
Definition: pcb_actions.h:224
int ZoomAutomatically(const TOOL_EVENT &aEvent)
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
static TOOL_ACTION graphicsOutlines
Display module edges as outlines.
Definition: pcb_actions.h:368
bool IsFootprintFrame() const
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:463
Declaration of the eda_3d_viewer class.
void Flip(T &aValue)
virtual void SetAutoZoom(bool aAutoZoom)
Does nothing.
const PCB_DISPLAY_OPTIONS & displayOptions() const
int GraphicOutlines(const TOOL_EVENT &aEvent)
BOARD * board() const
PCB_DRAW_PANEL_GAL * canvas() const
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1490
void Activate()
Function Activate() Runs the tool.
Create and handle a window for the 3d viewer connected to a Kiway and a pcbboard.
Definition: eda_3d_viewer.h:65
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Function Add() Adds a VIEW_ITEM to the view.
Definition: pcb_view.cpp:58
int TextOutlines(const TOOL_EVENT &aEvent)
void SetUseGrid(bool aGrid=true)
Definition: grid_helper.h:93
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:42
SGLIB_API S3DMODEL * GetModel(SCENEGRAPH *aNode)
Function GetModel creates an S3DMODEL representation of aNode (raw data, no transforms)
Definition: ifsg_api.cpp:471
virtual void Update3DView(bool aForceReload, const wxString *aTitle=nullptr)
Update the 3D view, if the viewer is opened by this frame.
class DRAWSEGMENT, a segment not on copper layers
Definition: typeinfo.h:91
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
void ReparentQuasiModal(wxNonOwnedWindow *aWindow)
Move a window's parent to be the top-level window and force the window to be on top.
Definition: gtk/ui.cpp:32
MODULE * module() const
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddItem()
static TOOL_ACTION toggleUnits
Definition: actions.h:142
void ShowContextMenu(SELECTION &aSelection)
Function ShowContextMenu.
Definition: tool_menu.cpp:59
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
int PadDisplayMode(const TOOL_EVENT &aEvent)
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
void SetEnd(const VECTOR2I &aEnd)
Set the current end of the rectangle (the end that moves with the cursor.
Color has changed.
Definition: view_item.h:59
static TOOL_ACTION textOutlines
Display texts as lines.
Definition: pcb_actions.h:371