KiCad PCB EDA Suite
Loading...
Searching...
No Matches
ratsnest_view_item.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) 2013 CERN
5 * Copyright The 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, see <https://www.gnu.org/licenses/>.
21 */
22
26
28
29#include <kiface_base.h>
31#include <pcbnew_settings.h>
32#include <pcb_painter.h>
34#include <layer_ids.h>
35#include <pcb_base_frame.h>
36#include <view/view.h>
37
38#include <memory>
39#include <utility>
40
41
42RATSNEST_VIEW_ITEM::RATSNEST_VIEW_ITEM( std::shared_ptr<CONNECTIVITY_DATA> aData ) :
44 m_data( std::move( aData ) )
45{
46}
47
48
50{
51 // Make it always visible
52 BOX2I bbox;
53 bbox.SetMaximum();
54
55 return bbox;
56}
57
58
59void RATSNEST_VIEW_ITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
60{
61 std::unique_lock<KISPINLOCK> lock( m_data->GetLock(), std::try_to_lock );
62
63 if( !lock )
64 return;
65
66 constexpr int CROSS_SIZE = 200000;
67
68 PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
69
70 if( !cfg )
71 return;
72
73 auto rs = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( aView->GetPainter()->GetSettings() );
74 KIGFX::GAL* gal = aView->GetGAL();
75 gal->SetIsStroke( true );
76 gal->SetIsFill( false );
78
79 std::set<int> highlightedNets = rs->GetHighlightNetCodes();
80 const std::set<int>& hiddenNets = rs->GetHiddenNets();
81
82 COLOR4D defaultColor = rs->GetColor( nullptr, LAYER_RATSNEST );
83 COLOR4D color = defaultColor;
84 const bool colorByNet = rs->GetNetColorMode() != NET_COLOR_MODE::OFF;
85 const bool dimStatic = m_data->GetLocalRatsnest().size() > 0 || highlightedNets.size() > 0;
86
87 std::map<int, KIGFX::COLOR4D>& netColors = rs->GetNetColorMap();
88
89 const bool onlyVisibleLayers = cfg->m_Display.m_RatsnestMode == RATSNEST_MODE::VISIBLE;
90 LSET visibleLayers;
91
92 // If we are in "other layers off" mode, the active layer is the only visible layer
93 if( rs->m_ContrastModeDisplay == HIGH_CONTRAST_MODE::HIDDEN )
94 {
95 if( rs->GetPrimaryHighContrastLayer() > UNDEFINED_LAYER )
96 visibleLayers.set( rs->GetPrimaryHighContrastLayer() );
97 }
98 else
99 {
101 [&]( PCB_LAYER_ID layer )
102 {
103 if( aView->IsLayerVisible( layer ) )
104 visibleLayers.set( layer );
105 } );
106 }
107
108 auto adjustColor =
109 [&]( COLOR4D& color_base, double brightnessDelta, double alpha )
110 {
111 if( rs->GetColor( nullptr, LAYER_PCB_BACKGROUND ).GetBrightness() < 0.5 )
112 return color_base.Brightened( brightnessDelta ).WithAlpha( std::min( alpha, 1.0 ) );
113 else
114 return color_base.Darkened( brightnessDelta ).WithAlpha( std::min( alpha, 1.0 ) );
115 };
116
118
119 // Adaptive tessellation filter for the curved ratsnest. The ratsnest is only a visual
120 // hint, so there is no need to tessellate the Bezier to the default 10 nm precision: a
121 // polyline whose deviation from the true curve stays below line_width/2 is fully hidden
122 // by the line's own thickness. We use line_width/4 for a 2x safety margin. The filter is
123 // in world units and scales with zoom (line width is already world-scaled above), so it
124 // produces far fewer segments without any visible faceting.
125 const double curveFilter = curved_ratsnest ? ( gal->GetLineWidth() / 4.0 ) : 0.0;
126
127 // Draw the "dynamic" ratsnest (i.e. for objects that may be currently being moved)
128 for( const RN_DYNAMIC_LINE& l : m_data->GetLocalRatsnest() )
129 {
130 if( hiddenNets.count( l.netCode ) )
131 continue;
132
133 const NETCLASS* nc = nullptr;
134 const NET_SETTINGS* netSettings = m_data->GetNetSettings();
135
136 if( m_data->HasNetNameForNetCode( l.netCode ) )
137 {
138 const wxString& netName = m_data->GetNetNameForNetCode( l.netCode );
139
140 if( netSettings && netSettings->HasEffectiveNetClass( netName ) )
141 nc = netSettings->GetCachedEffectiveNetClass( netName ).get();
142 }
143
144 if( colorByNet && netColors.count( l.netCode ) )
145 color = netColors.at( l.netCode );
146 else if( colorByNet && nc && nc->HasPcbColor() )
147 color = nc->GetPcbColor();
148 else
149 color = defaultColor;
150
151 if( color == COLOR4D::UNSPECIFIED )
152 color = defaultColor;
153
154 gal->SetStrokeColor( adjustColor( color, 0.5, color.a + 0.3 ) );
155
156 if( l.a == l.b )
157 {
158 gal->DrawLine( VECTOR2I( l.a.x - CROSS_SIZE, l.a.y - CROSS_SIZE ),
159 VECTOR2I( l.b.x + CROSS_SIZE, l.b.y + CROSS_SIZE ) );
160 gal->DrawLine( VECTOR2I( l.a.x - CROSS_SIZE, l.a.y + CROSS_SIZE ),
161 VECTOR2I( l.b.x + CROSS_SIZE, l.b.y - CROSS_SIZE ) );
162 }
163 else
164 {
165 if( curved_ratsnest )
166 {
167 int dx = l.b.x - l.a.x;
168 int dy = l.b.y - l.a.y;
169 const VECTOR2I center = VECTOR2I( l.a.x + 0.5 * dx - 0.1 * dy,
170 l.a.y + 0.5 * dy + 0.1 * dx );
171 gal->DrawCurve( l.a, center, center, l.b, curveFilter );
172 }
173 else
174 {
175 gal->DrawLine( l.a, l.b );
176 }
177 }
178 }
179
180 for( int i = 1 /* skip "No Net" at [0] */; i < m_data->GetNetCount(); ++i )
181 {
182 if( hiddenNets.count( i ) )
183 continue;
184
185 RN_NET* net = m_data->GetRatsnestForNet( i );
186
187 if( !net || m_data->GetConnectivityAlgo()->IsNetDirty( i ) )
188 continue;
189
190 const NETCLASS* nc = nullptr;
191 const NET_SETTINGS* netSettings = m_data->GetNetSettings();
192
193 if( m_data->HasNetNameForNetCode( i ) )
194 {
195 const wxString& netName = m_data->GetNetNameForNetCode( i );
196
197 if( netSettings && netSettings->HasEffectiveNetClass( netName ) )
198 nc = netSettings->GetCachedEffectiveNetClass( netName ).get();
199 }
200
201 if( colorByNet && netColors.count( i ) )
202 color = netColors.at( i );
203 else if( colorByNet && nc && nc->HasPcbColor() )
204 color = nc->GetPcbColor();
205 else
206 color = defaultColor;
207
208 if( color == COLOR4D::UNSPECIFIED )
209 color = defaultColor;
210
211 if( dimStatic )
212 color = adjustColor( color, 0.0, color.a / 2 );
213
214 // Draw the "static" ratsnest
215 if( highlightedNets.count( i ) )
216 gal->SetStrokeColor( adjustColor( color, 0.8, color.a + 0.4 ) );
217 else
218 gal->SetStrokeColor( color ); // using the default ratsnest color for not highlighted
219
220 for( const CN_EDGE& edge : net->GetEdges() )
221 {
222 if( !edge.IsVisible() )
223 continue;
224
225 const std::shared_ptr<const CN_ANCHOR>& sourceNode = edge.GetSourceNode();
226 const std::shared_ptr<const CN_ANCHOR>& targetNode = edge.GetTargetNode();
227
228 if( !sourceNode || sourceNode->Dirty() || !targetNode || targetNode->Dirty() )
229 continue;
230
231 const VECTOR2I source( sourceNode->Pos() );
232 const VECTOR2I target( targetNode->Pos() );
233
234 bool enable = !sourceNode->GetNoLine() && !targetNode->GetNoLine();
235 bool show;
236
237 // If the global ratsnest is currently enabled, the local ratsnest should be easy to
238 // turn off, so either element can disable it.
239 // If the global ratsnest is disabled, the local ratsnest should be easy to turn on
240 // so either element can enable it.
242 {
243 show = sourceNode->Parent()->GetLocalRatsnestVisible() &&
244 targetNode->Parent()->GetLocalRatsnestVisible();
245 }
246 else
247 {
248 show = sourceNode->Parent()->GetLocalRatsnestVisible() ||
249 targetNode->Parent()->GetLocalRatsnestVisible();
250 }
251
252 if( onlyVisibleLayers && show )
253 {
254 LSET sourceLayers = sourceNode->Parent()->GetLayerSet();
255 LSET targetLayers = targetNode->Parent()->GetLayerSet();
256
257 if( !( sourceLayers & visibleLayers ).any() ||
258 !( targetLayers & visibleLayers ).any() )
259 {
260 show = false;
261 }
262 }
263
264 if ( enable && show )
265 {
266 if ( source == target )
267 {
268 gal->DrawLine( VECTOR2I( source.x - CROSS_SIZE, source.y - CROSS_SIZE ),
269 VECTOR2I( source.x + CROSS_SIZE, source.y + CROSS_SIZE ) );
270 gal->DrawLine( VECTOR2I( source.x - CROSS_SIZE, source.y + CROSS_SIZE ),
271 VECTOR2I( source.x + CROSS_SIZE, source.y - CROSS_SIZE ) );
272 }
273 else
274 {
275 if( curved_ratsnest )
276 {
277 int dx = target.x - source.x;
278 int dy = target.y - source.y;
279 const VECTOR2I center = VECTOR2I( source.x + 0.5 * dx - 0.1 * dy,
280 source.y + 0.5 * dy + 0.1 * dx );
281 gal->DrawCurve( source, center, center, target, curveFilter );
282 }
283 else
284 {
285 gal->DrawLine( source, target );
286 }
287 }
288 }
289 }
290 }
291}
292
293
294std::vector<int> RATSNEST_VIEW_ITEM::ViewGetLayers() const
295{
296 return { LAYER_RATSNEST };
297}
298
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
@ curved_ratsnest
@ HIDDEN
Inactive layers are hidden.
@ OFF
Net (and netclass) colors are not shown.
@ VISIBLE
Ratsnest lines are drawn to items on visible layers only.
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
BASE_SET & set(size_t pos)
Definition base_set.h:116
constexpr void SetMaximum()
Definition box2.h:76
CN_EDGE represents a point-to-point connection, whether realized or unrealized (ie: tracks etc.
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:398
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:37
APP_SETTINGS_BASE * KifaceSettings() const
Definition kiface_base.h:91
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition color4d.h:308
COLOR4D Darkened(double aFactor) const
Return a color that is darker by a given factor, without modifying object.
Definition color4d.h:279
double a
Alpha component.
Definition color4d.h:392
COLOR4D Brightened(double aFactor) const
Return a color that is brighter by a given factor, without modifying object.
Definition color4d.h:265
Abstract interface for drawing on a 2D-surface.
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
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 void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
virtual void DrawCurve(const VECTOR2D &startPoint, const VECTOR2D &controlPointA, const VECTOR2D &controlPointB, const VECTOR2D &endPoint, double aFilterValue=0.0)
Draw a cubic bezier spline.
float GetLineWidth() const
Get the line width.
double GetWorldScale() const
Get the world scale.
PCB specific render settings.
Definition pcb_painter.h:78
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:63
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:207
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition view.h:427
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:225
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:604
void RunOnLayers(const std::function< void(PCB_LAYER_ID)> &aFunction) const
Execute a function on each layer of the LSET.
Definition lset.h:263
A collection of nets and the parameters used to route or test these nets.
Definition netclass.h:38
COLOR4D GetPcbColor(bool aIsForSave=false) const
Definition netclass.h:195
bool HasPcbColor() const
Definition netclass.h:194
NET_SETTINGS stores various net-related settings in a project context.
bool HasEffectiveNetClass(const wxString &aNetName) const
Determines if an effective netclass for the given net name has been cached.
std::shared_ptr< NETCLASS > GetCachedEffectiveNetClass(const wxString &aNetName) const
Returns an already cached effective netclass for the given net name.
DISPLAY_OPTIONS m_Display
const BOX2I ViewBBox() const override
RATSNEST_VIEW_ITEM(std::shared_ptr< CONNECTIVITY_DATA > aData)
Class that draws missing connections on a PCB.
std::shared_ptr< CONNECTIVITY_DATA > m_data
Object containing ratsnest data.
void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
std::vector< int > ViewGetLayers() const override
Describe ratsnest for a single net.
const std::vector< CN_EDGE > & GetEdges() const
A type-safe container of any type.
Definition ki_any.h:92
@ LAYER_PCB_BACKGROUND
PCB background color.
Definition layer_ids.h:277
@ LAYER_RATSNEST
Definition layer_ids.h:249
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ UNDEFINED_LAYER
Definition layer_ids.h:57
STL namespace.
Class that computes missing connections on a PCB.
VECTOR2I center
@ NOT_USED
the 3d code uses this value
Definition typeinfo.h:72
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683