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 TOOL_EVENT originalEvent = aEvent; // This can change out from under us when the event loop runs
114 SCOPED_TOOL_PUSHER raii( &frame, originalEvent );
115
116 auto setCursor =
117 [&]()
118 {
119 frame.GetCanvas()->SetCurrentCursor( KICURSOR::PENCIL );
120 };
121
122 Activate();
123 // Must be done after Activate() so that it gets set into the correct context
124 controls.ShowCursor( true );
125 controls.CaptureCursor( false );
126 controls.SetAutoPan( false );
127 // Set initial cursor
128 setCursor();
129
130 bool originSet = false;
132 CENTRELINE_RECT_ITEM previewRect( tpGeomMgr, inductorAreaAspect );
133
134 previewRect.SetFillColor( inductorAreaFill );
135 previewRect.SetStrokeColor( inductorAreaStroke );
137 view.Add( &previewRect );
138
139 while( auto evt = Wait() )
140 {
141 setCursor();
142 VECTOR2I cursorPos = controls.GetCursorPosition();
143
144 auto cleanup =
145 [&] ()
146 {
147 originSet = false;
148 controls.CaptureCursor( false );
149 controls.SetAutoPan( false );
150 view.SetVisible( &previewRect, false );
151 view.Update( &previewRect, KIGFX::GEOMETRY );
152 };
153
154 if( evt->IsCancelInteractive() )
155 {
156 if( originSet )
157 cleanup();
158 else
159 break;
160 }
161 else if( evt->IsActivate() )
162 {
163 if( originSet )
164 cleanup();
165
166 if( evt->IsMoveTool() )
167 {
168 // Make sure we come back after the move tool is done
169 frame.PushTool( originalEvent );
170 }
171
172 break;
173 }
174 // A click or drag starts
175 else if( !originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) ) )
176 {
177 tpGeomMgr.SetOrigin( cursorPos );
178 tpGeomMgr.SetEnd( cursorPos );
179
180 originSet = true;
181 controls.CaptureCursor( true );
182 controls.SetAutoPan( true );
183 }
184 // another click after origin set is the end
185 // left up is also the end, as you'll only get that after a drag
186 else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
187 {
188 // second click, we're done:
189 // delegate to the point-to-point inductor creator function
190 createInductorBetween( tpGeomMgr.GetOrigin(), tpGeomMgr.GetEnd() );
191
192 // start again if needed
193 originSet = false;
194 controls.CaptureCursor( false );
195 controls.SetAutoPan( false );
196
197 view.SetVisible( &previewRect, false );
198 view.Update( &previewRect, KIGFX::GEOMETRY );
199 }
200 // any move or drag once the origin was set updates
201 // the end point
202 else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
203 {
204 tpGeomMgr.SetAngleSnap( GetAngleSnapMode() );
205 tpGeomMgr.SetEnd( cursorPos );
206
207 view.SetVisible( &previewRect, true );
208 view.Update( &previewRect, KIGFX::GEOMETRY );
209 }
210 else if( evt->IsClick( BUT_RIGHT ) )
211 {
212 m_menu->ShowContextMenu( selection() );
213 }
214 else
215 {
216 evt->SetPassEvent();
217 }
218 }
219
220 view.Remove( &previewRect );
221
222 frame.GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
223 controls.CaptureCursor( false );
224 controls.SetAutoPan( false );
225 return 0;
226}
227
228
229
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