QXRD  0.11.16
qxrddetectorsimulated.cpp
Go to the documentation of this file.
2 #include "qxrdexperiment.h"
3 #include "qxrdacquisition.h"
4 #include "qcepallocator.h"
5 #include "qxrddebug.h"
6 
7 #include <stdio.h>
8 #include <QPainter>
9 
11  QxrdExperimentWPtr expt,
13  int detNum,
14  QcepObject *parent) :
15  QxrdDetector(saver, expt, acq, QxrdDetectorThread::SimulatedDetector, detNum, parent)
16 {
18  printf("QxrdDetectorSimulated::QxrdDetectorSimulated(%p)\n", this);
19  }
20 
21  connect(&m_Timer, &QTimer::timeout, this, &QxrdDetectorSimulated::onTimerTimeout);
22 }
23 
25 {
26 #ifndef QT_NO_DEBUG
27  printf("Deleting simulated detector\n");
28 #endif
29 
31  printf("QxrdDetectorSimulated::~QxrdDetectorSimulated(%p)\n", this);
32  }
33 }
34 
36 {
38 
39 // printf("Need to implement QxrdDetectorSimulated::pushDefaultsToProxy\n");
40 }
41 
43 {
45 
46 // printf("Need to implement QxrdDetectorSimulated::pushPropertiesToProxy\n");
47 }
48 
50 {
52 
53 // printf("Need to implement QxrdDetectorSimulated::pullPropertiesfromProxy\n");
54 }
55 
57 {
58  if (QThread::currentThread() != thread()) {
59  QMetaObject::invokeMethod(this, "onExposureTimeChanged");
60  } else {
63 
64  if (isEnabled() && acq && expt) {
65  double newTime = acq->get_ExposureTime();
66 
67  expt->printMessage(tr("Exposure time changed to %1").arg(newTime));
68 
69  m_Timer.start(newTime*1000);
70  }
71  }
72 }
73 
74 //void QxrdDetectorSimulated::setupExposureMenu(QDoubleSpinBox * /*cb*/, double /*initialExposure*/)
75 //{
86 //}
87 
89 {
90  if (QThread::currentThread() != thread()) {
91  QMetaObject::invokeMethod(this, "startDetector", Qt::BlockingQueuedConnection);
92  } else {
94 
96 
97  if (checkDetectorEnabled() && acq) {
98  printMessage(tr("Starting simulated detector \"%1\"").arg(get_DetectorName()));
99 
100  set_NRows(2048);
101  set_NCols(2048);
102 
103  if (acq->get_ExposureTime() <= 0) {
104  acq->set_ExposureTime(0.1);
105  }
106 
108  }
109  }
110 }
111 
113 {
114  if (QThread::currentThread() != thread()) {
115  QMetaObject::invokeMethod(this, "startDetector", Qt::BlockingQueuedConnection);
116  } else {
117  printMessage(tr("Stopping simulated detector \"%1\"").arg(get_DetectorName()));
118 
120 
121  m_Timer.stop();
122  }
123 }
124 
125 static int frameCounter = 0;
126 
128 {
129  if (QThread::currentThread() != thread()) {
130  QMetaObject::invokeMethod(this, "beginAcquisition", Qt::BlockingQueuedConnection, Q_ARG(double, exposure));
131  } else {
133 
134  frameCounter = 0;
135  }
136 }
137 
139 {
140  if (QThread::currentThread() != thread()) {
141  QMetaObject::invokeMethod(this, "endAcquisition", Qt::BlockingQueuedConnection);
142  } else {
144  }
145 }
146 
148 {
149  if (QThread::currentThread() != thread()) {
150  QMetaObject::invokeMethod(this, "shutdownAcquisition", Qt::BlockingQueuedConnection);
151  } else {
153  }
154 }
155 
157 {
159 
160  if (checkDetectorEnabled() && acq) {
161  if (acq->synchronizedAcquisition()) {
162  acq->synchronizedAcquisition()->acquiredFrameAvailable(frameCounter);
163  }
164 
165  int nRows = get_NRows();
166  int nCols = get_NCols();
167 
169  nCols, nRows, acq.data());
170  int xpmsec = (int)(acq->get_ExposureTime()*1000+0.5);
171  int frame = frameCounter % 8;
172 
173  if (image) {
174  quint16 *ptr = image->data();
175 
176  for (int j=0; j<nRows; j++) {
177  for (int i=0; i<nCols; i++) {
178  if ((i>=frame*64) && (i<(frame+1)*64) && (j < 64)) {
179  *ptr++ = frame;
180  } else {
181  *ptr++ = xpmsec;
182  }
183  }
184  }
185 
186  if ((nRows > 1024) && (nCols > 1024)) {
187  const int labelWidth = 256;
188  const int labelHeight = 64;
189 
190  QImage imageLabel(labelWidth, labelHeight, QImage::Format_RGB32);
191  QPainter painter(&imageLabel);
192 
193  painter.fillRect(0,0,labelWidth,labelHeight, Qt::black);
194  painter.setPen(Qt::white);
195  painter.setFont(QFont("Times", labelHeight, QFont::Bold, true));
196  painter.drawText(0, labelHeight, tr("%1").arg(frameCounter));
197 
198  QRgb *rgb = (QRgb*) imageLabel.bits();
199  int nFrames = nRows / labelHeight;
200  int frameN = frameCounter % nFrames;
201  int plval = qGray(*rgb);
202  int pRgb = *rgb;
203 
204  for (int j=0; j<labelHeight; j++) {
205  for (int i=0; i<labelWidth; i++) {
206  int x = nCols-labelWidth+i;
207  int y = (frameN+1)*labelHeight-j;
208  // int val = image->value(x,y);
209  int vRgb = *rgb++;
210  int lval = qGray(vRgb);
211 
212  if (lval != plval) {
213  plval = lval;
214  }
215 
216  if (vRgb != pRgb) {
217  pRgb = vRgb;
218  }
219 
220  image->setValue(x,y,lval);
221  }
222  }
223  }
224  }
225 
226  enqueueAcquiredFrame(image);
227 
228  frameCounter++;
229  }
230 }
QSharedPointer< QxrdExperiment > QxrdExperimentPtr
QSharedPointer< QxrdDetectorProxy > QxrdDetectorProxyPtr
virtual void pushPropertiesToProxy(QxrdDetectorProxyPtr proxy)
QxrdExperimentWPtr m_Experiment
Definition: qxrddetector.h:93
qint64 qcepDebug(int cond)
Definition: qcepdebug.cpp:26
QSharedPointer< QxrdAcquisition > QxrdAcquisitionPtr
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime()) const
virtual void pullPropertiesfromProxy(QxrdDetectorProxyPtr proxy)
QxrdAcquisitionWPtr m_Acquisition
Definition: qxrddetector.h:94
QWeakPointer< QxrdExperiment > QxrdExperimentWPtr
bool checkDetectorEnabled()
void enqueueAcquiredFrame(QcepImageDataBasePtr img)
virtual void onExposureTimeChanged()
QxrdDetectorSimulated(QcepSettingsSaverWPtr saver, QxrdExperimentWPtr expt, QxrdAcquisitionWPtr acq, int detNum, QcepObject *parent)
void pullPropertiesfromProxy(QxrdDetectorProxyPtr proxy)
virtual void startDetector()
void pushPropertiesToProxy(QxrdDetectorProxyPtr proxy)
void beginAcquisition(double exposure)
virtual void stopDetector()
static QcepInt16ImageDataPtr newInt16Image(AllocationStrategy strat, int width, int height, QcepObject *parent)
QWeakPointer< QxrdAcquisition > QxrdAcquisitionWPtr
static int frameCounter
static void pushDefaultsToProxy(QxrdDetectorProxyPtr proxy, int detType)
static void pushDefaultsToProxy(QxrdDetectorProxyPtr proxy)
virtual void shutdownAcquisition()
virtual void endAcquisition()
QSharedPointer< QcepInt16ImageData > QcepInt16ImageDataPtr
virtual void beginAcquisition(double exposure)
QWeakPointer< QcepSettingsSaver > QcepSettingsSaverWPtr