/*
* Title: PhaseTableWin.cpp
* Author: M.Thomas BEAM Ltd
* Date: 2007-02-13
*
* Contents: Status Display
*
* Mod Rec:
*
*/
#include <PhaseTableWin.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qvgroupbox.h>
#include <qmessagebox.h>
#include <qlabel.h>
#include <qfiledialog.h>
#include <qcheckbox.h>
#include <BFile.h>
#include <qgroupbox.h>
#include <qvbox.h>
#include <qlistbox.h>
#include <qvalidator.h>
#include <qtable.h>
#include <Globals.h>
#include <math.h>
#include <TmsD.h>
#include <TmsC.h>
#include <TmsS.h>
#include <TmsLib.h>
using namespace Tms;
PhaseTableWin::PhaseTableWin(QWidget* parent,Control& c) : ocontrol(c) {
QGridLayout* grid;
BString s;
int row = 0;
int col = 0;
oparams.cycleType = "";
oparams.info = "";
oparams.pllInitialFrequency = 0;
oparams.pllInitialFrequencyDelay = 0;
oparams.pllFrefGain = 0;
oparams.pllGain = 0;
oparams.channel = 0;
QValidator* validator = new QIntValidator(-(int)pow(2,31), (int)pow(2,31), this );
grid = new QGridLayout(this,5,3);
grid->setMargin(20);
grid->setSpacing(20);
s = "<p><b>Cycle Parameters<b></p>";
s += "<p>Select Cycle parameters file to be loaded</p>";
QWidget* w;
QGridLayout* v;
QVGroupBox * loadParams = new QVGroupBox("Load Cycle Parameters",this,"loadParams");
w = new QWidget(loadParams,"loadParamsW");
v = new QGridLayout(w);
ocycleTypes = new QListBox(w,"cycleList");
oloadFromTms = new QPushButton("Load from TMS",w);
ofilename = new QLineEdit(w,"S");
oselectFile = new QPushButton("Select",w);
oloadFromFile = new QPushButton("Load from File",w);
v->setSpacing(10);
row = 0; col = 0;
v->addWidget(new QLabel("Load from TMS",w),row,col);
v->addMultiCellWidget(ocycleTypes,row,row + 8,col + 1,col +1);
v->addWidget(oloadFromTms,row,col + 3);
row = 9;col = 0;
v->addWidget(new QLabel("Load from File",w),row,0);
v->addWidget(ofilename,row,1);
v->addWidget(oselectFile,row,2);
v->addWidget(oloadFromFile,row,3);
QVGroupBox * baseParams = new QVGroupBox("Base Parameters",this,"baseParams");
w = new QWidget(baseParams,"baseParamsW");
v = new QGridLayout(w);
ocycleType = new QLineEdit(w,"");
oinfo = new QLineEdit(w,"");
opllInitialFrequency = new QLineEdit(w,"");opllInitialFrequency->setValidator(validator);
opllInitialFrequencyDelay = new QLineEdit(w,"");opllInitialFrequencyDelay->setValidator(validator);
opllFrefGain = new QLineEdit(w,"");opllFrefGain->setValidator(validator);
opllGain = new QLineEdit(w,"");opllGain->setValidator(validator);
v->addWidget(new QLabel("Cycle Type",w,""),row,0); v->addWidget(ocycleType,row,1); row++;
v->addWidget(new QLabel("Info",w,""),row,0); v->addWidget(oinfo,row,1); row++;
v->addWidget(new QLabel("Pll Initial Frequency",w,""),row,0); v->addWidget(opllInitialFrequency,row,1); row++;
v->addWidget(new QLabel("Pll Initial Frequency Delay",w,""),row,0); v->addWidget(opllInitialFrequencyDelay,row,1); row++;
v->addWidget(new QLabel("Pll Fref Gain",w,""),row,0); v->addWidget(opllFrefGain,row,1); row++;
v->addWidget(new QLabel("Pll Gain",w,""),row,0); v->addWidget(opllGain,row,1); row++;
QVGroupBox * positions = new QVGroupBox("Positions",this,"positions");
w = new QWidget(positions,"positionsW");
v = new QGridLayout(w);
v->setSpacing(10);
row = 0;
ophaseDelays = new QTable(40,1,w,"");
ooffset = new QSpinBox(w,"");
oaddOffsets = new QPushButton("Add Offset to all channels",w);
oloadDefaults = new QPushButton("Load Defaults",w,"");
ophaseDelays->horizontalHeader()->setLabel( 0, "Phase Offset" );
ophaseDelays->setColumnStretchable (0,true);
ooffset->setMaxValue(512);
ooffset->setMinValue(-512);
ooffset->setWrapping(true);
v->addWidget(new QLabel("Positions",w,""),row,0); v->addWidget(ophaseDelays,row,1); row++;
v->addWidget(new QLabel("Offset",w,""),row,0); v->addWidget(ooffset,row,1); v->addWidget(oaddOffsets,row,2); row++;
v->addWidget(oloadDefaults,row,0);
QHBox * controls = new QHBox(this,"controls");
oupload = new QPushButton("Upload",controls);
osaveFile = new QPushButton("Save to file",controls);
oupload->setMaximumWidth(200);
osaveFile->setMaximumWidth(200);
row = 0;
grid->addWidget(loadParams,row,0); row++;
grid->addWidget(baseParams,row,0); row++;
grid->addWidget(positions,row,0); row++;
grid->addWidget(controls,row,0); row++;
connect(oloadFromFile,SIGNAL(clicked()),this,SLOT(loadFromFile()));
connect(oloadFromTms,SIGNAL(clicked()),this,SLOT(loadFromTms()));
connect(oselectFile,SIGNAL(clicked()),this,SLOT(selectFile()));
connect(oaddOffsets,SIGNAL(clicked()),this,SLOT(addOffsets()));
connect(oloadDefaults,SIGNAL(clicked()),this,SLOT(loadDefaults()));
connect(osaveFile,SIGNAL(clicked()),this,SLOT(saveFile()));
connect(oupload,SIGNAL(clicked()),this,SLOT(upload()));
connect(&ocontrol,SIGNAL(newConnection()),this,SLOT(update()));
}
PhaseTableWin::~PhaseTableWin() {}
void PhaseTableWin::show() {
QWidget::show();
update();
}
void PhaseTableWin::update() {
if (! isVisible())
return;
updateControlList();
updateCycleInfo();
}
BError PhaseTableWin::updateControlList() {
BError err;
BIter i;
BList<CycleParamItem> l;
if (err = ocontrol.getControlList(l)) {
return err;
}
ocycleTypes->clear();
for (l.start(i);! l.isEnd(i);l.next(i)) {
ocycleTypes->insertItem(l[i].cycleType.retStr());
}
return err;
}
void PhaseTableWin::updateCycleInfo() {
ocycleType->setText(oparams.cycleType.retStr());
oinfo->setText(oparams.info.retStr());
opllInitialFrequency->setText(uIntToStr(oparams.pllInitialFrequency).retStr());
opllInitialFrequencyDelay->setText(uIntToStr(oparams.pllInitialFrequencyDelay).retStr());
opllFrefGain->setText(uIntToStr(oparams.pllFrefGain).retStr());
opllGain->setText(uIntToStr(oparams.pllGain).retStr());
for(unsigned int i = 0; i < oparams.frefPhaseDelay.size(); i++ ) {
BString s;
s.printf("%d",oparams.frefPhaseDelay[i]);
ophaseDelays->setText(i,0,s.retStr());
}
}
void PhaseTableWin::loadFromFile() {
BError err;
BFile f;
BString fname;
TmsCycleParams cycleParamFile("");
fname = ofilename->text().ascii();
if (err = f.open(fname,"r")) {
warningDialog("Cycle Parameters - Reading",err);
return;
}
if(err = cycleParamFile.getCycleParams(fname,oparams)) {
warningDialog("Cycle Parameters - Read Parsing",err);
return;
}
update();
gstatusbar->message("Cycle Parameters Read",2000);
}
void PhaseTableWin::loadFromTms() {
BError err;
BString type = ocycleTypes->currentText().ascii();
if (err = ocontrol.getControlInfo(type,0,oparams)) {
BString msg;
msg.printf("Attempting to access cycle type (%s) ",type.retStr());
msg += err.getString();
err.set(err.getErrorNo(),msg);
warningDialog("Load from Tms",msg);
return;
}
update();
}
void PhaseTableWin::loadCycleParams() {
BError err;
if(err = ocontrol.setControlInfo(oparams)){
warningDialog("Cycle Parameters - Loading Cycle Params",err);
return;
}
gstatusbar->message("Cycle Parameters Loaded",2000);
}
void PhaseTableWin::addOffsets() {
int val = ooffset->value();
for(unsigned int i = 0; i < oparams.frefPhaseDelay.size(); i++ ) {
int a;
a = oparams.frefPhaseDelay[i] + val;
a &= 0x1ff;
oparams.frefPhaseDelay[i] = a;
}
update();
}
void PhaseTableWin::loadDefaults() {
BArray<int> pos;
tmsPickupPositions(oparams.frefPhaseDelay);
update();
}
void PhaseTableWin::setParams() {
bool ok;
oparams.cycleType = ocycleType->text().ascii();
oparams.info = oinfo->text().ascii();
oparams.pllInitialFrequency = opllInitialFrequency->text().toUInt(&ok);
oparams.pllInitialFrequencyDelay = opllInitialFrequencyDelay->text().toUInt(&ok);
oparams.pllFrefGain = opllFrefGain->text().toUInt(&ok);
oparams.pllGain = opllGain->text().toUInt(&ok);
for(unsigned int i = 0; i < oparams.frefPhaseDelay.size(); i++ ) {
oparams.frefPhaseDelay[i] = ophaseDelays->text(i,0).toInt(&ok);
}
}
void PhaseTableWin::upload() {
BError err;
setParams();
if (err = ocontrol.setControlInfo(oparams)) {
warningDialog("Upload of Cycle Params",err);
return;
}
update();
}
void PhaseTableWin::saveFile() {
BError err;
BFile f;
BString fname;
TmsCycleParams cycleParamFile("");
setParams();
fname = ofilename->text().ascii();
QString s = QFileDialog::getSaveFileName("/usr/tms/stateTables","Tms Cycle Parameters File (*.spt)",this,"save file dialog","Specify filename");
fname = s.ascii();
if (fname.len() == 0)
return;
if (QFile::exists(fname.retStr())){
BString msg = "The specified file already exists are you ";
msg += BString("positive you wish to over write the file");
if (! confirmDialog("Confirm Action",msg)) {
return;
}
}
if(err = cycleParamFile.writeCycleParams(fname,oparams)) {
warningDialog("Cycle Parameters - Save",err);
return;
}
ofilename->setText(fname.retStr());
gstatusbar->message("Cycle Parameters Saved",2000);
}
void PhaseTableWin::selectFile() {
QString s = QFileDialog::getOpenFileName(
"/usr/tms/stateTables",
"Tms Cycle Parameters File (*.spt)",
this,
"open file dialog",
"Choose a file" );
ofilename->setText(s);
}
BString PhaseTableWin::uIntToStr(UInt32 val) {
BString s;
s.printf("%lu",val);
return s;
}
void PhaseTableWin::warningDialog(BString title, BError err){
BString m;
QMessageBox b;
m = BString("<h5>") + title + "</h5><p>" + err.getString() + "</p>";
b.setMinimumWidth(300);
b.setCaption("tmsControlGui - Warning");
b.setIcon(QMessageBox::Warning);
b.setText(m.retStr());
b.exec();
}
int PhaseTableWin::confirmDialog(BString title,BString msg){
BString s;
s = BString("<h5>") + title + "</h5><p>" + msg + "</p>";
QMessageBox mb( "tmsControlGui - Question",
s.retStr(),
QMessageBox::Information,
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::Cancel | QMessageBox::Escape,
QMessageBox::NoButton );
switch( mb.exec() ) {
case QMessageBox::Yes:
return 1;
break;
case QMessageBox::No:
return 0;
break;
case QMessageBox::Cancel:
return 0;
break;
}
return 0;
}