Save trace colors during session.

My first thought was to move the color out of the TRACE, which is
really a view object.  However we can't make signals a first-class
citizen either, because they change depending on what the current
simulation tab is and so (for instance) we can only load in the
workbook signals for the current tab.  Hours later I backed it all
out and arrived at this simpler, less "correct" solution.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/14231
This commit is contained in:
Jeff Young 2023-10-04 16:50:49 +01:00
parent ed4d66e76b
commit e9bdfe7210
4 changed files with 29 additions and 21 deletions

View File

@ -129,17 +129,15 @@ void SIM_PLOT_COLORS::FillDefaultColorList( bool aDarkMode )
} }
wxColour SIM_PLOT_COLORS::GenerateColor( std::map<wxString, TRACE*> aTraces ) wxColour SIM_PLOT_COLORS::GenerateColor( std::map<wxString, wxColour> aTraceColors )
{ {
for( COLOR_SET i = COLOR_SET::TRACE; i < getPlotColorCount(); ++i ) for( COLOR_SET i = COLOR_SET::TRACE; i < getPlotColorCount(); ++i )
{ {
bool hasColor = false; bool hasColor = false;
for( auto& t : aTraces ) for( const auto& [ vectorName, traceColor ] : aTraceColors )
{ {
TRACE* trace = t.second; if( traceColor == GetPlotColor( i ) )
if( trace->GetTraceColour() == GetPlotColor( i ) )
{ {
hasColor = true; hasColor = true;
break; break;
@ -151,6 +149,6 @@ wxColour SIM_PLOT_COLORS::GenerateColor( std::map<wxString, TRACE*> aTraces )
} }
// If all colors are in use, choose a suitable color in list // If all colors are in use, choose a suitable color in list
COLOR_SET idx = aTraces.size() % ( getPlotColorCount() - COLOR_SET::TRACE ); COLOR_SET idx = aTraceColors.size() % ( getPlotColorCount() - COLOR_SET::TRACE );
return GetPlotColor( COLOR_SET::TRACE + idx ); return GetPlotColor( COLOR_SET::TRACE + idx );
} }

View File

@ -64,7 +64,7 @@ public:
* @return a new color from the palette * @return a new color from the palette
* @param a collection of traces in the plot panel * @param a collection of traces in the plot panel
*/ */
wxColour GenerateColor( std::map<wxString, TRACE*> aTraces ); wxColour GenerateColor( std::map<wxString, wxColour> aTraceColors );
/** /**
* @brief Fills m_colorList by a default set of colors. * @brief Fills m_colorList by a default set of colors.
@ -74,13 +74,15 @@ public:
static void FillDefaultColorList( bool aWhiteBg ); static void FillDefaultColorList( bool aWhiteBg );
private: private:
///< The color list to draw traces, bg, fg, axis...
static std::vector<wxColour> m_colorList;
/** /**
* @return the count of colors in color list * @return the count of colors in color list
*/ */
enum COLOR_SET getPlotColorCount() { return static_cast<enum COLOR_SET>( m_colorList.size() ); } enum COLOR_SET getPlotColorCount() { return static_cast<enum COLOR_SET>( m_colorList.size() ); }
private:
///< The color list to draw traces, bg, fg, axis...
static std::vector<wxColour> m_colorList;
}; };
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 );

View File

@ -813,6 +813,8 @@ void SIM_PLOT_TAB::UpdateTraceStyle( TRACE* trace )
? wxPENSTYLE_DOT ? wxPENSTYLE_DOT
: wxPENSTYLE_SOLID; : wxPENSTYLE_SOLID;
trace->SetPen( wxPen( trace->GetTraceColour(), 2, penStyle ) ); trace->SetPen( wxPen( trace->GetTraceColour(), 2, penStyle ) );
m_sessionTraceColors[ trace->GetName() ] = trace->GetTraceColour();
} }
@ -848,7 +850,12 @@ TRACE* SIM_PLOT_TAB::GetOrAddTrace( const wxString& aVectorName, int aType )
} }
trace = new TRACE( aVectorName, (SIM_TRACE_TYPE) aType ); trace = new TRACE( aVectorName, (SIM_TRACE_TYPE) aType );
trace->SetTraceColour( m_colors.GenerateColor( m_traces ) );
if( m_sessionTraceColors.count( aVectorName ) )
trace->SetTraceColour( m_sessionTraceColors[ aVectorName ] );
else
trace->SetTraceColour( m_colors.GenerateColor( m_sessionTraceColors ) );
UpdateTraceStyle( trace ); UpdateTraceStyle( trace );
m_traces[ getTraceId( aVectorName, aType ) ] = trace; m_traces[ getTraceId( aVectorName, aType ) ] = trace;

View File

@ -369,22 +369,23 @@ private:
void updateAxes( int aNewTraceType = SIM_TRACE_TYPE::SPT_UNKNOWN ); void updateAxes( int aNewTraceType = SIM_TRACE_TYPE::SPT_UNKNOWN );
private: private:
SIM_PLOT_COLORS m_colors; SIM_PLOT_COLORS m_colors;
std::map<wxString, wxColour> m_sessionTraceColors;
// Top-level plot window // Top-level plot window
mpWindow* m_plotWin; mpWindow* m_plotWin;
wxBoxSizer* m_sizer; wxBoxSizer* m_sizer;
// Traces to be plotted // Traces to be plotted
std::map<wxString, TRACE*> m_traces; std::map<wxString, TRACE*> m_traces;
mpScaleXBase* m_axis_x; mpScaleXBase* m_axis_x;
mpScaleY* m_axis_y1; mpScaleY* m_axis_y1;
mpScaleY* m_axis_y2; mpScaleY* m_axis_y2;
mpScaleY* m_axis_y3; mpScaleY* m_axis_y3;
mpInfoLegend* m_legend; mpInfoLegend* m_legend;
bool m_dotted_cp; bool m_dotted_cp;
// Measurements (and their format strings) // Measurements (and their format strings)
std::vector<std::pair<wxString, wxString>> m_measurements; std::vector<std::pair<wxString, wxString>> m_measurements;