KiCad PCB EDA Suite
Loading...
Searching...
No Matches
symbol_diff_widget.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) 2023 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21#include <sch_painter.h>
22#include <eeschema_settings.h>
24#include <sch_view.h>
25#include <wx/sizer.h>
26#include <wx/stattext.h>
27#include <wx/slider.h>
28
29
31 EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType ) :
32 SYMBOL_PREVIEW_WIDGET( aParent, nullptr, false, aCanvasType ),
33 m_libraryItem( nullptr ),
34 m_slider( nullptr )
35{
36 wxBoxSizer* bottomSizer = new wxBoxSizer( wxHORIZONTAL );
37
38 wxStaticText* schLabel = new wxStaticText( this, wxID_ANY, _( "Schematic" ) );
39 wxStaticText* libLabel = new wxStaticText( this, wxID_ANY, _( "Library" ) );
40 m_slider = new wxSlider( this, wxID_ANY, 50, 0, 100 );
41
42 bottomSizer->Add( schLabel, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxALIGN_CENTRE_VERTICAL, 6 );
43 bottomSizer->Add( m_slider, 1, wxLEFT | wxRIGHT | wxALIGN_BOTTOM, 30 );
44 bottomSizer->Add( libLabel, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxALIGN_CENTRE_VERTICAL, 6 );
45
46 m_outerSizer->Add( bottomSizer, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, 10 );
47
48 Layout();
49
50 m_slider->Bind( wxEVT_SCROLL_TOP, &SYMBOL_DIFF_WIDGET::onSlider, this );
51 m_slider->Bind( wxEVT_SCROLL_BOTTOM, &SYMBOL_DIFF_WIDGET::onSlider, this );
52 m_slider->Bind( wxEVT_SCROLL_LINEUP, &SYMBOL_DIFF_WIDGET::onSlider, this );
53 m_slider->Bind( wxEVT_SCROLL_LINEDOWN, &SYMBOL_DIFF_WIDGET::onSlider, this );
54 m_slider->Bind( wxEVT_SCROLL_PAGEUP, &SYMBOL_DIFF_WIDGET::onSlider, this );
55 m_slider->Bind( wxEVT_SCROLL_PAGEDOWN, &SYMBOL_DIFF_WIDGET::onSlider, this );
56 m_slider->Bind( wxEVT_SCROLL_THUMBTRACK, &SYMBOL_DIFF_WIDGET::onSlider, this );
57 m_slider->Bind( wxEVT_SCROLL_THUMBRELEASE, &SYMBOL_DIFF_WIDGET::onSlider, this );
58 m_slider->Bind( wxEVT_SCROLL_CHANGED, &SYMBOL_DIFF_WIDGET::onSlider, this );
59}
60
61
63{
64 delete m_libraryItem;
65}
66
67
68void SYMBOL_DIFF_WIDGET::DisplayDiff( LIB_SYMBOL* aSchSymbol, LIB_SYMBOL* aLibSymbol, int aUnit,
69 int aConvert )
70{
71 KIGFX::VIEW* view = m_preview->GetView();
72
73 if( m_previewItem )
74 {
75 view->Remove( m_previewItem );
76 delete m_previewItem;
77 m_previewItem = nullptr;
78
79 wxASSERT( m_libraryItem );
80
81 view->Remove( m_libraryItem );
82 delete m_libraryItem;
83 m_libraryItem = nullptr;
84 }
85
86 if( aSchSymbol )
87 {
88 m_previewItem = aSchSymbol;
89
90 // For symbols having a De Morgan body style, use the first style
91 auto settings = static_cast<SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
92
93 settings->m_ShowUnit = ( m_previewItem->IsMulti() && !aUnit ) ? 1 : aUnit;
94 settings->m_ShowBodyStyle = ( m_previewItem->HasAlternateBodyStyle() && !aConvert ) ? 1 : aConvert;
95
96 view->Add( m_previewItem );
97
98 // Get the symbol size, in internal units
99 m_itemBBox = m_previewItem->GetUnitBoundingBox( settings->m_ShowUnit,
100 settings->m_ShowBodyStyle );
101
102 // Calculate the draw scale to fit the drawing area
104
105 wxASSERT( aLibSymbol );
106
107 m_libraryItem = aLibSymbol;
108 view->Add( m_libraryItem );
109 }
110
111 wxScrollEvent dummy;
112 onSlider( dummy );
113
114 m_preview->Show();
115 Layout();
116}
117
118
119void SYMBOL_DIFF_WIDGET::onSlider( wxScrollEvent& aEvent )
120{
121 KIGFX::VIEW* view = m_preview->GetView();
122 double pct = (double) m_slider->GetValue() / 100.0;
123
124 if( m_previewItem )
125 {
126 double val;
127
128 if( pct < 0.5 )
129 val = 0.0;
130 else
131 val = ( pct - 0.5 ) * 2;
132
134 view->Update( m_previewItem );
135
136 for( SCH_ITEM& child : m_previewItem->GetDrawItems() )
137 {
138 child.SetForcedTransparency( val );
139 view->Update( &child );
140 }
141 }
142
143 if( m_libraryItem )
144 {
145 double val;
146
147 if( pct > 0.5 )
148 val = 0.0;
149 else
150 val = 1.0 - ( pct * 2 );
151
153 view->Update( m_libraryItem );
154
155 for( SCH_ITEM& child : m_libraryItem->GetDrawItems() )
156 {
157 child.SetForcedTransparency( val );
158 view->Update( &child );
159 }
160 }
161
163
164 aEvent.Skip();
165}
virtual KIGFX::VIEW * GetView() const
Return a pointer to the #VIEW instance used in the panel.
void ForceRefresh()
Force a redraw.
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
void SetForcedTransparency(double aForcedTransparency)
Definition: view_item.h:152
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:315
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:354
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: view.cpp:1639
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:215
Define a library symbol object.
Definition: lib_symbol.h:78
bool IsMulti() const override
Definition: lib_symbol.h:543
const BOX2I GetUnitBoundingBox(int aUnit, int aBodyStyle, bool aIgnoreHiddenFields=true) const
Get the bounding box for the symbol.
bool HasAlternateBodyStyle() const override
Test if symbol has more than one body conversion type (DeMorgan).
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
Definition: lib_symbol.h:488
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:172
void DisplayDiff(LIB_SYMBOL *aSchSymbol, LIB_SYMBOL *aLibSymbol, int aUnit, int aConvert)
Set the currently displayed symbol.
LIB_SYMBOL * m_libraryItem
SYMBOL_DIFF_WIDGET(wxWindow *aParent, EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType)
Construct a symbol diff widget, consisting on a canvas for displaying a schematic and a library symbo...
void onSlider(wxScrollEvent &aEvent)
LIB_SYMBOL * m_previewItem
A local copy of the LIB_SYMBOL to display on the canvas.
EDA_DRAW_PANEL_GAL * m_preview
BOX2I m_itemBBox
The bounding box of the current item.
#define _(s)
std::vector< FAB_LAYER_COLOR > dummy