plot.cpp
3.86 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
#include "plot.h"
#include <qwt_color_map.h>
#include <qwt_plot_spectrogram.h>
#include <qwt_plot_layout.h>
#include <qwt_matrix_raster_data.h>
#include <qwt_scale_widget.h>
#include <qwt_plot_magnifier.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_renderer.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_canvas.h>
#include <qfiledialog.h>
#include <qimagewriter.h>
class RasterData: public QwtMatrixRasterData
{
public:
RasterData()
{
const double matrix[] =
{
1, 2, 4, 1,
6, 3, 5, 2,
4, 2, 1, 5,
5, 4, 2, 3
};
QVector<double> values;
for ( uint i = 0; i < sizeof(matrix) / sizeof(double); i++ )
values += matrix[i];
const int numColumns = 4;
setValueMatrix(values, numColumns);
setInterval( Qt::XAxis,
QwtInterval( -0.5, 3.5, QwtInterval::ExcludeMaximum ) );
setInterval( Qt::YAxis,
QwtInterval( -0.5, 3.5, QwtInterval::ExcludeMaximum ) );
setInterval( Qt::ZAxis, QwtInterval(1.0, 6.0) );
}
};
class ColorMap: public QwtLinearColorMap
{
public:
ColorMap():
QwtLinearColorMap(Qt::darkBlue, Qt::darkRed)
{
addColorStop(0.2, Qt::blue);
addColorStop(0.4, Qt::cyan);
addColorStop(0.6, Qt::yellow);
addColorStop(0.8, Qt::red);
}
};
Plot::Plot(QWidget *parent):
QwtPlot(parent)
{
#if 0
QwtPlotGrid *grid = new QwtPlotGrid();
grid->setPen(QPen(Qt::DotLine));
grid->attach(this);
#endif
d_spectrogram = new QwtPlotSpectrogram();
d_spectrogram->setRenderThreadCount(0); // use system specific thread count
d_spectrogram->setColorMap( new ColorMap() );
d_spectrogram->setData(new RasterData());
d_spectrogram->attach(this);
const QwtInterval zInterval = d_spectrogram->data()->interval( Qt::ZAxis );
// A color bar on the right axis
QwtScaleWidget *rightAxis = axisWidget(QwtPlot::yRight);
rightAxis->setColorBarEnabled(true);
rightAxis->setColorBarWidth(40);
rightAxis->setColorMap(zInterval, new ColorMap() );
setAxisScale(QwtPlot::yRight, zInterval.minValue(), zInterval.maxValue() );
enableAxis(QwtPlot::yRight);
plotLayout()->setAlignCanvasToScales(true);
setAxisScale(QwtPlot::xBottom, 0.0, 3.0);
setAxisMaxMinor(QwtPlot::xBottom, 0);
setAxisScale(QwtPlot::yLeft, 0.0, 3.0);
setAxisMaxMinor(QwtPlot::yLeft, 0);
QwtPlotMagnifier *magnifier = new QwtPlotMagnifier( canvas() );
magnifier->setAxisEnabled( QwtPlot::yRight, false);
QwtPlotPanner *panner = new QwtPlotPanner( canvas() );
panner->setAxisEnabled( QwtPlot::yRight, false);
canvas()->setBorderRadius( 10 );
}
void Plot::exportPlot()
{
QString fileName = "rasterview.pdf";
#ifndef QT_NO_FILEDIALOG
const QList<QByteArray> imageFormats =
QImageWriter::supportedImageFormats();
QStringList filter;
filter += "PDF Documents (*.pdf)";
#ifndef QWT_NO_SVG
filter += "SVG Documents (*.svg)";
#endif
filter += "Postscript Documents (*.ps)";
if ( imageFormats.size() > 0 )
{
QString imageFilter("Images (");
for ( int i = 0; i < imageFormats.size(); i++ )
{
if ( i > 0 )
imageFilter += " ";
imageFilter += "*.";
imageFilter += imageFormats[i];
}
imageFilter += ")";
filter += imageFilter;
}
fileName = QFileDialog::getSaveFileName(
this, "Export File Name", fileName,
filter.join(";;"), NULL, QFileDialog::DontConfirmOverwrite);
#endif
if ( !fileName.isEmpty() )
{
QwtPlotRenderer renderer;
renderer.renderDocument(this, fileName, QSizeF(300, 200), 85);
}
}
void Plot::setResampleMode(int mode)
{
RasterData *data = (RasterData *)d_spectrogram->data();
data->setResampleMode( (QwtMatrixRasterData::ResampleMode) mode);
replot();
}