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 The KiCad Developers, see AUTHORS.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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <bitmaps.h>
21#include <board_commit.h>
22#include <board_item.h>
24#include <footprint.h>
25#include <confirm.h>
30#include <tool/tool_manager.h>
31#include <tools/pcb_actions.h>
32#include <tools/pcb_selection.h>
33#include <view/view_controls.h>
34#include <view/view.h>
35
36
38 PCB_TOOL_BASE( "pcbnew.MicrowaveTool" )
39{
40}
41
42
45
46
48{
49}
50
51
53{
54 struct MICROWAVE_PLACER : public INTERACTIVE_PLACER_BASE
55 {
56 MICROWAVE_PLACER( MICROWAVE_TOOL* aTool, MICROWAVE_FOOTPRINT_SHAPE aType ) :
57 m_tool( aTool ),
58 m_itemType( aType )
59 { };
60
61 virtual ~MICROWAVE_PLACER()
62 {
63 }
64
65 std::unique_ptr<BOARD_ITEM> CreateItem() override
66 {
67 switch( m_itemType )
68 {
72 return std::unique_ptr<FOOTPRINT>( m_tool->createFootprint( m_itemType ) );
73
75 return std::unique_ptr<FOOTPRINT>( m_tool->createPolygonShape() );
76
77 default:
78 return std::unique_ptr<FOOTPRINT>();
79 };
80 }
81
82 private:
83 MICROWAVE_TOOL* m_tool;
85 };
86
87 MICROWAVE_PLACER placer( this, aEvent.Parameter<MICROWAVE_FOOTPRINT_SHAPE>() );
88
89 doInteractiveItemPlacement( aEvent, &placer, _( "Place microwave feature" ),
91
92 return 0;
93}
94
95
96static const COLOR4D inductorAreaFill( 0.3, 0.3, 0.5, 0.3 );
97static const COLOR4D inductorAreaStroke( 0.4, 1.0, 1.0, 1.0 );
98static const double inductorAreaStrokeWidth = 1.0;
99
102static const double inductorAreaAspect = 0.5;
103
104
106{
107 using namespace KIGFX::PREVIEW;
108
112
113 frame.PushTool( aEvent );
114
115 auto setCursor =
116 [&]()
117 {
118 frame.GetCanvas()->SetCurrentCursor( KICURSOR::PENCIL );
119 };
120
121 Activate();
122 // Must be done after Activate() so that it gets set into the correct context
123 controls.ShowCursor( true );
124 controls.CaptureCursor( false );
125 controls.SetAutoPan( false );
126 // Set initial cursor
127 setCursor();
128
129 bool originSet = false;
131 CENTRELINE_RECT_ITEM previewRect( tpGeomMgr, inductorAreaAspect );
132
133 previewRect.SetFillColor( inductorAreaFill );
134 previewRect.SetStrokeColor( inductorAreaStroke );
136 view.Add( &previewRect );
137
138 while( auto evt = Wait() )
139 {
140 setCursor();
141 VECTOR2I cursorPos = controls.GetCursorPosition();
142
143 auto cleanup =
144 [&] ()
145 {
146 originSet = false;
147 controls.CaptureCursor( false );
148 controls.SetAutoPan( false );
149 view.SetVisible( &previewRect, false );
150 view.Update( &previewRect, KIGFX::GEOMETRY );
151 };
152
153 if( evt->IsCancelInteractive() )
154 {
155 if( originSet )
156 cleanup();
157 else
158 {
159 frame.PopTool( aEvent );
160 break;
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( aEvent );
176 break;
177 }
178 }
179 // A click or drag starts
180 else if( !originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) ) )
181 {
182 tpGeomMgr.SetOrigin( cursorPos );
183 tpGeomMgr.SetEnd( cursorPos );
184
185 originSet = true;
186 controls.CaptureCursor( true );
187 controls.SetAutoPan( true );
188 }
189 // another click after origin set is the end
190 // left up is also the end, as you'll only get that after a drag
191 else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
192 {
193 // second click, we're done:
194 // delegate to the point-to-point inductor creator function
195 createInductorBetween( tpGeomMgr.GetOrigin(), tpGeomMgr.GetEnd() );
196
197 // start again if needed
198 originSet = false;
199 controls.CaptureCursor( false );
200 controls.SetAutoPan( false );
201
202 view.SetVisible( &previewRect, false );
203 view.Update( &previewRect, KIGFX::GEOMETRY );
204 }
205 // any move or drag once the origin was set updates
206 // the end point
207 else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
208 {
209 tpGeomMgr.SetAngleSnap( GetAngleSnapMode() );
210 tpGeomMgr.SetEnd( cursorPos );
211
212 view.SetVisible( &previewRect, true );
213 view.Update( &previewRect, KIGFX::GEOMETRY );
214 }
215 else if( evt->IsClick( BUT_RIGHT ) )
216 {
217 m_menu->ShowContextMenu( selection() );
218 }
219 else
220 {
221 evt->SetPassEvent();
222 }
223 }
224
225 view.Remove( &previewRect );
226
227 frame.GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
228 controls.CaptureCursor( false );
229 controls.SetAutoPan( false );
230 return 0;
231}
232
233
234
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
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.
An interface for classes handling user events controlling the view behavior such as zooming,...
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:63
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
static TOOL_ACTION microwaveCreateStubArc
static TOOL_ACTION microwaveCreateStub
static TOOL_ACTION microwaveCreateLine
static TOOL_ACTION microwaveCreateFunctionShape
The main frame for Pcbnew.
T * frame() const
KIGFX::PCB_VIEW * view() const
LEADER_MODE GetAngleSnapMode() const
Get the current angle snapping mode.
KIGFX::VIEW_CONTROLS * controls() const
PCB_TOOL_BASE(TOOL_ID aId, const std::string &aName)
Constructor.
@ 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
T * getEditFrame() const
Return the application window object, casted to requested user type.
Definition tool_base.h:182
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition tool_base.cpp:40
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition tool_base.cpp:34
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:74
Generic, UI-independent tool event.
Definition tool_event.h:167
T Parameter() const
Return a parameter assigned to the event.
Definition tool_event.h:469
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.
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 SetAngleSnap(LEADER_MODE aSnap)
void SetEnd(const VECTOR2I &aEnd)
Set the current end of the rectangle (the end that moves with the cursor.
This file is part of the common library.
@ ARROW
Definition cursors.h:42
@ PENCIL
Definition cursors.h:48
#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:51
@ BUT_LEFT
Definition tool_event.h:128
@ BUT_RIGHT
Definition tool_event.h:129
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683