Gerbview: consolidate some dialog code

Preparing also for a third usage where we autodetect the file type
This commit is contained in:
Mike Williams 2021-08-16 14:08:05 -04:00
parent db407a1c0b
commit 8692014691
2 changed files with 88 additions and 98 deletions

View File

@ -114,16 +114,71 @@ void GERBVIEW_FRAME::OnClearJobFileHistory( wxCommandEvent& aEvent )
}
bool GERBVIEW_FRAME::LoadGerberFiles( const wxString& aFullFileName )
bool GERBVIEW_FRAME::LoadFileOrShowDialog( const wxString& aFileName,
const wxString& dialogFiletypes,
const wxString& dialogTitle, const int filetype )
{
static int lastGerberFileWildcard = 0;
wxString filetypes;
wxArrayString filenamesList;
wxFileName filename = aFullFileName;
wxFileName filename = aFileName;
wxString currentPath;
if( !filename.IsOk() )
{
// Use the current working directory if the file name path does not exist.
if( filename.DirExists() )
currentPath = filename.GetPath();
else
{
currentPath = m_mruPath;
// On wxWidgets 3.1 (bug?) the path in wxFileDialog is ignored when
// finishing by the dir separator. Remove it if any:
if( currentPath.EndsWith( '\\' ) || currentPath.EndsWith( '/' ) )
currentPath.RemoveLast();
}
wxFileDialog dlg( this, dialogTitle, currentPath, filename.GetFullName(), dialogFiletypes,
wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE | wxFD_CHANGE_DIR );
dlg.SetFilterIndex( lastGerberFileWildcard );
if( dlg.ShowModal() == wxID_CANCEL )
return false;
lastGerberFileWildcard = dlg.GetFilterIndex();
dlg.GetPaths( filenamesList );
m_mruPath = currentPath = dlg.GetDirectory();
}
else
{
filenamesList.Add( aFileName );
currentPath = filename.GetPath();
m_mruPath = currentPath;
}
// Set the busy cursor
wxBusyCursor wait;
bool isFirstFile = GetImagesList()->GetLoadedImageCount() == 0;
std::vector<int> fileTypesVec( filenamesList.Count(), filetype );
bool success = LoadListOfGerberAndDrillFiles( currentPath, filenamesList, &fileTypesVec );
// Auto zoom is only applied if there is only one file loaded
if( isFirstFile )
{
Zoom_Automatique( false );
}
return success;
}
bool GERBVIEW_FRAME::LoadGerberFiles( const wxString& aFileName )
{
wxString filetypes;
wxFileName filename = aFileName;
/* Standard gerber filetypes
* (See http://en.wikipedia.org/wiki/Gerber_File)
* The .gbr (.pho in legacy files) extension is the default used in Pcbnew; however
@ -149,61 +204,29 @@ bool GERBVIEW_FRAME::LoadGerberFiles( const wxString& aFullFileName )
filetypes += _( "Bottom paste" ) + AddFileExtListToFilter( { "GBP" } ) + wxT( "|" );
filetypes += _( "Top paste" ) + AddFileExtListToFilter( { "GTP" } ) + wxT( "|" );
filetypes += _( "Keep-out layer" ) + AddFileExtListToFilter( { "GKO" } ) + wxT( "|" );
filetypes += _( "Mechanical layers" ) + AddFileExtListToFilter( { "GM1", "GM2", "GM3", "GM4", "GM5", "GM6", "GM7", "GM8", "GM9" } ) + wxT( "|" );
filetypes += _( "Mechanical layers" )
+ AddFileExtListToFilter(
{ "GM1", "GM2", "GM3", "GM4", "GM5", "GM6", "GM7", "GM8", "GM9" } )
+ wxT( "|" );
filetypes += _( "Top Pad Master" ) + AddFileExtListToFilter( { "GPT" } ) + wxT( "|" );
filetypes += _( "Bottom Pad Master" ) + AddFileExtListToFilter( { "GPB" } ) + wxT( "|" );
// All filetypes
filetypes += AllFilesWildcard();
// Use the current working directory if the file name path does not exist.
if( filename.DirExists() )
currentPath = filename.GetPath();
else
// 0 = gerber files
return LoadFileOrShowDialog( aFileName, filetypes, _( "Open Gerber File(s)" ), 0 );
}
bool GERBVIEW_FRAME::LoadExcellonFiles( const wxString& aFileName )
{
currentPath = m_mruPath;
wxString filetypes = DrillFileWildcard();
filetypes << wxT( "|" );
filetypes += AllFilesWildcard();
// On wxWidgets 3.1 (bug?) the path in wxFileDialog is ignored when
// finishing by the dir separator. Remove it if any:
if( currentPath.EndsWith( '\\' ) || currentPath.EndsWith( '/' ) )
currentPath.RemoveLast();
}
wxFileDialog dlg( this, _( "Open Gerber File(s)" ), currentPath, filename.GetFullName(),
filetypes,
wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE | wxFD_CHANGE_DIR );
dlg.SetFilterIndex( lastGerberFileWildcard );
if( dlg.ShowModal() == wxID_CANCEL )
return false;
lastGerberFileWildcard = dlg.GetFilterIndex();
dlg.GetPaths( filenamesList );
m_mruPath = currentPath = dlg.GetDirectory();
}
else
{
filenamesList.Add( aFullFileName );
m_mruPath = currentPath = filename.GetPath();
}
// Set the busy cursor
wxBusyCursor wait;
bool isFirstFile = GetImagesList()->GetLoadedImageCount() == 0;
// 0 is gerber files
std::vector<int> fileTypesVec( filenamesList.Count(), 0 );
bool success = LoadListOfGerberAndDrillFiles( currentPath, filenamesList, &fileTypesVec );
// Auto zoom is only applied if there is only one file loaded
if( isFirstFile )
{
Zoom_Automatique( false );
}
return success;
// 1 = drill files
return LoadFileOrShowDialog( aFileName, filetypes, _( "Open NC (Excellon) Drill File(s)" ), 1 );
}
@ -388,62 +411,6 @@ bool GERBVIEW_FRAME::LoadListOfGerberAndDrillFiles( const wxString& aPath,
}
bool GERBVIEW_FRAME::LoadExcellonFiles( const wxString& aFullFileName )
{
wxString filetypes;
wxArrayString filenamesList;
wxFileName filename = aFullFileName;
wxString currentPath;
if( !filename.IsOk() )
{
filetypes = DrillFileWildcard();
filetypes << wxT( "|" );
/* All filetypes */
filetypes += AllFilesWildcard();
/* Use the current working directory if the file name path does not exist. */
if( filename.DirExists() )
currentPath = filename.GetPath();
else
currentPath = m_mruPath;
wxFileDialog dlg( this, _( "Open NC (Excellon) Drill File(s)" ),
currentPath, filename.GetFullName(), filetypes,
wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE | wxFD_CHANGE_DIR );
if( dlg.ShowModal() == wxID_CANCEL )
return false;
dlg.GetPaths( filenamesList );
currentPath = wxGetCwd();
m_mruPath = currentPath;
}
else
{
filenamesList.Add( aFullFileName );
currentPath = filename.GetPath();
m_mruPath = currentPath;
}
// Set the busy cursor
wxBusyCursor wait;
bool isFirstFile = GetImagesList()->GetLoadedImageCount() == 0;
// 1 is drill files
std::vector<int> fileTypesVec( filenamesList.Count(), 1 );
bool success = LoadListOfGerberAndDrillFiles( currentPath, filenamesList, &fileTypesVec );
// Auto zoom is only applied if there is only one file loaded
if( isFirstFile )
{
Zoom_Automatique( false );
}
return success;
}
bool GERBVIEW_FRAME::unarchiveFiles( const wxString& aFullFileName, REPORTER* aReporter )

View File

@ -300,6 +300,16 @@ public:
*/
bool unarchiveFiles( const wxString& aFullFileName, REPORTER* aReporter = nullptr );
/**
* Load a given file or selected file(s), if the filename is empty.
*
* @param aFileName - file name with full path to open or empty string.
* if empty string: a dialog will be opened to select one or
* a set of files
* @return true if file was opened successfully.
*/
bool LoadAutodetectedFiles( const wxString& aFileName );
/**
* Load a given Gerber file or selected file(s), if the filename is empty.
*
@ -485,6 +495,19 @@ private:
void OnClearDrlFileHistory( wxCommandEvent& aEvent );
void OnClearGbrFileHistory( wxCommandEvent& aEvent );
/**
* Loads the file provided or shows a dialog to get the file(s) from the user.
*
* @param aFileName Name of the file to open. Shows a dialog if not specified.
* @param dialogFiletypes File extensions to pass to the dialog if necessary
* @param dialogTitle Dialog title to use if necessary
* @param filetype Type of file for parsing, 0 = gerber, 1 = drill, 2 = autodetect
*
* @return true if success opening all files, false otherwise
*/
bool LoadFileOrShowDialog( const wxString& aFileName, const wxString& dialogFiletypes,
const wxString& dialogTitle, const int filetype );
// The Tool Framework initialization
void setupTools();