/***************************************************************************
* Copyright (C) 2006 by LZ *
* *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "hv.h"
#include <qtango.h>
#include <QColor>
#include <QMessageBox>
#include <twidgetgroup.h>
void hv::fill_frame(vector <QString> text, TWidgetGroup &kleds, QFrame * &frame) {
QGridLayout *box1;
box1 = new QGridLayout(frame);
box1->setSpacing(3);
box1->setMargin(3);
int led_size = 18;
/*
for (unsigned i=0; i<text.size(); i++) {
box1->setRowMinimumHeight(i,23);
QLabel *lab = new QLabel(frame);
lab->setText(" "+text[i]);
QFont f("Helvetica", 12, QFont::Bold);
lab->setFont(f);
// lab->setSize(24);
kleds.addWidget(lab);
box1->addWidget(lab, i, 0, Qt::AlignLeft);
TLed *ptr = new TLed(frame);
ptr->setTrueColor(QColor::QColor(40, 200, 40));
ptr->setFalseColor(QColor::QColor(200, 200, 200));
ptr->setLedWidth(led_size-2);
ptr->setLedHeight(led_size-2);
ptr->setMinimumSize(led_size, led_size);
ptr->setMaximumSize(led_size, led_size);
ptr->setGeometry(1, 1, led_size, led_size);
kleds.addWidget(ptr);
box1->addWidget(ptr, i, 1, Qt::AlignCenter);
}
*/
for (unsigned i=0; i<text.size(); i++) {
box1->setRowMinimumHeight(i,23);
QLabel *lab = new QLabel(frame);
lab->setObjectName("labElement0");
lab->setText(" "+text[i]);
QFont f("Helvetica", 12, QFont::Bold);
lab->setFont(f);
// lab->setSize(24);
box1->addWidget(lab, i, 0, Qt::AlignLeft);
TLed *ptr = new TLed(frame);
ptr->setObjectName("ledElement1");
if (i < 4 || i > 8) {
ptr->setTrueColor(QColor(40, 160, 40));
}
else {
ptr->setTrueColor(QColor(160, 40, 40));
}
ptr->setFalseColor(QColor(170, 255, 255));
ptr->setLedWidth(led_size-2);
ptr->setLedHeight(led_size-2);
ptr->setMinimumSize(led_size, led_size);
ptr->setMaximumSize(led_size, led_size);
ptr->setGeometry(1, 1, led_size, led_size);
box1->addWidget(ptr, i, 1, Qt::AlignCenter);
#ifdef QTANGOLIB_GET_VERSION
kleds.setObjectNameList(QStringList() << lab->objectName() << ptr->objectName());
#else
kleds.addWidget(lab);
kleds.addWidget(ptr);
#endif
}
return;
}
hv::hv(QWidget *parent) : QDialog(parent)
{
ui.setupUi(this);
// static TWidgetGroup hv_leds(this);
// static TWidgetGroup hv_ramps;
vector <QString> hv_labs = explode(',', "FuG enabled,Ready (Not busy),Switching ON,Switching OFF,Ramping UP,Ramping Down,Ramping to low power,Making a new ramp,Aborting,OFF,Limited Power,Standby,Low Power,Full Power");
// fill_frame(hv_labs, hv_leds, ui.frame_StateFlags);
ui.mod_title->setText(QString("Mod %1 HV").arg(qApp->arguments().at(1)));
if (qApp->arguments()[1] == "1") {
/*
try {
hv_leds.setSource("p/mod/hv_p1/StateFlags");
}
catch (...) {
sleep(1);
try {
hv_leds.setSource("p/mod/hv_p1/StateFlags");
}
catch (...) {
QMessageBox::information(0, "cannot connect HV server",
QString("Cannot connect HV server for modulator "+qApp->arguments()[1]));
exit(EXIT_FAILURE);
}
}
*/
try {
hvServer = new Tango::DeviceProxy("p/mod/hv_p1");
}
catch (...) {
sleep(1);
try {
hvServer = new Tango::DeviceProxy("p/mod/hv_p1");
}
catch (...) {
QMessageBox::information(0, "cannot connect HV server",
QString("Cannot connect HV server for modulator "+qApp->arguments()[1]));
exit(EXIT_FAILURE);
}
}
try {
rossServer = new Tango::DeviceProxy("p/mod/ross_p1");
}
catch (...) {
sleep(1);
try {
rossServer = new Tango::DeviceProxy("p/mod/ross_p1");
}
catch (...) {
QMessageBox::information(0, "cannot connect HV server",
QString("Cannot connect HV server for modulator "+qApp->arguments()[1]));
exit(EXIT_FAILURE);
}
}
}
else if (qApp->arguments()[1] == "2") {
/*
try {
hv_leds.setSource("p/mod/hv_p2/StateFlags");
}
catch (...) {
sleep(1);
try {
hv_leds.setSource("p/mod/hv_p2/StateFlags");
}
catch (...) {
QMessageBox::information(0, "cannot connect HV server",
QString("Cannot connect HV server for modulator "+qApp->arguments()[1]));
exit(EXIT_FAILURE);
}
}
*/
try {
hvServer = new Tango::DeviceProxy("p/mod/hv_p2");
}
catch (...) {
sleep(1);
try {
hvServer = new Tango::DeviceProxy("p/mod/hv_p2");
}
catch (...) {
QMessageBox::information(0, "cannot connect HV server",
QString("Cannot connect HV server for modulator "+qApp->arguments()[1]));
exit(EXIT_FAILURE);
}
}
try {
rossServer = new Tango::DeviceProxy("p/mod/ross_p2");
}
catch (...) {
sleep(1);
try {
rossServer = new Tango::DeviceProxy("p/mod/ross_p2");
}
catch (...) {
QMessageBox::information(0, "cannot connect Ross server",
QString("Cannot connect Ross server for modulator "+qApp->arguments()[1]));
exit(EXIT_FAILURE);
}
}
}
else if (qApp->arguments()[1] == "10") {
/*
try {
hv_leds.setSource("f/mod/hv_f10/StateFlags");
}
catch (...) {
sleep(1);
try {
hv_leds.setSource("f/mod/hv_f10/StateFlags");
}
catch (...) {
QMessageBox::information(0, "cannot connect HV server",
QString("Cannot connect HV server for modulator "+qApp->arguments()[1]));
exit(EXIT_FAILURE);
}
}
*/
try {
hvServer = new Tango::DeviceProxy("f/mod/hv_f10");
}
catch (...) {
sleep(1);
try {
hvServer = new Tango::DeviceProxy("f/mod/hv_f10");
}
catch (...) {
QMessageBox::information(0, "cannot connect HV server",
QString("Cannot connect HV server for modulator "+qApp->arguments()[1]));
exit(EXIT_FAILURE);
}
}
try {
rossServer = new Tango::DeviceProxy("f/mod/ross_f10");
}
catch (...) {
sleep(1);
try {
hvServer = new Tango::DeviceProxy("f/mod/ross_f10");
}
catch (...) {
QMessageBox::information(0, "cannot connect Ross server",
QString("Cannot connect Ross server for modulator "+qApp->arguments()[1]));
exit(EXIT_FAILURE);
}
}
}
else {
QMessageBox::information(0, "cannot connect HV server",
QString("Cannot connect HV server for modulator "+qApp->arguments()[1]));
exit(EXIT_FAILURE);
}
try {
ramps = hvServer->read_attribute("ListRamp");
}
catch (...) {
try {
ramps = hvServer->read_attribute("ListRamp");
}
catch (...) {
QMessageBox::information(0, "cannot connect Ross server",
QString("Cannot connect list ramp for modulator "+qApp->arguments()[1]));
exit(EXIT_FAILURE);
}
}
vector<string> ramp;
ramps >> ramp;
for (unsigned i=0; i<ramp.size(); i++) {
ui.comboBox->insertItem(i, ramp[i].c_str(), QVariant(Qt::red));
}
// ui.comboBox->setCurrentItem(i);
connect(ui.View_PushButton, SIGNAL(clicked()), this, SLOT(view_ramp()));
connect(ui.Load_PushButton, SIGNAL(clicked()), this, SLOT(load_ramp()));
connect(ui.Save_PushButton, SIGNAL(clicked()), this, SLOT(save_ramp()));
connect(ui.makerampApply, SIGNAL(clicked()), this, SLOT(make_ramp()));
connect(ui.Off_PushButton, SIGNAL(clicked()), this, SLOT(hv_off()));
connect(ui.Ground_PushButton, SIGNAL(clicked()), this, SLOT(ross_ground()));
// QString q_cmd;
// ui.rampDown_ApplyNumeric->setTarget("p/mod/hv_p"+qApp->arguments()[1]+"->RampDown");
// ui.RampDn_PushButton->setTargets("p/mod/hv_p"+qApp->arguments()[1]+"->RampDown");
}
hv::~hv()
{
exit(0);
}
void hv::hv_off()
{
try {
int r = QMessageBox::question(this, "Confirm HV OFF", "Please confirm command: 'OFF'", "OFF", "Cancel");
if (r == 0) {
try {
hvServer->command_inout("Off");
}
catch (...) {
QMessageBox::information(0, "cannot switch HV off",
QString("Cannot switch HV off "+qApp->arguments()[1]));
}
}
}
catch (const Tango::DevFailed &e) {
cout << "Error: hv_off()" << endl;
}
}
void hv::ross_ground()
{
try {
int r = QMessageBox::question(this, "Confirm Ross Ground", "Please confirm command: 'Ground'", "Ground", "Cancel");
if (r == 0) {
try {
hvServer->command_inout("Off");
rossServer->command_inout("Off");
}
catch (...) {
QMessageBox::information(0, "cannot switch Ross Ground",
QString("Cannot switch Ross Ground "+qApp->arguments()[1]));
}
}
}
catch (const Tango::DevFailed &e) {
cout << "Error: ross_ground()" << endl;
}
}
void hv::view_ramp()
{
// static TWidgetGroup hv_ramp;
Tango::DeviceData rampName;
Tango::DeviceData rampData;
Tango::DevString str = CORBA::string_dup(ui.comboBox->currentText().toStdString().c_str());
rampName << str;
try {
rampData = hvServer->command_inout("ReadRamp", rampName);
}
catch (Tango::DevFailed &e) {
Tango::Except::print_exception(e);
}
vector<string> ramp;
rampData >> ramp;
ui.textEdit->clear();
for (unsigned i=0; i<ramp.size(); i++) {
ui.textEdit->append(ramp[i].substr(0,ramp[i].size()-1).c_str());
}
}
void hv::load_ramp()
{
// static TWidgetGroup hv_ramp;
Tango::DeviceData rampName;
Tango::DeviceData rampData;
Tango::DevString str = CORBA::string_dup(ui.comboBox->currentText().toStdString().c_str());
rampName << str;
try {
hvServer->command_inout("LoadRamp", rampName);
}
catch (Tango::DevFailed &e) {
Tango::Except::print_exception(e);
}
}
void hv::save_ramp()
{
// static TWidgetGroup hv_ramp;
Tango::DeviceData rampName;
Tango::DeviceData rampData;
Tango::DevVarStringArray *argout = new Tango::DevVarStringArray();
QStringList ramplist = ui.textEdit->toPlainText().split("\n");
argout->length(ramplist.size()+1);
(*argout)[0] = CORBA::string_dup(ui.comboBox->currentText().toStdString().c_str());
for (int i=0; i<ramplist.size(); i++) {
(*argout)[i+1] = CORBA::string_dup((ramplist[i].toStdString()+"\n").c_str());
cout << ramplist[i].toStdString() << endl;
}
rampData << argout;
try {
hvServer->command_inout("SaveRamp", rampData);
}
catch (Tango::DevFailed &e) {
Tango::Except::print_exception(e);
}
return;
}
void hv::make_ramp()
{
// static TWidgetGroup hv_ramp;
Tango::DeviceData rampData;
Tango::DevVarDoubleArray *argout = new Tango::DevVarDoubleArray();
argout->length(3);
(*argout)[0] = ui.makerampV->value();
(*argout)[1] = ui.makerampI->value();
(*argout)[2] = ui.makerampT->value();
cout << ui.makerampV->value() << endl;
rampData << argout;
try {
hvServer->command_inout("MakeRamp", rampData);
}
catch (Tango::DevFailed &e) {
// cout << "Tango::DevFailed " << e.errors[0].reason << endl;
QMessageBox::information(0, "ERROR", QString(e.errors[0].reason));
Tango::Except::print_exception(e);
}
catch (...) {
cout << "ERROR: operation failed" << endl;
}
/*
Tango::DeviceData rampName;
QStringList ramplist = ui.textEdit->toPlainText().split("\n");
for (unsigned i=0; i<ramplist.size(); i++) {
(*argout)[i+1] = CORBA::string_dup((ramplist[i].toStdString()+"\n").c_str());
cout << ramplist[i].toStdString() << endl;
}
*/
return;
}
vector <QString> explode(const char separator, const char* all)
{
unsigned i, j;
vector <QString> v;
char celem[50];
v.resize(0);
for(i=0; i < strlen(all); i++)
{
j = 0;
while(all[i] != separator && i < strlen(all) )
{
celem[j] = all[i];
i++;
j++;
}
celem[j] = '\0';
v.push_back(QString(celem) );
}
return v;
}