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 <lib_symbol.h>
25#include <sch_view.h>
26#include <wx/sizer.h>
27#include <wx/stattext.h>
28#include <wx/slider.h>
29
30
32 EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType ) :
33 SYMBOL_PREVIEW_WIDGET( aParent, nullptr, false, aCanvasType ),
34 m_libraryItem( nullptr ),
35 m_slider( nullptr )
36{
37 wxBoxSizer* bottomSizer = new wxBoxSizer( wxHORIZONTAL );
38
39 wxStaticText* schLabel = new wxStaticText( this, wxID_ANY, _( "Schematic" ) );
40 wxStaticText* libLabel = new wxStaticText( this, wxID_ANY, _( "Library" ) );
41 m_slider = new wxSlider( this, wxID_ANY, 50, 0, 100 );
42
43 bottomSizer->Add( schLabel, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxALIGN_CENTRE_VERTICAL, 6 );
44 bottomSizer->Add( m_slider, 1, wxLEFT | wxRIGHT | wxALIGN_BOTTOM, 30 );
45 bottomSizer->Add( libLabel, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxALIGN_CENTRE_VERTICAL, 6 );
46
47 m_outerSizer->Add( bottomSizer, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, 10 );
48
49 Layout();
50
51 m_slider->Bind( wxEVT_SCROLL_TOP, &SYMBOL_DIFF_WIDGET::onSlider, this );
52 m_slider->Bind( wxEVT_SCROLL_BOTTOM, &SYMBOL_DIFF_WIDGET::onSlider, this );
53 m_slider->Bind( wxEVT_SCROLL_LINEUP, &SYMBOL_DIFF_WIDGET::onSlider, this );
54 m_slider->Bind( wxEVT_SCROLL_LINEDOWN, &SYMBOL_DIFF_WIDGET::onSlider, this );
55 m_slider->Bind( wxEVT_SCROLL_PAGEUP, &SYMBOL_DIFF_WIDGET::onSlider, this );
56 m_slider->Bind( wxEVT_SCROLL_PAGEDOWN, &SYMBOL_DIFF_WIDGET::onSlider, this );
57 m_slider->Bind( wxEVT_SCROLL_THUMBTRACK, &SYMBOL_DIFF_WIDGET::onSlider, this );
58 m_slider->Bind( wxEVT_SCROLL_THUMBRELEASE, &SYMBOL_DIFF_WIDGET::onSlider, this );
59 m_slider->Bind( wxEVT_SCROLL_CHANGED, &SYMBOL_DIFF_WIDGET::onSlider, this );
60}
61
62
64{
65 delete m_libraryItem;
66}
67
68
69void SYMBOL_DIFF_WIDGET::DisplayDiff( LIB_SYMBOL* aSchSymbol, LIB_SYMBOL* aLibSymbol, int aUnit,
70 int aConvert )
71{
72 KIGFX::VIEW* view = m_preview->GetView();
73
74 if( m_previewItem )
75 {
76 view->Remove( m_previewItem );
77 delete m_previewItem;
78 m_previewItem = nullptr;
79
80 wxASSERT( m_libraryItem );
81
82 view->Remove( m_libraryItem );
83 delete m_libraryItem;
84 m_libraryItem = nullptr;
85 }
86
87 if( aSchSymbol )
88 {
89 m_previewItem = aSchSymbol;
90
91 // For symbols having a De Morgan body style, use the first style
92 auto settings = static_cast<SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
93
94 settings->m_ShowUnit = ( m_previewItem->IsMulti() && !aUnit ) ? 1 : aUnit;
95 settings->m_ShowBodyStyle = ( m_previewItem->HasAlternateBodyStyle() && !aConvert ) ? 1 : aConvert;
96
97 view->Add( m_previewItem );
98
99 // Get the symbol size, in internal units
100 m_itemBBox = m_previewItem->GetUnitBoundingBox( settings->m_ShowUnit,
101 settings->m_ShowBodyStyle );
102
103 // Calculate the draw scale to fit the drawing area
105
106 wxASSERT( aLibSymbol );
107
108 m_libraryItem = aLibSymbol;
109 view->Add( m_libraryItem );
110 }
111
112 wxScrollEvent dummy;
113 onSlider( dummy );
114
115 m_preview->Show();
116 Layout();
117}
118
119
120void SYMBOL_DIFF_WIDGET::onSlider( wxScrollEvent& aEvent )
121{
122 KIGFX::VIEW* view = m_preview->GetView();
123 double pct = (double) m_slider->GetValue() / 100.0;
124
125 if( m_previewItem )
126 {
127 double val;
128
129 if( pct < 0.5 )
130 val = 0.0;
131 else
132 val = ( pct - 0.5 ) * 2;
133
135 view->Update( m_previewItem );
136
137 for( SCH_ITEM& child : m_previewItem->GetDrawItems() )
138 {
139 child.SetForcedTransparency( val );
140 view->Update( &child );
141 }
142 }
143
144 if( m_libraryItem )
145 {
146 double val;
147
148 if( pct > 0.5 )
149 val = 0.0;
150 else
151 val = 1.0 - ( pct * 2 );
152
154 view->Update( m_libraryItem );
155
156 for( SCH_ITEM& child : m_libraryItem->GetDrawItems() )
157 {
158 child.SetForcedTransparency( val );
159 view->Update( &child );
160 }
161 }
162
164
165 aEvent.Skip();
166}
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:1631
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:77
bool IsMulti() const override
Definition: lib_symbol.h:541
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:486
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:174
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