From 7dbcbf6d157f1d18d4683ac0b3c8a79de547cfd5 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Thu, 3 Jan 2013 20:25:12 +0100 Subject: [PATCH] Kicad manager: cleanup code, remove dead or not working code. Fix minor isssues --- eeschema/viewlib_frame.cpp | 1 - kicad/class_treeproject_item.cpp | 89 +--------- kicad/class_treeproject_item.h | 13 +- kicad/tree_project_frame.cpp | 295 +++++++++---------------------- kicad/tree_project_frame.h | 21 ++- 5 files changed, 102 insertions(+), 317 deletions(-) diff --git a/eeschema/viewlib_frame.cpp b/eeschema/viewlib_frame.cpp index a28f1fcef4..9a5eb3a441 100644 --- a/eeschema/viewlib_frame.cpp +++ b/eeschema/viewlib_frame.cpp @@ -34,7 +34,6 @@ #include #include -//#include #include #include #include diff --git a/kicad/class_treeproject_item.cpp b/kicad/class_treeproject_item.cpp index c8dfb3f175..e4a62caa5f 100644 --- a/kicad/class_treeproject_item.cpp +++ b/kicad/class_treeproject_item.cpp @@ -77,94 +77,11 @@ wxString TREEPROJECT_ITEM::GetDir() const wxFileName filename = wxFileName( m_FileName ); - filename.MakeRelativeTo( wxGetCwd() ); - - wxArrayString dirs = filename.GetDirs(); - - wxString dir; - - for( unsigned int i = 0; i < dirs.Count(); i++ ) - { - dir += dirs[i] + filename.GetPathSeparator(); - } - + wxString dir = filename.GetPath(); return dir; } -// Move the object to dest -void TREEPROJECT_ITEM::Move( TREEPROJECT_ITEM* dest ) -{ - // function not safe. - return; - - const wxString sep = wxFileName().GetPathSeparator(); - - if( m_Type == TREE_DIRECTORY ) - return; - - if( !dest ) - return; - - if( m_parent != dest->m_parent ) - return; // Can not cross move! - - if( dest == this ) - return; // Can not move to ourself... - - wxTreeItemId parent = m_parent->GetItemParent( GetId() ); - - if( dest == dynamic_cast( m_parent->GetItemData( parent ) ) ) - return; // same parent ? - - // We need to create a new item from us, and move - // data to there ... - - // First move file on the disk - wxFileName fname( m_FileName ); - - wxString destName; - - if( !dest->GetDir().IsEmpty() ) - destName = dest->GetDir() + sep; - - destName += fname.GetFullName(); - - if( destName == GetFileName() ) - return; // Same place ?? - - // Move the file on the disk: - if( !wxRenameFile( GetFileName(), destName, false ) ) - { - wxMessageDialog( m_parent, _( "Unable to move file ... " ), - _( "Permission error ?" ), wxICON_ERROR | wxOK ); - return; - } - - SetFileName( destName ); - - if( TREE_DIRECTORY != GetType() ) - { - // Move the tree item itself now: - wxTreeItemId oldId = GetId(); - int i = m_parent->GetItemImage( oldId ); - wxString text = m_parent->GetItemText( oldId ); - - // Bye bye old Id :'( - wxTreeItemId newId = m_parent->AppendItem( dest->GetId(), text, i ); - m_parent->SetItemData( newId, this ); - m_parent->SetItemData( oldId, NULL ); - m_parent->Delete( oldId ); - } - else - { - // We should move recursively all files, but that's quite boring - // let's just refresh that's all ... TODO (change this to a better code ...) - wxCommandEvent dummy; - dynamic_cast( m_parent )->GetParent()->m_Parent->OnRefresh( dummy ); - } -} - /* rename the file checking if extension change occurs */ bool TREEPROJECT_ITEM::Rename( const wxString& name, bool check ) { @@ -218,7 +135,9 @@ type.\n Do you want to continue ?" return false; } +#ifndef KICAD_USE_FILES_WATCHER SetFileName( newFile ); +#endif return true; } @@ -263,7 +182,7 @@ bool TREEPROJECT_ITEM::Delete( bool check ) } -/* Called under item activation */ +// Called under item activation void TREEPROJECT_ITEM::Activate( TREE_PROJECT_FRAME* prjframe ) { wxString sep = wxFileName().GetPathSeparator(); diff --git a/kicad/class_treeproject_item.h b/kicad/class_treeproject_item.h index 059391324a..fd70212228 100644 --- a/kicad/class_treeproject_item.h +++ b/kicad/class_treeproject_item.h @@ -15,7 +15,6 @@ public: bool m_WasPopulated; // True the name is a directory, and its content was read private: wxTreeCtrl* m_parent; - wxMenu m_fileMenu; int m_state; public: @@ -50,17 +49,15 @@ public: m_FileName = name; } + /** + * @return the path of an item. + * if this item is a directory, returns the stored filename + * if this is a file, returns its path + */ wxString GetDir() const; bool Rename( const wxString& name, bool check = true ); bool Delete( bool check = true ); - void Move( TREEPROJECT_ITEM* dest ); void Activate( TREE_PROJECT_FRAME* prjframe ); - - const wxMenu* GetMenu() - { - return &m_fileMenu; - } - void SetState( int state ); }; diff --git a/kicad/tree_project_frame.cpp b/kicad/tree_project_frame.cpp index fd398fb325..b442b1a918 100644 --- a/kicad/tree_project_frame.cpp +++ b/kicad/tree_project_frame.cpp @@ -106,14 +106,11 @@ const wxString TextFileWildcard( wxT( "Text files (*.txt)|*.txt" ) ); * only useful files are shown. */ - /*****************************************************************************/ BEGIN_EVENT_TABLE( TREE_PROJECT_FRAME, wxSashLayoutWindow ) EVT_TREE_ITEM_ACTIVATED( ID_PROJECT_TREE, TREE_PROJECT_FRAME::OnSelect ) EVT_TREE_ITEM_EXPANDED( ID_PROJECT_TREE, TREE_PROJECT_FRAME::OnExpand ) EVT_TREE_ITEM_RIGHT_CLICK( ID_PROJECT_TREE, TREE_PROJECT_FRAME::OnRight ) -EVT_TREE_BEGIN_DRAG( ID_PROJECT_TREE, TREE_PROJECT_FRAME::OnDragStart ) -EVT_TREE_END_DRAG( ID_PROJECT_TREE, TREE_PROJECT_FRAME::OnDragEnd ) EVT_MENU( ID_PROJECT_TXTEDIT, TREE_PROJECT_FRAME::OnOpenSelectedFileWithTextEditor ) EVT_MENU( ID_PROJECT_NEWDIR, TREE_PROJECT_FRAME::OnCreateNewDirectory ) EVT_MENU( ID_PROJECT_DELETE, TREE_PROJECT_FRAME::OnDeleteFile ) @@ -134,8 +131,6 @@ TREE_PROJECT_FRAME::TREE_PROJECT_FRAME( KICAD_MANAGER_FRAME* parent ) : { m_Parent = parent; m_TreeProject = NULL; - wxMenuItem* item; - m_PopupMenu = NULL; #ifdef KICAD_USE_FILES_WATCHER m_watcher = NULL; Connect( wxEVT_FSWATCHER, @@ -154,156 +149,18 @@ TREE_PROJECT_FRAME::TREE_PROJECT_FRAME( KICAD_MANAGER_FRAME* parent ) : m_filters.push_back( wxT( "^no KiCad files found" ) ); - for( int i = 0; i < TREE_MAX; i++ ) - m_ContextMenus.push_back( new wxMenu() ); - - wxMenu* menu; - - // New files context menu: - wxMenu* menus[2]; - menus[0] = m_ContextMenus[TREE_DIRECTORY]; - menus[1] = m_ContextMenus[TREE_PROJECT]; - - for( int i = 0; i < 2; i++ ) - { - menu = menus[i]; - - // ID_PROJECT_NEWDIR - item = new wxMenuItem( menu, - ID_PROJECT_NEWDIR, - _( "New D&irectory" ), - _( "Create a New Directory" ) ); - item->SetBitmap( KiBitmap( directory_xpm ) ); - menu->Append( item ); - } - - - // Put the Rename and Delete file menu commands: - for( int i = TREE_PROJECT + 1; i < TREE_MAX; i++ ) - { - menu = m_ContextMenus[i]; - - // ID_PROJECT_RENAME - item = new wxMenuItem( menu, ID_PROJECT_RENAME, - TREE_DIRECTORY != i ? _( "&Rename file" ) : - _( "&Rename directory" ), - TREE_DIRECTORY != i ? _( "Rename file" ) : - _( "Rename directory" ) ); - item->SetBitmap( KiBitmap( right_xpm ) ); - menu->Append( item ); - - - if( TREE_DIRECTORY != i ) - { - // ID_PROJECT_TXTEDIT - item = new wxMenuItem( menu, - ID_PROJECT_TXTEDIT, - _( "&Edit in a text editor" ), - _( "Open the file in a Text Editor" ) ); - item->SetBitmap( KiBitmap( icon_txt_xpm ) ); - menu->Append( item ); - } - - // ID_PROJECT_DELETE - item = new wxMenuItem( menu, - ID_PROJECT_DELETE, - TREE_DIRECTORY != i ? _( "&Delete File" ) : - _( "&Delete Directory" ), - TREE_DIRECTORY != i ? _( "Delete the File" ) : - _( "Delete the Directory and its content" ) ); - item->SetBitmap( KiBitmap( delete_xpm ) ); - menu->Append( item ); - } - ReCreateTreePrj(); } TREE_PROJECT_FRAME::~TREE_PROJECT_FRAME() { - size_t i; - wxMenu* menu; - - for( i = 0; i < m_ContextMenus.size(); i++ ) - { - menu = m_ContextMenus[i]; - delete menu; - } - - if( m_PopupMenu ) - delete m_PopupMenu; - #ifdef KICAD_USE_FILES_WATCHER delete m_watcher; #endif } -/** - * @brief Allowing drag & drop of file other than the currently opened project - */ -/*****************************************************************************/ -void TREE_PROJECT_FRAME::OnDragStart( wxTreeEvent& event ) -/*****************************************************************************/ -{ - /* Ensure item is selected - * (Under Windows start drag does not activate the item) - */ - wxTreeItemId curr_item = event.GetItem(); - - m_TreeProject->SelectItem( curr_item ); - TREEPROJECT_ITEM* data = GetSelectedData(); - - if( data->GetFileName() == m_Parent->m_ProjectFileName.GetFullPath() ) - return; - - wxImage img = m_TreeProject->GetImageList()->GetBitmap( data->GetType() - 1 ).ConvertToImage(); - m_DragCursor = wxCursor( img ); - m_Parent->wxWindow::SetCursor( (wxCursor&) m_DragCursor ); - event.Allow(); -} - - -/*****************************************************************************/ -void TREE_PROJECT_FRAME::OnDragEnd( wxTreeEvent& event ) -/*****************************************************************************/ -{ - m_Parent->SetCursor( wxNullCursor ); - - TREEPROJECT_ITEM* source_data = GetSelectedData(); - wxTreeItemId dest = event.GetItem(); - - if( !dest.IsOk() ) - return; // Cancelled ... - - TREEPROJECT_ITEM* destData = - dynamic_cast( m_TreeProject->GetItemData( dest ) ); - - if( !destData ) - return; - - // the item can be a member of the selected directory; get the directory itself - if( TREE_DIRECTORY != destData->GetType() - && !m_TreeProject->ItemHasChildren( dest ) ) - { - // the item is a member of the selected directory; get the directory itself - dest = m_TreeProject->GetItemParent( dest ); - - if( !dest.IsOk() ) - return; // no parent ? - - // Select the right destData: - destData = - dynamic_cast( m_TreeProject->GetItemData( dest ) ); - - if( !destData ) - return; - } - - source_data->Move( destData ); -} - - /*****************************************************************************/ void TREE_PROJECT_FRAME::RemoveFilter( const wxString& filter ) /*****************************************************************************/ @@ -319,16 +176,6 @@ void TREE_PROJECT_FRAME::RemoveFilter( const wxString& filter ) } -/** - * @brief TODO - */ -/*****************************************************************************/ -wxMenu* TREE_PROJECT_FRAME::GetContextMenu( int type ) -/*****************************************************************************/ -{ - return m_ContextMenus[type]; -} - /** * Called by the popup menu in the tree frame @@ -379,7 +226,11 @@ void TREE_PROJECT_FRAME::OnCreateNewDirectory( wxCommandEvent& event ) return; if( wxMkdir( subdir ) ) + { +#ifndef KICAD_USE_FILES_WATCHER AddItemToTreeProject( subdir, root ); +#endif + } } @@ -578,40 +429,52 @@ bool TREE_PROJECT_FRAME::AddItemToTreeProject( const wxString& aName, // at the top of the hierarchy. The schematic is top level only if // there is a line in the header saying: // "Sheet 1 " + // However if the file has the same name as the project, it is always + // shown, because it is expected the root sheet. + // (and to fix an issue (under XP but could exist under other OS), + // when a .sch file is created, the file + // create is sent to the wxFileSystemWatcher, but the file still has 0 byte + // so it cannot detected as root sheet + // This is an ugly fix. if( isSchematic ) { - char line[128]; // small because we just need a few bytes from the start of a line - FILE* fp; + wxString fullFileName = aName.BeforeLast( '.' ); + wxString rootName; + TREEPROJECT_ITEM* itemData = GetItemIdData( m_root ); + if( itemData ) + rootName = itemData->m_FileName.BeforeLast( '.' ); - wxString FullFileName = aName; - - fp = wxFopen( FullFileName, wxT( "rt" ) ); - - if( fp == NULL ) + if( fullFileName != rootName ) { - return false; - } + char line[128]; // small because we just need a few bytes from the start of a line + FILE* fp; - addFile = false; + fullFileName = aName; + fp = wxFopen( fullFileName, wxT( "rt" ) ); - // check the first 100 lines for the "Sheet 1" string - int l =0; - for( int i = 0; i<100; ++i, l++ ) - { - if( !fgets( line, sizeof(line), fp ) ) - break; + if( fp == NULL ) + return false; - if( !strncmp( line, "Sheet 1 ", 8 ) ) + addFile = false; + + // check the first 100 lines for the "Sheet 1" string + for( int i = 0; i<100; ++i ) { - addFile = true; - break; + if( !fgets( line, sizeof(line), fp ) ) + break; + + if( !strncmp( line, "Sheet 1 ", 8 ) ) + { + addFile = true; + break; + } } + + fclose( fp ); + + if( !addFile ) + return false; // it is a non-top-level schematic } - - fclose( fp ); - - if( !addFile ) - return false; // it is a non-top-level schematic } for( int i = TREE_PROJECT; i < TREE_MAX; i++ ) @@ -728,7 +591,7 @@ void TREE_PROJECT_FRAME::ReCreateTreePrj() m_TreeProject->SetItemData( rootcellule, new TREEPROJECT_ITEM( TREE_PROJECT, - wxEmptyString, + fn.GetFullPath(), m_TreeProject ) ); // Now adding all current files if available @@ -776,11 +639,6 @@ void TREE_PROJECT_FRAME::OnRight( wxTreeEvent& Event ) /* Ensure item is selected (Under Windows right click does not select the item) */ m_TreeProject->SelectItem( curr_item ); - // Delete and recreate the context menu - delete ( m_PopupMenu ); - m_PopupMenu = new wxMenu(); - - // Get the current filename: tree_data = GetSelectedData(); if( !tree_data ) @@ -789,41 +647,45 @@ void TREE_PROJECT_FRAME::OnRight( wxTreeEvent& Event ) tree_id = tree_data->GetType(); FullFileName = tree_data->GetFileName(); - // copy menu contents in order of the next array: - wxMenu* menus[] = + wxMenu popupMenu; + + switch( tree_id ) { - GetContextMenu( tree_id ), - const_cast( tree_data->GetMenu() ) - }; + case TREE_PROJECT: + AddMenuItem( &popupMenu, ID_PROJECT_NEWDIR, + _( "New D&irectory" ), + _( "Create a New Directory" ), + KiBitmap( directory_xpm ) ); + break; - for( unsigned int j = 0; j < sizeof(menus) / sizeof(wxMenu*); j++ ) - { - wxMenu* menu = menus[j]; + case TREE_DIRECTORY: + AddMenuItem( &popupMenu, ID_PROJECT_NEWDIR, + _( "New D&irectory" ), + _( "Create a New Directory" ), + KiBitmap( directory_xpm ) ); + AddMenuItem( &popupMenu, ID_PROJECT_DELETE, + _( "&Delete Directory" ), + _( "Delete the Directory and its content" ), + KiBitmap( delete_xpm ) ); + break; - if( !menu ) - continue; - - wxMenuItemList list = menu->GetMenuItems(); - - for( unsigned int i = 0; i < list.GetCount(); i++ ) - { - // Grrrr! wxMenu does not have any copy constructor !! (do it by hand) - wxMenuItem* src = list[i]; - wxString label = src->GetItemLabelText(); - - // for obscure reasons, the & is translated into _ ... so replace it - label.Replace( wxT( "_" ), wxT( "&" ), true ); - wxMenuItem* item = new wxMenuItem( m_PopupMenu, src->GetId(), - label, - src->GetHelp(), src->GetKind() ); - - item->SetBitmap( src->GetBitmap() ); - m_PopupMenu->Append( item ); - } + default: + AddMenuItem( &popupMenu, ID_PROJECT_TXTEDIT, + _( "&Edit in a text editor" ), + _( "Open the file in a Text Editor" ), + KiBitmap( icon_txt_xpm ) ); + AddMenuItem( &popupMenu, ID_PROJECT_RENAME, + _( "&Rename file" ), + _( "Rename file" ), + KiBitmap( right_xpm ) ); + AddMenuItem( &popupMenu, ID_PROJECT_DELETE, + _( "&Delete File" ), + _( "Delete the Directory and its content" ), + KiBitmap( delete_xpm ) ); + break; } - if( m_PopupMenu ) - PopupMenu( m_PopupMenu ); + PopupMenu( &popupMenu ); } /* @@ -837,6 +699,9 @@ void TREE_PROJECT_FRAME::OnOpenSelectedFileWithTextEditor( wxCommandEvent& event if( !tree_data ) return; + if( tree_data->GetType() == TREE_DIRECTORY ) + return; + wxString FullFileName = tree_data->GetFileName(); AddDelimiterString( FullFileName ); wxString editorname = wxGetApp().GetEditorName(); @@ -893,8 +758,8 @@ void TREE_PROJECT_FRAME::OnRenameFile( wxCommandEvent& ) } -/** - * @brief TODO +/* + * called on a double click on an item */ /*****************************************************************************/ void TREE_PROJECT_FRAME::OnSelect( wxTreeEvent& Event ) diff --git a/kicad/tree_project_frame.h b/kicad/tree_project_frame.h index c57d33badc..4317b25178 100644 --- a/kicad/tree_project_frame.h +++ b/kicad/tree_project_frame.h @@ -46,13 +46,10 @@ class TREE_PROJECT_FRAME : public wxSashLayoutWindow public: KICAD_MANAGER_FRAME* m_Parent; TREEPROJECTFILES* m_TreeProject; - wxTreeItemId m_root; + private: - std::vector m_ContextMenus; + wxTreeItemId m_root; std::vector m_filters; - wxMenu* m_PopupMenu; - wxCursor m_DragCursor; - wxCursor m_Default; #ifdef KICAD_USE_FILES_WATCHER wxFileSystemWatcher* m_watcher; // file system watcher (since wxWidgets 2.9.2) @@ -73,7 +70,6 @@ public: #endif protected: - wxMenu* GetContextMenu( int type ); static wxString GetFileExt( TreeFileType type ); static wxString GetFileWildcard( TreeFileType type ); @@ -94,10 +90,19 @@ protected: TREEPROJECT_ITEM* GetItemIdData( wxTreeItemId aId ); private: + /** + * Called on a double click on an item + */ void OnSelect( wxTreeEvent& Event ); + + /** + * Called on a click on the + or - button of an item with children + */ void OnExpand( wxTreeEvent& Event ); - void OnDragStart( wxTreeEvent& event ); - void OnDragEnd( wxTreeEvent& event ); + + /** + * Called on a right click on an item + */ void OnRight( wxTreeEvent& Event ); /**