KiCad PCB EDA Suite
length_tuner_tool.cpp
Go to the documentation of this file.
1 /*
2  * KiRouter - a push-and-(sometimes-)shove PCB router
3  *
4  * Copyright (C) 2013-2017 CERN
5  * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
6  * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <core/optional.h>
23 
24 #include "class_draw_panel_gal.h"
25 #include "class_board.h"
26 
27 #include <pcb_edit_frame.h>
28 #include <pcbnew_id.h>
29 #include <view/view_controls.h>
30 #include <pcb_painter.h>
33 
34 #include <tool/context_menu.h>
35 #include <tool/tool_manager.h>
36 #include <tools/pcb_actions.h>
37 #include <hotkeys.h>
38 
39 #include "pns_segment.h"
40 #include "pns_router.h"
41 #include "pns_meander_placer.h" // fixme: move settings to separate header
42 #include "pns_tune_status_popup.h"
43 
44 #include "length_tuner_tool.h"
45 #include <bitmaps.h>
46 #include <tools/tool_event_utils.h>
47 
48 using namespace KIGFX;
49 
50 static TOOL_ACTION ACT_StartTuning( "pcbnew.LengthTuner.StartTuning", AS_CONTEXT,
52  _( "New Track" ), _( "Starts laying a new track." ) );
53 
54 static TOOL_ACTION ACT_EndTuning( "pcbnew.LengthTuner.EndTuning", AS_CONTEXT, WXK_END,
55  _( "End Track" ), _( "Stops laying the current meander." ) );
56 
57 static TOOL_ACTION ACT_Settings( "pcbnew.LengthTuner.Settings", AS_CONTEXT,
59  _( "Length Tuning Settings..." ), _( "Sets the length tuning parameters for currently routed item." ),
60  router_len_tuner_setup_xpm );
61 
62 static TOOL_ACTION ACT_SpacingIncrease( "pcbnew.LengthTuner.SpacingIncrease", AS_CONTEXT,
64  _( "Increase Spacing" ), _( "Increase meander spacing by one step." ),
65  router_len_tuner_dist_incr_xpm );
66 
67 static TOOL_ACTION ACT_SpacingDecrease( "pcbnew.LengthTuner.SpacingDecrease", AS_CONTEXT,
69  _( "Decrease Spacing" ), _( "Decrease meander spacing by one step." ),
70  router_len_tuner_dist_decr_xpm );
71 
72 static TOOL_ACTION ACT_AmplIncrease( "pcbnew.LengthTuner.AmplIncrease", AS_CONTEXT,
74  _( "Increase Amplitude" ), _( "Increase meander amplitude by one step." ),
75  router_len_tuner_amplitude_incr_xpm );
76 
77 static TOOL_ACTION ACT_AmplDecrease( "pcbnew.LengthTuner.AmplDecrease", AS_CONTEXT,
79  _( "Decrease Amplitude" ), _( "Decrease meander amplitude by one step." ),
80  router_len_tuner_amplitude_decr_xpm );
81 
82 
84  TOOL_BASE( "pcbnew.LengthTuner" )
85 {
86 }
87 
88 
90 {
91 public:
93  {
94  SetTitle( _( "Length Tuner" ) );
95  SetIcon( router_len_tuner_xpm );
96  DisplayTitle( true );
97 
99 
100  AppendSeparator();
101 
102  Add( ACT_SpacingIncrease );
103  Add( ACT_SpacingDecrease );
104  Add( ACT_AmplIncrease );
105  Add( ACT_AmplDecrease );
106  Add( ACT_Settings );
107  }
108 
109 private:
110  CONTEXT_MENU* create() const override
111  {
112  return new TUNER_TOOL_MENU();
113  }
114 };
115 
116 
118 {
119 }
120 
121 
123 {
124  if( aReason == RUN )
125  TOOL_BASE::Reset( aReason );
126 }
127 
128 
130 {
131  wxPoint p = wxGetMousePosition();
132 
133  p.x += 20;
134  p.y += 20;
135 
136  aPopup.UpdateStatus( m_router );
137  aPopup.Move( p );
138 }
139 
140 
142 {
143  if( m_startItem )
144  {
146 
147  if( m_startItem->Net() >= 0 )
148  highlightNet( true, m_startItem->Net() );
149  }
150 
151  controls()->ForceCursorPosition( false );
152  controls()->SetAutoPan( true );
153 
155  {
156  wxMessageBox( m_router->FailureReason(), _( "Error" ) );
157  highlightNet( false );
158  return;
159  }
160 
161  auto placer = static_cast<PNS::MEANDER_PLACER_BASE*>( m_router->Placer() );
162 
164 
165  VECTOR2I end( m_startSnapPoint );
166 
167  PNS_TUNE_STATUS_POPUP statusPopup( frame() );
168  statusPopup.Popup();
169 
170  m_router->Move( end, NULL );
171  updateStatusPopup( statusPopup );
172 
173  while( OPT_TOOL_EVENT evt = Wait() )
174  {
176  break;
177  else if( evt->IsMotion() )
178  {
179  end = evt->Position();
180  m_router->Move( end, NULL );
181  updateStatusPopup( statusPopup );
182  }
183  else if( evt->IsClick( BUT_LEFT ) )
184  {
185  if( m_router->FixRoute( evt->Position(), NULL ) )
186  break;
187  }
188  else if( evt->IsAction( &ACT_EndTuning ) )
189  {
190  if( m_router->FixRoute( end, NULL ) )
191  break;
192  }
193  else if( evt->IsAction( &ACT_AmplDecrease ) )
194  {
195  placer->AmplitudeStep( -1 );
196  m_router->Move( end, NULL );
197  updateStatusPopup( statusPopup );
198  }
199  else if( evt->IsAction( &ACT_AmplIncrease ) )
200  {
201  placer->AmplitudeStep( 1 );
202  m_router->Move( end, NULL );
203  updateStatusPopup( statusPopup );
204  }
205  else if(evt->IsAction( &ACT_SpacingDecrease ) )
206  {
207  placer->SpacingStep( -1 );
208  m_router->Move( end, NULL );
209  updateStatusPopup( statusPopup );
210  }
211  else if( evt->IsAction( &ACT_SpacingIncrease ) )
212  {
213  placer->SpacingStep( 1 );
214  m_router->Move( end, NULL );
215  updateStatusPopup( statusPopup );
216  }
217  else if( evt->IsAction( &ACT_Settings ) )
218  {
219  statusPopup.Hide();
221  meanderSettingsDialog( dummy );
222  statusPopup.Show();
223  }
224  }
225 
227  controls()->SetAutoPan( false );
228  controls()->ForceCursorPosition( false );
229  highlightNet( false );
230 }
231 
232 
234 {
235  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Trace Length" ) );
237 }
238 
239 
241 {
242  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Diff Pair Length" ) );
244 }
245 
246 
248 {
249  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Diff Pair Skew" ) );
251 }
252 
253 
255 {
259 }
260 
261 
263 {
264  // Deselect all items
266 
267  Activate();
268 
269  m_router->SetMode( aMode );
270 
271  controls()->SetSnapping( true );
272  controls()->ShowCursor( true );
273  frame()->UndoRedoBlock( true );
274 
275  std::unique_ptr<TUNER_TOOL_MENU> ctxMenu( new TUNER_TOOL_MENU );
276  SetContextMenu( ctxMenu.get() );
277 
278  // Main loop: keep receiving events
279  while( OPT_TOOL_EVENT evt = Wait() )
280  {
282  {
283  break; // Finish
284  }
285  else if( evt->IsMotion() )
286  {
287  updateStartItem( *evt );
288  }
289  else if( evt->IsClick( BUT_LEFT ) || evt->IsAction( &ACT_StartTuning ) )
290  {
291  updateStartItem( *evt );
292  performTuning();
293  }
294  else if( evt->IsAction( &ACT_Settings ) )
295  {
297  meanderSettingsDialog( dummy );
298  }
299  }
300 
302  frame()->UndoRedoBlock( false );
303 
304  // Store routing settings till the next invocation
307 
308  return 0;
309 }
310 
312 {
314 
315  PNS::MEANDER_SETTINGS settings = placer ? placer->MeanderSettings() : m_savedMeanderSettings;
316  DIALOG_PNS_LENGTH_TUNING_SETTINGS settingsDlg( frame(), settings, m_router->Mode() );
317 
318  if( settingsDlg.ShowModal() )
319  {
320  if( placer )
321  placer->UpdateSettings( settings );
322 
323  m_savedMeanderSettings = settings;
324  }
325 
326  return 0;
327 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:53
virtual void ShowCursor(bool aEnabled)
Function ShowCursor() Enables or disables display of cursor.
Class MEANDER_PLACER_BASE.
static TOOL_ACTION ACT_AmplIncrease("pcbnew.LengthTuner.AmplIncrease", AS_CONTEXT, TOOL_ACTION::LegacyHotKey(HK_ROUTE_TUNE_INCREASE_AMP), _("Increase Amplitude"), _("Increase meander amplitude by one step."), router_len_tuner_amplitude_incr_xpm)
virtual void SetToolID(int aId, int aCursor, const wxString &aToolMsg) override
Set the tool command ID to aId and sets the cursor to aCursor.
static TOOL_ACTION ACT_SpacingIncrease("pcbnew.LengthTuner.SpacingIncrease", AS_CONTEXT, TOOL_ACTION::LegacyHotKey(HK_ROUTE_TUNE_INCREASE_SPACING), _("Increase Spacing"), _("Increase meander spacing by one step."), router_len_tuner_dist_incr_xpm)
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:58
const LAYER_RANGE & Layers() const
Function Layers()
Definition: pns_item.h:209
const wxString & FailureReason() const
Definition: pns_router.h:214
Class CONTEXT_MENU.
Definition: context_menu.h:44
VIEW_CONTROLS class definition.
VECTOR2I m_startSnapPoint
Definition: pns_tool_base.h:70
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
Class BOARD to handle a board.
Tool is invoked after being inactive.
Definition: tool_base.h:82
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:215
PLACEMENT_ALGO * Placer()
Definition: pns_router.h:216
int TuneSingleTrace(const TOOL_EVENT &aEvent)
static TOOL_ACTION cancelInteractive
Definition: actions.h:45
SIZES_SETTINGS m_savedSizes
Stores sizes settings between router invocations.
Definition: pns_tool_base.h:67
OPT_TOOL_EVENT Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
virtual void SetSnapping(bool aEnabled)
Function SetSnapping() Enables/disables snapping cursor to grid.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:125
static int LegacyHotKey(int aHotKey)
Creates a hot key code that refers to a legacy hot key setting, instead of a particular key...
Definition: tool_action.h:174
static TOOL_ACTION ACT_Settings("pcbnew.LengthTuner.Settings", AS_CONTEXT, TOOL_ACTION::LegacyHotKey(HK_ROUTE_TUNE_SETTINGS), _("Length Tuning Settings..."), _("Sets the length tuning parameters for currently routed item."), router_len_tuner_setup_xpm)
void updateStatusPopup(PNS_TUNE_STATUS_POPUP &aPopup)
void UndoRedoBlock(bool aBlock=true)
Function UndoRedoBlock Enables/disable undo and redo operations.
Class MEANDER_SETTINGS.
Definition: pns_meander.h:57
SIZES_SETTINGS & Sizes()
Definition: pns_router.h:206
CONTEXT_MENU * create() const override
Returns an instance of this class. It has to be overridden in inheriting classes. ...
virtual void UpdateSettings(const MEANDER_SETTINGS &aSettings)
static TOOL_ACTION routerActivateTuneSingleTrace
Activation of the Push and Shove router (tune single line mode)
Definition: pcb_actions.h:204
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
Pcbnew hotkeys.
int meanderSettingsDialog(const TOOL_EVENT &aEvent)
virtual void updateStartItem(const TOOL_EVENT &aEvent, bool aIgnorePads=false)
KIGFX::VIEW_CONTROLS * controls() const
Definition: pcb_tool.h:138
virtual void Reset(RESET_REASON aReason)=0
Function Reset() Brings the tool to a known, initial state.
static TOOL_ACTION routerActivateTuneDiffPairSkew
Activation of the Push and Shove router (skew tuning mode)
Definition: pcb_actions.h:210
static TOOL_ACTION ACT_SpacingDecrease("pcbnew.LengthTuner.SpacingDecrease", AS_CONTEXT, TOOL_ACTION::LegacyHotKey(HK_ROUTE_TUNE_DECREASE_SPACING), _("Decrease Spacing"), _("Decrease meander spacing by one step."), router_len_tuner_dist_decr_xpm)
PNS::MEANDER_SETTINGS m_savedMeanderSettings
PCB_EDIT_FRAME * frame() const
Definition: pcb_tool.h:139
Class TOOL_EVENT.
Definition: tool_event.h:168
ITEM * m_startItem
Definition: pns_tool_base.h:68
bool FixRoute(const VECTOR2I &aP, ITEM *aItem, bool aForceFinish=false)
Definition: pns_router.cpp:367
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
ROUTER * m_router
Definition: pns_tool_base.h:77
void SetContextMenu(CONTEXT_MENU *aMenu, CONTEXT_MENU_TRIGGER aTrigger=CMENU_BUTTON)
Function SetContextMenu()
virtual void Popup(wxWindow *aFocus=nullptr)
void UpdateStatus(PNS::ROUTER *aRouter)
virtual void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0))
Function ForceCursorPosition() Places the cursor immediately at a given point.
static TOOL_ACTION routerActivateTuneDiffPair
Activation of the Push and Shove router (diff pair tuning mode)
Definition: pcb_actions.h:207
ROUTING_SETTINGS m_savedSettings
Stores routing settings between router invocations.
Definition: pns_tool_base.h:66
virtual void Move(const wxPoint &aWhere)
int Start() const
Definition: pns_layerset.h:83
int TuneDiffPairSkew(const TOOL_EVENT &aEvent)
bool IsCancelInteractive(const TOOL_EVENT &aEvt)
Function IsCancelInteractive()
void Move(const VECTOR2I &aP, ITEM *aItem)
Definition: pns_router.cpp:228
bool StartRouting(const VECTOR2I &aP, ITEM *aItem, int aLayer)
Definition: pns_router.cpp:173
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg...
void StopRouting()
Definition: pns_router.cpp:392
virtual void highlightNet(bool aEnabled, int aNetcode=-1)
int TuneDiffPair(const TOOL_EVENT &aEvent)
virtual void SetNoToolSelected()
Select the ID_NO_TOOL_SELECTED id tool (Idle tool)
static TOOL_ACTION ACT_AmplDecrease("pcbnew.LengthTuner.AmplDecrease", AS_CONTEXT, TOOL_ACTION::LegacyHotKey(HK_ROUTE_TUNE_DECREASE_AMP), _("Decrease Amplitude"), _("Decrease meander amplitude by one step."), router_len_tuner_amplitude_decr_xpm)
ROUTER_MODE
Definition: pns_router.h:64
static TOOL_ACTION ACT_StartTuning("pcbnew.LengthTuner.StartTuning", AS_CONTEXT, TOOL_ACTION::LegacyHotKey(HK_ADD_NEW_TRACK), _("New Track"), _("Starts laying a new track."))
void SetMode(ROUTER_MODE aMode)
Definition: pns_router.cpp:510
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
ROUTER_MODE Mode() const
Definition: pns_router.h:124
Class TOOL_BASE.
Definition: tool_base.h:68
int mainLoop(PNS::ROUTER_MODE aMode)
Class TOOL_ACTION.
Definition: tool_action.h:46
static TOOL_ACTION ACT_EndTuning("pcbnew.LengthTuner.EndTuning", AS_CONTEXT, WXK_END, _("End Track"), _("Stops laying the current meander."))
int Net() const
Function Net()
Definition: pns_item.h:179
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:80
virtual const MEANDER_SETTINGS & MeanderSettings() const
Function MeanderSettings()
void Activate()
Function Activate() Runs the tool.
virtual void SetActiveLayer(PCB_LAYER_ID aLayer) override
Function SetActiveLayer will change the currently active layer to aLayer and also update the PCB_LAYE...
ROUTING_SETTINGS & Settings()
Definition: pns_router.h:187
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:810
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:495