KiCad PCB EDA Suite
footprint_preview_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) 2017-2018 KiCad Developers, see AUTHORS.txt for contributors.
5  * Copyright (C) 2017 Chris Pavlina <pavlina.chris@gmail.com>
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
22 #include <wx/stattext.h>
23 #include <wx/sizer.h>
24 #include <kiway.h>
25 
26 
28  wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
29  wxFULL_REPAINT_ON_RESIZE | wxTAB_TRAVERSAL ),
30  m_prev_panel( nullptr ),
31  m_status( nullptr ),
32  m_statusPanel( nullptr ),
33  m_statusSizer( nullptr ),
34  m_outerSizer( nullptr )
35 {
37 
38  if( !m_prev_panel )
39  return;
40 
41  m_statusPanel = new wxPanel( this );
42  m_status = new wxStaticText( m_statusPanel, wxID_ANY, wxEmptyString );
43  m_statusSizer = new wxBoxSizer( wxVERTICAL );
44  m_statusSizer->Add( 0, 0, 1 ); // add a spacer
45  m_statusSizer->Add( m_status, 0, wxALIGN_CENTER );
46  m_statusSizer->Add( 0, 0, 1 ); // add a spacer
47  m_statusPanel->SetSizer( m_statusSizer );
48 
49  // Give the status panel the same color scheme as the canvas so it isn't jarring when switched to
50  m_statusPanel->SetBackgroundColour( m_prev_panel->GetBackgroundColor().ToColour() );
51  m_statusPanel->SetForegroundColour( m_prev_panel->GetForegroundColor().ToColour() );
52 
53  m_outerSizer = new wxBoxSizer( wxVERTICAL );
54  m_outerSizer->Add( m_prev_panel->GetWindow(), 1, wxALL | wxEXPAND, 0 );
55  m_outerSizer->Add( m_statusPanel, 1, wxALL | wxEXPAND, 0 );
56 
57  // Hide the status panel to start
58  m_statusPanel->Hide();
59 
60  m_prev_panel->SetStatusHandler( [this]( FOOTPRINT_STATUS s ){ this->OnStatusChange( s ); } );
61 
62  SetSizer( m_outerSizer );
63  Layout();
64 }
65 
66 
67 void FOOTPRINT_PREVIEW_WIDGET::SetStatusText( wxString const& aText )
68 {
69  m_status->SetLabel( aText );
70  m_statusPanel->Show();
71  m_prev_panel->GetWindow()->Hide();
72  Layout();
73 }
74 
75 
77 {
78  m_status->SetLabel( wxEmptyString );
79  m_statusPanel->Hide();
80  m_prev_panel->GetWindow()->Show();
81  Layout();
82 }
83 
84 
86 {
87  if( m_prev_panel )
88  (void) m_prev_panel->CacheFootprint( aFPID );
89 }
90 
91 
93 {
94  if( m_prev_panel )
95  (void) m_prev_panel->DisplayFootprint( aFPID );
96 }
97 
98 
100 {
101  switch( aStatus )
102  {
103  case FPS_NOT_FOUND:
104  SetStatusText( _( "Footprint not found" ) );
105  break;
106 
107  case FPS_LOADING:
108  SetStatusText( _( "Loading..." ) );
109  break;
110 
111  case FPS_READY:
112  ClearStatus();
113  }
114 
115  Refresh();
116 }
117 
118 
120  KIWAY& aKiway )
121 {
122  FOOTPRINT_PREVIEW_PANEL_BASE* panel = nullptr;
123 
124  try
125  {
126  KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
127 
128  auto window = kiface->CreateWindow( aParent, FRAME_FOOTPRINT_PREVIEW, &aKiway );
129 
130  panel = dynamic_cast<FOOTPRINT_PREVIEW_PANEL_BASE*>( window );
131 
132  if( window && !panel )
133  delete window;
134  }
135  catch( ... )
136  {
137  }
138 
139  return panel;
140 }
BITMAP2CMP_SETTINGS kiface
void OnStatusChange(FOOTPRINT_STATUS aStatus)
Callback from the FOOTPRINT_PREVIEW_PANEL.
virtual const KIGFX::COLOR4D & GetForegroundColor()=0
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
FOOTPRINT_PREVIEW_PANEL_BASE * m_prev_panel
virtual void CacheFootprint(LIB_ID const &aFPID)=0
Preload a footprint into the cache.
virtual void DisplayFootprint(LIB_ID const &aFPID)=0
Set the currently displayed footprint.
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
void ClearStatus()
Clear the contents of the status label and hide it.
virtual wxWindow * GetWindow()=0
Get the underlying wxWindow.
virtual const KIGFX::COLOR4D & GetBackgroundColor()=0
Get the colors to use in a preview widget to match the preview panel.
KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the s...
Definition: kiway.h:273
pcbnew DSO
Definition: kiway.h:282
void SetStatusText(wxString const &aText)
Set the contents of the status label and display it.
VTBL_ENTRY KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Function KiFACE returns the KIFACE* given a FACE_T.
Definition: kiway.cpp:178
Base class for the actual viewer panel.
static FOOTPRINT_PREVIEW_PANEL_BASE * Create(wxWindow *aParent, KIWAY &aKiway)
Return a footprint preview panel instance via Kiface.
void CacheFootprint(const LIB_ID &aFPID)
Preload a footprint into the cache.
#define _(s)
Definition: 3d_actions.cpp:33
void DisplayFootprint(const LIB_ID &aFPID)
Set the currently displayed footprint.
KIFACE is used by a participant in the KIWAY alchemy.
Definition: kiway.h:150
FOOTPRINT_PREVIEW_WIDGET(wxWindow *aParent, KIWAY &aKiway)
Construct a footprint preview widget.
virtual void SetStatusHandler(FOOTPRINT_STATUS_HANDLER aHandler)=0
Set the callback to receive status updates.