You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017-2023 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. #include <widgets/footprint_preview_widget.h>
  21. #include <lib_id.h>
  22. #include <wx/stattext.h>
  23. #include <wx/sizer.h>
  24. #include <kiway.h>
  25. FOOTPRINT_PREVIEW_WIDGET::FOOTPRINT_PREVIEW_WIDGET( wxWindow* aParent, KIWAY& aKiway ) :
  26. wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
  27. wxFULL_REPAINT_ON_RESIZE | wxTAB_TRAVERSAL ),
  28. m_prev_panel( nullptr ),
  29. m_status( nullptr ),
  30. m_statusPanel( nullptr ),
  31. m_statusSizer( nullptr ),
  32. m_outerSizer( nullptr )
  33. {
  34. m_prev_panel = FOOTPRINT_PREVIEW_PANEL_BASE::Create( this, aKiway );
  35. if( !m_prev_panel )
  36. return;
  37. m_statusPanel = new wxPanel( this );
  38. m_status = new wxStaticText( m_statusPanel, wxID_ANY, wxEmptyString );
  39. m_statusSizer = new wxBoxSizer( wxVERTICAL );
  40. m_statusSizer->Add( 0, 0, 1 ); // add a spacer
  41. m_statusSizer->Add( m_status, 0, wxALIGN_CENTER );
  42. m_statusSizer->Add( 0, 0, 1 ); // add a spacer
  43. m_statusPanel->SetSizer( m_statusSizer );
  44. // Give the status panel the same color scheme as the canvas so it isn't jarring when
  45. // switched to
  46. m_statusPanel->SetBackgroundColour( m_prev_panel->GetBackgroundColor().ToColour() );
  47. m_statusPanel->SetForegroundColour( m_prev_panel->GetForegroundColor().ToColour() );
  48. m_status->SetForegroundColour( m_prev_panel->GetForegroundColor().ToColour() );
  49. // Set our background so wx doesn't render a normal control background momentarily when
  50. // rapidly navigating with arrow keys
  51. SetBackgroundColour( m_prev_panel->GetBackgroundColor().ToColour() );
  52. SetForegroundColour( m_prev_panel->GetForegroundColor().ToColour() );
  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. SetSizer( m_outerSizer );
  57. SetStatusText( wxEmptyString );
  58. }
  59. void FOOTPRINT_PREVIEW_WIDGET::SetStatusText( wxString const& aText )
  60. {
  61. m_status->SetLabel( aText );
  62. m_statusPanel->Show();
  63. m_prev_panel->GetWindow()->Hide();
  64. Layout();
  65. }
  66. void FOOTPRINT_PREVIEW_WIDGET::ClearStatus()
  67. {
  68. m_status->SetLabel( wxEmptyString );
  69. m_statusPanel->Hide();
  70. m_prev_panel->GetWindow()->Show();
  71. Layout();
  72. }
  73. void FOOTPRINT_PREVIEW_WIDGET::SetUserUnits( EDA_UNITS aUnits )
  74. {
  75. m_prev_panel->SetUserUnits( aUnits );
  76. }
  77. void FOOTPRINT_PREVIEW_WIDGET::DisplayFootprint( const LIB_ID& aFPID )
  78. {
  79. if( !m_prev_panel || m_libid == aFPID )
  80. return;
  81. wxBusyCursor busy;
  82. if( m_prev_panel->DisplayFootprint( aFPID ) )
  83. {
  84. ClearStatus();
  85. m_libid = aFPID;
  86. }
  87. else
  88. {
  89. SetStatusText( _( "Footprint not found." ) );
  90. m_libid.clear();
  91. }
  92. }
  93. void FOOTPRINT_PREVIEW_WIDGET::DisplayFootprints( std::shared_ptr<FOOTPRINT> aFootprintA,
  94. std::shared_ptr<FOOTPRINT> aFootprintB )
  95. {
  96. ClearStatus();
  97. m_prev_panel->DisplayFootprints( aFootprintA, aFootprintB );
  98. }
  99. void FOOTPRINT_PREVIEW_WIDGET::RefreshAll()
  100. {
  101. m_prev_panel->RefreshAll();
  102. }
  103. FOOTPRINT_PREVIEW_PANEL_BASE* FOOTPRINT_PREVIEW_PANEL_BASE::Create( wxWindow* aParent,
  104. KIWAY& aKiway )
  105. {
  106. FOOTPRINT_PREVIEW_PANEL_BASE* panel = nullptr;
  107. try
  108. {
  109. KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
  110. wxWindow* window = kiface->CreateKiWindow( aParent, FRAME_FOOTPRINT_PREVIEW, &aKiway );
  111. panel = dynamic_cast<FOOTPRINT_PREVIEW_PANEL_BASE*>( window );
  112. if( window && !panel )
  113. delete window;
  114. }
  115. catch( ... )
  116. {
  117. }
  118. return panel;
  119. }