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