kicad/eeschema/sim/sim_plot_panel.cpp

251 lines
6.9 KiB
C++
Raw Normal View History

2016-08-11 12:41:07 +00:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 CERN
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
* @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
* as published by the Free Software Foundation; either version 2
* 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:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* 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
#include "sim_plot_panel.h"
#include <algorithm>
2016-08-11 12:41:14 +00:00
#include <limits>
2016-08-11 12:41:22 +00:00
void CURSOR::Plot( wxDC& aDC, mpWindow& aWindow )
{
if( !m_window )
m_window = &aWindow;
2016-08-11 12:41:22 +00:00
if( !m_visible )
return;
const auto& dataX = m_trace->GetDataX();
const auto& dataY = m_trace->GetDataY();
if( dataX.size() <= 1 )
return;
if( m_updateRequired )
2016-08-11 12:41:22 +00:00
{
m_coords.x = aWindow.p2x( m_dim.x );
// Find the closest point coordinates
auto maxXIt = std::upper_bound( dataX.begin(), dataX.end(), m_coords.x );
int maxIdx = maxXIt - dataX.begin();
int minIdx = maxIdx - 1;
// Out of bounds checks
if( minIdx < 0 )
{
minIdx = 0;
maxIdx = 1;
m_coords.x = dataX[0];
}
else if( maxIdx >= (int) dataX.size() )
{
maxIdx = dataX.size() - 1;
minIdx = maxIdx - 1;
m_coords.x = dataX[maxIdx];
}
const double leftX = dataX[minIdx];
const double rightX = dataX[maxIdx];
const double leftY = dataY[minIdx];
const double rightY = dataY[maxIdx];
m_coords.y = leftY + ( rightY - leftY ) / ( rightX - leftX ) * ( m_coords.x - leftX );
m_updateRequired = false;
// Notify the parent window about the changes
wxQueueEvent( aWindow.GetParent(), new wxCommandEvent( EVT_SIM_CURSOR_UPDATE ) );
2016-08-11 12:41:22 +00:00
}
else
{
UpdateReference();
}
// Line length in horizontal and vertical dimensions
const int horLen = aWindow.GetScrX();
const int verLen = aWindow.GetScrY();
const wxPoint cursorPos( aWindow.x2p( m_coords.x ), aWindow.y2p( m_coords.y ) );
2016-08-11 12:41:22 +00:00
aDC.SetPen( wxPen( *wxBLACK, 1, m_continuous ? wxSOLID : wxLONG_DASH ) );
aDC.DrawLine( -horLen, cursorPos.y, horLen, cursorPos.y );
aDC.DrawLine( cursorPos.x, -verLen, cursorPos.x, verLen );
}
SIM_PLOT_PANEL::SIM_PLOT_PANEL( wxWindow* parent, wxWindowID id, const wxPoint& pos,
const wxSize& size, long style, const wxString& name )
: mpWindow( parent, id, pos, size, style ), m_colorIdx( 0 )
2016-08-11 12:41:01 +00:00
{
m_axis_x = new mpScaleX( wxT( "T [s]" ) );
m_axis_x->SetTicks( false );
AddLayer( m_axis_x );
2016-08-11 12:41:01 +00:00
m_axis_y = new mpScaleY( wxT( "U [V]" ) );
m_axis_y->SetTicks( false );
AddLayer( m_axis_y );
m_coords = new mpInfoCoords( wxRect( 0, 0, 100, 40 ), wxWHITE_BRUSH );
AddLayer( m_coords );
m_topLevel.push_back( m_coords );
2016-08-11 12:41:14 +00:00
m_legend = new mpInfoLegend( wxRect( 0, 40, 40, 40 ), wxWHITE_BRUSH );
AddLayer( m_legend );
m_topLevel.push_back( m_legend );
}
2016-08-11 12:41:14 +00:00
SIM_PLOT_PANEL::~SIM_PLOT_PANEL()
{
// ~mpWindow destroys all the added layers, so there is no need to destroy m_traces contents
2016-08-11 12:41:14 +00:00
}
2016-08-11 12:41:24 +00:00
bool SIM_PLOT_PANEL::AddTrace( const wxString& aSpiceName, const wxString& aName, int aPoints,
const double* aT, const double* aY, int aFlags )
2016-08-11 12:41:01 +00:00
{
TRACE* t = NULL;
// Find previous entry, if there is one
2016-08-11 12:41:24 +00:00
auto prev = m_traces.find( aName );
bool addedNewEntry = ( prev == m_traces.end() );
2016-08-11 12:41:01 +00:00
2016-08-11 12:41:24 +00:00
if( addedNewEntry )
{
// New entry
2016-08-11 12:41:24 +00:00
t = new TRACE( aName, aSpiceName );
t->SetPen( wxPen( generateColor(), 1, wxSOLID ) );
2016-08-11 12:41:24 +00:00
m_traces[aName] = t;
// It is a trick to keep legend & coords always on the top
for( mpLayer* l : m_topLevel )
DelLayer( l );
AddLayer( t );
for( mpLayer* l : m_topLevel )
AddLayer( l );
}
else
{
2016-08-11 12:41:24 +00:00
t = prev->second;
}
2016-08-11 12:41:14 +00:00
t->SetData( std::vector<double>( aT, aT + aPoints ), std::vector<double>( aY, aY + aPoints ) );
UpdateAll();
2016-08-11 12:41:24 +00:00
return addedNewEntry;
}
bool SIM_PLOT_PANEL::DeleteTrace( const wxString& aName )
{
auto it = m_traces.find( aName );
2016-08-11 12:41:24 +00:00
if( it != m_traces.end() )
2016-08-11 12:41:24 +00:00
{
m_traces.erase( it );
TRACE* trace = it->second;
if( CURSOR* cursor = trace->GetCursor() )
DelLayer( cursor, true );
DelLayer( trace, true, true );
2016-08-11 12:41:24 +00:00
return true;
}
return false;
2016-08-11 12:41:01 +00:00
}
2016-08-11 12:41:07 +00:00
2016-08-11 12:41:24 +00:00
void SIM_PLOT_PANEL::DeleteAllTraces()
2016-08-11 12:41:01 +00:00
{
2016-08-11 12:41:24 +00:00
for( auto& t : m_traces )
2016-08-11 12:41:12 +00:00
{
DeleteTrace( t.first );
2016-08-11 12:41:12 +00:00
}
m_traces.clear();
}
2016-08-11 12:41:12 +00:00
2016-08-11 12:41:14 +00:00
bool SIM_PLOT_PANEL::HasCursorEnabled( const wxString& aName ) const
{
TRACE* t = GetTrace( aName );
return t ? t->HasCursor() : false;
}
void SIM_PLOT_PANEL::EnableCursor( const wxString& aName, bool aEnable )
{
TRACE* t = GetTrace( aName );
if( t == nullptr || t->HasCursor() == aEnable )
return;
if( aEnable )
{
CURSOR* c = new CURSOR( t );
t->SetCursor( c );
AddLayer( c );
}
else
{
CURSOR* c = t->GetCursor();
t->SetCursor( NULL );
DelLayer( c, true );
}
// Notify the parent window about the changes
wxQueueEvent( GetParent(), new wxCommandEvent( EVT_SIM_CURSOR_UPDATE ) );
}
wxColour SIM_PLOT_PANEL::generateColor()
{
/// @todo have a look at:
/// http://stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html
/// https://github.com/Gnuplotting/gnuplot-palettes
2016-08-11 12:41:12 +00:00
const unsigned long colors[] = { 0x000080, 0x008000, 0x800000, 0x008080, 0x800080, 0x808000, 0x808080 };
2016-08-11 12:41:14 +00:00
//const unsigned long colors[] = { 0xe3cea6, 0xb4781f, 0x8adfb2, 0x2ca033, 0x999afb, 0x1c1ae3, 0x6fbffd, 0x007fff, 0xd6b2ca, 0x9a3d6a };
2016-08-11 12:41:14 +00:00
// hls
//const unsigned long colors[] = { 0x0f1689, 0x0f7289, 0x35890f, 0x0f8945, 0x89260f, 0x890f53, 0x89820f, 0x630f89 };
2016-08-11 12:41:14 +00:00
// pastels, good for dark background
//const unsigned long colors[] = { 0x2fd8fe, 0x628dfa, 0x53d8a6, 0xa5c266, 0xb3b3b3, 0x94c3e4, 0xca9f8d, 0xac680e };
2016-08-11 12:41:14 +00:00
const unsigned int colorCount = sizeof(colors) / sizeof(unsigned long);
2016-08-11 12:41:14 +00:00
/// @todo generate shades to avoid repeating colors
return wxColour( colors[m_colorIdx++ % colorCount] );
2016-08-11 12:41:14 +00:00
}
wxDEFINE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );