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.

220 lines
6.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014-2019 CERN
  5. * @author Maciej Suminski <maciej.suminski@cern.ch>
  6. * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <sch_draw_panel.h>
  26. #include <wx/debug.h>
  27. #include <wx/event.h>
  28. #include <wx/gdicmn.h>
  29. #include <wx/window.h>
  30. #include <wx/windowid.h>
  31. #include <memory>
  32. #include <stdexcept>
  33. #include <confirm.h>
  34. #include <eda_draw_frame.h>
  35. #include <gal/definitions.h>
  36. #include <gal/graphics_abstraction_layer.h>
  37. #include <layers_id_colors_and_visibility.h>
  38. #include <math/vector2d.h>
  39. #include <pgm_base.h>
  40. #include <settings/settings_manager.h>
  41. #include <view/view.h>
  42. #include <view/view_controls.h>
  43. #include <view/wx_view_controls.h>
  44. #include <sch_base_frame.h>
  45. #include <sch_painter.h>
  46. SCH_DRAW_PANEL::SCH_DRAW_PANEL( wxWindow* aParentWindow, wxWindowID aWindowId,
  47. const wxPoint& aPosition, const wxSize& aSize,
  48. KIGFX::GAL_DISPLAY_OPTIONS& aOptions, GAL_TYPE aGalType ) :
  49. EDA_DRAW_PANEL_GAL( aParentWindow, aWindowId, aPosition, aSize, aOptions, aGalType ),
  50. m_parent( aParentWindow )
  51. {
  52. m_currentCursor = wxCURSOR_ARROW;
  53. m_view = new KIGFX::SCH_VIEW( true, dynamic_cast<SCH_BASE_FRAME*>( aParentWindow ) );
  54. m_view->SetGAL( m_gal );
  55. m_gal->SetWorldUnitLength( SCH_WORLD_UNIT );
  56. m_painter.reset( new KIGFX::SCH_PAINTER( m_gal ) );
  57. COLOR_SETTINGS* cs = nullptr;
  58. if( auto frame = dynamic_cast<SCH_BASE_FRAME*>( aParentWindow ) )
  59. cs = frame->GetColorSettings();
  60. else
  61. cs = Pgm().GetSettingsManager().GetColorSettings();
  62. wxASSERT( cs );
  63. m_painter->GetSettings()->LoadColors( cs );
  64. m_view->SetPainter( m_painter.get() );
  65. m_view->SetScaleLimits( 1000.0, 0.0001 ); // This fixes the zoom in and zoom out limits
  66. m_view->SetMirror( false, false );
  67. // Early initialization of the canvas background color,
  68. // before any OnPaint event is fired for the canvas using a wrong bg color
  69. auto settings = m_painter->GetSettings();
  70. m_gal->SetClearColor( settings->GetBackgroundColor() );
  71. setDefaultLayerOrder();
  72. setDefaultLayerDeps();
  73. GetView()->UpdateAllLayersOrder();
  74. // View controls is the first in the event handler chain, so the Tool Framework operates
  75. // on updated viewport data.
  76. m_viewControls = new KIGFX::WX_VIEW_CONTROLS( m_view, this );
  77. m_viewControls->SetSnapping( true );
  78. SetEvtHandlerEnabled( true );
  79. SetFocus();
  80. Show( true );
  81. Raise();
  82. StartDrawing();
  83. }
  84. SCH_DRAW_PANEL::~SCH_DRAW_PANEL()
  85. {
  86. }
  87. void SCH_DRAW_PANEL::DisplayComponent( const LIB_PART* aComponent )
  88. {
  89. GetView()->Clear();
  90. GetView()->DisplayComponent( const_cast<LIB_PART*>(aComponent) );
  91. }
  92. void SCH_DRAW_PANEL::DisplaySheet( const SCH_SCREEN *aScreen )
  93. {
  94. GetView()->Clear();
  95. if( aScreen )
  96. GetView()->DisplaySheet( const_cast<SCH_SCREEN*>( aScreen ) );
  97. }
  98. void SCH_DRAW_PANEL::setDefaultLayerOrder()
  99. {
  100. for( LAYER_NUM i = 0; (unsigned) i < sizeof( SCH_LAYER_ORDER ) / sizeof( LAYER_NUM ); ++i )
  101. {
  102. LAYER_NUM layer = SCH_LAYER_ORDER[i];
  103. wxASSERT( layer < KIGFX::VIEW::VIEW_MAX_LAYERS );
  104. m_view->SetLayerOrder( layer, i );
  105. }
  106. }
  107. bool SCH_DRAW_PANEL::SwitchBackend( GAL_TYPE aGalType )
  108. {
  109. VECTOR2D grid_size = m_gal->GetGridSize();
  110. bool rv = EDA_DRAW_PANEL_GAL::SwitchBackend( aGalType );
  111. setDefaultLayerDeps();
  112. m_gal->SetWorldUnitLength( SCH_WORLD_UNIT );
  113. // Keep grid size and grid visibility:
  114. m_gal->SetGridSize( grid_size );
  115. SCH_BASE_FRAME* frame = dynamic_cast<SCH_BASE_FRAME*>( GetParent() );
  116. if( frame )
  117. m_gal->SetGridVisibility( frame->IsGridVisible() );
  118. Refresh();
  119. return rv;
  120. }
  121. void SCH_DRAW_PANEL::setDefaultLayerDeps()
  122. {
  123. // caching makes no sense for Cairo and other software renderers
  124. auto target = m_backend == GAL_TYPE_OPENGL ? KIGFX::TARGET_CACHED : KIGFX::TARGET_NONCACHED;
  125. for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; i++ )
  126. m_view->SetLayerTarget( i, target );
  127. // Bitmaps are draw on a non cached GAL layer:
  128. m_view->SetLayerTarget( LAYER_DRAW_BITMAPS, KIGFX::TARGET_NONCACHED );
  129. // Some draw layers need specific settings
  130. m_view->SetLayerTarget( LAYER_GP_OVERLAY, KIGFX::TARGET_OVERLAY );
  131. m_view->SetLayerDisplayOnly( LAYER_GP_OVERLAY ) ;
  132. m_view->SetLayerTarget( LAYER_SELECT_OVERLAY, KIGFX::TARGET_OVERLAY );
  133. m_view->SetLayerDisplayOnly( LAYER_SELECT_OVERLAY ) ;
  134. m_view->SetLayerTarget( LAYER_WORKSHEET, KIGFX::TARGET_NONCACHED );
  135. m_view->SetLayerDisplayOnly( LAYER_WORKSHEET ) ;
  136. // m_view->SetLayerTarget( LAYER_SELECTION_SHADOWS, KIGFX::TARGET_NONCACHED );
  137. // m_view->SetLayerDisplayOnly( LAYER_SELECTION_SHADOWS ) ;
  138. }
  139. KIGFX::SCH_VIEW* SCH_DRAW_PANEL::GetView() const
  140. {
  141. return static_cast<KIGFX::SCH_VIEW*>( m_view );
  142. }
  143. void SCH_DRAW_PANEL::OnShow()
  144. {
  145. SCH_BASE_FRAME* frame = dynamic_cast<SCH_BASE_FRAME*>( GetParent() );
  146. try
  147. {
  148. // Check if the current rendering backend can be properly initialized
  149. m_view->UpdateItems();
  150. }
  151. catch( const std::runtime_error& e )
  152. {
  153. // Fallback to software renderer
  154. DisplayInfoMessage( frame, e.what() );
  155. SwitchBackend( GAL_TYPE_CAIRO );
  156. if( frame )
  157. frame->ActivateGalCanvas();
  158. }
  159. }
  160. void SCH_DRAW_PANEL::onPaint( wxPaintEvent& aEvent )
  161. {
  162. // The first wxPaintEvent can be fired at startup before the GAL engine is fully initialized
  163. // (depending on platforms). Do nothing in this case
  164. if( !m_gal->IsInitialized() || !m_gal->IsVisible() )
  165. return;
  166. EDA_DRAW_PANEL_GAL::onPaint( aEvent );
  167. }