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  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <ratsnest_viewitem.h>
31 #include <ratsnest_data.h>
33 #include <pcb_painter.h>
35 
36 #include <view/view.h>
37 
38 namespace KIGFX {
39 
41  EDA_ITEM( NOT_USED ), m_data( aData )
42 {
43 }
44 
45 
47 {
48  // Make it always visible
49  BOX2I bbox;
50  bbox.SetMaximum();
51 
52  return bbox;
53 }
54 
55 
56 void RATSNEST_VIEWITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
57 {
58  auto gal = aView->GetGAL();
59  gal->SetIsStroke( true );
60  gal->SetIsFill( false );
61  gal->SetLineWidth( 1.0 );
62  auto rs = aView->GetPainter()->GetSettings();
63  auto color = rs->GetColor( NULL, ITEM_GAL_LAYER( RATSNEST_VISIBLE ) );
64  int highlightedNet = rs->GetHighlightNetCode();
65 
66  // Dynamic ratsnest (for e.g. dragged items)
67  for( int i = 1; i < m_data->GetNetCount(); ++i )
68  {
69  RN_NET& net = m_data->GetNet( i );
70 
71  if( !net.IsVisible() )
72  continue;
73 
74  // Set brighter color for the temporary ratsnest
75  gal->SetStrokeColor( color.Brightened( 0.8 ) );
76 
77  // Draw the "dynamic" ratsnest (i.e. for objects that may be currently being moved)
78  for( const RN_NODE_PTR& node : net.GetSimpleNodes() )
79  {
80  // Skipping nodes with higher reference count avoids displaying redundant lines
81  if( node->GetRefCount() > 1 )
82  continue;
83 
84  RN_NODE_PTR dest = net.GetClosestNode( node, LINE_TARGET() );
85 
86  if( dest )
87  {
88  VECTOR2D origin( node->GetX(), node->GetY() );
89  VECTOR2D end( dest->GetX(), dest->GetY() );
90 
91  gal->DrawLine( origin, end );
92  }
93  }
94 
95  // Draw the "static" ratsnest
96  if( i != highlightedNet )
97  gal->SetStrokeColor( color ); // using the default ratsnest color for not highlighted
98 
99  const std::vector<RN_EDGE_MST_PTR>* edges = net.GetUnconnected();
100 
101  if( edges == NULL )
102  continue;
103 
104  for( const RN_EDGE_MST_PTR& edge : *edges )
105  {
106  const RN_NODE_PTR& sourceNode = edge->GetSourceNode();
107  const RN_NODE_PTR& targetNode = edge->GetTargetNode();
108  VECTOR2D source( sourceNode->GetX(), sourceNode->GetY() );
109  VECTOR2D target( targetNode->GetX(), targetNode->GetY() );
110 
111  gal->DrawLine( source, target );
112  }
113  }
114 }
115 
116 
117 void RATSNEST_VIEWITEM::ViewGetLayers( int aLayers[], int& aCount ) const
118 {
119  aCount = 1;
120  aLayers[0] = ITEM_GAL_LAYER( RATSNEST_VISIBLE );
121 }
122 
123 }
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:56
RATSNEST_VIEWITEM(RN_DATA *aData)
Class RN_DATA.
hed::NODE_PTR RN_NODE_PTR
Definition: ratsnest_data.h:64
Class that computes missing connections on a PCB.
the 3d code uses this value
Definition: typeinfo.h:92
bool IsVisible() const
Function IsVisible() Returns the visibility flag state.
const std::vector< RN_EDGE_MST_PTR > * GetUnconnected() const
Function GetUnconnected() Returns pointer to a vector of edges that makes ratsnest for a given net...
Leaves nodes that can be a ratsnest line target
Definition: ratsnest_data.h:95
virtual const COLOR4D & GetColor(const VIEW_ITEM *aItem, int aLayer) const =0
Function GetColor Returns the color that should be used to draw the specific VIEW_ITEM on the specifi...
const std::unordered_set< RN_NODE_PTR > & GetSimpleNodes() const
Function GetSimpleNodes() Returns list of nodes for which ratsnest is drawn in simple mode (i...
const RN_NODE_PTR GetClosestNode(const RN_NODE_PTR &aNode) const
Function GetClosestNode() Returns a single node that lies in the shortest distance from a specific no...
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:177
std::shared_ptr< hed::EDGE_MST > RN_EDGE_MST_PTR
Definition: ratsnest_data.h:69
void SetMaximum()
Definition: box2.h:61
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:196
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
#define ITEM_GAL_LAYER(layer)
macro for obtaining layer number for specific item (eg. pad or text)
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.
int GetNetCount() const
Function GetNetCount() Returns the number of nets handled by the ratsnest.
void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
Function ViewDraw() Draws the parts of the object belonging to layer aLayer.
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
Class RN_NET Describes ratsnest for a single net.
Class VIEW.
Definition: view.h:58
RN_NET & GetNet(int aNetCode)
Function GetNet() Returns ratsnest grouped by net numbers.
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
RN_DATA * m_data
Object containing ratsnest data.