KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_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
13
14#include <eda_item.h>
15#include <layer_ids.h>
16#include <sch_field.h>
17#include <sch_item.h>
18#include <sch_painter.h>
19#include <sch_pin.h>
20#include <sch_screen.h>
21#include <sch_sheet_path.h>
22#include <sch_symbol.h>
23#include <schematic.h>
24#include <set>
26
27
28namespace
29{
30
31class SCH_DIFF_PAINTER : public KIGFX::SCH_PAINTER
32{
33public:
34 SCH_DIFF_PAINTER( KIGFX::GAL* aGal, std::map<KIID, KIGFX::COLOR4D> aOverrides ) :
35 KIGFX::SCH_PAINTER( aGal ),
36 m_overrides( std::move( aOverrides ) )
37 {
38 }
39
40 bool Draw( const KIGFX::VIEW_ITEM* aItem, int aLayer ) override
41 {
42 const EDA_ITEM* edaItem = dynamic_cast<const EDA_ITEM*>( aItem );
43
44 if( edaItem )
45 {
46 auto it = m_overrides.find( edaItem->m_Uuid );
47
48 if( it != m_overrides.end() )
49 {
50 SCH_RENDER_SETTINGS* settings = GetSettings();
51 KIGFX::COLOR4D saved = settings->GetLayerColor( LAYER_BRIGHTENED );
52 settings->SetLayerColor( LAYER_BRIGHTENED, it->second );
53
54 bool result = KIGFX::SCH_PAINTER::Draw( aItem, aLayer );
55
56 settings->SetLayerColor( LAYER_BRIGHTENED, saved );
57 return result;
58 }
59 }
60
61 return KIGFX::SCH_PAINTER::Draw( aItem, aLayer );
62 }
63
64private:
65 std::map<KIID, KIGFX::COLOR4D> m_overrides;
66};
67
68} // namespace
69
70
71namespace KICAD_DIFF
72{
73
74std::vector<KIGFX::VIEW_ITEM*> CollectSchematicDiffContextItems( SCHEMATIC& aSchematic, SCH_SCREEN* aScreen )
75{
76 std::vector<KIGFX::VIEW_ITEM*> items;
77 SCH_SCREEN* screen = aScreen ? aScreen : aSchematic.RootScreen();
78
79 if( !screen )
80 return items;
81
82 for( SCH_ITEM* item : screen->Items() )
83 items.push_back( item );
84
85 return items;
86}
87
88
90 const KIGFX::COLOR4D& aColor )
91{
92 const KIGFX::COLOR4D gray( 0.5, 0.5, 0.5, 1.0 );
93
94 for( int layer = 0; layer < LAYER_ID_COUNT; ++layer )
95 aSettings.SetLayerColor( layer, gray );
96
97 aSettings.SetLayerColor( LAYER_BRIGHTENED, aColor );
98
99 aSettings.SetBackgroundColor( KIGFX::COLOR4D( 0.97, 0.97, 0.97, 1.0 ) );
100 aSettings.SetHighContrast( false );
101 aSettings.SetHighlight( false );
102 aSettings.m_OverrideItemColors = true;
103 aSettings.m_ShowPinsElectricalType = false;
104 aSettings.m_ShowHiddenPins = true;
105 aSettings.m_ShowHiddenFields = true;
106 aSettings.m_ShowVisibleFields = true;
107}
108
109
110std::unique_ptr<KIGFX::PAINTER> MakeSchDiffContextPainter( KIGFX::GAL* aGal, SCHEMATIC* aSchematic,
111 const KIGFX::COLOR4D& aColor,
112 std::map<KIID, KIGFX::COLOR4D> aOverrides )
113{
114 auto painter = std::make_unique<SCH_DIFF_PAINTER>( aGal, std::move( aOverrides ) );
115 painter->SetSchematic( aSchematic );
116 ConfigureSchDiffContextRenderSettings( *painter->GetSettings(), aColor );
117
118 return painter;
119}
120
121
122void ConfigureSchDiffCanvasContext( WIDGET_DIFF_CANVAS& aCanvas, SCHEMATIC* aReference, SCHEMATIC* aComparison,
123 const KIGFX::COLOR4D& aColor, const std::map<KIID, KIGFX::COLOR4D>& aOverrides,
124 const std::vector<KIGFX::VIEW_ITEM*>& aExtraItems,
125 const std::map<KIID, KICAD_DIFF::CATEGORY>& aCategories,
126 SCH_SCREEN* aReferenceScreen, SCH_SCREEN* aComparisonScreen )
127{
129 aReference ? aReference : aComparison,
130 aColor, aOverrides ) );
131
132 std::vector<KIGFX::VIEW_ITEM*> items;
133
134 if( aReference )
135 {
136 std::vector<KIGFX::VIEW_ITEM*> refItems = CollectSchematicDiffContextItems( *aReference, aReferenceScreen );
137 items.insert( items.end(), refItems.begin(), refItems.end() );
138 }
139
140 if( aComparison )
141 {
142 std::vector<KIGFX::VIEW_ITEM*> compItems = CollectSchematicDiffContextItems( *aComparison, aComparisonScreen );
143 items.insert( items.end(), compItems.begin(), compItems.end() );
144 }
145
146 items.insert( items.end(), aExtraItems.begin(), aExtraItems.end() );
147
148 for( KIGFX::VIEW_ITEM* viewItem : items )
149 {
150 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( viewItem );
151
152 if( !schItem )
153 continue;
154
155 const bool brighten = aOverrides.count( schItem->m_Uuid ) > 0;
156
157 if( brighten )
158 schItem->SetBrightened();
159 else
160 schItem->ClearBrightened();
161
162 // Mirror brightening onto pins and fields. The painter checks each
163 // child item's IsBrightened individually.
164 if( SCH_SYMBOL* sym = dynamic_cast<SCH_SYMBOL*>( schItem ) )
165 {
166 for( SCH_PIN* pin : sym->GetGraphicalPins( ALL_UNITS, ALL_BODY_STYLES ) )
167 {
168 if( !pin )
169 continue;
170
171 if( brighten )
172 pin->SetBrightened();
173 else
174 pin->ClearBrightened();
175 }
176
177 for( SCH_FIELD& field : sym->GetFields() )
178 {
179 if( brighten )
180 field.SetBrightened();
181 else
182 field.ClearBrightened();
183 }
184 }
185 }
186
187 aCanvas.SetContextItems( items );
188
189 aCanvas.SetItemDimmer(
190 [overrides = aOverrides]( KIGFX::VIEW_ITEM* aItem, bool aDim )
191 {
192 SCH_ITEM* sch = dynamic_cast<SCH_ITEM*>( aItem );
193
194 if( !sch )
195 return;
196
197 const bool inOverride = overrides.count( sch->m_Uuid ) > 0;
198 const bool brighten = !aDim && inOverride;
199
200 auto apply = [brighten]( EDA_ITEM* aEdaItem )
201 {
202 if( brighten )
203 aEdaItem->SetBrightened();
204 else
205 aEdaItem->ClearBrightened();
206 };
207
208 apply( sch );
209
210 if( SCH_SYMBOL* sym = dynamic_cast<SCH_SYMBOL*>( sch ) )
211 {
212 for( SCH_PIN* pin : sym->GetGraphicalPins( ALL_UNITS, ALL_BODY_STYLES ) )
213 {
214 if( pin )
215 apply( pin );
216 }
217
218 for( SCH_FIELD& field : sym->GetFields() )
219 apply( &field );
220 }
221 } );
222
223 std::map<KIGFX::VIEW_ITEM*, KICAD_DIFF::CATEGORY> itemCategories;
224
225 for( KIGFX::VIEW_ITEM* viewItem : items )
226 {
227 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( viewItem );
228
229 if( !schItem )
230 continue;
231
232 auto it = aCategories.find( schItem->m_Uuid );
233
234 if( it != aCategories.end() )
235 itemCategories[viewItem] = it->second;
236 }
237
238 aCanvas.SetItemCategories( std::move( itemCategories ) );
239}
240
241} // namespace KICAD_DIFF
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
void ClearBrightened()
Definition eda_item.h:154
void SetBrightened()
Definition eda_item.h:151
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Abstract interface for drawing on a 2D-surface.
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.
Contains methods for drawing schematic-specific items.
Definition sch_painter.h:66
virtual bool Draw(const VIEW_ITEM *, int) override
Takes an instance of VIEW_ITEM and passes it to a function that knows how to draw the item.
An abstract base class for deriving all objects that can be added to a VIEW.
Definition view_item.h:82
Holds all the data relating to one schematic.
Definition schematic.h:149
SCH_SCREEN * RootScreen() const
Helper to retrieve the screen of the root sheet.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
void SetBackgroundColor(const COLOR4D &aColor) override
Set the background color.
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:122
Schematic symbol object.
Definition sch_symbol.h:73
GAL-backed canvas for visualizing a KICAD_DIFF::DIFF_SCENE.
void SetContextItems(const std::vector< KIGFX::VIEW_ITEM * > &aItems)
Replace source document context items.
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 SetItemDimmer(DIMMER aDimmer)
#define LAYER_ID_COUNT
Must update this if you add any enums after Gerbview!
Definition layer_ids.h:652
@ LAYER_BRIGHTENED
Definition layer_ids.h:513
void ConfigureSchDiffCanvasContext(WIDGET_DIFF_CANVAS &aCanvas, SCHEMATIC *aReference, SCHEMATIC *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, SCH_SCREEN *aReferenceScreen, SCH_SCREEN *aComparisonScreen)
void ConfigureSchDiffContextRenderSettings(SCH_RENDER_SETTINGS &aSettings, const KIGFX::COLOR4D &aColor)
std::vector< KIGFX::VIEW_ITEM * > CollectSchematicDiffContextItems(SCHEMATIC &aSchematic, SCH_SCREEN *aScreen)
std::unique_ptr< KIGFX::PAINTER > MakeSchDiffContextPainter(KIGFX::GAL *aGal, SCHEMATIC *aSchematic, const KIGFX::COLOR4D &aColor, std::map< KIID, KIGFX::COLOR4D > aOverrides)
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
#define ALL_UNITS
Definition symbol.h:150
#define ALL_BODY_STYLES
Definition symbol.h:151
KIBIS_PIN * pin
wxString result
Test unit parsing edge cases and error handling.