KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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>
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 {
72 case MICROWAVE_FOOTPRINT_SHAPE::GAP:
73 case MICROWAVE_FOOTPRINT_SHAPE::STUB:
74 case MICROWAVE_FOOTPRINT_SHAPE::STUB_ARC:
75 return std::unique_ptr<FOOTPRINT>( m_tool->createFootprint( m_itemType ) );
76
77 case MICROWAVE_FOOTPRINT_SHAPE::FUNCTION_SHAPE:
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;
88 };
89
90 MICROWAVE_PLACER placer( this, aEvent.Parameter<MICROWAVE_FOOTPRINT_SHAPE>() );
91
92 doInteractiveItemPlacement( aEvent, &placer, _( "Place microwave feature" ),
94
95 return 0;
96}
97
98
99static const COLOR4D inductorAreaFill( 0.3, 0.3, 0.5, 0.3 );
100static const COLOR4D inductorAreaStroke( 0.4, 1.0, 1.0, 1.0 );
101static const double inductorAreaStrokeWidth = 1.0;
102
105static const double inductorAreaAspect = 0.5;
106
107
109{
110 using namespace KIGFX::PREVIEW;
111
114 PCB_EDIT_FRAME& frame = *getEditFrame<PCB_EDIT_FRAME>();
115
116 frame.PushTool( aEvent );
117
118 auto setCursor =
119 [&]()
120 {
121 frame.GetCanvas()->SetCurrentCursor( KICURSOR::PENCIL );
122 };
123
124 Activate();
125 // Must be done after Activate() so that it gets set into the correct context
126 controls.ShowCursor( true );
127 controls.CaptureCursor( false );
128 controls.SetAutoPan( false );
129 // Set initial cursor
130 setCursor();
131
132 bool originSet = false;
134 CENTRELINE_RECT_ITEM previewRect( tpGeomMgr, inductorAreaAspect );
135
136 previewRect.SetFillColor( inductorAreaFill );
137 previewRect.SetStrokeColor( inductorAreaStroke );
139 view.Add( &previewRect );
140
141 while( auto evt = Wait() )
142 {
143 setCursor();
144 VECTOR2I cursorPos = controls.GetCursorPosition();
145
146 auto cleanup =
147 [&] ()
148 {
149 originSet = false;
150 controls.CaptureCursor( false );
151 controls.SetAutoPan( false );
152 view.SetVisible( &previewRect, false );
153 view.Update( &previewRect, KIGFX::GEOMETRY );
154 };
155
156 if( evt->IsCancelInteractive() )
157 {
158 if( originSet )
159 cleanup();
160 else
161 {
162 frame.PopTool( aEvent );
163 break;
164 }
165 }
166 else if( evt->IsActivate() )
167 {
168 if( originSet )
169 cleanup();
170
171 if( evt->IsMoveTool() )
172 {
173 // leave ourselves on the stack so we come back after the move
174 break;
175 }
176 else
177 {
178 frame.PopTool( aEvent );
179 break;
180 }
181 }
182 // A click or drag starts
183 else if( !originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) ) )
184 {
185 tpGeomMgr.SetOrigin( cursorPos );
186 tpGeomMgr.SetEnd( cursorPos );
187
188 originSet = true;
189 controls.CaptureCursor( true );
190 controls.SetAutoPan( true );
191 }
192 // another click after origin set is the end
193 // left up is also the end, as you'll only get that after a drag
194 else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
195 {
196 // second click, we're done:
197 // delegate to the point-to-point inductor creator function
198 createInductorBetween( tpGeomMgr.GetOrigin(), tpGeomMgr.GetEnd() );
199
200 // start again if needed
201 originSet = false;
202 controls.CaptureCursor( false );
203 controls.SetAutoPan( false );
204
205 view.SetVisible( &previewRect, false );
206 view.Update( &previewRect, KIGFX::GEOMETRY );
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( Is45Limited() );
213 tpGeomMgr.SetEnd( cursorPos );
214
215 view.SetVisible( &previewRect, true );
216 view.Update( &previewRect, KIGFX::GEOMETRY );
217 }
218 else if( evt->IsClick( BUT_RIGHT ) )
219 {
220 m_menu->ShowContextMenu( selection() );
221 }
222 else
223 {
224 evt->SetPassEvent();
225 }
226 }
227
228 view.Remove( &previewRect );
229
230 frame.GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
231 controls.CaptureCursor( false );
232 controls.SetAutoPan( false );
233 return 0;
234}
235
236
237
239{
245
247}
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
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:91
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Add a VIEW_ITEM to the view.
Definition: pcb_view.cpp:57
virtual void Remove(VIEW_ITEM *aItem) override
Remove a VIEW_ITEM from the view.
Definition: pcb_view.cpp:74
Represent an area drawn by drawing a rectangle of a given aspect along a vector, with the midpoint of...
void SetFillColor(const COLOR4D &aNewColor)
Set the line width to set before drawing preview.
void SetStrokeColor(const COLOR4D &aNewColor)
Set the fill color to set before drawing preview.
Represent a very simple geometry manager for items that have a start and end point.
void SetOrigin(const VECTOR2I &aOrigin)
< Set the origin of the ruler (the fixed end)
void SetEnd(const VECTOR2I &aEnd)
Set the current end of the rectangle (the end that moves with the cursor.
An interface for classes handling user events controlling the view behavior such as zooming,...
virtual void CaptureCursor(bool aEnabled)
Force the cursor to stay within the drawing panel area.
virtual void ShowCursor(bool aEnabled)
Enable or disables display of cursor.
VECTOR2D GetCursorPosition() const
Return the current cursor position in world coordinates.
virtual void SetAutoPan(bool aEnabled)
Turn on/off auto panning (this feature is used when there is a tool active (eg.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1614
Tool responsible for adding microwave features to PCBs.
int drawMicrowaveInductor(const TOOL_EVENT &aEvent)
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
void Reset(RESET_REASON aReason) override
Bind handlers to corresponding TOOL_ACTIONs.
void createInductorBetween(const VECTOR2I &aStart, const VECTOR2I &aEnd)
Draw a microwave inductor interactively.
int addMicrowaveFootprint(const TOOL_EVENT &aEvent)
< Main interactive tool
~MICROWAVE_TOOL()
React to model/view changes.
static TOOL_ACTION microwaveCreateGap
Definition: pcb_actions.h:513
static TOOL_ACTION microwaveCreateStubArc
Definition: pcb_actions.h:517
static TOOL_ACTION microwaveCreateStub
Definition: pcb_actions.h:515
static TOOL_ACTION microwaveCreateLine
Definition: pcb_actions.h:521
static TOOL_ACTION microwaveCreateFunctionShape
Definition: pcb_actions.h:519
The main frame for Pcbnew.
T * frame() const
KIGFX::PCB_VIEW * view() const
virtual bool Is45Limited() const
Should the tool use its 45° mode option?
KIGFX::VIEW_CONTROLS * controls() const
@ IPO_FLIP
Handle flip action in the loop by calling the item's flip method.
@ IPO_ROTATE
Handle the rotate action in the loop by calling the item's rotate method.
@ IPO_REPEAT
Allow repeat placement of the item.
void doInteractiveItemPlacement(const TOOL_EVENT &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...
const PCB_SELECTION & selection() const
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition: tool_base.cpp:42
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:36
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:167
T Parameter() const
Return a parameter assigned to the event.
Definition: tool_event.h:460
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).
std::unique_ptr< 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.
void Activate()
Run the tool.
This file is part of the common library.
#define _(s)
static const double inductorAreaAspect
static const COLOR4D inductorAreaFill(0.3, 0.3, 0.5, 0.3)
static const COLOR4D inductorAreaStroke(0.4, 1.0, 1.0, 1.0)
static const double inductorAreaStrokeWidth
Aspect of the preview rectangle - this is hardcoded in the microwave backend for now.
MICROWAVE_FOOTPRINT_SHAPE
@ GEOMETRY
Position or shape has changed.
Definition: view_item.h:54
@ BUT_LEFT
Definition: tool_event.h:131
@ BUT_RIGHT
Definition: tool_event.h:132