2016-08-11 12:41:07 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2023-01-01 23:37:24 +00:00
|
|
|
* Copyright (C) 2016-2023 CERN
|
|
|
|
* Copyright (C) 2016-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2021-01-26 15:23:37 +00:00
|
|
|
*
|
2016-08-11 12:41:07 +00:00
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2016-08-11 12:41:21 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
2016-08-11 12:41:07 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2016-08-11 12:41:43 +00:00
|
|
|
* as published by the Free Software Foundation; either version 3
|
2016-08-11 12:41:07 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
2016-08-11 12:41:43 +00:00
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
2016-08-11 12:41:07 +00:00
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2016-08-11 12:41:01 +00:00
|
|
|
#ifndef __SIM_PLOT_PANEL_H
|
|
|
|
#define __SIM_PLOT_PANEL_H
|
|
|
|
|
2016-08-11 12:41:35 +00:00
|
|
|
#include "sim_types.h"
|
2020-04-15 01:51:58 +00:00
|
|
|
#include <map>
|
|
|
|
#include <widgets/mathplot.h>
|
2021-01-21 21:17:13 +00:00
|
|
|
#include <wx/colour.h>
|
2020-04-15 01:51:58 +00:00
|
|
|
#include <wx/sizer.h>
|
|
|
|
#include "sim_panel_base.h"
|
2021-01-21 21:17:13 +00:00
|
|
|
#include "sim_plot_colors.h"
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2023-03-03 22:36:07 +00:00
|
|
|
class SIMULATOR_FRAME;
|
2020-02-01 12:05:44 +00:00
|
|
|
class SIM_PLOT_PANEL;
|
2016-08-11 12:41:25 +00:00
|
|
|
class TRACE;
|
2016-08-11 12:41:22 +00:00
|
|
|
|
2023-03-03 22:36:07 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* The SIMULATOR_FRAME holds the main user-interface for running simulations.
|
|
|
|
*
|
|
|
|
* It contains a workbook with multiple tabs, each tab holding a SIM_PLOT_PANEL, a specific
|
|
|
|
* simulation command (.TRAN, .AC, etc.), and simulation settings (save all currents, etc.).
|
|
|
|
*
|
|
|
|
* Each plot can have multiple TRACEs. While internally each TRACE can have multiple cursors,
|
|
|
|
* the GUI supports only two cursors (and a differential cursor) for each plot.
|
|
|
|
*
|
|
|
|
* TRACEs are identified by a signal (V(OUT), I(R2), etc.) and a type (SPT_VOLTAGE, SPT_AC_PHASE,
|
|
|
|
* etc.).
|
|
|
|
*
|
|
|
|
* The simulator outputs simple signals in a vector of the same name. Complex signals (such as
|
|
|
|
* V(OUT) / V(IN)) are stored in vectors of the format "user%d".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< Cursor attached to a trace to follow its values:
|
2016-08-11 12:41:22 +00:00
|
|
|
class CURSOR : public mpInfoLayer
|
|
|
|
{
|
|
|
|
public:
|
2023-01-29 17:35:19 +00:00
|
|
|
CURSOR( TRACE* aTrace, SIM_PLOT_PANEL* aPlotPanel ) :
|
2021-01-21 21:17:13 +00:00
|
|
|
mpInfoLayer( wxRect( 0, 0, DRAG_MARGIN, DRAG_MARGIN ), wxTRANSPARENT_BRUSH ),
|
2023-01-14 00:17:33 +00:00
|
|
|
m_trace( aTrace ),
|
|
|
|
m_updateRequired( true ),
|
|
|
|
m_updateRef( false ),
|
|
|
|
m_coords( 0.0, 0.0 ),
|
2021-01-21 21:17:13 +00:00
|
|
|
m_window( nullptr )
|
2016-08-11 12:41:22 +00:00
|
|
|
{
|
2016-08-11 12:41:55 +00:00
|
|
|
SetDrawOutsideMargins( false );
|
2016-08-11 12:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Plot( wxDC& aDC, mpWindow& aWindow ) override;
|
|
|
|
|
2016-08-11 12:42:14 +00:00
|
|
|
void SetX( int aX )
|
|
|
|
{
|
|
|
|
m_reference.x = 0;
|
|
|
|
m_updateRef = true;
|
|
|
|
Move( wxPoint( aX, 0 ) );
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:41:25 +00:00
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
m_updateRequired = true;
|
|
|
|
}
|
|
|
|
|
2023-01-31 17:29:46 +00:00
|
|
|
bool Inside( const wxPoint& aPoint ) const override;
|
2016-08-11 12:41:22 +00:00
|
|
|
|
|
|
|
void Move( wxPoint aDelta ) override
|
|
|
|
{
|
2016-08-11 12:41:25 +00:00
|
|
|
Update();
|
2016-08-11 12:41:22 +00:00
|
|
|
mpInfoLayer::Move( aDelta );
|
|
|
|
}
|
|
|
|
|
2016-09-27 08:58:05 +00:00
|
|
|
void UpdateReference() override;
|
2016-08-11 12:41:22 +00:00
|
|
|
|
|
|
|
const wxRealPoint& GetCoords() const
|
|
|
|
{
|
|
|
|
return m_coords;
|
|
|
|
}
|
|
|
|
|
2023-01-29 16:01:03 +00:00
|
|
|
void SetCoordX( double aValue );
|
|
|
|
|
|
|
|
private:
|
|
|
|
void doSetCoordX( double aValue );
|
|
|
|
|
2023-01-29 17:35:19 +00:00
|
|
|
wxString getID();
|
|
|
|
|
2016-08-11 12:41:22 +00:00
|
|
|
private:
|
2023-01-29 17:35:19 +00:00
|
|
|
TRACE* m_trace;
|
|
|
|
bool m_updateRequired;
|
|
|
|
bool m_updateRef;
|
|
|
|
wxRealPoint m_coords;
|
|
|
|
mpWindow* m_window;
|
2016-08-11 12:41:22 +00:00
|
|
|
|
2017-03-19 13:13:44 +00:00
|
|
|
static constexpr int DRAG_MARGIN = 10;
|
2016-08-11 12:41:22 +00:00
|
|
|
};
|
|
|
|
|
2016-08-11 12:41:25 +00:00
|
|
|
|
|
|
|
class TRACE : public mpFXYVector
|
|
|
|
{
|
|
|
|
public:
|
2023-01-29 16:01:03 +00:00
|
|
|
TRACE( const wxString& aName, SIM_TRACE_TYPE aType ) :
|
2023-03-03 22:36:07 +00:00
|
|
|
mpFXYVector( aName ),
|
|
|
|
m_type( aType )
|
2016-08-11 12:42:17 +00:00
|
|
|
{
|
|
|
|
SetContinuity( true );
|
|
|
|
SetDrawOutsideMargins( false );
|
|
|
|
ShowName( false );
|
2023-03-19 23:02:23 +00:00
|
|
|
|
|
|
|
SetName( aName );
|
2016-08-11 12:42:17 +00:00
|
|
|
}
|
2016-08-11 12:41:25 +00:00
|
|
|
|
2023-03-19 23:02:23 +00:00
|
|
|
void SetName( const wxString& aName ) override
|
2023-02-27 21:08:05 +00:00
|
|
|
{
|
|
|
|
for( auto& [ idx, cursor ] : m_cursors )
|
|
|
|
{
|
|
|
|
if( cursor )
|
|
|
|
cursor->SetName( aName );
|
|
|
|
}
|
|
|
|
|
|
|
|
mpFXYVector::SetName( aName );
|
2023-03-19 23:02:23 +00:00
|
|
|
|
|
|
|
if( m_type & SPT_AC_MAG )
|
|
|
|
m_displayName = aName + _( " (gain)" );
|
|
|
|
else if( m_type & SPT_AC_PHASE )
|
|
|
|
m_displayName = aName + _( " (phase)" );
|
|
|
|
else
|
|
|
|
m_displayName = aName;
|
2023-02-27 21:08:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
/**
|
2021-01-26 15:23:37 +00:00
|
|
|
* Assigns new data set for the trace. aX and aY need to have the same length.
|
|
|
|
*
|
2016-08-11 12:42:17 +00:00
|
|
|
* @param aX are the X axis values.
|
|
|
|
* @param aY are the Y axis values.
|
|
|
|
*/
|
2016-08-11 12:41:55 +00:00
|
|
|
void SetData( const std::vector<double>& aX, const std::vector<double>& aY ) override
|
|
|
|
{
|
2023-01-29 16:01:03 +00:00
|
|
|
for( auto& [ idx, cursor ] : m_cursors )
|
|
|
|
{
|
|
|
|
if( cursor )
|
|
|
|
cursor->Update();
|
|
|
|
}
|
2016-08-11 12:41:55 +00:00
|
|
|
|
|
|
|
mpFXYVector::SetData( aX, aY );
|
|
|
|
}
|
|
|
|
|
2023-02-27 21:08:05 +00:00
|
|
|
const std::vector<double>& GetDataX() const { return m_xs; }
|
|
|
|
const std::vector<double>& GetDataY() const { return m_ys; }
|
2016-08-11 12:41:25 +00:00
|
|
|
|
2023-02-27 21:08:05 +00:00
|
|
|
bool HasCursor( int aCursorId ) { return m_cursors[ aCursorId ] != nullptr; }
|
2023-01-29 16:01:03 +00:00
|
|
|
|
2023-02-27 21:08:05 +00:00
|
|
|
void SetCursor( int aCursorId, CURSOR* aCursor ) { m_cursors[ aCursorId ] = aCursor; }
|
|
|
|
CURSOR* GetCursor( int aCursorId ) { return m_cursors[ aCursorId ]; }
|
|
|
|
std::map<int, CURSOR*>& GetCursors() { return m_cursors; }
|
2016-08-11 12:42:14 +00:00
|
|
|
|
2023-02-27 21:08:05 +00:00
|
|
|
SIM_TRACE_TYPE GetType() const { return m_type; }
|
2016-08-29 09:39:54 +00:00
|
|
|
|
2023-02-27 21:08:05 +00:00
|
|
|
void SetTraceColour( const wxColour& aColour ) { m_traceColour = aColour; }
|
|
|
|
wxColour GetTraceColour() const { return m_traceColour; }
|
2016-08-29 09:39:54 +00:00
|
|
|
|
2016-08-11 12:41:49 +00:00
|
|
|
protected:
|
2023-01-29 16:01:03 +00:00
|
|
|
std::map<int, CURSOR*> m_cursors; // No ownership; the mpWindow owns the CURSORs
|
|
|
|
SIM_TRACE_TYPE m_type;
|
|
|
|
wxColour m_traceColour;
|
2016-08-11 12:41:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
class SIM_PLOT_PANEL : public SIM_PANEL_BASE
|
2016-08-11 12:41:01 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-01-01 23:37:24 +00:00
|
|
|
SIM_PLOT_PANEL( const wxString& aCommand, int aOptions, wxWindow* parent, wxWindowID id,
|
2023-01-14 00:17:33 +00:00
|
|
|
const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
|
|
|
|
long style = 0, const wxString& name = wxPanelNameStr );
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
virtual ~SIM_PLOT_PANEL();
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2016-08-11 12:41:35 +00:00
|
|
|
wxString GetLabelX() const
|
|
|
|
{
|
2023-02-12 21:29:26 +00:00
|
|
|
return m_axis_x ? m_axis_x->GetName() : wxString( wxS( "" ) );
|
2016-08-11 12:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxString GetLabelY1() const
|
2016-08-11 12:41:26 +00:00
|
|
|
{
|
2023-02-12 21:29:26 +00:00
|
|
|
return m_axis_y1 ? m_axis_y1->GetName() : wxString( wxS( "" ) );
|
2016-08-11 12:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxString GetLabelY2() const
|
|
|
|
{
|
2023-02-12 21:29:26 +00:00
|
|
|
return m_axis_y2 ? m_axis_y2->GetName() : wxString( wxS( "" ) );
|
2016-08-11 12:41:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 20:52:52 +00:00
|
|
|
wxString GetLabelY3() const
|
|
|
|
{
|
2023-02-12 21:29:26 +00:00
|
|
|
return m_axis_y3 ? m_axis_y3->GetName() : wxString( wxS( "" ) );
|
2023-02-10 20:52:52 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 16:01:03 +00:00
|
|
|
wxString GetUnitsX() const;
|
2023-02-05 20:47:27 +00:00
|
|
|
wxString GetUnitsY1() const;
|
|
|
|
wxString GetUnitsY2() const;
|
2023-02-10 20:52:52 +00:00
|
|
|
wxString GetUnitsY3() const;
|
2023-01-29 16:01:03 +00:00
|
|
|
|
2016-08-11 12:41:52 +00:00
|
|
|
const std::map<wxString, TRACE*>& GetTraces() const
|
2016-08-11 12:41:18 +00:00
|
|
|
{
|
|
|
|
return m_traces;
|
|
|
|
}
|
|
|
|
|
2023-02-27 21:08:05 +00:00
|
|
|
TRACE* GetTrace( const wxString& aVecName, int aType ) const
|
2016-08-11 12:41:25 +00:00
|
|
|
{
|
2023-02-27 21:08:05 +00:00
|
|
|
auto trace = m_traces.find( getTraceId( aVecName, aType ) );
|
2016-08-11 12:41:25 +00:00
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
return trace == m_traces.end() ? nullptr : trace->second;
|
2016-08-11 12:41:25 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 12:41:27 +00:00
|
|
|
void ShowGrid( bool aEnable )
|
|
|
|
{
|
|
|
|
m_axis_x->SetTicks( !aEnable );
|
2016-08-11 12:41:35 +00:00
|
|
|
m_axis_y1->SetTicks( !aEnable );
|
|
|
|
m_axis_y2->SetTicks( !aEnable );
|
2023-02-10 20:52:52 +00:00
|
|
|
m_axis_y3->SetTicks( !aEnable );
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->UpdateAll();
|
2016-08-11 12:41:27 +00:00
|
|
|
}
|
2016-08-11 12:41:22 +00:00
|
|
|
|
2016-08-11 12:41:27 +00:00
|
|
|
bool IsGridShown() const
|
|
|
|
{
|
2017-12-18 08:33:05 +00:00
|
|
|
if( !m_axis_x || !m_axis_y1 )
|
|
|
|
return false;
|
|
|
|
|
2016-08-11 12:41:35 +00:00
|
|
|
assert( m_axis_x->GetTicks() == m_axis_y1->GetTicks() );
|
2016-08-11 12:41:27 +00:00
|
|
|
return !m_axis_x->GetTicks();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShowLegend( bool aEnable )
|
|
|
|
{
|
|
|
|
m_legend->SetVisible( aEnable );
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->UpdateAll();
|
2016-08-11 12:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsLegendShown() const
|
|
|
|
{
|
|
|
|
return m_legend->IsVisible();
|
|
|
|
}
|
|
|
|
|
2023-03-20 09:43:16 +00:00
|
|
|
wxPoint GetLegendPosition() const
|
|
|
|
{
|
|
|
|
return m_legend->GetPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetLegendPosition( const wxPoint& aPosition )
|
|
|
|
{
|
|
|
|
m_legend->Move( aPosition );
|
|
|
|
m_legend->UpdateReference();
|
|
|
|
}
|
|
|
|
|
2023-01-01 23:37:24 +00:00
|
|
|
/**
|
|
|
|
* Draw secondary signal traces (current or phase) with dotted lines
|
|
|
|
*/
|
|
|
|
void SetDottedSecondary( bool aEnable )
|
2019-11-19 16:17:37 +00:00
|
|
|
{
|
|
|
|
m_dotted_cp = aEnable;
|
|
|
|
|
|
|
|
for( const auto& tr : m_traces )
|
|
|
|
UpdateTraceStyle( tr.second );
|
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->UpdateAll();
|
2019-11-19 16:17:37 +00:00
|
|
|
}
|
|
|
|
|
2023-01-01 23:37:24 +00:00
|
|
|
bool GetDottedSecondary() const
|
2019-11-19 16:17:37 +00:00
|
|
|
{
|
|
|
|
return m_dotted_cp;
|
|
|
|
}
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
///< Toggle cursor for a particular trace.
|
2023-02-27 21:08:05 +00:00
|
|
|
void EnableCursor( const wxString& aVectorName, int aType, int aCursorId, bool aEnable,
|
|
|
|
const wxString& aSignalName );
|
2016-08-11 12:41:25 +00:00
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
///< Reset scale ranges to fit the current traces.
|
2016-08-11 12:42:04 +00:00
|
|
|
void ResetScales();
|
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< Update trace line style
|
2019-11-19 16:17:37 +00:00
|
|
|
void UpdateTraceStyle( TRACE* trace );
|
|
|
|
|
2023-03-24 15:57:52 +00:00
|
|
|
///< Update plot colors
|
2020-02-01 12:05:44 +00:00
|
|
|
void UpdatePlotColors();
|
|
|
|
|
2023-01-04 18:06:04 +00:00
|
|
|
void OnLanguageChanged() override;
|
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< Getter for math plot window
|
2023-03-03 22:36:07 +00:00
|
|
|
mpWindow* GetPlotWin() const { return m_plotWin; }
|
2020-04-15 01:51:58 +00:00
|
|
|
|
2023-03-02 00:37:12 +00:00
|
|
|
TRACE* AddTrace( const wxString& aVectorName, int aType );
|
2023-02-12 14:55:00 +00:00
|
|
|
|
|
|
|
void SetTraceData( TRACE* aTrace, unsigned int aPoints, const double* aX, const double* aY );
|
2021-06-30 00:13:10 +00:00
|
|
|
|
2023-02-27 21:08:05 +00:00
|
|
|
bool DeleteTrace( const wxString& aVectorName, int aTraceType );
|
|
|
|
void DeleteTrace( TRACE* aTrace );
|
2021-06-30 00:13:10 +00:00
|
|
|
|
2016-08-11 12:41:12 +00:00
|
|
|
private:
|
2023-03-02 00:37:12 +00:00
|
|
|
wxString getTraceId( const wxString& aVectorName, int aType ) const
|
2023-02-27 21:08:05 +00:00
|
|
|
{
|
2023-03-02 00:37:12 +00:00
|
|
|
return wxString::Format( wxS( "%s%d" ), aVectorName, aType & SPT_Y_AXIS_MASK );
|
2023-02-27 21:08:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 22:15:38 +00:00
|
|
|
///< @brief Construct the plot axes for DC simulation plot.
|
2023-02-27 21:08:05 +00:00
|
|
|
void prepareDCAxes( int aNewTraceType );
|
2021-01-25 21:29:13 +00:00
|
|
|
|
2023-03-24 15:57:52 +00:00
|
|
|
///< Create/Ensure axes are available for plotting
|
2023-02-27 21:08:05 +00:00
|
|
|
void updateAxes( int aNewTraceType = SIM_TRACE_TYPE::SPT_UNKNOWN );
|
2021-10-18 17:01:08 +00:00
|
|
|
|
2023-01-01 23:37:24 +00:00
|
|
|
private:
|
|
|
|
SIM_PLOT_COLORS m_colors;
|
2016-08-11 12:41:14 +00:00
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
// Top-level plot window
|
2023-01-01 23:37:24 +00:00
|
|
|
mpWindow* m_plotWin;
|
|
|
|
wxBoxSizer* m_sizer;
|
2020-04-15 01:51:58 +00:00
|
|
|
|
2016-08-11 12:41:21 +00:00
|
|
|
// Traces to be plotted
|
2016-08-11 12:41:52 +00:00
|
|
|
std::map<wxString, TRACE*> m_traces;
|
2016-08-11 12:41:14 +00:00
|
|
|
|
2023-01-01 23:37:24 +00:00
|
|
|
mpScaleXBase* m_axis_x;
|
|
|
|
mpScaleY* m_axis_y1;
|
|
|
|
mpScaleY* m_axis_y2;
|
2023-02-10 20:52:52 +00:00
|
|
|
mpScaleY* m_axis_y3;
|
2023-01-01 23:37:24 +00:00
|
|
|
mpInfoLegend* m_legend;
|
2019-11-19 16:17:37 +00:00
|
|
|
|
2023-01-01 23:37:24 +00:00
|
|
|
bool m_dotted_cp;
|
2016-08-11 12:41:01 +00:00
|
|
|
};
|
|
|
|
|
2016-08-11 12:41:27 +00:00
|
|
|
wxDECLARE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );
|
|
|
|
|
2016-08-11 12:41:01 +00:00
|
|
|
#endif
|