KiCad PCB EDA Suite
Loading...
Searching...
No Matches
mathplot.cpp
Go to the documentation of this file.
1
2// Name: mathplot.cpp
3// Purpose: Framework for plotting in wxWindows
4// Original Author: David Schalig
5// Maintainer: Davide Rondini
6// Contributors: Jose Luis Blanco, Val Greene, Maciej Suminski, Tomasz Wlostowski
7// Created: 21/07/2003
8// Last edit: 2024
9// Copyright: (c) David Schalig, Davide Rondini
10// Copyright (c) 2021-2024 KiCad Developers, see AUTHORS.txt for contributors.
11// Licence: wxWindows licence
13
14#include <wx/window.h>
15
16// Comment out for release operation:
17// (Added by J.L.Blanco, Aug 2007)
18//#define MATHPLOT_DO_LOGGING
19
20#ifdef __BORLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WX_PRECOMP
25#include "wx/object.h"
26#include "wx/font.h"
27#include "wx/colour.h"
28#include "wx/sizer.h"
29#include "wx/intl.h"
30#include "wx/dcclient.h"
31#include "wx/cursor.h"
32#include "gal/cursors.h"
33#endif
34
35#include <widgets/mathplot.h>
36#include <wx/graphics.h>
37#include <wx/image.h>
38
39#include <cmath>
40#include <cstdio> // used only for debug
41#include <ctime> // used for representation of x axes involving date
42#include <limits>
43#include <set>
44
45// Memory leak debugging
46#ifdef _DEBUG
47#define new DEBUG_NEW
48#endif
49
50// Legend margins
51#define mpLEGEND_MARGIN 5
52#define mpLEGEND_LINEWIDTH 10
53
54// See doxygen comments.
56
57
58bool mpFiniteRange( const std::vector<double>& aValues, double& aMin, double& aMax )
59{
60 bool found = false;
61
62 for( const double value : aValues )
63 {
64 if( !std::isfinite( value ) )
65 continue;
66
67 if( found )
68 {
69 aMin = std::min( aMin, value );
70 aMax = std::max( aMax, value );
71 }
72 else
73 {
74 aMin = value;
75 aMax = value;
76 found = true;
77 }
78 }
79
80 return found;
81}
82
83
84// -----------------------------------------------------------------------------
85// mpLayer
86// -----------------------------------------------------------------------------
87
88IMPLEMENT_ABSTRACT_CLASS( mpLayer, wxObject )
89
92{
93 SetPen( (wxPen&) *wxBLACK_PEN );
94 m_continuous = false; // Default
95 m_showName = true; // Default
96 m_visible = true;
97}
98
99
100// -----------------------------------------------------------------------------
101// mpInfoLayer
102// -----------------------------------------------------------------------------
103IMPLEMENT_DYNAMIC_CLASS( mpInfoLayer, mpLayer )
104
106{
107 m_dim = wxRect( 0, 0, 1, 1 );
108 m_brush = *wxTRANSPARENT_BRUSH;
109 m_reference.x = 0; m_reference.y = 0;
110 m_winX = 1; // parent->GetScrX();
111 m_winY = 1; // parent->GetScrY();
113}
114
115
116mpInfoLayer::mpInfoLayer( wxRect rect, const wxBrush* brush ) :
117 m_dim( rect )
118{
119 m_brush = *brush;
120 m_reference.x = rect.x;
121 m_reference.y = rect.y;
122 m_winX = 1; // parent->GetScrX();
123 m_winY = 1; // parent->GetScrY();
125}
126
127
131
132
133bool mpInfoLayer::Inside( const wxPoint& point ) const
134{
135 return m_dim.Contains( point );
136}
137
138
139bool mpInfoLayer::OnDoubleClick( const wxPoint& point, mpWindow& w )
140{
141 return false;
142}
143
144
146{
147 m_dim.SetX( m_reference.x + delta.x );
148 m_dim.SetY( m_reference.y + delta.y );
149}
150
151
153{
154 m_reference.x = m_dim.x;
155 m_reference.y = m_dim.y;
156}
157
158
160{
161 if( m_visible )
162 {
163 // Adjust relative position inside the window
164 int scrx = w.GetScrX();
165 int scry = w.GetScrY();
166
167 // Avoid dividing by 0
168 if( scrx == 0 )
169 scrx = 1;
170
171 if( scry == 0 )
172 scry = 1;
173
174 if( ( m_winX != scrx ) || ( m_winY != scry ) )
175 {
176 if( m_winX > 1 )
177 m_dim.x = (int) floor( (double) ( m_dim.x * scrx / m_winX ) );
178
179 if( m_winY > 1 )
180 {
181 m_dim.y = (int) floor( (double) ( m_dim.y * scry / m_winY ) );
183 }
184
185 // Finally update window size
186 m_winX = scrx;
187 m_winY = scry;
188 }
189
190 dc.SetPen( m_pen );
191 dc.SetBrush( m_brush );
192 dc.DrawRectangle( m_dim.x, m_dim.y, m_dim.width, m_dim.height );
193 }
194}
195
196
198{
199 return m_dim.GetPosition();
200}
201
202
204{
205 return m_dim.GetSize();
206}
207
208
213
214
215mpInfoLegend::mpInfoLegend( wxRect rect, const wxBrush* brush ) :
216 mpInfoLayer( rect, brush )
217{
218}
219
220
224
225
227{
228 if( m_visible )
229 {
230 // Adjust relative position inside the window
231 int scrx = w.GetScrX();
232 int scry = w.GetScrY();
233
234 if( m_winX != scrx || m_winY != scry )
235 {
236 if( m_winX > 1 )
237 m_dim.x = (int) floor( (double) ( m_dim.x * scrx / m_winX ) );
238
239 if( m_winY > 1 )
240 {
241 m_dim.y = (int) floor( (double) ( m_dim.y * scry / m_winY ) );
243 }
244
245 // Finally update window size
246 m_winX = scrx;
247 m_winY = scry;
248 }
249
250 // Opaque, so the traces and the grid it overlaps cannot show through and make the
251 // labels hard to read. It follows the plot's own background, dark mode included.
252 wxBrush background( w.GetBackgroundColour() );
253
254 dc.SetBrush( background );
255 dc.SetFont( GetPlotFont() );
256
257 const int baseWidth = mpLEGEND_MARGIN * 2 + mpLEGEND_LINEWIDTH;
258 int textX = baseWidth, textY = mpLEGEND_MARGIN;
259 int plotCount = 0;
260 int posY = 0;
261 int tmpX = 0;
262 int tmpY = 0;
263 mpLayer* layer = nullptr;
264 wxPen lpen;
265 wxString label;
266
267 for( unsigned int p = 0; p < w.CountAllLayers(); p++ )
268 {
269 layer = w.GetLayer( p );
270
271 if( layer->GetLayerType() == mpLAYER_PLOT && layer->IsVisible() )
272 {
273 label = layer->GetDisplayName();
274 dc.GetTextExtent( label, &tmpX, &tmpY );
275 textX = ( textX > tmpX + baseWidth ) ? textX : tmpX + baseWidth + mpLEGEND_MARGIN;
276 textY += tmpY;
277 }
278 }
279
280 dc.SetPen( m_pen );
281 dc.SetBrush( background );
282 m_dim.width = textX;
283
284 if( textY != mpLEGEND_MARGIN ) // Don't draw any thing if there are no visible layers
285 {
286 textY += mpLEGEND_MARGIN;
287 m_dim.height = textY;
288 dc.DrawRectangle( m_dim.x, m_dim.y, m_dim.width, m_dim.height );
289
290 // Set explicitly: other layers (e.g. plot cursors) leave their own text colour on
291 // the DC, which would otherwise leak into the legend's labels.
292 dc.SetTextForeground( w.GetForegroundColour() );
293
294 for( unsigned int p2 = 0; p2 < w.CountAllLayers(); p2++ )
295 {
296 layer = w.GetLayer( p2 );
297
298 if( layer->GetLayerType() == mpLAYER_PLOT && layer->IsVisible() )
299 {
300 label = layer->GetDisplayName();
301 lpen = layer->GetPen();
302 dc.GetTextExtent( label, &tmpX, &tmpY );
303 dc.SetPen( lpen );
304 posY = m_dim.y + mpLEGEND_MARGIN + plotCount * tmpY + (tmpY >> 1);
305 dc.DrawLine( m_dim.x + mpLEGEND_MARGIN, // X start coord
306 posY, // Y start coord
307 m_dim.x + mpLEGEND_LINEWIDTH + mpLEGEND_MARGIN, // X end coord
308 posY );
309 dc.DrawText( label,
310 m_dim.x + baseWidth,
311 m_dim.y + mpLEGEND_MARGIN + plotCount * tmpY );
312 plotCount++;
313 }
314 }
315 }
316 }
317}
318
319
320// -----------------------------------------------------------------------------
321// mpLayer implementations - functions
322// -----------------------------------------------------------------------------
323
324IMPLEMENT_ABSTRACT_CLASS( mpFX, mpLayer )
325
326mpFX::mpFX( const wxString& name, int flags )
327{
328 SetName( name );
329 m_flags = flags;
331}
332
333
334void mpFX::Plot( wxDC& dc, mpWindow& w )
335{
336 if( m_visible )
337 {
338 dc.SetPen( m_pen );
339
340 wxCoord startPx = w.GetMarginLeft();
341 wxCoord endPx = w.GetScrX() - w.GetMarginRight();
342 wxCoord minYpx = w.GetMarginTop();
343 wxCoord maxYpx = w.GetScrY() - w.GetMarginBottom();
344
345 wxCoord iy = 0;
346
347 if( m_pen.GetWidth() <= 1 )
348 {
349 for( wxCoord i = startPx; i < endPx; ++i )
350 {
351 iy = w.y2p( GetY( w.p2x( i ) ) );
352
353 // Draw the point only if you can draw outside margins or if the point is
354 // inside margins
355 if( ( iy >= minYpx ) && ( iy <= maxYpx ) )
356 dc.DrawPoint( i, iy );
357 }
358 }
359 else
360 {
361 for( wxCoord i = startPx; i < endPx; ++i )
362 {
363 iy = w.y2p( GetY( w.p2x( i ) ) );
364
365 // Draw the point only if you can draw outside margins or if the point is
366 // inside margins
367 if( iy >= minYpx && iy <= maxYpx )
368 dc.DrawLine( i, iy, i, iy );
369 }
370 }
371
372 if( !m_name.IsEmpty() && m_showName )
373 {
374 dc.SetFont( GetPlotFont() );
375
376 wxCoord tx, ty;
377 dc.GetTextExtent( m_name, &tx, &ty );
378
379 if( ( m_flags & mpALIGNMASK ) == mpALIGN_RIGHT )
380 tx = ( w.GetScrX() - tx ) - w.GetMarginRight() - 8;
381 else if( ( m_flags & mpALIGNMASK ) == mpALIGN_CENTER )
382 tx = ( ( w.GetScrX() - w.GetMarginRight() - w.GetMarginLeft() - tx ) / 2 )
383 + w.GetMarginLeft();
384 else
385 tx = w.GetMarginLeft() + 8;
386
387 dc.DrawText( m_name, tx, w.y2p( GetY( w.p2x( tx ) ) ) );
388 }
389 }
390}
391
392
393IMPLEMENT_ABSTRACT_CLASS( mpFY, mpLayer )
394
395
396mpFY::mpFY( const wxString& name, int flags )
397{
398 SetName( name );
399 m_flags = flags;
401}
402
403
404void mpFY::Plot( wxDC& dc, mpWindow& w )
405{
406 if( m_visible )
407 {
408 dc.SetPen( m_pen );
409
410 wxCoord i, ix;
411
412 wxCoord startPx = w.GetMarginLeft();
413 wxCoord endPx = w.GetScrX() - w.GetMarginRight();
414 wxCoord minYpx = w.GetMarginTop();
415 wxCoord maxYpx = w.GetScrY() - w.GetMarginBottom();
416
417 if( m_pen.GetWidth() <= 1 )
418 {
419 for( i = minYpx; i < maxYpx; ++i )
420 {
421 ix = w.x2p( GetX( w.p2y( i ) ) );
422
423 if( ( ix >= startPx ) && ( ix <= endPx ) )
424 dc.DrawPoint( ix, i );
425 }
426 }
427 else
428 {
429 for( i = 0; i< w.GetScrY(); ++i )
430 {
431 ix = w.x2p( GetX( w.p2y( i ) ) );
432
433 if( ( ix >= startPx ) && ( ix <= endPx ) )
434 dc.DrawLine( ix, i, ix, i );
435 }
436 }
437
438 if( !m_name.IsEmpty() && m_showName )
439 {
440 dc.SetFont( GetPlotFont() );
441
442 wxCoord tx, ty;
443 dc.GetTextExtent( m_name, &tx, &ty );
444
445 if( ( m_flags & mpALIGNMASK ) == mpALIGN_TOP )
446 ty = w.GetMarginTop() + 8;
447 else if( ( m_flags & mpALIGNMASK ) == mpALIGN_CENTER )
448 ty = ( ( w.GetScrY() - w.GetMarginTop() - w.GetMarginBottom() - ty ) / 2 )
449 + w.GetMarginTop();
450 else
451 ty = w.GetScrY() - 8 - ty - w.GetMarginBottom();
452
453 dc.DrawText( m_name, w.x2p( GetX( w.p2y( ty ) ) ), ty );
454 }
455 }
456}
457
458
459IMPLEMENT_ABSTRACT_CLASS( mpFXY, mpLayer )
460
461
462mpFXY::mpFXY( const wxString& name, int flags )
463{
464 SetName( name );
465 m_flags = flags;
467 m_scaleX = nullptr;
468 m_scaleY = nullptr;
469
470 // Avoid not initialized members:
472}
473
474
475void mpFXY::UpdateViewBoundary( wxCoord xnew, wxCoord ynew )
476{
477 // Keep track of how many points have been drawn and the bounding box
478 maxDrawX = (xnew > maxDrawX) ? xnew : maxDrawX;
479 minDrawX = (xnew < minDrawX) ? xnew : minDrawX;
480 maxDrawY = (maxDrawY > ynew) ? maxDrawY : ynew;
481 minDrawY = (minDrawY < ynew) ? minDrawY : ynew;
482 // drawnPoints++;
483}
484
485
486void mpFXY::Plot( wxDC& dc, mpWindow& w )
487{
488 // If trace doesn't have any data yet then it won't have any scale set. In any case, there's
489 // nothing to plot.
490 if( !GetCount() )
491 return;
492
493 wxCHECK_RET( m_scaleX, wxS( "X scale was not set" ) );
494 wxCHECK_RET( m_scaleY, wxS( "Y scale was not set" ) );
495
496 if( !m_visible )
497 return;
498
499 wxCoord startPx = w.GetMarginLeft();
500 wxCoord endPx = w.GetScrX() - w.GetMarginRight();
501 wxCoord minYpx = w.GetMarginTop();
502 wxCoord maxYpx = w.GetScrY() - w.GetMarginBottom();
503
504 // Check for a collapsed window before we try to allocate a negative number of points
505 if( endPx <= startPx || minYpx >= maxYpx )
506 return;
507
508 dc.SetPen( m_pen );
509
510 double x, y;
511 // Do this to reset the counters to evaluate bounding box for label positioning
512 Rewind();
513
514 if( GetNextXY( x, y ) && std::isfinite( x ) && std::isfinite( y ) )
515 {
516 maxDrawX = x;
517 minDrawX = x;
518 maxDrawY = y;
519 minDrawY = y;
520 }
521
522 // drawnPoints = 0;
523 Rewind();
524
525 dc.SetClippingRegion( startPx, minYpx, endPx - startPx + 1, maxYpx - minYpx + 1 );
526
527 if( !m_continuous )
528 {
529 bool first = true;
530 wxCoord ix = 0;
531 std::set<wxCoord> ys;
532
533 while( GetNextXY( x, y ) )
534 {
535 // a non-finite sample has no position on the axis, so there is nothing to draw
536 if( !std::isfinite( x ) || !std::isfinite( y ) )
537 continue;
538
539 double px = m_scaleX->TransformToPlot( x );
540 double py = m_scaleY->TransformToPlot( y );
541 wxCoord newX = w.x2p( px );
542
543 if( first )
544 {
545 ix = newX;
546 first = false;
547 }
548
549 if( newX == ix ) // continue until a new X coordinate is reached
550 {
551 // collect all unique points
552 ys.insert( w.y2p( py ) );
553 continue;
554 }
555
556 for( auto& iy: ys )
557 {
558 if( ( ix >= startPx ) && ( ix <= endPx ) && ( iy >= minYpx ) && ( iy <= maxYpx ) )
559 {
560 // for some reason DrawPoint does not use the current pen, so we use
561 // DrawLine for fat pens
562 if( m_pen.GetWidth() <= 1 )
563 dc.DrawPoint( ix, iy );
564 else
565 dc.DrawLine( ix, iy, ix, iy );
566
567 UpdateViewBoundary( ix, iy );
568 }
569 }
570
571 ys.clear();
572 ix = newX;
573 ys.insert( w.y2p( py ) );
574 }
575 }
576 else for( int sweep = 0; sweep < GetSweepCount(); ++sweep )
577 {
579
580 int count = 0;
581 int x0 = 0; // X position of merged current vertical line
582 int ymin0 = 0; // y min coord of merged current vertical line
583 int ymax0 = 0; // y max coord of merged current vertical line
584 int dupx0 = 0; // count of currently merged vertical lines
585 wxPoint line_start; // starting point of the current line to draw
586
587 // A buffer to store coordinates of lines to draw
588 std::vector<wxPoint>pointList;
589 pointList.reserve( ( endPx - startPx ) * 2 );
590
591 double nextX;
592 double nextY;
593 bool hasNext = GetNextXY( nextX, nextY );
594 bool offRight = false;
595
596 // Note: we can use dc.DrawLines() only for a reasonable number or points (<10,000),
597 // because at least on Windows dc.DrawLines() can hang for a lot of points. Note that
598 // this includes the intermediate points when drawing dotted lines.
599
600 // A buffer for the merged points, reused across the runs of a fragmented trace
601 std::vector<wxPoint> drawPoints;
602 drawPoints.reserve( ( endPx - startPx ) * 2 );
603
604 // Short verticals spoil anti-aliasing on Retina displays, so only significant
605 // aggregations are worth the ink; the user can zoom in for detail
606 auto flushVertical =
607 [&]()
608 {
609 if( count && dupx0 > 1 && abs( ymax0 - ymin0 ) > 2 )
610 dc.DrawLine( x0, ymin0, x0, ymax0 );
611 };
612
613 auto flushPolyline =
614 [&]()
615 {
616 if( pointList.size() > 1 )
617 {
618 // Second pass optimization is to merge horizontal segments. This improves
619 // the look of dotted lines, keeps the point count down, and it's easy.
620 //
621 // This pass also includes a final protection to keep MSW from hanging by
622 // chunking to a size it can handle.
623 drawPoints.clear();
624
625#ifdef __WXMSW__
626 int chunkSize = 10000;
627#else
628 int chunkSize = 100000;
629#endif
630 wxPenStyle penStyle = dc.GetPen().GetStyle();
631 bool isSolidPen = ( penStyle == wxPENSTYLE_SOLID
632 || penStyle == wxPENSTYLE_TRANSPARENT );
633
634 if( !isSolidPen )
635 chunkSize /= 500;
636
637 drawPoints.push_back( pointList[0] ); // push the first point in list
638
639 for( size_t ii = 1; ii < pointList.size()-1; ii++ )
640 {
641 // Skip intermediate points between the first point and the last point
642 // of the segment candidate. This optimization merges horizontal line
643 // segments, which breaks non-solid pen styles by altering segment
644 // lengths.
645 if( isSolidPen
646 && drawPoints.back().y == pointList[ii].y
647 && drawPoints.back().y == pointList[ii+1].y )
648 {
649 continue;
650 }
651 else
652 {
653 drawPoints.push_back( pointList[ii] );
654
655 if( (int) drawPoints.size() > chunkSize )
656 {
657 dc.DrawLines( (int) drawPoints.size(), &drawPoints[0] );
658 drawPoints.clear();
659
660 // Restart the line with the current point
661 drawPoints.push_back( pointList[ii] );
662 }
663 }
664 }
665
666 // push the last point to draw in list
667 if( drawPoints.back() != pointList.back() )
668 drawPoints.push_back( pointList.back() );
669
670 dc.DrawLines( (int) drawPoints.size(), &drawPoints[0] );
671 }
672
673 pointList.clear();
674 count = 0;
675
676 // offRight allows one point past the right edge so the line reaches it; each
677 // run of points gets that allowance for itself
678 offRight = false;
679 };
680
681 // Our first-pass optimization is to exclude points outside the view, and aggregate all
682 // contiguous y values found at a single x value into a vertical line.
683 while( hasNext )
684 {
685 x = nextX;
686 y = nextY;
687 hasNext = GetNextXY( nextX, nextY );
688
689 // A non-finite sample has no position on the axis, so end the run of points here and
690 // let the trace show a gap rather than a line bridging the missing data
691 if( !std::isfinite( x ) || !std::isfinite( y ) )
692 {
693 flushVertical();
694 flushPolyline();
695 continue;
696 }
697
698 double px = m_scaleX->TransformToPlot( x );
699 double py = m_scaleY->TransformToPlot( y );
700
701 wxCoord x1 = w.x2p( px );
702 wxCoord y1 = w.y2p( py );
703
704 // Note that we can't start *right* at the edge of the view because we need to
705 // interpolate between two points, one of which might be outside the view.
706 // Note: x1 is a value truncated from px by w.x2p(). So to be sure the first point
707 // is drawn, the x1 low limit is startPx-1 in plot coordinates
708 if( x1 < startPx-1 )
709 {
710 // a non-finite neighbour cannot pull this point into view
711 if( !std::isfinite( nextX ) )
712 continue;
713
714 wxCoord nextX1 = w.x2p( m_scaleX->TransformToPlot( nextX ) );
715
716 if( nextX1 < startPx-1 )
717 continue;
718 }
719 else if( x1 > endPx )
720 {
721 if( offRight )
722 continue;
723 else
724 offRight = true;
725 }
726
727 if( !count || line_start.x != x1 )
728 {
729 flushVertical();
730
731 x0 = x1;
732 ymin0 = ymax0 = y1;
733 dupx0 = 0;
734
735 pointList.emplace_back( wxPoint( x1, y1 ) );
736
737 line_start.x = x1;
738 line_start.y = y1;
739 count++;
740 }
741 else
742 {
743 ymin0 = std::min( ymin0, y1 );
744 ymax0 = std::max( ymax0, y1 );
745 x0 = x1;
746 dupx0++;
747 }
748 }
749
750 flushPolyline();
751 }
752
753 if( !m_name.IsEmpty() && m_showName )
754 {
755 dc.SetFont( GetPlotFont() );
756
757 wxCoord tx, ty;
758 dc.GetTextExtent( m_name, &tx, &ty );
759
760 if( ( m_flags & mpALIGNMASK ) == mpALIGN_NW )
761 {
762 tx = minDrawX + 8;
763 ty = maxDrawY + 8;
764 }
765 else if( ( m_flags & mpALIGNMASK ) == mpALIGN_NE )
766 {
767 tx = maxDrawX - tx - 8;
768 ty = maxDrawY + 8;
769 }
770 else if( ( m_flags & mpALIGNMASK ) == mpALIGN_SE )
771 {
772 tx = maxDrawX - tx - 8;
773 ty = minDrawY - ty - 8;
774 }
775 else
776 {
777 // mpALIGN_SW
778 tx = minDrawX + 8;
779 ty = minDrawY - ty - 8;
780 }
781
782 dc.DrawText( m_name, tx, ty );
783 }
784
785 dc.DestroyClippingRegion();
786}
787
788
789// -----------------------------------------------------------------------------
790// mpLayer implementations - furniture (scales, ...)
791// -----------------------------------------------------------------------------
792
793#define mpLN10 2.3025850929940456840179914546844
794
796{
797 double minV, maxV, minVvis, maxVvis;
798
799 GetDataRange( minV, maxV );
800 getVisibleDataRange( w, minVvis, maxVvis );
801
802 m_absVisibleMaxV = std::max( std::abs( minVvis ), std::abs( maxVvis ) );
803
804 m_tickValues.clear();
805 m_tickLabels.clear();
806
807 double minErr = 1000000000000.0;
808 double bestStep = 1.0;
809 int m_scrX = w.GetXScreen();
810
811 for( int i = 10; i <= 20; i += 2 )
812 {
813 double curr_step = fabs( maxVvis - minVvis ) / (double) i;
814 double base = pow( 10, floor( log10( curr_step ) ) );
815 double stepInt = floor( curr_step / base ) * base;
816 double err = fabs( curr_step - stepInt );
817
818 if( err < minErr )
819 {
820 minErr = err;
821 bestStep = stepInt;
822 }
823 }
824
825 double numberSteps = floor( ( maxVvis - minVvis ) / bestStep );
826
827 // Half the number of ticks according to window size.
828 // The value 96 is used to have only 4 ticks when m_scrX is 268.
829 // For each 96 device context units, is possible to add a new tick.
830 while( numberSteps - 2.0 >= m_scrX/96.0 )
831 {
832 bestStep *= 2;
833 numberSteps = floor( ( maxVvis - minVvis ) / bestStep );
834 }
835
836 double v = floor( minVvis / bestStep ) * bestStep;
837 double zeroOffset = 100000000.0;
838
839 while( v < maxVvis )
840 {
841 m_tickValues.push_back( v );
842
843 if( fabs( v ) < zeroOffset )
844 zeroOffset = fabs( v );
845
846 v += bestStep;
847 }
848
849 if( zeroOffset <= bestStep )
850 {
851 for( double& t : m_tickValues )
852 t -= zeroOffset;
853 }
854
855 for( double t : m_tickValues )
856 m_tickLabels.emplace_back( t );
857
858 updateTickLabels( dc, w );
859}
860
861
863{
864 m_rangeSet = false;
865 m_axisLocked = false;
866 m_axisMin = 0;
867 m_axisMax = 0;
869
870 // initialize these members mainly to avoid not initialized values
871 m_offset = 0.0;
872 m_scale = 1.0;
873 m_absVisibleMaxV = 0.0;
874 m_flags = 0; // Flag for axis alignment
875 m_ticks = true; // Flag to toggle between ticks or grid
876 m_minV = 0.0;
877 m_maxV = 0.0;
879 m_maxLabelWidth = 1;
880}
881
882
884{
886 m_maxLabelWidth = 0;
887
888 for( const TICK_LABEL& tickLabel : m_tickLabels )
889 {
890 int tx, ty;
891 const wxString s = tickLabel.label;
892
893 dc.GetTextExtent( s, &tx, &ty );
894 m_maxLabelHeight = std::max( ty, m_maxLabelHeight );
895 m_maxLabelWidth = std::max( tx, m_maxLabelWidth );
896 }
897}
898
899
901{
902 formatLabels();
904}
905
906
907void mpScaleY::getVisibleDataRange( mpWindow& w, double& minV, double& maxV )
908{
909 wxCoord minYpx = w.GetMarginTop();
910 wxCoord maxYpx = w.GetScrY() - w.GetMarginBottom();
911
912 double pymin = w.p2y( minYpx );
913 double pymax = w.p2y( maxYpx );
914
915 minV = TransformFromPlot( pymax );
916 maxV = TransformFromPlot( pymin );
917}
918
919
921{
922 // No need for slave ticks when there aren't 2 main ticks for them to go between
923 if( m_masterScale->m_tickValues.size() < 2 )
924 return;
925
926 m_tickValues.clear();
927 m_tickLabels.clear();
928
929 double p0 = m_masterScale->TransformToPlot( m_masterScale->m_tickValues[0] );
930 double p1 = m_masterScale->TransformToPlot( m_masterScale->m_tickValues[1] );
931
932 m_scale = 1.0 / ( m_maxV - m_minV );
933 m_offset = -m_minV;
934
935 double y_slave0 = p0 / m_scale;
936 double y_slave1 = p1 / m_scale;
937
938 double dy_slave = ( y_slave1 - y_slave0 );
939 double exponent = floor( log10( dy_slave ) );
940 double base = dy_slave / pow( 10.0, exponent );
941
942 double dy_scaled = ceil( 2.0 * base ) / 2.0 * pow( 10.0, exponent );
943
944 double minvv, maxvv;
945
946 getVisibleDataRange( w, minvv, maxvv );
947
948 minvv = floor( minvv / dy_scaled ) * dy_scaled;
949
950 m_scale = 1.0 / ( m_maxV - m_minV );
951 m_scale *= dy_slave / dy_scaled;
952
953 m_offset = p0 / m_scale - minvv;
954
955 m_tickValues.clear();
956
958
959 for( double tickValue : m_masterScale->m_tickValues )
960 {
961 double m = TransformFromPlot( m_masterScale->TransformToPlot( tickValue ) );
962 m_tickValues.push_back( m );
963 m_tickLabels.emplace_back( m );
964 m_absVisibleMaxV = std::max( m_absVisibleMaxV, fabs( m ) );
965 }
966}
967
968
970{
971 double minVvis, maxVvis;
972
973 if( m_axisLocked )
974 {
975 minVvis = m_axisMin;
976 maxVvis = m_axisMax;
977
978 double plotTop = w.p2y( w.GetMarginTop() );
979 double plotBottom = w.p2y( w.GetScrY() - w.GetMarginBottom() );
980 double plotSpan = plotTop - plotBottom;
981 double axisSpan = m_axisMax - m_axisMin;
982
983 if( axisSpan > 0.0 && plotSpan > 0.0 )
984 {
985 m_scale = plotSpan / axisSpan;
986 m_offset = plotBottom / m_scale - m_axisMin;
987 }
988 else
989 {
991 m_scale = 1.0 / axisSpan;
992 }
993 }
994 else if( m_masterScale )
995 {
997 updateTickLabels( dc, w );
998
999 return;
1000 }
1001 else
1002 {
1003 getVisibleDataRange( w, minVvis, maxVvis );
1004 }
1005
1006 m_absVisibleMaxV = std::max( std::abs( minVvis ), std::abs( maxVvis ) );
1007 m_tickValues.clear();
1008 m_tickLabels.clear();
1009
1010 double minErr = 1000000000000.0;
1011 double bestStep = 1.0;
1012 int m_scrY = w.GetYScreen();
1013
1014 for( int i = 10; i <= 20; i += 2 )
1015 {
1016 double curr_step = fabs( maxVvis - minVvis ) / (double) i;
1017 double base = pow( 10, floor( log10( curr_step ) ) );
1018 double stepInt = floor( curr_step / base ) * base;
1019 double err = fabs( curr_step - stepInt );
1020
1021 if( err< minErr )
1022 {
1023 minErr = err;
1024 bestStep = stepInt;
1025 }
1026 }
1027
1028 double numberSteps = floor( ( maxVvis - minVvis ) / bestStep );
1029
1030 // Half the number of ticks according to window size.
1031 // For each 32 device context units, is possible to add a new tick.
1032 while( numberSteps >= m_scrY / 32.0 )
1033 {
1034 bestStep *= 2;
1035 numberSteps = floor( ( maxVvis - minVvis ) / bestStep );
1036 }
1037
1038 double v = floor( minVvis / bestStep ) * bestStep;
1039 double zeroOffset = 100000000.0;
1040 const int iterLimit = 1000;
1041 int i = 0;
1042
1043 while( v <= maxVvis && i < iterLimit )
1044 {
1045 m_tickValues.push_back( v );
1046
1047 if( fabs( v ) < zeroOffset )
1048 zeroOffset = fabs( v );
1049
1050 v += bestStep;
1051 i++;
1052 }
1053
1054 // something weird happened...
1055 if( i == iterLimit )
1056 m_tickValues.clear();
1057
1058 if( zeroOffset <= bestStep )
1059 {
1060 for( double& t : m_tickValues )
1061 t -= zeroOffset;
1062 }
1063
1064 for( double t : m_tickValues )
1065 m_tickLabels.emplace_back( t );
1066
1067 updateTickLabels( dc, w );
1068}
1069
1070
1071void mpScaleXBase::getVisibleDataRange( mpWindow& w, double& minV, double& maxV )
1072{
1073 wxCoord startPx = w.GetMarginLeft();
1074 wxCoord endPx = w.GetScrX() - w.GetMarginRight();
1075 double pxmin = w.p2x( startPx );
1076 double pxmax = w.p2x( endPx );
1077
1078 minV = TransformFromPlot( pxmin );
1079 maxV = TransformFromPlot( pxmax );
1080}
1081
1082
1084{
1085 double minV, maxV, minVvis, maxVvis;
1086
1087 GetDataRange( minV, maxV );
1088 getVisibleDataRange( w, minVvis, maxVvis );
1089
1090 // double decades = log( maxV / minV ) / log(10);
1091 double minDecade = pow( 10, floor( log10( minV ) ) );
1092 double maxDecade = pow( 10, ceil( log10( maxV ) ) );
1093 double visibleDecades = log( maxVvis / minVvis ) / log( 10 );
1094 double step = 10.0;
1095 int m_scrX = w.GetXScreen();
1096
1097 double d;
1098
1099 m_tickValues.clear();
1100 m_tickLabels.clear();
1101
1102 if( minDecade == 0.0 )
1103 return;
1104
1105 // Half the number of ticks according to window size.
1106 // The value 96 is used to have only 4 ticks when m_scrX is 268.
1107 // For each 96 device context units, is possible to add a new tick.
1108 while( visibleDecades - 2 >= m_scrX / 96.0 )
1109 {
1110 step *= 10.0;
1111 visibleDecades = log( maxVvis / minVvis ) / log( step );
1112
1113 if( !std::isfinite( visibleDecades ) )
1114 break;
1115 }
1116
1117 for( d = minDecade; d<=maxDecade; d *= step )
1118 {
1119 m_tickLabels.emplace_back( d );
1120
1121 for( double dd = d; dd < d * step; dd += d )
1122 {
1123 if( visibleDecades < 2 )
1124 m_tickLabels.emplace_back( dd );
1125
1126 m_tickValues.push_back( dd );
1127 }
1128 }
1129
1130 updateTickLabels( dc, w );
1131}
1132
1133
1134IMPLEMENT_ABSTRACT_CLASS( mpScaleXBase, mpLayer )
1135IMPLEMENT_DYNAMIC_CLASS( mpScaleX, mpScaleXBase )
1136IMPLEMENT_DYNAMIC_CLASS( mpScaleXLog, mpScaleXBase )
1137
1138
1139mpScaleXBase::mpScaleXBase( const wxString& name, int flags, bool ticks, unsigned int type )
1140{
1141 SetName( name );
1142 SetPen( (wxPen&) *wxGREY_PEN );
1143 m_flags = flags;
1144 m_ticks = ticks;
1146}
1147
1148
1149mpScaleX::mpScaleX( const wxString& name, int flags, bool ticks, unsigned int type ) :
1150 mpScaleXBase( name, flags, ticks, type )
1151{
1152}
1153
1154
1155mpScaleXLog::mpScaleXLog( const wxString& name, int flags, bool ticks, unsigned int type ) :
1156 mpScaleXBase( name, flags, ticks, type )
1157{
1158}
1159
1160
1162{
1163 int tx, ty;
1164
1165 m_offset = -m_minV;
1166 m_scale = 1.0 / ( m_maxV - m_minV );
1167
1168 // Ticks are measured against whatever font the DC currently has set, so set ours first;
1169 // otherwise recalculateTicks() sizes m_maxLabelHeight from a leftover font, throwing off
1170 // where the axis name is placed relative to the bottom margin.
1171 dc.SetFont( GetPlotFont() );
1172 recalculateTicks( dc, w );
1173
1174 if( m_visible )
1175 {
1176 dc.SetPen( m_pen );
1177 int orgy = 0;
1178
1179 const int extend = w.GetScrX();
1180
1181 if( m_flags == mpALIGN_CENTER )
1182 orgy = w.y2p( 0 );
1183
1184 if( m_flags == mpALIGN_TOP )
1185 orgy = w.GetMarginTop();
1186
1187 if( m_flags == mpALIGN_BOTTOM )
1188 orgy = w.GetScrY() - w.GetMarginBottom();
1189
1191 orgy = w.GetScrY() - 1;
1192
1194 orgy = 1;
1195
1196 wxCoord startPx = w.GetMarginLeft();
1197 wxCoord endPx = w.GetScrX() - w.GetMarginRight();
1198 wxCoord minYpx = w.GetMarginTop();
1199 wxCoord maxYpx = w.GetScrY() - w.GetMarginBottom();
1200
1201 // int tmp=-65535;
1202
1203 // Control labels height to decide where to put axis name (below labels or on top of axis).
1204 int labelH = m_maxLabelHeight;
1205
1206 // int maxExtent = tc.MaxLabelWidth();
1207 for( double tp : m_tickValues )
1208 {
1209 double px = TransformToPlot( tp );
1210 const int p = (int) ( ( px - w.GetPosX() ) * w.GetScaleX() );
1211
1212 if( p >= startPx && p <= endPx )
1213 {
1214 if( m_ticks ) // draw axis ticks
1215 {
1217 dc.DrawLine( p, orgy, p, orgy - 4 );
1218 else
1219 dc.DrawLine( p, orgy, p, orgy + 4 );
1220 }
1221 else // draw grid dotted lines
1222 {
1223 m_pen.SetStyle( wxPENSTYLE_DOT );
1224 dc.SetPen( m_pen );
1225
1226 if( m_flags == mpALIGN_BOTTOM )
1227 {
1228 m_pen.SetStyle( wxPENSTYLE_DOT );
1229 dc.SetPen( m_pen );
1230 dc.DrawLine( p, orgy + 4, p, minYpx );
1231 m_pen.SetStyle( wxPENSTYLE_SOLID );
1232 dc.SetPen( m_pen );
1233 dc.DrawLine( p, orgy + 4, p, orgy - 4 );
1234 }
1235 else
1236 {
1237 if( m_flags == mpALIGN_TOP )
1238 dc.DrawLine( p, orgy - 4, p, maxYpx );
1239 else
1240 dc.DrawLine( p, minYpx, p, maxYpx );
1241 }
1242
1243 m_pen.SetStyle( wxPENSTYLE_SOLID );
1244 dc.SetPen( m_pen );
1245 }
1246 }
1247 }
1248
1249 m_pen.SetStyle( wxPENSTYLE_SOLID );
1250 dc.SetPen( m_pen );
1251 dc.DrawLine( startPx, minYpx, endPx, minYpx );
1252 dc.DrawLine( startPx, maxYpx, endPx, maxYpx );
1253
1254 // Actually draw labels, taking care of not overlapping them, and distributing them
1255 // regularly
1256 for( const TICK_LABEL& tickLabel : m_tickLabels )
1257 {
1258 if( !tickLabel.visible )
1259 continue;
1260
1261 double px = TransformToPlot( tickLabel.pos );
1262 const int p = (int) ( ( px - w.GetPosX() ) * w.GetScaleX() );
1263
1264 if( ( p >= startPx ) && ( p <= endPx ) )
1265 {
1266 // Write ticks labels in s string
1267 wxString s = tickLabel.label;
1268
1269 dc.GetTextExtent( s, &tx, &ty );
1270
1271 if( ( m_flags == mpALIGN_BORDER_BOTTOM ) || ( m_flags == mpALIGN_TOP ) )
1272 dc.DrawText( s, p - tx / 2, orgy - 4 - ty );
1273 else
1274 dc.DrawText( s, p - tx / 2, orgy + 4 );
1275 }
1276 }
1277
1278 // Draw axis name
1279 dc.GetTextExtent( m_name, &tx, &ty );
1280
1281 switch( m_nameFlags )
1282 {
1284 dc.DrawText( m_name, extend - tx - 4, orgy - 8 - ty - labelH );
1285 break;
1286
1287 case mpALIGN_BOTTOM:
1288 dc.DrawText( m_name, ( endPx + startPx ) / 2 - tx / 2, orgy + 6 + labelH );
1289 break;
1290
1291 case mpALIGN_CENTER:
1292 dc.DrawText( m_name, extend - tx - 4, orgy - 4 - ty );
1293 break;
1294
1295 case mpALIGN_TOP:
1296 if( w.GetMarginTop() > (ty + labelH + 8) )
1297 dc.DrawText( m_name, ( endPx - startPx - tx ) >> 1, orgy - 6 - ty - labelH );
1298 else
1299 dc.DrawText( m_name, extend - tx - 4, orgy + 4 );
1300
1301 break;
1302
1303 case mpALIGN_BORDER_TOP:
1304 dc.DrawText( m_name, extend - tx - 4, orgy + 6 + labelH );
1305 break;
1306
1307 default:
1308 break;
1309 }
1310 }
1311}
1312
1313
1314IMPLEMENT_DYNAMIC_CLASS( mpScaleY, mpLayer )
1315
1316
1317mpScaleY::mpScaleY( const wxString& name, int flags, bool ticks )
1318{
1319 SetName( name );
1320 SetPen( (wxPen&) *wxGREY_PEN );
1321 m_flags = flags;
1322 m_ticks = ticks;
1324 m_masterScale = nullptr;
1326}
1327
1328
1329void mpScaleY::Plot( wxDC& dc, mpWindow& w )
1330{
1331 m_offset = -m_minV;
1332 m_scale = 1.0 / ( m_maxV - m_minV );
1333
1334 // See mpScaleXBase::Plot(): set the font before measuring so label extents match what is
1335 // actually drawn.
1336 dc.SetFont( GetPlotFont() );
1337 recalculateTicks( dc, w );
1338
1339 if( m_visible )
1340 {
1341 dc.SetPen( m_pen );
1342
1343 int orgx = 0;
1344
1345 if( m_flags == mpALIGN_CENTER )
1346 orgx = w.x2p( 0 );
1347
1348 if( m_flags == mpALIGN_LEFT )
1349 orgx = w.GetMarginLeft();
1350
1351 if( m_flags == mpALIGN_RIGHT )
1352 orgx = w.GetScrX() - w.GetMarginRight();
1353
1354 if( m_flags == mpALIGN_FAR_RIGHT )
1355 orgx = w.GetScrX() - ( w.GetMarginRight() / 2 );
1356
1358 orgx = w.GetScrX() - 1;
1359
1361 orgx = 1;
1362
1363 wxCoord endPx = w.GetScrX() - w.GetMarginRight();
1364 wxCoord minYpx = w.GetMarginTop();
1365 wxCoord maxYpx = w.GetScrY() - w.GetMarginBottom();
1366
1367 // Draw line
1368 dc.DrawLine( orgx, minYpx, orgx, maxYpx );
1369
1370 wxCoord tx, ty;
1371 wxString s;
1372 wxString fmt;
1373
1374 int labelW = 0;
1375
1376 // Before staring cycle, calculate label height
1377 int labelHeight = 0;
1378 s.Printf( fmt, 0 );
1379 dc.GetTextExtent( s, &tx, &labelHeight );
1380
1381 for( double tp : m_tickValues )
1382 {
1383 double py = TransformToPlot( tp );
1384 const int p = (int) ( ( w.GetPosY() - py ) * w.GetScaleY() );
1385
1386 if( p >= minYpx && p <= maxYpx )
1387 {
1388 if( m_ticks ) // Draw axis ticks
1389 {
1391 dc.DrawLine( orgx, p, orgx + 4, p );
1392 else
1393 dc.DrawLine( orgx - 4, p, orgx, p );
1394 }
1395 else
1396 {
1397 dc.DrawLine( orgx - 4, p, orgx + 4, p );
1398
1399 m_pen.SetStyle( wxPENSTYLE_DOT );
1400 dc.SetPen( m_pen );
1401
1402 dc.DrawLine( orgx - 4, p, endPx, p );
1403
1404 m_pen.SetStyle( wxPENSTYLE_SOLID );
1405 dc.SetPen( m_pen );
1406 }
1407
1408 // Print ticks labels
1409 }
1410 }
1411
1412 for( const TICK_LABEL& tickLabel : m_tickLabels )
1413 {
1414 double py = TransformToPlot( tickLabel.pos );
1415 const int p = (int) ( ( w.GetPosY() - py ) * w.GetScaleY() );
1416
1417 if( !tickLabel.visible )
1418 continue;
1419
1420 if( p >= minYpx && p <= maxYpx )
1421 {
1422 s = tickLabel.label;
1423 dc.GetTextExtent( s, &tx, &ty );
1424
1427 dc.DrawText( s, orgx + 4, p - ty / 2 );
1428 else
1429 dc.DrawText( s, orgx - 4 - tx, p - ty / 2 ); // ( s, orgx+4, p-ty/2);
1430 }
1431 }
1432
1433 // Draw axis name
1434 dc.GetTextExtent( m_name, &tx, &ty );
1435
1436 switch( m_nameFlags )
1437 {
1439 dc.DrawText( m_name, labelW + 8, 4 );
1440 break;
1441
1442 case mpALIGN_LEFT:
1443 dc.DrawText( m_name, orgx - ( tx / 2 ), minYpx - ty - 4 );
1444 break;
1445
1446 case mpALIGN_CENTER:
1447 dc.DrawText( m_name, orgx + 4, 4 );
1448 break;
1449
1450 case mpALIGN_RIGHT:
1451 case mpALIGN_FAR_RIGHT:
1452 dc.DrawText( m_name, orgx - ( tx / 2 ), minYpx - ty - 4 );
1453 break;
1454
1456 dc.DrawText( m_name, orgx - 6 - tx - labelW, 4 );
1457 break;
1458
1459 default:
1460 break;
1461 }
1462 }
1463}
1464
1465
1466// -----------------------------------------------------------------------------
1467// mpWindow
1468// -----------------------------------------------------------------------------
1469
1470IMPLEMENT_DYNAMIC_CLASS( mpWindow, wxWindow )
1471
1472BEGIN_EVENT_TABLE( mpWindow, wxWindow )
1473EVT_PAINT( mpWindow::OnPaint )
1474EVT_SIZE( mpWindow::OnSize )
1475
1476EVT_MIDDLE_DOWN( mpWindow::OnMouseMiddleDown ) // JLB
1477EVT_RIGHT_UP( mpWindow::OnShowPopupMenu )
1478EVT_MOUSEWHEEL( mpWindow::onMouseWheel ) // JLB
1479EVT_MAGNIFY( mpWindow::onMagnify )
1480EVT_MOTION( mpWindow::onMouseMove ) // JLB
1481EVT_LEFT_DOWN( mpWindow::onMouseLeftDown )
1482EVT_LEFT_DCLICK( mpWindow::onMouseLeftDClick )
1483EVT_LEFT_UP( mpWindow::onMouseLeftRelease )
1484
1491END_EVENT_TABLE()
1492
1493
1498
1499
1500mpWindow::mpWindow( wxWindow* parent, wxWindowID id ) :
1501 mpWindow( DelegatingContructorTag(), parent, id, wxDefaultPosition, wxDefaultSize, 0,
1502 wxT( "mathplot" ) )
1503{
1504 m_popmenu.Append( mpID_ZOOM_UNDO, _( "Undo Last Zoom" ),
1505 _( "Return zoom to level prior to last zoom action" ) );
1506 m_popmenu.Append( mpID_ZOOM_REDO, _( "Redo Last Zoom" ),
1507 _( "Return zoom to level prior to last zoom undo" ) );
1508 m_popmenu.AppendSeparator();
1509 m_popmenu.Append( mpID_ZOOM_IN, _( "Zoom In" ), _( "Zoom in plot view." ) );
1510 m_popmenu.Append( mpID_ZOOM_OUT, _( "Zoom Out" ), _( "Zoom out plot view." ) );
1511 m_popmenu.Append( mpID_CENTER, _( "Center on Cursor" ),
1512 _( "Center plot view to this position" ) );
1513 m_popmenu.Append( mpID_FIT, _( "Fit on Screen" ), _( "Set plot view to show all items" ) );
1514
1515 m_layers.clear();
1516 SetBackgroundColour( *wxWHITE );
1517 m_bgColour = *wxWHITE;
1518 m_fgColour = *wxBLACK;
1519
1520 SetSizeHints( 128, 128 );
1521
1522 // J.L.Blanco: Eliminates the "flick" with the double buffer.
1523 SetBackgroundStyle( wxBG_STYLE_CUSTOM );
1524
1526 UpdateAll();
1527}
1528
1529
1531{
1532 // Free all the layers:
1533 DelAllLayers( true, false );
1534
1535 delete m_buff_bmp;
1536 m_buff_bmp = nullptr;
1537}
1538
1539
1540// Mouse handler, for detecting when the user drag with the right button or just "clicks" for
1541// the menu.
1542// JLB
1543void mpWindow::OnMouseMiddleDown( wxMouseEvent& event )
1544{
1545 m_mouseMClick.x = event.GetX();
1546 m_mouseMClick.y = event.GetY();
1547}
1548
1549
1550void mpWindow::onMagnify( wxMouseEvent& event )
1551{
1553 {
1554 event.Skip();
1555 return;
1556 }
1557
1558 float zoom = event.GetMagnification() + 1.0f;
1559 wxPoint pos( event.GetX(), event.GetY() );
1560
1561 if( zoom > 1.0f )
1562 ZoomIn( pos, zoom );
1563 else if( zoom < 1.0f )
1564 ZoomOut( pos, 1.0f / zoom );
1565}
1566
1567
1568// Process mouse wheel events
1569// JLB
1570void mpWindow::onMouseWheel( wxMouseEvent& event )
1571{
1573 {
1574 event.Skip();
1575 return;
1576 }
1577
1578 const wxMouseWheelAxis axis = event.GetWheelAxis();
1579 const int modifiers = event.GetModifiers();
1581
1582 if( axis == wxMOUSE_WHEEL_HORIZONTAL )
1583 {
1584 action = m_mouseWheelActions.horizontal;
1585 }
1586 else if( modifiers == wxMOD_NONE )
1587 {
1588 action = m_mouseWheelActions.verticalUnmodified;
1589 }
1590 else if( modifiers == wxMOD_CONTROL )
1591 {
1592 action = m_mouseWheelActions.verticalWithCtrl;
1593 }
1594 else if( modifiers == wxMOD_SHIFT )
1595 {
1596 action = m_mouseWheelActions.verticalWithShift;
1597 }
1598 else if( modifiers == wxMOD_ALT )
1599 {
1600 action = m_mouseWheelActions.verticalWithAlt;
1601 }
1602 else
1603 {
1604 event.Skip();
1605 return;
1606 }
1607
1608 PerformMouseWheelAction( event, action );
1609}
1610
1611
1612// If the user "drags" with the right button pressed, do "pan"
1613// JLB
1614void mpWindow::onMouseMove( wxMouseEvent& event )
1615{
1617 {
1618 event.Skip();
1619 return;
1620 }
1621
1622 wxCursor cursor = wxCURSOR_MAGNIFIER;
1623
1624 if( event.m_middleDown )
1625 {
1626 cursor = wxCURSOR_ARROW;
1627
1628 // The change:
1629 int Ax = m_mouseMClick.x - event.GetX();
1630 int Ay = m_mouseMClick.y - event.GetY();
1631
1632 // For the next event, use relative to this coordinates.
1633 m_mouseMClick.x = event.GetX();
1634 m_mouseMClick.y = event.GetY();
1635
1636 if( Ax )
1637 {
1638 double Ax_units = Ax / m_scaleX;
1639 SetXView( m_posX + Ax_units, m_desiredXmax + Ax_units, m_desiredXmin + Ax_units );
1640 }
1641
1642 if( Ay )
1643 {
1644 double Ay_units = -Ay / m_scaleY;
1645 SetYView( m_posY + Ay_units, m_desiredYmax + Ay_units, m_desiredYmin + Ay_units );
1646 }
1647
1648 if( Ax || Ay )
1649 UpdateAll();
1650 }
1651 else if( event.m_leftDown )
1652 {
1653 if( m_movingInfoLayer )
1654 {
1655 if( dynamic_cast<mpInfoLegend*>( m_movingInfoLayer ) )
1656 cursor = wxCURSOR_SIZING;
1657 else
1658 cursor = wxCURSOR_SIZEWE;
1659
1660 wxPoint moveVector( event.GetX() - m_mouseLClick.x, event.GetY() - m_mouseLClick.y );
1661 m_movingInfoLayer->Move( moveVector );
1662 m_zooming = false;
1663 }
1664 else
1665 {
1666 cursor = wxCURSOR_MAGNIFIER;
1667
1668 wxClientDC dc( this );
1669 wxPen pen( m_fgColour, 1, wxPENSTYLE_DOT );
1670 dc.SetPen( pen );
1671 dc.SetBrush( *wxTRANSPARENT_BRUSH );
1672 dc.DrawRectangle( m_mouseLClick.x, m_mouseLClick.y,
1673 event.GetX() - m_mouseLClick.x, event.GetY() - m_mouseLClick.y );
1674 m_zooming = true;
1677 m_zoomRect.width = event.GetX() - m_mouseLClick.x;
1678 m_zoomRect.height = event.GetY() - m_mouseLClick.y;
1679 }
1680
1681 UpdateAll();
1682 }
1683 else
1684 {
1685 for( mpLayer* layer : m_layers)
1686 {
1687 if( layer->IsInfo() && layer->IsVisible() )
1688 {
1689 mpInfoLayer* infoLayer = (mpInfoLayer*) layer;
1690
1691 if( infoLayer->Inside( event.GetPosition() ) )
1692 {
1693 if( dynamic_cast<mpInfoLegend*>( infoLayer ) )
1694 cursor = wxCURSOR_SIZING;
1695 else
1696 cursor = wxCURSOR_SIZEWE;
1697 }
1698 }
1699 }
1700 }
1701
1702 SetCursor( cursor );
1703
1704 event.Skip();
1705}
1706
1707
1708void mpWindow::onMouseLeftDown( wxMouseEvent& event )
1709{
1710 m_mouseLClick.x = event.GetX();
1711 m_mouseLClick.y = event.GetY();
1712 m_zooming = true;
1713 wxPoint pointClicked = event.GetPosition();
1714 m_movingInfoLayer = IsInsideInfoLayer( pointClicked );
1715
1716 // the grabbed overlay draws last, so it stays readable where overlays have to overlap
1717 if( m_movingInfoLayer )
1718 {
1719 DelLayer( m_movingInfoLayer, false, false );
1720 AddLayer( m_movingInfoLayer, false );
1721 Refresh( false );
1722 }
1723
1724 event.Skip();
1725}
1726
1727
1728void mpWindow::onMouseLeftDClick( wxMouseEvent& event )
1729{
1730 wxPoint pointClicked = event.GetPosition();
1731
1732 if( mpInfoLayer* infoLayer = IsInsideInfoLayer( pointClicked ) )
1733 {
1734 if( infoLayer->OnDoubleClick( pointClicked, *this ) )
1735 {
1736 UpdateAll();
1737 return;
1738 }
1739 }
1740
1741 event.Skip();
1742}
1743
1744
1745void mpWindow::onMouseLeftRelease( wxMouseEvent& event )
1746{
1747 wxPoint release( event.GetX(), event.GetY() );
1748 wxPoint press( m_mouseLClick.x, m_mouseLClick.y );
1749
1750 m_zooming = false;
1751
1752 if( m_movingInfoLayer != nullptr )
1753 {
1754 m_movingInfoLayer->UpdateReference();
1755 m_movingInfoLayer = nullptr;
1756 }
1757 else
1758 {
1759 if( release != press )
1760 ZoomRect( press, release );
1761 }
1762
1763 event.Skip();
1764}
1765
1766
1768{
1769 if( UpdateBBox() )
1771}
1772
1773
1774// JL
1775void mpWindow::Fit( double xMin, double xMax, double yMin, double yMax, const wxCoord* printSizeX,
1776 const wxCoord* printSizeY, wxOrientation directions )
1777{
1778 const bool isPrinting = printSizeX != nullptr && printSizeY != nullptr;
1779
1780 // Save desired borders:
1781 double newDesiredXmin = xMin;
1782 double newDesiredXmax = xMax;
1783 double newDesiredYmin = yMin;
1784 double newDesiredYmax = yMax;
1785
1786 // Provide a gap between the extrema of the curve and the top/bottom edges of the
1787 // plot area. Not to be confused with the left/right/top/bottom margins outside the plot area.
1788 const double xGap = fabs( xMax - xMin ) * m_leftRightPlotGapFactor;
1789 const double yGap = fabs( yMax - yMin ) * m_topBottomPlotGapFactor;
1790 xMin -= xGap;
1791 xMax += xGap;
1792 yMin -= yGap;
1793 yMax += yGap;
1794
1795 int newScrX = m_scrX;
1796 int newScrY = m_scrY;
1797
1798 if( isPrinting )
1799 {
1800 // Printer:
1801 newScrX = *printSizeX;
1802 newScrY = *printSizeY;
1803 }
1804 else
1805 {
1806 // Normal case (screen):
1807 GetClientSize( &newScrX, &newScrY );
1808 }
1809
1810 // Compute the width/height in pixels for the plot area.
1811 const int plotScreenWidth = newScrX - m_marginLeft - m_marginRight;
1812 const int plotScreenHeight = newScrY - m_marginTop - m_marginBottom;
1813
1814 // Adjust scale so that desired X/Y span plus extra gap fits in the plot area
1815 double desiredSpanX = xMax - xMin;
1816 double desiredSpanY = yMax - yMin;
1817 double newScaleX = ( desiredSpanX != 0 ) ? double( plotScreenWidth ) / desiredSpanX : 1;
1818 double newScaleY = ( desiredSpanY != 0 ) ? double( plotScreenHeight ) / desiredSpanY : 1;
1819
1820 // Adjust corner coordinates:
1821 // Upstream's aspect lock code has been removed, so no need to account for centering.
1822 double newPosX = xMin - ( m_marginLeft / newScaleX );
1823 double newPosY = yMax + ( m_marginTop / newScaleY );
1824
1825 // Commit above changes to member variables only if enabled for their respective dimension.
1826 if( ( ( directions & wxHORIZONTAL ) != 0 ) || isPrinting )
1827 {
1828 // Don't commit the passed desired bounds when printing
1829 if( !isPrinting )
1830 {
1831 m_desiredXmin = newDesiredXmin;
1832 m_desiredXmax = newDesiredXmax;
1833 }
1834
1835 m_scrX = newScrX;
1836 m_scaleX = newScaleX;
1837 m_posX = newPosX;
1838 }
1839
1840 if( ( ( directions & wxVERTICAL ) != 0 ) || isPrinting )
1841 {
1842 // Don't commit the passed desired bounds when printing
1843 if( !isPrinting )
1844 {
1845 m_desiredYmin = newDesiredYmin;
1846 m_desiredYmax = newDesiredYmax;
1847 }
1848
1849 m_scrY = newScrY;
1850 m_scaleY = newScaleY;
1851 m_posY = newPosY;
1852 }
1853
1854 // It is VERY IMPORTANT to NOT call Refresh if we are drawing to the printer!!
1855 // Otherwise, the DC dimensions will be those of the window instead of the printer device
1856 // The caller wanting to print should perform another Fit() afterwards to restore this
1857 // object's state.
1858 if( !isPrinting )
1859 {
1860 UpdateAll();
1861
1862 if( ( directions & wxHORIZONTAL ) != 0 )
1864 }
1865}
1866
1867
1868void mpWindow::AdjustLimitedView( wxOrientation directions )
1869{
1870 if( !m_enableLimitedView )
1871 return;
1872
1873 // The m_desired* members are expressed in plot coordinates.
1874 // They should be clamped against their respective m_minX, m_maxX, m_minY, m_maxY limits.
1875
1876 if( ( directions & wxHORIZONTAL ) != 0 )
1877 {
1878 if( m_desiredXmin < m_minX )
1879 {
1880 double diff = m_minX - m_desiredXmin;
1881 m_posX += diff;
1882 m_desiredXmax += diff;
1884 }
1885
1886 if( m_desiredXmax > m_maxX )
1887 {
1888 double diff = m_desiredXmax - m_maxX;
1889 m_posX -= diff;
1890 m_desiredXmin -= diff;
1892 }
1893 }
1894
1895 if( ( directions & wxVERTICAL ) != 0 )
1896 {
1897 if( m_desiredYmin < m_minY )
1898 {
1899 double diff = m_minY - m_desiredYmin;
1900 m_posY += diff;
1901 m_desiredYmax += diff;
1903 }
1904
1905 if( m_desiredYmax > m_maxY )
1906 {
1907 double diff = m_desiredYmax - m_maxY;
1908 m_posY -= diff;
1909 m_desiredYmin -= diff;
1911 }
1912 }
1913}
1914
1915
1916bool mpWindow::SetXView( double pos, double desiredMax, double desiredMin )
1917{
1918 // TODO (ecorm): Investigate X scale flickering when panning at minimum zoom level
1919 // Possible cause: When AdjustLimitedView subtracts the out-of-bound delta, it does not
1920 // revert back to the exact same original coordinates due to floating point rounding errors.
1921 m_posX = pos;
1922 m_desiredXmax = desiredMax;
1923 m_desiredXmin = desiredMin;
1924 AdjustLimitedView( wxHORIZONTAL );
1925
1927
1928 return true;
1929}
1930
1931
1932bool mpWindow::SetYView( double pos, double desiredMax, double desiredMin )
1933{
1934 m_posY = pos;
1935 m_desiredYmax = desiredMax;
1936 m_desiredYmin = desiredMin;
1937 AdjustLimitedView( wxVERTICAL );
1938
1939 return true;
1940}
1941
1942
1943void mpWindow::ZoomIn( const wxPoint& centerPoint )
1944{
1945 ZoomIn( centerPoint, zoomIncrementalFactor, wxBOTH );
1946}
1947
1948
1949void mpWindow::ZoomIn( const wxPoint& centerPoint, double zoomFactor, wxOrientation directions )
1950{
1951 DoZoom( centerPoint, zoomFactor, directions );
1952}
1953
1954
1955void mpWindow::ZoomOut( const wxPoint& centerPoint )
1956{
1957 ZoomOut( centerPoint, zoomIncrementalFactor, wxBOTH );
1958}
1959
1960
1961void mpWindow::ZoomOut( const wxPoint& centerPoint, double zoomFactor, wxOrientation directions )
1962{
1963 if( zoomFactor == 0 )
1964 zoomFactor = 1.0;
1965
1966 DoZoom( centerPoint, 1.0 / zoomFactor, directions );
1967}
1968
1969
1970void mpWindow::ZoomRect( wxPoint p0, wxPoint p1 )
1971{
1973
1974 // Constrain given rectangle to plot area
1975 const int pMinX = m_marginLeft;
1976 const int pMaxX = m_scrX - m_marginRight;
1977 const int pMinY = m_marginTop;
1978 const int pMaxY = m_scrY - m_marginBottom;
1979 p0.x = std::max( p0.x, pMinX );
1980 p0.x = std::min( p0.x, pMaxX );
1981 p0.y = std::max( p0.y, pMinY );
1982 p0.y = std::min( p0.y, pMaxY );
1983 p1.x = std::max( p1.x, pMinX );
1984 p1.x = std::min( p1.x, pMaxX );
1985 p1.y = std::max( p1.y, pMinY );
1986 p1.y = std::min( p1.y, pMaxY );
1987
1988 // Compute the 2 corners in graph coordinates:
1989 double p0x = p2x( p0.x );
1990 double p0y = p2y( p0.y );
1991 double p1x = p2x( p1.x );
1992 double p1y = p2y( p1.y );
1993
1994 // Order them:
1995 double zoom_x_min = p0x<p1x ? p0x : p1x;
1996 double zoom_x_max = p0x>p1x ? p0x : p1x;
1997 double zoom_y_min = p0y<p1y ? p0y : p1y;
1998 double zoom_y_max = p0y>p1y ? p0y : p1y;
1999
2000 Fit( zoom_x_min, zoom_x_max, zoom_y_min, zoom_y_max );
2001
2002 // Even with the input rectangle constrained to the plot area, it's still possible for the
2003 // resulting view to exceed limits when a portion of the gap is grabbed.
2005
2006 // These additional checks are needed because AdjustLimitedView only adjusts the position
2007 // and not the scale.
2008 wxOrientation directionsNeedingRefitting = ViewNeedsRefitting( wxBOTH );
2009
2010 if( directionsNeedingRefitting != 0 )
2011 Fit( m_minX, m_maxX, m_minY, m_maxY, nullptr, nullptr, directionsNeedingRefitting );
2012}
2013
2014
2015void mpWindow::pushZoomUndo( const std::array<double, 4>& aZoom )
2016{
2017 m_undoZoomStack.push( aZoom );
2018
2019 while( !m_redoZoomStack.empty() )
2020 m_redoZoomStack.pop();
2021}
2022
2023
2025{
2026 if( m_undoZoomStack.size() )
2027 {
2029
2030 std::array<double, 4> zoom = m_undoZoomStack.top();
2031 m_undoZoomStack.pop();
2032
2033 Fit( zoom[0], zoom[1], zoom[2], zoom[3] );
2035 }
2036}
2037
2038
2040{
2041 if( m_redoZoomStack.size() )
2042 {
2044
2045 std::array<double, 4> zoom = m_redoZoomStack.top();
2046 m_redoZoomStack.pop();
2047
2048 Fit( zoom[0], zoom[1], zoom[2], zoom[3] );
2050 }
2051}
2052
2053
2054void mpWindow::OnShowPopupMenu( wxMouseEvent& event )
2055{
2056 m_clickedX = event.GetX();
2057 m_clickedY = event.GetY();
2058
2059 m_popmenu.Enable( mpID_ZOOM_UNDO, !m_undoZoomStack.empty() );
2060 m_popmenu.Enable( mpID_ZOOM_REDO, !m_redoZoomStack.empty() );
2061
2062 PopupMenu( &m_popmenu, event.GetX(), event.GetY() );
2063}
2064
2065
2066void mpWindow::OnFit( wxCommandEvent& WXUNUSED( event ) )
2067{
2069
2070 Fit();
2071}
2072
2073
2074void mpWindow::OnCenter( wxCommandEvent& WXUNUSED( event ) )
2075{
2076 GetClientSize( &m_scrX, &m_scrY );
2077 int centerX = ( m_scrX - m_marginLeft - m_marginRight ) / 2;
2078 int centerY = ( m_scrY - m_marginTop - m_marginBottom ) / 2;
2079 SetPos( p2x( m_clickedX - centerX ), p2y( m_clickedY - centerY ) );
2080}
2081
2082
2093
2094
2095void mpWindow::onZoomIn( wxCommandEvent& WXUNUSED( event ) )
2096{
2097 ZoomIn( wxPoint( m_mouseMClick.x, m_mouseMClick.y ) );
2098}
2099
2100
2101void mpWindow::onZoomOut( wxCommandEvent& WXUNUSED( event ) )
2102{
2103 ZoomOut();
2104}
2105
2106
2107void mpWindow::onZoomUndo( wxCommandEvent& WXUNUSED( event ) )
2108{
2109 ZoomUndo();
2110}
2111
2112
2113void mpWindow::onZoomRedo( wxCommandEvent& WXUNUSED( event ) )
2114{
2115 ZoomRedo();
2116}
2117
2118
2119void mpWindow::OnSize( wxSizeEvent& WXUNUSED( event ) )
2120{
2121 // Try to fit again with the new window size:
2123}
2124
2125
2126bool mpWindow::AddLayer( mpLayer* layer, bool refreshDisplay )
2127{
2128 if( layer )
2129 {
2130 m_layers.push_back( layer );
2131
2132 if( refreshDisplay )
2133 UpdateAll();
2134
2135 return true;
2136 }
2137
2138 return false;
2139}
2140
2141
2142bool mpWindow::DelLayer( mpLayer* layer, bool alsoDeleteObject, bool refreshDisplay )
2143{
2144 wxLayerList::iterator layIt;
2145
2146 for( layIt = m_layers.begin(); layIt != m_layers.end(); layIt++ )
2147 {
2148 if( *layIt == layer )
2149 {
2150 // Also delete the object?
2151 if( alsoDeleteObject )
2152 delete *layIt;
2153
2154 m_layers.erase( layIt ); // this deleted the reference only
2155
2156 if( refreshDisplay )
2157 UpdateAll();
2158
2159 return true;
2160 }
2161 }
2162
2163 return false;
2164}
2165
2166
2167void mpWindow::DelAllLayers( bool alsoDeleteObject, bool refreshDisplay )
2168{
2169 while( m_layers.size()>0 )
2170 {
2171 // Also delete the object?
2172 if( alsoDeleteObject )
2173 delete m_layers[0];
2174
2175 m_layers.erase( m_layers.begin() ); // this deleted the reference only
2176 }
2177
2178 if( refreshDisplay )
2179 UpdateAll();
2180}
2181
2182
2183void mpWindow::OnPaint( wxPaintEvent& WXUNUSED( event ) )
2184{
2185 wxPaintDC paintDC( this );
2186
2187 paintDC.GetSize( &m_scrX, &m_scrY ); // This is the size of the visible area only!
2188
2189 // Selects direct or buffered draw:
2190 wxDC* targetDC = &paintDC;
2191
2192 // J.L.Blanco @ Aug 2007: Added double buffer support
2194 {
2195 // Allocate the backing bitmap in physical pixels and tag it with the window's DPI
2196 // scale factor so fonts and primitives rendered through the memory DC keep their
2197 // logical size once blitted to the DPI-aware paint DC. Without this, axis tick
2198 // labels and legends shrink proportionally to the display scale on HiDPI displays.
2199 const double scale = GetDPIScaleFactor();
2200 const int physX = static_cast<int>( std::round( m_scrX * scale ) );
2201 const int physY = static_cast<int>( std::round( m_scrY * scale ) );
2202
2203 if( !m_buff_bmp || m_last_lx != physX || m_last_ly != physY
2204 || m_buff_bmp->GetScaleFactor() != scale )
2205 {
2206 m_buff_dc.SelectObject( wxNullBitmap );
2207 delete m_buff_bmp;
2208 m_buff_bmp = new wxBitmap( physX, physY );
2209 m_buff_bmp->SetScaleFactor( scale );
2210 m_buff_dc.SelectObject( *m_buff_bmp );
2211 m_last_lx = physX;
2212 m_last_ly = physY;
2213 }
2214
2215 targetDC = &m_buff_dc;
2216 }
2217
2218 if( wxGraphicsContext* ctx = targetDC->GetGraphicsContext() )
2219 {
2220 if( !ctx->SetInterpolationQuality( wxINTERPOLATION_BEST ) )
2221 if( !ctx->SetInterpolationQuality( wxINTERPOLATION_GOOD ) )
2222 ctx->SetInterpolationQuality( wxINTERPOLATION_FAST );
2223
2224 ctx->SetAntialiasMode( wxANTIALIAS_DEFAULT );
2225 }
2226
2227 // Draw background:
2228 targetDC->SetPen( *wxTRANSPARENT_PEN );
2229 wxBrush brush( GetBackgroundColour() );
2230 targetDC->SetBrush( brush );
2231 targetDC->SetTextForeground( m_fgColour );
2232 targetDC->DrawRectangle( 0, 0, m_scrX, m_scrY );
2233
2234 // Draw all the layers:
2235 for( mpLayer* layer : m_layers )
2236 layer->Plot( *targetDC, *this );
2237
2238 if( m_zooming )
2239 {
2240 wxPen pen( m_fgColour, 1, wxPENSTYLE_DOT );
2241 targetDC->SetPen( pen );
2242 targetDC->SetBrush( *wxTRANSPARENT_BRUSH );
2243 targetDC->DrawRectangle( m_zoomRect );
2244 }
2245
2246 // If doublebuffer, draw now to the window:
2248 paintDC.Blit( 0, 0, m_scrX, m_scrY, targetDC, 0, 0 );
2249}
2250
2251
2252void mpWindow::DoZoom( const wxPoint& centerPoint, double zoomFactor, wxOrientation directions )
2253{
2254 const bool horizontally = ( directions & wxHORIZONTAL ) != 0;
2255 const bool vertically = ( directions & wxVERTICAL ) != 0;
2256
2258
2259 // Preserve the position of the clicked point:
2260 wxPoint c( centerPoint );
2261 if( c == wxDefaultPosition )
2262 {
2263 GetClientSize( &m_scrX, &m_scrY );
2264 c.x = ( m_scrX - m_marginLeft - m_marginRight ) / 2 + m_marginLeft;
2265 c.y = ( m_scrY - m_marginTop - m_marginBottom ) / 2 + m_marginTop;
2266 }
2267 else
2268 {
2269 c.x = std::max( c.x, m_marginLeft );
2270 c.x = std::min( c.x, m_scrX - m_marginRight );
2271 c.y = std::max( c.y, m_marginTop );
2272 c.y = std::min( c.y, m_scrY - m_marginBottom );
2273 }
2274
2275 // Zoom in/out:
2276 const double MAX_SCALE = 1e6;
2277 const double newScaleX = horizontally ? ( m_scaleX * zoomFactor ) : m_scaleX;
2278 const double newScaleY = vertically ? ( m_scaleY * zoomFactor ) : m_scaleY;
2279
2280 // Baaaaad things happen when you zoom in too much..
2281 if( newScaleX > MAX_SCALE || newScaleY > MAX_SCALE )
2282 return;
2283
2284 if( horizontally )
2285 {
2286 // Transform the clicked X point to layer coordinates:
2287 const double prior_layer_x = p2x( c.x );
2288
2289 // Adjust the new X scale and plot X origin:
2290 m_scaleX = newScaleX;
2291 m_posX = prior_layer_x - c.x / newScaleX;
2292
2293 // Recompute the desired X view extents:
2295 }
2296
2297 if( vertically )
2298 {
2299 // Transform the clicked Y point to layer coordinates:
2300 const double prior_layer_y = p2y( c.y );
2301
2302 // Adjust the new Y scale and plot Y origin:
2303 m_scaleY = newScaleY;
2304 m_posY = prior_layer_y + c.y / newScaleY;
2305
2306 // Recompute the desired Y view extents:
2308 }
2309
2310 AdjustLimitedView( directions );
2311
2312 if( zoomFactor < 1.0 )
2313 {
2314 // These additional checks are needed because AdjustLimitedView only adjusts the position
2315 // and not the scale.
2316 wxOrientation directionsNeedingRefitting = ViewNeedsRefitting( directions );
2317
2318 // If the view is still out-of-limits after AdjustLimitedView is called, perform a Fit
2319 // along the offending dimension(s).
2320 if( directionsNeedingRefitting != 0 )
2321 Fit( m_minX, m_maxX, m_minY, m_maxY, nullptr, nullptr, directionsNeedingRefitting );
2322 }
2323
2324 UpdateAll();
2325
2326 if( horizontally )
2328}
2329
2330
2331void mpWindow::RecomputeDesiredX( double& min, double& max )
2332{
2333 const int plotScreenWidth = m_scrX - m_marginLeft - m_marginRight;
2334 const double plotSpanX = plotScreenWidth / m_scaleX;
2335 const double desiredSpanX = plotSpanX / ( 2 * m_leftRightPlotGapFactor + 1 );
2336 const double xGap = desiredSpanX * m_leftRightPlotGapFactor;
2337 min = m_posX + ( m_marginLeft / m_scaleX ) + xGap;
2338 max = m_desiredXmin + desiredSpanX;
2339}
2340
2341
2342void mpWindow::RecomputeDesiredY( double& min, double& max )
2343{
2344 const int plotScreenHeight = m_scrY - m_marginTop - m_marginBottom;
2345 const double plotSpanY = plotScreenHeight / m_scaleY;
2346 const double desiredSpanY = plotSpanY / ( 2 * m_topBottomPlotGapFactor + 1 );
2347 const double yGap = desiredSpanY * m_topBottomPlotGapFactor;
2348 max = m_posY - ( m_marginTop / m_scaleY ) - yGap;
2349 min = m_desiredYmax - desiredSpanY;
2350}
2351
2352
2353wxOrientation mpWindow::ViewNeedsRefitting( wxOrientation directions ) const
2354{
2355 if( !m_enableLimitedView )
2356 return static_cast<wxOrientation>( 0 );
2357
2358 // Allow a gap between the extrema of the curve and the edges of the plot area. Not to be
2359 // confused with the left/right/top/bottom margins outside the plot area.
2360 const double xGap = fabs( m_maxX - m_minX ) * m_leftRightPlotGapFactor;
2361 const double yGap = fabs( m_maxY - m_minY ) * m_topBottomPlotGapFactor;
2362
2363 wxOrientation result = {};
2364
2365 if( ( directions & wxHORIZONTAL ) != 0 )
2366 {
2367 if( ( m_desiredXmax > m_maxX + xGap ) || ( m_desiredXmin < m_minX - xGap ) )
2368 result = static_cast<wxOrientation>( result | wxHORIZONTAL );
2369 }
2370
2371 if( ( directions & wxVERTICAL ) != 0 )
2372 {
2373 if( ( m_desiredYmax > m_maxY + yGap ) || ( m_desiredYmin < m_minY - yGap ) )
2374 result = static_cast<wxOrientation>( result | wxVERTICAL );
2375 }
2376
2377 return result;
2378}
2379
2380
2381void mpWindow::PerformMouseWheelAction( wxMouseEvent& event, MouseWheelAction action )
2382{
2383 const int change = event.GetWheelRotation();
2384 const double changeUnitsX = change / m_scaleX;
2385 const double changeUnitsY = change / m_scaleY;
2386 const wxPoint clickPt( event.GetX(), event.GetY() );
2387
2388 switch( action )
2389 {
2390 case MouseWheelAction::NONE: break;
2391
2393 SetXView( m_posX + changeUnitsX, m_desiredXmax + changeUnitsX,
2394 m_desiredXmin + changeUnitsX );
2395 UpdateAll();
2396 break;
2397
2399 SetXView( m_posX - changeUnitsX, m_desiredXmax - changeUnitsX,
2400 m_desiredXmin - changeUnitsX );
2401 UpdateAll();
2402 break;
2403
2405 SetYView( m_posY + changeUnitsY, m_desiredYmax + changeUnitsY, m_desiredYmin + changeUnitsY );
2406 UpdateAll();
2407 break;
2408
2410 if( event.GetWheelRotation() > 0 )
2411 ZoomIn( clickPt );
2412 else
2413 ZoomOut( clickPt );
2414
2415 break;
2416
2418 if( event.GetWheelRotation() > 0 )
2419 ZoomIn( clickPt, zoomIncrementalFactor, wxHORIZONTAL );
2420 else
2421 ZoomOut( clickPt, zoomIncrementalFactor, wxHORIZONTAL );
2422
2423 break;
2424
2426 if( event.GetWheelRotation() > 0 )
2427 ZoomIn( clickPt, zoomIncrementalFactor, wxVERTICAL );
2428 else
2429 ZoomOut( clickPt, zoomIncrementalFactor, wxVERTICAL );
2430
2431 break;
2432
2433 default:
2434 break;
2435 }
2436}
2437
2438
2440{
2441 m_minX = 0.0;
2442 m_maxX = 1.0;
2443 m_minY = 0.0;
2444 m_maxY = 1.0;
2445
2446 return true;
2447}
2448
2449
2451{
2452 UpdateBBox();
2453 Refresh( false );
2454}
2455
2456
2457void mpWindow::SetScaleX( double scaleX )
2458{
2459 if( scaleX != 0 )
2460 m_scaleX = scaleX;
2461
2462 UpdateAll();
2463}
2464
2465
2466// New methods implemented by Davide Rondini
2467
2468mpLayer* mpWindow::GetLayer( int position ) const
2469{
2470 if( ( position >= (int) m_layers.size() ) || position < 0 )
2471 return nullptr;
2472
2473 return m_layers[position];
2474}
2475
2476
2477const mpLayer* mpWindow::GetLayerByName( const wxString& name ) const
2478{
2479 for( const mpLayer* layer : m_layers )
2480 {
2481 if( !layer->GetName().Cmp( name ) )
2482 return layer;
2483 }
2484
2485 return nullptr; // Not found
2486}
2487
2488
2489void mpWindow::GetBoundingBox( double* bbox ) const
2490{
2491 bbox[0] = m_minX;
2492 bbox[1] = m_maxX;
2493 bbox[2] = m_minY;
2494 bbox[3] = m_maxY;
2495}
2496
2497
2498bool mpWindow::SaveScreenshot( wxImage& aImage, wxSize aImageSize, bool aFit )
2499{
2500 int sizeX, sizeY;
2501
2502 if( aImageSize == wxDefaultSize )
2503 {
2504 sizeX = m_scrX;
2505 sizeY = m_scrY;
2506 }
2507 else
2508 {
2509 sizeX = aImageSize.x;
2510 sizeY = aImageSize.y;
2511 SetScr( sizeX, sizeY );
2512 }
2513
2514 wxBitmap screenBuffer( sizeX, sizeY );
2515 wxMemoryDC screenDC;
2516 screenDC.SelectObject( screenBuffer );
2517 screenDC.SetPen( *wxWHITE_PEN );
2518 screenDC.SetTextForeground( m_fgColour );
2519 wxBrush brush( GetBackgroundColour() );
2520 screenDC.SetBrush( brush );
2521 screenDC.DrawRectangle( 0, 0, sizeX, sizeY );
2522
2523 if( aFit )
2524 Fit( m_minX, m_maxX, m_minY, m_maxY, &sizeX, &sizeY );
2525 else
2527
2528 // Draw all the layers:
2529 for( mpLayer* layer : m_layers )
2530 layer->Plot( screenDC, *this );
2531
2532 if( aImageSize != wxDefaultSize )
2533 {
2534 // Restore dimensions
2535 int bk_scrX = m_scrX;
2536 int bk_scrY = m_scrY;
2537 SetScr( bk_scrX, bk_scrY );
2538 Fit( m_desiredXmin, m_desiredXmax, m_desiredYmin, m_desiredYmax, &bk_scrX, &bk_scrY );
2539 UpdateAll();
2540 }
2541
2542 // Once drawing is complete, actually save screen shot
2543 aImage = screenBuffer.ConvertToImage();
2544
2545 return true;
2546}
2547
2548
2549void mpWindow::SetMargins( int top, int right, int bottom, int left )
2550{
2551 m_marginTop = top;
2553 m_marginBottom = bottom;
2555}
2556
2557
2559{
2560 for( mpLayer* layer : m_layers )
2561 {
2562 if( layer->IsInfo() )
2563 {
2564 mpInfoLayer* tmpLyr = static_cast<mpInfoLayer*>( layer );
2565
2566 if( tmpLyr->Inside( point ) )
2567 return tmpLyr;
2568 }
2569 }
2570
2571 return nullptr;
2572}
2573
2574
2575void mpWindow::SetLayerVisible( const wxString& name, bool viewable )
2576{
2577 if( mpLayer* lx = GetLayerByName( name ) )
2578 {
2579 lx->SetVisible( viewable );
2580 UpdateAll();
2581 }
2582}
2583
2584
2585bool mpWindow::IsLayerVisible( const wxString& name ) const
2586{
2587 if( const mpLayer* lx = GetLayerByName( name ) )
2588 return lx->IsVisible();
2589
2590 return false;
2591}
2592
2593
2594void mpWindow::SetLayerVisible( const unsigned int position, bool viewable )
2595{
2596 if( mpLayer* lx = GetLayer( position ) )
2597 {
2598 lx->SetVisible( viewable );
2599 UpdateAll();
2600 }
2601}
2602
2603
2604bool mpWindow::IsLayerVisible( unsigned int position ) const
2605{
2606 if( const mpLayer* lx = GetLayer( position ) )
2607 return lx->IsVisible();
2608
2609 return false;
2610}
2611
2612
2613void mpWindow::SetColourTheme( const wxColour& bgColour, const wxColour& drawColour,
2614 const wxColour& axesColour )
2615{
2616 SetBackgroundColour( bgColour );
2617 SetForegroundColour( drawColour );
2618 m_bgColour = bgColour;
2619 m_fgColour = drawColour;
2620 m_axColour = axesColour;
2621
2622 // Cycle between layers to set colours and properties to them
2623 for( mpLayer* layer : m_layers )
2624 {
2625 if( layer->GetLayerType() == mpLAYER_AXIS )
2626 {
2627 // Get the old pen to modify only colour, not style or width.
2628 wxPen axisPen = layer->GetPen();
2629 axisPen.SetColour( axesColour );
2630 layer->SetPen( axisPen );
2631 }
2632
2633 if( layer->GetLayerType() == mpLAYER_INFO )
2634 {
2635 // Get the old pen to modify only colour, not style or width.
2636 wxPen infoPen = layer->GetPen();
2637 infoPen.SetColour( drawColour );
2638 layer->SetPen( infoPen );
2639 }
2640 }
2641}
2642
2643
2644template <typename... Ts>
2646 wxWindow( std::forward<Ts>( windowArgs )... ),
2647 m_minX( 0.0 ),
2648 m_maxX( 0.0 ),
2649 m_minY( 0.0 ),
2650 m_maxY( 0.0 ),
2651 m_scaleX( 1.0 ),
2652 m_scaleY( 1.0 ),
2653 m_posX( 0.0 ),
2654 m_posY( 0.0 ),
2655 m_scrX( 64 ),
2656 m_scrY( 64 ),
2657 m_clickedX( 0 ),
2658 m_clickedY( 0 ),
2659 m_desiredXmin( 0.0 ),
2660 m_desiredXmax( 1.0 ),
2661 m_desiredYmin( 0.0 ),
2662 m_desiredYmax( 1.0 ),
2665 m_marginTop( 0 ),
2666 m_marginRight( 0 ),
2667 m_marginBottom( 0 ),
2668 m_marginLeft( 0 ),
2669 m_last_lx( 0 ),
2670 m_last_ly( 0 ),
2671 m_buff_bmp( nullptr ),
2672 m_enableDoubleBuffer( false ),
2674 m_enableLimitedView( false ),
2676 m_movingInfoLayer( nullptr ),
2677 m_zooming( false )
2678{}
2679
2680
2682{
2683 if( wxGraphicsContext* ctx = m_buff_dc.GetGraphicsContext() )
2684 {
2685 if( !ctx->SetInterpolationQuality( wxINTERPOLATION_BEST )
2686 || !ctx->SetInterpolationQuality( wxINTERPOLATION_GOOD ) )
2687 {
2688 ctx->SetInterpolationQuality( wxINTERPOLATION_FAST );
2689 }
2690
2691 ctx->SetAntialiasMode( wxANTIALIAS_DEFAULT );
2692 }
2693}
2694
2695
2696// -----------------------------------------------------------------------------
2697// mpFXYVector implementation - by Jose Luis Blanco (AGO-2007)
2698// -----------------------------------------------------------------------------
2699
2700IMPLEMENT_DYNAMIC_CLASS( mpFXYVector, mpFXY )
2701
2702
2703mpFXYVector::mpFXYVector( const wxString& name, int flags ) :
2704 mpFXY( name, flags )
2705{
2706 m_index = 0;
2707 m_sweepWindow = 0;
2708 m_minX = -1;
2709 m_maxX = 1;
2710 m_minY = -1;
2711 m_maxY = 1;
2713}
2714
2715
2716double mpScaleX::TransformToPlot( double x ) const
2717{
2718 return ( x + m_offset ) * m_scale;
2719}
2720
2721
2722double mpScaleX::TransformFromPlot( double xplot ) const
2723{
2724 return xplot / m_scale - m_offset;
2725}
2726
2727
2728double mpScaleY::TransformToPlot( double x ) const
2729{
2730 return ( x + m_offset ) * m_scale;
2731}
2732
2733
2734double mpScaleY::TransformFromPlot( double xplot ) const
2735{
2736 return xplot / m_scale - m_offset;
2737}
2738
2739
2740double mpScaleXLog::TransformToPlot( double x ) const
2741{
2742 double xlogmin = log10( m_minV );
2743 double xlogmax = log10( m_maxV );
2744
2745 return ( log10( x ) - xlogmin ) / ( xlogmax - xlogmin );
2746}
2747
2748
2749double mpScaleXLog::TransformFromPlot( double xplot ) const
2750{
2751 double xlogmin = log10( m_minV );
2752 double xlogmax = log10( m_maxV );
2753
2754 return pow( 10.0, xplot * ( xlogmax - xlogmin ) + xlogmin );
2755}
2756
2757
2759{
2760 m_index = 0;
2761 m_sweepWindow = std::numeric_limits<size_t>::max();
2762}
2763
2764
2765void mpFXYVector::SetSweepWindow( int aSweepIdx )
2766{
2767 m_index = aSweepIdx * m_sweepSize;
2768 m_sweepWindow = ( aSweepIdx + 1 ) * m_sweepSize;
2769}
2770
2771
2772bool mpFXYVector::GetNextXY( double& x, double& y )
2773{
2774 if( m_index >= m_xs.size() || m_index >= m_sweepWindow )
2775 {
2776 return false;
2777 }
2778 else
2779 {
2780 x = m_xs[m_index];
2781 y = m_ys[m_index++];
2782 return m_index <= m_xs.size() && m_index <= m_sweepWindow;
2783 }
2784}
2785
2786
2788{
2789 m_xs.clear();
2790 m_ys.clear();
2791}
2792
2793
2794void mpFXYVector::SetData( const std::vector<double>& xs, const std::vector<double>& ys )
2795{
2796 // Check if the data vectors are of the same size
2797 if( xs.size() != ys.size() )
2798 return;
2799
2800 // Copy the data:
2801 m_xs = xs;
2802 m_ys = ys;
2803
2804 // An axis with nothing finite to show is left NaN so UpdateScales() knows this layer cannot
2805 // constrain it
2806 const double unbounded = std::numeric_limits<double>::quiet_NaN();
2807
2808 if( !mpFiniteRange( m_xs, m_minX, m_maxX ) )
2809 m_minX = m_maxX = unbounded;
2810
2811 if( !mpFiniteRange( m_ys, m_minY, m_maxY ) )
2812 m_minY = m_maxY = unbounded;
2813}
2814
2815
2817{
2818 m_scaleX = scaleX;
2819 m_scaleY = scaleY;
2820
2821 UpdateScales();
2822}
2823
2824
2826{
2827 // Bounds are NaN when the layer holds nothing plottable on that axis; extending the range
2828 // with them would poison it for every other layer
2829 if( m_scaleX && std::isfinite( GetMinX() ) && std::isfinite( GetMaxX() ) )
2830 m_scaleX->ExtendDataRange( GetMinX(), GetMaxX() );
2831
2832 if( m_scaleY && std::isfinite( GetMinY() ) && std::isfinite( GetMaxY() ) )
2833 m_scaleY->ExtendDataRange( GetMinY(), GetMaxY() );
2834}
2835
2836
2837double mpFXY::s2x( double plotCoordX ) const
2838{
2839 return m_scaleX ? m_scaleX->TransformFromPlot( plotCoordX ) : plotCoordX;
2840}
2841
2842
2843double mpFXY::s2y( double plotCoordY ) const
2844{
2845 return m_scaleY ? m_scaleY->TransformFromPlot( plotCoordY ) : plotCoordY;
2846}
2847
2848
2849double mpFXY::x2s( double x ) const
2850{
2851 return m_scaleX ? m_scaleX->TransformToPlot( x ) : x;
2852}
2853
2854
2855double mpFXY::y2s( double y ) const
2856{
2857 return m_scaleY ? m_scaleY->TransformToPlot( y ) : y;
2858}
const char * name
A class providing graphs functionality for a 2D plot (either continuous or a set of points),...
Definition mathplot.h:1477
void SetSweepWindow(int aSweepIdx) override
bool GetNextXY(double &x, double &y) override
Get locus value for next N.
mpFXYVector(const wxString &name=wxEmptyString, int flags=mpALIGN_NE)
double m_maxY
Definition mathplot.h:1530
std::vector< double > m_ys
Definition mathplot.h:1523
double m_minX
Loaded at SetData.
Definition mathplot.h:1530
std::vector< double > m_xs
The internal copy of the set of data to draw.
Definition mathplot.h:1523
size_t m_sweepWindow
Definition mathplot.h:1526
virtual void SetData(const std::vector< double > &xs, const std::vector< double > &ys)
Changes the internal data: the set of points to draw.
double m_maxX
Definition mathplot.h:1530
void Clear()
Clears all the data, leaving the layer empty.
size_t m_sweepSize
Definition mathplot.h:1532
void Rewind() override
Rewind value enumeration with mpFXY::GetNextXY.
size_t m_index
Definition mathplot.h:1525
double m_minY
Definition mathplot.h:1530
Abstract base class providing plot and labeling functionality for a locus plot F:N->X,...
Definition mathplot.h:580
mpScaleBase * m_scaleX
Definition mathplot.h:625
wxCoord maxDrawY
Definition mathplot.h:623
double s2y(double plotCoordY) const
virtual void SetScale(mpScaleBase *scaleX, mpScaleBase *scaleY)
mpScaleBase * m_scaleY
Definition mathplot.h:625
wxCoord maxDrawX
Definition mathplot.h:623
virtual void Rewind()=0
Rewind value enumeration with mpFXY::GetNextXY.
void UpdateScales()
virtual void SetSweepWindow(int aSweepIdx)
Definition mathplot.h:591
virtual size_t GetCount() const =0
int m_flags
Definition mathplot.h:620
mpFXY(const wxString &name=wxEmptyString, int flags=mpALIGN_NE)
Definition mathplot.cpp:462
virtual void Plot(wxDC &dc, mpWindow &w) override
Layer plot handler.
Definition mathplot.cpp:486
double y2s(double y) const
void UpdateViewBoundary(wxCoord xnew, wxCoord ynew)
Update label positioning data.
Definition mathplot.cpp:475
double x2s(double x) const
virtual int GetSweepCount() const
Definition mathplot.h:601
wxCoord minDrawX
Definition mathplot.h:623
double s2x(double plotCoordX) const
wxCoord minDrawY
Definition mathplot.h:623
virtual bool GetNextXY(double &x, double &y)=0
Get locus value for next N.
Abstract base class providing plot and labeling functionality for functions F:X->Y.
Definition mathplot.h:514
virtual double GetY(double x) const =0
Get function value for argument.
mpFX(const wxString &name=wxEmptyString, int flags=mpALIGN_RIGHT)
Definition mathplot.cpp:326
virtual void Plot(wxDC &dc, mpWindow &w) override
Layer plot handler.
Definition mathplot.cpp:334
int m_flags
Definition mathplot.h:535
Abstract base class providing plot and labeling functionality for functions F:Y->X.
Definition mathplot.h:546
int m_flags
Definition mathplot.h:567
virtual double GetX(double y) const =0
Get function value for argument.
mpFY(const wxString &name=wxEmptyString, int flags=mpALIGN_TOP)
Definition mathplot.cpp:396
virtual void Plot(wxDC &dc, mpWindow &w) override
Layer plot handler.
Definition mathplot.cpp:404
Base class to create small rectangular info boxes mpInfoLayer is the base class to create a small rec...
Definition mathplot.h:364
virtual ~mpInfoLayer()
Destructor.
Definition mathplot.cpp:128
wxPoint m_reference
Definition mathplot.h:417
wxRect m_dim
Definition mathplot.h:415
mpInfoLayer()
Default constructor.
Definition mathplot.cpp:105
wxPoint GetPosition() const
Returns the position of the upper left corner of the box (in pixels)
Definition mathplot.cpp:197
virtual void Plot(wxDC &dc, mpWindow &w) override
Plot method.
Definition mathplot.cpp:159
virtual void UpdateReference()
Updates the rectangle reference point.
Definition mathplot.cpp:152
virtual bool Inside(const wxPoint &point) const
Checks whether a point is inside the info box rectangle.
Definition mathplot.cpp:133
virtual bool OnDoubleClick(const wxPoint &point, mpWindow &w)
Definition mathplot.cpp:139
wxBrush m_brush
Definition mathplot.h:418
virtual void Move(wxPoint delta)
Moves the layer rectangle of given pixel deltas.
Definition mathplot.cpp:145
wxSize GetSize() const
Returns the size of the box (in pixels)
Definition mathplot.cpp:203
Implements the legend to be added to the plot This layer allows you to add a legend to describe the p...
Definition mathplot.h:430
mpInfoLegend()
Default constructor.
Definition mathplot.cpp:209
virtual void Plot(wxDC &dc, mpWindow &w) override
Plot method.
Definition mathplot.cpp:226
~mpInfoLegend()
Default destructor.
Definition mathplot.cpp:221
const wxString & GetDisplayName() const
Definition mathplot.h:258
mpLayerType GetLayerType() const
Get layer type: a Layer can be of different types: plot lines, axis, info boxes, etc,...
Definition mathplot.h:320
bool IsVisible() const
Checks whether the layer is visible or not.
Definition mathplot.h:324
virtual double GetMinY() const
Get inclusive bottom border of bounding box.
Definition mathplot.h:203
bool m_continuous
Specify if the layer will be plotted as a continuous line or a set of points.
Definition mathplot.h:345
bool m_showName
States whether the name of the layer must be shown (default is true).
Definition mathplot.h:346
bool m_visible
Toggles layer visibility.
Definition mathplot.h:348
virtual void SetName(const wxString &name)
Set layer name.
Definition mathplot.h:306
const wxPen & GetPen() const
Get pen set for this layer.
Definition mathplot.h:287
mpLayerType m_type
Define layer type, which is assigned by constructor.
Definition mathplot.h:347
wxString m_name
Layer's name.
Definition mathplot.h:343
const wxFont & GetPlotFont() const
Get the font to draw this layer with, which is the default one when none was set.
Definition mathplot.h:276
void SetPen(const wxPen &pen)
Set layer pen.
Definition mathplot.h:316
wxPen m_pen
Layer's pen.
Definition mathplot.h:341
virtual double GetMaxX() const
Get inclusive right border of bounding box.
Definition mathplot.h:198
virtual double GetMinX() const
Get inclusive left border of bounding box.
Definition mathplot.h:193
virtual double GetMaxY() const
Get inclusive top border of bounding box.
Definition mathplot.h:208
Plot layer implementing a x-scale ruler.
Definition mathplot.h:652
bool m_axisLocked
Definition mathplot.h:802
double m_scale
Definition mathplot.h:795
virtual void recalculateTicks(wxDC &dc, mpWindow &w)
Definition mathplot.h:787
double m_offset
Definition mathplot.h:795
void GetDataRange(double &minV, double &maxV) const
Definition mathplot.h:679
std::vector< double > m_tickValues
Definition mathplot.h:792
bool m_rangeSet
Definition mathplot.h:801
virtual double TransformToPlot(double x) const
Definition mathplot.h:764
virtual void formatLabels()
Definition mathplot.h:789
int m_maxLabelHeight
Definition mathplot.h:805
double m_axisMin
Definition mathplot.h:803
double m_maxV
Definition mathplot.h:800
void computeLabelExtents(wxDC &dc, mpWindow &w)
Definition mathplot.cpp:883
int m_maxLabelWidth
Definition mathplot.h:806
std::vector< TICK_LABEL > m_tickLabels
Definition mathplot.h:793
double m_absVisibleMaxV
Definition mathplot.h:796
bool m_ticks
Definition mathplot.h:799
double m_minV
Definition mathplot.h:800
void updateTickLabels(wxDC &dc, mpWindow &w)
Definition mathplot.cpp:900
int m_nameFlags
Definition mathplot.h:798
virtual double TransformFromPlot(double xplot) const
Definition mathplot.h:765
double m_axisMax
Definition mathplot.h:804
virtual void getVisibleDataRange(mpWindow &w, double &minV, double &maxV) override
virtual void Plot(wxDC &dc, mpWindow &w) override
Plot given view of layer to the given device context.
mpScaleXBase(const wxString &name=wxT("X"), int flags=mpALIGN_CENTER, bool ticks=true, unsigned int type=mpX_NORMAL)
Full constructor.
mpScaleXLog(const wxString &name=wxT("log(X)"), int flags=mpALIGN_CENTER, bool ticks=true, unsigned int type=mpX_NORMAL)
Full constructor.
virtual double TransformFromPlot(double xplot) const override
virtual double TransformToPlot(double x) const override
void recalculateTicks(wxDC &dc, mpWindow &w) override
virtual double TransformToPlot(double x) const override
virtual double TransformFromPlot(double xplot) const override
virtual void recalculateTicks(wxDC &dc, mpWindow &w) override
Definition mathplot.cpp:795
mpScaleX(const wxString &name=wxT("X"), int flags=mpALIGN_CENTER, bool ticks=true, unsigned int type=mpX_NORMAL)
Full constructor.
Plot layer implementing a y-scale ruler.
Definition mathplot.h:889
int m_flags
Definition mathplot.h:923
mpScaleY * m_masterScale
Definition mathplot.h:922
virtual void getVisibleDataRange(mpWindow &w, double &minV, double &maxV) override
Definition mathplot.cpp:907
virtual double TransformFromPlot(double xplot) const override
void computeSlaveTicks(mpWindow &w)
Definition mathplot.cpp:920
virtual void Plot(wxDC &dc, mpWindow &w) override
Layer plot handler.
bool m_ticks
Definition mathplot.h:924
virtual void recalculateTicks(wxDC &dc, mpWindow &w) override
Definition mathplot.cpp:969
virtual double TransformToPlot(double x) const override
mpScaleY(const wxString &name=wxT("Y"), int flags=mpALIGN_CENTER, bool ticks=true)
Canvas for plotting mpLayer implementations.
Definition mathplot.h:970
double m_desiredYmin
Definition mathplot.h:1417
bool SaveScreenshot(wxImage &aImage, wxSize aImageSize=wxDefaultSize, bool aFit=false)
Draw the window on a wxBitmap, then save it to a file.
mpInfoLayer * m_movingInfoLayer
Definition mathplot.h:1435
void DelAllLayers(bool alsoDeleteObject, bool refreshDisplay=true)
Remove all layers from the plot.
bool m_zooming
Definition mathplot.h:1436
void SetColourTheme(const wxColour &bgColour, const wxColour &drawColour, const wxColour &axesColour)
Set Color theme.
virtual bool SetYView(double pos, double desiredMax, double desiredMin)
Applies new Y view coordinates depending on the settings.
void ZoomRect(wxPoint p0, wxPoint p1)
Zoom view fitting given coordinates to the window (p0 and p1 do not need to be in any specific order)
double m_maxY
Definition mathplot.h:1401
void onMouseLeftRelease(wxMouseEvent &event)
double m_posY
Definition mathplot.h:1405
int GetMarginLeft() const
Definition mathplot.h:1285
bool m_enableMouseNavigation
Definition mathplot.h:1430
int GetYScreen() const
Definition mathplot.h:1101
void RecomputeDesiredY(double &min, double &max)
wxOrientation ViewNeedsRefitting(wxOrientation directions) const
void OnPaint(wxPaintEvent &event)
void OnShowPopupMenu(wxMouseEvent &event)
double m_desiredXmax
Definition mathplot.h:1417
int m_last_lx
Definition mathplot.h:1426
void onMouseLeftDown(wxMouseEvent &event)
void onMouseWheel(wxMouseEvent &event)
static MouseWheelActionSet defaultMouseWheelActions()
void SetMargins(int top, int right, int bottom, int left)
Set window margins, creating a blank area where some kinds of layers cannot draw.
MouseWheelActionSet m_mouseWheelActions
Definition mathplot.h:1432
int m_marginLeft
Definition mathplot.h:1424
int m_marginTop
Definition mathplot.h:1424
double m_minY
Definition mathplot.h:1400
int GetScrX() const
Get current view's X dimension in device context units.
Definition mathplot.h:1091
int GetScrY() const
Get current view's Y dimension in device context units.
Definition mathplot.h:1100
double p2x(wxCoord pixelCoordX)
Converts mpWindow (screen) pixel coordinates into graph (floating point) coordinates,...
Definition mathplot.h:1145
double p2y(wxCoord pixelCoordY)
Converts mpWindow (screen) pixel coordinates into graph (floating point) coordinates,...
Definition mathplot.h:1150
wxMemoryDC m_buff_dc
Definition mathplot.h:1427
double m_maxX
Definition mathplot.h:1399
void initializeGraphicsContext()
int m_marginBottom
Definition mathplot.h:1424
void RecomputeDesiredX(double &min, double &max)
int m_clickedY
Definition mathplot.h:1409
wxCoord x2p(double x)
Converts graph (floating point) coordinates into mpWindow (screen) pixel coordinates,...
Definition mathplot.h:1155
wxColour m_bgColour
Definition mathplot.h:1394
MouseWheelAction
Enumerates the possible mouse wheel actions that can be performed on the plot.
Definition mathplot.h:976
double m_leftRightPlotGapFactor
Definition mathplot.h:1422
double m_posX
Definition mathplot.h:1404
wxPoint m_mouseLClick
Definition mathplot.h:1434
mpInfoLayer * IsInsideInfoLayer(wxPoint &point)
Check if a given point is inside the area of a mpInfoLayer and eventually returns its pointer.
void OnMouseMiddleDown(wxMouseEvent &event)
std::stack< std::array< double, 4 > > m_redoZoomStack
Definition mathplot.h:1439
void SetLayerVisible(const wxString &name, bool viewable)
Sets the visibility of a layer by its name.
int GetXScreen() const
Definition mathplot.h:1092
int GetMarginTop() const
Definition mathplot.h:1279
double m_scaleY
Definition mathplot.h:1403
void DoZoom(const wxPoint &centerPoint, double zoomFactor, wxOrientation directions)
int m_marginRight
Definition mathplot.h:1424
mpLayer * GetLayer(int position) const
void onZoomRedo(wxCommandEvent &event)
void ZoomIn(const wxPoint &centerPoint=wxDefaultPosition)
Zoom into current view and refresh display.
wxColour m_fgColour
Definition mathplot.h:1395
void ZoomOut(const wxPoint &centerPoint=wxDefaultPosition)
Zoom out current view and refresh display.
virtual void OnXViewChanged()
Called whenever the visible X range changes (pan, zoom, fit, ...).
Definition mathplot.h:1389
virtual bool UpdateBBox()
Recalculate global layer bounding box, and save it in m_minX,...
double m_minX
Definition mathplot.h:1398
wxRect m_zoomRect
Definition mathplot.h:1437
bool DelLayer(mpLayer *layer, bool alsoDeleteObject=false, bool refreshDisplay=true)
Remove a plot layer from the canvas.
void UpdateAll()
Refresh display.
wxLayerList m_layers
Definition mathplot.h:1392
void AdjustLimitedView(wxOrientation directions=wxBOTH)
Limits the zoomed or panned view to the area used by the plots.
wxCoord y2p(double y)
Converts graph (floating point) coordinates into mpWindow (screen) pixel coordinates,...
Definition mathplot.h:1160
wxMenu m_popmenu
Definition mathplot.h:1393
void OnCenter(wxCommandEvent &event)
virtual bool SetXView(double pos, double desiredMax, double desiredMin)
Applies new X view coordinates depending on the settings.
bool m_enableLimitedView
Definition mathplot.h:1431
int m_clickedX
Definition mathplot.h:1408
void SetScaleX(double scaleX)
Set current view's X scale and refresh display.
void onMagnify(wxMouseEvent &event)
wxBitmap * m_buff_bmp
Definition mathplot.h:1428
double m_topBottomPlotGapFactor
Definition mathplot.h:1421
void onZoomOut(wxCommandEvent &event)
wxPoint m_mouseMClick
Definition mathplot.h:1433
void pushZoomUndo(const std::array< double, 4 > &aZoom)
double m_desiredYmax
Definition mathplot.h:1417
std::stack< std::array< double, 4 > > m_undoZoomStack
Definition mathplot.h:1438
int GetMarginRight() const
Definition mathplot.h:1281
void GetBoundingBox(double *bbox) const
Returns the bounding box coordinates.
void SetScr(int scrX, int scrY)
Set current view's dimensions in device context units.
Definition mathplot.h:1140
int GetMarginBottom() const
Definition mathplot.h:1283
void PerformMouseWheelAction(wxMouseEvent &event, MouseWheelAction action)
wxColour m_axColour
Definition mathplot.h:1396
static double zoomIncrementalFactor
This value sets the zoom steps whenever the user clicks "Zoom in/out" or performs zoom with the mouse...
Definition mathplot.h:1259
void onMouseLeftDClick(wxMouseEvent &event)
double m_desiredXmin
These are updated in Fit, ZoomIn, ZoomOut, ZoomRect, SetXView, SetYView and may be different from the...
Definition mathplot.h:1417
double m_scaleX
Definition mathplot.h:1402
void ZoomRedo()
bool m_enableDoubleBuffer
Definition mathplot.h:1429
void ZoomUndo()
void OnFit(wxCommandEvent &event)
void OnSize(wxSizeEvent &event)
const mpLayer * GetLayerByName(const wxString &name) const
unsigned int CountAllLayers() const
Counts the number of plot layers, whether or not they have a bounding box.
Definition mathplot.h:1216
void SetPos(double posX, double posY)
Set current view's X and Y position and refresh display.
Definition mathplot.h:1133
void onZoomIn(wxCommandEvent &event)
double GetScaleY() const
Get current view's Y scale.
Definition mathplot.h:1071
int m_scrY
Definition mathplot.h:1407
double GetScaleX() const
Get current view's X scale.
Definition mathplot.h:1065
bool AddLayer(mpLayer *layer, bool refreshDisplay=true)
Add a plot layer to the canvas.
void Fit() override
Set view to fit global bounding box of all plot layers and refresh display.
void onZoomUndo(wxCommandEvent &event)
double GetPosY() const
Get current view's Y position.
Definition mathplot.h:1083
void onMouseMove(wxMouseEvent &event)
double GetPosX() const
Get current view's X position.
Definition mathplot.h:1077
int m_scrX
Definition mathplot.h:1406
bool IsLayerVisible(const wxString &name) const
Check whether a layer with given name is visible.
int m_last_ly
Definition mathplot.h:1426
#define _(s)
EVT_MENU(ID_COMPARE_PROJECT_BRANCHES, KICAD_MANAGER_FRAME::OnCompareProjectBranches) KICAD_MANAGER_FRAME
#define mpLEGEND_MARGIN
Definition mathplot.cpp:51
#define mpLEGEND_LINEWIDTH
Definition mathplot.cpp:52
bool mpFiniteRange(const std::vector< double > &aValues, double &aMin, double &aMax)
Find the smallest and largest finite value in aValues.
Definition mathplot.cpp:58
#define mpALIGN_BORDER_RIGHT
Aligns Y axis to right border.
Definition mathplot.h:493
bool WXDLLIMPEXP_MATHPLOT mpFiniteRange(const std::vector< double > &aValues, double &aMin, double &aMax)
Find the smallest and largest finite value in aValues.
Definition mathplot.cpp:58
@ mpLAYER_INFO
Definition mathplot.h:149
@ mpLAYER_UNDEF
Definition mathplot.h:146
@ mpLAYER_AXIS
Definition mathplot.h:147
@ mpLAYER_PLOT
Definition mathplot.h:148
#define mpALIGN_RIGHT
Aligns label to the right.
Definition mathplot.h:464
#define mpALIGN_NW
Aligns label to north-west.
Definition mathplot.h:497
#define mpALIGN_FAR_RIGHT
Aligns label to the right of mpALIGN_RIGHT.
Definition mathplot.h:478
#define mpALIGN_BORDER_TOP
Aligns X axis to top border.
Definition mathplot.h:476
#define mpALIGN_CENTER
Aligns label to the center.
Definition mathplot.h:466
#define mpALIGN_LEFT
Aligns label to the left.
Definition mathplot.h:468
#define mpALIGN_TOP
Aligns label to the top.
Definition mathplot.h:470
@ mpID_ZOOM_REDO
Definition mathplot.h:134
@ mpID_FIT
Definition mathplot.h:132
@ mpID_ZOOM_IN
Definition mathplot.h:135
@ mpID_CENTER
Definition mathplot.h:137
@ mpID_ZOOM_UNDO
Definition mathplot.h:133
@ mpID_ZOOM_OUT
Definition mathplot.h:136
#define mpALIGN_BORDER_LEFT
Aligns Y axis to left border.
Definition mathplot.h:491
#define mpALIGNMASK
Definition mathplot.h:462
#define mpALIGN_SE
Aligns label to south-east.
Definition mathplot.h:501
#define mpALIGN_NE
Aligns label to north-east.
Definition mathplot.h:495
#define mpALIGN_BOTTOM
Aligns label to the bottom.
Definition mathplot.h:472
#define mpALIGN_BORDER_BOTTOM
Aligns X axis to bottom border.
Definition mathplot.h:474
STL namespace.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
#define MAX_SCALE
const int scale
Contains the set of modified mouse wheel actions that can be performed on the plot.
Definition mathplot.h:991
MouseWheelAction horizontal
Definition mathplot.h:1001
MouseWheelAction verticalWithShift
Definition mathplot.h:999
MouseWheelAction verticalWithCtrl
Definition mathplot.h:998
MouseWheelAction verticalWithAlt
Definition mathplot.h:1000
MouseWheelAction verticalUnmodified
Definition mathplot.h:997
DATABASE_CONNECTION dc(odbcBackend(cs))
KIBIS top(path, &reporter)
static double sweep(float aStart, float aEnd)
wxString result
Test unit parsing edge cases and error handling.
int delta
static thread_pool * tp
static PIECE rect(double aLeft, double aTop, double aRight, double aBottom, bool aMixed=false)