ADDED: library tree context menu option to open sym/fp library files from the defined text editor.

Short description:

Works for Symbol and Footprint Editor behind an advanced config option.
For Symbol Editor it is shown for a single item selection (library or symbol).
For Footprint Editor it is shown for a footprint selection.
(fp editor allows a single tree item selection only).
Option stays hidden if current frame has been modified.

Also small fix(?) for similar action to the project manager.
(Call for the Execution has moved inside the file loop.)

Fixes: https://gitlab.com/kicad/code/kicad/-/issues/15736
This commit is contained in:
aris-kimi 2024-02-29 19:01:59 +00:00 committed by Seth Hillbrand
parent 15e963bb32
commit 24529e5242
13 changed files with 162 additions and 12 deletions

View File

@ -100,6 +100,7 @@ static const wxChar V3DRT_BevelExtentFactor[] = wxT( "V3DRT_BevelExtentFactor" )
static const wxChar UseClipper2[] = wxT( "UseClipper2" );
static const wxChar EnableGenerators[] = wxT( "EnableGenerators" );
static const wxChar EnableGit[] = wxT( "EnableGit" );
static const wxChar EnableLibWithText[] = wxT( "EnableLibWithText" );
static const wxChar EnableEeschemaPrintCairo[] = wxT( "EnableEeschemaPrintCairo" );
static const wxChar DisambiguationTime[] = wxT( "DisambiguationTime" );
static const wxChar PcbSelectionVisibilityRatio[] = wxT( "PcbSelectionVisibilityRatio" );
@ -230,6 +231,7 @@ ADVANCED_CFG::ADVANCED_CFG()
m_ShowRepairSchematic = false;
m_EnableGenerators = false;
m_EnableGit = false;
m_EnableLibWithText = false;
m_EnableEeschemaPrintCairo = true;
@ -432,6 +434,9 @@ void ADVANCED_CFG::loadSettings( wxConfigBase& aCfg )
configParams.push_back( new PARAM_CFG_BOOL( true, AC_KEYS::EnableGit,
&m_EnableGit, m_EnableGit ) );
configParams.push_back( new PARAM_CFG_BOOL( true, AC_KEYS::EnableLibWithText,
&m_EnableLibWithText, m_EnableLibWithText ) );
configParams.push_back( new PARAM_CFG_BOOL( true, AC_KEYS::EnableEeschemaPrintCairo,
&m_EnableEeschemaPrintCairo,
m_EnableEeschemaPrintCairo ) );

View File

@ -29,7 +29,6 @@
*/
#include <wx/mimetype.h>
#include <wx/filename.h>
#include <wx/dir.h>
#include <pgm_base.h>
@ -137,7 +136,8 @@ wxString FindKicadFile( const wxString& shortname )
}
int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProcess *aCallback )
int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProcess* aCallback,
bool aFileForKicad )
{
wxString fullEditorName;
std::vector<wxString> params;
@ -206,10 +206,18 @@ int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProce
pushParam();
fullEditorName = FindKicadFile( params[0] );
if( aFileForKicad )
fullEditorName = FindKicadFile( params[0] );
else
fullEditorName = params[0];
params.erase( params.begin() );
#else
fullEditorName = FindKicadFile( aEditorName );
if( aFileForKicad )
fullEditorName = FindKicadFile( aEditorName );
else
fullEditorName = aEditorName;
#endif
if( wxFileExists( fullEditorName ) )

View File

@ -243,6 +243,13 @@ TOOL_ACTION EE_ACTIONS::exportSymbol( TOOL_ACTION_ARGS()
.Tooltip( _( "Export a symbol to a new library file" ) )
.Icon( BITMAPS::export_part ) );
TOOL_ACTION EE_ACTIONS::openWithTextEditor( TOOL_ACTION_ARGS()
.Name( "eeschema.SymbolLibraryControl.openWithTextEditor" )
.Scope( AS_GLOBAL )
.FriendlyName( _( "Edit in a Text Editor..." ) )
.Tooltip( _( "Open a library file with a text editor" ) )
.Icon( BITMAPS::editor ) );
TOOL_ACTION EE_ACTIONS::updateSymbolFields( TOOL_ACTION_ARGS()
.Name( "eeschema.SymbolLibraryControl.updateSymbolFields" )
.Scope( AS_GLOBAL )

View File

@ -209,6 +209,7 @@ public:
static TOOL_ACTION exportSymbol;
static TOOL_ACTION updateSymbolFields;
static TOOL_ACTION setUnitDisplayName;
static TOOL_ACTION openWithTextEditor;
// Hierarchy navigation
static TOOL_ACTION changeSheet;

View File

@ -22,6 +22,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <advanced_config.h>
#include <kiway.h>
#include <pgm_base.h>
#include <sch_painter.h>
@ -35,6 +36,7 @@
#include <wildcards_and_files_ext.h>
#include <bitmaps/bitmap_types.h>
#include <confirm.h>
#include <gestfich.h> // To open with a text editor
#include <wx/filedlg.h>
#include "wx/generic/textdlgg.h"
#include "string_utils.h"
@ -96,6 +98,15 @@ bool SYMBOL_EDITOR_CONTROL::Init()
{
return editFrame->GetTreeSelectionCount() > 1;
};
auto canOpenWithTextEditor =
[ editFrame ]( const SELECTION& aSel )
{
// The option is shown if the lib has no current edits
LIB_SYMBOL_LIBRARY_MANAGER& libMgr = editFrame->GetLibManager();
wxString libName = editFrame->GetTargetLibId().GetLibNickname();
bool ret = !libMgr.IsLibraryModified( libName );
return ret;
};
ctxMenu.AddItem( ACTIONS::pinLibrary, unpinnedLibSelectedCondition );
ctxMenu.AddItem( ACTIONS::unpinLibrary, pinnedLibSelectedCondition );
@ -123,7 +134,16 @@ bool SYMBOL_EDITOR_CONTROL::Init()
ctxMenu.AddItem( EE_ACTIONS::exportSymbol, symbolSelectedCondition );
// If we've got nothing else to show, at least show a hide tree option
ctxMenu.AddSeparator();
ctxMenu.AddItem( EE_ACTIONS::hideSymbolTree, !libInferredCondition );
if( ADVANCED_CFG::GetCfg().m_EnableLibWithText )
{
ctxMenu.AddSeparator();
ctxMenu.AddItem( EE_ACTIONS::openWithTextEditor,
canOpenWithTextEditor
&& ( symbolSelectedCondition || libSelectedCondition ) );
}
}
return true;
@ -246,6 +266,36 @@ int SYMBOL_EDITOR_CONTROL::ExportSymbol( const TOOL_EVENT& aEvent )
}
int SYMBOL_EDITOR_CONTROL::OpenWithTextEditor( const TOOL_EVENT& aEvent )
{
if( m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR ) )
{
wxString fullEditorName = Pgm().GetTextEditor();
if( fullEditorName.IsEmpty() )
{
wxMessageBox( _( "No text editor selected in KiCad. Please choose one." ) );
return 0;
}
SYMBOL_EDIT_FRAME* editFrame = static_cast<SYMBOL_EDIT_FRAME*>( m_frame );
LIB_SYMBOL_LIBRARY_MANAGER& libMgr = editFrame->GetLibManager();
LIB_ID libId = editFrame->GetTreeLIBID();
wxString libName = libId.GetLibNickname();
wxString tempFName = libMgr.GetLibrary( libName )->GetFullURI( true ).wc_str();
if( !tempFName.IsEmpty() )
{
ExecuteFile( fullEditorName, tempFName, nullptr, false );
}
}
return 0;
}
int SYMBOL_EDITOR_CONTROL::CutCopyDelete( const TOOL_EVENT& aEvt )
{
if( m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR ) )
@ -760,6 +810,7 @@ void SYMBOL_EDITOR_CONTROL::setTransitions()
Go( &SYMBOL_EDITOR_CONTROL::CutCopyDelete, EE_ACTIONS::copySymbol.MakeEvent() );
Go( &SYMBOL_EDITOR_CONTROL::DuplicateSymbol, EE_ACTIONS::pasteSymbol.MakeEvent() );
Go( &SYMBOL_EDITOR_CONTROL::ExportSymbol, EE_ACTIONS::exportSymbol.MakeEvent() );
Go( &SYMBOL_EDITOR_CONTROL::OpenWithTextEditor, EE_ACTIONS::openWithTextEditor.MakeEvent() );
Go( &SYMBOL_EDITOR_CONTROL::ExportView, EE_ACTIONS::exportSymbolView.MakeEvent() );
Go( &SYMBOL_EDITOR_CONTROL::ExportSymbolAsSVG, EE_ACTIONS::exportSymbolAsSVG.MakeEvent() );
Go( &SYMBOL_EDITOR_CONTROL::AddSymbolToSchematic, EE_ACTIONS::addSymbolToSchematic.MakeEvent() );

View File

@ -57,6 +57,7 @@ public:
int DuplicateSymbol( const TOOL_EVENT& aEvent );
int RenameSymbol( const TOOL_EVENT& newName );
int ExportSymbol( const TOOL_EVENT& aEvent );
int OpenWithTextEditor( const TOOL_EVENT& aEvent );
int ExportView( const TOOL_EVENT& aEvent );
int ExportSymbolAsSVG( const TOOL_EVENT& aEvent );
int AddSymbolToSchematic( const TOOL_EVENT& aEvent );

View File

@ -475,6 +475,15 @@ public:
*/
bool m_EnableGit;
/**
* Enable option to load lib files with text editor.
*
* Setting name: "EnableLibWithText"
* Valid values: 0 or 1
* Default value: 0
*/
bool m_EnableLibWithText;
/**
* Enable Eeschema printing using Cairo.
*

View File

@ -57,9 +57,15 @@ void KiCopyFile( const wxString& aSrcPath, const wxString& aDestPath, wxString&
/**
* Call the executable file \a aEditorName with the parameter \a aFileName.
* @param aEditorName is the full filename for the binary.
* @param aFileName is the full filename of the file to open.
* @param aCallback a wxProcess* for the call.
* @param aFileForKicad a boolean to flag if aFileName runs with a KiCad binary.
* In this case aFileName is a shortname and FindKicadFile() is called to return the path.
* In the other case, aFileName is a full file name (passed prefixed with the path).
*/
int ExecuteFile( const wxString& aEditorName, const wxString& aFileName = wxEmptyString,
wxProcess* aCallback = nullptr );
wxProcess* aCallback = nullptr, bool aFileForKicad = true );
/**
* Add un " to the start and the end of string (if not already done).

View File

@ -1004,19 +1004,16 @@ void PROJECT_TREE_PANE::onOpenSelectedFileWithTextEditor( wxCommandEvent& event
}
std::vector<PROJECT_TREE_ITEM*> tree_data = GetSelectedData();
wxString files;
for( PROJECT_TREE_ITEM* item_data : tree_data )
{
wxString fullFileName = item_data->GetFileName();
if( !files.IsEmpty() )
files += " ";
files += fullFileName;
if( !fullFileName.IsEmpty() )
{
ExecuteFile( editorname, fullFileName.wc_str(), nullptr, false );
}
}
ExecuteFile( editorname, files );
}

View File

@ -23,6 +23,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <advanced_config.h>
#include "footprint_editor_control.h"
#include <wx/generic/textdlgg.h>
#include <string_utils.h>
@ -32,6 +33,7 @@
#include <footprint_edit_frame.h>
#include <pcbnew_id.h>
#include <confirm.h>
#include <gestfich.h> // To open with a text editor
#include <widgets/wx_infobar.h>
#include <footprint.h>
#include <pad.h>
@ -118,6 +120,15 @@ bool FOOTPRINT_EDITOR_CONTROL::Init()
return fp != nullptr;
};
auto canOpenWithTextEditor =
[ this ]( const SELECTION& aSel )
{
// The option is shown if the editor has no current edits,
// dumb/simple guard against opening a new file that does not exist on disk
bool ret = !m_frame->IsContentModified();
return ret;
};
ctxMenu.AddItem( ACTIONS::pinLibrary, unpinnedLibSelectedCondition );
ctxMenu.AddItem( ACTIONS::unpinLibrary, pinnedLibSelectedCondition );
@ -145,6 +156,13 @@ bool FOOTPRINT_EDITOR_CONTROL::Init()
// If we've got nothing else to show, at least show a hide tree option
ctxMenu.AddItem( PCB_ACTIONS::hideFootprintTree, !libInferredCondition );
if( ADVANCED_CFG::GetCfg().m_EnableLibWithText )
{
ctxMenu.AddSeparator();
ctxMenu.AddItem( PCB_ACTIONS::openWithTextEditor,
canOpenWithTextEditor && fpSelectedCondition );
}
return true;
}
@ -567,6 +585,43 @@ int FOOTPRINT_EDITOR_CONTROL::ExportFootprint( const TOOL_EVENT& aEvent )
}
int FOOTPRINT_EDITOR_CONTROL::OpenWithTextEditor( const TOOL_EVENT& aEvent )
{
wxString fullEditorName = Pgm().GetTextEditor();
if( fullEditorName.IsEmpty() )
{
wxMessageBox( _( "No text editor selected in KiCad. Please choose one." ) );
return 0;
}
FP_LIB_TABLE* globalTable = dynamic_cast<FP_LIB_TABLE*>( &GFootprintTable );
FP_LIB_TABLE* projectTable = PROJECT_PCB::PcbFootprintLibs( &m_frame->Prj() );
LIB_ID libId = m_frame->GetTreeFPID();
const char* libName = libId.GetLibNickname().c_str();
wxString libItemName = wxEmptyString;
for( FP_LIB_TABLE* table : { globalTable, projectTable } )
{
if( !table )
break;
libItemName = table->FindRow( libName, true )->GetFullURI( true ).c_str();
libItemName = libItemName + "/" + libId.GetLibItemName() + ".kicad_mod";
}
if( !libItemName.IsEmpty() )
{
ExecuteFile( fullEditorName, libItemName.wc_str(), nullptr, false );
}
return 0;
}
int FOOTPRINT_EDITOR_CONTROL::EditFootprint( const TOOL_EVENT& aEvent )
{
m_frame->LoadFootprintFromLibrary( m_frame->GetTreeFPID() );
@ -788,6 +843,7 @@ void FOOTPRINT_EDITOR_CONTROL::setTransitions()
Go( &FOOTPRINT_EDITOR_CONTROL::ImportFootprint, PCB_ACTIONS::importFootprint.MakeEvent() );
Go( &FOOTPRINT_EDITOR_CONTROL::ExportFootprint, PCB_ACTIONS::exportFootprint.MakeEvent() );
Go( &FOOTPRINT_EDITOR_CONTROL::OpenWithTextEditor, PCB_ACTIONS::openWithTextEditor.MakeEvent() );
Go( &FOOTPRINT_EDITOR_CONTROL::EditTextAndGraphics, PCB_ACTIONS::editTextAndGraphics.MakeEvent() );
Go( &FOOTPRINT_EDITOR_CONTROL::CleanupGraphics, PCB_ACTIONS::cleanupGraphics.MakeEvent() );

View File

@ -63,6 +63,7 @@ public:
int DeleteFootprint( const TOOL_EVENT& aEvent );
int ImportFootprint( const TOOL_EVENT& aEvent );
int ExportFootprint( const TOOL_EVENT& aEvent );
int OpenWithTextEditor( const TOOL_EVENT& aEvent );
int PinLibrary( const TOOL_EVENT& aEvent );
int UnpinLibrary( const TOOL_EVENT& aEvent );

View File

@ -767,6 +767,13 @@ TOOL_ACTION PCB_ACTIONS::exportFootprint( TOOL_ACTION_ARGS()
.Tooltip( _( "Export edited footprint to file" ) )
.Icon( BITMAPS::export_module ) );
TOOL_ACTION PCB_ACTIONS::openWithTextEditor( TOOL_ACTION_ARGS()
.Name( "pcbnew.ModuleEditor.openWithTextEditor" )
.Scope( AS_GLOBAL )
.FriendlyName( _( "Edit in a Text Editor..." ) )
.Tooltip( _( "Open a library file with a text editor" ) )
.Icon( BITMAPS::editor ) );
TOOL_ACTION PCB_ACTIONS::footprintProperties( TOOL_ACTION_ARGS()
.Name( "pcbnew.ModuleEditor.footprintProperties" )
.Scope( AS_GLOBAL )

View File

@ -461,6 +461,7 @@ public:
static TOOL_ACTION pasteFootprint;
static TOOL_ACTION importFootprint;
static TOOL_ACTION exportFootprint;
static TOOL_ACTION openWithTextEditor;
static TOOL_ACTION footprintProperties;
static TOOL_ACTION defaultPadProperties;