Debian Codegraph @

qwt/playground/rescaler/MainWindow.cpp

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