KiCad PCB EDA Suite
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 (C) 2018-2022 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, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#ifndef __DRAWING_TOOL_H
28#define __DRAWING_TOOL_H
29
30#include <optional>
31#include <tool/tool_menu.h>
32#include <tools/pcb_tool_base.h>
33#include <tools/pcb_actions.h>
34
35namespace KIGFX
36{
37 class VIEW;
38 class VIEW_CONTROLS;
39}
40
41class BOARD;
43class PCB_SHAPE;
45
51{
52public:
55
57 bool Init() override;
58
60 void Reset( RESET_REASON aReason ) override;
61
63 enum class MODE
64 {
65 NONE,
66 LINE,
68 CIRCLE,
69 ARC,
70 IMAGE,
71 TEXT,
72 ANCHOR,
73 DXF,
74 DIMENSION,
75 KEEPOUT,
76 ZONE,
78 VIA
79 };
80
85 MODE GetDrawingMode() const;
86
89 std::vector<BOARD_ITEM*> DrawBoardCharacteristics( const VECTOR2I& origin, PCB_LAYER_ID aLayer,
90 bool aDrawNow, VECTOR2I* tablesize );
91
94 std::vector<BOARD_ITEM*> DrawSpecificationStackup( const VECTOR2I& origin, PCB_LAYER_ID aLayer,
95 bool aDrawNow, VECTOR2I* tablesize );
96
99 int PlaceCharacteristics( const TOOL_EVENT& aEvent );
100
103 int PlaceStackup( const TOOL_EVENT& aEvent );
104
112 int DrawLine( const TOOL_EVENT& aEvent );
113
120 int DrawRectangle( const TOOL_EVENT& aEvent );
121
128 int DrawCircle( const TOOL_EVENT& aEvent );
129
137 int DrawArc( const TOOL_EVENT& aEvent );
138
143 int PlaceImage( const TOOL_EVENT& aEvent );
144
149 int PlaceText( const TOOL_EVENT& aEvent );
150
158 int DrawDimension( const TOOL_EVENT& aEvent );
159
173 int DrawZone( const TOOL_EVENT& aEvent );
174
175 int DrawVia( const TOOL_EVENT& aEvent );
176
180 int PlaceImportedGraphics( const TOOL_EVENT& aEvent );
181
194 int InteractivePlaceWithPreview( const TOOL_EVENT& aEvent,
195 std::vector<BOARD_ITEM*>& aItems,
196 std::vector<BOARD_ITEM*>& aPreview, LSET* aLayers );
197
198
202 int SetAnchor( const TOOL_EVENT& aEvent );
203
207 int ToggleHV45Mode( const TOOL_EVENT& toolEvent );
208
210 void setTransitions() override;
211
212 void SetStroke( const STROKE_PARAMS& aStroke, PCB_LAYER_ID aLayer )
213 {
214 m_layer = aLayer;
215 m_stroke = aStroke;
216 }
217
218 void UpdateStatusBar() const;
219
220private:
232 bool drawShape( const TOOL_EVENT& aTool, PCB_SHAPE** aGraphic,
233 std::optional<VECTOR2D> aStartingPoint );
234
243 bool drawArc( const TOOL_EVENT& aTool, PCB_SHAPE** aGraphic,
244 std::optional<VECTOR2D> aStartingPoint );
245
268 bool getSourceZoneForAction( ZONE_MODE aMode, ZONE** aZone );
269
276
284 VECTOR2I getClampedDifferenceEnd( const VECTOR2I& aOrigin, const VECTOR2I& aEnd )
285 {
286 typedef std::numeric_limits<int> coord_limits;
287 const int guardValue = 1;
288
289 VECTOR2I::extended_type maxDiff = coord_limits::max() - guardValue;
290
291 VECTOR2I::extended_type xDiff = VECTOR2I::extended_type( aEnd.x ) - aOrigin.x;
292 VECTOR2I::extended_type yDiff = VECTOR2I::extended_type( aEnd.y ) - aOrigin.y;
293
294 if( xDiff > maxDiff )
295 xDiff = maxDiff;
296 if( yDiff > maxDiff )
297 yDiff = maxDiff;
298
299 if( xDiff < -maxDiff )
300 xDiff = -maxDiff;
301 if( yDiff < -maxDiff )
302 yDiff = -maxDiff;
303
304 return aOrigin + VECTOR2I( int( xDiff ), int( yDiff ) );
305 }
306
314 VECTOR2I getClampedRadiusEnd( const VECTOR2I& aOrigin, const VECTOR2I& aEnd )
315 {
316 typedef std::numeric_limits<int> coord_limits;
317 const int guardValue = 10;
318
319 VECTOR2I::extended_type xDiff = VECTOR2I::extended_type( aEnd.x ) - aOrigin.x;
320 VECTOR2I::extended_type yDiff = VECTOR2I::extended_type( aEnd.y ) - aOrigin.y;
321
322 double maxRadius = coord_limits::max() / 2 - guardValue;
323 double radius = std::hypot( xDiff, yDiff );
324
325 if( radius > maxRadius )
326 {
327 double scaleFactor = maxRadius / radius;
328
329 xDiff = KiROUND<double, int>( xDiff * scaleFactor );
330 yDiff = KiROUND<double, int>( yDiff * scaleFactor );
331 }
332
333 return aOrigin + VECTOR2I( int( xDiff ), int( yDiff ) );
334 }
335
337 int getSegmentWidth( PCB_LAYER_ID aLayer ) const;
338
344 bool m_inDrawingTool; // Re-entrancy guard
345
346 PCB_LAYER_ID m_layer; // The layer we last drew on
347 STROKE_PARAMS m_stroke; // Current stroke for multi-segment drawing
349
350 static const unsigned int WIDTH_STEP; // Amount of width change for one -/+ key press
351 static const unsigned int COORDS_PADDING; // Padding from coordinates limits for this tool
352
353
354 friend class ZONE_CREATE_HELPER; // give internal access to helper classes
355};
356
357#endif /* __DRAWING_TOOL_H */
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:269
Represent basic circle geometry with utility geometry functions.
Definition: circle.h:33
Tool responsible for drawing graphical elements like lines, arcs, circles, etc.
Definition: drawing_tool.h:51
TEXT_ATTRIBUTES m_textAttrs
Definition: drawing_tool.h:348
MODE GetDrawingMode() const
Return the current drawing mode of the DRAWING_TOOL or #MODE::NONE if not currently in any drawing mo...
void SetStroke(const STROKE_PARAMS &aStroke, PCB_LAYER_ID aLayer)
Definition: drawing_tool.h:212
PCB_LAYER_ID m_layer
Definition: drawing_tool.h:346
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 DrawVia(const TOOL_EVENT &aEvent)
KIGFX::VIEW_CONTROLS * m_controls
Definition: drawing_tool.h:340
KIGFX::VIEW * m_view
Definition: drawing_tool.h:339
STROKE_PARAMS m_stroke
Definition: drawing_tool.h:347
int DrawZone(const TOOL_EVENT &aEvent)
Start interactively drawing a zone.
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.
Definition: drawing_tool.h:284
int ToggleHV45Mode(const TOOL_EVENT &toolEvent)
Toggle the horizontal/vertical/45-degree constraint for drawing tools.
int PlaceCharacteristics(const TOOL_EVENT &aEvent)
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 in footprint editor.
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.
Definition: drawing_tool.h:314
bool m_inDrawingTool
Definition: drawing_tool.h:344
static const unsigned int WIDTH_STEP
Definition: drawing_tool.h:350
static const unsigned int COORDS_PADDING
Definition: drawing_tool.h:351
bool drawArc(const TOOL_EVENT &aTool, PCB_SHAPE **aGraphic, std::optional< VECTOR2D > aStartingPoint)
Start drawing an arc.
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.
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
std::vector< BOARD_ITEM * > DrawBoardCharacteristics(const VECTOR2I &origin, PCB_LAYER_ID aLayer, bool aDrawNow, VECTOR2I *tablesize)
int PlaceImage(const TOOL_EVENT &aEvent)
Display a dialog that allows one to select and image then decide where to place the image in the edit...
int DrawCircle(const TOOL_EVENT &aEvent)
Start interactively drawing a circle.
std::vector< BOARD_ITEM * > DrawSpecificationStackup(const VECTOR2I &origin, PCB_LAYER_ID aLayer, bool aDrawNow, VECTOR2I *tablesize)
BOARD * m_board
Definition: drawing_tool.h:341
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
Definition: drawing_tool.h:342
void UpdateStatusBar() const
bool getSourceZoneForAction(ZONE_MODE aMode, ZONE **aZone)
Draw a polygon, that is added as a zone or a keepout area.
int getSegmentWidth(PCB_LAYER_ID aLayer) const
bool drawShape(const TOOL_EVENT &aTool, PCB_SHAPE **aGraphic, std::optional< VECTOR2D > aStartingPoint)
Start drawing a selected shape (i.e.
Manage an 8-bit channel image.
Definition: image.h:90
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:69
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:532
Common, abstract interface for edit frames.
Abstract dimension API.
Class that handles the drawing of a polygon, including management of last corner deletion and drawing...
Simple container to manage line stroke parameters.
Definition: stroke_params.h:88
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:156
VECTOR2_TRAITS< int >::extended_type extended_type
Definition: vector2d.h:72
An adjunct helper to the DRAWING_TOOL interactive tool, which handles incoming geometry changes from ...
Handle a list of polygons defining a copper zone.
Definition: zone.h:57
@ NONE
Definition: kibis.h:53
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:246
ZONE_MODE
Definition: pcb_actions.h:34
@ VIA
Normal via.
Definition: router_tool.cpp:90
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590