// $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/src/obs_ctrl/Qt/engPanel/engineering_ctrl_panel.cpp 552 2018-05-04 09:04:24Z depa $
#include "engineering_ctrl_panel.h"

engineerUi::engineerUi()
{
  QFont font;
  font.setPointSize(12);

  std::cout << "QQ1" << std::endl;
  this->resize(1111, 652);
  centralwidget = new QWidget();
  centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
  centralwidget->setMaximumSize(QSize(1110, 610));
  std::cout << "QQ2" <<  std::endl;
  gridLayout = new QGridLayout(centralwidget);
  gridLayout->setObjectName(QString::fromUtf8("gridLayout"));

  QSizePolicy sizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  btnTiptiltCtrl = new QPushButton(centralwidget);
  btnTiptiltCtrl->setObjectName(QString::fromUtf8("btnTiptiltCtrl"));

  sizePolicy.setHorizontalStretch(0);
  sizePolicy.setVerticalStretch(0);
  sizePolicy.setHeightForWidth(btnTiptiltCtrl->sizePolicy().hasHeightForWidth());
  btnTiptiltCtrl->setSizePolicy(sizePolicy);
  btnTiptiltCtrl->setMaximumSize(QSize(270, 100));
  std::cout << "QQ3" << std::endl;
  gridLayout->addWidget(btnTiptiltCtrl, 0, 0, 1, 1);

  btnTrackCtrl = new QPushButton(centralwidget);
  btnTrackCtrl->setObjectName(QString::fromUtf8("btnTrackCtrl"));
  sizePolicy.setHeightForWidth(btnTrackCtrl->sizePolicy().hasHeightForWidth());
  btnTrackCtrl->setSizePolicy(sizePolicy);
  //btnTrackCtrl->setMinimumSize(QSize(134, 102));
  btnTrackCtrl->setMaximumSize(QSize(270, 100));

  gridLayout->addWidget(btnTrackCtrl, 1, 0, 1, 1);
  std::cout << "QQ4" << std::endl;
  btnMotionCtrl = new QPushButton(centralwidget);
  btnMotionCtrl->setObjectName(QString::fromUtf8("btnMotionCtrl"));
  sizePolicy.setHeightForWidth(btnMotionCtrl->sizePolicy().hasHeightForWidth());
  btnMotionCtrl->setSizePolicy(sizePolicy);
  // btnMotionCtrl->setMinimumSize(QSize(134, 102));
  btnMotionCtrl->setMaximumSize(QSize(270, 100));

  gridLayout->addWidget(btnMotionCtrl, 2, 0, 1, 1);
  std::cout << "QQ5" << std::endl;
  btnCalibCtrl = new QPushButton(centralwidget);
  btnCalibCtrl->setObjectName(QString::fromUtf8("btnCalibCtrl"));
  sizePolicy.setHeightForWidth(btnCalibCtrl->sizePolicy().hasHeightForWidth());
  btnCalibCtrl->setSizePolicy(sizePolicy);
  btnCalibCtrl->setMaximumSize(QSize(270, 100));

  gridLayout->addWidget(btnCalibCtrl, 3, 0, 1, 1);
  std::cout << "QQ6" << std::endl;
  labSynoptic = new QLabel(centralwidget);
  labSynoptic->setObjectName(QString::fromUtf8("labSynoptic"));
  sizePolicy.setHeightForWidth(labSynoptic->sizePolicy().hasHeightForWidth());
  labSynoptic->setSizePolicy(sizePolicy);
  labSynoptic->setMaximumSize(QSize(700, 500));
  labSynoptic->setPixmap(QPixmap(QString::fromUtf8(":/sharknir-synoptic.png")));
  labSynoptic->setScaledContents(true);

  gridLayout->addWidget(labSynoptic, 0, 1, 4, 1);

  setCentralWidget(centralWidget);
  std::cout << "QQ7" << std::endl;
  menubar = new QMenuBar();
  menuFile = new QMenu();
  // menubar->addAction(menuFile->menuAction());
  // menuFile->addAction(actionClose);

  std::cout << "QQ8" << std::endl;
  QObject::connect(menubar, SIGNAL(triggered(QAction*)), this, SLOT(close()));
  QObject::connect(btnCalibCtrl, SIGNAL(clicked()), this, SLOT(on_calib_button_clicked()));
  QObject::connect(btnTrackCtrl, SIGNAL(clicked()), this, SLOT(on_track_button_clicked()));
  QObject::connect(btnTiptiltCtrl, SIGNAL(clicked()), this, SLOT(on_tiptilt_button_clicked()));
  QObject::connect(btnMotionCtrl, SIGNAL(clicked()), this, SLOT(on_motion_button_clicked()));
  std::cout << "QQ9" << std::endl;

  createActions();
  createMenus();
}


// engineerUi::~engineerUi()
// {
//   std::cout << "Adios" << std::endl;
// }

void engineerUi::createActions()
{
  actionClose = new QAction(QString::fromUtf8("Close"), this);
  actionClose->setShortcuts(QKeySequence::Quit);
  actionClose->setStatusTip("Exit the application");
  connect(actionClose, SIGNAL(triggered()), this, SLOT(close()));
}

void engineerUi::createMenus()
{
  menuFile = menuBar()->addMenu("&File");
  menuFile->addAction(actionClose);
}


// SLOTS

void engineerUi::on_calib_button_clicked()
{
  QProcess procCalibCtrl;
  QString program = "niceQUI";
  QStringList arguments;
  arguments << "--CALIB_CTRL.CONFIG:Endpoint=[NAME=calib_ctrl-svr-DUMMY,TCP=localhost:60400]";
  arguments << "--NICEQUI_WINDOW_TITLE:String=\"SHARK-NIR:Calibration_Unit_Control\"";
  procCalibCtrl.startDetached(program, arguments);
  procCalibCtrl.waitForStarted();
}

void engineerUi::on_track_button_clicked()
{
  QProcess procTrackCtrl;
  QString program = "niceQUI";
  QStringList arguments;
  arguments << "--TRACK_CTRL.CONFIG:Endpoint=[NAME=track_ctrl-svr-DUMMY,TCP=localhost:60300]";
  arguments << "--NICEQUI_WINDOW_TITLE:String=\"SHARK-NIR:Tracking_Control\"";
  procTrackCtrl.startDetached(program, arguments);
  procTrackCtrl.waitForStarted();
}

void engineerUi::on_tiptilt_button_clicked()
{
  QProcess procTiptiltCtrl;
  QString program = "niceQUI";
  QStringList arguments;
  arguments << "--TIPTILT_CTRL.CONFIG:Endpoint=[NAME=tiptilt_ctrl-svr-DUMMY,TCP=localhost:60100]";
  arguments << "--NICEQUI_WINDOW_TITLE:String=\"SHARK-NIR:Tip/Tilt_Control\"";
  procTiptiltCtrl.startDetached(program, arguments);
  procTiptiltCtrl.waitForStarted();
}

void engineerUi::on_motion_button_clicked()
{
  QProcess procMotionCtrl;
  QString program = "niceQUI";
  QStringList arguments;
  arguments << "--MOTION_CTRL.CONFIG:Endpoint=[NAME=motion_ctrl-svr-DUMMY,TCP=localhost:60400]";
  arguments << "--NICEQUI_WINDOW_TITLE:String=\"SHARK-NIR:Motion_Control\"";
  procMotionCtrl.startDetached(program, arguments);
  procMotionCtrl.waitForStarted();
}




int main(int argc, char **args)
{
  //Q_INIT_RESOURCE(application);
  // Q_INIT_RESOURCE(resources);

  QApplication app(argc, args);
  app.setOrganizationName("inaf");
  app.setApplicationName("SHINS");

  engineerUi mainUi;

  mainUi.show();
  std::cout << "QQ10" << std::endl;
  return app.exec();
}
