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/sizer.h>
35 #include "sim_panel_base.h"
36 
37 class SIM_PLOT_FRAME;
38 class SIM_PLOT_PANEL;
39 class TRACE;
40 
42 class CURSOR : public mpInfoLayer
43 {
44 public:
45  CURSOR( const TRACE* aTrace, SIM_PLOT_PANEL* aPlotPanel )
46  : mpInfoLayer( wxRect( 0, 0, DRAG_MARGIN, DRAG_MARGIN ), wxTRANSPARENT_BRUSH ),
47  m_trace( aTrace ), m_updateRequired( true ), m_updateRef( false ),
48  m_coords( 0.0, 0.0 ), m_window( nullptr ), m_plotPanel( aPlotPanel )
49  {
50  SetDrawOutsideMargins( false );
51  }
52 
53  void Plot( wxDC& aDC, mpWindow& aWindow ) override;
54 
55  void SetX( int aX )
56  {
57  m_reference.x = 0;
58  m_updateRef = true;
59  Move( wxPoint( aX, 0 ) );
60  }
61 
62  void Update()
63  {
64  m_updateRequired = true;
65  }
66 
67  bool Inside( wxPoint& aPoint ) override;
68 
69  void Move( wxPoint aDelta ) override
70  {
71  Update();
72  mpInfoLayer::Move( aDelta );
73  }
74 
75  void UpdateReference() override;
76 
77  const wxRealPoint& GetCoords() const
78  {
79  return m_coords;
80  }
81 
82 private:
83  const TRACE* m_trace;
85  wxRealPoint m_coords;
88 
89  static constexpr int DRAG_MARGIN = 10;
90 };
91 
92 
93 class TRACE : public mpFXYVector
94 {
95 public:
96  TRACE( const wxString& aName ) :
97  mpFXYVector( aName ), m_cursor( nullptr ), m_flags( 0 )
98  {
99  SetContinuity( true );
100  SetDrawOutsideMargins( false );
101  ShowName( false );
102  }
103 
110  void SetData( const std::vector<double>& aX, const std::vector<double>& aY ) override
111  {
112  if( m_cursor )
113  m_cursor->Update();
114 
115  mpFXYVector::SetData( aX, aY );
116  }
117 
118  const std::vector<double>& GetDataX() const
119  {
120  return m_xs;
121  }
122 
123  const std::vector<double>& GetDataY() const
124  {
125  return m_ys;
126  }
127 
128  bool HasCursor() const
129  {
130  return m_cursor != nullptr;
131  }
132 
133  void SetCursor( CURSOR* aCursor )
134  {
135  m_cursor = aCursor;
136  }
137 
138  CURSOR* GetCursor() const
139  {
140  return m_cursor;
141  }
142 
143  void SetFlags( int aFlags )
144  {
145  m_flags = aFlags;
146  }
147 
148  int GetFlags() const
149  {
150  return m_flags;
151  }
152 
153  void SetTraceColour( wxColour aColour )
154  {
155  m_traceColour = aColour;
156  }
157 
158  wxColour GetTraceColour()
159  {
160  return m_traceColour;
161  }
162 
163 protected:
165  int m_flags;
166  wxColour m_traceColour;
167 };
168 
169 
171 {
172 public:
173  SIM_PLOT_PANEL( wxString aCommand, wxWindow* parent, SIM_PLOT_FRAME* aMainFrame, wxWindowID id,
174  const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
175  long style = 0, const wxString& name = wxPanelNameStr );
176 
177  virtual ~SIM_PLOT_PANEL();
178 
181  {
182  m_masterFrame = aFrame;
183  }
184 
185  wxString GetLabelX() const
186  {
187  return m_axis_x ? m_axis_x->GetName() : "";
188  }
189 
190  wxString GetLabelY1() const
191  {
192  return m_axis_y1 ? m_axis_y1->GetName() : "";
193  }
194 
195  wxString GetLabelY2() const
196  {
197  return m_axis_y2 ? m_axis_y2->GetName() : "";
198  }
199 
200  bool AddTrace( const wxString& aName, int aPoints, const double* aX,
201  const double* aY, SIM_PLOT_TYPE aFlags );
202 
203  bool DeleteTrace( const wxString& aName );
204 
205  void DeleteAllTraces();
206 
207  bool TraceShown( const wxString& aName ) const
208  {
209  return m_traces.count( aName ) > 0;
210  }
211 
212  const std::map<wxString, TRACE*>& GetTraces() const
213  {
214  return m_traces;
215  }
216 
217  TRACE* GetTrace( const wxString& aName ) const
218  {
219  auto trace = m_traces.find( aName );
220 
221  return trace == m_traces.end() ? NULL : trace->second;
222  }
223 
224  void ShowGrid( bool aEnable )
225  {
226  m_axis_x->SetTicks( !aEnable );
227  m_axis_y1->SetTicks( !aEnable );
228  m_axis_y2->SetTicks( !aEnable );
229  m_plotWin->UpdateAll();
230  }
231 
232  bool IsGridShown() const
233  {
234  if( !m_axis_x || !m_axis_y1 )
235  return false;
236 
237  assert( m_axis_x->GetTicks() == m_axis_y1->GetTicks() );
238  return !m_axis_x->GetTicks();
239  }
240 
241  void ShowLegend( bool aEnable )
242  {
243  m_legend->SetVisible( aEnable );
244  m_plotWin->UpdateAll();
245  }
246 
247  bool IsLegendShown() const
248  {
249  return m_legend->IsVisible();
250  }
251 
252  void SetDottedCurrentPhase( bool aEnable )
253  {
254  m_dotted_cp = aEnable;
255 
256  for( const auto& tr : m_traces )
257  {
258  UpdateTraceStyle( tr.second );
259  }
260 
261  m_plotWin->UpdateAll();
262  }
263 
265  {
266  return m_dotted_cp;
267  }
268 
270  bool HasCursorEnabled( const wxString& aName ) const;
271 
273  void EnableCursor( const wxString& aName, bool aEnable );
274 
276  void ResetScales();
277 
279  void UpdateTraceStyle( TRACE* trace );
280 
286  wxColour GetPlotColor( int aIndex );
287 
289  void UpdatePlotColors();
290 
293  {
294  return m_plotWin;
295  }
296 
297 private:
299  wxColour generateColor();
300 
302  void prepareDCAxes();
303 
304  // Color index to get a new color from the palette
305  unsigned int m_colorIdx;
306 
307  // Top-level plot window
309  wxBoxSizer* m_sizer;
310 
311  // Traces to be plotted
312  std::map<wxString, TRACE*> m_traces;
313 
318 
320 
321  std::vector<mpLayer*> m_topLevel;
322 
324 };
325 
326 wxDECLARE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );
327 
328 #endif
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: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
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
Toggles 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: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()
set the pointer to the sim plot frame
int m_flags
TRACE * GetTrace(const wxString &aName) const
SIM_PLOT_PANEL * m_plotPanel
wxString GetLabelY1() const
void UpdateTraceStyle(TRACE *trace)
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:2750
void EnableCursor(const wxString &aName, bool aEnable)
Resets scale ranges to fit the current traces.
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
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()
Update trace line style.
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
wxPoint m_reference
Definition: mathplot.h:397
void UpdatePlotColors()
Getter for math plot window.
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:3291
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
SIM_PLOT_PANEL(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)
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
Definition: sim_types.h:46
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)