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.

159 lines
4.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2023 Jon Evans <jon@craftyjon.com>
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  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 <dialogs/panel_plugin_settings.h>
  21. #include <api/api_server.h>
  22. #include <widgets/ui_common.h>
  23. #include <pgm_base.h>
  24. #include <python_manager.h>
  25. #include <settings/common_settings.h>
  26. #include <settings/settings_manager.h>
  27. PANEL_PLUGIN_SETTINGS::PANEL_PLUGIN_SETTINGS( wxWindow* aParent ) :
  28. PANEL_PLUGIN_SETTINGS_BASE( aParent ),
  29. m_pythonInterpreterValid( false )
  30. {
  31. wxFont helpFont = KIUI::GetInfoFont( this ).Italic();
  32. m_stPythonStatus->SetFont( helpFont );
  33. m_stApiStatus->SetFont( helpFont );
  34. }
  35. void PANEL_PLUGIN_SETTINGS::ResetPanel()
  36. {
  37. }
  38. bool PANEL_PLUGIN_SETTINGS::TransferDataToWindow()
  39. {
  40. SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
  41. COMMON_SETTINGS* settings = mgr.GetCommonSettings();
  42. m_cbEnableApi->SetValue( settings->m_Api.enable_server );
  43. m_pickerPythonInterpreter->SetFileName( settings->m_Api.python_interpreter );
  44. validatePythonInterpreter();
  45. updateApiStatusText();
  46. return true;
  47. }
  48. bool PANEL_PLUGIN_SETTINGS::TransferDataFromWindow()
  49. {
  50. SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
  51. COMMON_SETTINGS* settings = mgr.GetCommonSettings();
  52. wxString interpreter = m_pickerPythonInterpreter->GetTextCtrlValue();
  53. if( m_pythonInterpreterValid || interpreter.IsEmpty() )
  54. settings->m_Api.python_interpreter = interpreter;
  55. settings->m_Api.enable_server = m_cbEnableApi->GetValue();
  56. return true;
  57. }
  58. void PANEL_PLUGIN_SETTINGS::OnPythonInterpreterChanged( wxFileDirPickerEvent& event )
  59. {
  60. validatePythonInterpreter();
  61. }
  62. void PANEL_PLUGIN_SETTINGS::OnBtnDetectAutomaticallyClicked( wxCommandEvent& aEvent )
  63. {
  64. wxString interpreter = PYTHON_MANAGER::FindPythonInterpreter();
  65. if( !interpreter.IsEmpty() )
  66. {
  67. m_pickerPythonInterpreter->SetPath( interpreter );
  68. validatePythonInterpreter();
  69. }
  70. }
  71. void PANEL_PLUGIN_SETTINGS::OnEnableApiChecked( wxCommandEvent& aEvent )
  72. {
  73. validatePythonInterpreter();
  74. updateApiStatusText();
  75. }
  76. void PANEL_PLUGIN_SETTINGS::updateApiStatusText()
  77. {
  78. #ifdef KICAD_IPC_API
  79. if( m_cbEnableApi->GetValue() && Pgm().GetApiServer().Running() )
  80. {
  81. m_stApiStatus->SetLabel( wxString::Format( _( "Listening at %s" ),
  82. Pgm().GetApiServer().SocketPath() ) );
  83. }
  84. else
  85. {
  86. m_stApiStatus->SetLabel( wxEmptyString );
  87. }
  88. #else
  89. m_stApiStatus->SetLabel( _( "This installation of KiCad does not have API support enabled." ) );
  90. #endif
  91. }
  92. void PANEL_PLUGIN_SETTINGS::validatePythonInterpreter()
  93. {
  94. if( !m_cbEnableApi->GetValue() )
  95. {
  96. m_stPythonStatus->SetLabel( _( "KiCad API is not enabled; external Python plugins will "
  97. "not be available" ) );
  98. return;
  99. }
  100. m_pythonInterpreterValid = false;
  101. wxFileName pythonExe( m_pickerPythonInterpreter->GetTextCtrlValue() );
  102. if( !pythonExe.FileExists() )
  103. {
  104. m_stPythonStatus->SetLabel( _( "No valid Python interpreter chosen; external Python "
  105. "plugins will not be available" ) );
  106. return;
  107. }
  108. PYTHON_MANAGER manager( pythonExe.GetFullPath() );
  109. manager.Execute( { wxS( "--version" ) },
  110. [&]( int aRetCode, const wxString& aStdOut, const wxString& aStdErr )
  111. {
  112. wxString msg;
  113. if( aRetCode == 0 && aStdOut.Contains( wxS( "Python 3" ) ) )
  114. {
  115. msg = wxString::Format( _( "Found %s" ), aStdOut );
  116. m_pythonInterpreterValid = true;
  117. }
  118. else
  119. {
  120. msg = _( "Not a valid Python 3 interpreter" );
  121. }
  122. m_stPythonStatus->SetLabel( msg );
  123. Layout();
  124. },
  125. /* aEnv = */ nullptr,
  126. /* aSaveOutput = */ true );
  127. }