1 #include "plot.h"
2 #include "settings.h"
3 #include <qwt_date.h>
4 #include <qwt_date_scale_draw.h>
5 #include <qwt_date_scale_engine.h>
6 #include <qwt_plot_panner.h>
7 #include <qwt_plot_magnifier.h>
8 #include <qwt_plot_grid.h>
9 #include <qwt_plot_layout.h>
10 
Plot(QWidget * parent)11 Plot::Plot( QWidget *parent ):
12     QwtPlot( parent )
13 {
14     setAutoFillBackground( true );
15     setPalette( Qt::darkGray );
16     setCanvasBackground( Qt::white );
17 
18     plotLayout()->setAlignCanvasToScales( true );
19 
20     initAxis( QwtPlot::yLeft, "Local Time", Qt::LocalTime );
21     initAxis( QwtPlot::yRight,
22         "Coordinated Universal Time ( UTC )", Qt::UTC );
23 
24     QwtPlotPanner *panner = new QwtPlotPanner( canvas() );
25     QwtPlotMagnifier *magnifier = new QwtPlotMagnifier( canvas() );
26 
27     for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
28     {
29         const bool on = axis == QwtPlot::yLeft ||
30             axis == QwtPlot::yRight;
31 
32         enableAxis( axis, on );
33         panner->setAxisEnabled( axis, on );
34         magnifier->setAxisEnabled( axis, on );
35     }
36 
37     QwtPlotGrid *grid = new QwtPlotGrid();
38     grid->setMajorPen( Qt::black, 0, Qt::SolidLine );
39     grid->setMinorPen( Qt::gray, 0 , Qt::SolidLine );
40     grid->enableX( false );
41     grid->enableXMin( false );
42     grid->enableY( true );
43     grid->enableYMin( true );
44 
45     grid->attach( this );
46 }
47 
initAxis(int axis,const QString & title,Qt::TimeSpec timeSpec)48 void Plot::initAxis( int axis,
49     const QString& title, Qt::TimeSpec timeSpec )
50 {
51     setAxisTitle( axis, title );
52 
53     QwtDateScaleDraw *scaleDraw = new QwtDateScaleDraw( timeSpec );
54     QwtDateScaleEngine *scaleEngine = new QwtDateScaleEngine( timeSpec );
55 
56 #if 0
57     if ( timeSpec == Qt::LocalTime )
58     {
59         scaleDraw->setTimeSpec( Qt::OffsetFromUTC );
60         scaleDraw->setUtcOffset( 10 );
61 
62         scaleEngine->setTimeSpec( Qt::OffsetFromUTC );
63         scaleEngine->setUtcOffset( 10 );
64     }
65 #endif
66     setAxisScaleDraw( axis, scaleDraw );
67     setAxisScaleEngine( axis, scaleEngine );
68 }
69 
applySettings(const Settings & settings)70 void Plot::applySettings( const Settings &settings )
71 {
72     applyAxisSettings( QwtPlot::yLeft, settings );
73     applyAxisSettings( QwtPlot::yRight, settings );
74 
75     replot();
76 }
77 
applyAxisSettings(int axis,const Settings & settings)78 void Plot::applyAxisSettings( int axis, const Settings &settings )
79 {
80     QwtDateScaleEngine *scaleEngine =
81         static_cast<QwtDateScaleEngine *>( axisScaleEngine( axis ) );
82 
83     scaleEngine->setMaxWeeks( settings.maxWeeks );
84     setAxisMaxMinor( axis, settings.maxMinorSteps );
85     setAxisMaxMajor( axis, settings.maxMajorSteps );
86 
87 
88     setAxisScale( axis, QwtDate::toDouble( settings.startDateTime ),
89         QwtDate::toDouble( settings.endDateTime ) );
90 }
91