Commit a7f19392 authored by Dick Hollenbeck's avatar Dick Hollenbeck

simplify and fix the technique used to get the project fp-lib-table

parent 34dda6a1
......@@ -24,6 +24,7 @@
*/
#include <fctsys.h>
#include <wx/config.h> // wxExpandEnvVars()
#include <wx/filename.h>
#include <wx/stdpaths.h>
......@@ -45,19 +46,6 @@
using namespace FP_LIB_TABLE_T;
/**
* Definition for enabling and disabling footprint library trace output. See the
* wxWidgets documentation on using the WXTRACE environment variable.
*/
static const wxString traceFpLibTable( wxT( "KicadFpLibTable" ) );
/// The footprint library table name used when no project file is passed to Pcbnew or CvPcb.
/// This is used temporarily to store the project specific library table until the project
/// file being edited is saved. It is then moved to the file fp-lib-table in the folder where
/// the project file is saved.
static const wxChar templateProjectFileName[] = wxT( "prj-fp-lib-table" );
static const wxChar global_tbl_name[] = wxT( "fp-lib-table" );
......@@ -408,19 +396,6 @@ void FP_LIB_TABLE::ROW::Format( OUTPUTFORMATTER* out, int nestLevel ) const
);
}
void FP_LIB_TABLE::Save( const wxFileName& aPath ) const throw( IO_ERROR )
{
wxFileName fn = GetProjectTableFileName( aPath.GetFullPath() );
wxLogTrace( traceFpLibTable, wxT( "Saving footprint libary table <%s>." ),
GetChars( fn.GetFullPath() ) );
FILE_OUTPUTFORMATTER sf( fn.GetFullPath() );
Format( &sf, 0 );
}
#define OPT_SEP '|' ///< options separator character
PROPERTIES* FP_LIB_TABLE::ParseOptions( const std::string& aOptionsList )
......@@ -842,34 +817,6 @@ const wxString FP_LIB_TABLE::GlobalPathEnvVariableName()
}
wxString FP_LIB_TABLE::GetProjectTableFileName( const wxString& aProjectFullName )
{
wxFileName fn = aProjectFullName;
wxString path = fn.GetPath();
// Set $KICAD_PRJ_PATH to user's configuration path if aPath is not set or does not exist.
if( !fn.IsOk() || !wxFileName::IsDirReadable( path ) )
{
fn.AssignDir( wxStandardPaths::Get().GetUserConfigDir() );
#if defined( __WINDOWS__ )
fn.AppendDir( wxT( "kicad" ) );
#endif
fn.SetName( templateProjectFileName );
}
else
{
fn.SetName( global_tbl_name );
}
wxLogTrace( traceFpLibTable, wxT( "Project footprint lib table file '%s'." ),
GetChars( fn.GetFullPath() ) );
return fn.GetFullPath();
}
bool FP_LIB_TABLE::LoadGlobalTable( FP_LIB_TABLE& aTable ) throw (IO_ERROR, PARSE_ERROR )
{
bool tableExists = true;
......@@ -911,39 +858,38 @@ wxString FP_LIB_TABLE::GetGlobalTableFileName()
{
wxFileName fn;
// This is possibly problematic with an uncertain wxApp title, which is now
// the case. We'll need a better technique soon.
fn.SetPath( wxStandardPaths::Get().GetUserConfigDir() );
#if defined( __WINDOWS__ )
fn.AppendDir( wxT( "kicad" ) );
#endif
fn.SetName( GetFileName() );
wxLogTrace( traceFpLibTable, wxT( "Global footprint library table file '%s'." ),
GetChars( fn.GetFullPath() ) );
fn.SetName( global_tbl_name );
return fn.GetFullPath();
}
// prefer wxString filename so it can be seen in a debugger easier than wxFileName.
const wxString FP_LIB_TABLE::GetFileName()
{
return global_tbl_name;
}
void FP_LIB_TABLE::Load( const wxFileName& aFileName, FP_LIB_TABLE* aFallBackTable )
void FP_LIB_TABLE::Load( const wxString& aFileName )
throw( IO_ERROR )
{
fallBack = aFallBackTable;
// Empty footprint library tables are valid.
if( aFileName.IsOk() && aFileName.FileExists() )
if( wxFileName::IsFileReadable( aFileName ) )
{
FILE_LINE_READER reader( aFileName.GetFullPath() );
FILE_LINE_READER reader( aFileName );
FP_LIB_TABLE_LEXER lexer( &reader );
Parse( &lexer );
}
}
void FP_LIB_TABLE::Save( const wxString& aFileName ) const throw( IO_ERROR )
{
FILE_OUTPUTFORMATTER sf( aFileName );
Format( &sf, 0 );
}
......@@ -82,6 +82,49 @@ const wxString PROJECT::GetProjectFullName() const
}
const wxString PROJECT::FootprintLibTblName() const
{
wxFileName fn = GetProjectFullName();
wxString path = fn.GetPath();
// DBG(printf( "path:'%s' fn:'%s'\n", TO_UTF8(path), TO_UTF8(fn.GetFullPath()) );)
// if there's no path to the project name, or the name as a whole is bogus or its not
// write-able then use a template file.
if( !fn.GetDirCount() || !fn.IsOk() || !wxFileName::IsDirWritable( path ) )
{
// return a template filename now.
// this next line is likely a problem now, since it relies on an
// application title which is no longer constant or known. This next line needs
// to be re-thought out.
fn.AssignDir( wxStandardPaths::Get().GetUserConfigDir() );
#if defined( __WINDOWS__ )
fn.AppendDir( wxT( "kicad" ) );
#endif
/*
The footprint library table name used when no project file is passed
to Pcbnew or CvPcb. This is used temporarily to store the project
specific library table until the project file being edited is saved.
It is then moved to the file fp-lib-table in the folder where the
project file is saved.
*/
fn.SetName( wxT( "prj-fp-lib-table" ) );
}
else // normal path.
{
fn.SetName( wxT( "fp-lib-table" ) );
}
fn.ClearExt();
return fn.GetFullPath();
}
RETAINED_PATH& PROJECT::RPath( RETPATH_T aIndex )
{
unsigned ndx = unsigned( aIndex );
......
......@@ -93,14 +93,11 @@ void CVPCB_MAINFRAME::LoadProjectFile( const wxString& aFileName )
prj.SetProjectFullName( fn.GetFullPath() );
*/
wxFileName projectFpLibTableFileName = FP_LIB_TABLE::GetProjectTableFileName( fn.GetFullPath() );
wxString projectFpLibTableFileName = prj.FootprintLibTblName();
try
{
// Stack the project specific FP_LIB_TABLE overlay on top of the global table.
// ~FP_LIB_TABLE() will not touch the fallback table, so multiple projects may
// stack this way, all using the same global fallback table.
FootprintLibs()->Load( projectFpLibTableFileName, &GFootprintTable );
FootprintLibs()->Load( projectFpLibTableFileName );
}
catch( const IO_ERROR& ioe )
{
......
......@@ -210,6 +210,9 @@ FP_LIB_TABLE* CVPCB_MAINFRAME::FootprintLibs() const
if( !tbl )
{
// Stack the project specific FP_LIB_TABLE overlay on top of the global table.
// ~FP_LIB_TABLE() will not touch the fallback table, so multiple projects may
// stack this way, all using the same global fallback table.
tbl = new FP_LIB_TABLE( &GFootprintTable );
prj.Elem( PROJECT::FPTBL, tbl );
}
......@@ -499,46 +502,45 @@ void CVPCB_MAINFRAME::ConfigCvpcb( wxCommandEvent& event )
void CVPCB_MAINFRAME::OnEditFootprintLibraryTable( wxCommandEvent& aEvent )
{
bool tableChanged = false;
int r = InvokePcbLibTableEditor( this, &GFootprintTable, FootprintLibs() );
bool tableChanged = false;
int r = InvokePcbLibTableEditor( this, &GFootprintTable, FootprintLibs() );
if( r & 1 )
{
wxString fileName = FP_LIB_TABLE::GetGlobalTableFileName();
try
{
FILE_OUTPUTFORMATTER sf( FP_LIB_TABLE::GetGlobalTableFileName() );
GFootprintTable.Format( &sf, 0 );
GFootprintTable.Save( fileName );
tableChanged = true;
}
catch( const IO_ERROR& ioe )
{
wxString msg = wxString::Format( _(
"Error occurred saving the global footprint library "
"table:\n\n%s" ),
GetChars( ioe.errorText ) );
"Error occurred saving the global footprint library table:\n'%s'\n%s" ),
GetChars( fileName ),
GetChars( ioe.errorText )
);
wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
}
}
if( r & 2 )
{
wxFileName fn = m_NetlistFileName;
fn.SetName( FP_LIB_TABLE::GetFileName() );
fn.SetExt( wxEmptyString );
wxString fileName = Prj().FootprintLibTblName();
try
{
FILE_OUTPUTFORMATTER sf( fn.GetFullPath() );
FootprintLibs()->Format( &sf, 0 );
FootprintLibs()->Save( fileName );
tableChanged = true;
}
catch( IO_ERROR& ioe )
catch( const IO_ERROR& ioe )
{
wxString msg;
msg.Printf( _( "Error occurred saving the global footprint library "
"table:\n\n%s" ), ioe.errorText.GetData() );
wxString msg = wxString::Format( _(
"Error occurred saving the project footprint library table:\n'%s'\n%s" ),
GetChars( fileName ),
GetChars( ioe.errorText )
);
wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
}
}
......
......@@ -317,25 +317,25 @@ int CVPCB_MAINFRAME::SaveCmpLinkFile( const wxString& aFullFileName )
// Save the project specific footprint library table.
if( !FootprintLibs()->IsEmpty( false ) )
{
wxFileName fpLibFileName = fn;
fpLibFileName.ClearExt();
fpLibFileName.SetName( FP_LIB_TABLE::GetFileName() );
wxString fp_lib_tbl = Prj().FootprintLibTblName();
if( fpLibFileName.FileExists()
if( wxFileName::FileExists( fp_lib_tbl )
&& IsOK( this, _( "A footprint library table already exists in this path.\n\nDo "
"you want to overwrite it?" ) ) )
{
try
{
FootprintLibs()->Save( fpLibFileName );
FootprintLibs()->Save( fp_lib_tbl );
}
catch( const IO_ERROR& ioe )
{
DisplayError( this,
wxString::Format( _( "An error occurred attempting to save the "
"footprint library table <%s>\n\n%s" ),
GetChars( fpLibFileName.GetFullPath() ),
GetChars( ioe.errorText ) ) );
wxString msg = wxString::Format( _(
"An error occurred attempting to save the "
"footprint library table '%s'\n\n%s" ),
GetChars( fp_lib_tbl ),
GetChars( ioe.errorText )
);
DisplayError( this, msg );
}
}
}
......
......@@ -357,8 +357,6 @@ public:
*/
void Format( OUTPUTFORMATTER* out, int nestLevel ) const throw( IO_ERROR );
void Save( const wxFileName& aPath ) const throw( IO_ERROR );
/**
* Function GetLogicalLibs
* returns the logical library names, all of them that are pertinent to
......@@ -544,11 +542,13 @@ public:
*/
static wxString GetGlobalTableFileName();
#if 0
/**
* Function GetFileName
* @return the footprint library file name.
*/
static const wxString GetFileName();
#endif
/**
* Function GlobalPathEnvVarVariableName
......@@ -560,19 +560,24 @@ public:
*/
static const wxString GlobalPathEnvVariableName();
static wxString GetProjectTableFileName( const wxString& aProjectFullName );
/**
* Function Load
* loads the footprint library table using the path defined in \a aFileName with
* \a aFallBackTable.
*
* @param aFileName contains the path and possible the file name and extension.
* @param aFallBackTable the fall back footprint library table which can be NULL.
* @param aFileName contains the full path to the s-expression file.
*
* @throw IO_ERROR if an error occurs attempting to load the footprint library
* table.
*/
void Load( const wxFileName& aFileName, FP_LIB_TABLE* aFallBackTable ) throw( IO_ERROR );
void Load( const wxString& aFileName ) throw( IO_ERROR );
/**
* Function Save
* writes this table to aFileName in s-expression form.
* @param aFileName is the name of the file to write to.
*/
void Save( const wxString& aFileName ) const throw( IO_ERROR );
protected:
......
......@@ -82,6 +82,13 @@ public:
*/
VTBL_ENTRY const wxString GetProjectFullName() const;
/**
* Function FootprintLibTblName
* returns the path and filename of this project's fp-lib-table,
* i.e. the project specific one, not the global one.
*/
VTBL_ENTRY const wxString FootprintLibTblName() const;
/**
* Function ConfigSave
* saves the current "project" parameters into the wxConfigBase* derivative.
......
......@@ -181,7 +181,11 @@ FP_LIB_TABLE* PCB_BASE_FRAME::FootprintLibs() const
if( !tbl )
{
// Stack the project specific FP_LIB_TABLE overlay on top of the global table.
// ~FP_LIB_TABLE() will not touch the fallback table, so multiple projects may
// stack this way, all using the same global fallback table.
tbl = new FP_LIB_TABLE( &GFootprintTable );
prj.Elem( PROJECT::FPTBL, tbl );
}
......
......@@ -605,25 +605,25 @@ bool PCB_EDIT_FRAME::SavePcbFile( const wxString& aFileName, bool aCreateBackupF
// Save the project specific footprint library table.
if( !FootprintLibs()->IsEmpty( false ) )
{
wxFileName fn = pcbFileName;
fn.ClearExt();
fn.SetName( FP_LIB_TABLE::GetFileName() );
wxString fp_lib_tbl = Prj().FootprintLibTblName();
if( fn.FileExists()
if( wxFileName::FileExists( fp_lib_tbl )
&& IsOK( this, _( "A footprint library table already exists in this path.\n\nDo "
"you want to overwrite it?" ) ) )
{
try
{
FootprintLibs()->Save( fn );
FootprintLibs()->Save( fp_lib_tbl );
}
catch( IO_ERROR& ioe )
catch( const IO_ERROR& ioe )
{
DisplayError( this,
wxString::Format( _( "An error occurred attempting to save the "
"footprint library table '%s'\n\n%s" ),
GetChars( fn.GetFullPath() ),
GetChars( ioe.errorText ) ) );
wxString msg = wxString::Format( _(
"An error occurred attempting to save the "
"footprint library table '%s'\n\n%s" ),
GetChars( fp_lib_tbl ),
GetChars( ioe.errorText )
);
DisplayError( this, msg );
}
}
}
......
......@@ -478,6 +478,9 @@ void IFACE::OnKifaceEnd()
// wxPython will do its own cleanup as part of that process.
// This should only be called if python was setup correctly.
/* bring this in, but without a linker error:
pcbnewFinishPythonScripting();
*/
#endif
}
......@@ -82,13 +82,13 @@ void PCB_EDIT_FRAME::Process_Config( wxCommandEvent& event )
case ID_MENU_PCB_SHOW_HIDE_MUWAVE_TOOLBAR:
m_show_microwave_tools = ! m_show_microwave_tools;
m_auimgr.GetPane( wxT( "m_microWaveToolBar" ) ).Show( m_show_microwave_tools );
m_auimgr.Update();
m_auimgr.Update();
GetMenuBar()->SetLabel( ID_MENU_PCB_SHOW_HIDE_MUWAVE_TOOLBAR,
m_show_microwave_tools ?
_( "Hide Microwave Toolbar" ): _( "Show Microwave Toolbar" ));
break;
case ID_PCB_LAYERS_SETUP:
InstallDialogLayerSetup();
......@@ -108,7 +108,7 @@ void PCB_EDIT_FRAME::Process_Config( wxCommandEvent& event )
GFootprintTable.Format( &sf, 0 );
tableChanged = true;
}
catch( IO_ERROR& ioe )
catch( const IO_ERROR& ioe )
{
wxString msg = wxString::Format( _(
"Error occurred saving the global footprint library "
......@@ -123,18 +123,20 @@ void PCB_EDIT_FRAME::Process_Config( wxCommandEvent& event )
// is kept in memory and created in the path when the new board is saved.
if( (r & 2) && !GetBoard()->GetFileName().IsEmpty() )
{
wxFileName fn = GetBoard()->GetFileName();
wxString tblName = Prj().FootprintLibTblName();
try
{
FootprintLibs()->Save( fn );
FootprintLibs()->Save( tblName );
tableChanged = true;
}
catch( IO_ERROR& ioe )
catch( const IO_ERROR& ioe )
{
wxString msg;
msg.Printf( _( "Error occurred saving project specific footprint library "
"table:\n\n%s" ), ioe.errorText.GetData() );
wxString msg = wxString::Format( _(
"Error occurred saving project specific footprint library "
"table:\n\n%s" ),
GetChars( ioe.errorText )
);
wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
}
}
......@@ -171,27 +173,27 @@ void PCB_EDIT_FRAME::Process_Config( wxCommandEvent& event )
break;
case ID_CONFIG_READ:
{
fn = GetBoard()->GetFileName();
fn.SetExt( ProjectFileExtension );
{
fn = GetBoard()->GetFileName();
fn.SetExt( ProjectFileExtension );
wxFileDialog dlg( this, _( "Read Project File" ), fn.GetPath(),
fn.GetFullName(), ProjectFileWildcard,
wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR );
wxFileDialog dlg( this, _( "Read Project File" ), fn.GetPath(),
fn.GetFullName(), ProjectFileWildcard,
wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR );
if( dlg.ShowModal() == wxID_CANCEL )
break;
if( dlg.ShowModal() == wxID_CANCEL )
break;
if( !wxFileExists( dlg.GetPath() ) )
{
wxString msg;
msg.Printf( _( "File %s not found" ), GetChars( dlg.GetPath() ) );
DisplayError( this, msg );
break;
}
if( !wxFileExists( dlg.GetPath() ) )
{
wxString msg;
msg.Printf( _( "File %s not found" ), GetChars( dlg.GetPath() ) );
DisplayError( this, msg );
break;
}
LoadProjectSettings( dlg.GetPath() );
}
LoadProjectSettings( dlg.GetPath() );
}
break;
// Hotkey IDs
......@@ -258,17 +260,15 @@ bool PCB_EDIT_FRAME::LoadProjectSettings( const wxString& aProjectFileName )
SetElementVisibility( RATSNEST_VISIBLE, showRats );
#endif
fn = GetBoard()->GetFileName();
wxFileName projectFpLibTableFileName = FP_LIB_TABLE::GetProjectTableFileName( fn.GetFullPath() );
wxString projectFpLibTableFileName = Prj().FootprintLibTblName();
FootprintLibs()->Clear();
try
{
FootprintLibs()->Load( projectFpLibTableFileName, &GFootprintTable );
FootprintLibs()->Load( projectFpLibTableFileName );
}
catch( IO_ERROR ioe )
catch( const IO_ERROR& ioe )
{
DisplayError( this, ioe.errorText );
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment