KiCad PCB EDA Suite
Loading...
Searching...
No Matches
anchor_debug.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
25
27#include <gal/painter.h>
28#include <render_settings.h>
29
30using namespace KIGFX;
31
33 EDA_ITEM( nullptr, NOT_USED ) // Never added to a BOARD/SCHEMATIC so it needs no type
34{
35}
36
37
39{
40 return new ANCHOR_DEBUG();
41}
42
43
45{
46 // We could be a bit more careful here, but also we need to
47 // know the world scale to cover everything exactly, and there
48 // is only one of these.
49 BOX2I bbox;
50 bbox.SetMaximum();
51 return bbox;
52}
53
54
55std::vector<int> ANCHOR_DEBUG::ViewGetLayers() const
56{
57 return { LAYER_GP_OVERLAY };
58}
59
60
62{
63 m_nearest.reset();
64 m_anchors.clear();
65}
66
67
68void ANCHOR_DEBUG::AddAnchor( const VECTOR2I& aAnchor )
69{
70 m_anchors[aAnchor]++;
71}
72
73
75{
76 m_nearest = aNearest;
77}
78
79
80void ANCHOR_DEBUG::ViewDraw( int, VIEW* aView ) const
81{
82 GAL& gal = *aView->GetGAL();
83 RENDER_SETTINGS& settings = *aView->GetPainter()->GetSettings();
84
85 const COLOR4D textColor = settings.GetLayerColor( LAYER_AUX_ITEMS );
86
87 const BOX2I viewport = BOX2ISafe( aView->GetViewport() );
88
89 gal.SetIsFill( false );
90 gal.SetIsStroke( true );
91 gal.SetLineWidth( 1 );
92
93 const int markerRad = aView->ToWorld( 3 );
94 const int markerTextHeight = aView->ToWorld( 6 );
95 const int markerTextGap = aView->ToWorld( 3 );
96 const int summaryTextHeight = aView->ToWorld( 10 );
97 const VECTOR2I textOffset = { markerRad + markerTextGap, 0 };
98
99 TEXT_ATTRIBUTES attributes;
100 attributes.m_Halign = GR_TEXT_H_ALIGN_LEFT;
101 attributes.m_Size = VECTOR2I( markerTextHeight, markerTextHeight );
102
103 const KIFONT::METRICS& fontMetrics = KIFONT::METRICS::Default();
104 const KIFONT::FONT& font = *KIFONT::FONT::GetFont();
105
106 std::size_t total = 0;
107
108 for( const auto& [anchor, count] : m_anchors )
109 {
110 if( m_nearest && *m_nearest == anchor )
111 gal.SetStrokeColor( RED );
112 else
113 gal.SetStrokeColor( YELLOW );
114
115 gal.DrawCircle( anchor, markerRad );
116
117 const std::string countStr = std::to_string( count );
118 font.Draw( &gal, countStr, anchor + textOffset, attributes, fontMetrics );
119
120 total += count;
121 }
122
123 gal.SetStrokeColor( textColor );
124
125 const int boundaryMargin = aView->ToWorld( 20 );
126 VECTOR2I fontPos{ viewport.GetLeft(), viewport.GetTop() };
127 fontPos += VECTOR2I{ boundaryMargin, boundaryMargin };
128
129 attributes.m_Size = VECTOR2I{ summaryTextHeight, summaryTextHeight };
130
131 wxString totalStr = wxString::Format( "Current snap anchors: %lu", total );
132 font.Draw( &gal, totalStr, fontPos, attributes, fontMetrics );
133}
constexpr BOX2I BOX2ISafe(const BOX2D &aInput)
Definition: box2.h:929
constexpr void SetMaximum()
Definition: box2.h:80
constexpr coord_type GetLeft() const
Definition: box2.h:228
constexpr coord_type GetTop() const
Definition: box2.h:229
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:131
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false, const std::vector< wxString > *aEmbeddedFiles=nullptr, bool aForDrawingSheet=false)
Definition: font.cpp:146
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttributes, const METRICS &aFontMetrics) const
Draw a string.
Definition: font.cpp:249
static const METRICS & Default()
Definition: font.cpp:52
View item to draw debug items for anchors.
Definition: anchor_debug.h:45
std::map< VECTOR2I, size_t > m_anchors
Definition: anchor_debug.h:80
ANCHOR_DEBUG * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
OPT_VECTOR2I m_nearest
Definition: anchor_debug.h:81
const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
void SetNearest(const OPT_VECTOR2I &aNearest)
Set the nearest anchor to the given position.
void AddAnchor(const VECTOR2I &aAnchor)
Add an anchor at the given position.
void ViewDraw(int aLayer, VIEW *aView) const override
Draw the parts of the object belonging to layer aLayer.
std::vector< int > ViewGetLayers() const override
Return the all the layers within the VIEW the object is painted on.
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
Abstract interface for drawing on a 2D-surface.
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
virtual void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius)
Draw a circle using world coordinates.
virtual void SetLineWidth(float aLineWidth)
Set the line width.
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition: view.cpp:520
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:198
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Converts a screen space point/vector to a point/vector in world space coordinates.
Definition: view.cpp:457
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:216
GR_TEXT_H_ALIGN_T m_Halign
@ YELLOW
Definition: color4d.h:67
@ RED
Definition: color4d.h:59
@ LAYER_GP_OVERLAY
General purpose overlay.
Definition: layer_ids.h:241
@ LAYER_AUX_ITEMS
Auxiliary items (guides, rule, etc).
Definition: layer_ids.h:245
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
std::optional< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:39
@ GR_TEXT_H_ALIGN_LEFT
@ NOT_USED
the 3d code uses this value
Definition: typeinfo.h:79
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695