Library Editor: component cut/copy/paste/duplicate

This commit is contained in:
Tomasz Włostowski 2017-11-13 20:39:36 +01:00 committed by Maciej Suminski
parent 2ed19668cd
commit 7fc1e1d53c
7 changed files with 108 additions and 2 deletions

View File

@ -175,7 +175,6 @@ struct null_deleter
} }
}; };
LIB_PART::LIB_PART( const wxString& aName, PART_LIB* aLibrary ) : LIB_PART::LIB_PART( const wxString& aName, PART_LIB* aLibrary ) :
EDA_ITEM( LIB_PART_T ), EDA_ITEM( LIB_PART_T ),
m_me( this, null_deleter() ) m_me( this, null_deleter() )

View File

@ -251,7 +251,7 @@ class LIB_PART : public EDA_ITEM
private: private:
void deleteAllFields(); void deleteAllFields();
// LIB_PART() { } // not legal
public: public:

View File

@ -201,6 +201,10 @@ enum id_eeschema_frm
ID_LIBEDIT_SAVE_PART, ID_LIBEDIT_SAVE_PART,
ID_LIBEDIT_REVERT_PART, ID_LIBEDIT_REVERT_PART,
ID_LIBEDIT_REMOVE_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. */ /* Library editor horizontal toolbar IDs. */
ID_DE_MORGAN_NORMAL_BUTT, ID_DE_MORGAN_NORMAL_BUTT,

View File

@ -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 ) void LIB_EDIT_FRAME::OnRevertPart( wxCommandEvent& aEvent )
{ {
LIB_ID libId = getTargetLibId(); LIB_ID libId = getTargetLibId();

View File

@ -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_SAVE_PART, LIB_EDIT_FRAME::OnSavePart )
EVT_TOOL( ID_LIBEDIT_REVERT_PART, LIB_EDIT_FRAME::OnRevertPart ) EVT_TOOL( ID_LIBEDIT_REVERT_PART, LIB_EDIT_FRAME::OnRevertPart )
EVT_TOOL( ID_LIBEDIT_REMOVE_PART, LIB_EDIT_FRAME::OnRemovePart ) 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. // Main horizontal toolbar.
EVT_TOOL( ID_TO_LIBVIEW, LIB_EDIT_FRAME::OnOpenLibraryViewer ) 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_EXPORT_PART, LIB_EDIT_FRAME::OnUpdateEditingPart )
EVT_UPDATE_UI( ID_LIBEDIT_SAVE_PART, LIB_EDIT_FRAME::OnUpdatePartModified ) 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_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_GET_FRAME_EDIT_FIELDS, LIB_EDIT_FRAME::OnUpdateEditingPart )
EVT_UPDATE_UI( ID_LIBEDIT_CHECK_PART, 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 ) 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 ) void LIB_EDIT_FRAME::OnUpdateUndo( wxUpdateUIEvent& event )
{ {
event.Enable( GetCurPart() && GetScreen() && event.Enable( GetCurPart() && GetScreen() &&

View File

@ -39,6 +39,7 @@
#include <lib_draw_item.h> #include <lib_draw_item.h>
#include <lib_collectors.h> #include <lib_collectors.h>
#include <core/optional.h>
class SCH_EDIT_FRAME; class SCH_EDIT_FRAME;
class SYMBOL_LIB_TABLE; class SYMBOL_LIB_TABLE;
@ -288,6 +289,9 @@ public:
*/ */
void OnRemovePart( wxCommandEvent& aEvent ); void OnRemovePart( wxCommandEvent& aEvent );
void OnCopyCutPart( wxCommandEvent& aEvent );
void OnPasteDuplicatePart( wxCommandEvent& aEvent );
void OnSelectAlias( wxCommandEvent& event ); void OnSelectAlias( wxCommandEvent& event );
void OnSelectPart( wxCommandEvent& event ); void OnSelectPart( wxCommandEvent& event );
@ -317,6 +321,7 @@ public:
void OnUpdateEditingPart( wxUpdateUIEvent& event ); void OnUpdateEditingPart( wxUpdateUIEvent& event );
void OnUpdatePartModified( wxUpdateUIEvent& aEvent ); void OnUpdatePartModified( wxUpdateUIEvent& aEvent );
void OnUpdateLibModified( wxUpdateUIEvent& aEvent ); void OnUpdateLibModified( wxUpdateUIEvent& aEvent );
void OnUpdateClipboardNotEmpty( wxUpdateUIEvent& aEvent );
void OnUpdateUndo( wxUpdateUIEvent& event ); void OnUpdateUndo( wxUpdateUIEvent& event );
void OnUpdateRedo( wxUpdateUIEvent& event ); void OnUpdateRedo( wxUpdateUIEvent& event );
void OnUpdateSaveLib( wxUpdateUIEvent& event ); void OnUpdateSaveLib( wxUpdateUIEvent& event );
@ -714,6 +719,13 @@ public:
void emptyScreen(); 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<LIB_PART> m_copiedPart;
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };

View File

@ -56,6 +56,7 @@ CMP_TREE_PANE::CMP_TREE_PANE( LIB_EDIT_FRAME* aParent, LIB_MANAGER* aLibMgr )
menuLibrary->AppendSeparator(); menuLibrary->AppendSeparator();
menuLibrary->Append( ID_LIBEDIT_NEW_PART, _( "New component..." ) ); menuLibrary->Append( ID_LIBEDIT_NEW_PART, _( "New component..." ) );
menuLibrary->Append( ID_LIBEDIT_IMPORT_PART, _( "Import component..." ) ); menuLibrary->Append( ID_LIBEDIT_IMPORT_PART, _( "Import component..." ) );
menuLibrary->Append( ID_LIBEDIT_PASTE_PART, _( "Paste component" ) );
std::unique_ptr<wxMenu> menuPart = std::make_unique<wxMenu>(); std::unique_ptr<wxMenu> menuPart = std::make_unique<wxMenu>();
menuPart->Append( ID_LIBEDIT_EDIT_PART, _( "Edit" ) ); 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_SAVE_PART, _( "Save" ) );
menuPart->Append( ID_LIBEDIT_REVERT_PART, _( "Revert" ) ); menuPart->Append( ID_LIBEDIT_REVERT_PART, _( "Revert" ) );
menuPart->AppendSeparator(); 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 // Append the library menu to the component menu
for( size_t i = 0; i < menuLibrary->GetMenuItemCount(); ++i ) for( size_t i = 0; i < menuLibrary->GetMenuItemCount(); ++i )