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
22#include <wx/bmpbuttn.h>
23#include <wx/sizer.h>
24#include <wx/slider.h>
25#include <wx/stattext.h>
26
27#include <bitmaps.h>
28#include <hotkeys_basic.h>
29#include <lib_symbol.h>
30#include <sch_painter.h>
31#include <eeschema_settings.h>
33#include <sch_view.h>
34
35
37 EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType ) :
38 SYMBOL_PREVIEW_WIDGET( aParent, nullptr, false, aCanvasType ),
39 m_libraryItem( nullptr ),
40 m_slider( nullptr )
41{
42 wxBoxSizer* bottomSizer = new wxBoxSizer( wxHORIZONTAL );
43
44 wxStaticText* schLabel = new wxStaticText( this, wxID_ANY, _( "Schematic" ) );
45 wxStaticText* libLabel = new wxStaticText( this, wxID_ANY, _( "Library" ) );
46 m_slider = new wxSlider( this, wxID_ANY, 50, 0, 100 );
47
48 bottomSizer->Add( schLabel, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxALIGN_CENTRE_VERTICAL, 6 );
49 bottomSizer->Add( m_slider, 1, wxLEFT | wxRIGHT | wxALIGN_BOTTOM, 30 );
50 bottomSizer->Add( libLabel, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxALIGN_CENTRE_VERTICAL, 6 );
51
52 m_toggleButton = new wxBitmapButton( this, wxID_ANY, KiBitmapBundle( BITMAPS::swap ) );
53 wxString toggleTooltip = _( "Toggle between A and B display" );
54 toggleTooltip = AddHotkeyName( toggleTooltip, '/', HOTKEY_ACTION_TYPE::IS_COMMENT );
55 m_toggleButton->SetToolTip( toggleTooltip );
56
57 bottomSizer->Add( m_toggleButton, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL, 6 );
58
59 m_outerSizer->Add( bottomSizer, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, 10 );
60
61 Layout();
62
63 m_slider->Bind( wxEVT_SCROLL_TOP, &SYMBOL_DIFF_WIDGET::onSlider, this );
64 m_slider->Bind( wxEVT_SCROLL_BOTTOM, &SYMBOL_DIFF_WIDGET::onSlider, this );
65 m_slider->Bind( wxEVT_SCROLL_LINEUP, &SYMBOL_DIFF_WIDGET::onSlider, this );
66 m_slider->Bind( wxEVT_SCROLL_LINEDOWN, &SYMBOL_DIFF_WIDGET::onSlider, this );
67 m_slider->Bind( wxEVT_SCROLL_PAGEUP, &SYMBOL_DIFF_WIDGET::onSlider, this );
68 m_slider->Bind( wxEVT_SCROLL_PAGEDOWN, &SYMBOL_DIFF_WIDGET::onSlider, this );
69 m_slider->Bind( wxEVT_SCROLL_THUMBTRACK, &SYMBOL_DIFF_WIDGET::onSlider, this );
70 m_slider->Bind( wxEVT_SCROLL_THUMBRELEASE, &SYMBOL_DIFF_WIDGET::onSlider, this );
71 m_slider->Bind( wxEVT_SCROLL_CHANGED, &SYMBOL_DIFF_WIDGET::onSlider, this );
72
73 Bind( wxEVT_CHAR_HOOK, &SYMBOL_DIFF_WIDGET::onCharHook, this );
74
75 m_toggleButton->Bind( wxEVT_BUTTON,
76 [this]( wxCommandEvent& aEvent )
77 {
78 ToggleAB();
79 } );
80}
81
82
84{
85 delete m_libraryItem;
86}
87
88
89void SYMBOL_DIFF_WIDGET::DisplayDiff( LIB_SYMBOL* aSchSymbol, LIB_SYMBOL* aLibSymbol, int aUnit,
90 int aConvert )
91{
92 KIGFX::VIEW* view = m_preview->GetView();
93
94 if( m_previewItem )
95 {
96 view->Remove( m_previewItem );
97 delete m_previewItem;
98 m_previewItem = nullptr;
99
100 wxASSERT( m_libraryItem );
101
102 view->Remove( m_libraryItem );
103 delete m_libraryItem;
104 m_libraryItem = nullptr;
105 }
106
107 if( aSchSymbol )
108 {
109 m_previewItem = aSchSymbol;
110
111 // For symbols having a De Morgan body style, use the first style
112 auto settings = static_cast<SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
113
114 settings->m_ShowUnit = ( m_previewItem->IsMulti() && !aUnit ) ? 1 : aUnit;
115 settings->m_ShowBodyStyle = ( m_previewItem->HasAlternateBodyStyle() && !aConvert ) ? 1 : aConvert;
116
117 view->Add( m_previewItem );
118
119 // Get the symbol size, in internal units
120 m_itemBBox = m_previewItem->GetUnitBoundingBox( settings->m_ShowUnit,
121 settings->m_ShowBodyStyle );
122
123 // Calculate the draw scale to fit the drawing area
125
126 wxASSERT( aLibSymbol );
127
128 m_libraryItem = aLibSymbol;
129 view->Add( m_libraryItem );
130 }
131
132 wxScrollEvent dummy;
133 onSlider( dummy );
134
135 m_preview->Show();
136 Layout();
137}
138
139
141{
142 const int val = m_slider->GetValue();
143
144 if( val == 0 )
145 m_slider->SetValue( 100 );
146 else
147 m_slider->SetValue( 0 );
148
149 wxScrollEvent dummy;
150 onSlider( dummy );
151}
152
153
154void SYMBOL_DIFF_WIDGET::onSlider( wxScrollEvent& aEvent )
155{
156 KIGFX::VIEW* view = m_preview->GetView();
157 double pct = (double) m_slider->GetValue() / 100.0;
158
159 if( m_previewItem )
160 {
161 double val;
162
163 if( pct < 0.5 )
164 val = 0.0;
165 else
166 val = ( pct - 0.5 ) * 2;
167
169 view->Update( m_previewItem );
170
171 for( SCH_ITEM& child : m_previewItem->GetDrawItems() )
172 {
173 child.SetForcedTransparency( val );
174 view->Update( &child );
175 }
176 }
177
178 if( m_libraryItem )
179 {
180 double val;
181
182 if( pct > 0.5 )
183 val = 0.0;
184 else
185 val = 1.0 - ( pct * 2 );
186
188 view->Update( m_libraryItem );
189
190 for( SCH_ITEM& child : m_libraryItem->GetDrawItems() )
191 {
192 child.SetForcedTransparency( val );
193 view->Update( &child );
194 }
195 }
196
198
199 aEvent.Skip();
200}
201
202
203void SYMBOL_DIFF_WIDGET::onCharHook( wxKeyEvent& aEvent )
204{
205 if( aEvent.GetKeyCode() == '/' )
206 {
207 ToggleAB();
208 }
209 else
210 {
211 aEvent.Skip();
212 }
213}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
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:153
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:299
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:334
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:1669
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:217
Define a library symbol object.
Definition: lib_symbol.h:78
bool IsMulti() const override
Definition: lib_symbol.h:554
const BOX2I GetUnitBoundingBox(int aUnit, int aBodyStyle, bool aIgnoreHiddenFields=true) const
Get the bounding box for the symbol.
Definition: lib_symbol.cpp:943
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:499
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:166
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 onCharHook(wxKeyEvent &aEvent)
void ToggleAB()
Toggle between full-A and full-B display.
void onSlider(wxScrollEvent &aEvent)
wxBitmapButton * m_toggleButton
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)
wxString AddHotkeyName(const wxString &aText, int aHotKey, HOTKEY_ACTION_TYPE aStyle)
std::vector< FAB_LAYER_COLOR > dummy