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.

202 lines
6.4 KiB

6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016-2020 KiCad Developers, see AUTHORS.txt for contributors.
  5. * Copyright (C) 2017 Chris Pavlina <pavlina.chris@gmail.com>
  6. * Copyright (C) 2016 Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  7. *
  8. * This program is free software: you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation, either version 3 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <memory>
  22. #include <utility>
  23. #include "pcbnew_settings.h"
  24. #include <base_units.h>
  25. #include <board.h>
  26. #include <footprint.h>
  27. #include <eda_draw_frame.h>
  28. #include <footprint_preview_panel.h>
  29. #include <fp_lib_table.h>
  30. #include <kiway.h>
  31. #include <math/box2.h>
  32. #include <pcb_painter.h>
  33. #include <pcb_draw_panel_gal.h>
  34. #include <pcb_edit_frame.h>
  35. #include <pgm_base.h>
  36. #include <settings/settings_manager.h>
  37. #include <view/view.h>
  38. #include <wx/stattext.h>
  39. #include <zoom_defines.h>
  40. FOOTPRINT_PREVIEW_PANEL::FOOTPRINT_PREVIEW_PANEL( KIWAY* aKiway, wxWindow* aParent,
  41. std::unique_ptr<KIGFX::GAL_DISPLAY_OPTIONS> aOpts,
  42. GAL_TYPE aGalType ) :
  43. PCB_DRAW_PANEL_GAL( aParent, -1, wxPoint( 0, 0 ), wxSize( 200, 200 ), *aOpts, aGalType ),
  44. KIWAY_HOLDER( aKiway, KIWAY_HOLDER::PANEL ),
  45. m_displayOptions( std::move( aOpts ) ),
  46. m_currentFootprint( nullptr )
  47. {
  48. SetStealsFocus( false );
  49. ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
  50. EnableScrolling( false, false ); // otherwise Zoom Auto disables GAL canvas
  51. m_dummyBoard = std::make_unique<BOARD>();
  52. UpdateColors();
  53. SyncLayersVisibility( m_dummyBoard.get() );
  54. Raise();
  55. Show( true );
  56. StartDrawing();
  57. }
  58. FOOTPRINT_PREVIEW_PANEL::~FOOTPRINT_PREVIEW_PANEL( )
  59. {
  60. if( m_currentFootprint )
  61. {
  62. GetView()->Remove( m_currentFootprint.get() );
  63. GetView()->Clear();
  64. m_currentFootprint->SetParent( nullptr );
  65. }
  66. }
  67. const COLOR4D& FOOTPRINT_PREVIEW_PANEL::GetBackgroundColor() const
  68. {
  69. KIGFX::PAINTER* painter = GetView()->GetPainter();
  70. auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
  71. return settings->GetBackgroundColor();
  72. }
  73. const COLOR4D& FOOTPRINT_PREVIEW_PANEL::GetForegroundColor() const
  74. {
  75. KIGFX::PAINTER* painter = GetView()->GetPainter();
  76. auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
  77. return settings->GetLayerColor( F_Fab );
  78. }
  79. void FOOTPRINT_PREVIEW_PANEL::renderFootprint( std::shared_ptr<FOOTPRINT> aFootprint )
  80. {
  81. if( m_currentFootprint )
  82. {
  83. GetView()->Remove( m_currentFootprint.get() );
  84. GetView()->Clear();
  85. m_currentFootprint->SetParent( nullptr );
  86. }
  87. m_currentFootprint = aFootprint;
  88. if( !m_currentFootprint )
  89. return;
  90. m_currentFootprint->SetParent( m_dummyBoard.get() );
  91. // Ensure we are not using the high contrast mode to display the selected footprint
  92. KIGFX::PAINTER* painter = GetView()->GetPainter();
  93. auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
  94. settings->m_ContrastModeDisplay = HIGH_CONTRAST_MODE::NORMAL;
  95. GetView()->Add( m_currentFootprint.get() );
  96. GetView()->SetVisible( m_currentFootprint.get(), true );
  97. GetView()->Update( m_currentFootprint.get(), KIGFX::ALL );
  98. BOX2I bbox = m_currentFootprint->ViewBBox();
  99. bbox.Merge( m_currentFootprint->Value().ViewBBox() );
  100. bbox.Merge( m_currentFootprint->Reference().ViewBBox() );
  101. if( bbox.GetSize().x > 0 && bbox.GetSize().y > 0 )
  102. {
  103. // Autozoom
  104. GetView()->SetViewport( BOX2D( bbox.GetOrigin(), bbox.GetSize() ) );
  105. // Add a margin
  106. GetView()->SetScale( GetView()->GetScale() * 0.7 );
  107. Refresh();
  108. }
  109. }
  110. bool FOOTPRINT_PREVIEW_PANEL::DisplayFootprint( const LIB_ID& aFPID )
  111. {
  112. FP_LIB_TABLE* fptbl = Prj().PcbFootprintLibs();
  113. try
  114. {
  115. const FOOTPRINT* fp = fptbl->GetEnumeratedFootprint( aFPID.GetLibNickname(), aFPID.GetLibItemName() );
  116. if( fp )
  117. m_currentFootprint.reset( static_cast<FOOTPRINT*>( fp->Duplicate() ) );
  118. else
  119. m_currentFootprint.reset();
  120. }
  121. catch( ... )
  122. {
  123. m_currentFootprint.reset();
  124. }
  125. renderFootprint( m_currentFootprint );
  126. Refresh();
  127. return m_currentFootprint != nullptr;
  128. }
  129. wxWindow* FOOTPRINT_PREVIEW_PANEL::GetWindow()
  130. {
  131. return static_cast<wxWindow*>( this );
  132. }
  133. FOOTPRINT_PREVIEW_PANEL* FOOTPRINT_PREVIEW_PANEL::New( KIWAY* aKiway, wxWindow* aParent )
  134. {
  135. PCBNEW_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>();
  136. if( cfg->m_Window.grid.sizes.empty() )
  137. cfg->m_Window.grid.sizes = cfg->DefaultGridSizeList();
  138. // Currently values read from config file are not used because the user cannot
  139. // change this config
  140. //if( cfg->m_Window.zoom_factors.empty() )
  141. {
  142. cfg->m_Window.zoom_factors = { ZOOM_LIST_PCBNEW };
  143. }
  144. std::unique_ptr<KIGFX::GAL_DISPLAY_OPTIONS> gal_opts;
  145. gal_opts = std::make_unique<KIGFX::GAL_DISPLAY_OPTIONS>();
  146. gal_opts->ReadConfig( *Pgm().GetCommonSettings(), cfg->m_Window, aParent );
  147. auto canvasType = static_cast<EDA_DRAW_PANEL_GAL::GAL_TYPE>( cfg->m_Graphics.canvas_type );
  148. auto panel = new FOOTPRINT_PREVIEW_PANEL( aKiway, aParent, std::move( gal_opts ), canvasType );
  149. panel->UpdateColors();
  150. const GRID_SETTINGS& gridCfg = cfg->m_Window.grid;
  151. panel->GetGAL()->SetGridVisibility( gridCfg.show );
  152. //Bounds checking cannot include number of elements as an index!
  153. int gridIdx = std::max( 0, std::min( gridCfg.last_size_idx, (int) gridCfg.sizes.size() - 1 ) );
  154. double gridSize = EDA_UNIT_UTILS::UI::DoubleValueFromString( pcbIUScale, EDA_UNITS::MILS,
  155. gridCfg.sizes[ gridIdx ] );
  156. panel->GetGAL()->SetGridSize( VECTOR2D( gridSize, gridSize ) );
  157. return panel;
  158. }