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 class SIM_PLOT_PANEL;
56 class TUNER_SLIDER;
57 
58 
59 // Identifiers (indexes) for color choice in color table
61 {
66  SIM_TRACE_COLOR // First index for trace colors list
67 };
68 
69 
72 {
73 public:
74  TRACE_DESC( const NETLIST_EXPORTER_PSPICE_SIM& aExporter, const wxString& aName,
75  SIM_PLOT_TYPE aType, const wxString& aParam );
76 
79  const TRACE_DESC& aDescription, SIM_PLOT_TYPE aNewType )
80  : TRACE_DESC( aExporter, aDescription.GetName(), aNewType, aDescription.GetParam() )
81  {
82  }
83 
84  const wxString& GetTitle() const
85  {
86  return m_title;
87  }
88 
89  const wxString& GetName() const
90  {
91  return m_name;
92  }
93 
94  const wxString& GetParam() const
95  {
96  return m_param;
97  }
98 
100  {
101  return m_type;
102  }
103 
104 private:
105  // Three basic parameters
107  wxString m_name;
108 
111 
113  wxString m_param;
114 
115  // Generated data
117  wxString m_title;
118 };
119 
122 {
123 public:
125  SIM_PLOT_FRAME( KIWAY* aKiway, wxWindow* aParent );
126  ~SIM_PLOT_FRAME();
127 
128  void StartSimulation();
129  void StopSimulation();
130  bool IsSimulationRunning();
131 
138  SIM_PLOT_PANEL* NewPlotPanel( SIM_TYPE aSimType );
139 
144  void AddVoltagePlot( const wxString& aNetName );
145 
151  void AddCurrentPlot( const wxString& aDeviceName, const wxString& aParam );
152 
156  void AddTuner( SCH_COMPONENT* aComponent );
157 
164  void RemoveTuner( TUNER_SLIDER* aTuner, bool aErase = true );
165 
169  SIM_PLOT_PANEL* CurrentPlot() const;
170 
175 
180  bool GetPlotBgOpt() const { return m_plotUseWhiteBg; }
181 
186  wxColor GetPlotColor( int aColorId );
187 
191  int GetPlotColorCount() { return m_colorList.size(); }
192 
193  void LoadSettings( APP_SETTINGS_BASE* aCfg ) override;
194 
195  void SaveSettings( APP_SETTINGS_BASE* aCfg ) override;
196 
198 
199 private:
200 
203  void setIconsForMenuItems();
204 
209  void fillDefaultColorList( bool aWhiteBg );
210 
217  void addPlot( const wxString& aName, SIM_PLOT_TYPE aType, const wxString& aParam );
218 
224  void removePlot( const wxString& aPlotName, bool aErase = true );
225 
229  void updateNetlistExporter();
230 
238  bool updatePlot( const TRACE_DESC& aDescriptor, SIM_PLOT_PANEL* aPanel );
239 
243  void updateSignalList();
244 
248  void updateCursors();
249 
254  void updateTuners();
255 
259  void applyTuners();
260 
266  bool loadWorkbook( const wxString& aPath );
267 
273  bool saveWorkbook( const wxString& aPath );
274 
278  SIM_PLOT_TYPE GetXAxisType( SIM_TYPE aType ) const;
279 
280  // Menu handlers
281  void menuNewPlot( wxCommandEvent& aEvent ) override;
282  void menuOpenWorkbook( wxCommandEvent& event ) override;
283  void menuSaveWorkbook( wxCommandEvent& event ) override;
284 
285  void menuExit( wxCommandEvent& event ) override
286  {
287  Close();
288  }
289 
290  void menuSaveImage( wxCommandEvent& event ) override;
291  void menuSaveCsv( wxCommandEvent& event ) override;
292  void menuZoomIn( wxCommandEvent& event ) override;
293  void menuZoomOut( wxCommandEvent& event ) override;
294  void menuZoomFit( wxCommandEvent& event ) override;
295  void menuShowGrid( wxCommandEvent& event ) override;
296  void menuShowGridUpdate( wxUpdateUIEvent& event ) override;
297  void menuShowLegend( wxCommandEvent& event ) override;
298  void menuShowLegendUpdate( wxUpdateUIEvent& event ) override;
299  void menuShowDotted( wxCommandEvent& event ) override;
300  void menuShowDottedUpdate( wxUpdateUIEvent& event ) override;
301  void menuWhiteBackground( wxCommandEvent& event ) override;
302  void menuShowWhiteBackgroundUpdate( wxUpdateUIEvent& event ) override
303  {
304  event.Check( m_plotUseWhiteBg );
305  }
306 
307  // Event handlers
308  void onPlotChanged( wxAuiNotebookEvent& event ) override;
309  void onPlotClose( wxAuiNotebookEvent& event ) override;
310 
311  void onSignalDblClick( wxMouseEvent& event ) override;
312  void onSignalRClick( wxListEvent& event ) override;
313 
314  void onSimulate( wxCommandEvent& event );
315  void onSettings( wxCommandEvent& event );
316  void onAddSignal( wxCommandEvent& event );
317  void onProbe( wxCommandEvent& event );
318  void onTune( wxCommandEvent& event );
319  void onShowNetlist( wxCommandEvent& event );
320 
321  void onClose( wxCloseEvent& aEvent );
322 
323  void onCursorUpdate( wxCommandEvent& aEvent );
324  void onSimUpdate( wxCommandEvent& aEvent );
325  void onSimReport( wxCommandEvent& aEvent );
326  void onSimStarted( wxCommandEvent& aEvent );
327  void onSimFinished( wxCommandEvent& aEvent );
328 
329  // adjust the sash dimension of splitter windows after reading
330  // the config settings
331  // must be called after the config settings are read, and once the
332  // frame is initialized (end of the Ctor)
333  void setSubWindowsSashSize();
334 
335  // Toolbar buttons
336  wxToolBarToolBase* m_toolSimulate;
337  wxToolBarToolBase* m_toolAddSignals;
338  wxToolBarToolBase* m_toolProbe;
339  wxToolBarToolBase* m_toolTune;
340  wxToolBarToolBase* m_toolSettings;
341 
343  std::unique_ptr<NETLIST_EXPORTER_PSPICE_SIM> m_exporter;
344  std::shared_ptr<SPICE_SIMULATOR> m_simulator;
346 
347  typedef std::map<wxString, TRACE_DESC> TRACE_MAP;
348 
349  struct PLOT_INFO
350  {
353 
355  wxString m_simCommand;
356  };
357 
359  std::map<SIM_PLOT_PANEL*, PLOT_INFO> m_plots;
360 
362  std::list<TUNER_SLIDER*> m_tuners;
363 
364  // Trick to preserve settings between runs:
365  // the DIALOG_SIM_SETTINGS is not destroyed after closing the dialog.
366  // Once created it will be not shown (shown only on request) during a session
367  // and will be destroyed only when closing the simulator frame.
369 
370  // Right click context menu for signals in the listbox
371  class SIGNAL_CONTEXT_MENU : public wxMenu
372  {
373  public:
374  SIGNAL_CONTEXT_MENU( const wxString& aSignal, SIM_PLOT_FRAME* aPlotFrame );
375 
376  private:
377  void onMenuEvent( wxMenuEvent& aEvent );
378 
379  const wxString& m_signal;
381 
383  {
387  };
388  };
389 
392 
396 
398  static wxString m_savedWorkbooksPath;
399 
400  // Variables for temporary storage:
406 
408  std::vector<wxColour> m_colorList;
409 };
410 
411 // Commands
412 wxDECLARE_EVENT( EVT_SIM_UPDATE, wxCommandEvent );
413 wxDECLARE_EVENT( EVT_SIM_REPORT, wxCommandEvent );
414 
415 // Notifications
416 wxDECLARE_EVENT( EVT_SIM_STARTED, wxCommandEvent );
417 wxDECLARE_EVENT( EVT_SIM_FINISHED, wxCommandEvent );
418 
419 #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
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
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
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.
void updateCursors()
Updates the cursor values list.
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
const wxString & GetParam() const
SCH_EDIT_FRAME * m_schematicFrame
void setSubWindowsSashSize()
Stores the common settings that are saved and loaded for each window / frame.
Definition: app_settings.h:52
std::map< SIM_PLOT_PANEL *, PLOT_INFO > m_plots
Map of plot panels and associated data
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:75
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)
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)
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
SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:99
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:35
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 onClose(wxCloseEvent &aEvent)
void LoadSettings(APP_SETTINGS_BASE *aCfg) override
Load common frame parameters from a configuration file.
wxToolBarToolBase * m_toolTune
void onMenuEvent(wxMenuEvent &aEvent)
SIM_PLOT_PANEL * NewPlotPanel(SIM_TYPE aSimType)
Creates a new plot panel for a given simulation type and adds it to the main notebook.
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.