From c237838d00c4039e4323c4cde52d83c66b019216 Mon Sep 17 00:00:00 2001 From: jean-pierre charras <jp.charras@wanadoo.fr> Date: Tue, 21 Oct 2014 18:34:51 +0200 Subject: [PATCH] =?UTF-8?q?python=20scripting:=20refinement=20for=20UTF8?= =?UTF-8?q?=20class=20(from=20Miguel=20=C3=81ngel=20Ajo=20Pelayo)=20add=20?= =?UTF-8?q?=5F=5Fstr=5F=5F=20=20method,=20usefull=20to=20print=20a=20UTF8?= =?UTF-8?q?=20string.=20Kicad=20manager:=20fix=20issues=20in=20file=20watc?= =?UTF-8?q?her:=20changes=20(adding/removing=20files)=20=20in=20project=20?= =?UTF-8?q?folder=20not=20seen=20by=20Kicad=20manager=20(perhaps=20due=20t?= =?UTF-8?q?o=20changes=20in=20internal=20wxWidgets=20code=20between=202.9.?= =?UTF-8?q?5=20and=203.0).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kicad/class_treeprojectfiles.cpp | 2 + kicad/kicad.h | 3 +- kicad/tree_project_frame.cpp | 75 ++++++++++++++++++++++---------- scripting/kicad.i | 11 +++++ scripts/test_kicad_plugin.py | 6 +-- 5 files changed, 70 insertions(+), 27 deletions(-) diff --git a/kicad/class_treeprojectfiles.cpp b/kicad/class_treeprojectfiles.cpp index 2889a09848..b67db30de5 100644 --- a/kicad/class_treeprojectfiles.cpp +++ b/kicad/class_treeprojectfiles.cpp @@ -76,6 +76,8 @@ TREEPROJECTFILES::TREEPROJECTFILES( TREE_PROJECT_FRAME* parent ) : m_ImageList->Add( KiBitmap( post_drill_xpm ) ); // TREE_DRILL m_ImageList->Add( KiBitmap( svg_file_xpm ) ); // TREE_SVG m_ImageList->Add( KiBitmap( pagelayout_load_default_xpm ) );// TREE_PAGE_LAYOUT_DESCR + m_ImageList->Add( KiBitmap( module_xpm ) ); // TREE_FOOTPRINT_FILE + m_ImageList->Add( KiBitmap( library_xpm ) ); // TREE_SCHEMATIC_LIBFILE SetImageList( m_ImageList ); } diff --git a/kicad/kicad.h b/kicad/kicad.h index 74f04abf68..7be6ddd794 100644 --- a/kicad/kicad.h +++ b/kicad/kicad.h @@ -54,7 +54,6 @@ class TREE_PROJECT_FRAME; // // When changing this enum please verify (and perhaps update) // TREE_PROJECT_FRAME::GetFileExt(), -// TREE_PROJECT_FRAME::GetFileExt() // s_AllowedExtensionsToList[] enum TreeFileType { @@ -74,6 +73,8 @@ enum TreeFileType { TREE_DRILL, // Excellon drill file (.drl) TREE_SVG, // SVG file (.svg) TREE_PAGE_LAYOUT_DESCR, // Page layout and title block descr file (.kicad_wks) + TREE_FOOTPRINT_FILE, // footprint file (.kicad_mod) + TREE_SCHEMATIC_LIBFILE, // schematic library file (.lib) TREE_MAX }; diff --git a/kicad/tree_project_frame.cpp b/kicad/tree_project_frame.cpp index 3dc0dfa7c8..c6a8006e56 100644 --- a/kicad/tree_project_frame.cpp +++ b/kicad/tree_project_frame.cpp @@ -65,9 +65,11 @@ static const wxChar* s_allowedExtensionsToList[] = wxT( "^.*\\.pro$" ), wxT( "^.*\\.pdf$" ), wxT( "^[^$].*\\.brd$" ), // Legacy Pcbnew files - wxT( "^[^$].*\\.kicad_pcb$" ), // S format Pcbnew files + wxT( "^[^$].*\\.kicad_pcb$" ), // S format Pcbnew board files wxT( "^[^$].*\\.kicad_wks$" ), // S format kicad page layout descr files + wxT( "^[^$].*\\.kicad_mod$" ), // S format kicad footprint files, currently not listed wxT( "^.*\\.net$" ), + wxT( "^.*\\.lib$" ), // Schematic library file wxT( "^.*\\.txt$" ), wxT( "^.*\\.pho$" ), // Gerber file (Old Kicad extension) wxT( "^.*\\.gbr$" ), // Gerber file @@ -75,7 +77,6 @@ static const wxChar* s_allowedExtensionsToList[] = wxT( "^.*\\.gt[alops]$" ), // Gerber front (or top) layer file wxT( "^.*\\.g[0-9]{1,2}$" ), // Gerber inner layer file wxT( "^.*\\.odt$" ), - wxT( "^.*\\.sxw$" ), wxT( "^.*\\.htm$" ), wxT( "^.*\\.html$" ), wxT( "^.*\\.rpt$" ), // Report files @@ -196,27 +197,33 @@ void TREE_PROJECT_FRAME::OnCreateNewDirectory( wxCommandEvent& event ) root = m_TreeProject->GetSelection(); } + wxString prj_dir = wxPathOnly( m_Parent->GetProjectFileName() ); + // Ask for the new sub directory name wxString curr_dir = treeData->GetDir(); - // Make the current subdir relative to the current path: if( !curr_dir.IsEmpty() ) // A subdir is selected { - curr_dir += wxFileName::GetPathSeparator(); - curr_dir += wxT( "dummy" ); - wxFileName fn( curr_dir ); - fn.MakeRelativeTo(); - curr_dir = fn.GetPath() + wxFileName::GetPathSeparator(); + // Make this subdir name relative to the current path. + // It will be more easy to read by the user, in the next dialog + wxFileName fn; + fn.AssignDir( curr_dir ); + fn.MakeRelativeTo( prj_dir ); + curr_dir = fn.GetPath(); + + if( !curr_dir.IsEmpty() ) + curr_dir += wxFileName::GetPathSeparator(); } - wxString msg = wxString::Format( _( "Current working directory:\n%s" ), GetChars( wxGetCwd() ) ); - + wxString msg = wxString::Format( _( "Current project directory:\n%s" ), GetChars( prj_dir ) ); wxString subdir = wxGetTextFromUser( msg, _( "Create New Directory" ), curr_dir ); if( subdir.IsEmpty() ) return; - if( wxMkdir( subdir ) ) + wxString full_dirname = prj_dir + wxFileName::GetPathSeparator() + subdir; + + if( wxMkdir( full_dirname ) ) { #ifndef KICAD_USE_FILES_WATCHER AddItemToTreeProject( subdir, root ); @@ -287,6 +294,14 @@ wxString TREE_PROJECT_FRAME::GetFileExt( TreeFileType type ) ext = PageLayoutDescrFileExtension; break; + case TREE_FOOTPRINT_FILE: + ext = KiCadFootprintFileExtension; + break; + + case TREE_SCHEMATIC_LIBFILE: + ext = SchematicLibraryFileExtension; + break; + default: // Eliminates unnecessary GCC warning. break; } @@ -357,7 +372,15 @@ wxString TREE_PROJECT_FRAME::GetFileWildcard( TreeFileType type ) ext = PageLayoutDescrFileWildcard; break; - default: // Eliminates unnecessary GCC warning. + case TREE_FOOTPRINT_FILE: + ext = KiCadFootprintLibFileWildcard; + break; + + case TREE_SCHEMATIC_LIBFILE: + ext = SchematicLibraryFileWildcard; + break; + + default: // Eliminates unnecessary GCC warning. break; } @@ -536,7 +559,7 @@ bool TREE_PROJECT_FRAME::AddItemToTreeProject( const wxString& aName, { do // Add name in tree, but do not recurse { - wxString path = aName + wxCONFIG_PATH_SEPARATOR + dir_filename; + wxString path = aName + wxFileName::GetPathSeparator() + dir_filename; AddItemToTreeProject( path, cellule, false ); } while( dir.GetNext( &dir_filename ) ); } @@ -603,8 +626,8 @@ void TREE_PROJECT_FRAME::ReCreateTreePrj() { if( filename != fn.GetFullName() ) { - wxString n = dir.GetName() + wxCONFIG_PATH_SEPARATOR + filename; - AddItemToTreeProject( n, m_root ); + wxString name = dir.GetName() + wxFileName::GetPathSeparator() + filename; + AddItemToTreeProject( name, m_root ); } cont = dir.GetNext( &filename ); @@ -791,8 +814,8 @@ void TREE_PROJECT_FRAME::OnExpand( wxTreeEvent& Event ) { do // Add name to tree item, but do not recurse in subdirs: { - wxString n = fileName + wxCONFIG_PATH_SEPARATOR + dir_filename; - AddItemToTreeProject( n, kid, false ); + wxString name = fileName + wxFileName::GetPathSeparator() + dir_filename; + AddItemToTreeProject( name, kid, false ); } while( dir.GetNext( &dir_filename ) ); } @@ -830,9 +853,11 @@ TREEPROJECT_ITEM* TREE_PROJECT_FRAME::GetItemIdData( wxTreeItemId aId ) wxTreeItemId TREE_PROJECT_FRAME::findSubdirTreeItem( const wxString& aSubDir ) { + wxString prj_dir = wxPathOnly( m_Parent->GetProjectFileName() ); + // If the subdir is the current working directory, return m_root // in main list: - if( wxGetCwd() == aSubDir ) + if( prj_dir == aSubDir ) return m_root; // The subdir is in the main tree or in a subdir: Locate it @@ -905,8 +930,8 @@ void TREE_PROJECT_FRAME::OnFileSystemEvent( wxFileSystemWatcherEvent& event ) return; } - wxTreeItemId root_id = findSubdirTreeItem( subdir ); + if( !root_id.IsOk() ) return; @@ -976,12 +1001,15 @@ void TREE_PROJECT_FRAME::FileWatcherReset() // moreover, under wxWidgets 2.9.4, AddTree does not work properly. // We can see wxString under a debugger, not a wxFileName - wxString pro_dir = wxPathOnly( m_Parent->GetProjectFileName() ); + wxString prj_dir = wxPathOnly( m_Parent->GetProjectFileName() ); + wxFileName fn; + fn.AssignDir( prj_dir ); + fn.DontFollowLink(); #ifdef __WINDOWS__ - m_watcher->AddTree( pro_dir ); + m_watcher->AddTree( fn ); #else - m_watcher->Add( pro_dir ); + m_watcher->Add( fn ); // Add subdirs wxTreeItemIdValue cookie; @@ -1017,7 +1045,8 @@ void TREE_PROJECT_FRAME::FileWatcherReset() if( wxFileName::IsDirReadable( path ) ) // linux whines about watching protected dir { - m_watcher->Add( path ); + fn.AssignDir( path ); + m_watcher->Add( fn ); // if kid is a subdir, push in list to explore it later if( itemData->IsPopulated() && m_TreeProject->GetChildrenCount( kid ) ) diff --git a/scripting/kicad.i b/scripting/kicad.i index 35c7f91790..198a0eccfc 100644 --- a/scripting/kicad.i +++ b/scripting/kicad.i @@ -132,8 +132,19 @@ %pythoncode { + /* + * Get the char buffer of the UTF8 string + */ def GetChars(self): return self.Cast_to_CChar() + + /* + * Convert the UTF8 string to a python string + * Same as GetChars(), but more easy to use in print command + */ + def __str__(self): + return self.GetChars() + } } diff --git a/scripts/test_kicad_plugin.py b/scripts/test_kicad_plugin.py index 54758dca43..3bb20cc331 100755 --- a/scripts/test_kicad_plugin.py +++ b/scripts/test_kicad_plugin.py @@ -65,7 +65,7 @@ footprint = plugin.FootprintLoad( lib_path2, 'footprint' ) fpid = footprint.GetFPID() fpid.SetLibNickname( UTF8( 'mylib' ) ) -name = fpid.Format().GetChars() +name = fpid.Format().GetChars() # example to get the UTF8 char buffer # Always after a FootprintLoad() the internal name should match the one used to load it. print( "Internal name should be 'footprint': '%s'" % name ) @@ -76,10 +76,10 @@ footprint = plugin.FootprintLoad( lib_path1, 'mine' ) fpid = footprint.GetFPID() fpid.SetLibNickname( UTF8( 'other_mylib' ) ) -name = fpid.Format().GetChars() # Always after a FootprintLoad() the internal name should match the one used to load it. -print( "Internal name should be 'mine': '%s'" % name ) +# Example to print an UTF8 string +print( "Internal name should be 'mine': '%s'" % fpid.Format() ) # As of 3-Dec-2013 this test is passed by KICAD_PLUGIN and Wayne is owed an atta boy!