4
#if QT_VERSION < 0x040000
5
#include <qwhatsthis.h>
7
#include <qwt_plot_layout.h>
8
#include <qwt_plot_canvas.h>
9
#include <qwt_plot_grid.h>
10
#include <qwt_plot_curve.h>
11
#include <qwt_symbol.h>
12
#include <qwt_scale_widget.h>
13
#include <qwt_wheel.h>
16
Plot::Plot(QWidget *parent):
19
setTitle("Interactive Plot");
21
setCanvasColor(Qt::darkCyan);
23
QwtPlotGrid *grid = new QwtPlotGrid;
24
grid->setMajPen(QPen(Qt::white, 0, Qt::DotLine));
29
setAxisScale(QwtPlot::xBottom, 0.0, 100.0);
30
setAxisScale(QwtPlot::yLeft, 0.0, 100.0);
32
// Avoid jumping when label with 3 digits
33
// appear/disappear when scrolling vertically
35
QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
36
sd->setMinimumExtent( sd->extent(QPen(),
37
axisWidget(QwtPlot::yLeft)->font()));
39
plotLayout()->setAlignCanvasToScales(true);
41
insertCurve(Qt::Vertical, Qt::blue, 30.0);
42
insertCurve(Qt::Vertical, Qt::magenta, 70.0);
43
insertCurve(Qt::Horizontal, Qt::yellow, 30.0);
44
insertCurve(Qt::Horizontal, Qt::white, 70.0);
48
// ------------------------------------
49
// We add a color bar to the left axis
50
// ------------------------------------
52
QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(yLeft);
53
scaleWidget->setMargin(10); // area for the color bar
54
d_colorBar = new ColorBar(Qt::Vertical, scaleWidget);
55
d_colorBar->setRange(Qt::red, Qt::darkBlue);
56
#if QT_VERSION >= 0x040000
57
d_colorBar->setFocusPolicy( Qt::TabFocus );
59
d_colorBar->setFocusPolicy( QWidget::TabFocus );
62
connect(d_colorBar, SIGNAL(selected(const QColor &)),
63
SLOT(setCanvasColor(const QColor &)));
65
// we need the resize events, to lay out the color bar
66
scaleWidget->installEventFilter(this);
68
// ------------------------------------
69
// We add a wheel to the canvas
70
// ------------------------------------
72
d_wheel = new QwtWheel(canvas());
73
d_wheel->setOrientation(Qt::Vertical);
74
d_wheel->setRange(-100, 100);
75
d_wheel->setValue(0.0);
76
d_wheel->setMass(0.2);
77
d_wheel->setTotalAngle(4 * 360.0);
79
connect(d_wheel, SIGNAL(valueChanged(double)),
80
SLOT(scrollLeftAxis(double)));
82
// we need the resize events, to lay out the wheel
83
canvas()->installEventFilter(this);
85
#if QT_VERSION < 0x040000
86
QWhatsThis::add(d_colorBar,
87
"Selecting a color will change the background of the plot.");
88
QWhatsThis::add(scaleWidget,
89
"Selecting a value at the scale will insert a new curve.");
90
QWhatsThis::add(d_wheel,
91
"With the wheel you can move the visible area.");
92
QWhatsThis::add(axisWidget(xBottom),
93
"Selecting a value at the scale will insert a new curve.");
95
d_colorBar->setWhatsThis(
96
"Selecting a color will change the background of the plot.");
97
scaleWidget->setWhatsThis(
98
"Selecting a value at the scale will insert a new curve.");
99
d_wheel->setWhatsThis(
100
"With the wheel you can move the visible area.");
101
axisWidget(xBottom)->setWhatsThis(
102
"Selecting a value at the scale will insert a new curve.");
107
void Plot::setCanvasColor(const QColor &c)
109
setCanvasBackground(c);
113
void Plot::scrollLeftAxis(double value)
115
setAxisScale(yLeft, value, value + 100.0);
119
bool Plot::eventFilter(QObject *object, QEvent *e)
121
if ( e->type() == QEvent::Resize )
123
const QSize &size = ((QResizeEvent *)e)->size();
124
if ( object == (QObject *)axisWidget(yLeft) )
126
const QwtScaleWidget *scaleWidget = axisWidget(yLeft);
128
const int margin = 2;
130
// adjust the color bar to the scale backbone
131
const int x = size.width() - scaleWidget->margin() + margin;
132
const int w = scaleWidget->margin() - 2 * margin;
133
const int y = scaleWidget->startBorderDist();
134
const int h = size.height() -
135
scaleWidget->startBorderDist() - scaleWidget->endBorderDist();
137
d_colorBar->setGeometry(x, y, w, h);
139
if ( object == canvas() )
143
const int margin = 2;
145
const QRect cr = canvas()->contentsRect();
146
d_wheel->setGeometry(
147
cr.right() - margin - w, cr.center().y() - h / 2, w, h);
151
return QwtPlot::eventFilter(object, e);
154
void Plot::insertCurve(int axis, double base)
157
if ( axis == yLeft || axis == yRight )
162
QRgb rgb = (uint)rand();
163
insertCurve(o, QColor(rgb), base);
167
void Plot::insertCurve(Qt::Orientation o,
168
const QColor &c, double base)
170
QwtPlotCurve *curve = new QwtPlotCurve();
173
curve->setSymbol(QwtSymbol(QwtSymbol::Ellipse,
174
Qt::gray, c, QSize(8, 8)));
177
double y[sizeof(x) / sizeof(x[0])];
179
for ( uint i = 0; i < sizeof(x) / sizeof(x[0]); i++ )
181
double v = 5.0 + i * 10.0;
182
if ( o == Qt::Horizontal )
194
curve->setData(x, y, sizeof(x) / sizeof(x[0]));