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-2021 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 <board_item.h>
27 #include <class_draw_panel_gal.h>
28 #include <footprint.h>
29 #include <confirm.h>
34 #include <tool/tool_manager.h>
35 #include <tools/pcb_actions.h>
36 #include <view/view_controls.h>
37 #include <view/view.h>
38 
39 
41  PCB_TOOL_BASE( "pcbnew.MicrowaveTool" )
42 {
43 }
44 
45 
47 {}
48 
49 
51 {
52 }
53 
54 
56 {
57  struct MICROWAVE_PLACER : public INTERACTIVE_PLACER_BASE
58  {
59  MICROWAVE_PLACER( MICROWAVE_TOOL* aTool, MICROWAVE_FOOTPRINT_SHAPE aType ) :
60  m_tool( aTool ),
61  m_itemType( aType )
62  { };
63 
64  virtual ~MICROWAVE_PLACER()
65  {
66  }
67 
68  std::unique_ptr<BOARD_ITEM> CreateItem() override
69  {
70  switch( m_itemType )
71  {
75  return std::unique_ptr<FOOTPRINT>( m_tool->createFootprint( m_itemType ) );
76 
78  return std::unique_ptr<FOOTPRINT>( m_tool->createPolygonShape() );
79 
80  default:
81  return std::unique_ptr<FOOTPRINT>();
82  };
83  }
84 
85  private:
86  MICROWAVE_TOOL* m_tool;
87  MICROWAVE_FOOTPRINT_SHAPE m_itemType;
88  };
89 
90  MICROWAVE_PLACER placer( this, aEvent.Parameter<MICROWAVE_FOOTPRINT_SHAPE>() );
91 
92  doInteractiveItemPlacement( aEvent.GetCommandStr().get(), &placer,
93  _( "Place microwave feature" ),
95 
96  return 0;
97 }
98 
99 
100 static const COLOR4D inductorAreaFill( 0.3, 0.3, 0.5, 0.3 );
101 static const COLOR4D inductorAreaStroke( 0.4, 1.0, 1.0, 1.0 );
102 static const double inductorAreaStrokeWidth = 1.0;
103 
106 static const double inductorAreaAspect = 0.5;
107 
108 
110 {
111  using namespace KIGFX::PREVIEW;
112 
113  KIGFX::VIEW& view = *getView();
115  PCB_EDIT_FRAME& frame = *getEditFrame<PCB_EDIT_FRAME>();
116 
117  std::string tool = aEvent.GetCommandStr().get();
118  frame.PushTool( tool );
119  Activate();
120 
121  TWO_POINT_GEOMETRY_MANAGER tpGeomMgr;
122 
123  CENTRELINE_RECT_ITEM previewRect( tpGeomMgr, inductorAreaAspect );
124 
125  previewRect.SetFillColor( inductorAreaFill );
126  previewRect.SetStrokeColor( inductorAreaStroke );
127  previewRect.SetLineWidth( inductorAreaStrokeWidth );
128 
129  bool originSet = false;
130 
131  controls.ShowCursor( true );
132  controls.CaptureCursor( false );
133  controls.SetAutoPan( false );
134 
135  view.Add( &previewRect );
136 
137  auto setCursor =
138  [&]()
139  {
141  };
142 
143  // Set initial cursor
144  setCursor();
145 
146  while( auto evt = Wait() )
147  {
148  setCursor();
149  VECTOR2I cursorPos = controls.GetCursorPosition();
150 
151  auto cleanup =
152  [&] ()
153  {
154  originSet = false;
155  controls.CaptureCursor( false );
156  controls.SetAutoPan( false );
157  view.SetVisible( &previewRect, false );
158  view.Update( &previewRect, KIGFX::GEOMETRY );
159  };
160 
161  if( evt->IsCancelInteractive() )
162  {
163  if( originSet )
164  cleanup();
165  else
166  {
167  frame.PopTool( tool );
168  break;
169  }
170  }
171  else if( evt->IsActivate() )
172  {
173  if( originSet )
174  cleanup();
175 
176  if( evt->IsMoveTool() )
177  {
178  // leave ourselves on the stack so we come back after the move
179  break;
180  }
181  else
182  {
183  frame.PopTool( tool );
184  break;
185  }
186  }
187  // A click or drag starts
188  else if( !originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) ) )
189  {
190  tpGeomMgr.SetOrigin( cursorPos );
191  tpGeomMgr.SetEnd( cursorPos );
192 
193  originSet = true;
194  controls.CaptureCursor( true );
195  controls.SetAutoPan( true );
196  }
197  // another click after origin set is the end
198  // left up is also the end, as you'll only get that after a drag
199  else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
200  {
201  // second click, we're done:
202  // delegate to the point-to-point inductor creator function
203  createInductorBetween( tpGeomMgr.GetOrigin(), tpGeomMgr.GetEnd() );
204 
205  // start again if needed
206  originSet = false;
207  controls.CaptureCursor( false );
208  controls.SetAutoPan( false );
209 
210  view.SetVisible( &previewRect, false );
211  view.Update( &previewRect, KIGFX::GEOMETRY );
212  }
213  // any move or drag once the origin was set updates
214  // the end point
215  else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
216  {
217  tpGeomMgr.SetAngleSnap( evt->Modifier( MD_CTRL ) );
218  tpGeomMgr.SetEnd( cursorPos );
219 
220  view.SetVisible( &previewRect, true );
221  view.Update( &previewRect, KIGFX::GEOMETRY );
222  }
223  else if( evt->IsClick( BUT_RIGHT ) )
224  {
226  }
227  else
228  {
229  evt->SetPassEvent();
230  }
231  }
232 
233  view.Remove( &previewRect );
234 
236  controls.CaptureCursor( false );
237  controls.SetAutoPan( false );
238  return 0;
239 }
240 
241 
242 
244 {
250 
252 }
virtual void ShowCursor(bool aEnabled)
Enable or disables display of cursor.
static const double inductorAreaStrokeWidth
Aspect of the preview rectangle - this is hardcoded in the microwave backend for now.
Represent an area drawn by drawing a rectangle of a given aspect along a vector, with the midpoint of...
TOOL_MENU m_menu
The functions below are not yet implemented - their interface may change.
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Suspend execution of the tool until an event specified in aEventList arrives.
Handle flip action in the loop by calling the item's flip method.
void SetStrokeColor(const COLOR4D &aNewColor)
Set the fill color to set before drawing preview.
This file is part of the common library.
static const double inductorAreaAspect
static TOOL_ACTION microwaveCreateStubArc
Definition: pcb_actions.h:411
~MICROWAVE_TOOL()
React to model/view changes.
Represent a very simple geometry manager for items that have a start and end point.
Tool responsible for adding microwave features to PCBs.
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
Bind handlers to corresponding TOOL_ACTIONs.
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
static TOOL_ACTION microwaveCreateLine
Definition: pcb_actions.h:415
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
static const COLOR4D inductorAreaFill(0.3, 0.3, 0.5, 0.3)
void SetCurrentCursor(KICURSOR cursor)
Set the current cursor shape for this panel.
virtual void Remove(VIEW_ITEM *aItem) override
Remove 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)
Draw a microwave inductor interactively.
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const override
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: pcb_view.cpp:92
static const COLOR4D inductorAreaStroke(0.4, 1.0, 1.0, 1.0)
const PCB_SELECTION & selection() const
Allow repeat placement of the item.
virtual void PopTool(const std::string &actionName)
virtual void CaptureCursor(bool aEnabled)
Force the cursor to stay within the drawing panel area.
T Parameter() const
Return a non-standard parameter assigned to the event.
Definition: tool_event.h:443
Generic, UI-independent tool event.
Definition: tool_event.h:173
void SetOrigin(const VECTOR2I &aOrigin)
< Set the origin of the ruler (the fixed end)
KIGFX::PCB_VIEW * view() const
An interface for classes handling user events controlling the view behavior such as zooming,...
static TOOL_ACTION microwaveCreateGap
Definition: pcb_actions.h:407
void SetFillColor(const COLOR4D &aNewColor)
Set the line width to set before drawing preview.
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:36
virtual void SetAutoPan(bool aEnabled)
Turn on/off auto panning (this feature is used when there is a tool active (eg.
static TOOL_ACTION microwaveCreateStub
Definition: pcb_actions.h:409
int drawMicrowaveInductor(const TOOL_EVENT &aEvent)
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:471
Handle the rotate action in the loop by calling the item's rotate method.
static TOOL_ACTION microwaveCreateFunctionShape
Definition: pcb_actions.h:413
int addMicrowaveFootprint(const TOOL_EVENT &aEvent)
< Main interactive tool
KIGFX::VIEW_CONTROLS * controls() const
#define _(s)
Definition: 3d_actions.cpp:33
The main frame for Pcbnew.
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1454
void Activate()
Run the tool.
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Add 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
Return the instance of VIEW_CONTROLS object used in the application.
Definition: tool_base.cpp:42
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
MICROWAVE_FOOTPRINT_SHAPE
void ShowContextMenu(SELECTION &aSelection)
Helper function to set and immediately show a CONDITIONAL_MENU in concert with the given SELECTION.
Definition: tool_menu.cpp:59
VECTOR2D GetCursorPosition() const
Return 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.
Position or shape has changed.
Definition: view_item.h:54
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:98