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 The 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
87
88
89void SYMBOL_DIFF_WIDGET::DisplayDiff( LIB_SYMBOL* aSchSymbol, LIB_SYMBOL* aLibSymbol, int aUnit,
90 int aBodyStyle )
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->IsMultiUnit() && !aUnit ) ? 1 : aUnit;
115 settings->m_ShowBodyStyle = ( m_previewItem->IsMultiBodyStyle() && !aBodyStyle ) ? 1 : aBodyStyle;
116
117 view->Add( m_previewItem );
118
119 // Get the symbol size, in internal units
120 m_itemBBox = m_previewItem->GetUnitBoundingBox( settings->m_ShowUnit, settings->m_ShowBodyStyle );
121
122 // Calculate the draw scale to fit the drawing area
124
125 wxASSERT( aLibSymbol );
126
127 m_libraryItem = aLibSymbol;
128 view->Add( m_libraryItem );
129 }
130
131 wxScrollEvent dummy;
132 onSlider( dummy );
133
134 m_preview->Show();
135 Layout();
136}
137
138
140{
141 const int val = m_slider->GetValue();
142
143 if( val == 0 )
144 m_slider->SetValue( 100 );
145 else
146 m_slider->SetValue( 0 );
147
148 wxScrollEvent dummy;
149 onSlider( dummy );
150}
151
152
153void SYMBOL_DIFF_WIDGET::onSlider( wxScrollEvent& aEvent )
154{
155 KIGFX::VIEW* view = m_preview->GetView();
156 double pct = (double) m_slider->GetValue() / 100.0;
157
158 if( m_previewItem )
159 {
160 double val;
161
162 if( pct < 0.5 )
163 val = 0.0;
164 else
165 val = ( pct - 0.5 ) * 2;
166
167 m_previewItem->SetForcedTransparency( val );
168 view->Update( m_previewItem );
169
170 for( SCH_ITEM& child : m_previewItem->GetDrawItems() )
171 {
172 child.SetForcedTransparency( val );
173 view->Update( &child );
174 }
175 }
176
177 if( m_libraryItem )
178 {
179 double val;
180
181 if( pct > 0.5 )
182 val = 0.0;
183 else
184 val = 1.0 - ( pct * 2 );
185
186 m_libraryItem->SetForcedTransparency( val );
187 view->Update( m_libraryItem );
188
189 for( SCH_ITEM& child : m_libraryItem->GetDrawItems() )
190 {
191 child.SetForcedTransparency( val );
192 view->Update( &child );
193 }
194 }
195
196 m_preview->ForceRefresh();
197
198 aEvent.Skip();
199}
200
201
202void SYMBOL_DIFF_WIDGET::onCharHook( wxKeyEvent& aEvent )
203{
204 if( aEvent.GetKeyCode() == '/' )
205 {
206 ToggleAB();
207 }
208 else
209 {
210 aEvent.Skip();
211 }
212}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:66
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:298
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition view.cpp:341
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:1685
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:220
Define a library symbol object.
Definition lib_symbol.h:85
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:167
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 DisplayDiff(LIB_SYMBOL *aSchSymbol, LIB_SYMBOL *aLibSymbol, int aUnit, int aBodyStyle)
Set the currently displayed symbol.
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.
SYMBOL_PREVIEW_WIDGET(wxWindow *aParent, KIWAY *aKiway, bool aIncludeStatus, EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType)
Construct a symbol preview widget.
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)
@ IS_COMMENT
std::vector< FAB_LAYER_COLOR > dummy