KiCad PCB EDA Suite
sim_plot_frame.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2016 CERN
5  * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
8  * @author Maciej Suminski <maciej.suminski@cern.ch>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 3
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you may find one here:
22  * https://www.gnu.org/licenses/gpl-3.0.html
23  * or you may search the http://www.gnu.org website for the version 3 license,
24  * or you may write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27 
28 #ifndef __SIM_PLOT_FRAME__
29 #define __SIM_PLOT_FRAME__
30 
37 #include "sim_plot_frame_base.h"
38 #include "sim_types.h"
39 
40 #include <kiway_player.h>
42 
43 #include <wx/event.h>
44 
45 #include <list>
46 #include <memory>
47 #include <map>
48 
49 class SCH_EDIT_FRAME;
50 class SCH_COMPONENT;
51 
52 class SPICE_SIMULATOR;
54 
55 #include "sim_plot_panel.h"
56 #include "sim_panel_base.h"
57 
59 class TUNER_SLIDER;
60 
61 
62 // Identifiers (indexes) for color choice in color table
64 {
69  SIM_TRACE_COLOR // First index for trace colors list
70 };
71 
72 
75 {
76 public:
77  TRACE_DESC( const NETLIST_EXPORTER_PSPICE_SIM& aExporter, const wxString& aName,
78  SIM_PLOT_TYPE aType, const wxString& aParam );
79 
82  const TRACE_DESC& aDescription, SIM_PLOT_TYPE aNewType )
83  : TRACE_DESC( aExporter, aDescription.GetName(), aNewType, aDescription.GetParam() )
84  {
85  }
86 
87  const wxString& GetTitle() const
88  {
89  return m_title;
90  }
91 
92  const wxString& GetName() const
93  {
94  return m_name;
95  }
96 
97  const wxString& GetParam() const
98  {
99  return m_param;
100  }
101 
103  {
104  return m_type;
105  }
106 
107 private:
108  // Three basic parameters
110  wxString m_name;
111 
114 
116  wxString m_param;
117 
118  // Generated data
120  wxString m_title;
121 };
122 
123 
126 {
127 public:
129  SIM_PLOT_FRAME( KIWAY* aKiway, wxWindow* aParent );
130  ~SIM_PLOT_FRAME();
131 
132  void StartSimulation( const wxString& aSimCommand = wxEmptyString );
133  void StopSimulation();
134  bool IsSimulationRunning();
135 
142  SIM_PANEL_BASE* NewPlotPanel( SIM_TYPE aSimType );
143 
148  void AddVoltagePlot( const wxString& aNetName );
149 
155  void AddCurrentPlot( const wxString& aDeviceName, const wxString& aParam );
156 
160  void AddTuner( SCH_COMPONENT* aComponent );
161 
168  void RemoveTuner( TUNER_SLIDER* aTuner, bool aErase = true );
169 
173  SIM_PLOT_PANEL* CurrentPlot() const;
174 
179 
184  bool GetPlotBgOpt() const { return m_plotUseWhiteBg; }
185 
190  wxColor GetPlotColor( int aColorId );
191 
195  int GetPlotColorCount() { return m_colorList.size(); }
196 
197  void LoadSettings( APP_SETTINGS_BASE* aCfg ) override;
198 
199  void SaveSettings( APP_SETTINGS_BASE* aCfg ) override;
200 
202 
203  // Simulator doesn't host a tool framework
204  wxWindow* GetToolCanvas() const override { return nullptr; }
205 
206 private:
207 
210  void setIconsForMenuItems();
211 
216  void fillDefaultColorList( bool aWhiteBg );
217 
222  {
223  return dynamic_cast<SIM_PANEL_BASE*>( m_plotNotebook->GetCurrentPage() );
224  }
225 
232  void addPlot( const wxString& aName, SIM_PLOT_TYPE aType, const wxString& aParam );
233 
239  void removePlot( const wxString& aPlotName, bool aErase = true );
240 
244  void updateNetlistExporter();
245 
253  bool updatePlot( const TRACE_DESC& aDescriptor, SIM_PLOT_PANEL* aPanel );
254 
258  void updateSignalList();
259 
264  void updateTuners();
265 
269  void applyTuners();
270 
276  bool loadWorkbook( const wxString& aPath );
277 
283  bool saveWorkbook( const wxString& aPath );
284 
288  SIM_PLOT_TYPE GetXAxisType( SIM_TYPE aType ) const;
289 
290  // Menu handlers
291  void menuNewPlot( wxCommandEvent& aEvent ) override;
292  void menuOpenWorkbook( wxCommandEvent& event ) override;
293  void menuSaveWorkbook( wxCommandEvent& event ) override;
294 
295  void menuExit( wxCommandEvent& event ) override
296  {
297  Close();
298  }
299 
300  void menuSaveImage( wxCommandEvent& event ) override;
301  void menuSaveCsv( wxCommandEvent& event ) override;
302  void menuZoomIn( wxCommandEvent& event ) override;
303  void menuZoomOut( wxCommandEvent& event ) override;
304  void menuZoomFit( wxCommandEvent& event ) override;
305  void menuShowGrid( wxCommandEvent& event ) override;
306  void menuShowGridUpdate( wxUpdateUIEvent& event ) override;
307  void menuShowLegend( wxCommandEvent& event ) override;
308  void menuShowLegendUpdate( wxUpdateUIEvent& event ) override;
309  void menuShowDotted( wxCommandEvent& event ) override;
310  void menuShowDottedUpdate( wxUpdateUIEvent& event ) override;
311  void menuWhiteBackground( wxCommandEvent& event ) override;
312  void menuShowWhiteBackgroundUpdate( wxUpdateUIEvent& event ) override
313  {
314  event.Check( m_plotUseWhiteBg );
315  }
316 
317  // Event handlers
318  void onPlotChanged( wxAuiNotebookEvent& event ) override;
319  void onPlotClose( wxAuiNotebookEvent& event ) override;
320 
321  void onSignalDblClick( wxMouseEvent& event ) override;
322  void onSignalRClick( wxListEvent& event ) override;
323 
324  void onSimulate( wxCommandEvent& event );
325  void onSettings( wxCommandEvent& event );
326  void onAddSignal( wxCommandEvent& event );
327  void onProbe( wxCommandEvent& event );
328  void onTune( wxCommandEvent& event );
329  void onShowNetlist( wxCommandEvent& event );
330 
331  void doCloseWindow() override;
332 
333  void onCursorUpdate( wxCommandEvent& aEvent );
334  void onSimUpdate( wxCommandEvent& aEvent );
335  void onSimReport( wxCommandEvent& aEvent );
336  void onSimStarted( wxCommandEvent& aEvent );
337  void onSimFinished( wxCommandEvent& aEvent );
338 
339  // adjust the sash dimension of splitter windows after reading
340  // the config settings
341  // must be called after the config settings are read, and once the
342  // frame is initialized (end of the Ctor)
343  void setSubWindowsSashSize();
344 
345  // Toolbar buttons
346  wxToolBarToolBase* m_toolSimulate;
347  wxToolBarToolBase* m_toolAddSignals;
348  wxToolBarToolBase* m_toolProbe;
349  wxToolBarToolBase* m_toolTune;
350  wxToolBarToolBase* m_toolSettings;
351 
353  std::unique_ptr<NETLIST_EXPORTER_PSPICE_SIM> m_exporter;
354  std::shared_ptr<SPICE_SIMULATOR> m_simulator;
356 
357  typedef std::map<wxString, TRACE_DESC> TRACE_MAP;
358 
359  struct PLOT_INFO
360  {
363 
365  wxString m_simCommand;
366  };
367 
369  std::map<SIM_PANEL_BASE*, PLOT_INFO> m_plots;
370 
372  std::list<TUNER_SLIDER*> m_tuners;
373 
374  // Trick to preserve settings between runs:
375  // the DIALOG_SIM_SETTINGS is not destroyed after closing the dialog.
376  // Once created it will be not shown (shown only on request) during a session
377  // and will be destroyed only when closing the simulator frame.
379 
380  // Right click context menu for signals in the listbox
381  class SIGNAL_CONTEXT_MENU : public wxMenu
382  {
383  public:
384  SIGNAL_CONTEXT_MENU( const wxString& aSignal, SIM_PLOT_FRAME* aPlotFrame );
385 
386  private:
387  void onMenuEvent( wxMenuEvent& aEvent );
388 
389  const wxString& m_signal;
391 
393  {
397  };
398  };
399 
402 
406 
408  static wxString m_savedWorkbooksPath;
409 
412 
413  // Variables for temporary storage:
419  unsigned int m_plotNumber;
420 
422  std::vector<wxColour> m_colorList;
423 };
424 
425 // Commands
426 wxDECLARE_EVENT( EVT_SIM_UPDATE, wxCommandEvent );
427 wxDECLARE_EVENT( EVT_SIM_REPORT, wxCommandEvent );
428 
429 // Notifications
430 wxDECLARE_EVENT( EVT_SIM_STARTED, wxCommandEvent );
431 wxDECLARE_EVENT( EVT_SIM_FINISHED, wxCommandEvent );
432 
433 #endif // __sim_plot_frame__
void onAddSignal(wxCommandEvent &event)
wxString m_simCommand
Spice directive used to execute the simulation
bool saveWorkbook(const wxString &aPath)
Saves plot settings to a file.
wxToolBarToolBase * m_toolProbe
void setIconsForMenuItems()
Give icons to menuitems of the main menubar.
void onSimStarted(wxCommandEvent &aEvent)
void RemoveTuner(TUNER_SLIDER *aTuner, bool aErase=true)
Removes an existing tuner.
bool GetPlotBgOpt() const
wxImageList * m_signalsIconColorList
imagelists uset to add a small coloured icon to signal names and cursors name, the same color as the ...
void onPlotChanged(wxAuiNotebookEvent &event) override
void menuWhiteBackground(wxCommandEvent &event) override
void onSignalDblClick(wxMouseEvent &event) override
const wxString & GetTitle() const
SIM_PANEL_BASE * NewPlotPanel(SIM_TYPE aSimType)
Creates a new plot panel for a given simulation type and adds it to the main notebook.
int m_splitterPlotAndConsoleSashPosition
void menuZoomFit(wxCommandEvent &event) override
TRACE_DESC(const NETLIST_EXPORTER_PSPICE_SIM &aExporter, const TRACE_DESC &aDescription, SIM_PLOT_TYPE aNewType)
Modifies an existing TRACE_DESC simulation type
std::list< TUNER_SLIDER * > m_tuners
List of currently displayed tuners
void doCloseWindow() override
SIM_PLOT_TYPE GetType() const
wxString m_title
Title displayed in the signal list/plot legend
Class SIM_PLOT_FRAME_BASE.
WINDOW_SETTINGS * GetWindowSettings(APP_SETTINGS_BASE *aCfg) override
Returns a pointer to the window settings for this frame.
void menuNewPlot(wxCommandEvent &aEvent) override
void applyTuners()
Applies component values specified using tunder sliders to the current netlist.
void AddVoltagePlot(const wxString &aNetName)
Adds a voltage plot for a given net name.
void AddCurrentPlot(const wxString &aDeviceName, const wxString &aParam)
Adds a current plot for a particular device.
Trace descriptor class
Schematic editor (Eeschema) main window.
void onSimUpdate(wxCommandEvent &aEvent)
void menuOpenWorkbook(wxCommandEvent &event) override
void menuShowGrid(wxCommandEvent &event) override
void menuZoomIn(wxCommandEvent &event) override
std::map< SIM_PANEL_BASE *, PLOT_INFO > m_plots
Map of plot panels and associated data
void menuZoomOut(wxCommandEvent &event) override
SIM_THREAD_REPORTER * m_reporter
std::shared_ptr< SPICE_SIMULATOR > m_simulator
bool updatePlot(const TRACE_DESC &aDescriptor, SIM_PLOT_PANEL *aPanel)
Updates plot in a particular SIM_PLOT_PANEL.
SIM_PANEL_BASE * m_welcomePanel
Info panel
SIM_PLOT_TYPE GetXAxisType(SIM_TYPE aType) const
Returns X axis for a given simulation type.
void menuSaveCsv(wxCommandEvent &event) override
void onPlotClose(wxAuiNotebookEvent &event) override
void StartSimulation(const wxString &aSimCommand=wxEmptyString)
const wxString & GetParam() const
wxAuiNotebook * m_plotNotebook
SCH_EDIT_FRAME * m_schematicFrame
void setSubWindowsSashSize()
Stores the common settings that are saved and loaded for each window / frame.
Definition: app_settings.h:81
SIM_PLOT_PANEL * m_lastSimPlot
Panel that was used as the most recent one for simulations
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:99
wxColor GetPlotColor(int aColorId)
void onSimReport(wxCommandEvent &aEvent)
SIM_TYPE
Possible simulation types
Definition: sim_types.h:29
std::vector< wxColour > m_colorList
The color list to draw traces, bg, fg, axis...
void onSimFinished(wxCommandEvent &aEvent)
void menuShowWhiteBackgroundUpdate(wxUpdateUIEvent &event) override
void fillDefaultColorList(bool aWhiteBg)
Fills m_colorList by a default set of colors.
void onShowNetlist(wxCommandEvent &event)
void menuShowLegendUpdate(wxUpdateUIEvent &event) override
KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the s...
Definition: kiway.h:273
wxToolBarToolBase * m_toolAddSignals
int m_splitterTuneValuesSashPosition
void SaveSettings(APP_SETTINGS_BASE *aCfg) override
Saves common frame parameters to a configuration data file.
TRACE_DESC(const NETLIST_EXPORTER_PSPICE_SIM &aExporter, const wxString &aName, SIM_PLOT_TYPE aType, const wxString &aParam)
SIM_PANEL_BASE * currentPlotWindow() const
Returns the currently opened plot panel (or NULL if there is none).
void onSignalRClick(wxListEvent &event) override
bool IsSimulationRunning()
void menuShowLegend(wxCommandEvent &event) override
void onTune(wxCommandEvent &event)
wxToolBarToolBase * m_toolSimulate
void menuSaveImage(wxCommandEvent &event) override
Implementing SIM_PLOT_FRAME_BASE.
bool loadWorkbook(const wxString &aPath)
Loads plot settings from a file.
static wxString m_savedWorkbooksPath
A string to store the path of saved workbooks during a session
void onCursorUpdate(wxCommandEvent &aEvent)
wxWindow * GetToolCanvas() const override
Canvas access.
DIALOG_SIM_SETTINGS * m_settingsDlg
const wxString & GetName() const
std::unique_ptr< NETLIST_EXPORTER_PSPICE_SIM > m_exporter
void updateNetlistExporter()
Reloads the current schematic for the netlist exporter.
void menuExit(wxCommandEvent &event) override
void onSimulate(wxCommandEvent &event)
wxString m_param
Name of the signal parameter
TRACE_MAP m_traces
Map of the traces displayed on the plot
SIGNAL_CONTEXT_MENU(const wxString &aSignal, SIM_PLOT_FRAME *aPlotFrame)
void onProbe(wxCommandEvent &event)
SIM_COLOR_SET
void updateSignalList()
Updates the list of currently plotted signals.
wxDECLARE_EVENT(EVT_SIM_UPDATE, wxCommandEvent)
SIM_PLOT_TYPE m_type
Type of the signal
unsigned int m_plotNumber
Schematic symbol object.
Definition: sch_component.h:80
int m_splitterSignalsSashPosition
SIM_PLOT_PANEL * CurrentPlot() const
Returns the currently opened plot panel (or NULL if there is none).
std::map< wxString, TRACE_DESC > TRACE_MAP
void menuShowDotted(wxCommandEvent &event) override
int m_splitterLeftRightSashPosition
void menuShowDottedUpdate(wxUpdateUIEvent &event) override
const NETLIST_EXPORTER_PSPICE_SIM * GetExporter() const
Returns the netlist exporter object used for simulations.
void removePlot(const wxString &aPlotName, bool aErase=true)
Removes a plot with a specific title.
Custom widget to handle quick component values modification and simulation on the fly.
Definition: tuner_slider.h:40
void AddTuner(SCH_COMPONENT *aComponent)
Adds a tuner for a component.
void menuShowGridUpdate(wxUpdateUIEvent &event) override
void menuSaveWorkbook(wxCommandEvent &event) override
SIM_PLOT_TYPE
Possible plot types
Definition: sim_types.h:44
void addPlot(const wxString &aName, SIM_PLOT_TYPE aType, const wxString &aParam)
Adds a new plot to the current panel.
void onSettings(wxCommandEvent &event)
void LoadSettings(APP_SETTINGS_BASE *aCfg) override
Load common frame parameters from a configuration file.
wxToolBarToolBase * m_toolTune
void onMenuEvent(wxMenuEvent &aEvent)
Special netlist exporter flavor that allows one to override simulation commands.
void updateTuners()
Filters out tuners for components that do not exist anymore.
wxToolBarToolBase * m_toolSettings
wxString m_name
Name of the measured net/device
SIM_PLOT_FRAME(KIWAY *aKiway, wxWindow *aParent)
Constructor.