diff --git a/eeschema/dialogs/dialog_sim_model.cpp b/eeschema/dialogs/dialog_sim_model.cpp index 93ada009b0..ace7ad4229 100644 --- a/eeschema/dialogs/dialog_sim_model.cpp +++ b/eeschema/dialogs/dialog_sim_model.cpp @@ -35,10 +35,14 @@ #include #include #include +#include "grid_tricks.h" using CATEGORY = SIM_MODEL::PARAM::CATEGORY; +#define USED_PIN_PREFIX wxT( "⇤" ) + + template DIALOG_SIM_MODEL::DIALOG_SIM_MODEL( wxWindow* aParent, SCH_SYMBOL& aSymbol, std::vector& aFields ) @@ -112,11 +116,21 @@ DIALOG_SIM_MODEL::DIALOG_SIM_MODEL( wxWindow* aParent, SCH_SYMBOL& aSymbol, wxFAIL; } + m_pinAssignmentsGrid->PushEventHandler( new GRID_TRICKS( m_pinAssignmentsGrid ) ); + // Now all widgets have the size fixed, call FinishDialogSettings finishDialogSettings(); } +template +DIALOG_SIM_MODEL::~DIALOG_SIM_MODEL() +{ + // Delete the GRID_TRICKS. + m_pinAssignmentsGrid->PopEventHandler( true ); +} + + template bool DIALOG_SIM_MODEL::TransferDataToWindow() { @@ -538,8 +552,6 @@ void DIALOG_SIM_MODEL::updatePinAssignments() modelPinString ); } - wxArrayString modelPinChoices = getModelPinChoices(); - // Set up the Symbol column grid values and Model column cell editors with dropdown options. for( int i = 0; i < m_pinAssignmentsGrid->GetNumberRows(); ++i ) { @@ -552,15 +564,12 @@ void DIALOG_SIM_MODEL::updatePinAssignments() wxString curModelPinString = m_pinAssignmentsGrid->GetCellValue( i, static_cast( PIN_COLUMN::MODEL ) ); - wxArrayString actualModelPinChoices( modelPinChoices ); - - if( curModelPinString != _( "Not Connected" ) ) - actualModelPinChoices.Insert( curModelPinString, 0 ); + wxArrayString modelPinChoices = getModelPinChoices( curModelPinString ); // Using `new` here shouldn't cause a memory leak because `SetCellEditor()` calls // `DecRef()` on its last editor. m_pinAssignmentsGrid->SetCellEditor( i, static_cast( PIN_COLUMN::MODEL ), - new wxGridCellChoiceEditor( actualModelPinChoices ) ); + new wxGridCellChoiceEditor( modelPinChoices ) ); } // TODO: Show a preview of the symbol with the pin numbers shown. @@ -935,18 +944,21 @@ int DIALOG_SIM_MODEL::getModelPinIndex( const wxString& aModelPinString ) con template -wxArrayString DIALOG_SIM_MODEL::getModelPinChoices() const +wxArrayString DIALOG_SIM_MODEL::getModelPinChoices( const wxString& aCurrentValue ) const { wxArrayString modelPinChoices; for( int i = 0; i < curModel().GetPinCount(); ++i ) { const SIM_MODEL::PIN& modelPin = curModel().GetPin( i ); + const wxString& pinString = getModelPinString( i ); - if( modelPin.symbolPinNumber != "" ) - continue; - - modelPinChoices.Add( getModelPinString( i ) ); + if( pinString == aCurrentValue ) + modelPinChoices.Add( pinString ); + else if( modelPin.symbolPinNumber != "" ) + modelPinChoices.Add( wxString( USED_PIN_PREFIX ) + wxS( " " ) + pinString ); + else + modelPinChoices.Add( pinString ); } modelPinChoices.Add( _( "Not Connected" ) ); @@ -1193,10 +1205,18 @@ void DIALOG_SIM_MODEL::onCodePreviewSetFocus( wxFocusEvent& aEvent ) template void DIALOG_SIM_MODEL::onPinAssignmentsGridCellChange( wxGridEvent& aEvent ) { - int symbolPinIndex = aEvent.GetRow(); - int oldModelPinIndex = getModelPinIndex( aEvent.GetString() ); - int modelPinIndex = getModelPinIndex( - m_pinAssignmentsGrid->GetCellValue( aEvent.GetRow(), aEvent.GetCol() ) ); + int symbolPinIndex = aEvent.GetRow(); + wxString oldModelPinName = aEvent.GetString(); + wxString modelPinName = m_pinAssignmentsGrid->GetCellValue( aEvent.GetRow(), aEvent.GetCol() ); + + if( oldModelPinName.StartsWith( USED_PIN_PREFIX ) ) + oldModelPinName = oldModelPinName.AfterFirst( ' ' ); + + if( modelPinName.StartsWith( USED_PIN_PREFIX ) ) + modelPinName = modelPinName.AfterFirst( ' ' ); + + int oldModelPinIndex = getModelPinIndex( oldModelPinName ); + int modelPinIndex = getModelPinIndex( modelPinName ); if( oldModelPinIndex != SIM_MODEL::PIN::NOT_CONNECTED ) curModel().SetPinSymbolPinNumber( oldModelPinIndex, "" ); diff --git a/eeschema/dialogs/dialog_sim_model.h b/eeschema/dialogs/dialog_sim_model.h index 42386c295f..c57b253424 100644 --- a/eeschema/dialogs/dialog_sim_model.h +++ b/eeschema/dialogs/dialog_sim_model.h @@ -71,6 +71,8 @@ public: DIALOG_SIM_MODEL( wxWindow* aParent, SCH_SYMBOL& aSymbol, std::vector& aSchFields ); + ~DIALOG_SIM_MODEL(); + private: bool TransferDataToWindow() override; bool TransferDataFromWindow() override; @@ -96,7 +98,7 @@ private: wxString getSymbolPinString( int aSymbolPinNumber ) const; wxString getModelPinString( int aModelPinIndex ) const; int getModelPinIndex( const wxString& aModelPinString ) const; - wxArrayString getModelPinChoices() const; + wxArrayString getModelPinChoices( const wxString& aCurrentValue ) const; void onRadioButton( wxCommandEvent& aEvent ) override; void onBrowseButtonClick( wxCommandEvent& aEvent ) override;