From c6a4f25538fc37d8c3c7405123ca8158aae4fdad Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Sun, 11 Jul 2021 20:31:50 +0200 Subject: [PATCH] Use wx events to update simulator title, start tools and menu items grayed out --- eeschema/sim/sim_plot_frame.cpp | 67 ++++++++++------------------ eeschema/sim/sim_plot_frame.h | 9 ++-- eeschema/sim/sim_plot_frame_base.cpp | 4 ++ eeschema/sim/sim_plot_frame_base.fbp | 20 ++++----- eeschema/sim/sim_workbook.cpp | 49 ++++++++++++++------ eeschema/sim/sim_workbook.h | 16 ++++--- 6 files changed, 86 insertions(+), 79 deletions(-) diff --git a/eeschema/sim/sim_plot_frame.cpp b/eeschema/sim/sim_plot_frame.cpp index 75d140158d..e027d688b9 100644 --- a/eeschema/sim/sim_plot_frame.cpp +++ b/eeschema/sim/sim_plot_frame.cpp @@ -185,6 +185,13 @@ SIM_PLOT_FRAME::SIM_PLOT_FRAME( KIWAY* aKiway, wxWindow* aParent ) : m_toolSettings = m_toolBar->AddTool( wxID_ANY, _( "Sim Parameters" ), KiBitmap( BITMAPS::config ), _( "Simulation parameters and settings" ), wxITEM_NORMAL ); + // Start all toolbar buttons except settings as disabled + m_toolSimulate->Enable( false ); + m_toolAddSignals->Enable( false ); + m_toolProbe->Enable( false ); + m_toolTune->Enable( false ); + m_toolSettings->Enable( true ); + Connect( m_toolSimulate->GetId(), wxEVT_UPDATE_UI, wxUpdateUIEventHandler( SIM_PLOT_FRAME::menuSimulateUpdate ), NULL, this ); Connect( m_toolAddSignals->GetId(), wxEVT_UPDATE_UI, @@ -205,6 +212,9 @@ SIM_PLOT_FRAME::SIM_PLOT_FRAME( KIWAY* aKiway, wxWindow* aParent ) : Connect( m_toolSettings->GetId(), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( SIM_PLOT_FRAME::onSettings ), NULL, this ); + Bind( EVT_WORKBOOK_MODIFIED, &SIM_PLOT_FRAME::onWorkbookModified, this ); + Bind( EVT_WORKBOOK_CLR_MODIFIED, &SIM_PLOT_FRAME::onWorkbookClrModified, this ); + // Bind toolbar buttons event to existing menu event handlers, so they behave the same Bind( wxEVT_COMMAND_MENU_SELECTED, &SIM_PLOT_FRAME::onSimulate, this, m_runSimulation->GetId() ); @@ -233,13 +243,11 @@ SIM_PLOT_FRAME::SIM_PLOT_FRAME( KIWAY* aKiway, wxWindow* aParent ) : wxSafeYield(); setSubWindowsSashSize(); - UpdateWindowUI(); - // Ensure the window is on top Raise(); initWorkbook(); - updateFrame(); + updateTitle(); } @@ -368,12 +376,6 @@ void SIM_PLOT_FRAME::updateTitle() } -void SIM_PLOT_FRAME::updateFrame() -{ - updateTitle(); -} - - // A small helper struct to handle bitmaps initialisation in menus struct BM_MENU_INIT_ITEM { @@ -529,7 +531,6 @@ SIM_PANEL_BASE* SIM_PLOT_FRAME::NewPlotPanel( wxString aSimCommand ) m_workbook->AddPage( dynamic_cast( plotPanel ), pageTitle, true ); - updateFrame(); return plotPanel; } @@ -691,7 +692,6 @@ void SIM_PLOT_FRAME::removePlot( const wxString& aPlotName ) updateSignalList(); wxCommandEvent dummy; onCursorUpdate( dummy ); - updateFrame(); } @@ -797,14 +797,12 @@ bool SIM_PLOT_FRAME::updatePlot( const wxString& aName, SIM_PLOT_TYPE aType, con offset += inner; } - updateFrame(); return true; } } m_workbook->AddTrace( aPlotPanel, aName, size, data_x.data(), data_y.data(), aType, aParam ); - updateFrame(); return true; } @@ -924,10 +922,7 @@ bool SIM_PLOT_FRAME::loadWorkbook( const wxString& aPath ) file.GetCurrentLine()+1 ) ) if( !file.Open() ) - { - updateFrame(); return false; - } long plotsCount; @@ -936,7 +931,6 @@ bool SIM_PLOT_FRAME::loadWorkbook( const wxString& aPath ) DISPLAY_LOAD_ERROR( "Error loading workbook: Line %d is not an integer." ); file.Close(); - updateFrame(); return false; } @@ -949,7 +943,6 @@ bool SIM_PLOT_FRAME::loadWorkbook( const wxString& aPath ) DISPLAY_LOAD_ERROR( "Error loading workbook: Line %d is not an integer." ); file.Close(); - updateFrame(); return false; } @@ -969,7 +962,6 @@ bool SIM_PLOT_FRAME::loadWorkbook( const wxString& aPath ) DISPLAY_LOAD_ERROR( "Error loading workbook: Line %d is not an integer." ); file.Close(); - updateFrame(); return false; } @@ -984,7 +976,6 @@ bool SIM_PLOT_FRAME::loadWorkbook( const wxString& aPath ) ); file.Close(); - updateFrame(); return false; } @@ -995,7 +986,6 @@ bool SIM_PLOT_FRAME::loadWorkbook( const wxString& aPath ) DISPLAY_LOAD_ERROR( "Error loading workbook: Line %d is empty." ); file.Close(); - updateFrame(); return false; } @@ -1006,7 +996,6 @@ bool SIM_PLOT_FRAME::loadWorkbook( const wxString& aPath ) DISPLAY_LOAD_ERROR( "Error loading workbook: Line %d is empty." ); file.Close(); - updateFrame(); return false; } @@ -1018,8 +1007,6 @@ bool SIM_PLOT_FRAME::loadWorkbook( const wxString& aPath ) // Successfully loading a workbook does not count as modyfying it. m_workbook->ClrModified(); - - updateFrame(); return true; } @@ -1085,8 +1072,6 @@ bool SIM_PLOT_FRAME::saveWorkbook( const wxString& aPath ) m_simulator->Settings()->SetWorkbookFilename( filename.GetFullName() ); m_workbook->ClrModified(); - updateFrame(); - return res; } @@ -1110,10 +1095,7 @@ void SIM_PLOT_FRAME::menuNewPlot( wxCommandEvent& aEvent ) SIM_TYPE type = m_exporter->GetSimType(); if( SIM_PANEL_BASE::IsPlottable( type ) ) - { NewPlotPanel( m_exporter->GetUsedSimCommand() ); - updateFrame(); - } } @@ -1367,7 +1349,6 @@ void SIM_PLOT_FRAME::onPlotClose( wxAuiNotebookEvent& event ) void SIM_PLOT_FRAME::onPlotClosed( wxAuiNotebookEvent& event ) { - updateFrame(); } @@ -1376,14 +1357,11 @@ void SIM_PLOT_FRAME::onPlotChanged( wxAuiNotebookEvent& event ) updateSignalList(); wxCommandEvent dummy; onCursorUpdate( dummy ); - - updateFrame(); } void SIM_PLOT_FRAME::onPlotDragged( wxAuiNotebookEvent& event ) { - updateFrame(); } @@ -1415,6 +1393,18 @@ void SIM_PLOT_FRAME::onSignalRClick( wxListEvent& event ) } +void SIM_PLOT_FRAME::onWorkbookModified( wxCommandEvent& event ) +{ + updateTitle(); +} + + +void SIM_PLOT_FRAME::onWorkbookClrModified( wxCommandEvent& event ) +{ + updateTitle(); +} + + void SIM_PLOT_FRAME::onSimulate( wxCommandEvent& event ) { if( m_simulator->IsRunning() ) @@ -1472,7 +1462,6 @@ void SIM_PLOT_FRAME::onSettings( wxCommandEvent& event ) } m_simulator->Init(); - updateFrame(); } } @@ -1663,8 +1652,6 @@ void SIM_PLOT_FRAME::onCursorUpdate( wxCommandEvent& event ) m_cursors->SetItem( idx, Y_COL, SPICE_VALUE( coords.y ).ToSpiceString() ); } } - - updateFrame(); } @@ -1672,8 +1659,6 @@ void SIM_PLOT_FRAME::onSimStarted( wxCommandEvent& aEvent ) { m_toolBar->SetToolNormalBitmap( ID_SIM_RUN, KiBitmap( BITMAPS::sim_stop ) ); SetCursor( wxCURSOR_ARROWWAIT ); - - updateFrame(); } @@ -1730,7 +1715,6 @@ void SIM_PLOT_FRAME::onSimFinished( wxCommandEvent& aEvent ) updateSignalList(); plotPanel->GetPlotWin()->UpdateAll(); plotPanel->ResetScales(); - updateFrame(); } else if( simType == ST_OP ) { @@ -1765,7 +1749,6 @@ void SIM_PLOT_FRAME::onSimFinished( wxCommandEvent& aEvent ) } m_simFinished = true; - updateFrame(); } @@ -1789,8 +1772,6 @@ void SIM_PLOT_FRAME::onSimUpdate( wxCommandEvent& aEvent ) applyTuners(); m_simulator->Run(); } - - updateFrame(); } @@ -1798,8 +1779,6 @@ void SIM_PLOT_FRAME::onSimReport( wxCommandEvent& aEvent ) { m_simConsole->AppendText( aEvent.GetString() + "\n" ); m_simConsole->SetInsertionPointEnd(); - - updateFrame(); } diff --git a/eeschema/sim/sim_plot_frame.h b/eeschema/sim/sim_plot_frame.h index f640485bc7..76000d4761 100644 --- a/eeschema/sim/sim_plot_frame.h +++ b/eeschema/sim/sim_plot_frame.h @@ -148,12 +148,6 @@ private: */ void updateTitle(); - /** - * Update the frame to match the changes to the workbook. Should be always called after the - * workbook was modified. - */ - void updateFrame(); - /** * Give icons to menuitems of the main menubar. */ @@ -290,6 +284,9 @@ private: void onSignalDblClick( wxMouseEvent& event ) override; void onSignalRClick( wxListEvent& event ) override; + void onWorkbookModified( wxCommandEvent& event ); + void onWorkbookClrModified( wxCommandEvent& event ); + void onSimulate( wxCommandEvent& event ); void onSettings( wxCommandEvent& event ); void onAddSignal( wxCommandEvent& event ); diff --git a/eeschema/sim/sim_plot_frame_base.cpp b/eeschema/sim/sim_plot_frame_base.cpp index f3352baeb5..ed3d2c9b61 100644 --- a/eeschema/sim/sim_plot_frame_base.cpp +++ b/eeschema/sim/sim_plot_frame_base.cpp @@ -54,17 +54,21 @@ SIM_PLOT_FRAME_BASE::SIM_PLOT_FRAME_BASE( wxWindow* parent, wxWindowID id, const m_simulationMenu = new wxMenu(); m_runSimulation = new wxMenuItem( m_simulationMenu, ID_MENU_RUN_SIM, wxString( _("Run Simulation") ) + wxT('\t') + wxT("Ctrl+R"), wxEmptyString, wxITEM_NORMAL ); m_simulationMenu->Append( m_runSimulation ); + m_runSimulation->Enable( false ); m_simulationMenu->AppendSeparator(); m_addSignals = new wxMenuItem( m_simulationMenu, ID_MENU_ADD_SIGNAL, wxString( _("Add Signals...") ) + wxT('\t') + wxT("Ctrl+A"), wxEmptyString, wxITEM_NORMAL ); m_simulationMenu->Append( m_addSignals ); + m_addSignals->Enable( false ); m_probeSignals = new wxMenuItem( m_simulationMenu, ID_MENU_PROBE_SIGNALS, wxString( _("Probe from schematics") ) + wxT('\t') + wxT("Ctrl+P"), wxEmptyString, wxITEM_NORMAL ); m_simulationMenu->Append( m_probeSignals ); + m_probeSignals->Enable( false ); m_tuneValue = new wxMenuItem( m_simulationMenu, ID_MENU_TUNE_SIGNALS, wxString( _("Tune Component Value") ) + wxT('\t') + wxT("Ctrl+T"), wxEmptyString, wxITEM_NORMAL ); m_simulationMenu->Append( m_tuneValue ); + m_tuneValue->Enable( false ); m_simulationMenu->AppendSeparator(); diff --git a/eeschema/sim/sim_plot_frame_base.fbp b/eeschema/sim/sim_plot_frame_base.fbp index 6d69d6e5b0..1055c04dcf 100644 --- a/eeschema/sim/sim_plot_frame_base.fbp +++ b/eeschema/sim/sim_plot_frame_base.fbp @@ -1,7 +1,7 @@ - + C++ 1 @@ -27,7 +27,7 @@ UI 0 0 - + 0 wxAUI_MGR_DEFAULT @@ -54,7 +54,7 @@ SIM_PLOT_FRAME wxTAB_TRAVERSAL 1 - + 1 @@ -76,7 +76,7 @@ - + File m_fileMenu protected @@ -191,14 +191,14 @@ menuExit - + Simulation m_simulationMenu protected 0 - 1 + 0 ID_MENU_RUN_SIM wxITEM_NORMAL @@ -216,7 +216,7 @@ 0 - 1 + 0 ID_MENU_ADD_SIGNAL wxITEM_NORMAL @@ -230,7 +230,7 @@ 0 - 1 + 0 ID_MENU_PROBE_SIGNALS wxITEM_NORMAL @@ -244,7 +244,7 @@ 0 - 1 + 0 ID_MENU_TUNE_SIGNALS wxITEM_NORMAL @@ -290,7 +290,7 @@ - + View m_viewMenu protected diff --git a/eeschema/sim/sim_workbook.cpp b/eeschema/sim/sim_workbook.cpp index 6c39c89ef7..c074f443cd 100644 --- a/eeschema/sim/sim_workbook.cpp +++ b/eeschema/sim/sim_workbook.cpp @@ -40,42 +40,63 @@ SIM_WORKBOOK::SIM_WORKBOOK( wxWindow* aParent, wxWindowID aId, const wxPoint& aP bool SIM_WORKBOOK::AddPage( wxWindow* page, const wxString& caption, bool select, const wxBitmap& bitmap ) { - m_modified = true; - return wxAuiNotebook::AddPage( page, caption, select, bitmap ); + bool res = wxAuiNotebook::AddPage( page, caption, select, bitmap ); + setModified( res ); + return res; } bool SIM_WORKBOOK::AddPage( wxWindow* page, const wxString& text, bool select, int imageId ) { - m_modified = true; - return wxAuiNotebook::AddPage( page, text, select, imageId ); + bool res = wxAuiNotebook::AddPage( page, text, select, imageId ); + setModified( res ); + return res; } bool SIM_WORKBOOK::DeleteAllPages() { - m_modified = true; - return wxAuiNotebook::DeleteAllPages(); + bool res = wxAuiNotebook::DeleteAllPages(); + setModified( res ); + return res; } bool SIM_WORKBOOK::DeletePage( size_t page ) { - m_modified = true; - return wxAuiNotebook::DeletePage( page ); + bool res = wxAuiNotebook::DeletePage( page ); + setModified( res ); + return res; } -void SIM_WORKBOOK::AddTrace( SIM_PLOT_PANEL* aPlotPanel, const wxString& aName, int aPoints, const +bool SIM_WORKBOOK::AddTrace( SIM_PLOT_PANEL* aPlotPanel, const wxString& aName, int aPoints, const double* aX, const double* aY, SIM_PLOT_TYPE aType, const wxString& aParam ) { - aPlotPanel->addTrace( aName, aPoints, aX, aY, aType, aParam ); - m_modified = true; + bool res = aPlotPanel->addTrace( aName, aPoints, aX, aY, aType, aParam ); + setModified( res ); + return res; } -void SIM_WORKBOOK::DeleteTrace( SIM_PLOT_PANEL* aPlotPanel, const wxString& aName ) +bool SIM_WORKBOOK::DeleteTrace( SIM_PLOT_PANEL* aPlotPanel, const wxString& aName ) { - aPlotPanel->deleteTrace( aName ); - m_modified = true; + bool res = aPlotPanel->deleteTrace( aName ); + setModified( res ); + return res; } + +void SIM_WORKBOOK::ClrModified() +{ + m_modified = false; + wxPostEvent( GetParent(), wxCommandEvent( EVT_WORKBOOK_CLR_MODIFIED ) ); +} + +void SIM_WORKBOOK::setModified( bool value ) +{ + m_modified = value; + wxPostEvent( GetParent(), wxCommandEvent( EVT_WORKBOOK_MODIFIED ) ); +} + +wxDEFINE_EVENT( EVT_WORKBOOK_MODIFIED, wxCommandEvent ); +wxDEFINE_EVENT( EVT_WORKBOOK_CLR_MODIFIED, wxCommandEvent ); diff --git a/eeschema/sim/sim_workbook.h b/eeschema/sim/sim_workbook.h index 866b320d19..09bc235e1b 100644 --- a/eeschema/sim/sim_workbook.h +++ b/eeschema/sim/sim_workbook.h @@ -25,7 +25,7 @@ #ifndef __SIM_WORKBOOK__ #define __SIM_WORKBOOK__ -#include "dialog_sim_settings.h" +#include #include #include @@ -48,13 +48,14 @@ public: // Custom methods - void AddTrace( SIM_PLOT_PANEL* aPlotPanel, const wxString& aName, int aPoints, const double* - aX, const double* aY, SIM_PLOT_TYPE aType, const wxString& aParam ); - void DeleteTrace( SIM_PLOT_PANEL* aPlotPanel, const wxString& aName ); + bool AddTrace( SIM_PLOT_PANEL* aPlotPanel, const wxString& aName, int aPoints, const double* + aX, const double* aY, SIM_PLOT_TYPE aType, const wxString& aParam ); + bool DeleteTrace( SIM_PLOT_PANEL* aPlotPanel, const wxString& aName ); void SetSimCommand( SIM_PANEL_BASE* aPlotPanel, const wxString& aSimCommand ) { aPlotPanel->setSimCommand( aSimCommand ); + setModified(); } const wxString& GetSimCommand( const SIM_PANEL_BASE* aPlotPanel ) @@ -62,12 +63,17 @@ public: return aPlotPanel->getSimCommand(); } - void ClrModified() { m_modified = false; } + void ClrModified(); bool IsModified() const { return m_modified; } private: + void setModified( bool value = true ); + ///< Dirty bit, indicates something in the workbook has changed bool m_modified; }; +wxDECLARE_EVENT( EVT_WORKBOOK_MODIFIED, wxCommandEvent ); +wxDECLARE_EVENT( EVT_WORKBOOK_CLR_MODIFIED, wxCommandEvent ); + #endif // __SIM_WORKBOOK__