KiCad PCB EDA Suite
ratsnest_viewitem.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 (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
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, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
32 #include <ratsnest_viewitem.h>
33 #include <ratsnest_data.h>
35 #include <pcb_painter.h>
37 
38 #include <memory>
39 #include <utility>
40 
41 #include <view/view.h>
42 
43 namespace KIGFX {
44 
45 RATSNEST_VIEWITEM::RATSNEST_VIEWITEM( std::shared_ptr<CONNECTIVITY_DATA> aData ) :
46  EDA_ITEM( NOT_USED ), m_data( std::move(aData) )
47 {
48 }
49 
50 
52 {
53  // Make it always visible
54  BOX2I bbox;
55  bbox.SetMaximum();
56 
57  return bbox;
58 }
59 
60 
61 void RATSNEST_VIEWITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
62 {
63  std::unique_lock<std::mutex> lock( m_data->GetLock(), std::try_to_lock );
64 
65  if( !lock )
66  return;
67 
68  constexpr int CROSS_SIZE = 200000;
69 
70  auto gal = aView->GetGAL();
71  gal->SetIsStroke( true );
72  gal->SetIsFill( false );
73  gal->SetLineWidth( 1.0 );
74  auto rs = aView->GetPainter()->GetSettings();
75  auto color = rs->GetColor( NULL, LAYER_RATSNEST );
76 
77  int highlightedNet = rs->GetHighlightNetCode();
78 
79  gal->SetStrokeColor( color.Brightened(0.5) );
80 
81  // Draw the "dynamic" ratsnest (i.e. for objects that may be currently being moved)
82  for( const auto& l : m_data->GetDynamicRatsnest() )
83  {
84  if ( l.a == l.b )
85  {
86  gal->DrawLine( VECTOR2I( l.a.x - CROSS_SIZE, l.a.y - CROSS_SIZE ),
87  VECTOR2I( l.b.x + CROSS_SIZE, l.b.y + CROSS_SIZE ) );
88  gal->DrawLine( VECTOR2I( l.a.x - CROSS_SIZE, l.a.y + CROSS_SIZE ),
89  VECTOR2I( l.b.x + CROSS_SIZE, l.b.y - CROSS_SIZE ) );
90  }
91  else
92  {
93  gal->DrawLine( l.a, l.b );
94  }
95  }
96 
97  for( int i = 1 /* skip "No Net" at [0] */; i < m_data->GetNetCount(); ++i )
98  {
99  RN_NET* net = m_data->GetRatsnestForNet( i );
100 
101  if( !net )
102  continue;
103 
104  // Draw the "static" ratsnest
105  if( i != highlightedNet )
106  gal->SetStrokeColor( color ); // using the default ratsnest color for not highlighted
107  else
108  gal->SetStrokeColor( color.Brightened(0.8) );
109 
110  for( const auto& edge : net->GetUnconnected() )
111  {
112  //if ( !edge.IsVisible() )
113  // continue;
114 
115  const auto& sourceNode = edge.GetSourceNode();
116  const auto& targetNode = edge.GetTargetNode();
117  const VECTOR2I source( sourceNode->Pos() );
118  const VECTOR2I target( targetNode->Pos() );
119 
120  if( !sourceNode->Valid() || !targetNode->Valid() )
121  continue;
122 
123  bool enable = !sourceNode->GetNoLine() && !targetNode->GetNoLine();
124  bool show;
125 
126  // If the ratsnest layer is currently enabled, the local ratsnest
127  // should be easy to turn off, so either element can disable it
128  // If the layer is off, the local ratsnest should be easy to turn on
129  // so either element can enable it.
130  if( sourceNode->Parent()->GetBoard()->IsElementVisible( LAYER_RATSNEST ) )
131  show = sourceNode->Parent()->GetLocalRatsnestVisible() &&
132  targetNode->Parent()->GetLocalRatsnestVisible();
133  else
134  show = sourceNode->Parent()->GetLocalRatsnestVisible() ||
135  targetNode->Parent()->GetLocalRatsnestVisible();
136 
137  if ( enable && show )
138  {
139  if ( source == target )
140  {
141  gal->DrawLine( VECTOR2I( source.x - CROSS_SIZE, source.y - CROSS_SIZE ),
142  VECTOR2I( source.x + CROSS_SIZE, source.y + CROSS_SIZE ) );
143  gal->DrawLine( VECTOR2I( source.x - CROSS_SIZE, source.y + CROSS_SIZE ),
144  VECTOR2I( source.x + CROSS_SIZE, source.y - CROSS_SIZE ) );
145  }
146  else
147  {
148  gal->DrawLine( source, target );
149  }
150  }
151  }
152  }
153 }
154 
155 
156 void RATSNEST_VIEWITEM::ViewGetLayers( int aLayers[], int& aCount ) const
157 {
158  aCount = 1;
159  aLayers[0] = LAYER_RATSNEST;
160 }
161 
162 }
const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers. ...
Class that draws missing connections on a PCB.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:57
RATSNEST_VIEWITEM(std::shared_ptr< CONNECTIVITY_DATA > aData)
std::shared_ptr< CONNECTIVITY_DATA > m_data
Object containing ratsnest data.
Class that computes missing connections on a PCB.
the 3d code uses this value
Definition: typeinfo.h:80
int color
Definition: DXF_plotter.cpp:62
Template specialization to enable wxStrings for certain containers (e.g. unordered_map) ...
Definition: bitmap.cpp:55
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:180
void SetMaximum()
Definition: box2.h:71
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
const std::vector< CN_EDGE > GetUnconnected() const
Function GetUnconnected() Returns pointer to a vector of edges that makes ratsnest for a given net...
Definition: ratsnest_data.h:98
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
void ViewGetLayers(int aLayers[], int &aCount) const override
Function ViewGetLayers() Returns the all the layers within the VIEW the object is painted on...
Board layer functions and definitions.
void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
Function ViewDraw() Draws the parts of the object belonging to layer aLayer.
size_t i
Definition: json11.cpp:597
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:154
Class RN_NET Describes ratsnest for a single net.
Definition: ratsnest_data.h:59
Class VIEW.
Definition: view.h:61
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.