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-2017 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 3
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * https://www.gnu.org/licenses/gpl-3.0.html
22  * or you may search the http://www.gnu.org website for the version 3 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #ifndef __SIM_PLOT_PANEL_H
28 #define __SIM_PLOT_PANEL_H
29 
30 #include "sim_types.h"
31 #include <map>
32 #include <widgets/mathplot.h>
33 #include <wx/sizer.h>
34 #include "sim_panel_base.h"
35 
36 class SIM_PLOT_FRAME;
37 class SIM_PLOT_PANEL;
38 class TRACE;
39 
41 class CURSOR : public mpInfoLayer
42 {
43 public:
44  CURSOR( const TRACE* aTrace, SIM_PLOT_PANEL* aPlotPanel )
45  : mpInfoLayer( wxRect( 0, 0, DRAG_MARGIN, DRAG_MARGIN ), wxTRANSPARENT_BRUSH ),
46  m_trace( aTrace ), m_updateRequired( true ), m_updateRef( false ),
47  m_coords( 0.0, 0.0 ), m_window( nullptr ), m_plotPanel( aPlotPanel )
48  {
49  SetDrawOutsideMargins( false );
50  }
51 
52  void Plot( wxDC& aDC, mpWindow& aWindow ) override;
53 
54  void SetX( int aX )
55  {
56  m_reference.x = 0;
57  m_updateRef = true;
58  Move( wxPoint( aX, 0 ) );
59  }
60 
61  void Update()
62  {
63  m_updateRequired = true;
64  }
65 
66  bool Inside( wxPoint& aPoint ) override;
67 
68  void Move( wxPoint aDelta ) override
69  {
70  Update();
71  mpInfoLayer::Move( aDelta );
72  }
73 
74  void UpdateReference() override;
75 
76  const wxRealPoint& GetCoords() const
77  {
78  return m_coords;
79  }
80 
81 private:
82  const TRACE* m_trace;
84  wxRealPoint m_coords;
87 
88  static constexpr int DRAG_MARGIN = 10;
89 };
90 
91 
92 class TRACE : public mpFXYVector
93 {
94 public:
95  TRACE( const wxString& aName ) :
96  mpFXYVector( aName ), m_cursor( nullptr ), m_flags( 0 )
97  {
98  SetContinuity( true );
99  SetDrawOutsideMargins( false );
100  ShowName( false );
101  }
102 
108  void SetData( const std::vector<double>& aX, const std::vector<double>& aY ) override
109  {
110  if( m_cursor )
111  m_cursor->Update();
112 
113  mpFXYVector::SetData( aX, aY );
114  }
115 
116  const std::vector<double>& GetDataX() const
117  {
118  return m_xs;
119  }
120 
121  const std::vector<double>& GetDataY() const
122  {
123  return m_ys;
124  }
125 
126  bool HasCursor() const
127  {
128  return m_cursor != nullptr;
129  }
130 
131  void SetCursor( CURSOR* aCursor )
132  {
133  m_cursor = aCursor;
134  }
135 
136  CURSOR* GetCursor() const
137  {
138  return m_cursor;
139  }
140 
141  void SetFlags( int aFlags )
142  {
143  m_flags = aFlags;
144  }
145 
146  int GetFlags() const
147  {
148  return m_flags;
149  }
150 
151  void SetTraceColour( wxColour aColour )
152  {
153  m_traceColour = aColour;
154  }
155 
156  wxColour GetTraceColour()
157  {
158  return m_traceColour;
159  }
160 
161 protected:
163  int m_flags;
164  wxColour m_traceColour;
165 };
166 
167 
169 {
170 public:
171  SIM_PLOT_PANEL( SIM_TYPE aType, wxWindow* parent, SIM_PLOT_FRAME* aMainFrame,
172  wxWindowID id, const wxPoint& pos = wxDefaultPosition,
173  const wxSize& size = wxDefaultSize, long style = 0, const wxString& name = wxPanelNameStr );
174 
175  virtual ~SIM_PLOT_PANEL();
176 
179  {
180  m_masterFrame = aFrame;
181  }
182 
183  wxString GetLabelX() const
184  {
185  return m_axis_x ? m_axis_x->GetName() : "";
186  }
187 
188  wxString GetLabelY1() const
189  {
190  return m_axis_y1 ? m_axis_y1->GetName() : "";
191  }
192 
193  wxString GetLabelY2() const
194  {
195  return m_axis_y2 ? m_axis_y2->GetName() : "";
196  }
197 
198  bool AddTrace( const wxString& aName, int aPoints,
199  const double* aX, const double* aY, SIM_PLOT_TYPE aFlags );
200 
201  bool DeleteTrace( const wxString& aName );
202 
203  void DeleteAllTraces();
204 
205  bool TraceShown( const wxString& aName ) const
206  {
207  return m_traces.count( aName ) > 0;
208  }
209 
210  const std::map<wxString, TRACE*>& GetTraces() const
211  {
212  return m_traces;
213  }
214 
215  TRACE* GetTrace( const wxString& aName ) const
216  {
217  auto trace = m_traces.find( aName );
218 
219  return trace == m_traces.end() ? NULL : trace->second;
220  }
221 
222  void ShowGrid( bool aEnable )
223  {
224  m_axis_x->SetTicks( !aEnable );
225  m_axis_y1->SetTicks( !aEnable );
226  m_axis_y2->SetTicks( !aEnable );
227  m_plotWin->UpdateAll();
228  }
229 
230  bool IsGridShown() const
231  {
232  if( !m_axis_x || !m_axis_y1 )
233  return false;
234 
235  assert( m_axis_x->GetTicks() == m_axis_y1->GetTicks() );
236  return !m_axis_x->GetTicks();
237  }
238 
239  void ShowLegend( bool aEnable )
240  {
241  m_legend->SetVisible( aEnable );
242  m_plotWin->UpdateAll();
243  }
244 
245  bool IsLegendShown() const
246  {
247  return m_legend->IsVisible();
248  }
249 
250  void SetDottedCurrentPhase( bool aEnable )
251  {
252  m_dotted_cp = aEnable;
253 
254  for( const auto& tr : m_traces )
255  {
256  UpdateTraceStyle( tr.second );
257  }
258 
259  m_plotWin->UpdateAll();
260  }
261 
263  {
264  return m_dotted_cp;
265  }
266 
268  bool HasCursorEnabled( const wxString& aName ) const;
269 
271  void EnableCursor( const wxString& aName, bool aEnable );
272 
274  void ResetScales();
275 
277  void UpdateTraceStyle( TRACE* trace );
278 
284  wxColour GetPlotColor( int aIndex );
285 
287  void UpdatePlotColors();
288 
291  {
292  return m_plotWin;
293  }
294 
295 private:
297  wxColour generateColor();
298 
299  // Color index to get a new color from the palette
300  unsigned int m_colorIdx;
301 
302  // Top-level plot window
304  wxBoxSizer* m_sizer;
305 
306  // Traces to be plotted
307  std::map<wxString, TRACE*> m_traces;
308 
313 
315 
316  std::vector<mpLayer*> m_topLevel;
317 
319 };
320 
321 wxDECLARE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );
322 
323 #endif
void Plot(wxDC &aDC, mpWindow &aWindow) override
Plot method.
void SetMasterFrame(SIM_PLOT_FRAME *aFrame)
set the pointer to the sim plot frame
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:263
static constexpr int DRAG_MARGIN
mpWindow * m_plotWin
const TRACE * m_trace
const wxString & GetName() const
Get layer name.
Definition: mathplot.h:239
wxRealPoint m_coords
mpWindow * GetPlotWin() const
Getter for math plot window
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
Returns true if the trace has cursor shown.
wxColour GetTraceColour()
bool IsGridShown() const
bool GetDottedCurrentPhase() const
void SetContinuity(bool continuity)
Set the 'continuity' property of the layer (true:draws a continuous line, false:draws separate points...
Definition: mathplot.h:254
A class providing graphs functionality for a 2D plot (either continuous or a set of points),...
Definition: mathplot.h:1566
void SetDottedCurrentPhase(bool aEnable)
std::vector< double > m_xs
The internal copy of the set of data to draw.
Definition: mathplot.h:1590
SIM_PLOT_FRAME * m_masterFrame
std::vector< double > m_ys
Definition: mathplot.h:1590
virtual ~SIM_PLOT_PANEL()
int m_flags
TRACE * GetTrace(const wxString &aName) const
SIM_PLOT_PANEL * m_plotPanel
wxString GetLabelY1() const
void UpdateTraceStyle(TRACE *trace)
Update trace line style
void UpdateReference() override
Updates the rectangle reference point.
void SetFlags(int aFlags)
wxDECLARE_EVENT(EVT_SIM_CURSOR_UPDATE, wxCommandEvent)
bool GetTicks()
Get Y axis ticks or grid.
Definition: mathplot.h:974
bool DeleteTrace(const wxString &aName)
const wxRealPoint & GetCoords() const
void UpdateAll()
Refresh display.
Definition: mathplot.cpp:2748
void EnableCursor(const wxString &aName, bool aEnable)
Toggles cursor for a particular trace.
virtual void Move(wxPoint delta)
Moves the layer rectangle of given pixel deltas.
Definition: mathplot.cpp:141
wxString GetLabelX() const
#define NULL
unsigned int m_colorIdx
void Update()
void SetVisible(bool show)
Sets layer visibility.
Definition: mathplot.h:303
wxString GetLabelY2() const
bool IsVisible()
Checks whether the layer is visible or not.
Definition: mathplot.h:299
std::vector< mpLayer * > m_topLevel
SIM_TYPE
Possible simulation types
Definition: sim_types.h:29
Base class to create small rectangular info boxes mpInfoLayer is the base class to create a small rec...
Definition: mathplot.h:336
std::map< wxString, TRACE * > m_traces
Plot layer implementing a y-scale ruler.
Definition: mathplot.h:942
bool AddTrace(const wxString &aName, int aPoints, const double *aX, const double *aY, SIM_PLOT_TYPE aFlags)
mpScaleY * m_axis_y2
void SetTicks(bool enable)
Set X axis ticks or grid.
Definition: mathplot.h:715
void ResetScales()
Resets scale ranges to fit the current traces
bool GetTicks()
Get X axis ticks or grid.
Definition: mathplot.h:720
void ShowGrid(bool aEnable)
bool m_updateRequired
void SetDrawOutsideMargins(bool drawModeOutside)
Set Draw mode: inside or outside margins.
Definition: mathplot.h:282
Implementing SIM_PLOT_FRAME_BASE.
mpWindow * m_window
SIM_PLOT_PANEL(SIM_TYPE aType, wxWindow *parent, SIM_PLOT_FRAME *aMainFrame, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxString &name=wxPanelNameStr)
wxPoint m_reference
Definition: mathplot.h:397
void UpdatePlotColors()
Update plot colors
const char * name
Definition: DXF_plotter.cpp:59
CURSOR(const TRACE *aTrace, SIM_PLOT_PANEL *aPlotPanel)
int GetFlags() const
mpInfoLegend * m_legend
void SetX(int aX)
CURSOR * GetCursor() const
wxColour generateColor()
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:3289
void SetData(const std::vector< double > &aX, const std::vector< double > &aY) override
Assigns new data set for the trace.
Canvas for plotting mpLayer implementations.
Definition: mathplot.h:1047
void SetTicks(bool ticks)
Set Y axis ticks or grid.
Definition: mathplot.h:970
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:438
const std::map< wxString, TRACE * > & GetTraces() const
wxColour GetPlotColor(int aIndex)
A proxy to SIM_PLOT_FRAME::GetPlotColor()
void SetCursor(CURSOR *aCursor)
SIM_PLOT_TYPE
Possible plot types
Definition: sim_types.h:44
bool m_updateRef
CURSOR * m_cursor
bool Inside(wxPoint &aPoint) override
Checks whether a point is inside the info box rectangle.
wxBoxSizer * m_sizer
TRACE(const wxString &aName)
bool TraceShown(const wxString &aName) const
Cursor attached to a trace to follow its values:
void SetTraceColour(wxColour aColour)