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  // fixme: wx code not allowed inside tools!
132  wxPoint p = wxGetMousePosition();
133 
134  p.x += 20;
135  p.y += 20;
136 
137  aPopup.UpdateStatus( m_router );
138  aPopup.Move( p );
139 }
140 
141 
143 {
144  if( m_startItem )
145  {
147 
148  if( m_startItem->Net() >= 0 )
149  highlightNet( true, m_startItem->Net() );
150  }
151 
152  controls()->ForceCursorPosition( false );
153  controls()->SetAutoPan( true );
154 
156  {
157  wxMessageBox( m_router->FailureReason(), _( "Error" ) );
158  highlightNet( false );
159  return;
160  }
161 
162  auto placer = static_cast<PNS::MEANDER_PLACER_BASE*>( m_router->Placer() );
163 
165 
166  VECTOR2I end( m_startSnapPoint );
167 
168  PNS_TUNE_STATUS_POPUP statusPopup( frame() );
169  statusPopup.Popup();
170 
171  m_router->Move( end, NULL );
172  updateStatusPopup( statusPopup );
173 
174  while( OPT_TOOL_EVENT evt = Wait() )
175  {
177  break;
178  else if( evt->IsMotion() )
179  {
180  end = evt->Position();
181  m_router->Move( end, NULL );
182  updateStatusPopup( statusPopup );
183  }
184  else if( evt->IsClick( BUT_LEFT ) )
185  {
186  if( m_router->FixRoute( evt->Position(), NULL ) )
187  break;
188  }
189  else if( evt->IsAction( &ACT_EndTuning ) )
190  {
191  if( m_router->FixRoute( end, NULL ) )
192  break;
193  }
194  else if( evt->IsAction( &ACT_AmplDecrease ) )
195  {
196  placer->AmplitudeStep( -1 );
197  m_router->Move( end, NULL );
198  updateStatusPopup( statusPopup );
199  }
200  else if( evt->IsAction( &ACT_AmplIncrease ) )
201  {
202  placer->AmplitudeStep( 1 );
203  m_router->Move( end, NULL );
204  updateStatusPopup( statusPopup );
205  }
206  else if(evt->IsAction( &ACT_SpacingDecrease ) )
207  {
208  placer->SpacingStep( -1 );
209  m_router->Move( end, NULL );
210  updateStatusPopup( statusPopup );
211  }
212  else if( evt->IsAction( &ACT_SpacingIncrease ) )
213  {
214  placer->SpacingStep( 1 );
215  m_router->Move( end, NULL );
216  updateStatusPopup( statusPopup );
217  }
218  else if( evt->IsAction( &ACT_Settings ) )
219  {
220  statusPopup.Hide();
222  meanderSettingsDialog( dummy );
223  statusPopup.Show();
224  }
225  }
226 
228  controls()->SetAutoPan( false );
229  controls()->ForceCursorPosition( false );
230  highlightNet( false );
231 }
232 
233 
235 {
236  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Trace Length" ) );
238 }
239 
240 
242 {
243  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Diff Pair Length" ) );
245 }
246 
247 
249 {
250  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Diff Pair Skew" ) );
252 }
253 
254 
256 {
260 }
261 
262 
264 {
265  // Deselect all items
267 
268  Activate();
269 
270  m_router->SetMode( aMode );
271 
272  controls()->SetSnapping( true );
273  controls()->ShowCursor( true );
274  frame()->UndoRedoBlock( true );
275 
276  std::unique_ptr<TUNER_TOOL_MENU> ctxMenu( new TUNER_TOOL_MENU );
277  SetContextMenu( ctxMenu.get() );
278 
279  // Main loop: keep receiving events
280  while( OPT_TOOL_EVENT evt = Wait() )
281  {
283  {
284  break; // Finish
285  }
286  else if( evt->IsMotion() )
287  {
288  updateStartItem( *evt );
289  }
290  else if( evt->IsClick( BUT_LEFT ) || evt->IsAction( &ACT_StartTuning ) )
291  {
292  updateStartItem( *evt );
293  performTuning();
294  }
295  else if( evt->IsAction( &ACT_Settings ) )
296  {
298  meanderSettingsDialog( dummy );
299  }
300  }
301 
303  frame()->UndoRedoBlock( false );
304 
305  // Store routing settings till the next invocation
308 
309  return 0;
310 }
311 
313 {
315 
316  PNS::MEANDER_SETTINGS settings = placer ? placer->MeanderSettings() : m_savedMeanderSettings;
317  DIALOG_PNS_LENGTH_TUNING_SETTINGS settingsDlg( frame(), settings, m_router->Mode() );
318 
319  if( settingsDlg.ShowModal() )
320  {
321  if( placer )
322  placer->UpdateSettings( settings );
323 
324  m_savedMeanderSettings = settings;
325  }
326 
327  return 0;
328 }
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:57
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:209
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:215
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:78
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:212
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:813
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:495