Eeschema,sim: extract plot colors to new class

This commit is contained in:
Sylwester Kocjan 2021-01-21 22:17:13 +01:00 committed by Wayne Stambaugh
parent cc6df515a1
commit 959ae73a01
7 changed files with 286 additions and 148 deletions

View File

@ -286,6 +286,7 @@ if( KICAD_SPICE )
${EESCHEMA_SRCS}
sim/netlist_exporter_pspice_sim.cpp
sim/ngspice.cpp
sim/sim_plot_colors.cpp
sim/sim_plot_frame.cpp
sim/sim_plot_frame_base.cpp
sim/sim_plot_panel.cpp

View File

@ -0,0 +1,157 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2021 Sylwester Kocjan <s.kocjan@o2.pl>
* Copyright (C) 2016-2021 KiCad Developers, see AUTHORS.txt for contributors.
*
* 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 3
* 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:
* https://www.gnu.org/licenses/gpl-3.0.html
* or you may search the http://www.gnu.org website for the version 3 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "sim_plot_colors.h"
#include "sim_plot_panel.h"
#include <wx/stc/stc.h>
std::vector<wxColour> SIM_PLOT_COLORS::m_colorList;
inline bool operator<( SIM_PLOT_COLORS::COLOR_SET& x, SIM_PLOT_COLORS::COLOR_SET& y )
{
return static_cast<int>( x ) < static_cast<int>( y );
}
inline bool operator>=( SIM_PLOT_COLORS::COLOR_SET& x, SIM_PLOT_COLORS::COLOR_SET& y )
{
return static_cast<int>( x ) >= static_cast<int>( y );
}
inline bool operator<( SIM_PLOT_COLORS::COLOR_SET& x, int y )
{
return static_cast<int>( x ) < y;
}
inline bool operator>=( SIM_PLOT_COLORS::COLOR_SET& x, int y )
{
return static_cast<int>( x ) >= y;
}
inline SIM_PLOT_COLORS::COLOR_SET operator+( SIM_PLOT_COLORS::COLOR_SET x,
SIM_PLOT_COLORS::COLOR_SET y )
{
return static_cast<SIM_PLOT_COLORS::COLOR_SET>( static_cast<int>( x ) + static_cast<int>( y ) );
}
inline SIM_PLOT_COLORS::COLOR_SET operator-( SIM_PLOT_COLORS::COLOR_SET x,
SIM_PLOT_COLORS::COLOR_SET y )
{
return static_cast<SIM_PLOT_COLORS::COLOR_SET>( static_cast<int>( x ) - static_cast<int>( y ) );
}
inline SIM_PLOT_COLORS::COLOR_SET operator%( int x, SIM_PLOT_COLORS::COLOR_SET y )
{
return static_cast<SIM_PLOT_COLORS::COLOR_SET>( x % static_cast<int>( y ) );
}
inline SIM_PLOT_COLORS::COLOR_SET& operator++( SIM_PLOT_COLORS::COLOR_SET& x )
{
x = static_cast<SIM_PLOT_COLORS::COLOR_SET>( (int) x + 1 );
return x;
}
wxColour SIM_PLOT_COLORS::GetPlotColor( COLOR_SET aColorId )
{
// return the wxColor selected in color list or BLACK is not in list
if( aColorId >= 0 && aColorId < m_colorList.size() )
return m_colorList[static_cast<int>( aColorId )];
return wxColour( 0, 0, 0 );
}
void SIM_PLOT_COLORS::FillDefaultColorList( bool aWhiteBg )
{
m_colorList.clear();
if( aWhiteBg )
{
m_colorList.emplace_back( 255, 255, 255 ); // Bg color
m_colorList.emplace_back( 0, 0, 0 ); // Fg color (texts)
m_colorList.emplace_back( 130, 130, 130 ); // Axis color
m_colorList.emplace_back( 0, 0, 0 ); // cursors color
}
else
{
m_colorList.emplace_back( 0, 0, 0 ); // Bg color
m_colorList.emplace_back( 255, 255, 255 ); // Fg color (texts)
m_colorList.emplace_back( 130, 130, 130 ); // Axis color
m_colorList.emplace_back( 255, 255, 255 ); // cursors color
}
// Add a list of color for traces, starting at index SIM_TRACE_COLOR
m_colorList.emplace_back( 0xE4, 0x1A, 0x1C );
m_colorList.emplace_back( 0x37, 0x7E, 0xB8 );
m_colorList.emplace_back( 0x4D, 0xAF, 0x4A );
m_colorList.emplace_back( 0x98, 0x4E, 0xA3 );
m_colorList.emplace_back( 0xFF, 0x7F, 0x00 );
m_colorList.emplace_back( 0xFF, 0xFF, 0x33 );
m_colorList.emplace_back( 0xA6, 0x56, 0x28 );
m_colorList.emplace_back( 0xF7, 0x81, 0xBF );
m_colorList.emplace_back( 0x66, 0xC2, 0xA5 );
m_colorList.emplace_back( 0xFC, 0x8D, 0x62 );
m_colorList.emplace_back( 0x8D, 0xA0, 0xCB );
m_colorList.emplace_back( 0xE7, 0x8A, 0xC3 );
m_colorList.emplace_back( 0xA6, 0xD8, 0x54 );
m_colorList.emplace_back( 0xFF, 0xD9, 0x2F );
m_colorList.emplace_back( 0xE5, 0xC4, 0x94 );
m_colorList.emplace_back( 0xB3, 0xB3, 0xB3 );
}
wxColour SIM_PLOT_COLORS::GenerateColor( std::map<wxString, TRACE*> aTraces )
{
for( COLOR_SET i = COLOR_SET::TRACE; i < getPlotColorCount(); ++i )
{
bool hasColor = false;
for( auto& t : aTraces )
{
TRACE* trace = t.second;
if( trace->GetTraceColour() == GetPlotColor( i ) )
{
hasColor = true;
break;
}
}
if( !hasColor )
return GetPlotColor( i );
}
// If all colors are in use, choose a suitable color in list
COLOR_SET idx = aTraces.size() % ( getPlotColorCount() - COLOR_SET::TRACE );
return GetPlotColor( COLOR_SET::TRACE + idx );
}

View File

@ -0,0 +1,99 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2021 Sylwester Kocjan <s.kocjan@o2.pl>
* Copyright (C) 2016-2021 KiCad Developers, see AUTHORS.txt for contributors.
*
* 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 3
* 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:
* https://www.gnu.org/licenses/gpl-3.0.html
* or you may search the http://www.gnu.org website for the version 3 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef __SIM_PLOT_COLORS__
#define __SIM_PLOT_COLORS__
#include <map>
#include <vector>
#include <wx/colour.h>
#include <wx/string.h>
class TRACE;
/**
* @file sim_plot_colors.h
*
* Class is responsible for providing colors for traces on simulation plot
*/
class SIM_PLOT_COLORS
{
public:
SIM_PLOT_COLORS(){};
~SIM_PLOT_COLORS(){};
// Identifiers (indexes) for color choice in color table
enum class COLOR_SET
{
BACKGROUND,
FOREGROUND,
AXIS,
CURSOR,
TRACE // First index for trace colors list
};
/**
* @return the wxColor selected in color list.
* @param aColorId is the index in color list
*/
wxColour GetPlotColor( enum COLOR_SET aColorId );
/**
* @return a new color from the palette
* @param a collection of traces in the plot panel
*/
wxColour GenerateColor( std::map<wxString, TRACE*> aTraces );
/**
* @brief Fills m_colorList by a default set of colors.
* @param aWhiteBg = true to use a white (or clear) background
* false to use a dark background
*/
static void FillDefaultColorList( bool aWhiteBg );
private:
///> The color list to draw traces, bg, fg, axis...
static std::vector<wxColour> m_colorList;
/**
* @return the count of colors in color list
*/
enum COLOR_SET getPlotColorCount() { return static_cast<enum COLOR_SET>( m_colorList.size() ); }
};
inline bool operator<( SIM_PLOT_COLORS::COLOR_SET& x, SIM_PLOT_COLORS::COLOR_SET& y );
inline bool operator>=( SIM_PLOT_COLORS::COLOR_SET& x, SIM_PLOT_COLORS::COLOR_SET& y );
inline bool operator<( SIM_PLOT_COLORS::COLOR_SET& x, int y );
inline bool operator>=( SIM_PLOT_COLORS::COLOR_SET& x, int y );
inline SIM_PLOT_COLORS::COLOR_SET operator+( SIM_PLOT_COLORS::COLOR_SET x,
SIM_PLOT_COLORS::COLOR_SET y );
inline SIM_PLOT_COLORS::COLOR_SET operator-( SIM_PLOT_COLORS::COLOR_SET x,
SIM_PLOT_COLORS::COLOR_SET y );
inline SIM_PLOT_COLORS::COLOR_SET operator%( int x, SIM_PLOT_COLORS::COLOR_SET y );
inline SIM_PLOT_COLORS::COLOR_SET& operator++( SIM_PLOT_COLORS::COLOR_SET& x );
#endif // __SIM_PLOT_COLORS__

View File

@ -36,6 +36,7 @@
#include <dialogs/dialog_signal_list.h>
#include "netlist_exporter_pspice_sim.h"
#include <pgm_base.h>
#include "sim_plot_colors.h"
#include "sim_plot_frame.h"
#include "sim_plot_panel.h"
#include "spice_simulator.h"
@ -142,7 +143,7 @@ SIM_PLOT_FRAME::SIM_PLOT_FRAME( KIWAY* aKiway, wxWindow* aParent )
LoadSettings( config() );
// Prepare the color list to plot traces
fillDefaultColorList( GetPlotBgOpt() );
SIM_PLOT_COLORS::FillDefaultColorList( GetPlotBgOpt() );
// Give icons to menuitems
setIconsForMenuItems();
@ -373,56 +374,6 @@ void SIM_PLOT_FRAME::setSubWindowsSashSize()
}
wxColor SIM_PLOT_FRAME::GetPlotColor( int aColorId )
{
// return the wxColor selected in color list or BLACK is not in list
if( aColorId >= 0 && aColorId < (int)m_colorList.size() )
return m_colorList[aColorId];
return wxColor( 0, 0, 0 );
}
void SIM_PLOT_FRAME::fillDefaultColorList( bool aWhiteBg )
{
m_colorList.clear();
if( aWhiteBg )
{
m_colorList.emplace_back( 255, 255, 255 ); // Bg color
m_colorList.emplace_back( 0, 0, 0 ); // Fg color (texts)
m_colorList.emplace_back( 130, 130, 130 ); // Axis color
m_colorList.emplace_back( 0, 0, 0 ); // cursors color
}
else
{
m_colorList.emplace_back( 0, 0, 0 ); // Bg color
m_colorList.emplace_back( 255, 255, 255 ); // Fg color (texts)
m_colorList.emplace_back( 130, 130, 130 ); // Axis color
m_colorList.emplace_back( 255, 255, 255 ); // cursors color
}
// Add a list of color for traces, starting at index SIM_TRACE_COLOR
m_colorList.emplace_back( 0xE4, 0x1A, 0x1C );
m_colorList.emplace_back( 0x37, 0x7E, 0xB8 );
m_colorList.emplace_back( 0x4D, 0xAF, 0x4A );
m_colorList.emplace_back( 0x98, 0x4E, 0xA3 );
m_colorList.emplace_back( 0xFF, 0x7F, 0x00 );
m_colorList.emplace_back( 0xFF, 0xFF, 0x33 );
m_colorList.emplace_back( 0xA6, 0x56, 0x28 );
m_colorList.emplace_back( 0xF7, 0x81, 0xBF );
m_colorList.emplace_back( 0x66, 0xC2, 0xA5 );
m_colorList.emplace_back( 0xFC, 0x8D, 0x62 );
m_colorList.emplace_back( 0x8D, 0xA0, 0xCB );
m_colorList.emplace_back( 0xE7, 0x8A, 0xC3 );
m_colorList.emplace_back( 0xA6, 0xD8, 0x54 );
m_colorList.emplace_back( 0xFF, 0xD9, 0x2F );
m_colorList.emplace_back( 0xE5, 0xC4, 0x94 );
m_colorList.emplace_back( 0xB3, 0xB3, 0xB3 );
}
void SIM_PLOT_FRAME::StartSimulation( const wxString& aSimCommand )
{
STRING_FORMATTER formatter;
@ -824,7 +775,7 @@ void SIM_PLOT_FRAME::updateSignalList()
{
wxBitmap bitmap( isize, isize );
bmDC.SelectObject( bitmap );
wxColour tcolor = trace.second->GetTraceColour();
wxColour tcolor = trace.second->GetPen().GetColour();
wxColour bgColor = m_signals->wxWindow::GetBackgroundColour();
bmDC.SetPen( wxPen( bgColor ) );
@ -1213,7 +1164,7 @@ void SIM_PLOT_FRAME::menuWhiteBackground( wxCommandEvent& event )
m_plotUseWhiteBg = not m_plotUseWhiteBg;
// Rebuild the color list to plot traces
fillDefaultColorList( GetPlotBgOpt() );
SIM_PLOT_COLORS::FillDefaultColorList( GetPlotBgOpt() );
// Now send changes to all SIM_PLOT_PANEL
for( size_t page = 0; page < m_plotNotebook->GetPageCount(); page++ )

View File

@ -59,18 +59,7 @@ class SIM_THREAD_REPORTER;
class TUNER_SLIDER;
// Identifiers (indexes) for color choice in color table
enum SIM_COLOR_SET
{
SIM_BG_COLOR,
SIM_FG_COLOR,
SIM_AXIS_COLOR,
SIM_CURSOR_COLOR,
SIM_TRACE_COLOR // First index for trace colors list
};
///< Trace descriptor class
///> Trace descriptor class
class TRACE_DESC
{
public:
@ -187,17 +176,6 @@ public:
*/
bool GetPlotBgOpt() const { return m_plotUseWhiteBg; }
/**
* @return the wxColor selected in color list.
* @param aColorId is the index in color list
*/
wxColor GetPlotColor( int aColorId );
/**
* @return the count of colors in color list
*/
int GetPlotColorCount() { return m_colorList.size(); }
void LoadSettings( APP_SETTINGS_BASE* aCfg ) override;
void SaveSettings( APP_SETTINGS_BASE* aCfg ) override;
@ -213,13 +191,6 @@ private:
*/
void setIconsForMenuItems();
/**
* Fill m_colorList by a default set of colors.
*
* @param aWhiteBg true to use a white (or clear) background false to use a dark background.
*/
void fillDefaultColorList( bool aWhiteBg );
/**
* Return the currently opened plot panel (or NULL if there is none).
*/
@ -427,9 +398,6 @@ private:
int m_splitterTuneValuesSashPosition;
bool m_plotUseWhiteBg;
unsigned int m_plotNumber;
///< The color list to draw traces, bg, fg, axis...
std::vector<wxColour> m_colorList;
};
// Commands

View File

@ -23,6 +23,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "sim_plot_colors.h"
#include "sim_plot_panel.h"
#include "sim_plot_frame.h"
@ -260,8 +261,9 @@ void CURSOR::Plot( wxDC& aDC, mpWindow& aWindow )
wxCoord topPx = m_drawOutsideMargins ? 0 : aWindow.GetMarginTop();
wxCoord bottomPx = m_drawOutsideMargins ? aWindow.GetScrY() : aWindow.GetScrY() - aWindow.GetMarginBottom();
aDC.SetPen( wxPen( m_plotPanel->GetPlotColor( SIM_CURSOR_COLOR ), 1,
m_continuous ? wxPENSTYLE_SOLID : wxPENSTYLE_LONG_DASH ) );
wxPen pen = GetPen();
pen.SetStyle( m_continuous ? wxPENSTYLE_SOLID : wxPENSTYLE_LONG_DASH );
aDC.SetPen( pen );
if( topPx < cursorPos.y && cursorPos.y < bottomPx )
aDC.DrawLine( leftPx, cursorPos.y, rightPx, cursorPos.y );
@ -294,7 +296,6 @@ void CURSOR::UpdateReference()
SIM_PLOT_PANEL::SIM_PLOT_PANEL( wxString aCommand, wxWindow* parent, SIM_PLOT_FRAME* aMainFrame,
wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name )
: SIM_PANEL_BASE( aCommand, parent, id, pos, size, style, name ),
m_colorIdx( 0 ),
m_axis_x( nullptr ),
m_axis_y1( nullptr ),
m_axis_y2( nullptr ),
@ -307,8 +308,7 @@ SIM_PLOT_PANEL::SIM_PLOT_PANEL( wxString aCommand, wxWindow* parent, SIM_PLOT_FR
m_plotWin->LimitView( true );
m_plotWin->SetMargins( 50, 80, 50, 80 );
m_plotWin->SetColourTheme( GetPlotColor( SIM_BG_COLOR ), GetPlotColor( SIM_FG_COLOR ),
GetPlotColor( SIM_AXIS_COLOR ) );
UpdatePlotColors();
switch( GetType() )
{
@ -417,19 +417,20 @@ void SIM_PLOT_PANEL::prepareDCAxes()
void SIM_PLOT_PANEL::UpdatePlotColors()
{
// Update bg and fg colors:
m_plotWin->SetColourTheme( GetPlotColor( SIM_BG_COLOR ), GetPlotColor( SIM_FG_COLOR ),
GetPlotColor( SIM_AXIS_COLOR ) );
m_plotWin->SetColourTheme( m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::BACKGROUND ),
m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::FOREGROUND ),
m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::AXIS ) );
// Update color of all traces
for( auto& t : m_traces )
if( t.second->GetCursor() )
t.second->GetCursor()->SetPen(
wxPen( m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::CURSOR ) ) );
m_plotWin->UpdateAll();
}
wxColour SIM_PLOT_PANEL::GetPlotColor( int aIndex )
{
return m_masterFrame->GetPlotColor( aIndex );
}
void SIM_PLOT_PANEL::UpdateTraceStyle( TRACE* trace )
{
int flags = trace->GetFlags();
@ -472,7 +473,7 @@ bool SIM_PLOT_PANEL::AddTrace( const wxString& aName, int aPoints,
// New entry
trace = new TRACE( aName );
trace->SetTraceColour( generateColor() );
trace->SetTraceColour( m_colors.GenerateColor( m_traces ) );
UpdateTraceStyle( trace );
m_traces[aName] = trace;
@ -550,7 +551,6 @@ void SIM_PLOT_PANEL::DeleteAllTraces()
DeleteTrace( t.first );
}
m_colorIdx = 0;
m_traces.clear();
}
@ -578,6 +578,7 @@ void SIM_PLOT_PANEL::EnableCursor( const wxString& aName, bool aEnable )
- GetPlotWin()->GetMarginRight() )
/ 2;
c->SetX( plotCenter );
c->SetPen( wxPen( m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::CURSOR ) ) );
t->SetCursor( c );
m_plotWin->AddLayer( c );
}
@ -609,33 +610,4 @@ void SIM_PLOT_PANEL::ResetScales()
}
wxColour SIM_PLOT_PANEL::generateColor()
{
const unsigned int colorCount = m_masterFrame->GetPlotColorCount() - SIM_TRACE_COLOR;
for( int i = 0; i < (int)colorCount - 1; i++ )
{
const wxColour color = GetPlotColor( i+SIM_TRACE_COLOR );
bool hasColor = false;
for( auto& t : m_traces )
{
TRACE* trace = t.second;
if( trace->GetTraceColour() == color )
{
hasColor = true;
break;
}
}
if( !hasColor )
return color;
}
// If all colors are in use, choose a suitable color in list
int idx = m_traces.size() % colorCount;
return wxColour( GetPlotColor( idx + SIM_TRACE_COLOR ) );
}
wxDEFINE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );

View File

@ -31,8 +31,10 @@
#include "sim_types.h"
#include <map>
#include <widgets/mathplot.h>
#include <wx/colour.h>
#include <wx/sizer.h>
#include "sim_panel_base.h"
#include "sim_plot_colors.h"
class SIM_PLOT_FRAME;
class SIM_PLOT_PANEL;
@ -42,10 +44,10 @@ class TRACE;
class CURSOR : public mpInfoLayer
{
public:
CURSOR( const TRACE* aTrace, SIM_PLOT_PANEL* aPlotPanel )
: mpInfoLayer( wxRect( 0, 0, DRAG_MARGIN, DRAG_MARGIN ), wxTRANSPARENT_BRUSH ),
m_trace( aTrace ), m_updateRequired( true ), m_updateRef( false ),
m_coords( 0.0, 0.0 ), m_window( nullptr ), m_plotPanel( aPlotPanel )
CURSOR( const TRACE* aTrace, SIM_PLOT_PANEL* aPlotPanel ) :
mpInfoLayer( wxRect( 0, 0, DRAG_MARGIN, DRAG_MARGIN ), wxTRANSPARENT_BRUSH ),
m_trace( aTrace ), m_updateRequired( true ), m_updateRef( false ), m_coords( 0.0, 0.0 ),
m_window( nullptr )
{
SetDrawOutsideMargins( false );
}
@ -84,7 +86,6 @@ private:
bool m_updateRequired, m_updateRef;
wxRealPoint m_coords;
mpWindow* m_window;
SIM_PLOT_PANEL* m_plotPanel;
static constexpr int DRAG_MARGIN = 10;
};
@ -278,14 +279,7 @@ public:
///< Update trace line style
void UpdateTraceStyle( TRACE* trace );
/**
* A proxy to SIM_PLOT_FRAME::GetPlotColor()
* @return the color stored in m_colorList.
* @param aIndex is the index in list
*/
wxColour GetPlotColor( int aIndex );
///< Update plot colors
///> Update plot colors
void UpdatePlotColors();
///< Getter for math plot window
@ -295,14 +289,10 @@ public:
}
private:
///< @return a new color from the palette
wxColour generateColor();
///< @brief Construct the plot axes for DC simulation plot.
void prepareDCAxes();
// Color index to get a new color from the palette
unsigned int m_colorIdx;
SIM_PLOT_COLORS m_colors;
// Top-level plot window
mpWindow* m_plotWin;