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.

219 lines
6.4 KiB

4 years ago
4 years ago
  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-2023 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 <layer_ids.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. #include <zoom_defines.h>
  47. SCH_DRAW_PANEL::SCH_DRAW_PANEL( wxWindow* aParentWindow, wxWindowID aWindowId,
  48. const wxPoint& aPosition, const wxSize& aSize,
  49. KIGFX::GAL_DISPLAY_OPTIONS& aOptions, GAL_TYPE aGalType )
  50. : EDA_DRAW_PANEL_GAL( aParentWindow, aWindowId, aPosition, aSize, aOptions, aGalType )
  51. {
  52. m_view = new KIGFX::SCH_VIEW( true, dynamic_cast<SCH_BASE_FRAME*>( GetParentEDAFrame() ) );
  53. m_view->SetGAL( m_gal );
  54. m_gal->SetWorldUnitLength( SCH_WORLD_UNIT );
  55. m_painter.reset( new KIGFX::SCH_PAINTER( m_gal ) );
  56. COLOR_SETTINGS* cs = nullptr;
  57. if( auto frame = dynamic_cast<SCH_BASE_FRAME*>( GetParentEDAFrame() ) )
  58. cs = frame->GetColorSettings();
  59. else
  60. cs = Pgm().GetSettingsManager().GetColorSettings();
  61. wxASSERT( cs );
  62. m_painter->GetSettings()->LoadColors( cs );
  63. m_view->SetPainter( m_painter.get() );
  64. // This fixes the zoom in and zoom out limits:
  65. m_view->SetScaleLimits( ZOOM_MAX_LIMIT_EESCHEMA, ZOOM_MIN_LIMIT_EESCHEMA );
  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. SetEvtHandlerEnabled( true );
  78. SetFocus();
  79. Show( true );
  80. Raise();
  81. StartDrawing();
  82. }
  83. SCH_DRAW_PANEL::~SCH_DRAW_PANEL()
  84. {
  85. }
  86. void SCH_DRAW_PANEL::DisplaySymbol( LIB_SYMBOL* aSymbol )
  87. {
  88. GetView()->DisplaySymbol( aSymbol );
  89. }
  90. void SCH_DRAW_PANEL::DisplaySheet( SCH_SCREEN *aScreen )
  91. {
  92. GetView()->Clear();
  93. if( aScreen )
  94. GetView()->DisplaySheet( aScreen );
  95. else
  96. GetView()->Cleanup();
  97. }
  98. void SCH_DRAW_PANEL::setDefaultLayerOrder()
  99. {
  100. for( int i = 0; (unsigned) i < sizeof( SCH_LAYER_ORDER ) / sizeof( int ); ++i )
  101. {
  102. int 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. bool rv = EDA_DRAW_PANEL_GAL::SwitchBackend( aGalType );
  110. setDefaultLayerDeps();
  111. m_gal->SetWorldUnitLength( SCH_WORLD_UNIT );
  112. Refresh();
  113. return rv;
  114. }
  115. void SCH_DRAW_PANEL::setDefaultLayerDeps()
  116. {
  117. // caching makes no sense for Cairo and other software renderers
  118. auto target = m_backend == GAL_TYPE_OPENGL ? KIGFX::TARGET_CACHED : KIGFX::TARGET_NONCACHED;
  119. for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; i++ )
  120. m_view->SetLayerTarget( i, target );
  121. // Bitmaps are draw on a non cached GAL layer:
  122. m_view->SetLayerTarget( LAYER_DRAW_BITMAPS, KIGFX::TARGET_NONCACHED );
  123. // Some draw layers need specific settings
  124. m_view->SetLayerTarget( LAYER_GP_OVERLAY, KIGFX::TARGET_OVERLAY );
  125. m_view->SetLayerDisplayOnly( LAYER_GP_OVERLAY ) ;
  126. m_view->SetLayerTarget( LAYER_SELECT_OVERLAY, KIGFX::TARGET_OVERLAY );
  127. m_view->SetLayerDisplayOnly( LAYER_SELECT_OVERLAY ) ;
  128. m_view->SetLayerTarget( LAYER_DRAWINGSHEET, KIGFX::TARGET_NONCACHED );
  129. m_view->SetLayerDisplayOnly( LAYER_DRAWINGSHEET ) ;
  130. m_view->SetLayerTarget( LAYER_OP_VOLTAGES, KIGFX::TARGET_OVERLAY );
  131. m_view->SetLayerDisplayOnly( LAYER_OP_VOLTAGES );
  132. m_view->SetLayerTarget( LAYER_OP_CURRENTS, KIGFX::TARGET_OVERLAY );
  133. m_view->SetLayerDisplayOnly( LAYER_OP_CURRENTS );
  134. m_view->SetLayerTarget( LAYER_SELECTION_SHADOWS, KIGFX::TARGET_OVERLAY );
  135. m_view->SetLayerDisplayOnly( LAYER_SELECTION_SHADOWS ) ;
  136. }
  137. KIGFX::SCH_VIEW* SCH_DRAW_PANEL::GetView() const
  138. {
  139. return static_cast<KIGFX::SCH_VIEW*>( m_view );
  140. }
  141. void SCH_DRAW_PANEL::OnShow()
  142. {
  143. SCH_BASE_FRAME* frame = dynamic_cast<SCH_BASE_FRAME*>( GetParentEDAFrame() );
  144. try
  145. {
  146. // Check if the current rendering backend can be properly initialized
  147. m_view->UpdateItems();
  148. }
  149. catch( const std::runtime_error& e )
  150. {
  151. DisplayInfoMessage( frame, e.what() );
  152. // Use fallback if one is available
  153. if( GAL_FALLBACK != m_backend )
  154. {
  155. SwitchBackend( GAL_FALLBACK );
  156. if( frame )
  157. frame->ActivateGalCanvas();
  158. }
  159. }
  160. }
  161. void SCH_DRAW_PANEL::onPaint( wxPaintEvent& aEvent )
  162. {
  163. // The first wxPaintEvent can be fired at startup before the GAL engine is fully initialized
  164. // (depending on platforms). Do nothing in this case
  165. if( !m_gal->IsInitialized() || !m_gal->IsVisible() )
  166. return;
  167. EDA_DRAW_PANEL_GAL::onPaint( aEvent );
  168. }