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, see <https://www.gnu.org/licenses/>.
18 */
19
21
23#include <gal/painter.h>
24#include <render_settings.h>
25
26using namespace KIGFX;
27
29 EDA_ITEM( nullptr, NOT_USED ) // Never added to a BOARD/SCHEMATIC so it needs no type
30{
31}
32
33
35{
36 return new ANCHOR_DEBUG();
37}
38
39
41{
42 // We could be a bit more careful here, but also we need to
43 // know the world scale to cover everything exactly, and there
44 // is only one of these.
45 BOX2I bbox;
46 bbox.SetMaximum();
47 return bbox;
48}
49
50
51std::vector<int> ANCHOR_DEBUG::ViewGetLayers() const
52{
53 return { LAYER_GP_OVERLAY };
54}
55
56
58{
59 m_nearest.reset();
60 m_anchors.clear();
61}
62
63
64void ANCHOR_DEBUG::AddAnchor( const VECTOR2I& aAnchor )
65{
66 m_anchors[aAnchor]++;
67}
68
69
71{
72 m_nearest = aNearest;
73}
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 = BOX2ISafe( 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 std::size_t total = 0;
103
104 for( const auto& [anchor, count] : m_anchors )
105 {
106 if( m_nearest && *m_nearest == anchor )
107 gal.SetStrokeColor( RED );
108 else
109 gal.SetStrokeColor( YELLOW );
110
111 gal.DrawCircle( anchor, markerRad );
112
113 const std::string countStr = std::to_string( count );
114 font.Draw( &gal, countStr, anchor + textOffset, attributes, fontMetrics );
115
116 total += count;
117 }
118
119 gal.SetStrokeColor( textColor );
120
121 const int boundaryMargin = aView->ToWorld( 20 );
122 VECTOR2I fontPos{ viewport.GetLeft(), viewport.GetTop() };
123 fontPos += VECTOR2I{ boundaryMargin, boundaryMargin };
124
125 attributes.m_Size = VECTOR2I{ summaryTextHeight, summaryTextHeight };
126
127 wxString totalStr = wxString::Format( "Current snap anchors: %zu", total );
128 font.Draw( &gal, totalStr, fontPos, attributes, fontMetrics );
129}
constexpr BOX2I BOX2ISafe(const BOX2D &aInput)
Definition box2.h:925
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr void SetMaximum()
Definition box2.h:76
constexpr coord_type GetLeft() const
Definition box2.h:224
constexpr coord_type GetTop() const
Definition box2.h:225
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:37
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:94
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:143
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttributes, const METRICS &aFontMetrics, std::optional< VECTOR2I > aMousePos=std::nullopt, wxString *aActiveUrl=nullptr) const
Draw a string.
Definition font.cpp:246
static const METRICS & Default()
Definition font.cpp:48
std::map< VECTOR2I, size_t > m_anchors
ANCHOR_DEBUG * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
OPT_VECTOR2I m_nearest
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:101
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:63
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition view.cpp:597
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:207
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:534
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:225
GR_TEXT_H_ALIGN_T m_Halign
@ YELLOW
Definition color4d.h:63
@ RED
Definition color4d.h:55
@ LAYER_GP_OVERLAY
General purpose overlay.
Definition layer_ids.h:275
@ LAYER_AUX_ITEMS
Auxiliary items (guides, rule, etc).
Definition layer_ids.h:279
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
std::optional< VECTOR2I > OPT_VECTOR2I
Definition seg.h:35
@ GR_TEXT_H_ALIGN_LEFT
@ NOT_USED
the 3d code uses this value
Definition typeinfo.h:72
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683