KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_diff_canvas_context.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 3
9 * of the License, or (at your option) any later version.
10 */
11
14
15#include <board.h>
16#include <board_item.h>
17#include <eda_item.h>
18#include <footprint.h>
19#include <gal/painter.h>
20#include <layer_ids.h>
21#include <pad.h>
22#include <pcb_display_options.h>
23#include <pcb_draw_panel_gal.h>
24#include <pcb_track.h>
25#include <view/view.h>
27#include <zone.h>
28
29#include <memory>
30#include <set>
31
32
33namespace
34{
35
36class PCB_DIFF_PAINTER : public KIGFX::PCB_PAINTER
37{
38public:
39 PCB_DIFF_PAINTER( KIGFX::GAL* aGal, std::map<KIID, KIGFX::COLOR4D> aOverrides ) :
40 KIGFX::PCB_PAINTER( aGal, FRAME_PCB_EDITOR ),
41 m_overrides( std::move( aOverrides ) ),
42 m_dimmed( std::make_shared<std::set<KIID>>() )
43 {
44 }
45
48 std::shared_ptr<std::set<KIID>> DimmedSet() const { return m_dimmed; }
49
50 bool Draw( const KIGFX::VIEW_ITEM* aItem, int aLayer ) override
51 {
52 const EDA_ITEM* edaItem = dynamic_cast<const EDA_ITEM*>( aItem );
53
54 if( edaItem )
55 {
56 KIID key = KICAD_DIFF::PcbDiffItemId( *edaItem );
57
58 if( m_overrides.count( key ) == 0 )
59 {
60 if( const BOARD_ITEM* boardItem = dynamic_cast<const BOARD_ITEM*>( aItem ) )
61 {
62 if( const FOOTPRINT* fp = boardItem->GetParentFootprint() )
63 key = fp->m_Uuid;
64 }
65 }
66
67 auto it = m_overrides.find( key );
68
69 if( it != m_overrides.end() && m_dimmed->count( key ) == 0 )
70 {
71 // PCB_PAINTER tints normal items via the layer color, not via
72 // LAYER_BRIGHTENED. Swap the colors of the layer we are drawing on
73 // and the layer it takes its color from so the override paints the item.
74 KIGFX::PCB_RENDER_SETTINGS* settings = GetSettings();
75 std::vector<std::pair<int, KIGFX::COLOR4D>> saved;
76
77 for( int layer : colorSourceLayers( aLayer ) )
78 {
79 saved.emplace_back( layer, settings->GetLayerColor( layer ) );
80 settings->SetLayerColor( layer, it->second );
81 }
82
83 bool result = KIGFX::PCB_PAINTER::Draw( aItem, aLayer );
84
85 for( const auto& [layer, color] : saved )
86 settings->SetLayerColor( layer, color );
87
88 return result;
89 }
90 }
91
92 return KIGFX::PCB_PAINTER::Draw( aItem, aLayer );
93 }
94
95private:
97 static std::vector<int> colorSourceLayers( int aLayer )
98 {
99 // Numbers, netnames and drill holes sit on their item, so in its colour they vanish
100 if( IsNetnameLayer( aLayer ) || aLayer == LAYER_PAD_PLATEDHOLES || aLayer == LAYER_NON_PLATEDHOLES
101 || aLayer == LAYER_VIA_HOLES )
102 {
103 return {};
104 }
105
106 if( IsPadCopperLayer( aLayer ) )
107 return { aLayer, aLayer - LAYER_PAD_COPPER_START, LAYER_VIA_HOLES };
108
109 if( IsViaCopperLayer( aLayer ) )
110 return { aLayer, aLayer - LAYER_VIA_COPPER_START };
111
112 if( IsClearanceLayer( aLayer ) )
113 return { aLayer, aLayer - LAYER_CLEARANCE_START };
114
115 if( IsZoneFillLayer( aLayer ) )
116 return { aLayer, aLayer - LAYER_ZONE_START };
117
118 if( aLayer == LAYER_PAD_HOLEWALLS )
119 return { aLayer, LAYER_VIA_HOLES };
120
121 return { aLayer };
122 }
123
124 std::map<KIID, KIGFX::COLOR4D> m_overrides;
125 std::shared_ptr<std::set<KIID>> m_dimmed;
126};
127
128} // namespace
129
130
131namespace KICAD_DIFF
132{
133
134std::vector<KIGFX::VIEW_ITEM*> CollectFootprintDiffContextItems( FOOTPRINT& aFootprint )
135{
136 std::vector<KIGFX::VIEW_ITEM*> items;
137
138 items.push_back( &aFootprint );
139
140 for( PCB_FIELD* field : aFootprint.GetFields() )
141 items.push_back( field );
142
143 for( BOARD_ITEM* item : aFootprint.GraphicalItems() )
144 items.push_back( item );
145
146 for( PAD* pad : aFootprint.Pads() )
147 items.push_back( pad );
148
149 for( ZONE* zone : aFootprint.Zones() )
150 items.push_back( zone );
151
152 return items;
153}
154
155
156std::vector<KIGFX::VIEW_ITEM*> CollectBoardDiffContextItems( BOARD& aBoard )
157{
158 std::vector<KIGFX::VIEW_ITEM*> items;
159
160 for( PCB_TRACK* track : aBoard.Tracks() )
161 items.push_back( track );
162
163 for( ZONE* zone : aBoard.Zones() )
164 items.push_back( zone );
165
166 for( BOARD_ITEM* item : aBoard.Drawings() )
167 items.push_back( item );
168
169 for( FOOTPRINT* fp : aBoard.Footprints() )
170 {
171 std::vector<KIGFX::VIEW_ITEM*> fpItems = CollectFootprintDiffContextItems( *fp );
172 items.insert( items.end(), fpItems.begin(), fpItems.end() );
173 }
174
175 return items;
176}
177
178
180 const KIGFX::COLOR4D& aColor )
181{
182 const KIGFX::COLOR4D gray( 0.45, 0.45, 0.45, 1.0 );
183 const KIGFX::COLOR4D background( 0.97, 0.97, 0.97, 1.0 );
184
185 aSettings.SetAllLayerColors( gray );
186 aSettings.SetLayerColor( LAYER_BRIGHTENED, aColor );
187 aSettings.SetLayerColor( LAYER_PCB_BACKGROUND, background );
188 aSettings.SetLayerColor( LAYER_PAD_PLATEDHOLES, background );
189
190 // Labels sit on grey or diff coloured copper and on light drills, where only dark text reads
191 for( int layer = NETNAMES_LAYER_ID_START; layer < NETNAMES_LAYER_ID_END; ++layer )
192 aSettings.SetLayerColor( layer, KIGFX::COLOR4D::BLACK );
193 aSettings.SetBackgroundColor( background );
194
195 PCB_DISPLAY_OPTIONS displayOptions;
198 displayOptions.m_TrackOpacity = 1.0;
199 displayOptions.m_ViaOpacity = 1.0;
200 displayOptions.m_PadOpacity = 1.0;
201 // Zones fill translucent so footprints and traces under a copper pour
202 // stay visible in the compare view.
203 displayOptions.m_ZoneOpacity = 0.5;
204 displayOptions.m_ImageOpacity = 1.0;
205 displayOptions.m_FilledShapeOpacity = 1.0;
206 aSettings.LoadDisplayOptions( displayOptions );
207 aSettings.SetHighContrast( false );
208 aSettings.SetHighlight( false );
209}
210
211
212std::unique_ptr<KIGFX::PAINTER> MakePcbDiffContextPainter( KIGFX::GAL* aGal, const KIGFX::COLOR4D& aColor,
213 std::map<KIID, KIGFX::COLOR4D> aOverrides )
214{
215 auto painter = std::make_unique<PCB_DIFF_PAINTER>( aGal, std::move( aOverrides ) );
216 ConfigurePcbDiffContextRenderSettings( *painter->GetSettings(), aColor );
217
218 return painter;
219}
220
221
222void ConfigurePcbDiffCanvasContext( WIDGET_DIFF_CANVAS& aCanvas, BOARD* aReference, BOARD* aComparison,
223 const KIGFX::COLOR4D& aColor, const std::map<KIID, KIGFX::COLOR4D>& aOverrides,
224 const std::vector<KIGFX::VIEW_ITEM*>& aExtraItems,
225 const std::map<KIID, KICAD_DIFF::CATEGORY>& aCategories )
226{
227 auto painter = std::make_unique<PCB_DIFF_PAINTER>( aCanvas.GetGAL(), aOverrides );
228 ConfigurePcbDiffContextRenderSettings( *painter->GetSettings(), aColor );
229
230 std::shared_ptr<std::set<KIID>> dimmed = painter->DimmedSet();
231 aCanvas.SetContextPainter( std::move( painter ) );
233
234 aCanvas.SetItemDimmer(
235 [dimmed]( KIGFX::VIEW_ITEM* aItem, bool aDim )
236 {
237 EDA_ITEM* eda = dynamic_cast<EDA_ITEM*>( aItem );
238
239 if( !eda )
240 return;
241
242 if( aDim )
243 dimmed->insert( PcbDiffItemId( *eda ) );
244 else
245 dimmed->erase( PcbDiffItemId( *eda ) );
246 } );
247
248 std::vector<KIGFX::VIEW_ITEM*> items;
249
250 if( aReference )
251 {
252 std::vector<KIGFX::VIEW_ITEM*> refItems = CollectBoardDiffContextItems( *aReference );
253 items.insert( items.end(), refItems.begin(), refItems.end() );
254 }
255
256 if( aComparison )
257 {
258 std::vector<KIGFX::VIEW_ITEM*> compItems = CollectBoardDiffContextItems( *aComparison );
259 items.insert( items.end(), compItems.begin(), compItems.end() );
260 }
261
262 items.insert( items.end(), aExtraItems.begin(), aExtraItems.end() );
263
264 aCanvas.SetContextItems( items );
265
266 ApplyPcbGalLayerOrder( aCanvas.GetView() );
267 ApplyPcbGalLayerDeps( aCanvas.GetView() );
268
269 std::map<KIGFX::VIEW_ITEM*, KICAD_DIFF::CATEGORY> itemCategories;
270
271 for( KIGFX::VIEW_ITEM* viewItem : items )
272 {
273 EDA_ITEM* edaItem = dynamic_cast<EDA_ITEM*>( viewItem );
274
275 if( !edaItem )
276 continue;
277
278 auto it = aCategories.find( PcbDiffItemId( *edaItem ) );
279
280 if( it != aCategories.end() )
281 itemCategories[viewItem] = it->second;
282 }
283
284 aCanvas.SetItemCategories( std::move( itemCategories ) );
285}
286
287
289 std::shared_ptr<const void> aOwner )
290{
291 if( !aBefore && !aAfter )
292 {
293 aCanvas.SetContextItems( {} );
294 return;
295 }
296
297 const DIFF_COLOR_THEME theme;
298 std::map<KIID, KIGFX::COLOR4D> overrides;
299 std::vector<KIGFX::VIEW_ITEM*> items;
300
301 if( !aBefore || !aAfter )
302 {
303 // A whole footprint added or removed takes its colour from the footprint
304 FOOTPRINT* shown = aAfter ? aAfter : aBefore;
305 overrides[shown->m_Uuid] = aAfter ? theme.added : theme.removed;
306 items = CollectFootprintDiffContextItems( *shown );
307 }
308 else
309 {
310 items = CollectFootprintDiffContextItems( *aAfter );
311
312 for( const LIB_ELEMENT& element : DiffFootprintElements( aBefore, aAfter ) )
313 {
314 overrides[PcbDiffItemId( *element.item )] = theme.ForKind( element.kind );
315
316 // Removed elements exist only on the before side
317 if( element.kind == CHANGE_KIND::REMOVED )
318 items.push_back( const_cast<EDA_ITEM*>( element.item ) );
319 }
320 }
321
322 aCanvas.SetContextPainter( MakePcbDiffContextPainter( aCanvas.GetGAL(), theme.reference, std::move( overrides ) ) );
323 aCanvas.SetContextItems( items, std::move( aOwner ) );
324 ApplyPcbGalLayerOrder( aCanvas.GetView() );
325 ApplyPcbGalLayerDeps( aCanvas.GetView() );
326 aCanvas.ZoomToFit();
327}
328
329
331 const FP_LIB_DIFFER::FOOTPRINT_MAP& aAfter, const wxString& aName )
332{
333 // Each view takes over the items it draws, so it gets copies parented to a board of their own
334 auto holder = std::make_shared<BOARD>();
335 holder->SetBoardUse( BOARD_USE::FPHOLDER );
336
337 // Pads only draw on layers their board enables, and a library footprint may use any of them
338 holder->SetEnabledLayers( LSET::AllLayersMask() );
339
340 auto copy = [&]( const FP_LIB_DIFFER::FOOTPRINT_MAP& aSide ) -> FOOTPRINT*
341 {
342 auto footprint = aSide.find( aName );
343
344 if( footprint == aSide.end() || !footprint->second )
345 return nullptr;
346
347 FOOTPRINT* clone = static_cast<FOOTPRINT*>( footprint->second->Clone() );
348 holder->Add( clone, ADD_MODE::APPEND, true );
349 return clone;
350 };
351
352 FOOTPRINT* before = copy( aBefore );
353 FOOTPRINT* after = copy( aAfter );
354
355 ConfigureFpDiffCanvasContext( aCanvas, before, after, holder );
356}
357
358} // namespace KICAD_DIFF
@ FPHOLDER
Definition board.h:403
@ NORMAL
Inactive layers are shown normally (no high-contrast mode)
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
const ZONES & Zones() const
Definition board.h:469
const FOOTPRINTS & Footprints() const
Definition board.h:465
const TRACKS & Tracks() const
Definition board.h:463
const DRAWINGS & Drawings() const
Definition board.h:467
virtual KIGFX::VIEW * GetView() const
Return a pointer to the KIGFX::VIEW instance used in the panel.
KIGFX::GAL * GetGAL() const
Return a pointer to the GAL instance used in the panel.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
const KIID m_Uuid
Definition eda_item.h:599
ZONES & Zones()
Definition footprint.h:361
std::deque< PAD * > & Pads()
Definition footprint.h:355
void GetFields(std::vector< PCB_FIELD * > &aVector, bool aVisibleOnly) const
Populate a std::vector with PCB_TEXTs.
DRAWINGS & GraphicalItems()
Definition footprint.h:358
std::map< wxString, const FOOTPRINT * > FOOTPRINT_MAP
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
static const COLOR4D BLACK
Definition color4d.h:404
Abstract interface for drawing on a 2D-surface.
Contains methods for drawing PCB-specific items.
virtual bool Draw(const VIEW_ITEM *aItem, int aLayer) override
Takes an instance of VIEW_ITEM and passes it to a function that knows how to draw the item.
PCB specific render settings.
Definition pcb_painter.h:94
void SetBackgroundColor(const COLOR4D &aColor) override
Set the background color.
void LoadDisplayOptions(const PCB_DISPLAY_OPTIONS &aOptions)
Load settings related to display options (high-contrast mode, full or outline modes for vias/pads/tra...
void SetAllLayerColors(const COLOR4D &aColor)
Change the color used to draw every layer.
void SetLayerColor(int aLayer, const COLOR4D &aColor)
Change the color used to draw a layer.
void SetHighContrast(bool aEnabled)
Turns on/off high contrast display mode.
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
void SetHighlight(bool aEnabled, int aNetcode=-1, bool aMulti=false)
Turns on/off highlighting.
An abstract base class for deriving all objects that can be added to a VIEW.
Definition view_item.h:82
static const LSET & AllLayersMask()
Definition lset.cpp:637
Definition pad.h:61
double m_TrackOpacity
Opacity override for all tracks.
double m_FilledShapeOpacity
Opacity override for graphic shapes.
double m_ZoneOpacity
Opacity override for filled zone areas.
double m_ImageOpacity
Opacity override for user images.
double m_PadOpacity
Opacity override for SMD pads and PTHs.
double m_ViaOpacity
Opacity override for all types of via.
HIGH_CONTRAST_MODE m_ContrastModeDisplay
How inactive layers are displayed.
ZONE_DISPLAY_MODE m_ZoneDisplayMode
GAL-backed canvas for visualizing a KICAD_DIFF::DIFF_SCENE.
void ZoomToFit()
Center the view on the scene's document bbox.
void SetItemCategories(std::map< KIGFX::VIEW_ITEM *, KICAD_DIFF::CATEGORY > aMap)
Tag context items by change category so SetCategoryVisible can hide / show them in lockstep with the ...
void SetContextPainter(std::unique_ptr< KIGFX::PAINTER > aPainter)
Install the native painter used for drawing source document context.
void SetItemIdGetter(const ITEM_ID_GETTER &aGetter)
void SetContextItems(const std::vector< KIGFX::VIEW_ITEM * > &aItems, std::shared_ptr< const void > aOwner=nullptr)
Replace source document context items.
void SetItemDimmer(DIMMER aDimmer)
Handle a list of polygons defining a copper zone.
Definition zone.h:70
@ FRAME_PCB_EDITOR
Definition frame_type.h:38
@ NETNAMES_LAYER_ID_START
Definition layer_ids.h:190
@ NETNAMES_LAYER_ID_END
Definition layer_ids.h:201
bool IsPadCopperLayer(int aLayer)
Definition layer_ids.h:948
bool IsClearanceLayer(int aLayer)
Definition layer_ids.h:960
@ LAYER_PAD_COPPER_START
Virtual layers for pad copper on a given copper layer.
Definition layer_ids.h:358
@ LAYER_NON_PLATEDHOLES
Draw usual through hole vias.
Definition layer_ids.h:235
@ LAYER_PCB_BACKGROUND
PCB background color.
Definition layer_ids.h:277
@ LAYER_PAD_PLATEDHOLES
to draw pad holes (plated)
Definition layer_ids.h:267
@ LAYER_VIA_COPPER_START
Virtual layers for via copper on a given copper layer.
Definition layer_ids.h:362
@ LAYER_CLEARANCE_START
Virtual layers for pad/via/track clearance outlines for a given copper layer.
Definition layer_ids.h:366
@ LAYER_ZONE_START
Virtual layers for stacking zones and tracks on a given copper layer.
Definition layer_ids.h:354
@ LAYER_VIA_HOLES
Draw via holes (pad holes do not use this layer).
Definition layer_ids.h:270
@ LAYER_PAD_HOLEWALLS
Definition layer_ids.h:293
@ LAYER_BRIGHTENED
Definition layer_ids.h:522
bool IsViaCopperLayer(int aLayer)
Definition layer_ids.h:954
bool IsNetnameLayer(int aLayer)
Test whether a layer is a netname layer.
Definition layer_ids.h:936
bool IsZoneFillLayer(int aLayer)
Definition layer_ids.h:942
std::vector< KIGFX::VIEW_ITEM * > CollectBoardDiffContextItems(BOARD &aBoard)
std::vector< LIB_ELEMENT > DiffFootprintElements(const FOOTPRINT *aBefore, const FOOTPRINT *aAfter)
Compare the elements of two library footprints.
void ConfigureFpDiffCanvasContext(WIDGET_DIFF_CANVAS &aCanvas, FOOTPRINT *aBefore, FOOTPRINT *aAfter, std::shared_ptr< const void > aOwner)
Draw aAfter with its changes from aBefore marked. aOwner keeps both alive while they are shown.
void ConfigurePcbDiffCanvasContext(WIDGET_DIFF_CANVAS &aCanvas, BOARD *aReference, BOARD *aComparison, const KIGFX::COLOR4D &aColor, const std::map< KIID, KIGFX::COLOR4D > &aOverrides, const std::vector< KIGFX::VIEW_ITEM * > &aExtraItems, const std::map< KIID, KICAD_DIFF::CATEGORY > &aCategories)
std::unique_ptr< KIGFX::PAINTER > MakePcbDiffContextPainter(KIGFX::GAL *aGal, const KIGFX::COLOR4D &aColor, std::map< KIID, KIGFX::COLOR4D > aOverrides)
KIID PcbDiffItemId(const EDA_ITEM &aItem)
Fields match by parent footprint and untranslated name, including after a library refresh.
void ConfigurePcbDiffContextRenderSettings(KIGFX::PCB_RENDER_SETTINGS &aSettings, const KIGFX::COLOR4D &aColor)
std::vector< KIGFX::VIEW_ITEM * > CollectFootprintDiffContextItems(FOOTPRINT &aFootprint)
void ShowFootprintPair(WIDGET_DIFF_CANVAS &aCanvas, const FP_LIB_DIFFER::FOOTPRINT_MAP &aBefore, const FP_LIB_DIFFER::FOOTPRINT_MAP &aAfter, const wxString &aName)
Draw the footprint aName as it is in aBefore and aAfter.
void ApplyPcbGalLayerDeps(KIGFX::VIEW *aView)
Make pcbnew's derived layers (pad copper, zones, holes, netnames) follow their board layer's visibili...
void ApplyPcbGalLayerOrder(KIGFX::VIEW *aView)
Apply pcbnew's canonical GAL layer order (copper and zones below silk, fab and courtyard) to any view...
Diff colors with defaults for light backgrounds.
Definition diff_scene.h:278
KIGFX::COLOR4D reference
Default color for source-document context geometry.
Definition diff_scene.h:292
const KIGFX::COLOR4D & ForKind(KICAD_DIFF::CHANGE_KIND aKind) const
Color of an added, removed or modified element.
Definition diff_scene.h:296
One changed element of a library item, such as a pin, pad, field or graphic.
wxString result
Test unit parsing edge cases and error handling.