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, 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 <tools/pcb_selection.h>
37#include <view/view_controls.h>
38#include <view/view.h>
39
40
42 PCB_TOOL_BASE( "pcbnew.MicrowaveTool" )
43{
44}
45
46
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<FOOTPRINT>( m_tool->createFootprint( m_itemType ) );
77
79 return std::unique_ptr<FOOTPRINT>( m_tool->createPolygonShape() );
80
81 default:
82 return std::unique_ptr<FOOTPRINT>();
83 };
84 }
85
86 private:
87 MICROWAVE_TOOL* m_tool;
89 };
90
91 MICROWAVE_PLACER placer( this, aEvent.Parameter<MICROWAVE_FOOTPRINT_SHAPE>() );
92
93 doInteractiveItemPlacement( aEvent, &placer, _( "Place microwave feature" ),
95
96 return 0;
97}
98
99
100static const COLOR4D inductorAreaFill( 0.3, 0.3, 0.5, 0.3 );
101static const COLOR4D inductorAreaStroke( 0.4, 1.0, 1.0, 1.0 );
102static const double inductorAreaStrokeWidth = 1.0;
103
106static const double inductorAreaAspect = 0.5;
107
108
110{
111 using namespace KIGFX::PREVIEW;
112
116
117 frame.PushTool( aEvent );
118
119 auto setCursor =
120 [&]()
121 {
122 frame.GetCanvas()->SetCurrentCursor( KICURSOR::PENCIL );
123 };
124
125 Activate();
126 // Must be done after Activate() so that it gets set into the correct context
127 controls.ShowCursor( true );
128 controls.CaptureCursor( false );
129 controls.SetAutoPan( false );
130 // Set initial cursor
131 setCursor();
132
133 bool originSet = false;
135 CENTRELINE_RECT_ITEM previewRect( tpGeomMgr, inductorAreaAspect );
136
137 previewRect.SetFillColor( inductorAreaFill );
138 previewRect.SetStrokeColor( inductorAreaStroke );
140 view.Add( &previewRect );
141
142 while( auto evt = Wait() )
143 {
144 setCursor();
145 VECTOR2I cursorPos = controls.GetCursorPosition();
146
147 auto cleanup =
148 [&] ()
149 {
150 originSet = false;
151 controls.CaptureCursor( false );
152 controls.SetAutoPan( false );
153 view.SetVisible( &previewRect, false );
154 view.Update( &previewRect, KIGFX::GEOMETRY );
155 };
156
157 if( evt->IsCancelInteractive() )
158 {
159 if( originSet )
160 cleanup();
161 else
162 {
163 frame.PopTool( aEvent );
164 break;
165 }
166 }
167 else if( evt->IsActivate() )
168 {
169 if( originSet )
170 cleanup();
171
172 if( evt->IsMoveTool() )
173 {
174 // leave ourselves on the stack so we come back after the move
175 break;
176 }
177 else
178 {
179 frame.PopTool( aEvent );
180 break;
181 }
182 }
183 // A click or drag starts
184 else if( !originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) ) )
185 {
186 tpGeomMgr.SetOrigin( cursorPos );
187 tpGeomMgr.SetEnd( cursorPos );
188
189 originSet = true;
190 controls.CaptureCursor( true );
191 controls.SetAutoPan( true );
192 }
193 // another click after origin set is the end
194 // left up is also the end, as you'll only get that after a drag
195 else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
196 {
197 // second click, we're done:
198 // delegate to the point-to-point inductor creator function
199 createInductorBetween( tpGeomMgr.GetOrigin(), tpGeomMgr.GetEnd() );
200
201 // start again if needed
202 originSet = false;
203 controls.CaptureCursor( false );
204 controls.SetAutoPan( false );
205
206 view.SetVisible( &previewRect, false );
207 view.Update( &previewRect, KIGFX::GEOMETRY );
208 }
209 // any move or drag once the origin was set updates
210 // the end point
211 else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
212 {
213 tpGeomMgr.SetAngleSnap( GetAngleSnapMode() );
214 tpGeomMgr.SetEnd( cursorPos );
215
216 view.SetVisible( &previewRect, true );
217 view.Update( &previewRect, KIGFX::GEOMETRY );
218 }
219 else if( evt->IsClick( BUT_RIGHT ) )
220 {
221 m_menu->ShowContextMenu( selection() );
222 }
223 else
224 {
225 evt->SetPassEvent();
226 }
227 }
228
229 view.Remove( &previewRect );
230
231 frame.GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
232 controls.CaptureCursor( false );
233 controls.SetAutoPan( false );
234 return 0;
235}
236
237
238
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:105
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:67
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:186
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition tool_base.cpp:44
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition tool_base.cpp:38
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:78
Generic, UI-independent tool event.
Definition tool_event.h:171
T Parameter() const
Return a parameter assigned to the event.
Definition tool_event.h:473
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:46
@ PENCIL
Definition cursors.h:52
#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:55
@ BUT_LEFT
Definition tool_event.h:132
@ BUT_RIGHT
Definition tool_event.h:133
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695