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-2024 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 void ApplyPreferences( const SIM_PREFERENCES& aPrefs ) override
199 {
201 }
202
203 wxString GetLabelX() const
204 {
205 return m_axis_x ? m_axis_x->GetName() : wxString( wxS( "" ) );
206 }
207
208 wxString GetLabelY1() const
209 {
210 return m_axis_y1 ? m_axis_y1->GetName() : wxString( wxS( "" ) );
211 }
212
213 wxString GetLabelY2() const
214 {
215 return m_axis_y2 ? m_axis_y2->GetName() : wxString( wxS( "" ) );
216 }
217
218 wxString GetLabelY3() const
219 {
220 return m_axis_y3 ? m_axis_y3->GetName() : wxString( wxS( "" ) );
221 }
222
223 bool GetY1Scale( double* aMin, double* aMax ) const
224 {
225 if( m_axis_y1 )
226 return m_axis_y1->GetAxisMinMax( aMin, aMax );
227
228 return false;
229 }
230
231 bool GetY2Scale( double* aMin, double* aMax ) const
232 {
233 if( m_axis_y2 )
234 return m_axis_y2->GetAxisMinMax( aMin, aMax );
235
236 return false;
237 }
238
239 bool GetY3Scale( double* aMin, double* aMax ) const
240 {
241 if( m_axis_y3 )
242 return m_axis_y3->GetAxisMinMax( aMin, aMax );
243
244 return false;
245 }
246
247 void SetY1Scale( bool aLock, double aMin, double aMax );
248 void SetY2Scale( bool aLock, double aMin, double aMax );
249 void SetY3Scale( bool aLock, double aMin, double aMax );
250
251 wxString GetUnitsX() const;
252 wxString GetUnitsY1() const;
253 wxString GetUnitsY2() const;
254 wxString GetUnitsY3() const;
255
256 const std::map<wxString, TRACE*>& GetTraces() const
257 {
258 return m_traces;
259 }
260
261 TRACE* GetTrace( const wxString& aVecName, int aType ) const
262 {
263 auto trace = m_traces.find( getTraceId( aVecName, aType ) );
264
265 return trace == m_traces.end() ? nullptr : trace->second;
266 }
267
268 void ShowGrid( bool aEnable )
269 {
270 if( m_axis_x )
271 m_axis_x->SetTicks( !aEnable );
272
273 if( m_axis_y1 )
274 m_axis_y1->SetTicks( !aEnable );
275
276 if( m_axis_y2 )
277 m_axis_y2->SetTicks( !aEnable );
278
279 if( m_axis_y3 )
280 m_axis_y3->SetTicks( !aEnable );
281
283 }
284
285 bool IsGridShown() const
286 {
287 if( !m_axis_x || !m_axis_y1 )
288 return false;
289
290 return !m_axis_x->GetTicks();
291 }
292
293 void ShowLegend( bool aEnable )
294 {
295 m_legend->SetVisible( aEnable );
297 }
298
299 bool IsLegendShown() const
300 {
301 return m_legend->IsVisible();
302 }
303
304 wxPoint GetLegendPosition() const
305 {
306 return m_legend->GetPosition();
307 }
308
309 void SetLegendPosition( const wxPoint& aPosition )
310 {
311 m_legend->Move( aPosition );
313 m_LastLegendPosition = aPosition;
314 }
315
319 void SetDottedSecondary( bool aEnable )
320 {
321 m_dotted_cp = aEnable;
322
323 for( const auto& [ name, trace ] : m_traces )
324 UpdateTraceStyle( trace );
325
327 }
328
330 {
331 return m_dotted_cp;
332 }
333
335 void EnableCursor( const wxString& aVectorName, int aType, int aCursorId, bool aEnable,
336 const wxString& aSignalName );
337
339 void ResetScales( bool aIncludeX );
340
342 void UpdateTraceStyle( TRACE* trace );
343
345 void UpdatePlotColors();
346
347 void OnLanguageChanged() override;
348
350 mpWindow* GetPlotWin() const { return m_plotWin; }
351
352 TRACE* GetOrAddTrace( const wxString& aVectorName, int aType );
353
354 void SetTraceData( TRACE* aTrace, std::vector<double>& aX, std::vector<double>& aY,
355 int aSweepCount, size_t aSweepSize );
356
357 bool DeleteTrace( const wxString& aVectorName, int aTraceType );
358 void DeleteTrace( TRACE* aTrace );
359
360 std::vector<std::pair<wxString, wxString>>& Measurements() { return m_measurements; }
361
363
364public:
366
367private:
370 {
371 static_assert( static_cast<unsigned>( mpWindow::MouseWheelAction::COUNT )
372 == static_cast<unsigned>( SIM_MOUSE_WHEEL_ACTION::COUNT ),
373 "mpWindow::MouseWheelAction enum must match SIM_MOUSE_WHEEL_ACTION" );
374
377 m.verticalUnmodified = static_cast<A>( s.vertical_unmodified );
378 m.verticalWithCtrl = static_cast<A>( s.vertical_with_ctrl );
379 m.verticalWithShift = static_cast<A>( s.vertical_with_shift );
380 m.verticalWithAlt = static_cast<A>( s.vertical_with_alt );
381 return m;
382 }
383
384 wxString getTraceId( const wxString& aVectorName, int aType ) const
385 {
386 return wxString::Format( wxS( "%s%d" ), aVectorName, aType & SPT_Y_AXIS_MASK );
387 }
388
390 void prepareDCAxes( int aNewTraceType );
391
393 void updateAxes( int aNewTraceType = SIM_TRACE_TYPE::SPT_UNKNOWN );
394
395private:
397 std::map<wxString, wxColour> m_sessionTraceColors;
398
399 // Top-level plot window
401 wxBoxSizer* m_sizer;
402
403 // Traces to be plotted
404 std::map<wxString, TRACE*> m_traces;
405
411
413
414 // Measurements (and their format strings)
415 std::vector<std::pair<wxString, wxString>> m_measurements;
416};
417
418wxDECLARE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );
419
420#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:406
wxString GetLabelY1() const
Definition: sim_plot_tab.h:208
mpWindow * GetPlotWin() const
Definition: sim_plot_tab.h:350
void prepareDCAxes(int aNewTraceType)
Create/Ensure axes are available for plotting.
void ShowGrid(bool aEnable)
Definition: sim_plot_tab.h:268
wxString GetUnitsY2() const
void SetY2Scale(bool aLock, double aMin, double aMax)
TRACE * GetTrace(const wxString &aVecName, int aType) const
Definition: sim_plot_tab.h:261
virtual ~SIM_PLOT_TAB()
wxString GetLabelX() const
Definition: sim_plot_tab.h:203
bool IsGridShown() const
Definition: sim_plot_tab.h:285
const std::map< wxString, TRACE * > & GetTraces() const
Definition: sim_plot_tab.h:256
wxString GetLabelY3() const
Definition: sim_plot_tab.h:218
void SetY1Scale(bool aLock, double aMin, double aMax)
bool GetDottedSecondary() const
Toggle cursor for a particular trace.
Definition: sim_plot_tab.h:329
wxPoint GetLegendPosition() const
Definition: sim_plot_tab.h:304
mpInfoLegend * m_legend
Definition: sim_plot_tab.h:410
std::vector< std::pair< wxString, wxString > > m_measurements
Definition: sim_plot_tab.h:415
void SetY3Scale(bool aLock, double aMin, double aMax)
wxBoxSizer * m_sizer
Definition: sim_plot_tab.h:401
std::map< wxString, TRACE * > m_traces
Definition: sim_plot_tab.h:404
bool IsLegendShown() const
Definition: sim_plot_tab.h:299
std::vector< std::pair< wxString, wxString > > & Measurements()
Definition: sim_plot_tab.h:360
SIM_PLOT_COLORS m_colors
Definition: sim_plot_tab.h:396
void UpdateTraceStyle(TRACE *trace)
Update plot colors.
void SetLegendPosition(const wxPoint &aPosition)
Definition: sim_plot_tab.h:309
void ResetScales(bool aIncludeX)
Update trace line style.
void UpdatePlotColors()
mpScaleY * m_axis_y2
Definition: sim_plot_tab.h:408
void ShowLegend(bool aEnable)
Definition: sim_plot_tab.h:293
bool GetY3Scale(double *aMin, double *aMax) const
Definition: sim_plot_tab.h:239
wxString GetLabelY2() const
Definition: sim_plot_tab.h:213
void SetTraceData(TRACE *aTrace, std::vector< double > &aX, std::vector< double > &aY, int aSweepCount, size_t aSweepSize)
wxString GetUnitsX() const
void OnLanguageChanged() override
Getter for math plot window.
void EnsureThirdYAxisExists()
wxString getTraceId(const wxString &aVectorName, int aType) const
Construct the plot axes for DC simulation plot.
Definition: sim_plot_tab.h:384
TRACE * GetOrAddTrace(const wxString &aVectorName, int aType)
mpScaleY * m_axis_y1
Definition: sim_plot_tab.h:407
void SetDottedSecondary(bool aEnable)
Draw secondary signal traces (current or phase) with dotted lines.
Definition: sim_plot_tab.h:319
void ApplyPreferences(const SIM_PREFERENCES &aPrefs) override
Definition: sim_plot_tab.h:198
mpScaleY * m_axis_y3
Definition: sim_plot_tab.h:409
wxPoint m_LastLegendPosition
Definition: sim_plot_tab.h:365
wxString GetUnitsY1() const
std::map< wxString, wxColour > m_sessionTraceColors
Definition: sim_plot_tab.h:397
mpWindow * m_plotWin
Definition: sim_plot_tab.h:400
static mpWindow::MouseWheelActionSet convertMouseWheelActions(const SIM_MOUSE_WHEEL_ACTION_SET &s)
Definition: sim_plot_tab.h:369
bool GetY2Scale(double *aMin, double *aMax) const
Definition: sim_plot_tab.h:231
void updateAxes(int aNewTraceType=SIM_TRACE_TYPE::SPT_UNKNOWN)
bool GetY1Scale(double *aMin, double *aMax) const
Definition: sim_plot_tab.h:223
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:1409
std::vector< double > m_ys
Definition: mathplot.h:1435
std::vector< double > m_xs
The internal copy of the set of data to draw.
Definition: mathplot.h:1435
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:2629
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:634
bool GetTicks() const
Get X axis ticks or grid.
Definition: mathplot.h:639
bool GetAxisMinMax(double *minV, double *maxV)
Definition: mathplot.h:690
Plot layer implementing a y-scale ruler.
Definition: mathplot.h:828
Canvas for plotting mpLayer implementations.
Definition: mathplot.h:908
void SetMouseWheelActions(const MouseWheelActionSet &s)
Set the pan/zoom actions corresponding to mousewheel/trackpad events.
Definition: mathplot.h:1106
MouseWheelAction
Enumerates the possible mouse wheel actions that can be performed on the plot.
Definition: mathplot.h:914
void UpdateAll()
Refresh display.
Definition: mathplot.cpp:2286
#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
Contains the set of modified mouse wheel actions that can be performed on a simulator plot.
SIM_MOUSE_WHEEL_ACTION vertical_unmodified
SIM_MOUSE_WHEEL_ACTION vertical_with_alt
SIM_MOUSE_WHEEL_ACTION vertical_with_ctrl
SIM_MOUSE_WHEEL_ACTION vertical_with_shift
Contains preferences pertaining to the simulator.
SIM_MOUSE_WHEEL_ACTION_SET mouse_wheel_actions
Contains the set of modified mouse wheel actions that can be performed on the plot.
Definition: mathplot.h:929
MouseWheelAction verticalWithShift
Definition: mathplot.h:937
MouseWheelAction verticalWithCtrl
Definition: mathplot.h:936
MouseWheelAction verticalWithAlt
Definition: mathplot.h:938
MouseWheelAction verticalUnmodified
Definition: mathplot.h:935