From 7fc1e1d53c15740f3ab1de4a3abf2b923912f55e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20W=C5=82ostowski?= Date: Mon, 13 Nov 2017 20:39:36 +0100 Subject: [PATCH] Library Editor: component cut/copy/paste/duplicate --- eeschema/class_libentry.cpp | 1 - eeschema/class_libentry.h | 2 +- eeschema/eeschema_id.h | 4 ++ eeschema/libedit.cpp | 75 ++++++++++++++++++++++++++++++ eeschema/libeditframe.cpp | 11 +++++ eeschema/libeditframe.h | 12 +++++ eeschema/widgets/cmp_tree_pane.cpp | 5 ++ 7 files changed, 108 insertions(+), 2 deletions(-) diff --git a/eeschema/class_libentry.cpp b/eeschema/class_libentry.cpp index 7f2b694e75..b6f46f8c2d 100644 --- a/eeschema/class_libentry.cpp +++ b/eeschema/class_libentry.cpp @@ -175,7 +175,6 @@ struct null_deleter } }; - LIB_PART::LIB_PART( const wxString& aName, PART_LIB* aLibrary ) : EDA_ITEM( LIB_PART_T ), m_me( this, null_deleter() ) diff --git a/eeschema/class_libentry.h b/eeschema/class_libentry.h index a5095c2630..6c2f1ee1ed 100644 --- a/eeschema/class_libentry.h +++ b/eeschema/class_libentry.h @@ -251,7 +251,7 @@ class LIB_PART : public EDA_ITEM private: void deleteAllFields(); - // LIB_PART() { } // not legal + public: diff --git a/eeschema/eeschema_id.h b/eeschema/eeschema_id.h index 7a971f5852..c641323b7a 100644 --- a/eeschema/eeschema_id.h +++ b/eeschema/eeschema_id.h @@ -201,6 +201,10 @@ enum id_eeschema_frm ID_LIBEDIT_SAVE_PART, ID_LIBEDIT_REVERT_PART, ID_LIBEDIT_REMOVE_PART, + ID_LIBEDIT_CUT_PART, + ID_LIBEDIT_COPY_PART, + ID_LIBEDIT_PASTE_PART, + ID_LIBEDIT_DUPLICATE_PART, /* Library editor horizontal toolbar IDs. */ ID_DE_MORGAN_NORMAL_BUTT, diff --git a/eeschema/libedit.cpp b/eeschema/libedit.cpp index ba1e297d1d..d0e3afd94d 100644 --- a/eeschema/libedit.cpp +++ b/eeschema/libedit.cpp @@ -375,6 +375,81 @@ void LIB_EDIT_FRAME::OnRemovePart( wxCommandEvent& aEvent ) } +void LIB_EDIT_FRAME::OnCopyCutPart( wxCommandEvent& aEvent ) +{ + int unit = 0; + LIB_ID partId = m_treePane->GetCmpTree()->GetSelectedLibId( &unit ); + LIB_PART* part = m_libMgr->GetBufferedPart( partId.GetLibItemName(), partId.GetLibNickname() ); + + if( !part ) + return; + + LIB_ID libId = getTargetLibId(); + m_copiedPart.reset( new LIB_PART( *part ) ); + + if( aEvent.GetId() == ID_LIBEDIT_CUT_PART ) + { + if( isCurrentPart( libId ) ) + emptyScreen(); + + m_libMgr->RemovePart( libId.GetLibItemName(), libId.GetLibNickname() ); + } +} + + +void LIB_EDIT_FRAME::OnPasteDuplicatePart( wxCommandEvent& aEvent ) +{ + int unit = 0; + LIB_ID libId = m_treePane->GetCmpTree()->GetSelectedLibId( &unit ); + wxString lib = libId.GetLibNickname(); + + if( !m_libMgr->LibraryExists( lib ) ) + return; + + LIB_PART* srcPart = nullptr; + + if( aEvent.GetId() == ID_LIBEDIT_DUPLICATE_PART ) + srcPart = m_libMgr->GetBufferedPart( libId.GetLibItemName(), lib ); + else if( aEvent.GetId() == ID_LIBEDIT_PASTE_PART ) + srcPart = m_copiedPart.get(); + else + wxFAIL; + + if( !srcPart ) + return; + + LIB_PART newPart( *srcPart ); + fixDuplicateAliases( &newPart, lib ); + m_libMgr->UpdatePart( &newPart, lib ); +} + + +void LIB_EDIT_FRAME::fixDuplicateAliases( LIB_PART* aPart, const wxString& aLibrary ) +{ + wxString newName; + + for( int i = 0; i < aPart->GetAliasCount(); ++i ) + { + LIB_ALIAS* alias = aPart->GetAlias( i ); + int sfx = 0; + newName = alias->GetName(); + + while( m_libMgr->PartExists( newName, aLibrary ) ) + { + wxString suffix = ( sfx == 0 ) ? wxT( " (copy)" ) + : wxString::Format( _( " (copy %d)" ), sfx ); + newName = alias->GetName() + suffix; + ++sfx; + } + + if( i == 0 ) + aPart->SetName( newName ); + else + alias->SetName( newName ); + } +} + + void LIB_EDIT_FRAME::OnRevertPart( wxCommandEvent& aEvent ) { LIB_ID libId = getTargetLibId(); diff --git a/eeschema/libeditframe.cpp b/eeschema/libeditframe.cpp index 1022a4507f..f4eb10a86c 100644 --- a/eeschema/libeditframe.cpp +++ b/eeschema/libeditframe.cpp @@ -103,6 +103,10 @@ BEGIN_EVENT_TABLE( LIB_EDIT_FRAME, EDA_DRAW_FRAME ) EVT_TOOL( ID_LIBEDIT_SAVE_PART, LIB_EDIT_FRAME::OnSavePart ) EVT_TOOL( ID_LIBEDIT_REVERT_PART, LIB_EDIT_FRAME::OnRevertPart ) EVT_TOOL( ID_LIBEDIT_REMOVE_PART, LIB_EDIT_FRAME::OnRemovePart ) + EVT_TOOL( ID_LIBEDIT_CUT_PART, LIB_EDIT_FRAME::OnCopyCutPart ) + EVT_TOOL( ID_LIBEDIT_COPY_PART, LIB_EDIT_FRAME::OnCopyCutPart ) + EVT_TOOL( ID_LIBEDIT_PASTE_PART, LIB_EDIT_FRAME::OnPasteDuplicatePart ) + EVT_TOOL( ID_LIBEDIT_DUPLICATE_PART, LIB_EDIT_FRAME::OnPasteDuplicatePart ) // Main horizontal toolbar. EVT_TOOL( ID_TO_LIBVIEW, LIB_EDIT_FRAME::OnOpenLibraryViewer ) @@ -166,6 +170,7 @@ BEGIN_EVENT_TABLE( LIB_EDIT_FRAME, EDA_DRAW_FRAME ) EVT_UPDATE_UI( ID_LIBEDIT_EXPORT_PART, LIB_EDIT_FRAME::OnUpdateEditingPart ) EVT_UPDATE_UI( ID_LIBEDIT_SAVE_PART, LIB_EDIT_FRAME::OnUpdatePartModified ) EVT_UPDATE_UI( ID_LIBEDIT_REVERT_PART, LIB_EDIT_FRAME::OnUpdatePartModified ) + EVT_UPDATE_UI( ID_LIBEDIT_PASTE_PART, LIB_EDIT_FRAME::OnUpdateClipboardNotEmpty ) EVT_UPDATE_UI( ID_LIBEDIT_GET_FRAME_EDIT_FIELDS, LIB_EDIT_FRAME::OnUpdateEditingPart ) EVT_UPDATE_UI( ID_LIBEDIT_CHECK_PART, LIB_EDIT_FRAME::OnUpdateEditingPart ) EVT_UPDATE_UI( ID_LIBEDIT_GET_FRAME_EDIT_PART, LIB_EDIT_FRAME::OnUpdateEditingPart ) @@ -525,6 +530,12 @@ void LIB_EDIT_FRAME::OnUpdateLibModified( wxUpdateUIEvent& aEvent ) } +void LIB_EDIT_FRAME::OnUpdateClipboardNotEmpty( wxUpdateUIEvent& aEvent ) +{ + aEvent.Enable( !!m_copiedPart ); +} + + void LIB_EDIT_FRAME::OnUpdateUndo( wxUpdateUIEvent& event ) { event.Enable( GetCurPart() && GetScreen() && diff --git a/eeschema/libeditframe.h b/eeschema/libeditframe.h index f441748a0b..a92a2a81d9 100644 --- a/eeschema/libeditframe.h +++ b/eeschema/libeditframe.h @@ -39,6 +39,7 @@ #include #include +#include class SCH_EDIT_FRAME; class SYMBOL_LIB_TABLE; @@ -288,6 +289,9 @@ public: */ void OnRemovePart( wxCommandEvent& aEvent ); + void OnCopyCutPart( wxCommandEvent& aEvent ); + void OnPasteDuplicatePart( wxCommandEvent& aEvent ); + void OnSelectAlias( wxCommandEvent& event ); void OnSelectPart( wxCommandEvent& event ); @@ -317,6 +321,7 @@ public: void OnUpdateEditingPart( wxUpdateUIEvent& event ); void OnUpdatePartModified( wxUpdateUIEvent& aEvent ); void OnUpdateLibModified( wxUpdateUIEvent& aEvent ); + void OnUpdateClipboardNotEmpty( wxUpdateUIEvent& aEvent ); void OnUpdateUndo( wxUpdateUIEvent& event ); void OnUpdateRedo( wxUpdateUIEvent& event ); void OnUpdateSaveLib( wxUpdateUIEvent& event ); @@ -714,6 +719,13 @@ public: void emptyScreen(); +private: + ///> Renames LIB_PART aliases to avoid conflicts before adding a component to a library + void fixDuplicateAliases( LIB_PART* aPart, const wxString& aLibrary ); + + // Copy/cut/paste buffer + std::unique_ptr m_copiedPart; + DECLARE_EVENT_TABLE() }; diff --git a/eeschema/widgets/cmp_tree_pane.cpp b/eeschema/widgets/cmp_tree_pane.cpp index de8d77e6ee..61fa9634b0 100644 --- a/eeschema/widgets/cmp_tree_pane.cpp +++ b/eeschema/widgets/cmp_tree_pane.cpp @@ -56,6 +56,7 @@ CMP_TREE_PANE::CMP_TREE_PANE( LIB_EDIT_FRAME* aParent, LIB_MANAGER* aLibMgr ) menuLibrary->AppendSeparator(); menuLibrary->Append( ID_LIBEDIT_NEW_PART, _( "New component..." ) ); menuLibrary->Append( ID_LIBEDIT_IMPORT_PART, _( "Import component..." ) ); + menuLibrary->Append( ID_LIBEDIT_PASTE_PART, _( "Paste component" ) ); std::unique_ptr menuPart = std::make_unique(); menuPart->Append( ID_LIBEDIT_EDIT_PART, _( "Edit" ) ); @@ -64,6 +65,10 @@ CMP_TREE_PANE::CMP_TREE_PANE( LIB_EDIT_FRAME* aParent, LIB_MANAGER* aLibMgr ) menuPart->Append( ID_LIBEDIT_SAVE_PART, _( "Save" ) ); menuPart->Append( ID_LIBEDIT_REVERT_PART, _( "Revert" ) ); menuPart->AppendSeparator(); + menuPart->Append( ID_LIBEDIT_CUT_PART, _( "Cut component" ) ); + menuPart->Append( ID_LIBEDIT_COPY_PART, _( "Copy component" ) ); + menuPart->Append( ID_LIBEDIT_DUPLICATE_PART, _( "Duplicate component" ) ); + menuPart->AppendSeparator(); // Append the library menu to the component menu for( size_t i = 0; i < menuLibrary->GetMenuItemCount(); ++i )