spectrumwindow.cpp
2.77 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
#include "spectrumwindow.h"
#include "ui_spectrumwindow.h"
#include "mainwindow.h"
SpectrumWindow::SpectrumWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::SpectrumWindow)
{
ui->setupUi(this);
ui->spectrumPlot->installEventFilter(this);
}
SpectrumWindow::~SpectrumWindow()
{
delete ui;
}
void SpectrumWindow::plotSpectrum(QVector<double> x, QVector<double> y){
//find the maximum and minimum values
double y_min = y[0];
double y_max = y[0];
for(int i=1; i<y.size(); i++){
if(y[i] < y_min) y_min = y[i];
if(y[i] > y_max) y_max = y[i];
}
ui->spectrumPlot->yAxis->setRange(y_min, y_max);
ui->spectrumPlot->addGraph();
ui->spectrumPlot->graph(0)->setData(x, y);
ui->spectrumPlot->replot();
}
void SpectrumWindow::initPlot(double x_min, double x_max, double y_min, double y_max, QString x_title, QString y_title){
// give the axes some labels:
ui->spectrumPlot->xAxis->setLabel(x_title);
ui->spectrumPlot->yAxis->setLabel(y_title);
// set axes ranges, so we see all data:
ui->spectrumPlot->xAxis->setRange(x_min, x_max);
ui->spectrumPlot->yAxis->setRange(y_min, y_max);
ui->spectrumPlot->replot();
}
/// Update the image viewing widget so that it always fills the main window
void SpectrumWindow::resizeEvent(QResizeEvent* event){
//get the current window size
QSize winsize = event->size();
//match the plot to the window
ui->spectrumPlot->setFixedSize(winsize.width(), winsize.height());
}
bool SpectrumWindow::eventFilter(QObject *target, QEvent *event)
{
if(target == ui->spectrumPlot && event->type() == QEvent::MouseMove)
{
QMouseEvent *_mouseEvent = static_cast<QMouseEvent*>(event);
//get the window position of the click
QPoint pixel = _mouseEvent->pos();
//calculate the position of the click in the image
double x = ui->spectrumPlot->xAxis->pixelToCoord(pixel.x());
double y = ui->spectrumPlot->yAxis->pixelToCoord(pixel.y());
}
else if(target == ui->spectrumPlot && event->type() == QEvent::MouseButtonPress)
{
QMouseEvent *_mouseEvent = static_cast<QMouseEvent*>(event);
//get the window position of the click
QPoint pixel = _mouseEvent->pos();
//calculate the position of the click in the image
double x = ui->spectrumPlot->xAxis->pixelToCoord(pixel.x());
double y = ui->spectrumPlot->yAxis->pixelToCoord(pixel.y());
//tell the parent MainWindow that a point on the spectrum was selected
((MainWindow*)parent())->mousePressSpectrum(x, y);
}
return false;
}
/// Update everything associated with when the image is clicked
void SpectrumWindow::mousePressEvent(QMouseEvent * event){
//get the window position of the click
QPoint winp = event->pos();
//calculate the position of the click in the image
QPoint p = winp;
//output the image position
std::cout<<p.x()<<" "<<p.y()<<std::endl;
}