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 <sch_view.h>
24#include <schematic.h>
25#include <set>
27
28
29namespace
30{
31
32class SCH_DIFF_PAINTER : public KIGFX::SCH_PAINTER
33{
34public:
35 SCH_DIFF_PAINTER( KIGFX::GAL* aGal, std::map<KIID, KIGFX::COLOR4D> aOverrides ) :
36 KIGFX::SCH_PAINTER( aGal ),
37 m_overrides( std::move( aOverrides ) )
38 {
39 }
40
41 bool Draw( const KIGFX::VIEW_ITEM* aItem, int aLayer ) override
42 {
43 const EDA_ITEM* edaItem = dynamic_cast<const EDA_ITEM*>( aItem );
44
45 if( edaItem )
46 {
47 auto it = m_overrides.find( edaItem->m_Uuid );
48
49 if( it != m_overrides.end() )
50 {
51 SCH_RENDER_SETTINGS* settings = GetSettings();
52 KIGFX::COLOR4D saved = settings->GetLayerColor( LAYER_BRIGHTENED );
53 settings->SetLayerColor( LAYER_BRIGHTENED, it->second );
54
55 bool result = KIGFX::SCH_PAINTER::Draw( aItem, aLayer );
56
57 settings->SetLayerColor( LAYER_BRIGHTENED, saved );
58 return result;
59 }
60 }
61
62 return KIGFX::SCH_PAINTER::Draw( aItem, aLayer );
63 }
64
65private:
66 std::map<KIID, KIGFX::COLOR4D> m_overrides;
67};
68
69} // namespace
70
71
72namespace KICAD_DIFF
73{
74
75std::vector<KIGFX::VIEW_ITEM*> CollectSchematicDiffContextItems( SCHEMATIC& aSchematic, SCH_SCREEN* aScreen )
76{
77 std::vector<KIGFX::VIEW_ITEM*> items;
78 SCH_SCREEN* screen = aScreen ? aScreen : aSchematic.RootScreen();
79
80 if( !screen )
81 return items;
82
83 for( SCH_ITEM* item : screen->Items() )
84 items.push_back( item );
85
86 return items;
87}
88
89
91 const KIGFX::COLOR4D& aColor )
92{
93 const KIGFX::COLOR4D gray( 0.5, 0.5, 0.5, 1.0 );
94
95 aSettings.SetAllLayerColors( gray );
96 aSettings.SetLayerColor( LAYER_BRIGHTENED, aColor );
97
98 aSettings.SetBackgroundColor( KIGFX::COLOR4D( 0.97, 0.97, 0.97, 1.0 ) );
99 aSettings.SetHighContrast( false );
100 aSettings.SetHighlight( false );
101 aSettings.m_OverrideItemColors = true;
102 aSettings.m_ShowPinsElectricalType = false;
103 aSettings.m_ShowHiddenPins = true;
104 aSettings.m_ShowHiddenFields = true;
105 aSettings.m_ShowVisibleFields = true;
106}
107
108
109std::unique_ptr<KIGFX::PAINTER> MakeSchDiffContextPainter( KIGFX::GAL* aGal, SCHEMATIC* aSchematic,
110 const KIGFX::COLOR4D& aColor,
111 std::map<KIID, KIGFX::COLOR4D> aOverrides )
112{
113 auto painter = std::make_unique<SCH_DIFF_PAINTER>( aGal, std::move( aOverrides ) );
114 painter->SetSchematic( aSchematic );
115 ConfigureSchDiffContextRenderSettings( *painter->GetSettings(), aColor );
116
117 return painter;
118}
119
120
121void ConfigureSchDiffCanvasContext( WIDGET_DIFF_CANVAS& aCanvas, SCHEMATIC* aReference, SCHEMATIC* aComparison,
122 const KIGFX::COLOR4D& aColor, const std::map<KIID, KIGFX::COLOR4D>& aOverrides,
123 const std::vector<KIGFX::VIEW_ITEM*>& aExtraItems,
124 const std::map<KIID, KICAD_DIFF::CATEGORY>& aCategories,
125 SCH_SCREEN* aReferenceScreen, SCH_SCREEN* aComparisonScreen,
126 std::shared_ptr<const void> aOwner )
127{
128 // The canvas defaults to board units, which caps a schematic's zoom a hundredfold too low
131 aReference ? aReference : aComparison,
132 aColor, aOverrides ) );
133
134 std::vector<KIGFX::VIEW_ITEM*> items;
135
136 if( aReference )
137 {
138 std::vector<KIGFX::VIEW_ITEM*> refItems = CollectSchematicDiffContextItems( *aReference, aReferenceScreen );
139 items.insert( items.end(), refItems.begin(), refItems.end() );
140 }
141
142 if( aComparison )
143 {
144 std::vector<KIGFX::VIEW_ITEM*> compItems = CollectSchematicDiffContextItems( *aComparison, aComparisonScreen );
145 items.insert( items.end(), compItems.begin(), compItems.end() );
146 }
147
148 items.insert( items.end(), aExtraItems.begin(), aExtraItems.end() );
149
150 for( KIGFX::VIEW_ITEM* viewItem : items )
151 {
152 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( viewItem );
153
154 if( !schItem )
155 continue;
156
157 const bool brighten = aOverrides.count( schItem->m_Uuid ) > 0;
158
159 if( brighten )
160 schItem->SetBrightened();
161 else
162 schItem->ClearBrightened();
163
164 // Mirror brightening onto pins and fields. The painter checks each
165 // child item's IsBrightened individually.
166 if( SCH_SYMBOL* sym = dynamic_cast<SCH_SYMBOL*>( schItem ) )
167 {
168 for( SCH_PIN* pin : sym->GetGraphicalPins( ALL_UNITS, ALL_BODY_STYLES ) )
169 {
170 if( !pin )
171 continue;
172
173 if( brighten )
174 pin->SetBrightened();
175 else
176 pin->ClearBrightened();
177 }
178
179 for( SCH_FIELD& field : sym->GetFields() )
180 {
181 if( brighten )
182 field.SetBrightened();
183 else
184 field.ClearBrightened();
185 }
186 }
187 }
188
189 aCanvas.SetContextItems( items, std::move( aOwner ) );
190
191 aCanvas.SetItemDimmer(
192 [overrides = aOverrides]( KIGFX::VIEW_ITEM* aItem, bool aDim )
193 {
194 SCH_ITEM* sch = dynamic_cast<SCH_ITEM*>( aItem );
195
196 if( !sch )
197 return;
198
199 const bool inOverride = overrides.count( sch->m_Uuid ) > 0;
200 const bool brighten = !aDim && inOverride;
201
202 auto apply = [brighten]( EDA_ITEM* aEdaItem )
203 {
204 if( brighten )
205 aEdaItem->SetBrightened();
206 else
207 aEdaItem->ClearBrightened();
208 };
209
210 apply( sch );
211
212 if( SCH_SYMBOL* sym = dynamic_cast<SCH_SYMBOL*>( sch ) )
213 {
214 for( SCH_PIN* pin : sym->GetGraphicalPins( ALL_UNITS, ALL_BODY_STYLES ) )
215 {
216 if( pin )
217 apply( pin );
218 }
219
220 for( SCH_FIELD& field : sym->GetFields() )
221 apply( &field );
222 }
223 } );
224
225 std::map<KIGFX::VIEW_ITEM*, KICAD_DIFF::CATEGORY> itemCategories;
226
227 for( KIGFX::VIEW_ITEM* viewItem : items )
228 {
229 SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( viewItem );
230
231 if( !schItem )
232 continue;
233
234 auto it = aCategories.find( schItem->m_Uuid );
235
236 if( it != aCategories.end() )
237 itemCategories[viewItem] = it->second;
238 }
239
240 aCanvas.SetItemCategories( std::move( itemCategories ) );
241}
242
243} // 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 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.
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:155
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:76
GAL-backed canvas for visualizing a KICAD_DIFF::DIFF_SCENE.
void SetWorldUnitLength(double aWorldUnitLength)
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 SetContextItems(const std::vector< KIGFX::VIEW_ITEM * > &aItems, std::shared_ptr< const void > aOwner=nullptr)
Replace source document context items.
void SetItemDimmer(DIMMER aDimmer)
@ LAYER_BRIGHTENED
Definition layer_ids.h:522
void ConfigureSchDiffContextRenderSettings(SCH_RENDER_SETTINGS &aSettings, const KIGFX::COLOR4D &aColor)
std::vector< KIGFX::VIEW_ITEM * > CollectSchematicDiffContextItems(SCHEMATIC &aSchematic, SCH_SCREEN *aScreen)
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, std::shared_ptr< const void > aOwner)
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.
constexpr double SCH_WORLD_UNIT(1e-7/0.0254)
#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.