You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

858 lines
23 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016-2023 CERN
  5. * Copyright (C) 2021-2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  8. * @author Maciej Suminski <maciej.suminski@cern.ch>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 3
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, you may find one here:
  22. * https://www.gnu.org/licenses/gpl-3.0.html
  23. * or you may search the http://www.gnu.org website for the version 3 license,
  24. * or you may write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  26. */
  27. #include "sim_plot_colors.h"
  28. #include "sim_plot_panel.h"
  29. #include "sim_plot_frame.h"
  30. #include <algorithm>
  31. #include <limits>
  32. static wxString formatFloat( double x, int nDigits )
  33. {
  34. wxString rv, fmt;
  35. if( nDigits )
  36. fmt.Printf( "%%.0%df", nDigits );
  37. else
  38. fmt = wxT( "%.0f" );
  39. rv.Printf( fmt, x );
  40. return rv;
  41. }
  42. static void getSISuffix( double x, const wxString& unit, int& power, wxString& suffix )
  43. {
  44. const int n_powers = 11;
  45. const struct
  46. {
  47. int exponent;
  48. char suffix;
  49. } powers[] =
  50. {
  51. { -18, 'a' },
  52. { -15, 'f' },
  53. { -12, 'p' },
  54. { -9, 'n' },
  55. { -6, 'u' },
  56. { -3, 'm' },
  57. { 0, 0 },
  58. { 3, 'k' },
  59. { 6, 'M' },
  60. { 9, 'G' },
  61. { 12, 'T' },
  62. { 14, 'P' }
  63. };
  64. power = 0;
  65. suffix = unit;
  66. if( x == 0.0 )
  67. return;
  68. for( int i = 0; i < n_powers - 1; i++ )
  69. {
  70. double r_cur = pow( 10, powers[i].exponent );
  71. if( fabs( x ) >= r_cur && fabs( x ) < r_cur * 1000.0 )
  72. {
  73. power = powers[i].exponent;
  74. if( powers[i].suffix )
  75. suffix = wxString( powers[i].suffix ) + unit;
  76. else
  77. suffix = unit;
  78. return;
  79. }
  80. }
  81. }
  82. static int countDecimalDigits( double x, int maxDigits )
  83. {
  84. if( std::isnan( x ) )
  85. {
  86. // avoid trying to count the decimals of NaN
  87. return 0;
  88. }
  89. int64_t k = (int)( ( x - floor( x ) ) * pow( 10.0, (double) maxDigits ) );
  90. int n = 0;
  91. while( k && ( ( k % 10LL ) == 0LL || ( k % 10LL ) == 9LL ) )
  92. {
  93. k /= 10LL;
  94. }
  95. n = 0;
  96. while( k != 0LL )
  97. {
  98. n++;
  99. k /= 10LL;
  100. }
  101. return n;
  102. }
  103. template <typename parent>
  104. class LIN_SCALE : public parent
  105. {
  106. public:
  107. LIN_SCALE( const wxString& name, const wxString& unit, int flags ) :
  108. parent( name, flags, false ),
  109. m_unit( unit )
  110. {};
  111. wxString GetUnits() const { return m_unit; }
  112. private:
  113. void formatLabels() override
  114. {
  115. double maxVis = parent::AbsVisibleMaxValue();
  116. wxString suffix;
  117. int power = 0;
  118. int digits = 0;
  119. int constexpr DIGITS = 3;
  120. getSISuffix( maxVis, m_unit, power, suffix );
  121. double sf = pow( 10.0, power );
  122. for( mpScaleBase::TickLabel& l : parent::TickLabels() )
  123. {
  124. int k = countDecimalDigits( l.pos / sf, DIGITS );
  125. digits = std::max( digits, k );
  126. }
  127. for( mpScaleBase::TickLabel& l : parent::TickLabels() )
  128. {
  129. l.label = formatFloat( l.pos / sf, digits ) + suffix;
  130. l.visible = true;
  131. }
  132. }
  133. private:
  134. const wxString m_unit;
  135. };
  136. template <typename parent>
  137. class LOG_SCALE : public parent
  138. {
  139. public:
  140. LOG_SCALE( const wxString& name, const wxString& unit, int flags ) :
  141. parent( name, flags, false ),
  142. m_unit( unit )
  143. {};
  144. wxString GetUnits() const { return m_unit; }
  145. private:
  146. void formatLabels() override
  147. {
  148. wxString suffix;
  149. int power;
  150. for( mpScaleBase::TickLabel& l : parent::TickLabels() )
  151. {
  152. getSISuffix( l.pos, m_unit, power, suffix );
  153. double sf = pow( 10.0, power );
  154. int k = countDecimalDigits( l.pos / sf, 3 );
  155. l.label = formatFloat( l.pos / sf, k ) + suffix;
  156. l.visible = true;
  157. }
  158. }
  159. private:
  160. const wxString m_unit;
  161. };
  162. void CURSOR::SetCoordX( double aValue )
  163. {
  164. wxRealPoint oldCoords = m_coords;
  165. doSetCoordX( aValue );
  166. m_updateRequired = false;
  167. m_updateRef = true;
  168. if( m_window )
  169. {
  170. wxRealPoint delta = m_coords - oldCoords;
  171. mpInfoLayer::Move( wxPoint( m_window->x2p( m_trace->x2s( delta.x ) ),
  172. m_window->y2p( m_trace->y2s( delta.y ) ) ) );
  173. m_window->Refresh();
  174. }
  175. }
  176. void CURSOR::doSetCoordX( double aValue )
  177. {
  178. m_coords.x = aValue;
  179. const std::vector<double>& dataX = m_trace->GetDataX();
  180. const std::vector<double>& dataY = m_trace->GetDataY();
  181. if( dataX.size() <= 1 )
  182. return;
  183. // Find the closest point coordinates
  184. auto maxXIt = std::upper_bound( dataX.begin(), dataX.end(), m_coords.x );
  185. int maxIdx = maxXIt - dataX.begin();
  186. int minIdx = maxIdx - 1;
  187. // Out of bounds checks
  188. if( minIdx < 0 )
  189. {
  190. minIdx = 0;
  191. maxIdx = 1;
  192. m_coords.x = dataX[0];
  193. }
  194. else if( maxIdx >= (int) dataX.size() )
  195. {
  196. maxIdx = dataX.size() - 1;
  197. minIdx = maxIdx - 1;
  198. m_coords.x = dataX[maxIdx];
  199. }
  200. const double leftX = dataX[minIdx];
  201. const double rightX = dataX[maxIdx];
  202. const double leftY = dataY[minIdx];
  203. const double rightY = dataY[maxIdx];
  204. // Linear interpolation
  205. m_coords.y = leftY + ( rightY - leftY ) / ( rightX - leftX ) * ( m_coords.x - leftX );
  206. }
  207. wxString CURSOR::getID()
  208. {
  209. for( const auto& [ id, cursor ] : m_trace->GetCursors() )
  210. {
  211. if( cursor == this )
  212. return wxString::Format( _( "%d" ), id );
  213. }
  214. return wxEmptyString;
  215. }
  216. void CURSOR::Plot( wxDC& aDC, mpWindow& aWindow )
  217. {
  218. if( !m_window )
  219. m_window = &aWindow;
  220. if( !m_visible || m_trace->GetDataX().size() <= 1 )
  221. return;
  222. if( m_updateRequired )
  223. {
  224. doSetCoordX( m_trace->s2x( aWindow.p2x( m_dim.x ) ) );
  225. m_updateRequired = false;
  226. // Notify the parent window about the changes
  227. wxQueueEvent( aWindow.GetParent(), new wxCommandEvent( EVT_SIM_CURSOR_UPDATE ) );
  228. }
  229. else
  230. {
  231. m_updateRef = true;
  232. }
  233. if( m_updateRef )
  234. {
  235. UpdateReference();
  236. m_updateRef = false;
  237. }
  238. // Line length in horizontal and vertical dimensions
  239. const wxPoint cursorPos( aWindow.x2p( m_trace->x2s( m_coords.x ) ),
  240. aWindow.y2p( m_trace->y2s( m_coords.y ) ) );
  241. wxCoord leftPx = m_drawOutsideMargins ? 0 : aWindow.GetMarginLeft();
  242. wxCoord rightPx = m_drawOutsideMargins ? aWindow.GetScrX() :
  243. aWindow.GetScrX() - aWindow.GetMarginRight();
  244. wxCoord topPx = m_drawOutsideMargins ? 0 : aWindow.GetMarginTop();
  245. wxCoord bottomPx = m_drawOutsideMargins ? aWindow.GetScrY() :
  246. aWindow.GetScrY() - aWindow.GetMarginBottom();
  247. wxPen pen = GetPen();
  248. wxColour fg = GetPen().GetColour();
  249. pen.SetColour( COLOR4D( m_trace->GetTraceColour() ).Mix( fg, 0.6 ).ToColour() );
  250. pen.SetStyle( m_continuous ? wxPENSTYLE_SOLID : wxPENSTYLE_LONG_DASH );
  251. aDC.SetPen( pen );
  252. if( topPx < cursorPos.y && cursorPos.y < bottomPx )
  253. aDC.DrawLine( leftPx, cursorPos.y, rightPx, cursorPos.y );
  254. if( leftPx < cursorPos.x && cursorPos.x < rightPx )
  255. {
  256. aDC.DrawLine( cursorPos.x, topPx, cursorPos.x, bottomPx );
  257. wxString id = getID();
  258. wxSize size = aDC.GetTextExtent( wxS( "M" ) );
  259. wxRect textRect( wxPoint( cursorPos.x + 1 - size.x / 2, topPx - 4 - size.y ), size );
  260. wxBrush brush;
  261. wxPoint poly[3];
  262. // Because a "1" looks off-center if it's actually centred.
  263. if( id == "1" )
  264. textRect.x -= 1;
  265. // We want an equalateral triangle, so use size.y for both axes.
  266. size.y += 3;
  267. // Make sure it's an even number so the slopes of the sides will be identical.
  268. size.y = ( size.y / 2 ) * 2;
  269. poly[0] = { cursorPos.x - 1 - size.y / 2, topPx - size.y };
  270. poly[1] = { cursorPos.x + 1 + size.y / 2, topPx - size.y };
  271. poly[2] = { cursorPos.x, topPx };
  272. brush.SetStyle( wxBRUSHSTYLE_SOLID );
  273. brush.SetColour( m_trace->GetTraceColour() );
  274. aDC.SetBrush( brush );
  275. aDC.DrawPolygon( 3, poly );
  276. aDC.SetTextForeground( fg );
  277. aDC.DrawLabel( id, textRect, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL );
  278. }
  279. }
  280. bool CURSOR::Inside( const wxPoint& aPoint ) const
  281. {
  282. if( !m_window || !m_trace )
  283. return false;
  284. return ( std::abs( (double) aPoint.x -
  285. m_window->x2p( m_trace->x2s( m_coords.x ) ) ) <= DRAG_MARGIN )
  286. || ( std::abs( (double) aPoint.y -
  287. m_window->y2p( m_trace->y2s( m_coords.y ) ) ) <= DRAG_MARGIN );
  288. }
  289. void CURSOR::UpdateReference()
  290. {
  291. if( !m_window )
  292. return;
  293. m_reference.x = m_window->x2p( m_trace->x2s( m_coords.x ) );
  294. m_reference.y = m_window->y2p( m_trace->y2s( m_coords.y ) );
  295. }
  296. SIM_PLOT_PANEL::SIM_PLOT_PANEL( const wxString& aCommand, int aOptions, wxWindow* parent,
  297. wxWindowID id, const wxPoint& pos, const wxSize& size, long style,
  298. const wxString& name ) :
  299. SIM_PANEL_BASE( aCommand, aOptions, parent, id, pos, size, style, name ),
  300. m_axis_x( nullptr ),
  301. m_axis_y1( nullptr ),
  302. m_axis_y2( nullptr ),
  303. m_axis_y3( nullptr ),
  304. m_dotted_cp( false )
  305. {
  306. m_sizer = new wxBoxSizer( wxVERTICAL );
  307. m_plotWin = new mpWindow( this, wxID_ANY, pos, size, style );
  308. m_plotWin->LimitView( true );
  309. m_plotWin->SetMargins( 35, 70, 35, 70 );
  310. UpdatePlotColors();
  311. updateAxes();
  312. // a mpInfoLegend displays le name of traces on the left top panel corner:
  313. m_legend = new mpInfoLegend( wxRect( 0, 40, 200, 40 ), wxTRANSPARENT_BRUSH );
  314. m_legend->SetVisible( false );
  315. m_plotWin->AddLayer( m_legend );
  316. m_plotWin->EnableDoubleBuffer( true );
  317. m_plotWin->UpdateAll();
  318. m_sizer->Add( m_plotWin, 1, wxALL | wxEXPAND, 1 );
  319. SetSizer( m_sizer );
  320. }
  321. SIM_PLOT_PANEL::~SIM_PLOT_PANEL()
  322. {
  323. // ~mpWindow destroys all the added layers, so there is no need to destroy m_traces contents
  324. }
  325. wxString SIM_PLOT_PANEL::GetUnitsX() const
  326. {
  327. LOG_SCALE<mpScaleXLog>* logScale = dynamic_cast<LOG_SCALE<mpScaleXLog>*>( m_axis_x );
  328. LIN_SCALE<mpScaleX>* linScale = dynamic_cast<LIN_SCALE<mpScaleX>*>( m_axis_x );
  329. if( logScale )
  330. return logScale->GetUnits();
  331. else if( linScale )
  332. return linScale->GetUnits();
  333. else
  334. return wxEmptyString;
  335. }
  336. wxString SIM_PLOT_PANEL::GetUnitsY1() const
  337. {
  338. LIN_SCALE<mpScaleY>* linScale = dynamic_cast<LIN_SCALE<mpScaleY>*>( m_axis_y1 );
  339. if( linScale )
  340. return linScale->GetUnits();
  341. else
  342. return wxEmptyString;
  343. }
  344. wxString SIM_PLOT_PANEL::GetUnitsY2() const
  345. {
  346. LIN_SCALE<mpScaleY>* linScale = dynamic_cast<LIN_SCALE<mpScaleY>*>( m_axis_y2 );
  347. if( linScale )
  348. return linScale->GetUnits();
  349. else
  350. return wxEmptyString;
  351. }
  352. wxString SIM_PLOT_PANEL::GetUnitsY3() const
  353. {
  354. LIN_SCALE<mpScaleY>* linScale = dynamic_cast<LIN_SCALE<mpScaleY>*>( m_axis_y3 );
  355. if( linScale )
  356. return linScale->GetUnits();
  357. else
  358. return wxEmptyString;
  359. }
  360. void SIM_PLOT_PANEL::updateAxes( SIM_TRACE_TYPE aNewTraceType )
  361. {
  362. switch( GetType() )
  363. {
  364. case ST_AC:
  365. if( !m_axis_x )
  366. {
  367. m_axis_x = new LOG_SCALE<mpScaleXLog>( wxEmptyString, wxT( "Hz" ), mpALIGN_BOTTOM );
  368. m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
  369. m_plotWin->AddLayer( m_axis_x );
  370. m_axis_y1 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "dBV" ), mpALIGN_LEFT );
  371. m_axis_y1->SetNameAlign( mpALIGN_LEFT );
  372. m_plotWin->AddLayer( m_axis_y1 );
  373. m_axis_y2 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "°" ), mpALIGN_RIGHT );
  374. m_axis_y2->SetNameAlign( mpALIGN_RIGHT );
  375. m_axis_y2->SetMasterScale( m_axis_y1 );
  376. m_plotWin->AddLayer( m_axis_y2 );
  377. }
  378. m_axis_x->SetName( _( "Frequency" ) );
  379. m_axis_y1->SetName( _( "Gain" ) );
  380. m_axis_y2->SetName( _( "Phase" ) );
  381. break;
  382. case ST_DC:
  383. prepareDCAxes( aNewTraceType );
  384. break;
  385. case ST_NOISE:
  386. if( !m_axis_x )
  387. {
  388. m_axis_x = new LOG_SCALE<mpScaleXLog>( wxEmptyString, wxT( "Hz" ), mpALIGN_BOTTOM );
  389. m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
  390. m_plotWin->AddLayer( m_axis_x );
  391. m_axis_y1 = new mpScaleY( wxEmptyString, mpALIGN_LEFT, false );
  392. m_axis_y1->SetNameAlign( mpALIGN_LEFT );
  393. m_plotWin->AddLayer( m_axis_y1 );
  394. }
  395. m_axis_x->SetName( _( "Frequency" ) );
  396. m_axis_y1->SetName( _( "noise [(V or A)^2/Hz]" ) );
  397. break;
  398. case ST_TRANSIENT:
  399. if( !m_axis_x )
  400. {
  401. m_axis_x = new LIN_SCALE<mpScaleX>( wxEmptyString, wxT( "s" ), mpALIGN_BOTTOM );
  402. m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
  403. m_plotWin->AddLayer( m_axis_x );
  404. m_axis_y1 = new LIN_SCALE<mpScaleY>(wxEmptyString, wxT( "V" ), mpALIGN_LEFT );
  405. m_axis_y1->SetNameAlign( mpALIGN_LEFT );
  406. m_plotWin->AddLayer( m_axis_y1 );
  407. m_axis_y2 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "A" ), mpALIGN_RIGHT );
  408. m_axis_y2->SetNameAlign( mpALIGN_RIGHT );
  409. m_axis_y2->SetMasterScale( m_axis_y1 );
  410. m_plotWin->AddLayer( m_axis_y2 );
  411. }
  412. m_axis_x->SetName( _( "Time" ) );
  413. m_axis_y1->SetName( _( "Voltage" ) );
  414. m_axis_y2->SetName( _( "Current" ) );
  415. if( ( aNewTraceType & SPT_POWER ) && !m_axis_y3 )
  416. {
  417. m_plotWin->SetMargins( 35, 140, 35, 70 );
  418. m_axis_y3 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "W" ), mpALIGN_FAR_RIGHT );
  419. m_axis_y3->SetNameAlign( mpALIGN_FAR_RIGHT );
  420. m_axis_y3->SetMasterScale( m_axis_y1 );
  421. m_plotWin->AddLayer( m_axis_y3 );
  422. }
  423. if( m_axis_y3 )
  424. m_axis_y3->SetName( _( "Power" ) );
  425. break;
  426. default:
  427. // suppress warnings
  428. break;
  429. }
  430. }
  431. void SIM_PLOT_PANEL::prepareDCAxes( SIM_TRACE_TYPE aNewTraceType )
  432. {
  433. wxString sim_cmd = GetSimCommand().Lower();
  434. wxString rem;
  435. if( sim_cmd.StartsWith( ".dc", &rem ) )
  436. {
  437. wxChar ch;
  438. rem.Trim( false );
  439. try
  440. {
  441. ch = rem.GetChar( 0 );
  442. }
  443. catch( ... )
  444. {
  445. // Best efforts
  446. }
  447. switch( ch )
  448. {
  449. // Make sure that we have a reliable default (even if incorrectly labeled)
  450. default:
  451. case 'v':
  452. if( !m_axis_x )
  453. {
  454. m_axis_x = new LIN_SCALE<mpScaleX>( wxEmptyString, wxT( "V" ), mpALIGN_BOTTOM );
  455. m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
  456. m_plotWin->AddLayer( m_axis_x );
  457. }
  458. m_axis_x->SetName( _( "Voltage (swept)" ) );
  459. break;
  460. case 'i':
  461. if( !m_axis_x )
  462. {
  463. m_axis_x = new LIN_SCALE<mpScaleX>( wxEmptyString, wxT( "A" ), mpALIGN_BOTTOM );
  464. m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
  465. m_plotWin->AddLayer( m_axis_x );
  466. }
  467. m_axis_x->SetName( _( "Current (swept)" ) );
  468. break;
  469. case 'r':
  470. if( !m_axis_x )
  471. {
  472. m_axis_x = new LIN_SCALE<mpScaleX>( wxEmptyString, wxT( "" ), mpALIGN_BOTTOM );
  473. m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
  474. m_plotWin->AddLayer( m_axis_x );
  475. }
  476. m_axis_x->SetName( _( "Resistance (swept)" ) );
  477. break;
  478. case 't':
  479. if( !m_axis_x )
  480. {
  481. m_axis_x = new LIN_SCALE<mpScaleX>( wxEmptyString, wxT( "°C" ), mpALIGN_BOTTOM );
  482. m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
  483. m_plotWin->AddLayer( m_axis_x );
  484. }
  485. m_axis_x->SetName( _( "Temperature (swept)" ) );
  486. break;
  487. }
  488. if( !m_axis_y1 )
  489. {
  490. m_axis_y1 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "V" ), mpALIGN_LEFT );
  491. m_axis_y1->SetNameAlign( mpALIGN_LEFT );
  492. m_plotWin->AddLayer( m_axis_y1 );
  493. }
  494. if( !m_axis_y2 )
  495. {
  496. m_axis_y2 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "A" ), mpALIGN_RIGHT );
  497. m_axis_y2->SetNameAlign( mpALIGN_RIGHT );
  498. m_plotWin->AddLayer( m_axis_y2 );
  499. }
  500. m_axis_y1->SetName( _( "Voltage (measured)" ) );
  501. m_axis_y2->SetName( _( "Current" ) );
  502. if( ( aNewTraceType & SPT_POWER ) && !m_axis_y3 )
  503. {
  504. m_plotWin->SetMargins( 35, 140, 35, 70 );
  505. m_axis_y3 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "W" ), mpALIGN_FAR_RIGHT );
  506. m_axis_y3->SetNameAlign( mpALIGN_FAR_RIGHT );
  507. m_axis_y3->SetMasterScale( m_axis_y1 );
  508. m_plotWin->AddLayer( m_axis_y3 );
  509. }
  510. if( m_axis_y3 )
  511. m_axis_y3->SetName( _( "Power" ) );
  512. }
  513. }
  514. void SIM_PLOT_PANEL::UpdatePlotColors()
  515. {
  516. // Update bg and fg colors:
  517. m_plotWin->SetColourTheme( m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::BACKGROUND ),
  518. m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::FOREGROUND ),
  519. m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::AXIS ) );
  520. // Update color of all traces
  521. for( const auto& [ name, trace ] : m_traces )
  522. {
  523. for( const auto& [ id, cursor ] : trace->GetCursors() )
  524. {
  525. if( cursor )
  526. cursor->SetPen( wxPen( m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::CURSOR ) ) );
  527. }
  528. }
  529. m_plotWin->UpdateAll();
  530. }
  531. void SIM_PLOT_PANEL::OnLanguageChanged()
  532. {
  533. updateAxes();
  534. m_plotWin->UpdateAll();
  535. }
  536. void SIM_PLOT_PANEL::UpdateTraceStyle( TRACE* trace )
  537. {
  538. int type = trace->GetType();
  539. wxPenStyle penStyle = ( ( ( type & SPT_AC_PHASE ) || ( type & SPT_CURRENT ) ) && m_dotted_cp )
  540. ? wxPENSTYLE_DOT
  541. : wxPENSTYLE_SOLID;
  542. trace->SetPen( wxPen( trace->GetTraceColour(), 2, penStyle ) );
  543. }
  544. TRACE* SIM_PLOT_PANEL::AddTrace( const wxString& aTitle, const wxString& aName,
  545. SIM_TRACE_TYPE aType )
  546. {
  547. TRACE* trace = nullptr;
  548. auto it = m_traces.find( aTitle );
  549. if( it != m_traces.end() )
  550. return it->second;
  551. updateAxes( aType );
  552. if( GetType() == ST_TRANSIENT || GetType() == ST_DC )
  553. {
  554. bool hasVoltageTraces = false;
  555. for( const auto& [ name, candidate ] : m_traces )
  556. {
  557. if( candidate->GetType() & SPT_VOLTAGE )
  558. {
  559. hasVoltageTraces = true;
  560. break;
  561. }
  562. }
  563. if( !hasVoltageTraces )
  564. {
  565. if( m_axis_y2 )
  566. m_axis_y2->SetMasterScale( nullptr );
  567. if( m_axis_y3 )
  568. m_axis_y3->SetMasterScale( nullptr );
  569. }
  570. }
  571. trace = new TRACE( aName, aType );
  572. trace->SetTraceColour( m_colors.GenerateColor( m_traces ) );
  573. UpdateTraceStyle( trace );
  574. m_traces[ aTitle ] = trace;
  575. m_plotWin->AddLayer( (mpLayer*) trace );
  576. return trace;
  577. }
  578. void SIM_PLOT_PANEL::SetTraceData( TRACE* trace, unsigned int aPoints, const double* aX,
  579. const double* aY )
  580. {
  581. std::vector<double> tmp( aY, aY + aPoints );
  582. if( GetType() == ST_AC )
  583. {
  584. if( trace->GetType() & SPT_AC_PHASE )
  585. {
  586. for( unsigned int i = 0; i < aPoints; i++ )
  587. tmp[i] = tmp[i] * 180.0 / M_PI; // convert to degrees
  588. }
  589. else
  590. {
  591. for( unsigned int i = 0; i < aPoints; i++ )
  592. {
  593. // log( 0 ) is not valid.
  594. if( tmp[i] != 0 )
  595. tmp[i] = 20 * log( tmp[i] ) / log( 10.0 ); // convert to dB
  596. }
  597. }
  598. }
  599. trace->SetData( std::vector<double>( aX, aX + aPoints ), tmp );
  600. if( ( trace->GetType() & SPT_AC_PHASE ) || ( trace->GetType() & SPT_CURRENT ) )
  601. trace->SetScale( m_axis_x, m_axis_y2 );
  602. else if( trace->GetType() & SPT_POWER )
  603. trace->SetScale( m_axis_x, m_axis_y3 );
  604. else
  605. trace->SetScale( m_axis_x, m_axis_y1 );
  606. for( auto& [ cursorId, cursor ] : trace->GetCursors() )
  607. {
  608. if( cursor )
  609. cursor->SetCoordX( cursor->GetCoords().x );
  610. }
  611. m_plotWin->UpdateAll();
  612. }
  613. bool SIM_PLOT_PANEL::DeleteTrace( const wxString& aName )
  614. {
  615. auto it = m_traces.find( aName );
  616. if( it != m_traces.end() )
  617. {
  618. TRACE* trace = it->second;
  619. m_traces.erase( it );
  620. for( const auto& [ id, cursor ] : trace->GetCursors() )
  621. {
  622. if( cursor )
  623. m_plotWin->DelLayer( cursor, true );
  624. }
  625. m_plotWin->DelLayer( trace, true, true );
  626. ResetScales();
  627. return true;
  628. }
  629. return false;
  630. }
  631. void SIM_PLOT_PANEL::EnableCursor( const wxString& aSignalName, const wxString aTraceName,
  632. int aCursorId, bool aEnable )
  633. {
  634. TRACE* t = GetTrace( aTraceName );
  635. if( t == nullptr || t->HasCursor( aCursorId ) == aEnable )
  636. return;
  637. if( aEnable )
  638. {
  639. CURSOR* cursor = new CURSOR( t, this );
  640. mpWindow* win = GetPlotWin();
  641. int width = win->GetXScreen() - win->GetMarginLeft() - win->GetMarginRight();
  642. int center = win->GetMarginLeft() + KiROUND( width * ( aCursorId == 1 ? 0.4 : 0.6 ) );
  643. cursor->SetName( aSignalName );
  644. cursor->SetX( center );
  645. cursor->SetPen( wxPen( m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::CURSOR ) ) );
  646. t->SetCursor( aCursorId, cursor );
  647. m_plotWin->AddLayer( cursor );
  648. }
  649. else
  650. {
  651. CURSOR* cursor = t->GetCursor( aCursorId );
  652. t->SetCursor( aCursorId, nullptr );
  653. m_plotWin->DelLayer( cursor, true );
  654. }
  655. // Notify the parent window about the changes
  656. wxQueueEvent( GetParent(), new wxCommandEvent( EVT_SIM_CURSOR_UPDATE ) );
  657. }
  658. void SIM_PLOT_PANEL::ResetScales()
  659. {
  660. if( m_axis_x )
  661. m_axis_x->ResetDataRange();
  662. if( m_axis_y1 )
  663. m_axis_y1->ResetDataRange();
  664. if( m_axis_y2 )
  665. m_axis_y2->ResetDataRange();
  666. if( m_axis_y3 )
  667. m_axis_y3->ResetDataRange();
  668. for( auto& [ name, trace ] : m_traces )
  669. trace->UpdateScales();
  670. }
  671. wxDEFINE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );