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 <widgets/mathplot.h>
31 #include <map>
32 #include "sim_types.h"
33 
34 class TRACE;
35 
37 class CURSOR : public mpInfoLayer
38 {
39 public:
40  CURSOR( const TRACE* aTrace )
41  : mpInfoLayer( wxRect( 0, 0, DRAG_MARGIN, DRAG_MARGIN ), wxTRANSPARENT_BRUSH ),
42  m_trace( aTrace ), m_updateRequired( true ), m_updateRef( false ),
43  m_coords( 0.0, 0.0 ), m_window( nullptr )
44  {
45  SetDrawOutsideMargins( false );
46  }
47 
48  void Plot( wxDC& aDC, mpWindow& aWindow ) override;
49 
50  void SetX( int aX )
51  {
52  m_reference.x = 0;
53  m_updateRef = true;
54  Move( wxPoint( aX, 0 ) );
55  }
56 
57  void Update()
58  {
59  m_updateRequired = true;
60  }
61 
62  bool Inside( wxPoint& aPoint ) override;
63 
64  void Move( wxPoint aDelta ) override
65  {
66  Update();
67  mpInfoLayer::Move( aDelta );
68  }
69 
70  void UpdateReference() override;
71 
72  const wxRealPoint& GetCoords() const
73  {
74  return m_coords;
75  }
76 
77 private:
78  const TRACE* m_trace;
80  wxRealPoint m_coords;
82 
83  static constexpr int DRAG_MARGIN = 10;
84 };
85 
86 
87 class TRACE : public mpFXYVector
88 {
89 public:
90  TRACE( const wxString& aName ) :
91  mpFXYVector( aName ), m_cursor( nullptr ), m_flags( 0 )
92  {
93  SetContinuity( true );
94  SetDrawOutsideMargins( false );
95  ShowName( false );
96  }
97 
103  void SetData( const std::vector<double>& aX, const std::vector<double>& aY ) override
104  {
105  if( m_cursor )
106  m_cursor->Update();
107 
108  mpFXYVector::SetData( aX, aY );
109  }
110 
111  const std::vector<double>& GetDataX() const
112  {
113  return m_xs;
114  }
115 
116  const std::vector<double>& GetDataY() const
117  {
118  return m_ys;
119  }
120 
121  bool HasCursor() const
122  {
123  return m_cursor != nullptr;
124  }
125 
126  void SetCursor( CURSOR* aCursor )
127  {
128  m_cursor = aCursor;
129  }
130 
131  CURSOR* GetCursor() const
132  {
133  return m_cursor;
134  }
135 
136  void SetFlags( int aFlags )
137  {
138  m_flags = aFlags;
139  }
140 
141  int GetFlags() const
142  {
143  return m_flags;
144  }
145 
146  void SetTraceColour( wxColour aColour )
147  {
148  m_traceColour = aColour;
149  }
150 
151  wxColour GetTraceColour()
152  {
153  return m_traceColour;
154  }
155 
156 protected:
158  int m_flags;
159  wxColour m_traceColour;
160 };
161 
162 
163 class SIM_PLOT_PANEL : public mpWindow
164 {
165 public:
166  SIM_PLOT_PANEL( SIM_TYPE aType, wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition,
167  const wxSize& size = wxDefaultSize, long style = 0, const wxString& name = wxPanelNameStr );
168 
169  ~SIM_PLOT_PANEL();
170 
172  {
173  return m_type;
174  }
175 
176  static bool IsPlottable( SIM_TYPE aSimType );
177 
178  wxString GetLabelX() const
179  {
180  return m_axis_x ? m_axis_x->GetName() : "";
181  }
182 
183  wxString GetLabelY1() const
184  {
185  return m_axis_y1 ? m_axis_y1->GetName() : "";
186  }
187 
188  wxString GetLabelY2() const
189  {
190  return m_axis_y2 ? m_axis_y2->GetName() : "";
191  }
192 
193  bool AddTrace( const wxString& aName, int aPoints,
194  const double* aX, const double* aY, SIM_PLOT_TYPE aFlags );
195 
196  bool DeleteTrace( const wxString& aName );
197 
198  void DeleteAllTraces();
199 
200  bool TraceShown( const wxString& aName ) const
201  {
202  return m_traces.count( aName ) > 0;
203  }
204 
205  const std::map<wxString, TRACE*>& GetTraces() const
206  {
207  return m_traces;
208  }
209 
210  TRACE* GetTrace( const wxString& aName ) const
211  {
212  auto trace = m_traces.find( aName );
213 
214  return trace == m_traces.end() ? NULL : trace->second;
215  }
216 
217  void ShowGrid( bool aEnable )
218  {
219  m_axis_x->SetTicks( !aEnable );
220  m_axis_y1->SetTicks( !aEnable );
221  m_axis_y2->SetTicks( !aEnable );
222  UpdateAll();
223  }
224 
225  bool IsGridShown() const
226  {
227  assert( m_axis_x->GetTicks() == m_axis_y1->GetTicks() );
228  return !m_axis_x->GetTicks();
229  }
230 
231  void ShowLegend( bool aEnable )
232  {
233  m_legend->SetVisible( aEnable );
234  UpdateAll();
235  }
236 
237  bool IsLegendShown() const
238  {
239  return m_legend->IsVisible();
240  }
241 
243  bool HasCursorEnabled( const wxString& aName ) const;
244 
246  void EnableCursor( const wxString& aName, bool aEnable );
247 
249  void ResetScales();
250 
251 private:
253  wxColour generateColor();
254 
255  // Color index to get a new color from the palette
256  unsigned int m_colorIdx;
257 
258  // Traces to be plotted
259  std::map<wxString, TRACE*> m_traces;
260 
265 
266  std::vector<mpLayer*> m_topLevel;
267 
269 };
270 
271 wxDECLARE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );
272 
273 #endif
SIM_PLOT_PANEL(SIM_TYPE aType, wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxString &name=wxPanelNameStr)
void Plot(wxDC &aDC, mpWindow &aWindow) override
Plot method.
void ShowName(bool show)
Shows or hides the text label with the name of the layer (default is visible).
Definition: mathplot.h:261
const wxString & GetName() const
Get layer name.
Definition: mathplot.h:237
long trace
Definition: solve.cpp:232
static constexpr int DRAG_MARGIN
const TRACE * m_trace
wxRealPoint m_coords
mpScaleY * m_axis_y1
const SIM_TYPE m_type
bool TraceShown(const wxString &aName) const
wxColour m_traceColour
const std::vector< double > & GetDataY() const
void ShowLegend(bool aEnable)
wxString GetLabelX() const
void Move(wxPoint aDelta) override
Moves the layer rectangle of given pixel deltas.
static bool IsPlottable(SIM_TYPE aSimType)
CURSOR * GetCursor() const
mpScaleXBase * m_axis_x
wxColour GetTraceColour()
int GetFlags() const
void SetContinuity(bool continuity)
Set the 'continuity' property of the layer (true:draws a continuous line, false:draws separate points...
Definition: mathplot.h:252
A class providing graphs functionality for a 2D plot (either continuous or a set of points)...
Definition: mathplot.h:1528
std::vector< double > m_xs
The internal copy of the set of data to draw.
Definition: mathplot.h:1552
std::vector< double > m_ys
Definition: mathplot.h:1552
int m_flags
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:966
bool DeleteTrace(const wxString &aName)
void UpdateAll()
Refresh display.
Definition: mathplot.cpp:2814
bool IsGridShown() const
void EnableCursor(const wxString &aName, bool aEnable)
Toggles cursor for a particular trace.
bool IsLegendShown() const
virtual void Move(wxPoint delta)
Moves the layer rectangle of given pixel deltas.
Definition: mathplot.cpp:140
wxString GetLabelY2() const
unsigned int m_colorIdx
void Update()
void SetVisible(bool show)
Sets layer visibility.
Definition: mathplot.h:301
const std::map< wxString, TRACE * > & GetTraces() const
wxString GetLabelY1() const
bool IsVisible()
Checks whether the layer is visible or not.
Definition: mathplot.h:297
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:334
std::map< wxString, TRACE * > m_traces
Plot layer implementing a y-scale ruler.
Definition: mathplot.h:934
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:708
CURSOR(const TRACE *aTrace)
void ResetScales()
Resets scale ranges to fit the current traces
bool GetTicks()
Get X axis ticks or grid.
Definition: mathplot.h:712
void ShowGrid(bool aEnable)
bool m_updateRequired
void SetDrawOutsideMargins(bool drawModeOutside)
Set Draw mode: inside or outside margins.
Definition: mathplot.h:280
SIM_TYPE GetType() const
mpWindow * m_window
wxPoint m_reference
Definition: mathplot.h:395
bool HasCursor() const
const wxRealPoint & GetCoords() const
TRACE * GetTrace(const wxString &aName) const
mpInfoLegend * m_legend
void SetX(int aX)
wxColour generateColor()
Returns a new color from the palette
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:3361
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:1039
void SetTicks(bool ticks)
Set Y axis ticks or grid.
Definition: mathplot.h:962
const char * name
const std::vector< double > & GetDataX() 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:436
void SetCursor(CURSOR *aCursor)
SIM_PLOT_TYPE
Possible plot types
Definition: sim_types.h:35
bool m_updateRef
CURSOR * m_cursor
bool Inside(wxPoint &aPoint) override
Checks whether a point is inside the info box rectangle.
TRACE(const wxString &aName)
bool HasCursorEnabled(const wxString &aName) const
Returns true if the trace has cursor shown.
Cursor attached to a trace to follow its values:
void SetTraceColour(wxColour aColour)