/*
* Title: ControlDataWin.cpp
* Author: M.Thomas BEAM Ltd
* Date: 2007-02-13
*
* Contents: Control of data requests. Test code.
*
* Mod Rec:
*
*/
#include <ControlDataWin.h>
#include <qlayout.h>
#include <qvbox.h>
#include <TmsD.h>
#include <qvalidator.h>
#include <qmessagebox.h>
#include <BFile.h>
#include <qwt/qwt_scale.h>
#include <qhbox.h>
using namespace Tms;
using namespace std;
static ComboDefinition cycleDef[] = {
{ 0, "CyclePeriodStart", CyclePeriodStart, 1 },
{ 1, "CyclePeriodCalibration", CyclePeriodCalibration, 0 },
{ 2, "CyclePeriodHarmonic0", CyclePeriodHarmonic0, 0 },
{ 3, "CyclePeriodHarmonic1", CyclePeriodHarmonic1, 0 },
{ 4, "CyclePeriodHarmonic2", CyclePeriodHarmonic2, 0 },
{ 5, "CyclePeriodHarmonic3", CyclePeriodHarmonic3, 0 },
{ 6, "CyclePeriodHarmonic4", CyclePeriodHarmonic4, 0 },
{ 7, "CyclePeriodHarmonic5", CyclePeriodHarmonic5, 0 },
{ 8, "CyclePeriodHarmonic6", CyclePeriodHarmonic6, 0 },
{ 9, "CyclePeriodHarmonic7", CyclePeriodHarmonic7, 0 },
{ 0,0,0,0 }
};
static ComboDefinition functionDef[] = {
{ 0, "DataFunctionRaw", DataFunctionRaw, 1 },
{ 1, "DataFunctionMean0", DataFunctionMean0, 0 },
{ 2, "DataFunctionMean1", DataFunctionMean1, 0 },
{ 0,0,0,0 }
};
static ComboDefinition bunchDef[] = {
{ 0, "Bunch 0", 0, 1 },
{ 1, "Bunch 1", 1, 0 },
{ 2, "Bunch 1", 2, 0 },
{ 3, "Bunch 2", 3, 0 },
{ 0,0,0,0 }
};
ControlDataWin::ControlDataWin(QWidget* w,Control& c) : ocontrol(c) {
QPen pen;
int row = 0;
int col = 0;
QVBox* mv = new QVBox(this);
oplotSigma = new BQwtPlot(mv);
oplotDeltaX = new BQwtPlot(mv);
oplotDeltaY = new BQwtPlot(mv);
BQwtPlotControlBasic* controls = new BQwtPlotControlBasic(mv,oplotSigma);
QWidget* t = new QWidget(mv);
QGridLayout* v = new QGridLayout(t);
controls->addPlot(oplotDeltaX);
controls->addPlot(oplotDeltaY);
oticker = new QTimer(this);
QValidator* validator = new QIntValidator(0, (int)pow(2,31), this );
pen.setColor("blue");
ocurrentCycle = new QLabel("",t);
ocurrentCycle->setFixedWidth(200);
ocyclePeriod = new BQComboBox(cycleDef,t,"");
ofunction = new BQComboBox(functionDef,t,"");
obunchNumber = new BQComboBox(bunchDef,t,"");
ocycleNumber = new QLineEdit(t,"");ocycleNumber->setValidator(validator);
oautoCycleNumber = new QCheckBox(t,"");
ostartTime = new QLineEdit(t,"");ostartTime->setValidator( validator );
oorbitNumber = new QLineEdit(t,"");oorbitNumber->setValidator( validator );
oargument = new QLineEdit(t,"");oargument->setValidator( validator );
onumValues = new QLineEdit(t,"");onumValues->setValidator( validator );
ochannel = new QSpinBox(1,60,1,t,"");
ochannel->setWrapping(true);
onumValues->setText("2048");
oautoCycleNumber->setChecked(true);
ostartTime->setText("0");
oorbitNumber->setText("0");
oargument->setText("0");
int width = 200;
oautoCycleNumber->setMaximumWidth(width);
ocycleNumber->setMaximumWidth(width);
ochannel->setMaximumWidth(width);
ocyclePeriod->setMaximumWidth(width);
ostartTime->setMaximumWidth(width);
oorbitNumber->setMaximumWidth(width);
oargument->setMaximumWidth(width);
onumValues->setMaximumWidth(width);
obunchNumber->setMaximumWidth(width);
ofunction->setMaximumWidth(width);
col = 0;row = 0;
v->addWidget(new QLabel("Auto Cycle Num",t,""),row,col); v->addWidget(oautoCycleNumber,row,col+1);row++;
v->addWidget(new QLabel("Cycle Number",t,""),row,col); v->addMultiCellWidget(ocycleNumber,row,row,col+1,col+2); row++;
v->addWidget(new QLabel("Channel",t,""),row,col); v->addMultiCellWidget(ochannel,row,row,col+1,col+2); row++;
v->addWidget(new QLabel("Cycle Period",t,""),row,col); v->addMultiCellWidget(ocyclePeriod,row,row,col+1,col+2); row++;
v->addWidget(new QLabel("Start Time",t,""),row,col); v->addMultiCellWidget(ostartTime,row,row,col+1,col+2); row++;
v->addWidget(new QLabel("Orbit Number",t,""),row,col); v->addMultiCellWidget(oorbitNumber,row,row,col+1,col+2);row++;
col = 2;row = 0;
v->addWidget(ocurrentCycle,row,col);
col = 3;row = 0;
v->addWidget(new QLabel("Argument",t,""),row,col); v->addWidget(oargument,row,col+1); row++;
v->addWidget(new QLabel("Num Values",t,""),row,col); v->addWidget(onumValues,row,col+1); row++;
v->addWidget(new QLabel("Bunch Number",t,""),row,col); v->addWidget(obunchNumber,row,col+1); row++;
v->addWidget(new QLabel("Function",t,""),row,col); v->addWidget(ofunction,row,col+1); row++;
v->setSpacing(5);
QHBox* h = new QHBox(mv);
QPushButton* refreshData = new QPushButton("Refresh Data",h,"refresh");
QPushButton* saveFile = new QPushButton("Save To File",h,"save");
h->setMargin(4);
refreshData->setMaximumWidth(200);
saveFile->setMaximumWidth(200);
osigma.curve = oplotSigma->insertCurve("Sigma");
odeltaX.curve = oplotDeltaX->insertCurve("deltaX");
odeltaY.curve = oplotDeltaY->insertCurve("deltaY");
oplotSigma->setLegendPosition(QwtPlot::Right);
oplotDeltaX->setLegendPosition(QwtPlot::Right);
oplotDeltaY->setLegendPosition(QwtPlot::Right);
oplotSigma->setMinimumHeight(100);
oplotDeltaX->setMinimumHeight(150);
oplotDeltaY->setMinimumHeight(180);
oplotSigma->enableLegend(true,osigma.curve);
oplotDeltaX->enableLegend(true,odeltaX.curve);
oplotDeltaY->enableLegend(true,odeltaY.curve);
oplotSigma->enableXBottomAxis(0);
oplotDeltaX->enableXBottomAxis(0);
oplotDeltaY->enableXBottomAxis(1);
oplotSigma->setCurvePen(osigma.curve,pen);
oplotDeltaX->setCurvePen(odeltaX.curve,pen);
oplotDeltaY->setCurvePen(odeltaY.curve,pen);
QwtScale* sd;
sd = (QwtScale*)(oplotSigma->axis(QwtPlot::yLeft));
#ifdef ZAP // Autoscale
sd->setTitle("Testing");
sd->setPaletteBackgroundColor("Red");
oplotSigma->setAxisScale(QwtPlot::yLeft,-4000,10000);
oplotDeltaX->setAxisScale(QwtPlot::yLeft,-4000,10000);
oplotDeltaY->setAxisScale(QwtPlot::yLeft,-4000,10000);
#endif
connect(refreshData,SIGNAL(clicked()),this,SLOT(refresh()));
connect(saveFile,SIGNAL(clicked()),this,SLOT(saveFile()));
connect(oplotSigma,SIGNAL(plotEvent(QWheelEvent*)),this,SLOT(plotSyncEvents(QWheelEvent*)));
connect(oplotDeltaX,SIGNAL(plotEvent(QWheelEvent*)),this,SLOT(plotSyncEvents(QWheelEvent*)));
connect(oplotDeltaY,SIGNAL(plotEvent(QWheelEvent*)),this,SLOT(plotSyncEvents(QWheelEvent*)));
connect(oautoCycleNumber, SIGNAL(stateChanged(int)), this, SLOT(updateAutoCycle()));
connect(oticker, SIGNAL(timeout()), this, SLOT(updateStatus()));
}
ControlDataWin::~ControlDataWin() {}
void ControlDataWin::show() {
updateAutoCycle();
QWidget::show();
}
void ControlDataWin::hide() {
oticker->stop();
QWidget::hide();
}
void ControlDataWin::refresh() {
BError err;
QString s;
UInt32 cycleNumber;
if (oautoCycleNumber->isChecked()) {
BString s;
if(err = ocontrol.getCycleNumber(cycleNumber)){
return;
}
odataInfo.cycleNumber = cycleNumber + 2;
s.printf("%d",odataInfo.cycleNumber);
ocycleNumber->setText(s.retStr());
oautoCycleNumber->setChecked(false);
}
else {
odataInfo.cycleNumber = ocycleNumber->text().toUInt();
}
odataInfo.channel = ochannel->text().toUInt();
odataInfo.startTime = ostartTime->text().toUInt();
odataInfo.orbitNumber = oorbitNumber->text().toUInt();
odataInfo.argument = oargument->text().toUInt();
odataInfo.numValues = onumValues->text().toUInt();
odataInfo.bunchNumber = (int)obunchNumber->getValue();
odataInfo.cyclePeriod = (int)ocyclePeriod->getValue();
odataInfo.function = (int)ofunction->getValue();
printDataInfo();
if (err = ocontrol.getData(odataInfo,odata)) {
warningDialog("Reading Data",err);
return;
}
setPlotData();
plot();
}
void ControlDataWin::setPlotData() {
unsigned int n = 0;
ox.data.resize(odata.numValues);
osigma.data.resize(odata.numValues);
odeltaX.data.resize(odata.numValues);
odeltaY.data.resize(odata.numValues);
oplotSigma->setRange(odata.numValues);
oplotDeltaX->setRange(odata.numValues);
oplotDeltaY->setRange(odata.numValues);
for (n = 0;n < odata.numValues;n++) {
ox.data[n] = n;
osigma.data[n] = odata.dataValues[n].sigma;
odeltaX.data[n] = odata.dataValues[n].deltaX;
odeltaY.data[n] = odata.dataValues[n].deltaY;
}
oplotSigma->setCurveData(osigma.curve,ox.data.data(),osigma.data.data(),odata.numValues);
oplotDeltaX->setCurveData(odeltaX.curve,ox.data.data(),odeltaX.data.data(),odata.numValues);
oplotDeltaY->setCurveData(odeltaY.curve,ox.data.data(),odeltaY.data.data(),odata.numValues);
}
void ControlDataWin::plot() {
oplotSigma->replot();
oplotDeltaX->replot();
oplotDeltaY->replot();
}
void ControlDataWin::warningDialog(BString title, BError err){
BString m;
m = BString("<h5>") + title + "</h5><p>" + err.getString() + "</p>";
QMessageBox::warning(this, "Warning", m.retStr());
}
void ControlDataWin::plotSyncEvents(QWheelEvent* e) {
oplotSigma->syntheticWheelEvent(e);
oplotDeltaX->syntheticWheelEvent(e);
oplotDeltaY->syntheticWheelEvent(e);
}
void ControlDataWin::saveFile() {
unsigned int n;
BFile f;
BString fname = "data.dat";
if (f.open(fname,"w+")) {
printf("Cannot open save file (%s)\n",fname.retStr());
return;
}
f.printf("# tmsControlGui - Saved data file\n");
f.printf("# Runtime data\n");
f.printf("# Sigma,DeltaX,DeltaY\n");
f.printf("#\n");
for (n = 0;n < odata.dataValues.size();n++) {
f.printf("%d %d %d\n",
odata.dataValues[n].sigma,
odata.dataValues[n].deltaX,
odata.dataValues[n].deltaY);
}
f.close();
}
void ControlDataWin::updateAutoCycle() {
if (oautoCycleNumber->isChecked()) {
oticker->start( 1000, FALSE );
ocycleNumber->setEnabled(false);
}
else {
ocycleNumber->setEnabled(true);
oticker->stop();
}
updateStatus();
}
void ControlDataWin::updateStatus() {
BError err;
UInt32 cycleNumber;
BString s;
if (oautoCycleNumber->isChecked()) {
if(err = ocontrol.getCycleNumber(cycleNumber)){
return;
}
s.printf("Current Cycle = %d",cycleNumber);
}
ocurrentCycle->setText(s.retStr());
}
void ControlDataWin::printDataInfo() {
printf("Cycle Number\t(%d)\n",odataInfo.cycleNumber);
printf("Period\t\t(%d)\n",odataInfo.cyclePeriod);
printf("Start Time\t(%d)\n",odataInfo.startTime);
printf("Orbit Number\t(%d)\n",odataInfo.orbitNumber);
printf("Bunch Number\t(%d)\n",odataInfo.bunchNumber);
printf("Function\t(%d)\n",odataInfo.function);
printf("Argument\t(%d)\n",odataInfo.argument);
printf("Num Values\t(%d)\n",odataInfo.numValues);
}