You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

374 lines
12 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016-2022 CERN
  5. * Copyright (C) 2017-2022 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. #ifndef __SIM_PLOT_FRAME__
  28. #define __SIM_PLOT_FRAME__
  29. #include "sim_plot_frame_base.h"
  30. #include "sim_types.h"
  31. #include <kiway_player.h>
  32. #include <dialogs/dialog_sim_command.h>
  33. #include <wx/event.h>
  34. #include <list>
  35. #include <memory>
  36. #include <map>
  37. class SCH_EDIT_FRAME;
  38. class SCH_SYMBOL;
  39. class SPICE_SIMULATOR;
  40. class SPICE_SIMULATOR_SETTINGS;
  41. class NGSPICE_CIRCUIT_MODEL;
  42. #include "sim_plot_panel.h"
  43. #include "sim_panel_base.h"
  44. #include "sim_workbook.h"
  45. class SIM_THREAD_REPORTER;
  46. class TUNER_SLIDER;
  47. /**
  48. * Implementing SIM_PLOT_FRAME_BASE
  49. */
  50. class SIM_PLOT_FRAME : public SIM_PLOT_FRAME_BASE
  51. {
  52. public:
  53. SIM_PLOT_FRAME( KIWAY* aKiway, wxWindow* aParent );
  54. ~SIM_PLOT_FRAME();
  55. void StartSimulation( const wxString& aSimCommand = wxEmptyString );
  56. /**
  57. * Create a new plot panel for a given simulation type and adds it to the main notebook.
  58. *
  59. * @param aSimType is requested simulation type.
  60. * @return The new plot panel.
  61. */
  62. SIM_PANEL_BASE* NewPlotPanel( wxString aSimCommand );
  63. /**
  64. * Add a voltage plot for a given net name.
  65. *
  66. * @param aNetName is the net name for which a voltage plot should be created.
  67. */
  68. void AddVoltagePlot( const wxString& aNetName );
  69. /**
  70. * Add a current plot for a particular device.
  71. *
  72. * @param aDeviceName is the device name (e.g. R1, C1).
  73. * @param aParam is the current type (e.g. I, Ic, Id).
  74. */
  75. void AddCurrentPlot( const wxString& aDeviceName );
  76. /**
  77. * Add a tuner for a symbol.
  78. */
  79. void AddTuner( const SCH_SHEET_PATH& aSheetPath, SCH_SYMBOL* aSymbol );
  80. /**
  81. * Remove an existing tuner.
  82. *
  83. * @param aTuner is the tuner to be removed.
  84. * @param aErase decides whether the tuner should be also removed from the tuners list.
  85. * Otherwise it is removed only from the SIM_PLOT_FRAME pane.
  86. */
  87. void RemoveTuner( TUNER_SLIDER* aTuner, bool aErase = true );
  88. /**
  89. * Safely update a field of the associated symbol without dereferencing
  90. * the symbol.
  91. *
  92. * @param aSymbol id of the symbol needing updating
  93. * @param aId id of the symbol field
  94. * @param aValue new value of the symbol field
  95. */
  96. void UpdateTunerValue( const SCH_SHEET_PATH& aSheetPath, const KIID& aSymbol,
  97. const wxString& aRef, const wxString& aValue );
  98. /**
  99. * Return the currently opened plot panel (or NULL if there is none).
  100. */
  101. SIM_PLOT_PANEL* GetCurrentPlot() const;
  102. /**
  103. * Return the netlist exporter object used for simulations.
  104. */
  105. const NGSPICE_CIRCUIT_MODEL* GetExporter() const;
  106. /**
  107. * @return the current background option for plotting.
  108. * false for drak bg, true for clear bg
  109. */
  110. bool GetPlotBgOpt() const { return m_plotUseWhiteBg; }
  111. void LoadSettings( APP_SETTINGS_BASE* aCfg ) override;
  112. void SaveSettings( APP_SETTINGS_BASE* aCfg ) override;
  113. WINDOW_SETTINGS* GetWindowSettings( APP_SETTINGS_BASE* aCfg ) override;
  114. // Simulator doesn't host a tool framework
  115. wxWindow* GetToolCanvas() const override { return nullptr; }
  116. private:
  117. /**
  118. * Load the currently active workbook stored in the project settings. If there is none,
  119. * generate a filename for the currently active workbook and store it in the project settings.
  120. */
  121. void initWorkbook();
  122. /**
  123. * Set the main window title bar text.
  124. */
  125. void updateTitle();
  126. /**
  127. * Give icons to menuitems of the main menubar.
  128. */
  129. void setIconsForMenuItems();
  130. /**
  131. * Add a new plot to the current panel.
  132. *
  133. * @param aName is the device/net name.
  134. * @param aType describes the type of plot.
  135. * @param aParam is the parameter for the device/net (e.g. I, Id, V).
  136. */
  137. void addPlot( const wxString& aName, SIM_PLOT_TYPE aType );
  138. /**
  139. * Remove a plot with a specific title.
  140. *
  141. * @param aPlotName is the full plot title (e.g. I(Net-C1-Pad1)).
  142. */
  143. void removePlot( const wxString& aPlotName );
  144. /**
  145. * Update plot in a particular SIM_PLOT_PANEL. If the panel does not contain
  146. * the plot, it will be added.
  147. *
  148. * @param aName is the device/net name.
  149. * @param aType describes the type of plot.
  150. * @param aParam is the parameter for the device/net (e.g. I, Id, V).
  151. * @param aPlotPanel is the panel that should receive the update.
  152. * @return True if a plot was successfully added/updated.
  153. */
  154. bool updatePlot( const wxString& aName, SIM_PLOT_TYPE aType, SIM_PLOT_PANEL* aPlotPanel );
  155. /**
  156. * Update the list of currently plotted signals.
  157. */
  158. void updateSignalList();
  159. /**
  160. * Apply component values specified using tuner sliders to the current netlist.
  161. */
  162. void applyTuners();
  163. /**
  164. * Load plot settings from a file.
  165. *
  166. * @param aPath is the file name.
  167. * @return True if successful.
  168. */
  169. bool loadWorkbook( const wxString& aPath );
  170. /**
  171. * Save plot settings to a file.
  172. *
  173. * @param aPath is the file name.
  174. * @return True if successful.
  175. */
  176. bool saveWorkbook( const wxString& aPath );
  177. /**
  178. * Return the default filename (with extension) to be used in file browser dialog.
  179. */
  180. wxString getDefaultFilename();
  181. /**
  182. * Return the default path to be used in file browser dialog.
  183. */
  184. wxString getDefaultPath();
  185. /**
  186. * Return the currently opened plot panel (or NULL if there is none).
  187. */
  188. SIM_PANEL_BASE* getCurrentPlotWindow() const
  189. {
  190. return dynamic_cast<SIM_PANEL_BASE*>( m_workbook->GetCurrentPage() );
  191. }
  192. /**
  193. *
  194. */
  195. wxString getCurrentSimCommand() const
  196. {
  197. if( getCurrentPlotWindow() == nullptr )
  198. return m_circuitModel->GetSheetSimCommand();
  199. else
  200. return m_workbook->GetSimCommand( getCurrentPlotWindow() );
  201. }
  202. /**
  203. * Return X axis for a given simulation type.
  204. */
  205. SIM_PLOT_TYPE getXAxisType( SIM_TYPE aType ) const;
  206. // Menu handlers
  207. void menuNewPlot( wxCommandEvent& aEvent ) override;
  208. void menuOpenWorkbook( wxCommandEvent& event ) override;
  209. void menuSaveWorkbook( wxCommandEvent& event ) override;
  210. void menuSaveWorkbookAs( wxCommandEvent& event ) override;
  211. void menuExit( wxCommandEvent& event ) override
  212. {
  213. Close();
  214. }
  215. void menuSaveImage( wxCommandEvent& event ) override;
  216. void menuSaveCsv( wxCommandEvent& event ) override;
  217. void menuZoomIn( wxCommandEvent& event ) override;
  218. void menuZoomOut( wxCommandEvent& event ) override;
  219. void menuZoomFit( wxCommandEvent& event ) override;
  220. void menuShowGrid( wxCommandEvent& event ) override;
  221. void menuShowGridUpdate( wxUpdateUIEvent& event ) override;
  222. void menuShowLegend( wxCommandEvent& event ) override;
  223. void menuShowLegendUpdate( wxUpdateUIEvent& event ) override;
  224. void menuShowDotted( wxCommandEvent& event ) override;
  225. void menuShowDottedUpdate( wxUpdateUIEvent& event ) override;
  226. void menuWhiteBackground( wxCommandEvent& event ) override;
  227. void menuShowWhiteBackgroundUpdate( wxUpdateUIEvent& event ) override
  228. {
  229. event.Check( m_plotUseWhiteBg );
  230. }
  231. void menuSimulateUpdate( wxUpdateUIEvent& event ) override;
  232. void menuAddSignalsUpdate( wxUpdateUIEvent& event ) override;
  233. void menuProbeUpdate( wxUpdateUIEvent& event ) override;
  234. void menuTuneUpdate( wxUpdateUIEvent& event ) override;
  235. // Event handlers
  236. void onPlotClose( wxAuiNotebookEvent& event ) override;
  237. void onPlotClosed( wxAuiNotebookEvent& event ) override;
  238. void onPlotChanged( wxAuiNotebookEvent& event ) override;
  239. void onPlotDragged( wxAuiNotebookEvent& event ) override;
  240. void onSignalDblClick( wxMouseEvent& event ) override;
  241. void onSignalRClick( wxListEvent& event ) override;
  242. void onWorkbookModified( wxCommandEvent& event );
  243. void onWorkbookClrModified( wxCommandEvent& event );
  244. void onSimulate( wxCommandEvent& event );
  245. void onSettings( wxCommandEvent& event );
  246. void onAddSignal( wxCommandEvent& event );
  247. void onProbe( wxCommandEvent& event );
  248. void onTune( wxCommandEvent& event );
  249. void onShowNetlist( wxCommandEvent& event );
  250. bool canCloseWindow( wxCloseEvent& aEvent ) override;
  251. void doCloseWindow() override;
  252. void onCursorUpdate( wxCommandEvent& aEvent );
  253. void onSimUpdate( wxCommandEvent& aEvent );
  254. void onSimReport( wxCommandEvent& aEvent );
  255. void onSimStarted( wxCommandEvent& aEvent );
  256. void onSimFinished( wxCommandEvent& aEvent );
  257. // adjust the sash dimension of splitter windows after reading
  258. // the config settings
  259. // must be called after the config settings are read, and once the
  260. // frame is initialized (end of the Ctor)
  261. void setSubWindowsSashSize();
  262. // Toolbar buttons
  263. wxToolBarToolBase* m_toolSimulate;
  264. wxToolBarToolBase* m_toolAddSignals;
  265. wxToolBarToolBase* m_toolProbe;
  266. wxToolBarToolBase* m_toolTune;
  267. wxToolBarToolBase* m_toolSettings;
  268. SCH_EDIT_FRAME* m_schematicFrame;
  269. std::shared_ptr<NGSPICE_CIRCUIT_MODEL> m_circuitModel;
  270. std::shared_ptr<SPICE_SIMULATOR> m_simulator;
  271. SIM_THREAD_REPORTER* m_reporter;
  272. ///< List of currently displayed tuners
  273. std::list<TUNER_SLIDER*> m_tuners;
  274. // Right click context menu for signals in the listbox
  275. class SIGNAL_CONTEXT_MENU : public wxMenu
  276. {
  277. public:
  278. SIGNAL_CONTEXT_MENU( const wxString& aSignal, SIM_PLOT_FRAME* aPlotFrame );
  279. private:
  280. void onMenuEvent( wxMenuEvent& aEvent );
  281. const wxString& m_signal;
  282. SIM_PLOT_FRAME* m_plotFrame;
  283. enum SIGNAL_CONTEXT_MENU_EVENTS
  284. {
  285. REMOVE_SIGNAL = 944,
  286. SHOW_CURSOR,
  287. HIDE_CURSOR
  288. };
  289. };
  290. ///< Panel that was used as the most recent one for simulations
  291. SIM_PANEL_BASE* m_lastSimPlot;
  292. ///< imagelists used to add a small colored icon to signal names
  293. ///< and cursors name, the same color as the corresponding signal traces
  294. wxImageList* m_signalsIconColorList;
  295. // Variables for temporary storage:
  296. int m_splitterLeftRightSashPosition;
  297. int m_splitterPlotAndConsoleSashPosition;
  298. int m_splitterSignalsSashPosition;
  299. int m_splitterTuneValuesSashPosition;
  300. bool m_plotUseWhiteBg;
  301. unsigned int m_plotNumber;
  302. bool m_simFinished;
  303. };
  304. // Commands
  305. wxDECLARE_EVENT( EVT_SIM_UPDATE, wxCommandEvent );
  306. wxDECLARE_EVENT( EVT_SIM_REPORT, wxCommandEvent );
  307. // Notifications
  308. wxDECLARE_EVENT( EVT_SIM_STARTED, wxCommandEvent );
  309. wxDECLARE_EVENT( EVT_SIM_FINISHED, wxCommandEvent );
  310. #endif // __sim_plot_frame__