KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_plot_tab.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_tab.h"
37#include "sim_plot_colors.h"
38
39class SIMULATOR_FRAME;
40class SIM_PLOT_TAB;
41class TRACE;
42
63class CURSOR : public mpInfoLayer
64{
65public:
66 CURSOR( TRACE* aTrace, SIM_PLOT_TAB* aPlotTab ) :
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 }
75
76 void Plot( wxDC& aDC, mpWindow& aWindow ) override;
77
78 void SetX( int aX )
79 {
80 m_reference.x = 0;
81 m_updateRef = true;
82 Move( wxPoint( aX, 0 ) );
83 }
84
85 void Update()
86 {
87 m_updateRequired = true;
88 }
89
90 bool Inside( const wxPoint& aPoint ) const override;
91
92 void Move( wxPoint aDelta ) override
93 {
94 Update();
95 mpInfoLayer::Move( aDelta );
96 }
97
98 void UpdateReference() override;
99
100 const wxRealPoint& GetCoords() const
101 {
102 return m_coords;
103 }
104
105 void SetCoordX( double aValue );
106
107private:
108 void doSetCoordX( double aValue );
109
110 wxString getID();
111
112private:
116 wxRealPoint m_coords;
118
119 static constexpr int DRAG_MARGIN = 10;
120};
121
122
123class TRACE : public mpFXYVector
124{
125public:
126 TRACE( const wxString& aName, SIM_TRACE_TYPE aType ) :
127 mpFXYVector( aName ),
128 m_type( aType )
129 {
130 SetContinuity( true );
131 ShowName( false );
132 SetName( aName );
133 }
134
135 void SetName( const wxString& aName ) override
136 {
137 for( auto& [ idx, cursor ] : m_cursors )
138 {
139 if( cursor )
140 cursor->SetName( aName );
141 }
142
143 mpFXYVector::SetName( aName );
144
145 if( m_type & SPT_AC_GAIN )
146 m_displayName = aName + _( " (gain)" );
147 else if( m_type & SPT_AC_PHASE )
148 m_displayName = aName + _( " (phase)" );
149 else
150 m_displayName = aName;
151 }
152
159 void SetData( const std::vector<double>& aX, const std::vector<double>& aY ) override
160 {
161 for( auto& [ idx, cursor ] : m_cursors )
162 {
163 if( cursor )
164 cursor->Update();
165 }
166
167 mpFXYVector::SetData( aX, aY );
168 }
169
170 const std::vector<double>& GetDataX() const { return m_xs; }
171 const std::vector<double>& GetDataY() const { return m_ys; }
172
173 bool HasCursor( int aCursorId ) { return m_cursors[ aCursorId ] != nullptr; }
174
175 void SetCursor( int aCursorId, CURSOR* aCursor ) { m_cursors[ aCursorId ] = aCursor; }
176 CURSOR* GetCursor( int aCursorId ) { return m_cursors[ aCursorId ]; }
177 std::map<int, CURSOR*>& GetCursors() { return m_cursors; }
178
179 SIM_TRACE_TYPE GetType() const { return m_type; }
180
181 void SetTraceColour( const wxColour& aColour ) { m_traceColour = aColour; }
182 wxColour GetTraceColour() const { return m_traceColour; }
183
184protected:
185 std::map<int, CURSOR*> m_cursors; // No ownership; the mpWindow owns the CURSORs
188};
189
190
191class SIM_PLOT_TAB : public SIM_TAB
192{
193public:
194 SIM_PLOT_TAB( const wxString& aSimCommand, wxWindow* parent );
195
196 virtual ~SIM_PLOT_TAB();
197
198 wxString GetLabelX() const
199 {
200 return m_axis_x ? m_axis_x->GetName() : wxString( wxS( "" ) );
201 }
202
203 wxString GetLabelY1() const
204 {
205 return m_axis_y1 ? m_axis_y1->GetName() : wxString( wxS( "" ) );
206 }
207
208 wxString GetLabelY2() const
209 {
210 return m_axis_y2 ? m_axis_y2->GetName() : wxString( wxS( "" ) );
211 }
212
213 wxString GetLabelY3() const
214 {
215 return m_axis_y3 ? m_axis_y3->GetName() : wxString( wxS( "" ) );
216 }
217
218 bool GetY1Scale( double* aMin, double* aMax ) const
219 {
220 if( m_axis_y1 )
221 return m_axis_y1->GetAxisMinMax( aMin, aMax );
222
223 return false;
224 }
225
226 bool GetY2Scale( double* aMin, double* aMax ) const
227 {
228 if( m_axis_y2 )
229 return m_axis_y2->GetAxisMinMax( aMin, aMax );
230
231 return false;
232 }
233
234 bool GetY3Scale( double* aMin, double* aMax ) const
235 {
236 if( m_axis_y3 )
237 return m_axis_y3->GetAxisMinMax( aMin, aMax );
238
239 return false;
240 }
241
242 void SetY1Scale( bool aLock, double aMin, double aMax );
243 void SetY2Scale( bool aLock, double aMin, double aMax );
244 void SetY3Scale( bool aLock, double aMin, double aMax );
245
246 wxString GetUnitsX() const;
247 wxString GetUnitsY1() const;
248 wxString GetUnitsY2() const;
249 wxString GetUnitsY3() const;
250
251 const std::map<wxString, TRACE*>& GetTraces() const
252 {
253 return m_traces;
254 }
255
256 TRACE* GetTrace( const wxString& aVecName, int aType ) const
257 {
258 auto trace = m_traces.find( getTraceId( aVecName, aType ) );
259
260 return trace == m_traces.end() ? nullptr : trace->second;
261 }
262
263 void ShowGrid( bool aEnable )
264 {
265 if( m_axis_x )
266 m_axis_x->SetTicks( !aEnable );
267
268 if( m_axis_y1 )
269 m_axis_y1->SetTicks( !aEnable );
270
271 if( m_axis_y2 )
272 m_axis_y2->SetTicks( !aEnable );
273
274 if( m_axis_y3 )
275 m_axis_y3->SetTicks( !aEnable );
276
278 }
279
280 bool IsGridShown() const
281 {
282 if( !m_axis_x || !m_axis_y1 )
283 return false;
284
285 return !m_axis_x->GetTicks();
286 }
287
288 void ShowLegend( bool aEnable )
289 {
290 m_legend->SetVisible( aEnable );
292 }
293
294 bool IsLegendShown() const
295 {
296 return m_legend->IsVisible();
297 }
298
299 wxPoint GetLegendPosition() const
300 {
301 return m_legend->GetPosition();
302 }
303
304 void SetLegendPosition( const wxPoint& aPosition )
305 {
306 m_legend->Move( aPosition );
308 m_LastLegendPosition = aPosition;
309 }
310
314 void SetDottedSecondary( bool aEnable )
315 {
316 m_dotted_cp = aEnable;
317
318 for( const auto& [ name, trace ] : m_traces )
319 UpdateTraceStyle( trace );
320
322 }
323
325 {
326 return m_dotted_cp;
327 }
328
330 void EnableCursor( const wxString& aVectorName, int aType, int aCursorId, bool aEnable,
331 const wxString& aSignalName );
332
334 void ResetScales( bool aIncludeX );
335
337 void UpdateTraceStyle( TRACE* trace );
338
340 void UpdatePlotColors();
341
342 void OnLanguageChanged() override;
343
345 mpWindow* GetPlotWin() const { return m_plotWin; }
346
347 TRACE* GetOrAddTrace( const wxString& aVectorName, int aType );
348
349 void SetTraceData( TRACE* aTrace, std::vector<double>& aX, std::vector<double>& aY );
350
351 bool DeleteTrace( const wxString& aVectorName, int aTraceType );
352 void DeleteTrace( TRACE* aTrace );
353
354 std::vector<std::pair<wxString, wxString>>& Measurements() { return m_measurements; }
355
356public:
358
359private:
360 wxString getTraceId( const wxString& aVectorName, int aType ) const
361 {
362 return wxString::Format( wxS( "%s%d" ), aVectorName, aType & SPT_Y_AXIS_MASK );
363 }
364
366 void prepareDCAxes( int aNewTraceType );
367
369 void updateAxes( int aNewTraceType = SIM_TRACE_TYPE::SPT_UNKNOWN );
370
371private:
373 std::map<wxString, wxColour> m_sessionTraceColors;
374
375 // Top-level plot window
377 wxBoxSizer* m_sizer;
378
379 // Traces to be plotted
380 std::map<wxString, TRACE*> m_traces;
381
387
389
390 // Measurements (and their format strings)
391 std::vector<std::pair<wxString, wxString>> m_measurements;
392};
393
394wxDECLARE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );
395
396#endif
const char * name
Definition: DXF_plotter.cpp:57
The SIMULATOR_FRAME holds the main user-interface for running simulations.
Definition: sim_plot_tab.h:64
CURSOR(TRACE *aTrace, SIM_PLOT_TAB *aPlotTab)
Definition: sim_plot_tab.h:66
mpWindow * m_window
Definition: sim_plot_tab.h:117
const wxRealPoint & GetCoords() const
Definition: sim_plot_tab.h:100
void Move(wxPoint aDelta) override
Moves the layer rectangle of given pixel deltas.
Definition: sim_plot_tab.h:92
void SetX(int aX)
Definition: sim_plot_tab.h:78
wxString getID()
wxRealPoint m_coords
Definition: sim_plot_tab.h:116
bool m_updateRef
Definition: sim_plot_tab.h:115
static constexpr int DRAG_MARGIN
Definition: sim_plot_tab.h:119
bool Inside(const wxPoint &aPoint) const override
Checks whether a point is inside the info box rectangle.
void doSetCoordX(double aValue)
void SetCoordX(double aValue)
void UpdateReference() override
Updates the rectangle reference point.
bool m_updateRequired
Definition: sim_plot_tab.h:114
TRACE * m_trace
Definition: sim_plot_tab.h:113
void Plot(wxDC &aDC, mpWindow &aWindow) override
Plot method.
void Update()
Definition: sim_plot_tab.h:85
The SIMULATOR_FRAME holds the main user-interface for running simulations.
bool DeleteTrace(const wxString &aVectorName, int aTraceType)
void EnableCursor(const wxString &aVectorName, int aType, int aCursorId, bool aEnable, const wxString &aSignalName)
Reset scale ranges to fit the current traces.
mpScaleXBase * m_axis_x
Definition: sim_plot_tab.h:382
wxString GetLabelY1() const
Definition: sim_plot_tab.h:203
mpWindow * GetPlotWin() const
Definition: sim_plot_tab.h:345
void prepareDCAxes(int aNewTraceType)
Create/Ensure axes are available for plotting.
void ShowGrid(bool aEnable)
Definition: sim_plot_tab.h:263
wxString GetUnitsY2() const
void SetTraceData(TRACE *aTrace, std::vector< double > &aX, std::vector< double > &aY)
void SetY2Scale(bool aLock, double aMin, double aMax)
TRACE * GetTrace(const wxString &aVecName, int aType) const
Definition: sim_plot_tab.h:256
virtual ~SIM_PLOT_TAB()
wxString GetLabelX() const
Definition: sim_plot_tab.h:198
bool IsGridShown() const
Definition: sim_plot_tab.h:280
const std::map< wxString, TRACE * > & GetTraces() const
Definition: sim_plot_tab.h:251
wxString GetLabelY3() const
Definition: sim_plot_tab.h:213
void SetY1Scale(bool aLock, double aMin, double aMax)
bool GetDottedSecondary() const
Toggle cursor for a particular trace.
Definition: sim_plot_tab.h:324
wxPoint GetLegendPosition() const
Definition: sim_plot_tab.h:299
mpInfoLegend * m_legend
Definition: sim_plot_tab.h:386
std::vector< std::pair< wxString, wxString > > m_measurements
Definition: sim_plot_tab.h:391
void SetY3Scale(bool aLock, double aMin, double aMax)
wxBoxSizer * m_sizer
Definition: sim_plot_tab.h:377
std::map< wxString, TRACE * > m_traces
Definition: sim_plot_tab.h:380
bool IsLegendShown() const
Definition: sim_plot_tab.h:294
std::vector< std::pair< wxString, wxString > > & Measurements()
Definition: sim_plot_tab.h:354
SIM_PLOT_COLORS m_colors
Definition: sim_plot_tab.h:372
void UpdateTraceStyle(TRACE *trace)
Update plot colors.
void SetLegendPosition(const wxPoint &aPosition)
Definition: sim_plot_tab.h:304
void ResetScales(bool aIncludeX)
Update trace line style.
void UpdatePlotColors()
mpScaleY * m_axis_y2
Definition: sim_plot_tab.h:384
void ShowLegend(bool aEnable)
Definition: sim_plot_tab.h:288
bool GetY3Scale(double *aMin, double *aMax) const
Definition: sim_plot_tab.h:234
wxString GetLabelY2() const
Definition: sim_plot_tab.h:208
wxString GetUnitsX() const
void OnLanguageChanged() override
Getter for math plot window.
wxString getTraceId(const wxString &aVectorName, int aType) const
Construct the plot axes for DC simulation plot.
Definition: sim_plot_tab.h:360
TRACE * GetOrAddTrace(const wxString &aVectorName, int aType)
mpScaleY * m_axis_y1
Definition: sim_plot_tab.h:383
void SetDottedSecondary(bool aEnable)
Draw secondary signal traces (current or phase) with dotted lines.
Definition: sim_plot_tab.h:314
mpScaleY * m_axis_y3
Definition: sim_plot_tab.h:385
wxPoint m_LastLegendPosition
Definition: sim_plot_tab.h:357
wxString GetUnitsY1() const
std::map< wxString, wxColour > m_sessionTraceColors
Definition: sim_plot_tab.h:373
mpWindow * m_plotWin
Definition: sim_plot_tab.h:376
bool GetY2Scale(double *aMin, double *aMax) const
Definition: sim_plot_tab.h:226
void updateAxes(int aNewTraceType=SIM_TRACE_TYPE::SPT_UNKNOWN)
bool GetY1Scale(double *aMin, double *aMax) const
Definition: sim_plot_tab.h:218
wxString GetUnitsY3() const
void SetTraceColour(const wxColour &aColour)
Definition: sim_plot_tab.h:181
void SetName(const wxString &aName) override
Set layer name.
Definition: sim_plot_tab.h:135
wxColour m_traceColour
Definition: sim_plot_tab.h:187
std::map< int, CURSOR * > & GetCursors()
Definition: sim_plot_tab.h:177
SIM_TRACE_TYPE m_type
Definition: sim_plot_tab.h:186
SIM_TRACE_TYPE GetType() const
Definition: sim_plot_tab.h:179
std::map< int, CURSOR * > m_cursors
Definition: sim_plot_tab.h:185
bool HasCursor(int aCursorId)
Definition: sim_plot_tab.h:173
TRACE(const wxString &aName, SIM_TRACE_TYPE aType)
Definition: sim_plot_tab.h:126
void SetData(const std::vector< double > &aX, const std::vector< double > &aY) override
Assigns new data set for the trace.
Definition: sim_plot_tab.h:159
void SetCursor(int aCursorId, CURSOR *aCursor)
Definition: sim_plot_tab.h:175
const std::vector< double > & GetDataY() const
Definition: sim_plot_tab.h:171
wxColour GetTraceColour() const
Definition: sim_plot_tab.h:182
const std::vector< double > & GetDataX() const
Definition: sim_plot_tab.h:170
CURSOR * GetCursor(int aCursorId)
Definition: sim_plot_tab.h:176
A class providing graphs functionality for a 2D plot (either continuous or a set of points),...
Definition: mathplot.h:1362
std::vector< double > m_ys
Definition: mathplot.h:1385
std::vector< double > m_xs
The internal copy of the set of data to draw.
Definition: mathplot.h:1385
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:2490
Base class to create small rectangular info boxes mpInfoLayer is the base class to create a small rec...
Definition: mathplot.h:330
wxPoint m_reference
Definition: mathplot.h:381
wxPoint GetPosition() const
Returns the position of the upper left corner of the box (in pixels)
Definition: mathplot.cpp:164
virtual void UpdateReference()
Updates the rectangle reference point.
Definition: mathplot.cpp:119
virtual void Move(wxPoint delta)
Moves the layer rectangle of given pixel deltas.
Definition: mathplot.cpp:112
Implements the legend to be added to the plot This layer allows you to add a legend to describe the p...
Definition: mathplot.h:393
bool IsVisible() const
Checks whether the layer is visible or not.
Definition: mathplot.h:291
const wxString & GetName() const
Get layer name.
Definition: mathplot.h:239
void SetContinuity(bool continuity)
Set the 'continuity' property of the layer (true:draws a continuous line, false:draws separate points...
Definition: mathplot.h:259
void ShowName(bool show)
Shows or hides the text label with the name of the layer (default is visible).
Definition: mathplot.h:268
virtual void SetName(const wxString &name)
Set layer name.
Definition: mathplot.h:273
void SetVisible(bool show)
Sets layer visibility.
Definition: mathplot.h:295
wxString m_displayName
Definition: mathplot.h:311
void SetTicks(bool enable)
Set X axis ticks or grid.
Definition: mathplot.h:632
bool GetTicks() const
Get X axis ticks or grid.
Definition: mathplot.h:637
bool GetAxisMinMax(double *minV, double *maxV)
Definition: mathplot.h:688
Plot layer implementing a y-scale ruler.
Definition: mathplot.h:826
Canvas for plotting mpLayer implementations.
Definition: mathplot.h:906
void UpdateAll()
Refresh display.
Definition: mathplot.cpp:2203
#define _(s)
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:50
@ SPT_AC_PHASE
Definition: sim_types.h:54
@ SPT_AC_GAIN
Definition: sim_types.h:55
@ SPT_Y_AXIS_MASK
Definition: sim_types.h:58