qavrg 0.0.28
qavrgwindow.cpp
Go to the documentation of this file.
00001 #include "qavrgwindow.h"
00002 #include "qavrgapplication.h"
00003 #include "qavrgacquisition.h"
00004 #include "qavrgfillpattern.h"
00005 #include "qavrghighlighter.h"
00006 #include "qavrgresultsmodel.h"
00007 #include "qavrgsettings.h"
00008 #include "qcepproperty.h"
00009 #include "qavrgfitter.h"
00010 #include "qavrgscriptingengine.h"
00011 #include "qavrgmeasurer.h"
00012 #include "qavrgtestwindow.h"
00013 
00014 #include <qwt_plot.h>
00015 #include <qwt_plot_curve.h>
00016 #include <qwt_plot_zoomer.h>
00017 #include <qwt_plot_magnifier.h>
00018 #include <qwt_plot_panner.h>
00019 #include <qwt_plot_picker.h>
00020 #include <qwt_picker_machine.h>
00021 #include <qwt_legend.h>
00022 #include <qwt_symbol.h>
00023 #include <QPen>
00024 #include <QFile>
00025 #include <QCheckBox>
00026 #include <QSignalMapper>
00027 #include <QMessageBox>
00028 #include <QCloseEvent>
00029 #include <QMetaProperty>
00030 #include <QProgressBar>
00031 #include <QTimer>
00032 #include <QFileDialog>
00033 #include <stdio.h>
00034 #include <QThread>
00035 
00036 QavrgWindow::QavrgWindow(QavrgApplication *app, QWidget *parent)
00037   : QMainWindow(parent),
00038     m_DisplayXUnits(app->saver(), this, "displayXUnits", 0, "X Units in Display"),
00039     m_DisplayXRange(app->saver(), this, "displayXRange", 0, "X Range Display"),
00040     m_DisplayOrbit(app->saver(), this, "displayOrbit", 0, "Display Orbit"),
00041     m_DisplayBunch(app->saver(), this, "displayBunch", 0, "Display Bunch"),
00042     m_Application(app),
00043     m_Acquisition(NULL),
00044     m_ScriptingEngine(NULL),
00045     m_Progress(NULL),
00046     m_StatusMessage(NULL),
00047     m_XUnits(0),
00048     m_XStep(1.0),
00049     m_Highlighter(NULL),
00050     m_AcquireDockWidget(NULL),
00051     m_InputsDockWidget(NULL),
00052     m_OutputDockWidget(NULL),
00053     m_TimingDockWidget(NULL),
00054     m_ViewDockWidget(NULL)
00055 {
00056   setupUi(this);
00057 
00058   m_AcquireDockWidget = new QavrgAcquireDialog();
00059   m_InputsDockWidget = new QavrgInputsDialog();
00060   m_OutputDockWidget = new QavrgOutputDialog();
00061   m_TimingDockWidget = new QavrgTimingDialog();
00062   m_ViewDockWidget = new QavrgViewDialog();
00063 
00064   addDockWidget(Qt::RightDockWidgetArea, m_AcquireDockWidget);
00065   tabifyDockWidget(m_AcquireDockWidget, m_InputsDockWidget);
00066   tabifyDockWidget(m_InputsDockWidget, m_OutputDockWidget);
00067 
00068   addDockWidget(Qt::RightDockWidgetArea, m_TimingDockWidget);
00069   tabifyDockWidget(m_TimingDockWidget, m_ViewDockWidget);
00070 
00071   m_WindowsMenu -> addAction(m_ViewDockWidget -> toggleViewAction());
00072   m_WindowsMenu -> addAction(m_AcquireDockWidget -> toggleViewAction());
00073   m_WindowsMenu -> addAction(m_OutputDockWidget -> toggleViewAction());
00074   m_WindowsMenu -> addAction(m_InputsDockWidget -> toggleViewAction());
00075   m_WindowsMenu -> addAction(m_TimingDockWidget -> toggleViewAction());
00076   m_WindowsMenu -> addSeparator();
00077   m_WindowsMenu -> addAction(m_ToolBar -> toggleViewAction());
00078 
00079   m_Plot->setCanvasBackground(QColor(Qt::white));
00080 
00081   m_Zoomer = new QwtPlotZoomer(QwtPlot::xBottom, QwtPlot::yLeft, m_Plot->canvas());
00082   m_Zoomer -> setStateMachine(new QwtPickerDragRectMachine());
00083   m_Zoomer -> setTrackerMode(QwtPicker::ActiveOnly);
00084   m_Zoomer -> setRubberBand(QwtPicker::RectRubberBand);
00085 
00086   m_Zoomer -> setMousePattern(QwtEventPattern::MouseSelect2,
00087                               Qt::LeftButton, Qt::ControlModifier | Qt::ShiftModifier);
00088   m_Zoomer -> setMousePattern(QwtEventPattern::MouseSelect3,
00089                               Qt::LeftButton, Qt::ControlModifier);
00090 
00091   m_Magnifier = new QwtPlotMagnifier(m_Plot->canvas());
00092   m_Magnifier -> setMouseButton(Qt::NoButton);
00093 
00094   m_Panner = new QwtPlotPanner(m_Plot->canvas());
00095   m_Panner -> setMouseButton(Qt::MidButton);
00096 
00097   m_Measurer = new QavrgMeasurer(QwtPlot::xBottom, QwtPlot::yLeft, m_Plot->canvas());
00098   m_Measurer -> setStateMachine(new QwtPickerPolygonMachine());
00099   m_Measurer -> setTrackerMode(QwtPicker::AlwaysOn);
00100   m_Measurer -> setRubberBand(QwtPicker::PolygonRubberBand);
00101 
00102   connect(m_Measurer, SIGNAL(printMessage(QString)), this, SLOT(printMessage(QString)));
00103   connect(m_Measurer, SIGNAL(printMessage(QString)), this, SLOT(displayStatusMessage(QString)));
00104 
00105   m_Legend = new QwtLegend;
00106   m_Legend -> setFrameStyle(QFrame::Box|QFrame::Sunken);
00107   m_Legend -> setDefaultItemMode(QwtLegendData::Checkable);
00108 
00109   m_Plot -> insertLegend(m_Legend, QwtPlot::BottomLegend);
00110 
00111   setZoomingMode();
00112 
00113   connect(m_ActionAutoScale, SIGNAL(triggered()), this, SLOT(autoScale()));
00114   connect(m_ActionQuit, SIGNAL(triggered()), m_Application, SLOT(possiblyQuit()));
00115   connect(m_ActionLoadData, SIGNAL(triggered()), this, SLOT(loadData()));
00116   connect(m_ActionSaveData, SIGNAL(triggered()), this, SLOT(saveData()));
00117 
00118   connect(m_ActionLoadSettings, SIGNAL(triggered()), m_Application, SLOT(loadSettings()));
00119   connect(m_ActionSaveSettings, SIGNAL(triggered()), m_Application, SLOT(saveSettings()));
00120 
00121   connect(m_ActionEvaluateScript, SIGNAL(triggered()), this, SLOT(doScript()));
00122   connect(m_EvaluateScript, SIGNAL(clicked()), this, SLOT(doScript()));
00123   connect(m_CancelScript, SIGNAL(clicked()), this, SLOT(cancelScript()));
00124 
00125   m_CancelScript   -> setEnabled(false);
00126   m_EvaluateScript -> setEnabled(true);
00127 
00128   m_Highlighter = new QavrgHighlighter(m_InputScript->document());
00129 
00130   m_HelpText -> setReadOnly(true);
00131 
00132   QFile file(":/qavrghelptext.html");
00133 
00134   file.open(QIODevice::ReadOnly);
00135 
00136   QString contents = file.readAll();
00137 
00138   m_HelpText -> setHtml(contents);
00139 
00140   m_StatusMessage = new QLabel(NULL);
00141   m_StatusMessage -> setMinimumWidth(200);
00142   m_StatusMessage -> setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00143 
00144   statusBar() -> addPermanentWidget(m_StatusMessage);
00145 
00146   m_Progress = new QProgressBar(NULL);
00147   m_Progress -> setMinimumWidth(150);
00148   m_Progress -> setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00149 
00150   statusBar() -> addPermanentWidget(m_Progress);
00151 
00152   m_Progress -> hide();
00153 
00154   connect(&m_StatusTimer, SIGNAL(timeout()), this, SLOT(clearStatusMessage()));
00155 
00156   connect(m_ZoomButton, SIGNAL(clicked()), this, SLOT(setZoomingMode()));
00157   connect(m_ZoomOutButton, SIGNAL(clicked()), this, SLOT(zoomOut()));
00158   connect(m_ZoomAllButton, SIGNAL(clicked()), this, SLOT(zoomAll()));
00159   connect(m_MeasureButton, SIGNAL(clicked()), this, SLOT(setMeasuringMode()));
00160 
00161   connect(m_OutputDockWidget->m_BrowseOutputDirectory, SIGNAL(clicked()), this, SLOT(browseOutputDirectory()));
00162 
00163   QString winTitle = windowTitle();
00164 
00165   setWindowTitle(winTitle + " v" STR(QAVRG_VERSION));
00166 }
00167 
00168 void QavrgWindow::setAcquisition(QavrgAcquisition *acq)
00169 {
00170   m_Acquisition = acq;
00171 
00172   connect(m_AcquireDockWidget->m_AcquireDataButton,      SIGNAL(clicked()), m_ActionAcquireData, SIGNAL(triggered()));
00173   connect(m_AcquireDockWidget->m_AcquireReferenceButton, SIGNAL(clicked()), m_ActionAcquireReference, SIGNAL(triggered()));
00174   connect(m_AcquireDockWidget->m_AcquireDarkButton,      SIGNAL(clicked()), m_ActionAcquireDark, SIGNAL(triggered()));
00175   connect(m_AcquireDockWidget->m_AcquireOnceButton,      SIGNAL(clicked()), m_ActionAcquireOnce, SIGNAL(triggered()));
00176   connect(m_AcquireDockWidget->m_AcquireScopeButton,     SIGNAL(clicked()), m_ActionAcquireScope, SIGNAL(triggered()));
00177   connect(m_AcquireDockWidget->m_AcquireCancelButton,    SIGNAL(clicked()), m_ActionAcquireCancel, SIGNAL(triggered()));
00178 
00179   connect(m_ActionAcquireData,      SIGNAL(triggered()), m_Acquisition, SLOT(acquireData()));
00180   connect(m_ActionAcquireReference, SIGNAL(triggered()), m_Acquisition, SLOT(acquireReference()));
00181   connect(m_ActionAcquireDark,      SIGNAL(triggered()), m_Acquisition, SLOT(acquireDark()));
00182   connect(m_ActionAcquireOnce,      SIGNAL(triggered()), m_Acquisition, SLOT(acquireOnce()));
00183   connect(m_ActionAcquireScope,     SIGNAL(triggered()), m_Acquisition, SLOT(acquireScope()));
00184   connect(m_ActionAcquireCancel,    SIGNAL(triggered()), this, SLOT(acquireCancel()));
00185 
00186   connect(m_Acquisition,         SIGNAL(acquisitionStarted()), this, SLOT(onAcquisitionStarted()));
00187   connect(m_Acquisition,         SIGNAL(acquisitionCancelled()), this, SLOT(onAcquisitionCancelled()));
00188   connect(m_Acquisition,         SIGNAL(acquisitionProgress(double)), this, SLOT(onAcquisitionProgress(double)));
00189   connect(m_Acquisition,         SIGNAL(acquisitionCompleted()), this, SLOT(onAcquisitionCompleted()));
00190   connect(m_AcquireDockWidget->m_SetReferenceButton,     SIGNAL(clicked()), m_Acquisition, SLOT(setReferenceData()));
00191   connect(m_AcquireDockWidget->m_SetDarkButton,          SIGNAL(clicked()), m_Acquisition, SLOT(setDarkData()));
00192 
00193   m_DisplayXUnits        .linkTo(m_ViewDockWidget->m_DisplayXUnits);
00194   m_DisplayXRange        .linkTo(m_ViewDockWidget->m_DisplayXRange);
00195   m_DisplayOrbit         .linkTo(m_ViewDockWidget->m_DisplayOrbit);
00196   m_DisplayBunch         .linkTo(m_ViewDockWidget->m_DisplayBunch);
00197 //
00198 //  m_DisplayXUnits.setDebug(true);
00199 //  m_DisplayXRange.setDebug(true);
00200 //  m_DisplayOrbit.setDebug(true);
00201 //  m_DisplayBunch.setDebug(true);
00202 
00203   connect(m_ViewDockWidget->m_DisplayXUnits,  SIGNAL(currentIndexChanged(int)), this, SLOT(viewScalingChanged()));
00204   connect(m_ViewDockWidget->m_DisplayXRange,  SIGNAL(currentIndexChanged(int)), this, SLOT(viewParametersChanged()));
00205   connect(m_ViewDockWidget->m_DisplayOrbit,   SIGNAL(valueChanged(int)), this, SLOT(viewParametersChanged()));
00206   connect(m_ViewDockWidget->m_DisplayBunch,   SIGNAL(valueChanged(int)), this, SLOT(viewParametersChanged()));
00207 
00208   connect(m_ViewDockWidget->m_AutoScale,       SIGNAL(clicked()), this, SLOT(autoScale()));
00209 
00210   QGridLayout * grid = new QGridLayout(m_ViewDockWidget->m_GraphChooser);
00211 
00212   grid -> addWidget(new QLabel("Chn"), 0,0, Qt::AlignHCenter);
00213   grid -> addWidget(new QLabel("Dat"), 0,1+DataSignal, Qt::AlignHCenter);
00214   grid -> addWidget(new QLabel("Raw"), 0,1+RawSignal, Qt::AlignHCenter);
00215   grid -> addWidget(new QLabel("Drk"), 0,1+DarkSignal, Qt::AlignHCenter);
00216   grid -> addWidget(new QLabel("Ref"), 0,1+RefSignal, Qt::AlignHCenter);
00217   grid -> addWidget(new QLabel("Fit"), 0,1+FitSignal, Qt::AlignHCenter);
00218 
00219   int nchan = m_Acquisition -> get_NChannels();
00220 
00221   if (nchan < 4) nchan = 4;
00222 
00223   QSignalMapper *mapper = new QSignalMapper(this);
00224 
00225   for (int i=0; i<nchan; i++) {
00226     grid -> addWidget(new QLabel(tr("ch%1").arg(i)), i+1,0, Qt::AlignHCenter);
00227 
00228     QCheckBox *datCkb = new QCheckBox();
00229     QCheckBox *rawCkb = new QCheckBox();
00230     QCheckBox *drkCkb = new QCheckBox();
00231     QCheckBox *refCkb = new QCheckBox();
00232     QCheckBox *fitCkb = new QCheckBox();
00233 
00234     datCkb -> setToolTip(tr("Display (Dark Subtracted) Data for channel %1").arg(i));
00235     rawCkb -> setToolTip(tr("Display Raw Data for channel %1").arg(i));
00236     drkCkb -> setToolTip(tr("Display Dark Data for channel %1").arg(i));
00237     refCkb -> setToolTip(tr("Display Reference Data for channel %1").arg(i));
00238     fitCkb -> setToolTip(tr("Display (Dark Subtracted) Fitted Data for channel %1").arg(i));
00239 
00240     grid -> addWidget(datCkb, i+1,1+DataSignal, Qt::AlignHCenter);
00241     grid -> addWidget(rawCkb, i+1,1+RawSignal, Qt::AlignHCenter);
00242     grid -> addWidget(drkCkb, i+1,1+DarkSignal, Qt::AlignHCenter);
00243     grid -> addWidget(refCkb, i+1,1+RefSignal, Qt::AlignHCenter);
00244     grid -> addWidget(fitCkb, i+1,1+FitSignal, Qt::AlignHCenter);
00245 
00246     connect(datCkb, SIGNAL(clicked()), mapper, SLOT(map()));
00247     connect(rawCkb, SIGNAL(clicked()), mapper, SLOT(map()));
00248     connect(drkCkb, SIGNAL(clicked()), mapper, SLOT(map()));
00249     connect(refCkb, SIGNAL(clicked()), mapper, SLOT(map()));
00250     connect(fitCkb, SIGNAL(clicked()), mapper, SLOT(map()));
00251 
00252     m_Acquisition -> fitter(i) -> prop_DisplayData() -> linkTo(datCkb);
00253     m_Acquisition -> fitter(i) -> prop_DisplayRaw()  -> linkTo(rawCkb);
00254     m_Acquisition -> fitter(i) -> prop_DisplayReference() -> linkTo(refCkb);
00255     m_Acquisition -> fitter(i) -> prop_DisplayDark() -> linkTo(drkCkb);
00256     m_Acquisition -> fitter(i) -> prop_DisplayFit() -> linkTo(fitCkb);
00257 
00258     mapper -> setMapping(datCkb, i*5+DataSignal);
00259     mapper -> setMapping(rawCkb, i*5+RawSignal);
00260     mapper -> setMapping(drkCkb, i*5+DarkSignal);
00261     mapper -> setMapping(refCkb, i*5+RefSignal);
00262     mapper -> setMapping(fitCkb, i*5+FitSignal);
00263 
00264     m_CheckBoxes.append(datCkb);
00265     m_CheckBoxes.append(rawCkb);
00266     m_CheckBoxes.append(drkCkb);
00267     m_CheckBoxes.append(refCkb);
00268     m_CheckBoxes.append(fitCkb);
00269   }
00270 
00271   connect(mapper, SIGNAL(mapped(int)), this, SLOT(chooserClicked(int)));
00272 
00273   QGridLayout *timingsGrid = new QGridLayout(m_TimingDockWidget->m_ChannelTimingsPanel);
00274 
00275   timingsGrid -> addWidget(new QLabel("Chan"), 0, 0, Qt::AlignHCenter);
00276   timingsGrid -> addWidget(new QLabel("Offset"), 0, 1, Qt::AlignHCenter);
00277   timingsGrid -> addWidget(new QLabel("Start"), 0, 2, Qt::AlignHCenter);
00278   timingsGrid -> addWidget(new QLabel("End"), 0, 3, Qt::AlignHCenter);
00279 
00280   for (int i=0; i<nchan; i++) {
00281     timingsGrid -> addWidget(new QLabel(tr("ch%1").arg(i)), i+1,0, Qt::AlignHCenter);
00282     QDoubleSpinBox *offset = new QDoubleSpinBox();
00283     QSpinBox *start = new QSpinBox();
00284     QSpinBox *end = new QSpinBox();
00285 
00286     offset -> setMinimum(-5.0);
00287     offset -> setMaximum(5.0);
00288     offset -> setSingleStep(0.01);
00289     offset -> setDecimals(3);
00290     offset -> setToolTip(tr("Set (software) fitting offset for channel %1 (in Volts)").arg(i));
00291 
00292     start  -> setMinimum(0);
00293     start  -> setMaximum(10000);
00294     start  -> setToolTip(tr("Set start of fitting region (in samples) for channel %1").arg(i));
00295 
00296     end    -> setMinimum(0);
00297     end    -> setMaximum(10000);
00298     end    -> setToolTip(tr("Set end of fitting region (in samples) for channel %1").arg(i));
00299 
00300     timingsGrid -> addWidget(offset, i+1, 1, Qt::AlignRight);
00301     timingsGrid -> addWidget(start,  i+1, 2, Qt::AlignRight);
00302     timingsGrid -> addWidget(end,    i+1, 3, Qt::AlignRight);
00303 
00304     m_Acquisition -> fitter(i) -> prop_FittingOffset() -> linkTo(offset);
00305     m_Acquisition -> fitter(i) -> prop_FittingStart()  -> linkTo(start);
00306     m_Acquisition -> fitter(i) -> prop_FittingEnd()    -> linkTo(end);
00307   }
00308 
00309   QGridLayout *gainsGrid = new QGridLayout(m_InputsDockWidget->m_InputGainsPanel);
00310 
00311   gainsGrid -> addWidget(new QLabel("Chan"), 0, 0, Qt::AlignHCenter);
00312   gainsGrid -> addWidget(new QLabel("Gain"), 0, 1, Qt::AlignHCenter);
00313   gainsGrid -> addWidget(new QLabel("Offset"), 0, 2, Qt::AlignHCenter);
00314 
00315   for (int i=0; i<nchan; i++) {
00316     gainsGrid -> addWidget(new QLabel(tr("ch%1").arg(i)), i+1,0, Qt::AlignHCenter);
00317     QComboBox *gain = new QComboBox();
00318     gain -> setEditable(false);
00319     gain -> addItem("50 mV", 0.05);
00320     gain -> addItem("100 mV", 0.1);
00321     gain -> addItem("200 mV", 0.2);
00322     gain -> addItem("500 mV", 0.5);
00323     gain -> addItem("1 V", 1.0);
00324     gain -> addItem("2 V", 2.0);
00325     gain -> addItem("5 V", 5.0);
00326     gain -> setToolTip(tr("Set input gain (full scale) for channel %1").arg(i));
00327 
00328     QDoubleSpinBox *offset = new QDoubleSpinBox();
00329     offset -> setMinimum(-5);
00330     offset -> setMaximum(5);
00331     offset -> setDecimals(3);
00332     offset -> setSingleStep(0.01);
00333     offset -> setToolTip(tr("Set input (hardware) offset for channel %1").arg(i));
00334 
00335     gainsGrid -> addWidget(gain, i+1, 1, Qt::AlignRight);
00336     gainsGrid -> addWidget(offset, i+1, 2, Qt::AlignRight);
00337 
00338     m_Acquisition -> fitter(i) -> prop_InputGain()   -> linkTo(gain);
00339     m_Acquisition -> fitter(i) -> prop_InputOffset() -> linkTo(offset);
00340   }
00341 
00342   m_Acquisition -> prop_IntegrationTime()      -> linkTo(m_AcquireDockWidget->m_IntegrationTime);
00343   m_Acquisition -> prop_ScopeIntegrationTime() -> linkTo(m_AcquireDockWidget->m_ScopeIntegrationTime);
00344   m_Acquisition -> prop_NRepeats()             -> linkTo(m_AcquireDockWidget->m_NRepeats);
00345   m_Acquisition -> prop_DarkNRepeats()         -> linkTo(m_AcquireDockWidget->m_DarkNRepeats);
00346   m_Acquisition -> prop_ReferenceNRepeats()    -> linkTo(m_AcquireDockWidget->m_ReferenceNRepeats);
00347   m_Acquisition -> prop_BucketsPerOrbit()      -> linkTo(m_TimingDockWidget->m_BucketsPerOrbit);
00348   m_Acquisition -> prop_SamplesPerOrbit()      -> linkTo(m_TimingDockWidget->m_SamplesPerOrbit);
00349   m_Acquisition -> prop_FillPatternMode()      -> linkTo(m_TimingDockWidget->m_FillPatternMode);
00350   m_Acquisition -> prop_TriggerLevel()         -> linkTo(m_InputsDockWidget->m_TriggerLevel);
00351   m_Acquisition -> prop_TriggerSlope()         -> linkTo(m_InputsDockWidget->m_TriggerSlope);
00352   m_Acquisition -> prop_TriggerSource()        -> linkTo(m_InputsDockWidget->m_TriggerSource);
00353   m_Acquisition -> prop_OutputDirectory()      -> linkTo(m_OutputDockWidget->m_OutputDirectory);
00354   m_Acquisition -> prop_FileName()             -> linkTo(m_OutputDockWidget->m_FileName);
00355   m_Acquisition -> prop_ScanNumber()           -> linkTo(m_OutputDockWidget->m_ScanNumber);
00356   m_Acquisition -> prop_NSamples()             -> linkTo(m_TimingDockWidget->m_SamplesToAcquire);
00357   m_Acquisition -> prop_NChannels()            -> linkTo(m_TimingDockWidget->m_ChannelsToAcquire);
00358   m_Acquisition -> prop_LaserRepRate()         -> linkTo(m_TimingDockWidget->m_LaserRepRate);
00359   m_Acquisition -> prop_CustomFilledBucketsPerOrbit() -> linkTo(m_TimingDockWidget->m_NFilledBuckets);
00360 
00361   m_TimingDockWidget->m_SamplesToAcquire -> setMinimum(0);
00362   m_TimingDockWidget->m_SamplesToAcquire -> setMaximum(m_Acquisition -> def_NSamples());
00363 
00364   m_TimingDockWidget->m_ChannelsToAcquire -> setMaximum(m_Acquisition -> def_NChannels());
00365 
00366   connect(m_Acquisition -> prop_FillPatternMode(),  SIGNAL(valueChanged(int,int)), this, SLOT(fillPatternModeChanged()));
00367   connect(m_Acquisition -> prop_CustomFilledBucketsPerOrbit(),  SIGNAL(valueChanged(int,int)), this, SLOT(fillPatternModeChanged()));
00368 
00369   connect(m_TimingDockWidget->m_TimingResetDefaults, SIGNAL(clicked()), m_Acquisition, SLOT(timingResetDefaults()));
00370   connect(m_InputsDockWidget->m_TriggerResetDefaults, SIGNAL(clicked()), m_Acquisition, SLOT(triggerResetDefaults()));
00371 
00372   onAcquisitionCompleted();
00373   fillPatternModeChanged();
00374 }
00375 
00376 void QavrgWindow::setScriptingEngine(QavrgScriptingEngine *engine)
00377 {
00378   m_ScriptingEngine = engine;
00379 }
00380 
00381 void QavrgWindow::fillPatternModeChanged()
00382 {
00383 //  printf("QavrgWindow::fillPatternModeChanged()\n");
00384 
00385   int newMode = m_Acquisition -> get_FillPatternMode();
00386 
00387   if (newMode == 3) {
00388     m_TimingDockWidget->m_NFilledBuckets -> setEnabled(true);
00389   } else {
00390     m_TimingDockWidget->m_NFilledBuckets -> setEnabled(false);
00391   }
00392 
00393   m_Acquisition -> fillPatternChanged();
00394 }
00395 
00396 void QavrgWindow::acquireCancel()
00397 {
00398   m_Acquisition -> acquireCancel();
00399 }
00400 
00401 void QavrgWindow::onAcquisitionStarted()
00402 {
00403   m_AcquireDockWidget->m_AcquireDataButton -> setEnabled(false);
00404   m_AcquireDockWidget->m_AcquireReferenceButton -> setEnabled(false);
00405   m_AcquireDockWidget->m_AcquireDarkButton -> setEnabled(false);
00406   m_AcquireDockWidget->m_AcquireOnceButton -> setEnabled(false);
00407   m_AcquireDockWidget->m_AcquireScopeButton -> setEnabled(false);
00408   m_AcquireDockWidget->m_AcquireCancelButton -> setEnabled(true);
00409 
00410   m_ActionAcquireData -> setEnabled(false);
00411   m_ActionAcquireReference -> setEnabled(false);
00412   m_ActionAcquireDark -> setEnabled(false);
00413   m_ActionAcquireOnce -> setEnabled(false);
00414   m_ActionAcquireScope -> setEnabled(false);
00415   m_ActionAcquireCancel -> setEnabled(true);
00416 
00417   displayStatusMessage("Acquisition Started...", m_Acquisition -> get_IntegrationTime()+5);
00418 
00419   m_Progress -> reset();
00420   m_Progress -> setRange(0,100);
00421   m_Progress -> show();
00422 }
00423 
00424 void QavrgWindow::onAcquisitionCancelled()
00425 {
00426   displayStatusMessage("Acquisition Cancelled...", m_Acquisition -> get_IntegrationTime()+5);
00427 }
00428 
00429 void QavrgWindow::onAcquisitionProgress(double pct)
00430 {
00431   if (pct < 0) {
00432     m_Progress -> setRange(0,0);
00433   } else {
00434     m_Progress -> setRange(0,100);
00435     m_Progress -> setValue(pct);
00436   }
00437 
00438   displayStatusMessage("Acquisition In Progress...", m_Acquisition -> get_IntegrationTime()+5);
00439 }
00440 
00441 void QavrgWindow::onAcquisitionCompleted()
00442 {
00443   m_AcquireDockWidget->m_AcquireDataButton -> setEnabled(true);
00444   m_AcquireDockWidget->m_AcquireReferenceButton -> setEnabled(true);
00445   m_AcquireDockWidget->m_AcquireDarkButton -> setEnabled(true);
00446   m_AcquireDockWidget->m_AcquireOnceButton -> setEnabled(true);
00447   m_AcquireDockWidget->m_AcquireScopeButton -> setEnabled(true);
00448   m_AcquireDockWidget->m_AcquireCancelButton -> setEnabled(false);
00449 
00450   m_ActionAcquireData -> setEnabled(true);
00451   m_ActionAcquireReference -> setEnabled(true);
00452   m_ActionAcquireDark -> setEnabled(true);
00453   m_ActionAcquireOnce -> setEnabled(true);
00454   m_ActionAcquireScope -> setEnabled(true);
00455   m_ActionAcquireCancel -> setEnabled(false);
00456 
00457   displayStatusMessage("Acquisition Completed...", 5);
00458 
00459   m_Progress -> reset();
00460   m_Progress -> hide();
00461 }
00462 
00463 void QavrgWindow::showDeviceError()
00464 {
00465 //  printf ("QavrgWindow::showDeviceError()\n");
00466 //
00467   QMessageBox::warning(this, "No Averager Available",
00468                        "No averager was found.  This is most likely caused either by there being "\
00469                        "another copy of qavrg running already, or perhaps you are running qavrg "\
00470                        "on a computer without an averager card.\n"\
00471                        "The program will continue in simulation mode");
00472 }
00473 
00474 void QavrgWindow::displayStatusMessage(QString msg, double time)
00475 {
00476   m_StatusMessage -> setText(msg);
00477 
00478   m_StatusTimer.start(time*1000.0);
00479 }
00480 
00481 void QavrgWindow::clearStatusMessage()
00482 {
00483   m_StatusMessage -> setText("");
00484 }
00485 
00486 void QavrgWindow::closeEvent ( QCloseEvent * event )
00487 {
00488   if (wantToClose()) {
00489     event -> accept();
00490   } else {
00491     event -> ignore();
00492   }
00493 }
00494 
00495 void QavrgWindow::possiblyClose()
00496 {
00497 //   printf("QxrdWindow::possiblyClose()\n");
00498   if (wantToClose()) {
00499     close();
00500   }
00501 }
00502 
00503 bool QavrgWindow::wantToClose()
00504 {
00505   return QMessageBox::question(this, tr("Really Close?"),
00506                                tr("Do you really want to close the window?"),
00507                                   QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Ok;
00508 }
00509 
00510 void QavrgWindow::doScript()
00511 {
00512   QString script = m_InputScript -> toPlainText();
00513 
00514   m_CancelScript   -> setEnabled(true);
00515   m_EvaluateScript -> setEnabled(false);
00516 
00517   emit executeCommand(script);
00518 }
00519 
00520 void QavrgWindow::cancelScript()
00521 {
00522   m_ScriptingEngine -> cancelCommand();
00523 }
00524 
00525 void QavrgWindow::finishedCommand(QScriptValue result)
00526 {
00527   if (result.isError()) {
00528     int line = result.property("lineNumber").toInteger();
00529 
00530     m_OutputResult -> append(tr("Error in line %1").arg(line));
00531     m_OutputResult -> append(result.property("message").toString());
00532   } else {
00533     m_OutputResult -> append(result.toString());
00534   }
00535 
00536   m_CancelScript   -> setEnabled(false);
00537   m_EvaluateScript -> setEnabled(true);
00538 }
00539 
00540 void QavrgWindow::printMessage(QString msg)
00541 {
00542   m_OutputResult -> append(msg);
00543 }
00544 
00545 void QavrgWindow::warningMessage(QString msg)
00546 {
00547   m_OutputResult -> append(tr("Error: %1").arg(msg));
00548 
00549   QMessageBox::warning(this, "Error", msg);
00550 }
00551 
00552 void QavrgWindow::chooserClicked(int /*n*/)
00553 {
00554   updateGraph();
00555 }
00556 
00557 
00558 void QavrgWindow::new_RawData()
00559 {
00560 //  printf("QavrgWindow::new_RawData()\n");
00561 //
00562 
00563   if (m_Acquisition -> decr_NQueuedResults() <= 1) {
00564     updateGraph();
00565 //  } else {
00566 //    printf("Skipped Update\n");
00567   }
00568 }
00569 
00570 void QavrgWindow::new_DarkData()
00571 {
00572 //  printf("QavrgWindow::new_DarkData()\n");
00573 //
00574 
00575   if (m_Acquisition -> decr_NQueuedResults() <= 1) {
00576     updateGraph();
00577 //  } else {
00578 //    printf("Skipped Update\n");
00579   }
00580 }
00581 
00582 void QavrgWindow::new_ReferenceData()
00583 {
00584 //  printf("QavrgWindow::new_ReferenceData()\n");
00585 //
00586 
00587   if (m_Acquisition -> decr_NQueuedResults() <= 1) {
00588     updateGraph();
00589 //  } else {
00590 //    printf("Skipped Update\n");
00591   }
00592 }
00593 
00594 void QavrgWindow::new_FitData()
00595 {
00596 //  printf("QavrgWindow::new_FitData()\n");
00597 //
00598 
00599   if (m_Acquisition -> decr_NQueuedResults() <= 1) {
00600     updateGraph();
00601 //  } else {
00602 //    printf("Skipped Update\n");
00603   }
00604 }
00605 
00606 void QavrgWindow::viewParametersChanged()
00607 {
00608 //  printf("QavrgWindow::viewParametersChanged()\n");
00609 //
00610   updateGraph();
00611 }
00612 
00613 void QavrgWindow::viewScalingChanged()
00614 {
00615 //  printf("QavrgWindow::viewScalingChanged()\n");
00616 //
00617   updateGraph();
00618 }
00619 
00620 QString QavrgWindow::signalName(QavrgWindow::Signal sig)
00621 {
00622   switch (sig) {
00623   case DataSignal:
00624     return "Data";
00625   case RawSignal:
00626     return "Raw";
00627   case DarkSignal:
00628     return "Dark";
00629   case RefSignal:
00630     return "Ref";
00631   case FitSignal:
00632     return "Fit";
00633   }
00634 
00635   return "Unk";
00636 }
00637 
00638 QPen QavrgWindow::curvePen(int ch)
00639 {
00640   QPen pen1(Qt::red);
00641   QPen pen2(Qt::green);
00642   QPen pen3(Qt::blue);
00643   QPen pen4(Qt::magenta);
00644 
00645   switch (ch & 3) {
00646   case 0:
00647     return pen1;
00648     break;
00649   case 1:
00650     return pen2;
00651     break;
00652   case 2:
00653     return pen3;
00654     break;
00655   default:
00656     return pen4;
00657     break;
00658   }
00659 
00660   return pen1;
00661 }
00662 
00663 void QavrgWindow::updateGraph()
00664 {
00665 //  printf("updateGraph\n");
00666 
00667   QTime tm;
00668   tm.start();
00669 
00670   int nSamples                   = m_Acquisition -> get_NSamples();
00671   int nChans                     = m_Acquisition -> get_NChannels();
00672   double nSamplesPerOrbit        = m_Acquisition -> get_SamplesPerOrbit();
00673 
00674   if (nSamplesPerOrbit <= 0) nSamplesPerOrbit = 3683.5;
00675 
00676   int nOrbits                    = nSamples / nSamplesPerOrbit;
00677   int nFilledBuckets             = m_Acquisition -> get_FilledBucketsPerOrbit();
00678 
00679   if (nFilledBuckets <= 0) nFilledBuckets = 24;
00680 
00681   double nSamplesPerFilledBucket = nSamplesPerOrbit / nFilledBuckets;
00682 
00683   int viewChoice                 = get_DisplayXRange();
00684   int xunits                     = get_DisplayXUnits();
00685   int viewOrbit                  = get_DisplayOrbit();
00686   int viewBunch                  = get_DisplayBunch();
00687 
00688   m_ViewDockWidget->m_DisplayOrbit -> setMaximum(nOrbits);
00689   m_ViewDockWidget->m_DisplayBunch -> setMinimum(-4);
00690   m_ViewDockWidget->m_DisplayBunch -> setMaximum(nFilledBuckets+4);
00691 
00692   switch (viewChoice) {
00693   case ViewAllData:
00694     m_ViewDockWidget->m_DisplayOrbit -> setEnabled(false);
00695     m_ViewDockWidget->m_DisplayBunch -> setEnabled(false);
00696     break;
00697 
00698   case ViewThreeOrbits:
00699     m_ViewDockWidget->m_DisplayOrbit -> setEnabled(true);
00700     m_ViewDockWidget->m_DisplayBunch -> setEnabled(false);
00701     break;
00702 
00703   case ViewOneOrbit:
00704     m_ViewDockWidget->m_DisplayOrbit -> setEnabled(true);
00705     m_ViewDockWidget->m_DisplayBunch -> setEnabled(false);
00706     break;
00707 
00708   case ViewOneBunch:
00709     m_ViewDockWidget->m_DisplayOrbit -> setEnabled(true);
00710     m_ViewDockWidget->m_DisplayBunch -> setEnabled(true);
00711     break;
00712   };
00713 
00714   //   printf("Update graph\n");
00715 
00716 //  QVector< QVector<double> > data = m_Acquisition->get_RawData();
00717 //  QVector< QVector<double> > ref  = m_Acquisition->get_ReferenceData();
00718 //  QVector< QVector<double> > dark = m_Acquisition->get_DarkData();
00719 //  QVector< QVector<double> > fit  = m_Acquisition->get_FitData();
00720 
00721 //  for (int i=0; i<10; i++) {
00722 //    printf("%g  ", data.value(0).value(i));
00723 //  }
00724 //  printf("%g\n", data.value(0).value(10));
00725 
00726   const char *xlabel; double xstep;
00727   int  nSamplesViewed = 0, firstSampleViewed = 0, useChannelOffsets = false;
00728 
00729   switch (xunits) {
00730   case NanosecondUnits:
00731   default:
00732     xstep = 1.0;
00733     xlabel = "Time (nsec)";
00734     break;
00735 
00736   case MicrosecondUnits:
00737     xstep = 1.0e-3;
00738     xlabel = "Time (usec)";
00739     break;
00740 
00741   case MillisecondUnits:
00742     xstep = 1.0e-6;
00743     xlabel = "Time (msec)";
00744     break;
00745 
00746   case OrbitUnits:
00747     xstep = 1.0/nSamplesPerOrbit;
00748     xlabel = "Orbit Number";
00749     break;
00750 
00751   case FilledBunchUnits:
00752     xstep = 1.0/nSamplesPerFilledBucket;
00753     xlabel = "Bunch Number";
00754     break;
00755   }
00756 
00757   m_Plot -> detachItems(QwtPlotItem::Rtti_PlotCurve);
00758   m_Plot -> detachItems(QwtPlotItem::Rtti_PlotMarker);
00759   m_Plot -> setAxisTitle(QwtPlot::xBottom, xlabel);
00760 
00761   switch (viewChoice) {
00762   case ViewAllData: // View all data
00763     {
00764       nSamplesViewed    = nSamples;
00765       firstSampleViewed = 0;
00766       useChannelOffsets = false;
00767     }
00768     break;
00769 
00770   case ViewThreeOrbits: // View one orbit
00771     {
00772       nSamplesViewed    = (int) (nSamplesPerOrbit*3);
00773       firstSampleViewed = (int) (viewOrbit*nSamplesPerOrbit);
00774       useChannelOffsets = false;
00775     }
00776     break;
00777 
00778   case ViewOneOrbit: // View one orbit
00779     {
00780       nSamplesViewed    = (int) (nSamplesPerOrbit);
00781       firstSampleViewed = (int) (viewOrbit*nSamplesPerOrbit);
00782       useChannelOffsets = false;
00783     }
00784     break;
00785 
00786   case ViewOneBunch: // View one bunch
00787     {
00788       nSamplesViewed    = (int) (nSamplesPerFilledBucket);
00789       firstSampleViewed = (int) (viewOrbit*nSamplesPerOrbit + viewBunch*nSamplesPerFilledBucket);
00790       useChannelOffsets = true;
00791     }
00792     break;
00793   }
00794 
00795   QVector<double> timeb(nSamplesViewed);
00796   QVector<double> ydata(nSamplesViewed);
00797 
00798   for (int i=0; i<nSamplesViewed; i++) {
00799     timeb[i] = i*xstep;
00800   }
00801 
00802   for (int ch = 0; ch < nChans; ch++) {
00803     QVector<double> data = m_Acquisition->fitter(ch)->get_RawData();
00804     QVector<double> ref  = m_Acquisition->fitter(ch)->get_ReferenceData();
00805     QVector<double> dark = m_Acquisition->fitter(ch)->get_DarkData();
00806     QVector<double> fit  = m_Acquisition->fitter(ch)->get_FitData();
00807 
00808     for (int sig = DataSignal; sig <= FitSignal; sig++) {
00809       QCheckBox *cb = m_CheckBoxes.value(ch*5+sig);
00810       if (cb && cb->isChecked()) {
00811         QwtPlotCurve *crv = new QwtPlotCurve(tr("Ch%1%2").arg(ch).arg(signalName((Signal) sig)));
00812 
00813         int firstSample = firstSampleViewed;
00814         int nSamples    = nSamplesViewed;
00815 
00816         if (useChannelOffsets) {
00817           int start = m_Acquisition -> fitter(ch) -> get_FittingStart();
00818           int end   = m_Acquisition -> fitter(ch) -> get_FittingEnd();
00819 
00820           firstSample += start;
00821           nSamples     = end - start;
00822 
00823           if (nSamples < 0) {
00824             nSamples = 0;
00825           }
00826 
00827           timeb.resize(nSamples);
00828           ydata.resize(nSamples);
00829 
00830           for (int i=0; i<nSamples; i++) {
00831             timeb[i] = (i+start)*xstep;
00832           }
00833         }
00834 
00835         QPen pen = curvePen(ch);
00836         QwtSymbol *symbol = new QwtSymbol();
00837         QwtPlotCurve::CurveStyle style = QwtPlotCurve::Lines;
00838 
00839         switch (sig) {
00840         case DataSignal:
00841           if (m_Acquisition->fitter(ch)->get_DarkAvailable()) {
00842             for (int i=0; i<nSamples; i++) {
00843               ydata[i] = data.value(firstSample+i) - dark.value(firstSample+i);
00844             }
00845           } else {
00846             for (int i=0; i<nSamples; i++) {
00847               ydata[i] = data.value(firstSample+i);
00848             }
00849           }
00850           if (nSamples < 12000) {
00851             symbol->setStyle(QwtSymbol::Ellipse);
00852             symbol->setSize(7,7);
00853             symbol->setPen(pen);
00854             symbol->setBrush(QBrush(pen.color()));
00855             style = QwtPlotCurve::NoCurve;
00856           }
00857           break;
00858 
00859         case RawSignal:
00860           for (int i=0; i<nSamples; i++) {
00861             ydata[i] = data.value(firstSample+i);
00862           }
00863           if (nSamples < 12000) {
00864             symbol->setStyle(QwtSymbol::Ellipse);
00865             symbol->setSize(5,5);
00866             symbol->setBrush(QBrush(pen.color()));
00867             symbol->setPen(pen);
00868             style = QwtPlotCurve::NoCurve;
00869           } else {
00870             pen.setStyle(Qt::DotLine);
00871           }
00872           break;
00873 
00874         case DarkSignal:
00875           for (int i=0; i<nSamples; i++) {
00876             ydata[i] = dark.value(firstSample+i);
00877           }
00878           pen.setStyle(Qt::DashDotDotLine);
00879           break;
00880 
00881         case RefSignal:
00882           for (int i=0; i<nSamples; i++) {
00883             ydata[i] = ref.value(firstSample+i);
00884           }
00885           pen.setStyle(Qt::DashLine);
00886           break;
00887 
00888         case FitSignal:
00889           for (int i=0; i<nSamples; i++) {
00890             ydata[i] = fit.value(firstSample+i);
00891           }
00892           pen.setStyle(Qt::DashDotLine);
00893           break;
00894         }
00895 
00896         crv -> setSamples(timeb.data(), ydata.data(), nSamples);
00897         crv -> setPen(pen);
00898         crv -> setSymbol(symbol);
00899         crv -> setStyle(style);
00900         crv -> attach(m_Plot);
00901       }
00902     }
00903   }
00904 
00905   if (xunits != m_XUnits) {
00906     // If the x scaling changes, go and update the coordinates in the zoom stack...
00907     double ratio = xstep/m_XStep;
00908 
00909     for (int i=0; i<m_Zoomer->zoomStack().count(); i++) {
00910       QRectF &zr = const_cast<QRectF&>(m_Zoomer->zoomStack()[i]);
00911 
00912       zr.setLeft(zr.left()*ratio);
00913       zr.setRight(zr.right()*ratio);
00914     }
00915 
00916     m_XUnits = xunits;
00917     m_XStep = xstep;
00918 
00919     m_Zoomer->zoom(m_Zoomer -> zoomRectIndex());
00920   }
00921 
00922   if (m_Zoomer -> zoomRectIndex() == 0) {
00923     m_Plot -> setAxisAutoScale(QwtPlot::xBottom);
00924     m_Plot -> setAxisAutoScale(QwtPlot::yLeft);
00925   }
00926 
00927   m_Plot->replot();
00928 
00929   if (m_Zoomer -> zoomRectIndex() == 0) {
00930     m_Zoomer -> setZoomBase();
00931   }
00932 
00933   emit printMessage(tr("updateGraph took %1 msec\n").arg(tm.restart()));
00934 }
00935 
00936 void QavrgWindow::setZoomingMode()
00937 {
00938   m_Zoomer    -> setEnabled(true);
00939   m_Magnifier -> setEnabled(true);
00940   m_Panner    -> setEnabled(true);
00941   m_Measurer  -> setEnabled(false);
00942 }
00943 
00944 void QavrgWindow::zoomOut()
00945 {
00946   m_Zoomer -> zoom(-1);
00947 }
00948 
00949 void QavrgWindow::zoomAll()
00950 {
00951   autoScale();
00952 }
00953 
00954 void QavrgWindow::setMeasuringMode()
00955 {
00956   m_Zoomer    -> setEnabled(false);
00957   m_Magnifier -> setEnabled(true);
00958   m_Panner    -> setEnabled(true);
00959   m_Measurer  -> setEnabled(true);
00960 }
00961 
00962 void QavrgWindow::autoScale()
00963 {
00964   m_Plot -> setAxisAutoScale(QwtPlot::xBottom);
00965   m_Plot -> setAxisAutoScale(QwtPlot::yLeft);
00966   
00967   m_Plot->replot();
00968 
00969   m_Zoomer -> setZoomBase();
00970 }
00971 
00972 void QavrgWindow::readSettings(QavrgSettings *settings)
00973 {
00974   QcepProperty::readSettings(this, &QavrgWindow::staticMetaObject, "display", settings);
00975 
00976   QByteArray geometry = settings->value("window/geometry").toByteArray();
00977   QByteArray winstate = settings->value("window/state").toByteArray();
00978 
00979   restoreGeometry(geometry);
00980   restoreState(winstate,1);
00981 }
00982 
00983 void QavrgWindow::writeSettings(QavrgSettings *settings)
00984 {
00985   QcepProperty::writeSettings(this, &QavrgWindow::staticMetaObject, "display", settings);
00986 
00987   settings->setValue("window/geometry", saveGeometry());
00988   settings->setValue("window/state", saveState(1));
00989 }
00990 
00991 void QavrgWindow::loadData()
00992 {
00993   QString loadName = QFileDialog::getOpenFileName(this, tr("Load Acquisitions"));
00994 
00995   if (!loadName.isNull()) {
00996     m_Acquisition -> loadData(loadName);
00997   }
00998 }
00999 
01000 void QavrgWindow::saveData()
01001 {
01002   QString saveName = QFileDialog::getSaveFileName(this, tr("Save Acquisitions"));
01003 
01004   if (!saveName.isNull()) {
01005     m_Acquisition -> saveData(saveName);
01006   }
01007 }
01008 
01009 void QavrgWindow::browseOutputDirectory()
01010 {
01011   QString directory = m_Acquisition -> get_OutputDirectory();
01012 
01013   QString newDirectory = QFileDialog::getExistingDirectory(this, "Output Directory", directory);
01014 
01015   if (!newDirectory.isNull()) {
01016     m_Acquisition -> set_OutputDirectory(newDirectory);
01017   }
01018 }
01019 
01020 void QavrgWindow::specialSecretTest(int useProxy, int useTransaction)
01021 {
01022   if (QThread::currentThread() == thread()) {
01023     QavrgTestWindow *w = new QavrgTestWindow(useProxy, useTransaction);
01024 
01025     w -> show();
01026   } else {
01027     QMetaObject::invokeMethod(this, "specialSecretTest", Q_ARG(int, useProxy), Q_ARG(int, useTransaction));
01028   }
01029 }