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.

200 lines
6.2 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 <board.h>
  25. #include <footprint.h>
  26. #include <eda_draw_frame.h>
  27. #include <footprint_preview_panel.h>
  28. #include <fp_lib_table.h>
  29. #include <kiway.h>
  30. #include <math/box2.h>
  31. #include <pcb_painter.h>
  32. #include <pcb_draw_panel_gal.h>
  33. #include <pcb_edit_frame.h>
  34. #include <pgm_base.h>
  35. #include <settings/settings_manager.h>
  36. #include <view/view.h>
  37. #include <wx/stattext.h>
  38. #include <zoom_defines.h>
  39. FOOTPRINT_PREVIEW_PANEL::FOOTPRINT_PREVIEW_PANEL( KIWAY* aKiway, wxWindow* aParent,
  40. std::unique_ptr<KIGFX::GAL_DISPLAY_OPTIONS> aOpts,
  41. GAL_TYPE aGalType ) :
  42. PCB_DRAW_PANEL_GAL( aParent, -1, wxPoint( 0, 0 ), wxSize( 200, 200 ), *aOpts, aGalType ),
  43. KIWAY_HOLDER( aKiway, KIWAY_HOLDER::PANEL ),
  44. m_displayOptions( std::move( aOpts ) ),
  45. m_currentFootprint( nullptr )
  46. {
  47. SetStealsFocus( false );
  48. ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
  49. EnableScrolling( false, false ); // otherwise Zoom Auto disables GAL canvas
  50. m_dummyBoard = std::make_unique<BOARD>();
  51. UpdateColors();
  52. SyncLayersVisibility( m_dummyBoard.get() );
  53. Raise();
  54. Show( true );
  55. StartDrawing();
  56. }
  57. FOOTPRINT_PREVIEW_PANEL::~FOOTPRINT_PREVIEW_PANEL( )
  58. {
  59. if( m_currentFootprint )
  60. {
  61. GetView()->Remove( m_currentFootprint.get() );
  62. GetView()->Clear();
  63. m_currentFootprint->SetParent( nullptr );
  64. }
  65. }
  66. const COLOR4D& FOOTPRINT_PREVIEW_PANEL::GetBackgroundColor()
  67. {
  68. KIGFX::PAINTER* painter = GetView()->GetPainter();
  69. auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
  70. return settings->GetBackgroundColor();
  71. }
  72. const COLOR4D& FOOTPRINT_PREVIEW_PANEL::GetForegroundColor()
  73. {
  74. KIGFX::PAINTER* painter = GetView()->GetPainter();
  75. auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
  76. return settings->GetCursorColor();
  77. }
  78. void FOOTPRINT_PREVIEW_PANEL::renderFootprint( std::shared_ptr<FOOTPRINT> aFootprint )
  79. {
  80. if( m_currentFootprint )
  81. {
  82. GetView()->Remove( m_currentFootprint.get() );
  83. GetView()->Clear();
  84. m_currentFootprint->SetParent( nullptr );
  85. }
  86. m_currentFootprint = aFootprint;
  87. if( !m_currentFootprint )
  88. return;
  89. m_currentFootprint->SetParent( m_dummyBoard.get() );
  90. // Ensure we are not using the high contrast mode to display the selected footprint
  91. KIGFX::PAINTER* painter = GetView()->GetPainter();
  92. auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
  93. settings->m_ContrastModeDisplay = HIGH_CONTRAST_MODE::NORMAL;
  94. GetView()->Add( m_currentFootprint.get() );
  95. GetView()->SetVisible( m_currentFootprint.get(), true );
  96. GetView()->Update( m_currentFootprint.get(), KIGFX::ALL );
  97. BOX2I bbox = m_currentFootprint->ViewBBox();
  98. bbox.Merge( m_currentFootprint->Value().ViewBBox() );
  99. bbox.Merge( m_currentFootprint->Reference().ViewBBox() );
  100. if( bbox.GetSize().x > 0 && bbox.GetSize().y > 0 )
  101. {
  102. // Autozoom
  103. GetView()->SetViewport( BOX2D( bbox.GetOrigin(), bbox.GetSize() ) );
  104. // Add a margin
  105. GetView()->SetScale( GetView()->GetScale() * 0.7 );
  106. Refresh();
  107. }
  108. }
  109. bool FOOTPRINT_PREVIEW_PANEL::DisplayFootprint( const LIB_ID& aFPID )
  110. {
  111. FP_LIB_TABLE* fptbl = Prj().PcbFootprintLibs();
  112. try
  113. {
  114. const FOOTPRINT* fp = fptbl->GetEnumeratedFootprint( aFPID.GetLibNickname(), aFPID.GetLibItemName() );
  115. if( fp )
  116. m_currentFootprint.reset( static_cast<FOOTPRINT*>( fp->Duplicate() ) );
  117. else
  118. m_currentFootprint.reset();
  119. }
  120. catch( ... )
  121. {
  122. m_currentFootprint.reset();
  123. }
  124. renderFootprint( m_currentFootprint );
  125. Refresh();
  126. return m_currentFootprint != nullptr;
  127. }
  128. wxWindow* FOOTPRINT_PREVIEW_PANEL::GetWindow()
  129. {
  130. return static_cast<wxWindow*>( this );
  131. }
  132. FOOTPRINT_PREVIEW_PANEL* FOOTPRINT_PREVIEW_PANEL::New( KIWAY* aKiway, wxWindow* aParent )
  133. {
  134. PCBNEW_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>();
  135. if( cfg->m_Window.grid.sizes.empty() )
  136. cfg->m_Window.grid.sizes = cfg->DefaultGridSizeList();
  137. // Currently values read from config file are not used because the user cannot
  138. // change this config
  139. //if( cfg->m_Window.zoom_factors.empty() )
  140. {
  141. cfg->m_Window.zoom_factors = { ZOOM_LIST_PCBNEW };
  142. }
  143. std::unique_ptr<KIGFX::GAL_DISPLAY_OPTIONS> gal_opts;
  144. gal_opts = std::make_unique<KIGFX::GAL_DISPLAY_OPTIONS>();
  145. gal_opts->ReadConfig( *Pgm().GetCommonSettings(), cfg->m_Window, aParent );
  146. auto canvasType = static_cast<EDA_DRAW_PANEL_GAL::GAL_TYPE>( cfg->m_Graphics.canvas_type );
  147. auto panel = new FOOTPRINT_PREVIEW_PANEL( aKiway, aParent, std::move( gal_opts ), canvasType );
  148. panel->UpdateColors();
  149. const GRID_SETTINGS& gridCfg = cfg->m_Window.grid;
  150. panel->GetGAL()->SetGridVisibility( gridCfg.show );
  151. //Bounds checking cannot include number of elements as an index!
  152. int gridIdx = std::max( 0, std::min( gridCfg.last_size_idx, (int) gridCfg.sizes.size() - 1 ) );
  153. int gridSize = (int) ValueFromString( EDA_UNITS::MILS, gridCfg.sizes[ gridIdx ] );
  154. panel->GetGAL()->SetGridSize( VECTOR2D( gridSize, gridSize ) );
  155. return panel;
  156. }