1 #include <cstdlib>
2 #include <qgroupbox.h>
3 #include <qcombobox.h>
4 #include <qlayout.h>
5 #include <qstatusbar.h>
6 #include <qlabel.h>
7 #include <qwt_plot.h>
8 #include <qwt_plot_rescaler.h>
9 #include <qwt_scale_div.h>
10 #include "plot.h"
11 #include "mainwindow.h"
12 
MainWindow()13 MainWindow::MainWindow()
14 {
15     QFrame *w = new QFrame( this );
16 
17     QWidget *panel = createPanel( w );
18     panel->setFixedWidth( 2 * panel->sizeHint().width() );
19     d_plot = createPlot( w );
20 
21     QHBoxLayout *layout = new QHBoxLayout( w );
22     layout->setMargin( 0 );
23     layout->addWidget( panel, 0 );
24     layout->addWidget( d_plot, 10 );
25 
26     setCentralWidget( w );
27 
28     setRescaleMode( 0 );
29 
30     ( void )statusBar();
31 }
32 
createPanel(QWidget * parent)33 QWidget *MainWindow::createPanel( QWidget *parent )
34 {
35     QGroupBox *panel = new QGroupBox( "Navigation Panel", parent );
36 
37     QComboBox *rescaleBox = new QComboBox( panel );
38     rescaleBox->setEditable( false );
39     rescaleBox->insertItem( KeepScales, "None" );
40     rescaleBox->insertItem( Fixed, "Fixed" );
41     rescaleBox->insertItem( Expanding, "Expanding" );
42     rescaleBox->insertItem( Fitting, "Fitting" );
43 
44     connect( rescaleBox, SIGNAL( activated( int ) ), SLOT( setRescaleMode( int ) ) );
45 
46     d_rescaleInfo = new QLabel( panel );
47     d_rescaleInfo->setSizePolicy(
48         QSizePolicy::Expanding, QSizePolicy::Expanding );
49     d_rescaleInfo->setWordWrap( true );
50 
51     QVBoxLayout *layout = new QVBoxLayout( panel );
52     layout->addWidget( rescaleBox );
53     layout->addWidget( d_rescaleInfo );
54     layout->addStretch( 10 );
55 
56     return panel;
57 }
58 
createPlot(QWidget * parent)59 Plot *MainWindow::createPlot( QWidget *parent )
60 {
61     Plot *plot = new Plot( parent, QwtInterval( 0.0, 1000.0 ) );
62     plot->replot();
63 
64     d_rescaler = new QwtPlotRescaler( plot->canvas() );
65     d_rescaler->setReferenceAxis( QwtPlot::xBottom );
66     d_rescaler->setAspectRatio( QwtPlot::yLeft, 1.0 );
67     d_rescaler->setAspectRatio( QwtPlot::yRight, 0.0 );
68     d_rescaler->setAspectRatio( QwtPlot::xTop, 0.0 );
69 
70     for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
71         d_rescaler->setIntervalHint( axis, QwtInterval( 0.0, 1000.0 ) );
72 
73     connect( plot, SIGNAL( resized( double, double ) ),
74         SLOT( showRatio( double, double ) ) );
75     return plot;
76 }
77 
setRescaleMode(int mode)78 void MainWindow::setRescaleMode( int mode )
79 {
80     bool doEnable = true;
81     QString info;
82     QRectF rectOfInterest;
83     QwtPlotRescaler::ExpandingDirection direction = QwtPlotRescaler::ExpandUp;
84 
85     switch( mode )
86     {
87         case KeepScales:
88         {
89             doEnable = false;
90             info = "All scales remain unchanged, when the plot is resized";
91             break;
92         }
93         case Fixed:
94         {
95             d_rescaler->setRescalePolicy( QwtPlotRescaler::Fixed );
96             info = "The scale of the bottom axis remains unchanged, "
97                 "when the plot is resized. All other scales are changed, "
98                 "so that a pixel on screen means the same distance for"
99                 "all scales.";
100             break;
101         }
102         case Expanding:
103         {
104             d_rescaler->setRescalePolicy( QwtPlotRescaler::Expanding );
105             info = "The scales of all axis are shrinked/expanded, when "
106                 "resizing the plot, keeping the distance that is represented "
107                 "by one pixel.";
108             d_rescaleInfo->setText( "Expanding" );
109             break;
110         }
111         case Fitting:
112         {
113             d_rescaler->setRescalePolicy( QwtPlotRescaler::Fitting );
114             const QwtInterval xIntv =
115                 d_rescaler->intervalHint( QwtPlot::xBottom );
116             const QwtInterval yIntv =
117                 d_rescaler->intervalHint( QwtPlot::yLeft );
118 
119             rectOfInterest = QRectF( xIntv.minValue(), yIntv.minValue(),
120                 xIntv.width(), yIntv.width() );
121             direction = QwtPlotRescaler::ExpandBoth;
122 
123             info = "Fitting";
124             break;
125         }
126     }
127 
128     d_plot->setRectOfInterest( rectOfInterest );
129 
130     d_rescaleInfo->setText( info );
131     d_rescaler->setEnabled( doEnable );
132     for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
133         d_rescaler->setExpandingDirection( direction );
134 
135     if ( doEnable )
136         d_rescaler->rescale();
137     else
138         d_plot->replot();
139 }
140 
showRatio(double xRatio,double yRatio)141 void MainWindow::showRatio( double xRatio, double yRatio )
142 {
143     const QString msg = QString( "%1, %2" ).arg( xRatio ).arg( yRatio );
144     statusBar()->showMessage( msg );
145 }
146 
147