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 <boost/optional.hpp>
23 
24 #include "class_draw_panel_gal.h"
25 #include "class_board.h"
26 
27 #include <wxPcbStruct.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 
38 #include "pns_segment.h"
39 #include "pns_router.h"
40 #include "pns_meander_placer.h" // fixme: move settings to separate header
41 #include "pns_tune_status_popup.h"
42 
43 #include "length_tuner_tool.h"
44 
45 using namespace KIGFX;
46 using boost::optional;
47 
48 static TOOL_ACTION ACT_StartTuning( "pcbnew.LengthTuner.StartTuning", AS_CONTEXT, 'X',
49  _( "New Track" ), _( "Starts laying a new track." ) );
50 
51 static TOOL_ACTION ACT_EndTuning( "pcbnew.LengthTuner.EndTuning", AS_CONTEXT, WXK_END,
52  _( "End Track" ), _( "Stops laying the current meander." ) );
53 
54 static TOOL_ACTION ACT_Settings( "pcbnew.LengthTuner.Settings", AS_CONTEXT, 'L',
55  _( "Length Tuning Settings" ), _( "Sets the length tuning parameters for currently routed item." ) );
56 
57 static TOOL_ACTION ACT_SpacingIncrease( "pcbnew.LengthTuner.SpacingIncrease", AS_CONTEXT, '1',
58  _( "Increase spacing" ), _( "Increase meander spacing by one step." ) );
59 
60 static TOOL_ACTION ACT_SpacingDecrease( "pcbnew.LengthTuner.SpacingDecrease", AS_CONTEXT, '2',
61  _( "Decrease spacing" ), _( "Decrease meander spacing by one step." ) );
62 
63 static TOOL_ACTION ACT_AmplIncrease( "pcbnew.LengthTuner.AmplIncrease", AS_CONTEXT, '3',
64  _( "Increase amplitude" ), _( "Increase meander amplitude by one step." ) );
65 
66 static TOOL_ACTION ACT_AmplDecrease( "pcbnew.LengthTuner.AmplDecrease", AS_CONTEXT, '4',
67  _( "Decrease amplitude" ), _( "Decrease meander amplitude by one step." ) );
68 
69 
71  TOOL_BASE( "pcbnew.LengthTuner" )
72 {
73 }
74 
75 
77 {
78 public:
80  {
81  SetTitle( _( "Length Tuner" ) );
82  DisplayTitle( true );
83 
84  //Add( ACT_StartTuning );
85  //Add( ACT_EndTuning );
86 
87  //AppendSeparator();
88 
93  Add( ACT_Settings );
94  }
95 
96 private:
97  CONTEXT_MENU* create() const override
98  {
99  return new TUNER_TOOL_MENU();
100  }
101 };
102 
103 
105 {
106 }
107 
108 
110 {
111  TOOL_BASE::Reset( aReason );
112 
116 }
117 
118 
120 {
121  wxPoint p = wxGetMousePosition();
122 
123  p.x += 20;
124  p.y += 20;
125 
126  aPopup.UpdateStatus( m_router );
127  aPopup.Move( p );
128 }
129 
130 
132 {
133  if( m_startItem )
134  {
136 
137  if( m_startItem->Net() >= 0 )
138  highlightNet( true, m_startItem->Net() );
139  }
140 
141  m_ctls->ForceCursorPosition( false );
142  m_ctls->SetAutoPan( true );
143 
145  {
146  wxMessageBox( m_router->FailureReason(), _( "Error" ) );
147  highlightNet( false );
148  return;
149  }
150 
151  PNS::MEANDER_PLACER_BASE* placer = static_cast<PNS::MEANDER_PLACER_BASE*>(
152  m_router->Placer() );
153 
155 
156  VECTOR2I end( m_startSnapPoint );
157 
158  PNS_TUNE_STATUS_POPUP statusPopup( m_frame );
159  statusPopup.Popup();
160 
161  m_router->Move( end, NULL );
162  updateStatusPopup( statusPopup );
163 
164  while( OPT_TOOL_EVENT evt = Wait() )
165  {
166  if( evt->IsCancel() || evt->IsActivate() )
167  break;
168  else if( evt->IsMotion() )
169  {
170  end = evt->Position();
171  m_router->Move( end, NULL );
172  updateStatusPopup( statusPopup );
173  }
174  else if( evt->IsClick( BUT_LEFT ) )
175  {
176  if( m_router->FixRoute( evt->Position(), NULL ) )
177  break;
178  }
179  else if( evt->IsAction( &ACT_EndTuning ) )
180  {
181  if( m_router->FixRoute( end, NULL ) )
182  break;
183  }
184  else if( evt->IsAction( &ACT_AmplDecrease ) )
185  {
186  placer->AmplitudeStep( -1 );
187  m_router->Move( end, NULL );
188  }
189  else if( evt->IsAction( &ACT_AmplIncrease ) )
190  {
191  placer->AmplitudeStep( 1 );
192  m_router->Move( end, NULL );
193  }
194  else if(evt->IsAction( &ACT_SpacingDecrease ) )
195  {
196  placer->SpacingStep( -1 );
197  m_router->Move( end, NULL );
198  }
199  else if( evt->IsAction( &ACT_SpacingIncrease ) )
200  {
201  placer->SpacingStep( 1 );
202  m_router->Move( end, NULL );
203  }
204  }
205 
207  highlightNet( false );
208 }
209 
210 
212 {
213  m_frame->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Trace Length" ) );
215 }
216 
217 
219 {
220  m_frame->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Diff Pair Length" ) );
222 }
223 
224 
226 {
227  m_frame->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Diff Pair Skew" ) );
229 }
230 
231 
233 {
236 }
237 
238 
240 {
241  // Deselect all items
243 
244  Activate();
245 
246  m_router->SetMode( aMode );
247 
248  m_ctls->SetSnapping( true );
249  m_ctls->ShowCursor( true );
250  m_frame->UndoRedoBlock( true );
251 
252  std::unique_ptr<TUNER_TOOL_MENU> ctxMenu( new TUNER_TOOL_MENU );
253  SetContextMenu( ctxMenu.get() );
254 
255  // Main loop: keep receiving events
256  while( OPT_TOOL_EVENT evt = Wait() )
257  {
258  if( evt->IsCancel() || evt->IsActivate() )
259  {
260  break; // Finish
261  }
262  else if( evt->IsMotion() )
263  {
264  updateStartItem( *evt );
265  }
266  else if( evt->IsClick( BUT_LEFT ) || evt->IsAction( &ACT_StartTuning ) )
267  {
268  updateStartItem( *evt );
269  performTuning();
270  }
271  }
272 
273  m_frame->SetToolID( ID_NO_TOOL_SELECTED, wxCURSOR_DEFAULT, wxEmptyString );
274  m_frame->UndoRedoBlock( false );
275 
276  // Store routing settings till the next invocation
279 
280  return 0;
281 }
282 
283 
285 {
286  DIALOG_PNS_SETTINGS settingsDlg( m_frame, m_router->Settings() );
287 
288  if( settingsDlg.ShowModal() == wxID_OK )
289  {
290  // FIXME: do we need an explicit update?
291  }
292 
293  return 0;
294 }
295 
296 
298 {
300 
301  if( !placer )
302  return 0;
303 
304  PNS::MEANDER_SETTINGS settings = placer->MeanderSettings();
305  DIALOG_PNS_LENGTH_TUNING_SETTINGS settingsDlg( m_frame, settings, m_router->Mode() );
306 
307  if( settingsDlg.ShowModal() )
308  placer->UpdateSettings( settings );
309 
311 
312  return 0;
313 }
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.
static TOOL_ACTION ACT_RouterOptions
Definition: pns_tool_base.h:47
static TOOL_ACTION ACT_SpacingDecrease("pcbnew.LengthTuner.SpacingDecrease", AS_CONTEXT, '2', _("Decrease spacing"), _("Decrease meander spacing by one step."))
Class MEANDER_PLACER_BASE.
static TOOL_ACTION ACT_AmplDecrease("pcbnew.LengthTuner.AmplDecrease", AS_CONTEXT, '4', _("Decrease amplitude"), _("Decrease meander amplitude by one step."))
virtual void SetToolID(int aId, int aCursor, const wxString &aToolMsg) override
Function SetToolID sets the tool command ID to aId and sets the cursor to aCursor.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:56
const LAYER_RANGE & Layers() const
Function Layers()
Definition: pns_item.h:207
virtual void SetActiveLayer(LAYER_ID aLayer) override
Function SetActiveLayer will change the currently active layer to aLayer and also update the PCB_LAYE...
Definition: pcbframe.cpp:893
const wxString & FailureReason() const
Definition: pns_router.h:205
Class CONTEXT_MENU.
Definition: context_menu.h:44
static TOOL_ACTION ACT_StartTuning("pcbnew.LengthTuner.StartTuning", AS_CONTEXT, 'X', _("New Track"), _("Starts laying a new track."))
VIEW_CONTROLS class definition.
VECTOR2I m_startSnapPoint
Definition: pns_tool_base.h:75
static TOOL_ACTION ACT_SpacingIncrease("pcbnew.LengthTuner.SpacingIncrease", AS_CONTEXT, '1', _("Increase spacing"), _("Increase meander spacing by one step."))
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
Class BOARD to handle a board.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:220
PLACEMENT_ALGO * Placer()
Definition: pns_router.h:207
int TuneSingleTrace(const TOOL_EVENT &aEvent)
SIZES_SETTINGS m_savedSizes
Stores sizes settings between router invocations.
Definition: pns_tool_base.h:72
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
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:197
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:175
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon=NULL)
Function Add() Adds an entry to the menu.
int meanderSettingsDialog(const TOOL_EVENT &aEvent)
KIGFX::VIEW_CONTROLS * m_ctls
Definition: pns_tool_base.h:81
void SetTransitions() override
Function SetTransitions() This method is meant to be overridden in order to specify handlers for even...
virtual void Move(const wxPoint &aWhere)
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:181
PNS::MEANDER_SETTINGS m_savedMeanderSettings
int routerOptionsDialog(const TOOL_EVENT &aEvent)
Class TOOL_EVENT.
Definition: tool_event.h:162
ITEM * m_startItem
Definition: pns_tool_base.h:73
bool FixRoute(const VECTOR2I &aP, ITEM *aItem)
Definition: pns_router.cpp:341
ROUTER * m_router
Definition: pns_tool_base.h:85
void SetContextMenu(CONTEXT_MENU *aMenu, CONTEXT_MENU_TRIGGER aTrigger=CMENU_BUTTON)
Function SetContextMenu()
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:178
ROUTING_SETTINGS m_savedSettings
Stores routing settings between router invocations.
Definition: pns_tool_base.h:71
int Start() const
Definition: pns_layerset.h:83
int TuneDiffPairSkew(const TOOL_EVENT &aEvent)
static TOOL_ACTION ACT_Settings("pcbnew.LengthTuner.Settings", AS_CONTEXT, 'L', _("Length Tuning Settings"), _("Sets the length tuning parameters for currently routed item."))
void Move(const VECTOR2I &aP, ITEM *aItem)
Definition: pns_router.cpp:202
virtual void SpacingStep(int aSign)
Function SpacingStep()
bool StartRouting(const VECTOR2I &aP, ITEM *aItem, int aLayer)
Definition: pns_router.cpp:156
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg...
static TOOL_ACTION ACT_AmplIncrease("pcbnew.LengthTuner.AmplIncrease", AS_CONTEXT, '3', _("Increase amplitude"), _("Increase meander amplitude by one step."))
void StopRouting()
Definition: pns_router.cpp:366
virtual void highlightNet(bool aEnabled, int aNetcode=-1)
int TuneDiffPair(const TOOL_EVENT &aEvent)
ROUTER_MODE
Definition: pns_router.h:64
virtual void Popup(wxWindow *aFocus=NULL)
TOOL_EVENT MakeEvent() const
Function HasHotKey() Checks if the action has a hot key assigned.
Definition: tool_action.h:104
void SetMode(ROUTER_MODE aMode)
Definition: pns_router.cpp:484
LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:766
ROUTER_MODE Mode() const
Definition: pns_router.h:116
Class TOOL_BASE.
Definition: tool_base.h:68
void SetTitle(const wxString &aTitle) override
Function SetTitle() Sets title for the context menu.
int mainLoop(PNS::ROUTER_MODE aMode)
Class TOOL_ACTION.
Definition: tool_action.h:46
PCB_EDIT_FRAME * m_frame
Definition: pns_tool_base.h:80
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:177
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 AmplitudeStep(int aSign)
Function AmplitudeStep()
void DisplayTitle(bool aDisplay=true)
Function DisplayTitle() Decides whether a title for a pop up menu should be displayed.
ROUTING_SETTINGS & Settings()
Definition: pns_router.h:178
virtual void updateStartItem(TOOL_EVENT &aEvent)