source: tools/ddd/Functions.cpp@ 37

Last change on this file since 37 was 34, checked in by ogrimm, 16 years ago
Slight change in graphics display
File size: 11.3 KB
Line 
1
2#include "GUI.h"
3
4//-----------------------------------------------------------------------
5//**************************** All functions ****************************
6//-----------------------------------------------------------------------
7
8// +++ Open file dialog +++
9void ddd::FileDialog(void) {
10 QString Filename = QFileDialog::getOpenFileName(this,
11 "Open raw file", INITIAL_DIRECTORY, "Raw data files (*.raw);; All files (*)");
12 if (Filename != NULL) {
13 FilenameBox->setText(Filename);
14 OpenDatafile();
15 }
16}
17
18// +++ Open selected file and read run header +++
19void ddd::OpenDatafile() {
20
21 CloseDatafile(); // Close previous file if open
22
23 // Write run header to temporary file
24 FILE *Tmpfile = tmpfile();
25 if(Tmpfile==NULL) {
26 QMessageBox::warning(this, "ddd Message","Could not open temporary file.",QMessageBox::Ok);
27 CloseDatafile();
28 return;
29 }
30
31 switch (RD->OpenDataFile(FilenameBox->text().toAscii().data(), Tmpfile)) {
32 case CTX_FOPEN: QMessageBox::warning(this, "ddd Message","Could not open file.",QMessageBox::Ok);
33 return;
34 case CTX_RHEADER: QMessageBox::warning(this, "ddd Message","Could not read run header.",QMessageBox::Ok);
35 return;
36 case CTX_BSTRUCT: QMessageBox::warning(this, "ddd Message","Could not read board structures.",QMessageBox::Ok);
37 return;
38 default: break;
39 }
40 if (RD->RHeader->MagicNum != MAGICNUM_FILE_CLOSED)
41 QMessageBox::warning(this, "ddd Message","Magic number in run header indicates that the file has not been closed properly.",QMessageBox::Ok);
42
43 rewind(Tmpfile);
44 QTextStream in(Tmpfile);
45 QString text = in.readAll();
46 RunHeaderDisplay->setPlainText(text);
47 fclose(Tmpfile);
48
49 // Enable spin boxes, set ranges and display first event
50 EventNo->setEnabled(true);
51 ChannelNo->setEnabled(true);
52 BoardNo->setEnabled(true);
53 PixelID->setEnabled(true);
54 EventNo->setValue(1);
55 EventNo->setRange(1, RD->RHeader->Events);
56 ChannelNo->setValue(0);
57 ChannelNo->setRange(0, RD->RHeader->NChannels*RD->RHeader->NChips-1);
58 BoardNo->setValue(0);
59 BoardNo->setRange(0, RD->RHeader->NCMCBoards-1);
60 DisplayEvent();
61}
62
63// +++ Close data file file if open, delete event header and disable spin boxes and displays +++
64void ddd::CloseDatafile() {
65 if(RD->CloseDataFile()==CTX_OK) {
66 EventNo->setEnabled(false);
67 ChannelNo->setEnabled(false);
68 BoardNo->setEnabled(false);
69 PixelID->setEnabled(false);
70 RunHeaderDisplay->clear();
71 EventHeaderDisplay->clear();
72 Signal->hide();
73 }
74}
75
76// +++ Read event header and display event (only called if Datafile is open) +++
77void ddd::DisplayEvent(int) {
78
79 PixelID->setText(PixMap->DRS_to_Pixel(BoardNo->value(),ChannelNo->value()/10,ChannelNo->value()%10).c_str());
80 if(Socket->state() == QAbstractSocket::ConnectedState) return; // do not execute if socket is open
81
82 // Read event
83 FILE *Tmpfile = tmpfile();
84
85 switch(RD->ReadEvent(EventNo->value(), Tmpfile)) {
86 case !CTX_OK:
87 QMessageBox::warning(this, "ddd Warning","Could not read event.",QMessageBox::Ok);
88 EventHeaderDisplay->clear();
89 break;
90 default:
91 rewind(Tmpfile);
92 QTextStream in(Tmpfile);
93 QString text = in.readAll();
94 EventHeaderDisplay->setPlainText(text);
95 // Case data in double format required by qwt library
96 double* x = new double [RD->RHeader->Samples];
97 double* y = new double [RD->RHeader->Samples];
98 for (unsigned int i=0; i<RD->RHeader->Samples; i++) {
99 x[i] = (double) (i/RD->BStruct[BoardNo->value()].NomFreq);
100 y[i] = (double) RD->Data[BoardNo->value()*RD->RHeader->NChips*RD->RHeader->NChannels *
101 RD->RHeader->Samples+ChannelNo->value()*RD->RHeader->Samples+i]*RD->BStruct[BoardNo->value()].ScaleFactor;
102 }
103 Signal->setData(x, y, RD->RHeader->Samples);
104 Signal->show();
105 Zoomer->setZoomBase(Signal->boundingRect());
106 delete[] x; delete[] y;
107 }
108 if(Tmpfile!=NULL) fclose(Tmpfile);
109}
110
111// +++ Open sub window handling the socket interface +++
112void ddd::OpenSocketWindow() {
113
114 if(SocketWindow->isVisible()) SocketWindow->hide();
115 else SocketWindow->show();
116}
117
118// +++ Acquire data through socket (acquire botton only available if socket exists) +++
119void ddd::GetSignalFromSocket() {
120 char Command[MAX_COM_SIZE];
121
122 GetButton->setEnabled(false);
123 WaitForData = true;
124 sprintf(Command, "read %d %d %d restart", BoardNo->value(), ChannelNo->value()/10, ChannelNo->value()%10);
125 Socket->write(Command);
126}
127
128// Quit application when clicking close button on window
129void ddd::closeEvent(QCloseEvent *) {
130 qApp->quit();
131}
132
133// +++ Connecting or disconnecting from client +++
134void ddd::MakeConnection() {
135
136 if(Socket->state() == QAbstractSocket::ConnectedState) {
137 ManualDisconnect = true;
138 Socket->disconnectFromHost();
139 }
140 else {
141 if (RD->IsFileOpen() && QMessageBox::question(this, "ddd Request","Connecting will close current data file. Proceed?",
142 QMessageBox::No, QMessageBox::Yes) != QMessageBox::Yes) return;
143 Socket->connectToHost(IPAddress->text(),Port->value());
144 Connect->setEnabled(false); // While waiting for connection, button not available
145 Socket->waitForConnected(SOCKET_TIMEOUT);
146 Connect->setEnabled(true);
147 if(Socket->state() != QAbstractSocket::ConnectedState)
148 QMessageBox::warning(this, "ddd Message","Could not connect to host.",QMessageBox::Ok);
149 else {
150 Connect->setText("Disconnect");
151 ConnectAction->setText("Disconnect");
152 Port->setEnabled(false);
153 IPAddress->setEnabled(false);
154 Command->setEnabled(true);
155 GetButton->setEnabled(true);
156 ManualDisconnect = false;
157
158 OpenAction->setEnabled(false);
159 FilenameBox->setEnabled(false);
160 LoadButton->setEnabled(false);
161 FilenameBox->clear();
162 CloseDatafile();
163
164 ChannelNo->setEnabled(true);
165 BoardNo->setEnabled(true);
166 PixelID->setEnabled(true);
167 ChannelNo->setRange(0, 65535);
168 BoardNo->setRange(0, 65535);
169
170 TabWidget->setTabEnabled(1,false);
171 TabWidget->setTabEnabled(2,false);
172
173 RunHeaderDisplay->clear();
174 EventHeaderDisplay->clear();
175 Signal->hide();
176 }
177 }
178}
179
180// +++ Send command to socket (command button available only if socket existing) +++
181void ddd::SendToSocket() {
182 Socket->write(Command->text().toAscii());
183 Command->clear();
184}
185
186// +++ Read data from socket and display +++
187void ddd::ReadFromSocket() {
188 // Check if channel data expected and error message arrived
189 QByteArray Data = Socket->readAll();
190 if (WaitForData && Data.contains("Error")) {
191 WaitForData = false;
192 GetButton->setEnabled(true);
193 QMessageBox::warning(this, "ddd Message","Could not read waveform data from socket.",QMessageBox::Ok);
194 return;
195 }
196
197 // Check if channel data were transmitted, if yes and waiting for data, extract and plot them
198 SocketOutput->insertPlainText(Data);
199 QString Text = SocketOutput->document()->toPlainText().trimmed();
200 if (WaitForData && Text.endsWith(QLatin1String("==END=="))) {
201 // Extract text between ==START== and ==END==
202 QByteArray Data=Text.mid(Text.lastIndexOf("==START==")+9,Text.length()-Text.lastIndexOf("==START==")-16).toAscii();
203
204 char *NextNumber = strtok(Data.data()," "); // Number of entries that follow
205 int Count=0, NumberOfEntries = atoi(NextNumber);
206 double *x = new double [NumberOfEntries];
207 double *y = new double [NumberOfEntries];
208
209 // Convert all entries (separated by a whitespace) to numbers
210 while((NextNumber=strtok(NULL, " "))!=NULL && Count<NumberOfEntries)
211 *(y+Count++) = atof(NextNumber);
212 if (Count==NumberOfEntries && NextNumber!=0)
213 QMessageBox::warning(this, "ddd Message","Found too many numbers in data block, truncated.",QMessageBox::Ok);
214 // Apply sampling frequency and scaling factor
215 for(int i=2; i<Count; i++) {
216 x[i] = (i-2) / y[0];
217 y[i] = y[i] * y[1];
218 }
219 if(NumberOfEntries>2) {
220 Signal->setData(x+2, y+2, NumberOfEntries-2); // Copies data, arrays can be deleted afterwards
221 Signal->show();
222 Zoomer->setZoomBase(Signal->boundingRect());
223 }
224 delete[] x; delete[] y;
225
226 if(ContinuousBox->isChecked()) {
227 usleep(100000); // Wait to limit maximum update rate
228 GetSignalFromSocket();
229 }
230 else {
231 WaitForData = false;
232 GetButton->setEnabled(true);
233 }
234 }
235}
236
237// +++ Disconnect from socket +++
238void ddd::GotDisconnected() {
239 Connect->setText("Connect");
240 ConnectAction->setText("Connect");
241 Port->setEnabled(true);
242 IPAddress->setEnabled(true);
243 Command->setEnabled(false);
244
245 GetButton->setEnabled(false);
246 FilenameBox->setEnabled(true);
247 OpenAction->setEnabled(true);
248 LoadButton->setEnabled(true);
249 ChannelNo->setEnabled(false);
250 BoardNo->setEnabled(false);
251 PixelID->setEnabled(false);
252 Signal->hide();
253 TabWidget->setTabEnabled(1, true);
254 TabWidget->setTabEnabled(2, true);
255
256 SocketOutput->clear();
257 if(!ManualDisconnect) QMessageBox::warning(this, "ddd Message","Socket disconnected, maybe host gone.",QMessageBox::Ok);
258}
259
260// +++ Translate pixel ID +++
261void ddd::TranslatePixelID() {
262
263 int Board = PixMap->Pixel_to_DRSboard(PixelID->text().toStdString());
264 int Chip = PixMap->Pixel_to_DRSchip(PixelID->text().toStdString());
265 int Channel = PixMap->Pixel_to_DRSchannel(PixelID->text().toStdString());
266
267 if(Board>=BoardNo->minimum() && Board<=BoardNo->maximum() &&
268 (Chip*10+Channel)>=ChannelNo->minimum() && (Chip*10+Channel)<=ChannelNo->maximum()) {
269 BoardNo->setValue(Board);
270 ChannelNo->setValue(Chip*10+Channel);
271 }
272 else if(Board==999999999) QMessageBox::warning(this, "ddd Message","Pixel ID unknown.",QMessageBox::Ok);
273 else QMessageBox::warning(this, "ddd Message","Pixel ID out of current range.",QMessageBox::Ok);
274}
275
276
277//------------------------------------------------------------------
278//**************************** All menus ***************************
279//------------------------------------------------------------------
280
281void ddd::MenuSave() {
282 QString Filename = QFileDialog::getSaveFileName(this,
283 "Filename of image", "/home/ogrimm/ddd", "Image files (*.bmp *.jpg *.png *.ppm *.tiff *.xbm *.xpm);;All files (*)");
284 if (Filename.length()>0) {
285 QPixmap Pixmap = QPixmap::grabWidget(Graph);
286 if(!Pixmap.save(Filename)) {
287 QMessageBox::warning(this, "ddd Message","Could not write image file.",QMessageBox::Ok);
288 remove(Filename.toAscii().data());
289 }
290 }
291}
292
293void ddd::MenuPrint() {
294 QPrinter *Printer = new QPrinter;
295 QPrintDialog *PrintDialog = new QPrintDialog(Printer, this);
296 if (PrintDialog->exec() == QDialog::Accepted) {
297 QPainter Painter(Printer);
298 QPixmap Pixmap = QPixmap::grabWidget(Graph);
299 Painter.drawPixmap(0, 0, Pixmap);
300 }
301 delete Printer; delete PrintDialog;
302}
303
304void ddd::MenuHelp() {
305 QMessageBox Message;
306 Message.setText("The DRS Data Display program can be used for two purposes:\n\n"
307 "1. Reading and displaying the content of a raw data file written by the drsdaq program\n"
308 "2. Acquiring and displaying online data from a running drsdaq program via the socket interface\n\n"
309 "With an established socket connection, displaying of raw data files is disabled.");
310 Message.setWindowTitle("ddd Help");
311 Message.exec();
312}
313
314void ddd::MenuAbout() {
315 QMessageBox::about(this, "ddd About","DRS Data Display\n\n"
316 "Written by Oliver Grimm, IPP, ETH Zurich\n"
317 "This version compiled "__DATE__".\n\n"
318 "Graphical user interface implemented with Qt.\n"
319 "Bug reports to oliver.grimm@phys.ethz.ch.");
320}
Note: See TracBrowser for help on using the repository browser.