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{
128 aCanvas.SetContextPainter(
129 MakeSchDiffContextPainter( aCanvas.GetGAL(), aReference ? aReference : aComparison, aColor, aOverrides ) );
130
131 std::vector<KIGFX::VIEW_ITEM*> items;
132
133 if( aReference )
134 {
135 std::vector<KIGFX::VIEW_ITEM*> refItems = CollectSchematicDiffContextItems( *aReference, aReferenceScreen );
136 items.insert( items.end(), refItems.begin(), refItems.end() );
137 }
138
139 if( aComparison )
140 {
141 std::vector<KIGFX::VIEW_ITEM*> compItems = CollectSchematicDiffContextItems( *aComparison, aComparisonScreen );
142 items.insert( items.end(), compItems.begin(), compItems.end() );
143 }
144
145 items.insert( items.end(), aExtraItems.begin(), aExtraItems.end() );
146
147 for( KIGFX::VIEW_ITEM* viewItem : items )
148 {
149 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( viewItem );
150
151 if( !schItem )
152 continue;
153
154 const bool brighten = aOverrides.count( schItem->m_Uuid ) > 0;
155
156 if( brighten )
157 schItem->SetBrightened();
158 else
159 schItem->ClearBrightened();
160
161 // Mirror brightening onto pins and fields. The painter checks each
162 // child item's IsBrightened individually.
163 if( SCH_SYMBOL* sym = dynamic_cast<SCH_SYMBOL*>( schItem ) )
164 {
165 for( SCH_PIN* pin : sym->GetPins() )
166 {
167 if( !pin )
168 continue;
169
170 if( brighten )
171 pin->SetBrightened();
172 else
173 pin->ClearBrightened();
174 }
175
176 for( SCH_FIELD& field : sym->GetFields() )
177 {
178 if( brighten )
179 field.SetBrightened();
180 else
181 field.ClearBrightened();
182 }
183 }
184 }
185
186 aCanvas.SetContextItems( items );
187
188 aCanvas.SetItemDimmer(
189 [overrides = aOverrides]( KIGFX::VIEW_ITEM* aItem, bool aDim )
190 {
191 SCH_ITEM* sch = dynamic_cast<SCH_ITEM*>( aItem );
192
193 if( !sch )
194 return;
195
196 const bool inOverride = overrides.count( sch->m_Uuid ) > 0;
197 const bool brighten = !aDim && inOverride;
198
199 auto apply = [brighten]( EDA_ITEM* aEdaItem )
200 {
201 if( brighten )
202 aEdaItem->SetBrightened();
203 else
204 aEdaItem->ClearBrightened();
205 };
206
207 apply( sch );
208
209 if( SCH_SYMBOL* sym = dynamic_cast<SCH_SYMBOL*>( sch ) )
210 {
211 for( SCH_PIN* pin : sym->GetPins() )
212 {
213 if( pin )
214 apply( pin );
215 }
216
217 for( SCH_FIELD& field : sym->GetFields() )
218 apply( &field );
219 }
220 } );
221
222 std::map<KIGFX::VIEW_ITEM*, KICAD_DIFF::CATEGORY> itemCategories;
223
224 for( KIGFX::VIEW_ITEM* viewItem : items )
225 {
226 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( viewItem );
227
228 if( !schItem )
229 continue;
230
231 auto it = aCategories.find( schItem->m_Uuid );
232
233 if( it != aCategories.end() )
234 itemCategories[viewItem] = it->second;
235 }
236
237 aCanvas.SetItemCategories( std::move( itemCategories ) );
238}
239
240} // 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:96
const KIID m_Uuid
Definition eda_item.h:531
void ClearBrightened()
Definition eda_item.h:148
void SetBrightened()
Definition eda_item.h:145
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:65
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:90
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:162
void SetBackgroundColor(const COLOR4D &aColor) override
Set the background color.
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:115
Schematic symbol object.
Definition sch_symbol.h:69
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:624
@ LAYER_BRIGHTENED
Definition layer_ids.h:489
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.
KIBIS_PIN * pin
wxString result
Test unit parsing edge cases and error handling.