KiCad PCB EDA Suite
sim_plot_panel.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-2023 CERN
5 * Copyright (C) 2016-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 * @author Maciej Suminski <[email protected]>
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_PANEL_H
29#define __SIM_PLOT_PANEL_H
30
31#include "sim_types.h"
32#include <map>
33#include <widgets/mathplot.h>
34#include <wx/colour.h>
35#include <wx/sizer.h>
36#include "sim_panel_base.h"
37#include "sim_plot_colors.h"
38
39class SIMULATOR_FRAME;
40class SIM_PLOT_PANEL;
41class TRACE;
42
63class CURSOR : public mpInfoLayer
64{
65public:
66 CURSOR( TRACE* aTrace, SIM_PLOT_PANEL* aPlotPanel ) :
67 mpInfoLayer( wxRect( 0, 0, DRAG_MARGIN, DRAG_MARGIN ), wxTRANSPARENT_BRUSH ),
68 m_trace( aTrace ),
69 m_updateRequired( true ),
70 m_updateRef( false ),
71 m_coords( 0.0, 0.0 ),
72 m_window( nullptr )
73 {
74 SetDrawOutsideMargins( false );
75 }
76
77 void Plot( wxDC& aDC, mpWindow& aWindow ) override;
78
79 void SetX( int aX )
80 {
81 m_reference.x = 0;
82 m_updateRef = true;
83 Move( wxPoint( aX, 0 ) );
84 }
85
86 void Update()
87 {
88 m_updateRequired = true;
89 }
90
91 bool Inside( const wxPoint& aPoint ) const override;
92
93 void Move( wxPoint aDelta ) override
94 {
95 Update();
96 mpInfoLayer::Move( aDelta );
97 }
98
99 void UpdateReference() override;
100
101 const wxRealPoint& GetCoords() const
102 {
103 return m_coords;
104 }
105
106 void SetCoordX( double aValue );
107
108private:
109 void doSetCoordX( double aValue );
110
111 wxString getID();
112
113private:
117 wxRealPoint m_coords;
119
120 static constexpr int DRAG_MARGIN = 10;
121};
122
123
124class TRACE : public mpFXYVector
125{
126public:
127 TRACE( const wxString& aName, SIM_TRACE_TYPE aType ) :
128 mpFXYVector( aName ),
129 m_type( aType )
130 {
131 SetContinuity( true );
132 SetDrawOutsideMargins( false );
133 ShowName( false );
134 }
135
136 void SetName( wxString aName ) override
137 {
138 for( auto& [ idx, cursor ] : m_cursors )
139 {
140 if( cursor )
141 cursor->SetName( aName );
142 }
143
144 mpFXYVector::SetName( aName );
145 }
146
153 void SetData( const std::vector<double>& aX, const std::vector<double>& aY ) override
154 {
155 for( auto& [ idx, cursor ] : m_cursors )
156 {
157 if( cursor )
158 cursor->Update();
159 }
160
161 mpFXYVector::SetData( aX, aY );
162 }
163
164 const std::vector<double>& GetDataX() const { return m_xs; }
165 const std::vector<double>& GetDataY() const { return m_ys; }
166
167 bool HasCursor( int aCursorId ) { return m_cursors[ aCursorId ] != nullptr; }
168
169 void SetCursor( int aCursorId, CURSOR* aCursor ) { m_cursors[ aCursorId ] = aCursor; }
170 CURSOR* GetCursor( int aCursorId ) { return m_cursors[ aCursorId ]; }
171 std::map<int, CURSOR*>& GetCursors() { return m_cursors; }
172
173 SIM_TRACE_TYPE GetType() const { return m_type; }
174
175 void SetTraceColour( const wxColour& aColour ) { m_traceColour = aColour; }
176 wxColour GetTraceColour() const { return m_traceColour; }
177
178protected:
179 std::map<int, CURSOR*> m_cursors; // No ownership; the mpWindow owns the CURSORs
182};
183
184
186{
187public:
188 SIM_PLOT_PANEL( const wxString& aCommand, int aOptions, wxWindow* parent, wxWindowID id,
189 const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
190 long style = 0, const wxString& name = wxPanelNameStr );
191
192 virtual ~SIM_PLOT_PANEL();
193
194 wxString GetLabelX() const
195 {
196 return m_axis_x ? m_axis_x->GetName() : wxString( wxS( "" ) );
197 }
198
199 wxString GetLabelY1() const
200 {
201 return m_axis_y1 ? m_axis_y1->GetName() : wxString( wxS( "" ) );
202 }
203
204 wxString GetLabelY2() const
205 {
206 return m_axis_y2 ? m_axis_y2->GetName() : wxString( wxS( "" ) );
207 }
208
209 wxString GetLabelY3() const
210 {
211 return m_axis_y3 ? m_axis_y3->GetName() : wxString( wxS( "" ) );
212 }
213
214 wxString GetUnitsX() const;
215 wxString GetUnitsY1() const;
216 wxString GetUnitsY2() const;
217 wxString GetUnitsY3() const;
218
219 const std::map<wxString, TRACE*>& GetTraces() const
220 {
221 return m_traces;
222 }
223
224 TRACE* GetTrace( const wxString& aVecName, int aType ) const
225 {
226 auto trace = m_traces.find( getTraceId( aVecName, aType ) );
227
228 return trace == m_traces.end() ? nullptr : trace->second;
229 }
230
231 void ShowGrid( bool aEnable )
232 {
233 m_axis_x->SetTicks( !aEnable );
234 m_axis_y1->SetTicks( !aEnable );
235 m_axis_y2->SetTicks( !aEnable );
236 m_axis_y3->SetTicks( !aEnable );
238 }
239
240 bool IsGridShown() const
241 {
242 if( !m_axis_x || !m_axis_y1 )
243 return false;
244
245 assert( m_axis_x->GetTicks() == m_axis_y1->GetTicks() );
246 return !m_axis_x->GetTicks();
247 }
248
249 void ShowLegend( bool aEnable )
250 {
251 m_legend->SetVisible( aEnable );
253 }
254
255 bool IsLegendShown() const
256 {
257 return m_legend->IsVisible();
258 }
259
263 void SetDottedSecondary( bool aEnable )
264 {
265 m_dotted_cp = aEnable;
266
267 for( const auto& tr : m_traces )
268 UpdateTraceStyle( tr.second );
269
271 }
272
274 {
275 return m_dotted_cp;
276 }
277
279 void EnableCursor( const wxString& aVectorName, int aType, int aCursorId, bool aEnable,
280 const wxString& aSignalName );
281
283 void ResetScales();
284
286 void UpdateTraceStyle( TRACE* trace );
287
289 void UpdatePlotColors();
290
291 void OnLanguageChanged() override;
292
294 mpWindow* GetPlotWin() const { return m_plotWin; }
295
296 TRACE* AddTrace( const wxString& aVectorName, int aType );
297
298 void SetTraceData( TRACE* aTrace, unsigned int aPoints, const double* aX, const double* aY );
299
300 bool DeleteTrace( const wxString& aVectorName, int aTraceType );
301 void DeleteTrace( TRACE* aTrace );
302
303private:
304 wxString getTraceId( const wxString& aVectorName, int aType ) const
305 {
306 return wxString::Format( wxS( "%s%d" ), aVectorName, aType & SPT_Y_AXIS_MASK );
307 }
308
310 void prepareDCAxes( int aNewTraceType );
311
313 void updateAxes( int aNewTraceType = SIM_TRACE_TYPE::SPT_UNKNOWN );
314
315private:
317
318 // Top-level plot window
320 wxBoxSizer* m_sizer;
321
322 // Traces to be plotted
323 std::map<wxString, TRACE*> m_traces;
324
330
332};
333
334wxDECLARE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );
335
336#endif
const char * name
Definition: DXF_plotter.cpp:56
The SIMULATOR_FRAME holds the main user-interface for running simulations.
mpWindow * m_window
const wxRealPoint & GetCoords() const
void Move(wxPoint aDelta) override
Moves the layer rectangle of given pixel deltas.
void SetX(int aX)
wxString getID()
wxRealPoint m_coords
bool m_updateRef
static constexpr int DRAG_MARGIN
bool Inside(const wxPoint &aPoint) const override
Checks whether a point is inside the info box rectangle.
void doSetCoordX(double aValue)
CURSOR(TRACE *aTrace, SIM_PLOT_PANEL *aPlotPanel)
void SetCoordX(double aValue)
void UpdateReference() override
Updates the rectangle reference point.
bool m_updateRequired
TRACE * m_trace
void Plot(wxDC &aDC, mpWindow &aWindow) override
Plot method.
void Update()
The SIMULATOR_FRAME holds the main user-interface for running simulations.
mpScaleY * m_axis_y1
void UpdatePlotColors()
‍Update plot colors
void ShowLegend(bool aEnable)
mpScaleY * m_axis_y3
wxBoxSizer * m_sizer
SIM_PLOT_COLORS m_colors
mpInfoLegend * m_legend
void UpdateTraceStyle(TRACE *trace)
void ResetScales()
Update trace line style.
bool IsGridShown() const
wxString GetLabelY2() const
wxString GetUnitsX() const
void updateAxes(int aNewTraceType=SIM_TRACE_TYPE::SPT_UNKNOWN)
‍Create/Ensure axes are available for plotting
TRACE * AddTrace(const wxString &aVectorName, int aType)
wxString GetLabelY3() const
bool DeleteTrace(const wxString &aVectorName, int aTraceType)
bool IsLegendShown() const
TRACE * GetTrace(const wxString &aVecName, int aType) const
virtual ~SIM_PLOT_PANEL()
wxString GetUnitsY3() const
wxString getTraceId(const wxString &aVectorName, int aType) const
Construct the plot axes for DC simulation plot.
void SetDottedSecondary(bool aEnable)
Draw secondary signal traces (current or phase) with dotted lines.
void OnLanguageChanged() override
Getter for math plot window.
void EnableCursor(const wxString &aVectorName, int aType, int aCursorId, bool aEnable, const wxString &aSignalName)
Reset scale ranges to fit the current traces.
bool GetDottedSecondary() const
Toggle cursor for a particular trace.
std::map< wxString, TRACE * > m_traces
mpScaleXBase * m_axis_x
void prepareDCAxes(int aNewTraceType)
const std::map< wxString, TRACE * > & GetTraces() const
mpWindow * m_plotWin
void SetTraceData(TRACE *aTrace, unsigned int aPoints, const double *aX, const double *aY)
wxString GetUnitsY2() const
SIM_PLOT_PANEL(const wxString &aCommand, int aOptions, wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxString &name=wxPanelNameStr)
mpWindow * GetPlotWin() const
mpScaleY * m_axis_y2
void ShowGrid(bool aEnable)
wxString GetLabelY1() const
wxString GetUnitsY1() const
wxString GetLabelX() const
void SetTraceColour(const wxColour &aColour)
wxColour m_traceColour
std::map< int, CURSOR * > & GetCursors()
SIM_TRACE_TYPE m_type
SIM_TRACE_TYPE GetType() const
std::map< int, CURSOR * > m_cursors
bool HasCursor(int aCursorId)
TRACE(const wxString &aName, SIM_TRACE_TYPE aType)
void SetData(const std::vector< double > &aX, const std::vector< double > &aY) override
Assigns new data set for the trace.
void SetCursor(int aCursorId, CURSOR *aCursor)
void SetName(wxString aName) override
Set layer name.
const std::vector< double > & GetDataY() const
wxColour GetTraceColour() const
const std::vector< double > & GetDataX() const
CURSOR * GetCursor(int aCursorId)
A class providing graphs functionality for a 2D plot (either continuous or a set of points),...
Definition: mathplot.h:1572
std::vector< double > m_ys
Definition: mathplot.h:1595
std::vector< double > m_xs
The internal copy of the set of data to draw.
Definition: mathplot.h:1595
virtual void SetData(const std::vector< double > &xs, const std::vector< double > &ys)
Changes the internal data: the set of points to draw.
Definition: mathplot.cpp:2930
Base class to create small rectangular info boxes mpInfoLayer is the base class to create a small rec...
Definition: mathplot.h:336
wxPoint m_reference
Definition: mathplot.h:396
virtual void Move(wxPoint delta)
Moves the layer rectangle of given pixel deltas.
Definition: mathplot.cpp:134
Implements the legend to be added to the plot This layer allows you to add a legend to describe the p...
Definition: mathplot.h:438
bool IsVisible() const
Checks whether the layer is visible or not.
Definition: mathplot.h:298
const wxString & GetName() const
Get layer name.
Definition: mathplot.h:238
void SetContinuity(bool continuity)
Set the 'continuity' property of the layer (true:draws a continuous line, false:draws separate points...
Definition: mathplot.h:253
void ShowName(bool show)
Shows or hides the text label with the name of the layer (default is visible).
Definition: mathplot.h:262
void SetVisible(bool show)
Sets layer visibility.
Definition: mathplot.h:302
virtual void SetName(wxString name)
Set layer name.
Definition: mathplot.h:267
void SetDrawOutsideMargins(bool drawModeOutside)
Set Draw mode: inside or outside margins.
Definition: mathplot.h:281
void SetTicks(bool enable)
Set X axis ticks or grid.
Definition: mathplot.h:716
bool GetTicks() const
Get X axis ticks or grid.
Definition: mathplot.h:721
Plot layer implementing a y-scale ruler.
Definition: mathplot.h:944
void SetTicks(bool ticks)
Set Y axis ticks or grid.
Definition: mathplot.h:971
bool GetTicks() const
Get Y axis ticks or grid.
Definition: mathplot.h:975
Canvas for plotting mpLayer implementations.
Definition: mathplot.h:1049
void UpdateAll()
Refresh display.
Definition: mathplot.cpp:2453
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
Class is responsible for providing colors for traces on simulation plot.
wxDECLARE_EVENT(EVT_SIM_CURSOR_UPDATE, wxCommandEvent)
SIM_TRACE_TYPE
Definition: sim_types.h:47
@ SPT_UNKNOWN
Definition: sim_types.h:63
@ SPT_Y_AXIS_MASK
Definition: sim_types.h:54