2023-01-29 18:06:05 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2023 Jon Evans <jon@craftyjon.com>
|
|
|
|
* Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-01-18 03:21:28 +00:00
|
|
|
#include <dialogs/panel_plugin_settings.h>
|
|
|
|
#include <api/api_server.h>
|
2023-01-29 18:06:05 +00:00
|
|
|
#include <widgets/ui_common.h>
|
|
|
|
#include <pgm_base.h>
|
|
|
|
#include <python_manager.h>
|
|
|
|
#include <settings/common_settings.h>
|
|
|
|
#include <settings/settings_manager.h>
|
|
|
|
|
|
|
|
|
2024-01-18 03:21:28 +00:00
|
|
|
PANEL_PLUGIN_SETTINGS::PANEL_PLUGIN_SETTINGS( wxWindow* aParent ) :
|
|
|
|
PANEL_PLUGIN_SETTINGS_BASE( aParent )
|
2023-01-29 18:06:05 +00:00
|
|
|
{
|
|
|
|
wxFont helpFont = KIUI::GetInfoFont( this ).Italic();
|
|
|
|
m_stPythonStatus->SetFont( helpFont );
|
2024-01-18 03:21:28 +00:00
|
|
|
m_stApiStatus->SetFont( helpFont );
|
2023-01-29 18:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-18 03:21:28 +00:00
|
|
|
void PANEL_PLUGIN_SETTINGS::ResetPanel()
|
2023-01-29 18:06:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-18 03:21:28 +00:00
|
|
|
bool PANEL_PLUGIN_SETTINGS::TransferDataToWindow()
|
2023-01-29 18:06:05 +00:00
|
|
|
{
|
|
|
|
SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
|
2024-01-18 03:21:28 +00:00
|
|
|
COMMON_SETTINGS* settings = mgr.GetCommonSettings();
|
2023-01-29 18:06:05 +00:00
|
|
|
|
2024-01-18 03:21:28 +00:00
|
|
|
m_cbEnableApi->SetValue( settings->m_Api.enable_server );
|
|
|
|
m_pickerPythonInterpreter->SetFileName( settings->m_Api.python_interpreter );
|
|
|
|
validatePythonInterpreter();
|
|
|
|
updateApiStatusText();
|
2023-01-29 18:06:05 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-18 03:21:28 +00:00
|
|
|
bool PANEL_PLUGIN_SETTINGS::TransferDataFromWindow()
|
2023-01-29 18:06:05 +00:00
|
|
|
{
|
|
|
|
SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
|
2024-01-18 03:21:28 +00:00
|
|
|
COMMON_SETTINGS* settings = mgr.GetCommonSettings();
|
2024-01-14 04:14:02 +00:00
|
|
|
wxString interpreter = m_pickerPythonInterpreter->GetTextCtrlValue();
|
2023-01-29 18:06:05 +00:00
|
|
|
|
2024-01-18 03:21:28 +00:00
|
|
|
if( m_pythonInterpreterValid || interpreter.IsEmpty() )
|
|
|
|
settings->m_Api.python_interpreter = interpreter;
|
|
|
|
|
|
|
|
settings->m_Api.enable_server = m_cbEnableApi->GetValue();
|
2023-01-29 18:06:05 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-18 03:21:28 +00:00
|
|
|
void PANEL_PLUGIN_SETTINGS::OnPythonInterpreterChanged( wxFileDirPickerEvent& event )
|
2023-01-29 18:06:05 +00:00
|
|
|
{
|
2024-01-18 03:21:28 +00:00
|
|
|
validatePythonInterpreter();
|
2023-01-29 18:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-18 03:21:28 +00:00
|
|
|
void PANEL_PLUGIN_SETTINGS::OnBtnDetectAutomaticallyClicked( wxCommandEvent& aEvent )
|
2023-01-29 18:06:05 +00:00
|
|
|
{
|
2024-01-18 03:21:28 +00:00
|
|
|
wxString interpreter = PYTHON_MANAGER::FindPythonInterpreter();
|
2024-01-14 04:14:02 +00:00
|
|
|
|
2024-01-18 03:21:28 +00:00
|
|
|
if( !interpreter.IsEmpty() )
|
2024-01-14 04:14:02 +00:00
|
|
|
{
|
2024-01-18 03:21:28 +00:00
|
|
|
m_pickerPythonInterpreter->SetPath( interpreter );
|
|
|
|
validatePythonInterpreter();
|
2024-01-14 04:14:02 +00:00
|
|
|
}
|
2023-01-29 18:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-18 03:21:28 +00:00
|
|
|
void PANEL_PLUGIN_SETTINGS::OnEnableApiChecked( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
validatePythonInterpreter();
|
|
|
|
updateApiStatusText();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PANEL_PLUGIN_SETTINGS::updateApiStatusText()
|
2023-01-29 18:06:05 +00:00
|
|
|
{
|
2024-01-20 23:35:29 +00:00
|
|
|
#ifdef KICAD_IPC_API
|
2024-01-18 03:21:28 +00:00
|
|
|
if( m_cbEnableApi->GetValue() && Pgm().GetApiServer().Running() )
|
|
|
|
{
|
|
|
|
m_stApiStatus->SetLabel( wxString::Format( _( "Listening at %s" ),
|
|
|
|
Pgm().GetApiServer().SocketPath() ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_stApiStatus->SetLabel( wxEmptyString );
|
|
|
|
}
|
2024-01-20 23:35:29 +00:00
|
|
|
#else
|
|
|
|
m_stApiStatus->SetLabel( _( "This installation of KiCad does not have API support enabled." ) );
|
|
|
|
#endif
|
2024-01-18 03:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PANEL_PLUGIN_SETTINGS::validatePythonInterpreter()
|
|
|
|
{
|
|
|
|
if( !m_cbEnableApi->GetValue() )
|
|
|
|
{
|
|
|
|
m_stPythonStatus->SetLabel( _( "KiCad API is not enabled; external Python plugins will "
|
|
|
|
"not be available" ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pythonInterpreterValid = false;
|
2023-01-29 18:06:05 +00:00
|
|
|
|
|
|
|
wxFileName pythonExe( m_pickerPythonInterpreter->GetTextCtrlValue() );
|
|
|
|
|
|
|
|
if( !pythonExe.FileExists() )
|
|
|
|
{
|
|
|
|
m_stPythonStatus->SetLabel( _( "No valid Python interpreter chosen; external Python "
|
|
|
|
"plugins will not be available" ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PYTHON_MANAGER manager( pythonExe.GetFullPath() );
|
|
|
|
|
|
|
|
manager.Execute( wxS( "--version" ),
|
2024-01-14 04:14:02 +00:00
|
|
|
[&]( int aRetCode, const wxString& aStdOut, const wxString& aStdErr )
|
2023-01-29 18:06:05 +00:00
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
|
|
|
|
if( aRetCode == 0 && aStdOut.Contains( wxS( "Python 3" ) ) )
|
|
|
|
{
|
|
|
|
msg = wxString::Format( _( "Found %s" ), aStdOut );
|
2024-01-18 03:21:28 +00:00
|
|
|
m_pythonInterpreterValid = true;
|
2023-01-29 18:06:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg = _( "Not a valid Python 3 interpreter" );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_stPythonStatus->SetLabel( msg );
|
|
|
|
} );
|
|
|
|
}
|