RSS Git Download  Clone
Raw Blame History
/*
* Title:	BQwtPlot.cpp
* Author:	M.Thomas BEAM Ltd
* Date:		2007-04-19
*
* Contents:	Enhanced plotting widget.
*
*
*/

#include <BQwtPlot.h>
#include <qwt/qwt_plot_canvas.h>
#include <qwt/qwt_plot.h>
#include <qlayout.h>
#include <limits.h>
#include <qhbox.h>


//#define DEBUG_PLOT	1;

BQwtPlot::BQwtPlot(QWidget *p, const char *name) : QwtPlot(p,name) {
	oxmin = 0;
	oxrange = 1024;
	oxscroll= oxrange/10;
	oyLeftMax = 4000;
	oyLeftMin = -4000;
	ocontrol = new BQwtPlotControl(this,this);
	connect(canvas(),SIGNAL(mouseReleased(const QMouseEvent&)),this,SLOT(mouseReleased(const QMouseEvent&)));
}

BQwtPlotControl* BQwtPlot::getPlotControls () {
	return ocontrol;
}

void BQwtPlot::getYLeftMaxMin(double& min,double& max) {
	min = oyLeftMin;
	max = oyLeftMax;
	return;
}

void BQwtPlot::getRange(int& max,int& actual) {
	max = oxrangeMax;
	actual = oxrange;
	return;
}

void BQwtPlot::setRange(int range) {
	oxrangeMax = range;
	oxrange = range;
	if ((oxscroll = oxrange/10) < 1)
		oxscroll = 1;	
}

void	BQwtPlot::zoomIn() {
	zoomX(-1,0);
}

void	BQwtPlot::zoomOut() {
	zoomX(1,0);
}


void BQwtPlot::wheelEvent ( QWheelEvent * e ) {
	emit	plotEvent(e);
	emit	plotEvent();
}

void BQwtPlot::mouseReleased(const QMouseEvent& e ) {
	if (e.button() == RightButton)
		ocontrol->show();
}

void BQwtPlot::syntheticWheelEvent ( QWheelEvent * e ) {
	int	delta = (e->delta() > 0) ? -1 : 1;
	int	zoomingX = (e->state() & Qt::ShiftButton) ? 1 : 0;
	int	zoomingY = (e->state() & Qt::ControlButton) ? 1 : 0;
	int	scrolling = (zoomingX || zoomingY) ? 0 : 1;

	QwtPlotCanvas*	c = this->canvas();
	if (zoomingX)
		zoomX(delta,(int)invTransform(xBottom,(int)(e->x() - c->x())));
	if (scrolling)
		scroll(delta);
}


void BQwtPlot::scroll(int delta) {
	if (oxrange < 100) 
		oxscroll = 1;
	else 
		oxscroll = oxrange/10;
	oxmin += oxscroll * delta;
	oxmax = oxmin + oxrange;

	if (oxmax > oxrangeMax) {
		oxmax = oxrangeMax;
		oxmin = oxrangeMax - oxrange;
	}			
	if (oxmin < 0) {
		oxmin = 0;
		oxmax = oxmin + oxrange;
	}
	setAxisScale(xBottom,oxmin,oxmax);
	replot();
}

void BQwtPlot::setPosition(int pos) {
	oxmin = pos;
	oxmax = oxmin + oxrange;

	if (oxmax > oxrangeMax) {
		oxmax = oxrangeMax;
		oxmin = oxrangeMax - oxrange;
	}			
	if (oxmin < 0) {
		oxmin = 0;
		oxmax = oxmin + oxrange;
	}
	setAxisScale(xBottom,oxmin,oxmax);
	replot();
}




void BQwtPlot::zoomX(int delta,int pos) {
	int	zoom = oxrange/2;

	oxrange += zoom * delta;
	if (oxrange < 30)
		oxrange = 30;
	if (oxrange > oxrangeMax)
		oxrange = oxrangeMax;

	oxmax = oxmin + oxrange;

	if (oxmax > oxrangeMax) {
		oxmax = oxrangeMax;
		oxmin = oxrangeMax - oxrange;
	}
	setAxisScale(xBottom,oxmin,oxmax);
	replot();
}

void BQwtPlot::setAxisScale(int axis,double mn,double mx) {
	oyLeftMax = mx;
	oyLeftMin = mn;
	QwtPlot::setAxisScale(axis,mn,mx);
}


BQwtPlotControlBasic::BQwtPlotControlBasic(QWidget *p,BQwtPlot* plot) : QWidget(p) {
	QHBoxLayout*	h = new QHBoxLayout(this);

	oplots.append(plot);

	h->setSpacing(5);
	h->setMargin(5);
	oscroll = new QScrollBar(Qt::Horizontal,this);
	ozoomIn = new QPushButton("Zoom In",this,"");
	ozoomOut = new QPushButton("Zoom Out",this,"");
	
	oscroll->setMaxValue(1000);
	oscroll->setMinValue(0);
	
	h->addWidget(oscroll);
	h->addWidget(ozoomIn);
	h->addWidget(ozoomOut);
	
	ozoomOut->setMaximumWidth(70);
	ozoomIn->setMaximumWidth(70);

	connect(oscroll,SIGNAL(valueChanged(int)),this,SLOT(scroll()));
	connect(ozoomOut,SIGNAL(clicked()),this,SLOT(zoomOut()));
	connect(ozoomIn,SIGNAL(clicked()),this,SLOT(zoomIn()));
}

void BQwtPlotControlBasic::addPlot(BQwtPlot* plot) {
	oplots.append(plot);
}


void BQwtPlotControlBasic::zoomIn() {
	int	max,actual;
	BIter	i;
	
	for (oplots.start(i);! oplots.isEnd(i);oplots.next(i)) { 
		oplots[i]->zoomIn();
	}	
	oplots[oplots.end()]->getRange(max,actual);
	oscroll->setMaxValue(max);
}

void BQwtPlotControlBasic::zoomOut() {
	int	max,actual;
	BIter	i;
	
	for (oplots.start(i);! oplots.isEnd(i);oplots.next(i)) { 
		oplots[i]->zoomOut();
	}	
	oplots[oplots.end()]->getRange(max,actual);
	oscroll->setMaxValue(max);
}

void BQwtPlotControlBasic::scroll() {
	BIter	i;
	
	for (oplots.start(i);! oplots.isEnd(i);oplots.next(i)) { 
		oplots[i]->setPosition(oscroll->value());
	}
}



BQwtPlotControl::BQwtPlotControl(QWidget *p,BQwtPlot* plot) : oplot(plot) {
	int	row;
	int	col;
	double	ymin;
	double	ymax;
	
	QGridLayout* 	v = new QGridLayout(this);
	QPushButton* 	close = new QPushButton("Close",this,"close");
	
	oscroll = new QScrollBar(Qt::Horizontal,this);
	ozoomIn = new QPushButton("Zoom In",this,"");
	ozoomOut = new QPushButton("Zoom Out",this,"");
	
	
	oymax = new QSpinBox(this);
	oymin = new QSpinBox(this);
	
	oymax->setMaxValue(INT_MAX);
	oymax->setMinValue(INT_MIN);
	oymax->setLineStep(1000);

	oymin->setMaxValue(INT_MAX);
	oymin->setMinValue(INT_MIN);
	oymin->setLineStep(1000);

	row = 0;
	col = 0;
	v->setSpacing(5);
	v->addWidget(new QLabel("Y Axis Max",this,""),row,col);   	v->addWidget(oymax,row,col+1);	row++;
	v->addWidget(new QLabel("Y Axis Min",this,""),row,col);   	v->addWidget(oymin,row,col+1);	row++;
	v->addWidget(ozoomIn,row,col);   		v->addWidget(ozoomOut,row,col+1);	row++;
	v->addMultiCellWidget(oscroll,row,row,col,col+1);	row++;
	v->addWidget(close,row,col+1);

	oplot->getYLeftMaxMin(ymin,ymax);
	oymax->setValue((int)ymax);
	oymin->setValue((int)ymin);

	connect(oymax,SIGNAL(valueChanged(int)),this,SLOT(update()));
	connect(oymin,SIGNAL(valueChanged(int)),this,SLOT(update()));
	connect(close,SIGNAL(released()),this,SLOT(close()));

	connect(oscroll,SIGNAL(valueChanged(int)),this,SLOT(scroll()));
	connect(ozoomOut,SIGNAL(clicked()),this,SLOT(zoomOut()));
	connect(ozoomIn,SIGNAL(clicked()),this,SLOT(zoomIn()));
}

void BQwtPlotControl::update() {
	double  mn = oymin->value();
	double	mx = oymax->value();
	oplot->setAxisScale(QwtPlot::yLeft,mn,mx);
	oplot->replot();
}

void BQwtPlotControl::zoomIn() {
	oplot->zoomIn();
}

void BQwtPlotControl::zoomOut() {
	oplot->zoomOut();
}

void BQwtPlotControl::scroll() {
	oplot->setPosition(oscroll->value());
}

void BQwtPlotControl::close() {
	this->hide();
}



CurveInfo::CurveInfo(BString ititle,BString icolour,BQwtPlot* iplot,double igain,double ioffset) {
	title = ititle;
	colour = icolour;
	plot = iplot;
	gain = igain;
	offset = ioffset;
	curve = 0;
}	

void CurveInfo::applyGainAndOffset() {
	int n;
	
	for (n =0;n< data.size();n++) {
		data[n] = (data[n] * gain) + offset; 
	}
}