From 0d849393109fe795656762d4731b1ca0b50585ff Mon Sep 17 00:00:00 2001
From: Oliver <oliver.henry.walters@gmail.com>
Date: Thu, 2 Feb 2017 21:38:34 +1100
Subject: [PATCH] libedit: Improved selection process for DeleteComponent

- Reused SelectComponent dialog
- Reduced selection to current library only
- Ability to filter component for deletion
- Automatically pre-select the current component
---
 eeschema/libedit.cpp | 33 ++++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/eeschema/libedit.cpp b/eeschema/libedit.cpp
index c22deae..ef9a60f 100644
--- a/eeschema/libedit.cpp
+++ b/eeschema/libedit.cpp
@@ -45,6 +45,9 @@
 #include <template_fieldnames.h>
 #include <wildcards_and_files_ext.h>
 
+#include <dialog_choose_component.h>
+#include <component_tree_search_container.h>
+
 #include <dialogs/dialog_lib_new_component.h>
 
 
@@ -519,6 +522,7 @@ void LIB_EDIT_FRAME::DeleteOnePart( wxCommandEvent& event )
     m_lastDrawItem = NULL;
     m_drawItem = NULL;
 
+    LIB_PART *part = GetCurPart();
     PART_LIB* lib = GetCurLib();
 
     if( !lib )
@@ -533,30 +537,28 @@ void LIB_EDIT_FRAME::DeleteOnePart( wxCommandEvent& event )
         }
     }
 
-    lib->GetAliasNames( nameList );
+    COMPONENT_TREE_SEARCH_CONTAINER search_container( Prj().SchLibs() );
 
-    if( nameList.IsEmpty() )
-    {
-        msg.Printf( _( "Part library '%s' is empty." ), GetChars( lib->GetName() ) );
-        wxMessageBox( msg, _( "Delete Entry Error" ), wxID_OK | wxICON_EXCLAMATION, this );
-        return;
-    }
+    wxString name = part ? part->GetName() : wxString( wxEmptyString );
+    search_container.AddLibrary( *lib );
+    search_container.SetPreselectNode( name, /* aUnit */ 0 );
 
-    msg.Printf( _( "Select one of %d components to delete\nfrom library '%s'." ),
-                int( nameList.GetCount() ),
-                GetChars( lib->GetName() ) );
+    wxString dialogTitle;
+    dialogTitle.Printf( _( "Delete Component (%u items loaded)" ), search_container.GetComponentsCount() );
 
-    wxSingleChoiceDialog dlg( this, msg, _( "Delete Part" ), nameList );
+    DIALOG_CHOOSE_COMPONENT dlg( this, dialogTitle, &search_container, m_convert );
 
-    if( dlg.ShowModal() == wxID_CANCEL || dlg.GetStringSelection().IsEmpty() )
+    if( dlg.ShowModal() == wxID_CANCEL )
+    {
         return;
+    }
 
-    libEntry = lib->FindAlias( dlg.GetStringSelection() );
+    libEntry = dlg.GetSelectedAlias( NULL );
 
     if( !libEntry )
     {
         msg.Printf( _( "Entry '%s' not found in library '%s'." ),
-                    GetChars( dlg.GetStringSelection() ),
+                    GetChars( libEntry->GetName() ),
                     GetChars( lib->GetName() ) );
         DisplayError( this, msg );
         return;
@@ -569,11 +571,12 @@ void LIB_EDIT_FRAME::DeleteOnePart( wxCommandEvent& event )
     if( !IsOK( this, msg ) )
         return;
 
-    LIB_PART* part = GetCurPart();
+    part = GetCurPart();
 
     if( !part || !part->HasAlias( libEntry->GetName() ) )
     {
         lib->RemoveAlias( libEntry );
+        m_canvas->Refresh();
         return;
     }
 
-- 
1.9.5.msysgit.0

