KiCad PCB EDA Suite
microwave_tool.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) 2017-2020 Kicad Developers, see change_log.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 
24 #include <bitmaps.h>
25 #include <board_commit.h>
26 #include <class_board_item.h>
27 #include <class_draw_panel_gal.h>
28 #include <class_module.h>
29 #include <confirm.h>
34 #include <tool/tool_manager.h>
35 #include <tools/pcb_actions.h>
36 #include <tools/selection_tool.h>
37 #include <view/view_controls.h>
38 #include <view/view.h>
39 
40 
42  PCB_TOOL_BASE( "pcbnew.MicrowaveTool" )
43 {
44 }
45 
46 
48 {}
49 
50 
52 {
53 }
54 
55 
57 {
58  struct MICROWAVE_PLACER : public INTERACTIVE_PLACER_BASE
59  {
60  MICROWAVE_PLACER( MICROWAVE_TOOL* aTool, MICROWAVE_FOOTPRINT_SHAPE aType ) :
61  m_tool( aTool ),
62  m_itemType( aType )
63  { };
64 
65  virtual ~MICROWAVE_PLACER()
66  {
67  }
68 
69  std::unique_ptr<BOARD_ITEM> CreateItem() override
70  {
71  switch( m_itemType )
72  {
76  return std::unique_ptr<MODULE>( m_tool->createFootprint( m_itemType ) );
77 
79  return std::unique_ptr<MODULE>( m_tool->createPolygonShape() );
80 
81  default:
82  return std::unique_ptr<MODULE>();
83  };
84  }
85 
86  private:
87  MICROWAVE_TOOL* m_tool;
88  MICROWAVE_FOOTPRINT_SHAPE m_itemType;
89  };
90 
91  MICROWAVE_PLACER placer( this, aEvent.Parameter<MICROWAVE_FOOTPRINT_SHAPE>() );
92 
93  doInteractiveItemPlacement( aEvent.GetCommandStr().get(), &placer,
94  _( "Place microwave feature" ),
96 
97  return 0;
98 }
99 
100 
101 static const COLOR4D inductorAreaFill( 0.3, 0.3, 0.5, 0.3 );
102 static const COLOR4D inductorAreaStroke( 0.4, 1.0, 1.0, 1.0 );
103 static const double inductorAreaStrokeWidth = 1.0;
104 
107 static const double inductorAreaAspect = 0.5;
108 
109 
111 {
112  using namespace KIGFX::PREVIEW;
113 
114  KIGFX::VIEW& view = *getView();
116  PCB_EDIT_FRAME& frame = *getEditFrame<PCB_EDIT_FRAME>();
117 
118  std::string tool = aEvent.GetCommandStr().get();
119  frame.PushTool( tool );
120  Activate();
121 
122  TWO_POINT_GEOMETRY_MANAGER tpGeomMgr;
123 
124  CENTRELINE_RECT_ITEM previewRect( tpGeomMgr, inductorAreaAspect );
125 
126  previewRect.SetFillColor( inductorAreaFill );
127  previewRect.SetStrokeColor( inductorAreaStroke );
128  previewRect.SetLineWidth( inductorAreaStrokeWidth );
129 
130  bool originSet = false;
131 
132  controls.ShowCursor( true );
133  controls.SetSnapping( true );
134  controls.CaptureCursor( false );
135  controls.SetAutoPan( false );
136 
137  view.Add( &previewRect );
138 
139  while( auto evt = Wait() )
140  {
141  frame.GetCanvas()->SetCurrentCursor( wxCURSOR_PENCIL );
142  VECTOR2I cursorPos = controls.GetCursorPosition();
143 
144  auto cleanup = [&] () {
145  originSet = false;
146  controls.CaptureCursor( false );
147  controls.SetAutoPan( false );
148  view.SetVisible( &previewRect, false );
149  view.Update( &previewRect, KIGFX::GEOMETRY );
150  };
151 
152  if( evt->IsCancelInteractive() )
153  {
154  if( originSet )
155  cleanup();
156  else
157  {
158  frame.PopTool( tool );
159  break;
160  }
161  }
162 
163  else if( evt->IsActivate() )
164  {
165  if( originSet )
166  cleanup();
167 
168  if( evt->IsMoveTool() )
169  {
170  // leave ourselves on the stack so we come back after the move
171  break;
172  }
173  else
174  {
175  frame.PopTool( tool );
176  break;
177  }
178  }
179 
180  // A click or drag starts
181  else if( !originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) ) )
182  {
183  tpGeomMgr.SetOrigin( cursorPos );
184  tpGeomMgr.SetEnd( cursorPos );
185 
186  originSet = true;
187  controls.CaptureCursor( true );
188  controls.SetAutoPan( true );
189  }
190 
191  // another click after origin set is the end
192  // left up is also the end, as you'll only get that after a drag
193  else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
194  {
195  // second click, we're done:
196  // delegate to the point-to-point inductor creator function
197  createInductorBetween( tpGeomMgr.GetOrigin(), tpGeomMgr.GetEnd() );
198 
199  // start again if needed
200  originSet = false;
201  controls.CaptureCursor( false );
202  controls.SetAutoPan( false );
203 
204  view.SetVisible( &previewRect, false );
205  view.Update( &previewRect, KIGFX::GEOMETRY );
206  }
207 
208  // any move or drag once the origin was set updates
209  // the end point
210  else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
211  {
212  tpGeomMgr.SetAngleSnap( evt->Modifier( MD_CTRL ) );
213  tpGeomMgr.SetEnd( cursorPos );
214 
215  view.SetVisible( &previewRect, true );
216  view.Update( &previewRect, KIGFX::GEOMETRY );
217  }
218 
219  else if( evt->IsClick( BUT_RIGHT ) )
220  {
222  }
223 
224  else
225  evt->SetPassEvent();
226  }
227 
228  controls.CaptureCursor( false );
229  controls.SetAutoPan( false );
230  view.Remove( &previewRect );
231  return 0;
232 }
233 
234 
235 
237 {
242 
244 }
virtual void ShowCursor(bool aEnabled)
Function ShowCursor() Enables or disables display of cursor.
static const double inductorAreaStrokeWidth
void SetLineWidth(double aNewWidth)
Set the line width to set before drawing preview
TOOL_MENU m_menu
functions below are not yet implemented - their interface may change
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
Handle flip action in the loop by calling the item's flip method.
void SetStrokeColor(const COLOR4D &aNewColor)
Set the stroke colour to set before drawing preview
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
This file is part of the common library.
static const double inductorAreaAspect
Aspect of the preview rectangle - this is hardcoded in the microwave backend for now
static TOOL_ACTION microwaveCreateStubArc
Definition: pcb_actions.h:389
void SetCurrentCursor(wxStockCursor aStockCursorID)
Function SetCurrentCursor Set the current cursor shape for this panel.
VIEW_CONTROLS class definition.
Classes BOARD_ITEM and BOARD_CONNECTED_ITEM.
MICROWAVE_TOOL.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
void Reset(RESET_REASON aReason) override
React to model/view changes
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
virtual void SetSnapping(bool aEnabled)
Function SetSnapping() Enables/disables snapping cursor to grid.
static TOOL_ACTION microwaveCreateLine
Definition: pcb_actions.h:393
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
static const COLOR4D inductorAreaFill(0.3, 0.3, 0.5, 0.3)
virtual void Remove(VIEW_ITEM *aItem) override
Function Remove() Removes a VIEW_ITEM from the view.
Definition: pcb_view.cpp:75
PCB_BASE_EDIT_FRAME * frame() const
void createInductorBetween(const VECTOR2I &aStart, const VECTOR2I &aEnd)
Create an inductor between the two points
const PCBNEW_SELECTION & selection() const
static const COLOR4D inductorAreaStroke(0.4, 1.0, 1.0, 1.0)
Allow repeat placement of the item.
virtual void PopTool(const std::string &actionName)
virtual void CaptureCursor(bool aEnabled)
Function CaptureCursor() Forces the cursor to stay within the drawing panel area.
T Parameter() const
Function Parameter() Returns a non-standard parameter assigned to the event.
Definition: tool_event.h:435
TOOL_EVENT.
Definition: tool_event.h:171
void SetOrigin(const VECTOR2I &aOrigin)
Set the origin of the ruler (the fixed end)
KIGFX::PCB_VIEW * view() const
VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (such a...
static TOOL_ACTION microwaveCreateGap
Definition: pcb_actions.h:385
void SetFillColor(const COLOR4D &aNewColor)
Set the fill colour to set before drawing preview
void setTransitions() override
Bind handlers to corresponding TOOL_ACTIONs
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:36
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 microwaveCreateStub
Definition: pcb_actions.h:387
int drawMicrowaveInductor(const TOOL_EVENT &aEvent)
Draw a microwave inductor interactively
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:463
Handle the rotate action in the loop by calling the item's rotate method.
static TOOL_ACTION microwaveCreateFunctionShape
Definition: pcb_actions.h:391
int addMicrowaveFootprint(const TOOL_EVENT &aEvent)
Main interactive tool
KIGFX::VIEW_CONTROLS * controls() const
#define _(s)
Definition: 3d_actions.cpp:33
PCB_EDIT_FRAME is the main frame for Pcbnew.
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.
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Function Add() Adds a VIEW_ITEM to the view.
Definition: pcb_view.cpp:58
void doInteractiveItemPlacement(const std::string &aTool, INTERACTIVE_PLACER_BASE *aPlacer, const wxString &aCommitMessage, int aOptions=IPO_ROTATE|IPO_FLIP|IPO_REPEAT)
Helper function for performing a common interactive idiom: wait for a left click, place an item there...
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:42
VIEW.
Definition: view.h:61
MICROWAVE_FOOTPRINT_SHAPE
void ShowContextMenu(SELECTION &aSelection)
Function ShowContextMenu.
Definition: tool_menu.cpp:59
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.
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
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99