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 CERN
5 * Copyright (C) 2016-2021 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 SIM_PLOT_FRAME;
40class SIM_PLOT_PANEL;
41class TRACE;
42
44class CURSOR : public mpInfoLayer
45{
46public:
47 CURSOR( const TRACE* aTrace, SIM_PLOT_PANEL* aPlotPanel ) :
48 mpInfoLayer( wxRect( 0, 0, DRAG_MARGIN, DRAG_MARGIN ), wxTRANSPARENT_BRUSH ),
49 m_trace( aTrace ), m_updateRequired( true ), m_updateRef( false ), m_coords( 0.0, 0.0 ),
50 m_window( nullptr )
51 {
52 SetDrawOutsideMargins( false );
53 }
54
55 void Plot( wxDC& aDC, mpWindow& aWindow ) override;
56
57 void SetX( int aX )
58 {
59 m_reference.x = 0;
60 m_updateRef = true;
61 Move( wxPoint( aX, 0 ) );
62 }
63
64 void Update()
65 {
66 m_updateRequired = true;
67 }
68
69 bool Inside( wxPoint& aPoint ) override;
70
71 void Move( wxPoint aDelta ) override
72 {
73 Update();
74 mpInfoLayer::Move( aDelta );
75 }
76
77 void UpdateReference() override;
78
79 const wxRealPoint& GetCoords() const
80 {
81 return m_coords;
82 }
83
84private:
85 const TRACE* m_trace;
87 wxRealPoint m_coords;
89
90 static constexpr int DRAG_MARGIN = 10;
91};
92
93
94class TRACE : public mpFXYVector
95{
96public:
97 TRACE( const wxString& aName, SIM_PLOT_TYPE aType ) :
98 mpFXYVector( aName ), m_cursor( nullptr ), m_type( aType )
99 {
100 SetContinuity( true );
101 SetDrawOutsideMargins( false );
102 ShowName( false );
103 }
104
111 void SetData( const std::vector<double>& aX, const std::vector<double>& aY ) override
112 {
113 if( m_cursor )
114 m_cursor->Update();
115
116 mpFXYVector::SetData( aX, aY );
117 }
118
119 const std::vector<double>& GetDataX() const
120 {
121 return m_xs;
122 }
123
124 const std::vector<double>& GetDataY() const
125 {
126 return m_ys;
127 }
128
129 bool HasCursor() const
130 {
131 return m_cursor != nullptr;
132 }
133
134 void SetCursor( CURSOR* aCursor )
135 {
136 m_cursor = aCursor;
137 }
138
140 {
141 return m_cursor;
142 }
143
145 {
146 return m_type;
147 }
148
149 void SetTraceColour( wxColour aColour )
150 {
151 m_traceColour = aColour;
152 }
153
154 wxColour GetTraceColour()
155 {
156 return m_traceColour;
157 }
158
159 const wxString& GetParam() const
160 {
161 return m_param;
162 }
163
164
165protected:
169
170private:
172 wxString m_param;
173};
174
175
177{
178 friend class SIM_WORKBOOK;
179
180public:
181 SIM_PLOT_PANEL( const wxString& aCommand, wxWindow* parent, SIM_PLOT_FRAME* aMainFrame,
182 wxWindowID id, const wxPoint& pos = wxDefaultPosition,
183 const wxSize& size = wxDefaultSize, long style = 0,
184 const wxString& name = wxPanelNameStr );
185
186 virtual ~SIM_PLOT_PANEL();
187
190 {
191 m_masterFrame = aFrame;
192 }
193
194 wxString GetLabelX() const
195 {
196 return m_axis_x ? m_axis_x->GetName() : "";
197 }
198
199 wxString GetLabelY1() const
200 {
201 return m_axis_y1 ? m_axis_y1->GetName() : "";
202 }
203
204 wxString GetLabelY2() const
205 {
206 return m_axis_y2 ? m_axis_y2->GetName() : "";
207 }
208
209 bool TraceShown( const wxString& aName ) const
210 {
211 return m_traces.count( aName ) > 0;
212 }
213
214 const std::map<wxString, TRACE*>& GetTraces() const
215 {
216 return m_traces;
217 }
218
219 TRACE* GetTrace( const wxString& aName ) const
220 {
221 auto trace = m_traces.find( aName );
222
223 return trace == m_traces.end() ? nullptr : trace->second;
224 }
225
226 void ShowGrid( bool aEnable )
227 {
228 m_axis_x->SetTicks( !aEnable );
229 m_axis_y1->SetTicks( !aEnable );
230 m_axis_y2->SetTicks( !aEnable );
232 }
233
234 bool IsGridShown() const
235 {
236 if( !m_axis_x || !m_axis_y1 )
237 return false;
238
239 assert( m_axis_x->GetTicks() == m_axis_y1->GetTicks() );
240 return !m_axis_x->GetTicks();
241 }
242
243 void ShowLegend( bool aEnable )
244 {
245 m_legend->SetVisible( aEnable );
247 }
248
249 bool IsLegendShown() const
250 {
251 return m_legend->IsVisible();
252 }
253
254 void SetDottedCurrentPhase( bool aEnable )
255 {
256 m_dotted_cp = aEnable;
257
258 for( const auto& tr : m_traces )
259 {
260 UpdateTraceStyle( tr.second );
261 }
262
264 }
265
267 {
268 return m_dotted_cp;
269 }
270
272 bool HasCursorEnabled( const wxString& aName ) const;
273
275 void EnableCursor( const wxString& aName, bool aEnable );
276
278 void ResetScales();
279
281 void UpdateTraceStyle( TRACE* trace );
282
284 void UpdatePlotColors();
285
288 {
289 return m_plotWin;
290 }
291
292protected:
293 bool addTrace( const wxString& aTitle, const wxString& aName, int aPoints, const double* aX,
294 const double* aY, SIM_PLOT_TYPE aType );
295
296 bool deleteTrace( const wxString& aName );
297
298 void deleteAllTraces();
299
300private:
302 void prepareDCAxes();
303
305 void updateAxes();
306
308
309 // Top-level plot window
311 wxBoxSizer* m_sizer;
312
313 // Traces to be plotted
314 std::map<wxString, TRACE*> m_traces;
315
320
322
323 std::vector<mpLayer*> m_topLevel;
324
326};
327
328wxDECLARE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );
329
330#endif
const char * name
Definition: DXF_plotter.cpp:56
Cursor attached to a trace to follow its values:
mpWindow * m_window
const wxRealPoint & GetCoords() const
void Move(wxPoint aDelta) override
Moves the layer rectangle of given pixel deltas.
void SetX(int aX)
wxRealPoint m_coords
bool m_updateRef
static constexpr int DRAG_MARGIN
void UpdateReference() override
Updates the rectangle reference point.
bool m_updateRequired
bool Inside(wxPoint &aPoint) override
Checks whether a point is inside the info box rectangle.
void Plot(wxDC &aDC, mpWindow &aWindow) override
Plot method.
CURSOR(const TRACE *aTrace, SIM_PLOT_PANEL *aPlotPanel)
const TRACE * m_trace
void Update()
Implementing SIM_PLOT_FRAME_BASE.
mpScaleY * m_axis_y1
bool addTrace(const wxString &aTitle, const wxString &aName, int aPoints, const double *aX, const double *aY, SIM_PLOT_TYPE aType)
void UpdatePlotColors()
‍Update plot colors
void ShowLegend(bool aEnable)
wxBoxSizer * m_sizer
SIM_PLOT_COLORS m_colors
TRACE * GetTrace(const wxString &aName) const
void EnableCursor(const wxString &aName, bool aEnable)
Reset scale ranges to fit the current traces.
mpInfoLegend * m_legend
void updateAxes()
‍Create/Ensure axes are available for plotting
void SetMasterFrame(SIM_PLOT_FRAME *aFrame)
void UpdateTraceStyle(TRACE *trace)
void ResetScales()
Update trace line style.
bool IsGridShown() const
wxString GetLabelY2() const
bool IsLegendShown() const
virtual ~SIM_PLOT_PANEL()
set the pointer to the sim plot frame
bool TraceShown(const wxString &aName) const
bool GetDottedCurrentPhase() const
Returns true if the trace has cursor shown.
std::map< wxString, TRACE * > m_traces
mpScaleXBase * m_axis_x
SIM_PLOT_FRAME * m_masterFrame
SIM_PLOT_PANEL(const wxString &aCommand, wxWindow *parent, SIM_PLOT_FRAME *aMainFrame, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxString &name=wxPanelNameStr)
const std::map< wxString, TRACE * > & GetTraces() const
bool deleteTrace(const wxString &aName)
mpWindow * m_plotWin
void SetDottedCurrentPhase(bool aEnable)
bool HasCursorEnabled(const wxString &aName) const
Toggle cursor for a particular trace.
mpWindow * GetPlotWin() const
mpScaleY * m_axis_y2
void ShowGrid(bool aEnable)
wxString GetLabelY1() const
std::vector< mpLayer * > m_topLevel
wxString GetLabelX() const
void SetCursor(CURSOR *aCursor)
wxColour m_traceColour
CURSOR * m_cursor
SIM_PLOT_TYPE m_type
wxColour GetTraceColour()
wxString m_param
< Name of the signal parameter
TRACE(const wxString &aName, SIM_PLOT_TYPE aType)
const wxString & GetParam() const
void SetTraceColour(wxColour aColour)
bool HasCursor() const
void SetData(const std::vector< double > &aX, const std::vector< double > &aY) override
Assigns new data set for the trace.
SIM_PLOT_TYPE GetType() const
const std::vector< double > & GetDataY() const
const std::vector< double > & GetDataX() const
CURSOR * GetCursor() const
A class providing graphs functionality for a 2D plot (either continuous or a set of points),...
Definition: mathplot.h:1570
std::vector< double > m_ys
Definition: mathplot.h:1593
std::vector< double > m_xs
The internal copy of the set of data to draw.
Definition: mathplot.h:1593
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:2880
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:132
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
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:714
bool GetTicks() const
Get X axis ticks or grid.
Definition: mathplot.h:719
Plot layer implementing a y-scale ruler.
Definition: mathplot.h:942
void SetTicks(bool ticks)
Set Y axis ticks or grid.
Definition: mathplot.h:969
bool GetTicks() const
Get Y axis ticks or grid.
Definition: mathplot.h:973
Canvas for plotting mpLayer implementations.
Definition: mathplot.h:1047
void UpdateAll()
Refresh display.
Definition: mathplot.cpp:2403
Class is responsible for providing colors for traces on simulation plot.
wxDECLARE_EVENT(EVT_SIM_CURSOR_UPDATE, wxCommandEvent)
SIM_PLOT_TYPE
Definition: sim_types.h:47