spectrumwindow.cpp 3.57 KB
#include "spectrumwindow.h"
#include "ui_spectrumwindow.h"

#include "mainwindow.h"

SpectrumWindow::SpectrumWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SpectrumWindow)
{
    ui->setupUi(this);
	
	//install an event filter to detect user clicks
	ui->spectrumPlot->installEventFilter(this);

	//create a line for the current band
	bandline = new QCPItemStraightLine(ui->spectrumPlot);
	ui->spectrumPlot->addItem(bandline);
}

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];
	}

	//set the range of the y axis to the minimum and maximum spectrum values
	ui->spectrumPlot->yAxis->setRange(y_min, y_max);

	//add the graph to the plot
	ui->spectrumPlot->graph(0)->setData(x, y);

	//redraw the plot
	ui->spectrumPlot->replot();

	std::cout<<"Items in the plot: "<<ui->spectrumPlot->itemCount()<<std::endl;
	std::cout<<bandline->point1->coords().x()<<", "<<bandline->point1->coords().y()<<std::endl;
}

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);

	//add a graph to the plot
	ui->spectrumPlot->addGraph();

	//set the initial position of the band line
	double xmid = (x_max + x_min) / 2;
	std::cout<<"xmid: "<<xmid<<std::endl;
	bandline->point1->setCoords(xmid, 0);
	bandline->point2->setCoords(xmid, 1);
	bandline->setPen(QPen(Qt::red));


	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());

		//set the position of the band line
		bandline->point1->setCoords(x, 0);
		bandline->point2->setCoords(x, 1);
		std::cout<<"coords set to: "<<x<<std::endl;
    }
	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);

		//set the position of the band line
		bandline->point1->setCoords(x, 0);
		bandline->point2->setCoords(x, 1);
		std::cout<<"coords set to: "<<x<<std::endl;

		//replot the spectrum with the new band line position
		ui->spectrumPlot->replot();
    }

	

    return false;
}