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