kicad/eeschema/sim/sim_plot_panel.cpp

138 lines
4.2 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>
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
{
//SetMargins( 10, 10, 10, 10 );
LockAspect();
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_legend = new mpInfoLegend( wxRect( 0, 0, 40, 40 ), wxWHITE_BRUSH );
AddLayer( m_legend );
2016-08-11 12:41:14 +00:00
//m_coords = new mpInfoCoords( wxRect( 80, 20, 10, 10 ), wxWHITE_BRUSH );
//AddLayer( m_coords );
}
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
}
void SIM_PLOT_PANEL::AddTrace( const wxString& aSpiceName, const wxString& aTitle, 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
auto it = std::find_if( m_traces.begin(), m_traces.end(),
[&](const TRACE* t) { return t->GetName() == aTitle; });
2016-08-11 12:41:01 +00:00
if( it == m_traces.end() )
{
// New entry
t = new TRACE( aTitle, aSpiceName );
t->SetPen( wxPen( generateColor(), 1, wxSOLID ) );
m_traces.push_back( t );
// It is a trick to keep legend always on the top
DelLayer( m_legend );
AddLayer( t );
AddLayer( m_legend );
}
else
{
t = *it;
}
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:01 +00:00
}
2016-08-11 12:41:07 +00:00
2016-08-11 12:41:01 +00:00
void SIM_PLOT_PANEL::DeleteTraces()
{
for( TRACE* t : m_traces )
2016-08-11 12:41:12 +00:00
{
DelLayer( t, true );
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
void SIM_PLOT_PANEL::ShowGrid( bool aEnable )
{
m_axis_x->SetTicks( !aEnable );
m_axis_y->SetTicks( !aEnable );
UpdateAll();
}
bool SIM_PLOT_PANEL::IsGridShown() const
{
assert( m_axis_x->GetTicks() == m_axis_y->GetTicks() );
return !m_axis_x->GetTicks();
}
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
}