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 <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 
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 
39 class SIM_PLOT_FRAME;
40 class SIM_PLOT_PANEL;
41 class TRACE;
42 
44 class CURSOR : public mpInfoLayer
45 {
46 public:
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 
84 private:
85  const TRACE* m_trace;
87  wxRealPoint m_coords;
89 
90  static constexpr int DRAG_MARGIN = 10;
91 };
92 
93 
94 class TRACE : public mpFXYVector
95 {
96 public:
97  TRACE( const wxString& aName, SIM_PLOT_TYPE aType, const wxString& aParam ) :
98  mpFXYVector( aName ), m_cursor( nullptr ), m_type( aType ), m_param( aParam )
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 
139  CURSOR* GetCursor() const
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 
165 protected:
168  wxColour m_traceColour;
169 
170 private:
172  wxString m_param;
173 };
174 
175 
177 {
178  friend class SIM_WORKBOOK;
179 
180 public:
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 );
231  m_plotWin->UpdateAll();
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 );
246  m_plotWin->UpdateAll();
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 
263  m_plotWin->UpdateAll();
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 
292 protected:
293  bool addTrace( const wxString& aTitle, const wxString& aName, int aPoints, const double* aX,
294  const double* aY, SIM_PLOT_TYPE aType, const wxString& aParam );
295 
296  bool deleteTrace( const wxString& aName );
297 
298  void deleteAllTraces();
299 
300 private:
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 
328 wxDECLARE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );
329 
330 #endif
bool addTrace(const wxString &aTitle, const wxString &aName, int aPoints, const double *aX, const double *aY, SIM_PLOT_TYPE aType, const wxString &aParam)
void Plot(wxDC &aDC, mpWindow &aWindow) override
Plot method.
void SetMasterFrame(SIM_PLOT_FRAME *aFrame)
bool IsLegendShown() const
void ShowName(bool show)
Shows or hides the text label with the name of the layer (default is visible).
Definition: mathplot.h:262
static constexpr int DRAG_MARGIN
mpWindow * m_plotWin
const TRACE * m_trace
const wxString & GetName() const
Get layer name.
Definition: mathplot.h:238
wxRealPoint m_coords
mpWindow * GetPlotWin() const
mpScaleY * m_axis_y1
bool HasCursor() const
wxColour m_traceColour
void ShowLegend(bool aEnable)
void Move(wxPoint aDelta) override
Moves the layer rectangle of given pixel deltas.
const std::vector< double > & GetDataX() const
mpScaleXBase * m_axis_x
bool HasCursorEnabled(const wxString &aName) const
Toggle cursor for a particular trace.
wxColour GetTraceColour()
bool IsGridShown() const
bool GetDottedCurrentPhase() const
Returns true if the trace has cursor shown.
void SetContinuity(bool continuity)
Set the 'continuity' property of the layer (true:draws a continuous line, false:draws separate points...
Definition: mathplot.h:253
A class providing graphs functionality for a 2D plot (either continuous or a set of points),...
Definition: mathplot.h:1569
void SetDottedCurrentPhase(bool aEnable)
std::vector< double > m_xs
The internal copy of the set of data to draw.
Definition: mathplot.h:1593
SIM_PLOT_FRAME * m_masterFrame
SIM_PLOT_TYPE m_type
std::vector< double > m_ys
Definition: mathplot.h:1593
virtual ~SIM_PLOT_PANEL()
set the pointer to the sim plot frame
TRACE * GetTrace(const wxString &aName) const
wxString GetLabelY1() const
void UpdateTraceStyle(TRACE *trace)
void UpdateReference() override
Updates the rectangle reference point.
bool GetTicks() const
Get X axis ticks or grid.
Definition: mathplot.h:719
bool GetTicks() const
Get Y axis ticks or grid.
Definition: mathplot.h:973
wxDECLARE_EVENT(EVT_SIM_CURSOR_UPDATE, wxCommandEvent)
const wxRealPoint & GetCoords() const
void UpdateAll()
Refresh display.
Definition: mathplot.cpp:2403
void EnableCursor(const wxString &aName, bool aEnable)
Reset scale ranges to fit the current traces.
SIM_PLOT_COLORS m_colors
virtual void Move(wxPoint delta)
Moves the layer rectangle of given pixel deltas.
Definition: mathplot.cpp:132
wxString GetLabelX() const
void Update()
void SetVisible(bool show)
Sets layer visibility.
Definition: mathplot.h:302
wxString GetLabelY2() const
std::vector< mpLayer * > m_topLevel
Base class to create small rectangular info boxes mpInfoLayer is the base class to create a small rec...
Definition: mathplot.h:335
std::map< wxString, TRACE * > m_traces
Plot layer implementing a y-scale ruler.
Definition: mathplot.h:941
bool IsVisible() const
Checks whether the layer is visible or not.
Definition: mathplot.h:298
mpScaleY * m_axis_y2
SIM_PLOT_TYPE GetType() const
void SetTicks(bool enable)
Set X axis ticks or grid.
Definition: mathplot.h:714
void updateAxes()
Create/Ensure axes are available for plotting
void ResetScales()
Update trace line style.
wxString m_param
< Name of the signal parameter
void ShowGrid(bool aEnable)
bool m_updateRequired
void SetDrawOutsideMargins(bool drawModeOutside)
Set Draw mode: inside or outside margins.
Definition: mathplot.h:281
Implementing SIM_PLOT_FRAME_BASE.
mpWindow * m_window
wxPoint m_reference
Definition: mathplot.h:396
void UpdatePlotColors()
Update plot colors
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 char * name
Definition: DXF_plotter.cpp:56
CURSOR(const TRACE *aTrace, SIM_PLOT_PANEL *aPlotPanel)
bool deleteTrace(const wxString &aName)
mpInfoLegend * m_legend
void SetX(int aX)
CURSOR * GetCursor() const
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
void SetData(const std::vector< double > &aX, const std::vector< double > &aY) override
Assigns new data set for the trace.
const wxString & GetParam() const
Canvas for plotting mpLayer implementations.
Definition: mathplot.h:1046
void SetTicks(bool ticks)
Set Y axis ticks or grid.
Definition: mathplot.h:969
const std::vector< double > & GetDataY() const
Implements the legend to be added to the plot This layer allows you to add a legend to describe the p...
Definition: mathplot.h:437
const std::map< wxString, TRACE * > & GetTraces() const
void SetCursor(CURSOR *aCursor)
SIM_PLOT_TYPE
Definition: sim_types.h:46
bool m_updateRef
Class is responsible for providing colors for traces on simulation plot.
CURSOR * m_cursor
bool Inside(wxPoint &aPoint) override
Checks whether a point is inside the info box rectangle.
wxBoxSizer * m_sizer
bool TraceShown(const wxString &aName) const
Cursor attached to a trace to follow its values:
TRACE(const wxString &aName, SIM_PLOT_TYPE aType, const wxString &aParam)
void SetTraceColour(wxColour aColour)