KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drawing_tool.h
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) 2014-2017 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Maciej Suminski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef __DRAWING_TOOL_H
24#define __DRAWING_TOOL_H
25
26#include <functional>
27#include <stack>
28#include <optional>
29#include <tool/tool_menu.h>
30#include <tools/pcb_selection.h>
31#include <tools/pcb_tool_base.h>
32#include <tools/pcb_actions.h>
33
34namespace KIGFX
35{
36 class VIEW;
37 class VIEW_CONTROLS;
38}
39
40class BOARD;
42class PCB_SHAPE;
46class STATUS_MIN_MAX_POPUP;
47
48
52
54{
55public:
58
60 bool Init() override;
61
63 void Reset( RESET_REASON aReason ) override;
64
90
95 MODE GetDrawingMode() const;
96
99 std::vector<BOARD_ITEM*> DrawSpecificationStackup( const VECTOR2I& origin, PCB_LAYER_ID aLayer,
100 bool aDrawNow, VECTOR2I* tablesize );
101
104 int PlaceStackup( const TOOL_EVENT& aEvent );
105
108 int PlaceTuningPattern( const TOOL_EVENT& aEvent );
109
117 int DrawLine( const TOOL_EVENT& aEvent );
118
125 int DrawRectangle( const TOOL_EVENT& aEvent );
126
133 int DrawCircle( const TOOL_EVENT& aEvent );
134
135 int DrawEllipse( const TOOL_EVENT& aEvent );
136
137 int DrawEllipseArc( const TOOL_EVENT& aEvent );
138
146 int DrawArc( const TOOL_EVENT& aEvent );
147
153 int DrawBezier( const TOOL_EVENT& aEvent );
154
159 int PlaceReferenceImage( const TOOL_EVENT& aEvent );
160
164 int PlacePoint( const TOOL_EVENT& aEvent );
165
170 int PlaceText( const TOOL_EVENT& aEvent );
171
172 /*
173 * Start interactively drawing a table (rows & columns of TEXTBOXes).
174 */
175 int DrawTable( const TOOL_EVENT& aEvent );
176
182 int DrawBarcode( const TOOL_EVENT& aEvent );
183
191 int DrawDimension( const TOOL_EVENT& aEvent );
192
206 int DrawZone( const TOOL_EVENT& aEvent );
207
208 int DrawVia( const TOOL_EVENT& aEvent );
209
213 int PlaceImportedGraphics( const TOOL_EVENT& aEvent );
214
228 std::vector<BOARD_ITEM*>& aItems,
229 std::vector<BOARD_ITEM*>& aPreview, LSET* aLayers );
230
231
235 int SetAnchor( const TOOL_EVENT& aEvent );
236
237
239 void setTransitions() override;
240
241 void SetStroke( const STROKE_PARAMS& aStroke, PCB_LAYER_ID aLayer )
242 {
243 m_layer = aLayer;
244 m_stroke = aStroke;
245 }
246
247 void UpdateStatusBar() const;
248
249private:
261 bool drawShape( const TOOL_EVENT& aTool, PCB_SHAPE** aGraphic,
262 std::optional<VECTOR2D> aStartingPoint,
263 std::stack<PCB_SHAPE*>* aCommittedGraphics );
264
265 int runSimpleShapeDraw( const TOOL_EVENT& aEvent, SHAPE_T aShapeType, MODE aMode, const wxString& aCommitLabel,
266 std::function<bool( const TOOL_EVENT&, PCB_SHAPE**, std::optional<VECTOR2D> )> aDrawer );
267
280 bool drawManagedShape( const TOOL_EVENT& aTool, std::unique_ptr<PCB_SHAPE>& aGraphic,
281 SHAPE_DRAW_BEHAVIOR& aBehavior,
282 const std::vector<VECTOR2D>& aInitialPts );
283
293
306 bool getSourceZoneForAction( ZONE_MODE aMode, ZONE** aZone );
307
314
322 VECTOR2I getClampedDifferenceEnd( const VECTOR2I& aOrigin, const VECTOR2I& aEnd )
323 {
324 typedef std::numeric_limits<int> coord_limits;
325 const int guardValue = 1;
326
327 VECTOR2I::extended_type maxDiff = coord_limits::max() - guardValue;
328
329 VECTOR2I::extended_type xDiff = VECTOR2I::extended_type( aEnd.x ) - aOrigin.x;
330 VECTOR2I::extended_type yDiff = VECTOR2I::extended_type( aEnd.y ) - aOrigin.y;
331
332 if( xDiff > maxDiff )
333 xDiff = maxDiff;
334 if( yDiff > maxDiff )
335 yDiff = maxDiff;
336
337 if( xDiff < -maxDiff )
338 xDiff = -maxDiff;
339 if( yDiff < -maxDiff )
340 yDiff = -maxDiff;
341
342 return aOrigin + VECTOR2I( int( xDiff ), int( yDiff ) );
343 }
344
352 VECTOR2I getClampedRadiusEnd( const VECTOR2I& aOrigin, const VECTOR2I& aEnd )
353 {
354 typedef std::numeric_limits<int> coord_limits;
355 const int guardValue = 10;
356
357 VECTOR2I::extended_type xDiff = VECTOR2I::extended_type( aEnd.x ) - aOrigin.x;
358 VECTOR2I::extended_type yDiff = VECTOR2I::extended_type( aEnd.y ) - aOrigin.y;
359
360 double maxRadius = coord_limits::max() / 2 - guardValue;
361 double radius = std::hypot( xDiff, yDiff );
362
363 if( radius > maxRadius )
364 {
365 double scaleFactor = maxRadius / radius;
366
367 xDiff = KiROUND<double, int>( xDiff * scaleFactor );
368 yDiff = KiROUND<double, int>( yDiff * scaleFactor );
369 }
370
371 return aOrigin + VECTOR2I( int( xDiff ), int( yDiff ) );
372 }
373
379 bool m_inDrawingTool; // Re-entrancy guard
380
381 PCB_LAYER_ID m_layer; // The layer we last drew on
382 STROKE_PARAMS m_stroke; // Current stroke for multi-segment drawing
384
388
389 static const unsigned int WIDTH_STEP; // Amount of width change for one -/+ key press
390 static const unsigned int COORDS_PADDING; // Padding from coordinates limits for this tool
391
392
393 friend class ZONE_CREATE_HELPER; // give internal access to helper classes
394};
395
396#endif /* __DRAWING_TOOL_H */
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:373
TEXT_ATTRIBUTES m_textAttrs
MODE GetDrawingMode() const
Return the current drawing mode of the DRAWING_TOOL or MODE::NONE if not currently in any drawing mod...
void SetStroke(const STROKE_PARAMS &aStroke, PCB_LAYER_ID aLayer)
PCB_SELECTION m_preview
int DrawEllipseArc(const TOOL_EVENT &aEvent)
PCB_LAYER_ID m_layer
int DrawTable(const TOOL_EVENT &aEvent)
int SetAnchor(const TOOL_EVENT &aEvent)
Place the footprint anchor (only in footprint editor).
int DrawDimension(const TOOL_EVENT &aEvent)
Start interactively drawing a dimension.
int PlacePoint(const TOOL_EVENT &aEvent)
Place a reference 0D point.
int DrawVia(const TOOL_EVENT &aEvent)
KIGFX::VIEW_CONTROLS * m_controls
friend class ZONE_CREATE_HELPER
bool drawManagedShape(const TOOL_EVENT &aTool, std::unique_ptr< PCB_SHAPE > &aGraphic, SHAPE_DRAW_BEHAVIOR &aBehavior, const std::vector< VECTOR2D > &aInitialPts)
Run the interactive drawing event loop for a shape, driven by a SHAPE_DRAW_BEHAVIOR.
KIGFX::VIEW * m_view
STROKE_PARAMS m_stroke
int DrawZone(const TOOL_EVENT &aEvent)
Start interactively drawing a zone.
int DrawBezier(const TOOL_EVENT &aEvent)
Start interactively drawing a bezier curve.
int DrawLine(const TOOL_EVENT &aEvent)
Start interactively drawing a line.
int DrawArc(const TOOL_EVENT &aEvent)
Start interactively drawing an arc.
VECTOR2I getClampedDifferenceEnd(const VECTOR2I &aOrigin, const VECTOR2I &aEnd)
Clamps the end vector to respect numeric limits of difference representation.
BOARD_CONNECTED_ITEM * m_pickerItem
bool drawShape(const TOOL_EVENT &aTool, PCB_SHAPE **aGraphic, std::optional< VECTOR2D > aStartingPoint, std::stack< PCB_SHAPE * > *aCommittedGraphics)
Start drawing a selected shape (i.e.
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
int PlaceImportedGraphics(const TOOL_EVENT &aEvent)
Place a drawing imported from a DXF or SVG file.
int PlaceStackup(const TOOL_EVENT &aEvent)
VECTOR2I getClampedRadiusEnd(const VECTOR2I &aOrigin, const VECTOR2I &aEnd)
Clamps the end vector to respect numeric limits of radius representation.
static const unsigned int WIDTH_STEP
int runSimpleShapeDraw(const TOOL_EVENT &aEvent, SHAPE_T aShapeType, MODE aMode, const wxString &aCommitLabel, std::function< bool(const TOOL_EVENT &, PCB_SHAPE **, std::optional< VECTOR2D >)> aDrawer)
static const unsigned int COORDS_PADDING
int InteractivePlaceWithPreview(const TOOL_EVENT &aEvent, std::vector< BOARD_ITEM * > &aItems, std::vector< BOARD_ITEM * > &aPreview, LSET *aLayers)
Interactively place a set of BOARD_ITEM.
int DrawBarcode(const TOOL_EVENT &aEvent)
Starts interactively drawing a barcode.
int DrawEllipse(const TOOL_EVENT &aEvent)
int DrawCircle(const TOOL_EVENT &aEvent)
Start interactively drawing a circle.
int PlaceTuningPattern(const TOOL_EVENT &aEvent)
std::vector< BOARD_ITEM * > DrawSpecificationStackup(const VECTOR2I &origin, PCB_LAYER_ID aLayer, bool aDrawNow, VECTOR2I *tablesize)
BOARD * m_board
int DrawRectangle(const TOOL_EVENT &aEvent)
Start interactively drawing a rectangle.
bool Init() override
Init() is called once upon a registration of the tool.
void constrainDimension(PCB_DIMENSION_BASE *aDim)
Force the dimension lime to be drawn on multiple of 45 degrees.
int PlaceText(const TOOL_EVENT &aEvent)
Display a dialog that allows one to input text and its settings and then lets the user decide where t...
PCB_BASE_EDIT_FRAME * m_frame
void UpdateStatusBar() const
int PlaceReferenceImage(const TOOL_EVENT &aEvent)
Display a dialog that allows one to select a reference image and then decide where to place the image...
PCB_TUNING_PATTERN * m_tuningPattern
bool getSourceZoneForAction(ZONE_MODE aMode, ZONE **aZone)
Draw a polygon, that is added as a zone or a keepout area.
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
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
Common, abstract interface for edit frames.
Abstract dimension API.
PCB_TOOL_BASE(TOOL_ID aId, const std::string &aName)
Constructor.
Class that handles the drawing of a polygon, including management of last corner deletion and drawing...
Abstract interface for interactive shape-drawing behaviours.
Simple container to manage line stroke parameters.
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
VECTOR2_TRAITS< int32_t >::extended_type extended_type
Definition vector2d.h:69
Handle a list of polygons defining a copper zone.
Definition zone.h:70
SHAPE_T
Definition eda_shape.h:44
void Reset() override
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
ZONE_MODE
Definition pcb_actions.h:33
int radius
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683