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 (C) 2024 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
54void ANCHOR_DEBUG::ViewGetLayers( int aLayers[], int& aCount ) const
55{
56 aLayers[0] = LAYER_GP_OVERLAY;
57 aCount = 1;
58}
59
61{
62 m_nearest.reset();
63 m_anchors.clear();
64}
65
66void ANCHOR_DEBUG::AddAnchor( const VECTOR2I& aAnchor )
67{
68 m_anchors[aAnchor]++;
69}
70
72{
73 m_nearest = aNearest;
74}
75
76void ANCHOR_DEBUG::ViewDraw( int, VIEW* aView ) const
77{
78 GAL& gal = *aView->GetGAL();
79 RENDER_SETTINGS& settings = *aView->GetPainter()->GetSettings();
80
81 const COLOR4D textColor = settings.GetLayerColor( LAYER_AUX_ITEMS );
82
83 const BOX2I viewport = KiROUND( aView->GetViewport() );
84
85 gal.SetIsFill( false );
86 gal.SetIsStroke( true );
87 gal.SetLineWidth( 1 );
88
89 const int markerRad = aView->ToWorld( 3 );
90 const int markerTextHeight = aView->ToWorld( 6 );
91 const int markerTextGap = aView->ToWorld( 3 );
92 const int summaryTextHeight = aView->ToWorld( 10 );
93 const VECTOR2I textOffset = { markerRad + markerTextGap, 0 };
94
95 TEXT_ATTRIBUTES attributes;
96 attributes.m_Halign = GR_TEXT_H_ALIGN_LEFT;
97 attributes.m_Size = VECTOR2I( markerTextHeight, markerTextHeight );
98
99 const KIFONT::METRICS& fontMetrics = KIFONT::METRICS::Default();
100 const KIFONT::FONT& font = *KIFONT::FONT::GetFont();
101
102 size_t total = 0;
103 for( const auto& [anchor, count] : m_anchors )
104 {
105 if( m_nearest && *m_nearest == anchor )
106 gal.SetStrokeColor( RED );
107 else
108 gal.SetStrokeColor( YELLOW );
109
110 gal.DrawCircle( anchor, markerRad );
111
112 const std::string countStr = std::to_string( count );
113 font.Draw( &gal, countStr, anchor + textOffset, attributes, fontMetrics );
114
115 total += count;
116 }
117
118 gal.SetStrokeColor( textColor );
119
120 const int boundaryMargin = aView->ToWorld( 20 );
121 VECTOR2I fontPos{ viewport.GetLeft(), viewport.GetTop() };
122 fontPos += VECTOR2I{ boundaryMargin, boundaryMargin };
123
124 attributes.m_Size = VECTOR2I{ summaryTextHeight, summaryTextHeight };
125
126 wxString totalStr = wxString::Format( "Current snap anchors: %lu", total );
127 font.Draw( &gal, totalStr, fontPos, attributes, fontMetrics );
128}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:969
constexpr void SetMaximum()
Definition: box2.h:75
constexpr coord_type GetLeft() const
Definition: box2.h:223
constexpr coord_type GetTop() const
Definition: box2.h:224
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:258
static const METRICS & Default()
Definition: font.cpp:52
View item to draw debug items for anchors.
Definition: anchor_debug.h:44
std::map< VECTOR2I, size_t > m_anchors
Definition: anchor_debug.h:79
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:80
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 ViewGetLayers(int aLayers[], int &aCount) const override
Return the all the layers within the VIEW the object is painted on.
void ViewDraw(int aLayer, VIEW *aView) const override
Draw the parts of the object belonging to layer aLayer.
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:68
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition: view.cpp:547
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:203
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:484
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:221
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:220
@ LAYER_AUX_ITEMS
Auxiliary items (guides, rule, etc)
Definition: layer_ids.h:224
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:691