Advertisement
Guest User

qtcagraph.cpp

a guest
Jun 24th, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <QtGui>
  2. #include <QProcess>
  3.  
  4. #include <qwt/qwt_plot.h>
  5. #include <qwt/qwt_plot_curve.h>
  6. #include <qwt/qwt_plot_zoomer.h>
  7. #include <qwt/qwt_plot_magnifier.h>
  8.  
  9. #include <qtcagraph.hpp>
  10.  
  11. QtCAgraph::QtCAgraph(QWidget* parent) : QWidget(parent) {
  12.     pv_name = new QLineEdit("PV name",this);
  13.     updateButton = new QPushButton("Start",this);
  14.     autoscaleXButton = new QPushButton("X",this);
  15.     autoscaleYButton = new QPushButton("Y",this);
  16.     autoscale = new QLabel("Autoscale:",this);
  17.     proc = new QProcess(this);
  18.     graph = new QwtPlot();
  19.     dataCurve = new QwtPlotCurve();
  20.     zoomer = new QwtPlotZoomer(graph->canvas());
  21.     mag = new QwtPlotMagnifier(graph->canvas());
  22.  
  23.     datasize = 128;
  24.     datax = new double[datasize];
  25.     datay = new double[datasize];
  26.     i = 0;
  27.  
  28.     connect( autoscaleXButton,SIGNAL(clicked()),
  29.         this,SLOT(resetAutoscaleX()) );
  30.     connect( autoscaleYButton,SIGNAL(clicked()),
  31.         this,SLOT(resetAutoscaleY()) );
  32.     connect( updateButton,SIGNAL(clicked()),
  33.         this,SLOT(updateClicked()) );
  34.     connect( proc,SIGNAL(readyReadStandardOutput()),
  35.         this,SLOT(readFromStdout()) );
  36.  
  37.     layout1 = new QHBoxLayout;
  38.     layout1->addWidget(pv_name);
  39.     layout1->addWidget(updateButton);
  40.     layout1->addWidget(autoscale);
  41.     layout1->addWidget(autoscaleXButton);
  42.     layout1->addWidget(autoscaleYButton);
  43.  
  44.     layout2 = new QVBoxLayout;
  45.     layout2->addLayout(layout1);
  46.     layout2->addWidget(graph);
  47.  
  48.     setLayout(layout2);
  49. }
  50.  
  51. QtCAgraph::~QtCAgraph() {
  52.     delete pv_name;
  53.     delete updateButton;
  54.     delete proc;
  55.  
  56.     delete graph;
  57. //    delete dataCurve;
  58.     delete zoomer;
  59.     delete mag;
  60.  
  61.     delete datax;
  62.     delete datay;
  63.  
  64.     delete layout1;
  65.     delete layout2;
  66. }
  67.  
  68. void QtCAgraph::updateClicked() {
  69.     QString pv = pv_name->text();
  70.     QStringList args;
  71.     args << "-f 4" << pv;
  72.  
  73.     program = "camonitor";
  74.    
  75.     if(proc->state()) {
  76.     proc->terminate();
  77.     updateButton->setText("Start monitor");
  78.     }
  79.     else {
  80.     proc->start(program,args);
  81.     updateButton->setText("Stop monitor");
  82.     }
  83. }
  84.  
  85. void QtCAgraph::readFromStdout() {
  86.     QByteArray output;
  87.     output = proc->readAllStandardOutput();
  88.  
  89.     QTextStream test(output);
  90.     QString name;
  91.     QString day;
  92.     QString time;
  93.     double value;
  94.     QString status;
  95.     QString severity;
  96.    
  97.     test >> name;
  98.     test >> day;
  99.     test >> time;
  100.     test >> value;
  101.     test >> status;
  102.     test >> severity;
  103.  
  104.     if (i == datasize) expandData();
  105.     datax[i] = i;
  106.     datay[i] = value;
  107.     i++;
  108.    
  109.     dataCurve->setRawSamples(datax,datay,i);
  110.     dataCurve->attach(graph);
  111.     graph->replot();
  112. }
  113.  
  114. void QtCAgraph::expandData() {
  115.     double* new_datax = new double[datasize*2];
  116.     double* new_datay = new double[datasize*2];
  117.  
  118.     for(int j=0; j<datasize; j++) {
  119.     new_datax[j] = datax[j];
  120.     new_datay[j] = datay[j];
  121.     }
  122.  
  123.     datasize *= 2;
  124.    
  125.     delete[] datax;
  126.     delete[] datay;
  127.  
  128.     datax = new_datax;
  129.     datay = new_datay;
  130. }
  131.  
  132. void QtCAgraph::resetAutoscaleX() {
  133.     graph->setAxisAutoScale(2);
  134.     graph->replot();
  135. }
  136.  
  137. void QtCAgraph::resetAutoscaleY() {
  138.     graph->setAxisAutoScale(0);
  139.     graph->replot();
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement