From fa9937701fa49b587ba2fab2214ec20cb96c9493 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Mon, 13 Jul 2020 14:16:21 +0100 Subject: [PATCH] Undo for Edit Component's LibId. --- .../dialogs/dialog_edit_components_libid.cpp | 115 +---- .../dialog_edit_components_libid_base.cpp | 76 ++-- .../dialog_edit_components_libid_base.fbp | 403 +----------------- .../dialog_edit_components_libid_base.h | 32 +- 4 files changed, 60 insertions(+), 566 deletions(-) diff --git a/eeschema/dialogs/dialog_edit_components_libid.cpp b/eeschema/dialogs/dialog_edit_components_libid.cpp index 1aa4cafa40..f80eae05ed 100644 --- a/eeschema/dialogs/dialog_edit_components_libid.cpp +++ b/eeschema/dialogs/dialog_edit_components_libid.cpp @@ -330,9 +330,6 @@ private: /// returns true if all new lib id are valid bool validateLibIds(); - /// Reverts all changes already made - void revertChanges(); - /** run the lib browser and set the selected LIB_ID for row aRow * @param aRow is the row to edit * @return false if the command was aborted @@ -344,15 +341,9 @@ private: // called on a right click or a left double click: void onCellBrowseLib( wxGridEvent& event ) override; - // Apply changes, but do not close the dialog - void onApplyButton( wxCommandEvent& event ) override; - // Cancel all changes, and close the dialog void onCancel( wxCommandEvent& event ) override { - if( m_isModified ) - revertChanges(); - // Just skipping the event doesn't work after the library browser was run if( IsQuasiModal() ) EndQuasiModal( wxID_CANCEL ); @@ -360,18 +351,9 @@ private: event.Skip(); } - // Undo all changes, and clear the list of new lib_ids - void onUndoChangesButton( wxCommandEvent& event ) override; - // Try to find a candidate for non existing symbols void onClickOrphansButton( wxCommandEvent& event ) override; - // UI event, to enable/disable buttons - void updateUIChangesButton( wxUpdateUIEvent& event ) override - { - m_buttonUndo->Enable( m_isModified ); - } - // Automatically called when click on OK button bool TransferDataFromWindow() override; @@ -580,28 +562,6 @@ bool DIALOG_EDIT_COMPONENTS_LIBID::validateLibIds() } -void DIALOG_EDIT_COMPONENTS_LIBID::onApplyButton( wxCommandEvent& event ) -{ - if( TransferDataFromWindow() ) - GetParent()->GetCanvas()->Refresh(); -} - - -void DIALOG_EDIT_COMPONENTS_LIBID::onUndoChangesButton( wxCommandEvent& event ) -{ - revertChanges(); - - int row_max = m_grid->GetNumberRows() - 1; - - for( int row = 0; row <= row_max; row++ ) - { - m_grid->SetCellValue( row, COL_NEW_LIBID, wxEmptyString ); - } - - m_isModified = false; -} - - void DIALOG_EDIT_COMPONENTS_LIBID::onCellBrowseLib( wxGridEvent& event ) { int row = event.GetRow(); @@ -621,10 +581,10 @@ void DIALOG_EDIT_COMPONENTS_LIBID::onClickOrphansButton( wxCommandEvent& event ) unsigned fixesCount = 0; // Try to find a candidate for non existing symbols in any loaded library - for( unsigned ii = 0; ii < m_OrphansRowIndexes.size(); ii++ ) + for( int orphanRow : m_OrphansRowIndexes ) { - wxString orphanLibid = m_grid->GetCellValue( m_OrphansRowIndexes[ii], COL_CURR_LIBID ); - int grid_row_idx = m_OrphansRowIndexes[ii]; //row index in m_grid for the current item + wxString orphanLibid = m_grid->GetCellValue( orphanRow, COL_CURR_LIBID ); + int grid_row_idx = orphanRow; //row index in m_grid for the current item LIB_ID curr_libid; curr_libid.Parse( orphanLibid, LIB_ID::ID_SCH, true ); @@ -748,7 +708,6 @@ bool DIALOG_EDIT_COMPONENTS_LIBID::TransferDataFromWindow() if( !validateLibIds() ) return false; - bool change = false; int row_max = m_grid->GetNumberRows() - 1; for( int row = 0; row <= row_max; row++ ) @@ -777,8 +736,7 @@ bool DIALOG_EDIT_COMPONENTS_LIBID::TransferDataFromWindow() { wxString msg; - msg.Printf( _( "Error occurred loading symbol %s from library %s." - "\n\n%s" ), + msg.Printf( _( "Error occurred loading symbol %s from library %s.\n\n%s" ), id.GetLibItemName().wx_str(), id.GetLibNickname().wx_str(), ioe.What() ); @@ -789,6 +747,10 @@ bool DIALOG_EDIT_COMPONENTS_LIBID::TransferDataFromWindow() if( symbol == nullptr ) continue; + GetParent()->SaveCopyInUndoList( cmp.m_Screen, cmp.m_Component, UR_CHANGED, + m_isModified ); + m_isModified = true; + cmp.m_Screen->Remove( cmp.m_Component ); SCH_FIELD* value = cmp.m_Component->GetField( VALUE ); @@ -798,76 +760,15 @@ bool DIALOG_EDIT_COMPONENTS_LIBID::TransferDataFromWindow() cmp.m_Component->SetLibId( id ); cmp.m_Component->SetLibSymbol( symbol->Flatten().release() ); - change = true; cmp.m_Screen->Append( cmp.m_Component ); cmp.m_Screen->SetModify(); } } - if( change ) - { - m_isModified = true; - } - return true; } -void DIALOG_EDIT_COMPONENTS_LIBID::revertChanges() -{ - bool change = false; - int row_max = m_grid->GetNumberRows() - 1; - - for( int row = 0; row <= row_max; row++ ) - { - for( CMP_CANDIDATE& cmp : m_components ) - { - if( cmp.m_Row != row ) - continue; - - LIB_ID id; - id.Parse( cmp.m_InitialLibId, LIB_ID::ID_SCH, true ); - - if( cmp.m_Component->GetLibId() != id ) - { - LIB_PART* symbol = nullptr; - - try - { - symbol = Prj().SchSymbolLibTable()->LoadSymbol( id ); - } - catch( const IO_ERROR& ioe ) - { - // It's probably a bad idea to show a user error dialog here because the - // the reason to use this dialog is when there are a bunch of broken library - // symbol links so just show a debug trace message for development purposes. - wxLogTrace( traceSymbolResolver, - "Error occurred loading symbol %s from library %s." - "\n\n%s", - id.GetLibItemName().wx_str(), - id.GetLibNickname().wx_str(), - ioe.What() ); - } - - cmp.m_Screen->Remove( cmp.m_Component ); - - if( symbol ) - cmp.m_Component->SetLibSymbol( symbol->Flatten().release() ); - else - cmp.m_Component->SetLibSymbol( symbol ); - - cmp.m_Component->SetLibId( id ); - cmp.m_Screen->Append( cmp.m_Component ); - change = true; - } - } - } - - if( change ) - GetParent()->GetCanvas()->Refresh(); -} - - void DIALOG_EDIT_COMPONENTS_LIBID::AdjustGridColumns( int aWidth ) { // Account for scroll bars diff --git a/eeschema/dialogs/dialog_edit_components_libid_base.cpp b/eeschema/dialogs/dialog_edit_components_libid_base.cpp index 9de8975cd3..c2ac6e6f4e 100644 --- a/eeschema/dialogs/dialog_edit_components_libid_base.cpp +++ b/eeschema/dialogs/dialog_edit_components_libid_base.cpp @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////// -// C++ code generated with wxFormBuilder (version Dec 30 2017) +// C++ code generated with wxFormBuilder (version Oct 26 2018) // http://www.wxformbuilder.org/ // // PLEASE DO *NOT* EDIT THIS FILE! @@ -14,19 +14,19 @@ DIALOG_EDIT_COMPONENTS_LIBID_BASE::DIALOG_EDIT_COMPONENTS_LIBID_BASE( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : DIALOG_SHIM( parent, id, title, pos, size, style ) { this->SetSizeHints( wxDefaultSize, wxDefaultSize ); - + wxBoxSizer* bSizerMain; bSizerMain = new wxBoxSizer( wxVERTICAL ); - + m_grid = new WX_GRID( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 ); - + // Grid m_grid->CreateGrid( 5, 3 ); m_grid->EnableEditing( true ); m_grid->EnableGridLines( true ); m_grid->EnableDragGridSize( false ); m_grid->SetMargins( 0, 0 ); - + // Columns m_grid->SetColSize( 0, 280 ); m_grid->SetColSize( 1, 280 ); @@ -37,77 +37,54 @@ DIALOG_EDIT_COMPONENTS_LIBID_BASE::DIALOG_EDIT_COMPONENTS_LIBID_BASE( wxWindow* m_grid->SetColLabelValue( 0, _("Symbols") ); m_grid->SetColLabelValue( 1, _("Current Library Reference") ); m_grid->SetColLabelValue( 2, _("New Library Reference") ); - m_grid->SetColLabelAlignment( wxALIGN_CENTRE, wxALIGN_CENTRE ); - + m_grid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER ); + // Rows m_grid->EnableDragRowSize( false ); m_grid->SetRowLabelSize( 0 ); - m_grid->SetRowLabelAlignment( wxALIGN_CENTRE, wxALIGN_CENTRE ); - + m_grid->SetRowLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER ); + // Label Appearance - + // Cell Defaults m_grid->SetDefaultCellAlignment( wxALIGN_LEFT, wxALIGN_TOP ); m_grid->SetMinSize( wxSize( -1,300 ) ); - + bSizerMain->Add( m_grid, 1, wxEXPAND|wxTOP|wxRIGHT|wxLEFT, 10 ); - - wxBoxSizer* bSizerMsgWarning; - bSizerMsgWarning = new wxBoxSizer( wxHORIZONTAL ); - - m_staticTextWarning = new wxStaticText( this, wxID_ANY, _("Warning: changes made from this dialog cannot be undone after closing it."), wxDefaultPosition, wxDefaultSize, 0 ); - m_staticTextWarning->Wrap( -1 ); - m_staticTextWarning->SetFont( wxFont( wxNORMAL_FONT->GetPointSize(), wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, wxEmptyString ) ); - - bSizerMsgWarning->Add( m_staticTextWarning, 1, wxALIGN_CENTER_VERTICAL|wxBOTTOM|wxRIGHT|wxTOP, 5 ); - - m_buttonUndo = new wxButton( this, wxID_ANY, _("Undo Changes"), wxDefaultPosition, wxDefaultSize, 0 ); - bSizerMsgWarning->Add( m_buttonUndo, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 ); - - - bSizerMain->Add( bSizerMsgWarning, 0, wxALL|wxEXPAND, 10 ); - - m_staticline2 = new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL ); - bSizerMain->Add( m_staticline2, 0, wxEXPAND|wxRIGHT|wxLEFT, 5 ); - + wxBoxSizer* bSizerButtons; bSizerButtons = new wxBoxSizer( wxHORIZONTAL ); - + m_buttonOrphanItems = new wxButton( this, wxID_ANY, _("Map Orphans"), wxDefaultPosition, wxDefaultSize, 0 ); m_buttonOrphanItems->SetToolTip( _("If some components are orphan (the linked symbol is found nowhere),\ntry to find a candidate having the same name in one of loaded symbol libraries.") ); - + bSizerButtons->Add( m_buttonOrphanItems, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); - - + + bSizerButtons->Add( 0, 0, 1, wxEXPAND, 5 ); - + m_sdbSizer = new wxStdDialogButtonSizer(); m_sdbSizerOK = new wxButton( this, wxID_OK ); m_sdbSizer->AddButton( m_sdbSizerOK ); - m_sdbSizerApply = new wxButton( this, wxID_APPLY ); - m_sdbSizer->AddButton( m_sdbSizerApply ); m_sdbSizerCancel = new wxButton( this, wxID_CANCEL ); m_sdbSizer->AddButton( m_sdbSizerCancel ); m_sdbSizer->Realize(); - + bSizerButtons->Add( m_sdbSizer, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); - - + + bSizerMain->Add( bSizerButtons, 0, wxEXPAND|wxLEFT, 5 ); - - + + this->SetSizer( bSizerMain ); this->Layout(); - + this->Centre( wxBOTH ); - + // Connect Events m_grid->Connect( wxEVT_GRID_CELL_LEFT_DCLICK, wxGridEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::onCellBrowseLib ), NULL, this ); m_grid->Connect( wxEVT_SIZE, wxSizeEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::OnSizeGrid ), NULL, this ); - m_buttonUndo->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::onUndoChangesButton ), NULL, this ); - m_buttonUndo->Connect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::updateUIChangesButton ), NULL, this ); m_buttonOrphanItems->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::onClickOrphansButton ), NULL, this ); - m_sdbSizerApply->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::onApplyButton ), NULL, this ); m_sdbSizerCancel->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::onCancel ), NULL, this ); } @@ -116,10 +93,7 @@ DIALOG_EDIT_COMPONENTS_LIBID_BASE::~DIALOG_EDIT_COMPONENTS_LIBID_BASE() // Disconnect Events m_grid->Disconnect( wxEVT_GRID_CELL_LEFT_DCLICK, wxGridEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::onCellBrowseLib ), NULL, this ); m_grid->Disconnect( wxEVT_SIZE, wxSizeEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::OnSizeGrid ), NULL, this ); - m_buttonUndo->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::onUndoChangesButton ), NULL, this ); - m_buttonUndo->Disconnect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::updateUIChangesButton ), NULL, this ); m_buttonOrphanItems->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::onClickOrphansButton ), NULL, this ); - m_sdbSizerApply->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::onApplyButton ), NULL, this ); m_sdbSizerCancel->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::onCancel ), NULL, this ); - + } diff --git a/eeschema/dialogs/dialog_edit_components_libid_base.fbp b/eeschema/dialogs/dialog_edit_components_libid_base.fbp index 48dac03b2e..2551046675 100644 --- a/eeschema/dialogs/dialog_edit_components_libid_base.fbp +++ b/eeschema/dialogs/dialog_edit_components_libid_base.fbp @@ -1,6 +1,6 @@ - + C++ @@ -14,6 +14,7 @@ dialog_edit_components_libid_base 1000 none + 1 dialog_edit_components_libid_base @@ -52,42 +53,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bSizerMain @@ -119,10 +84,10 @@ wxALIGN_TOP 0 1 - wxALIGN_CENTRE + wxALIGN_CENTER 22 "Symbols" "Current Library Reference" "New Library Reference" - wxALIGN_CENTRE + wxALIGN_CENTER 3 280,280,280 @@ -165,10 +130,10 @@ 1 Resizable - wxALIGN_CENTRE + wxALIGN_CENTER 0 - wxALIGN_CENTRE + wxALIGN_CENTER 5 1 @@ -179,324 +144,8 @@ - - - - - onCellBrowseLib - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - OnSizeGrid - - - - - 10 - wxALL|wxEXPAND - 0 - - - bSizerMsgWarning - wxHORIZONTAL - none - - 5 - wxALIGN_CENTER_VERTICAL|wxBOTTOM|wxRIGHT|wxTOP - 1 - - 1 - 1 - 1 - 1 - - - - - - - - 1 - 0 - 1 - - 1 - 0 - Dock - 0 - Left - 1 - - 1 - ,90,92,-1,70,0 - 0 - 0 - wxID_ANY - Warning: changes made from this dialog cannot be undone after closing it. - - 0 - - - 0 - - 1 - m_staticTextWarning - 1 - - - protected - 1 - - Resizable - 1 - - - - 0 - - - - - -1 - - - - - - - - - - - - - - - - - - - - - - - - - - - 5 - wxALIGN_CENTER_VERTICAL|wxALL - 0 - - 1 - 1 - 1 - 1 - - - - - - - - 1 - 0 - 1 - - 1 - 0 - 0 - Dock - 0 - Left - 1 - - 1 - - 0 - 0 - wxID_ANY - Undo Changes - - 0 - - - 0 - - 1 - m_buttonUndo - 1 - - - protected - 1 - - Resizable - 1 - - - - 0 - - - wxFILTER_NONE - wxDefaultValidator - - - - - onUndoChangesButton - - - - - - - - - - - - - - - - - - - - - - - updateUIChangesButton - - - - - - 5 - wxEXPAND|wxRIGHT|wxLEFT - 0 - - 1 - 1 - 1 - 1 - - - - - - - - 1 - 0 - 1 - - 1 - 0 - Dock - 0 - Left - 1 - - 1 - - 0 - 0 - wxID_ANY - - 0 - - - 0 - - 1 - m_staticline2 - 1 - - - protected - 1 - - Resizable - 1 - - wxLI_HORIZONTAL - ; forward_declare - 0 - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -523,25 +172,31 @@ + 1 0 1 1 + 0 0 + Dock 0 Left 1 1 + 0 0 wxID_ANY Map Orphans + + 0 0 @@ -556,6 +211,8 @@ protected 1 + + Resizable 1 @@ -571,29 +228,6 @@ onClickOrphansButton - - - - - - - - - - - - - - - - - - - - - - - @@ -611,7 +245,7 @@ wxALL|wxALIGN_CENTER_VERTICAL 0 - 1 + 0 1 0 0 @@ -622,14 +256,7 @@ m_sdbSizer protected - onApplyButton onCancel - - - - - - diff --git a/eeschema/dialogs/dialog_edit_components_libid_base.h b/eeschema/dialogs/dialog_edit_components_libid_base.h index 4b2cd23d46..e0e27838aa 100644 --- a/eeschema/dialogs/dialog_edit_components_libid_base.h +++ b/eeschema/dialogs/dialog_edit_components_libid_base.h @@ -1,12 +1,11 @@ /////////////////////////////////////////////////////////////////////////// -// C++ code generated with wxFormBuilder (version Dec 30 2017) +// C++ code generated with wxFormBuilder (version Oct 26 2018) // http://www.wxformbuilder.org/ // // PLEASE DO *NOT* EDIT THIS FILE! /////////////////////////////////////////////////////////////////////////// -#ifndef __DIALOG_EDIT_COMPONENTS_LIBID_BASE_H__ -#define __DIALOG_EDIT_COMPONENTS_LIBID_BASE_H__ +#pragma once #include #include @@ -20,10 +19,11 @@ class WX_GRID; #include #include #include -#include +#include +#include +#include #include #include -#include #include /////////////////////////////////////////////////////////////////////////// @@ -35,33 +35,25 @@ class WX_GRID; class DIALOG_EDIT_COMPONENTS_LIBID_BASE : public DIALOG_SHIM { private: - + protected: WX_GRID* m_grid; - wxStaticText* m_staticTextWarning; - wxButton* m_buttonUndo; - wxStaticLine* m_staticline2; wxButton* m_buttonOrphanItems; wxStdDialogButtonSizer* m_sdbSizer; wxButton* m_sdbSizerOK; - wxButton* m_sdbSizerApply; wxButton* m_sdbSizerCancel; - + // Virtual event handlers, overide them in your derived class virtual void onCellBrowseLib( wxGridEvent& event ) { event.Skip(); } virtual void OnSizeGrid( wxSizeEvent& event ) { event.Skip(); } - virtual void onUndoChangesButton( wxCommandEvent& event ) { event.Skip(); } - virtual void updateUIChangesButton( wxUpdateUIEvent& event ) { event.Skip(); } virtual void onClickOrphansButton( wxCommandEvent& event ) { event.Skip(); } - virtual void onApplyButton( wxCommandEvent& event ) { event.Skip(); } virtual void onCancel( wxCommandEvent& event ) { event.Skip(); } - - + + public: - - DIALOG_EDIT_COMPONENTS_LIBID_BASE( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = _("Symbol Library References"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 831,426 ), long style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER ); + + DIALOG_EDIT_COMPONENTS_LIBID_BASE( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = _("Symbol Library References"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 831,426 ), long style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER ); ~DIALOG_EDIT_COMPONENTS_LIBID_BASE(); - + }; -#endif //__DIALOG_EDIT_COMPONENTS_LIBID_BASE_H__