QXRD  0.11.16
qxrdapplication.cpp
Go to the documentation of this file.
1 #define _CRT_SECURE_NO_WARNINGS
2 
3 #include "qxrddebug.h"
4 #include "qxrdapplication.h"
5 #include "qxrdapplication-ptr.h"
6 #include "qxrdwelcomewindow.h"
7 #include "qxrdwindow.h"
8 #include "qxrdserverthread.h"
9 #include "qxrdserver.h"
10 #include "qxrdsimpleserverthread.h"
11 #include "qxrdsimpleserver.h"
13 #include "qxrddataprocessor.h"
14 #include "qxrdintegrator.h"
15 #include "qxrdacquisitionthread.h"
16 #include "qxrdacquisition.h"
17 #include "qcepallocatorthread.h"
18 #include "qcepallocator.h"
19 #include "qxrdfilesaverthread.h"
20 #include "qxrdfilesaver.h"
21 #include "qxrdscriptenginethread.h"
22 #include "qxrdscriptengine.h"
24 #include "qcepproperty.h"
26 #include "qxrdprocessorinterface.h"
28 #include "qxrdglobalsettings.h"
29 #include "qxrdexperimentthread.h"
30 #include "qxrdexperiment.h"
31 #include "qxrdexperimentsettings.h"
32 #include "qcepsettingssaver.h"
33 #include "qxrdsplashscreen.h"
34 #include "qxrdsplashscreen-ptr.h"
35 #include "qcepmutexlocker.h"
36 #include "qxrdcalibrant.h"
37 #include "qxrdcalibrantlibrary.h"
38 
39 #ifdef HAVE_PERKIN_ELMER
41 #endif
42 
43 #include <QTime>
44 #include <QtConcurrentRun>
45 #include <QFileDialog>
46 #include <QMessageBox>
47 #include <QString>
48 #include <tiffio.h>
49 #include <QPluginLoader>
50 #include <QSplashScreen>
51 #include <QDesktopServices>
52 #include <QUrl>
53 #include <QCoreApplication>
54 
55 #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
56 #include <QJsonObject>
57 #endif
58 
60 
61 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
62 QCoreApplication::EventFilter oldEventFilter;
63 
64 static bool myEventFilter(void *message, long *result)
65 {
66  eventCounter++;
67 
68  if (oldEventFilter) {
69  return oldEventFilter(message, result);
70  } else {
71  return false;
72  }
73 }
74 
75 QTimer eventCounterTimer;
76 #endif
77 
79 {
80  if (qcepDebug(DEBUG_APP)) {
81  printMessage(tr("%1 events processed").arg(eventCounter));
82  }
83 
84  eventCounter = 0;
85 }
86 
88 {
89  QStringList res;
90 
91  for (int i=0; i<argc; i++) {
92  res.append(argv[i]);
93  }
94 
95  return res;
96 }
97 
99  QcepApplication(argc, argv),
100  m_ObjectNamer(this, "application"),
102  new QcepSettingsSaver(this))),
103  m_RecentExperiments(m_Saver, this, "recentExperiments", QStringList(), "Recent Experiments"),
104  m_RecentExperimentsSize(m_Saver, this,"recentExperimentsSize", 8, "Number of Recent Experiments to Remember"),
105  m_CurrentExperiment(m_Saver, this, "currentExperiment", "", "Current Experiment"),
106  m_CurrentDirectory(m_Saver, this, "currentDirectory", QDir::homePath(), "Current Directory"),
107 // m_OpenDirectly(m_Saver, this,"openDirectly", false, "Open Last Experiment at Startup"),
108  m_Debug(m_Saver, this,"debug", 0, "Debug Level"),
109  m_FreshStart(QcepSettingsSaverPtr(), this,"freshStart", 0, "Do a Fresh Start"),
110  m_FileBrowserLimit(m_Saver, this, "fileBrowserLimit", 1000, "Max Number of Files in Browser Windows (0 = unlimited)"),
111  m_MessageWindowLines(m_Saver, this, "messageWindowLines", 1000, "Number of Lines in Message Window (0 = unlimited)"),
112  m_UpdateIntervalMsec(m_Saver, this, "updateIntervalMsec", 1000, "Time Intervale for Updates (in msec)"),
113  m_Argc(QcepSettingsSaverPtr(), this, "argc", argc, "Number of Command Line Arguments"),
114  m_Argv(QcepSettingsSaverPtr(), this, "argv", makeStringListFromArgs(argc, argv), "Command Line Arguments"),
115  m_GuiWanted(QcepSettingsSaverPtr(), this, "guiWanted", 1, "GUI Wanted?"),
116  m_StartDetectors(QcepSettingsSaverPtr(), this, "startDetectors", 1, "Start Detectors when opening experiments"),
117  m_CmdList(QcepSettingsSaverPtr(), this, "cmdList", QStringList(), "Commands to Execute"),
118  m_FileList(QcepSettingsSaverPtr(), this, "fileList", QStringList(), "Files to Process"),
119  m_LockerCount(QcepSettingsSaverPtr(), this, "lockerCount", 0, "Number of mutex locks taken"),
120  m_LockerRate(QcepSettingsSaverPtr(), this, "lockerRate", 0, "Mutex Locking Rate"),
121  m_Splash(NULL),
122  m_WelcomeWindow(NULL),
123  m_AllocatorThread(NULL),
124  m_Allocator(),
126  #ifdef HAVE_PERKIN_ELMER
127  m_PerkinElmerPluginInterface(NULL),
128  #endif
129  m_ResponseTimer(NULL),
130  m_SettingsMutex(),
132 {
134  printf("QxrdApplication::QxrdApplication(%p)\n", this);
135  }
136 
142 }
143 
145 {
146  m_Application = app;
147 
148  connect(this, &QCoreApplication::aboutToQuit, this, &QxrdApplication::finish);
149 
150  connect(&m_SplashTimer, &QTimer::timeout, this, &QxrdApplication::hideSplash);
151 
152  connect(&m_LockerTimer, &QTimer::timeout, this, &QxrdApplication::lockerTimerElapsed);
153 
154  m_LastLockerTime.start();
155  m_LockerTimer.start(5000);
156 
157  QDir::setCurrent(QDir::homePath());
158 
159  setOrganizationName("cep");
160  setOrganizationDomain("xray.aps.anl.gov");
161  setApplicationName("qxrd");
162 
163  printMessage("------ Starting QXRD Application ------");
164 
165  QString about = "QXRD Version " STR(QXRD_VERSION);
166 
167  if (sizeof(void*) == 4) {
168  about += " - 32 Bit";
169  } else {
170  about += " - 64 Bit";
171  }
172 
173 #ifdef Q_CC_MSVC
174  about += " MSVC";
175 #endif
176 
177 #ifdef Q_CC_GNU
178  about += " gcc";
179 #endif
180 
181 #ifdef QT_NO_DEBUG
182  about += " Release\n";
183 #else
184  about += " Debug\n";
185 #endif
186 
187  printMessage(about);
188  printMessage("QWT Version " QWT_VERSION_STR);
189  printMessage(tr("QT Version %1").arg(qVersion()));
190 
191  connect(prop_Debug(), &QcepInt64Property::valueChanged,
193  readSettings();
194 
195  for (int i=1; i<argc; i++) {
196  int dbg=0;
197 
198  if (strcmp(argv[i],"-fresh") == 0) {
199  set_FreshStart(true);
200  } else if (sscanf(argv[i],"-debug=%d",&dbg)==1) {
201  set_Debug(dbg);
202  } else if (strcmp(argv[i],"-nogui")==0) {
203  set_GuiWanted(false);
204  } else if (strcmp(argv[i],"-gui")==0) {
205  set_GuiWanted(true);
206  } else if (strcmp(argv[i],"-nostart")==0) {
207  printf("Don't start detectors\n");
208  set_StartDetectors(false);
209  } else if (strcmp(argv[i],"-start")==0) {
210  set_StartDetectors(true);
211  } else if (strcmp(argv[i],"-c")==0) {
212  if (i++ < argc) {
213  prop_CmdList()->appendValue(argv[i]);
214  }
215  } else if (strcmp(argv[i],"-s")==0) {
216  if (i++ < argc) {
217  prop_CmdList()->appendValue(tr("loadScript(\"%1\")").arg(argv[i]));
218  }
219  } else {
220  prop_FileList()->appendValue(argv[i]);
221  }
222  }
223 
224  if (get_GuiWanted() == false) {
225  foreach(QString cmd, get_CmdList()) {
226  printf("Cmd: %s\n", qPrintable(cmd));
227  }
228 
229  foreach(QString file, get_FileList()) {
230  printf("File: %s\n", qPrintable(file));
231  }
232  }
233 
236  m_AllocatorThread -> setObjectName("alloc");
237  m_AllocatorThread -> start();
239 
240  printMessage(tr("Home Path: %1").arg(QDir::homePath()));
241  printMessage(tr("Current Path: %1").arg(QDir::currentPath()));
242  printMessage(tr("Root Path %1").arg(QDir::rootPath()));
243 
245 
246  QThread::currentThread()->setObjectName("app");
247 
248  loadPlugins();
249  readSettings();
250 
251  printMessage(tr("Optimal thread count = %1").arg(QThread::idealThreadCount()));
252 
253 // m_ResponseTimer = new QxrdResponseTimer(30000, 5000, this);
254 
255  if (get_FreshStart()) {
257  }
258 
259  if (/*get_OpenDirectly() &&*/ (get_CurrentExperiment().length()>0)) {
260  openExperiment(get_CurrentExperiment());
261  } else {
263  }
264 
265  m_Saver->start();
266 
267  return true;
268 }
269 
271 {
272 #ifndef QT_NO_DEBUG
273  printf("Deleting application\n");
274 #endif
275 
276  m_Saver->performSave();
277 
279  printf("QxrdApplication::~QxrdApplication(%p)\n", this);
280  }
281 }
282 
284 {
285  if (qcepDebug(DEBUG_APP)) {
286  printMessage("QxrdApplication::finish()");
287  }
288 
289  writeSettings();
290 
291  m_ExperimentThreads.clear();
292  m_Experiments.clear();
293 
295 }
296 
298 {
300 
301  if (m_WelcomeWindow == NULL) {
303  m_WelcomeWindow->setAttribute(Qt::WA_DeleteOnClose, true);
304  }
305 
306  m_WelcomeWindow -> show();
307 }
308 
310 {
312 
313  if (m_WelcomeWindow) {
314  m_WelcomeWindow -> hide();
315  }
316 }
317 
318 //QWidget* QxrdApplication::window()
319 //{
320 // return m_WelcomeWindow;
321 //}
322 
323 //QxrdApplicationPtr QxrdApplication::application()
324 //{
325 // return g_Application;
326 //}
327 
328 #ifdef HAVE_PERKIN_ELMER
329 
330 QxrdPerkinElmerPluginInterfacePtr QxrdApplication::perkinElmerPlugin()
331 {
332  return m_PerkinElmerPluginInterface;
333 }
334 
335 #endif
336 
338 {
339  return m_NIDAQPluginInterface;
340 }
341 
342 #define xstr(s) str(s)
343 #define str(s) #s
344 
346 {
347  QList<QDir> pluginsDirList;
348 
349 #ifdef QXRD_PLUGIN_PATH
350  pluginsDirList.append(QDir(xstr(QXRD_PLUGIN_PATH)));
351 #endif
352  QDir pluginsDir = QDir(qApp->applicationDirPath());
353  pluginsDir.cd("plugins");
354  pluginsDirList.append(pluginsDir);
355 
356  foreach (QDir pluginsDir, pluginsDirList) {
357  if (qcepDebug(DEBUG_PLUGINS)) {
358  printf("Looking for plugins in directory %s\n", qPrintable(pluginsDir.absolutePath()));
359  }
360 
361  foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
362  QString fullPath = pluginsDir.absoluteFilePath(fileName);
363 
364  if (qcepDebug(DEBUG_PLUGINS)) {
365  printf("Looking for plugin in file %s\n", qPrintable(fullPath));
366  }
367 
368  if (QLibrary::isLibrary(fullPath)) {
369  QPluginLoader loader(fullPath);
370 
371 #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
372  QJsonObject meta = loader.metaData();
373 
374  if (qcepDebug(DEBUG_PLUGINS)) {
375  printf("Plugin metadata for %s\n", qPrintable(fullPath));
376  foreach(QString key, meta.keys()) {
377  printf("Key %s = %s\n", qPrintable(key), qPrintable(meta.value(key).toString()));
378  }
379  }
380 #endif
381 
382  QObject *plugin = loader.instance();
383  if (plugin) {
384  if (qcepDebug(DEBUG_PLUGINS)) {
385  printf("Loaded plugin from %s : type %s\n", qPrintable(fullPath), qPrintable(plugin->metaObject()->className()));
386  }
387 
388  QString pluginName = "";
389 
390  QxrdDetectorPluginInterface* detector = qobject_cast<QxrdDetectorPluginInterface*>(plugin);
391 
392  if (detector) {
393  pluginName = detector -> name();
394  }
395 
396  QxrdProcessorInterface* processor = qobject_cast<QxrdProcessorInterface*>(plugin);
397 
398  if (processor) {
399  pluginName = processor -> name();
400  }
401 
402 #ifdef HAVE_PERKIN_ELMER
403  QxrdPerkinElmerPluginInterface *perkinElmer = qobject_cast<QxrdPerkinElmerPluginInterface*>(plugin);
404 
405  if (perkinElmer) {
406  pluginName = perkinElmer -> name();
407 
408  m_PerkinElmerPluginInterface = QxrdPerkinElmerPluginInterfacePtr(perkinElmer);
409  }
410 #endif
411 
412  QxrdNIDAQPluginInterface *nidaq = qobject_cast<QxrdNIDAQPluginInterface*>(plugin);
413 
414  if (nidaq) {
415  pluginName = nidaq -> name();
416 
418  m_NIDAQPluginInterface -> setErrorOutput(this);
419  }
420 
421  splashMessage(tr("Loaded plugin \"%1\"").arg(pluginName));
422 
423  printMessage(tr("Loaded plugin \"%1\" from %2")
424  .arg(pluginName)
425  .arg(pluginsDir.absoluteFilePath(fileName)));
426  } else {
427  if (qcepDebug(DEBUG_PLUGINS)) {
428  printf("Failed to load plugin from %s : %s\n", qPrintable(fileName), qPrintable(loader.errorString()));
429  }
430 
431  if (QLibrary::isLibrary(pluginsDir.absoluteFilePath(fileName))) {
432  QString msg = tr("Failed to load plugin %1 : %2")
433  .arg(pluginsDir.absoluteFilePath(fileName))
434  .arg(loader.errorString());
435  splashMessage(msg);
436  printMessage(msg);
437  }
438  }
439  } else {
440  if (qcepDebug(DEBUG_PLUGINS)) {
441  printf("File %s is not a library\n", qPrintable(fileName));
442  }
443  }
444  }
445  }
446 }
447 
449 {
450  if (QThread::currentThread() != thread()) {
451  QMetaObject::invokeMethod(this, "splashMessage", Q_ARG(QString, msg));
452  } else {
454 
455  if (get_GuiWanted()) {
456  if (m_Splash == NULL) {
458  }
459 
460  m_Splash -> show();
461 
462  QString msgf = tr("Qxrd Version " STR(QXRD_VERSION) "\n")+msg;
463 
464  m_Splash->showMessage(msgf, Qt::AlignBottom|Qt::AlignHCenter);
465  processEvents();
466 
467  m_SplashTimer.start(5000);
468  }
469  }
470 }
471 
473 {
475 
476  if (m_Splash) {
477  m_Splash -> hide();
478  }
479 }
480 
481 void QxrdApplication::logMessage(QString /*msg*/)
482 {
483 }
484 
485 void QxrdApplication::warningMessage(QString msg, QDateTime /*ts*/)
486 {
487  if (experiment(0)) {
488  INVOKE_CHECK(QMetaObject::invokeMethod(experiment(0).data(), "warningMessage", Qt::BlockingQueuedConnection, Q_ARG(QString, msg)));
489  }
490 }
491 
492 void QxrdApplication::printMessage(QString msg, QDateTime ts)
493 {
495  } else {
496  QString message = ts.toString("yyyy.MM.dd : hh:mm:ss.zzz ")+
497  QThread::currentThread()->objectName()+": "+
498  msg.trimmed();
499 
500  message = message.replace("\n", " : ");
501 
502  logMessage(message);
503 
504  if (experiment(0)) {
505  INVOKE_CHECK(QMetaObject::invokeMethod(experiment(0).data(), "printMessage", Qt::QueuedConnection, Q_ARG(QString, msg)));
506  }
507  }
508 }
509 
510 void QxrdApplication::statusMessage(QString msg, QDateTime ts)
511 {
513  } else {
514  QString message = ts.toString("yyyy.MM.dd : hh:mm:ss.zzz ")+msg.trimmed();
515 
516  message = message.replace("\n", " : ");
517 
518  logMessage(message);
519 
520  if (experiment(0)) {
521  INVOKE_CHECK(QMetaObject::invokeMethod(experiment(0).data(), "statusMessage", Qt::QueuedConnection, Q_ARG(QString, message)));
522  }
523  }
524 }
525 
526 void QxrdApplication::criticalMessage(QString msg, QDateTime ts)
527 {
528  QString message = ts.toString("yyyy.MM.dd : hh:mm:ss.zzz ")+msg.trimmed();
529 
530  message = message.replace("\n", " : ");
531 
532  logMessage(message);
533 
534  if (experiment(0)) {
535  INVOKE_CHECK(QMetaObject::invokeMethod(experiment(0).data(), "criticalMessage", Qt::QueuedConnection, Q_ARG(QString, message)));
536  }
537 }
538 
540 {
541  return QDir::homePath();
542 }
543 
545 {
546  return QDir::currentPath();
547 }
548 
550 {
551  return QDir::rootPath();
552 }
553 
555 {
556  QcepMutexLocker lock(__FILE__, __LINE__, &m_SettingsMutex);
557 
558  QxrdGlobalSettings settings(this);
559 
560  readSettings(&settings, "application");
561 }
562 
563 void QxrdApplication::readSettings(QSettings *settings, QString section)
564 {
565  QcepProperty::readSettings(this, settings, section);
566 
568 
569  if (alloc) {
570  alloc->readSettings(settings, "allocator");
571  }
572 }
573 
575 {
576  QcepMutexLocker lock(__FILE__, __LINE__, &m_SettingsMutex);
577 
578  QxrdGlobalSettings settings(this);
579 
580  writeSettings(&settings, "application");
581 }
582 
583 void QxrdApplication::writeSettings(QSettings *settings, QString section)
584 {
585  QcepProperty::writeSettings(this, settings, section);
586 
588 
589  if (alloc) {
590  alloc->writeSettings(settings, "allocator");
591  }
592 }
593 
595 {
596  QString loadPrefsFrom = QFileDialog::getOpenFileName(NULL,
597  "Load QXRD Preferences from...");
598 
599  if (loadPrefsFrom != "") {
600  loadPreferences(loadPrefsFrom);
601  }
602 }
603 
605 {
606  QxrdGlobalSettings settings(path, QSettings::IniFormat);
607 
608  readSettings(&settings, "application");
609 }
610 
612 {
613  QString savePrefsTo = QFileDialog::getSaveFileName(NULL,
614  "Save QXRD Preferences to...");
615 
616  if (savePrefsTo != "") {
617  savePreferences(savePrefsTo);
618  }
619 }
620 
622 {
623  QFile::remove(path+".new");
624 
625  {
626  QxrdGlobalSettings settings(path+".new", QSettings::IniFormat);
627 
628  writeSettings(&settings, "application");
629  }
630 
631  QFile::remove(path+".bak");
632  QFile::rename(path, path+".bak");
633  QFile::rename(path+".new", path);
634 }
635 
637 {
638  if (wantToQuit()) {
639  quit();
640  }
641 }
642 
644 {
645  return QMessageBox::question(NULL, tr("Really Quit?"),
646  tr("Do you really want to exit the application?"),
647  QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Ok;
648 }
649 
651 {
652  return m_Allocator;
653 }
654 
656 {
657  QString about = "QXRD Data Acquisition for PE Area Detectors\nVersion " STR(QXRD_VERSION);
658 
659  if (sizeof(void*) == 4) {
660  about += " - 32 Bit";
661  } else {
662  about += " - 64 Bit";
663  }
664 
665 #ifdef Q_CC_MSVC
666  about += " MSVC";
667 #endif
668 
669 #ifdef Q_CC_GNU
670 #ifdef Q_CC_CLANG
671  about += " clang";
672 #else
673  about += " gcc";
674 #endif
675 #endif
676 
677 #ifdef QT_NO_DEBUG
678  about += " Release\n";
679 #else
680  about += " Debug\n";
681 #endif
682 
683  about += tr("Qt Version %1\n").arg(qVersion());
684  about += tr("Qceplib Version %1\n").arg(STR(QCEPLIB_VERSION));
685  about += tr("QWT Version %1\n").arg(STR(QCEPLIB_QWT_VERSION));
686  about += tr("Mar345 Version %1\n").arg(STR(QCEPLIB_MAR345_VERSION));
687  about += tr("CBF Version %1\n").arg(STR(QCEPLIB_CBF_VERSION));
688  about += tr("TIFF Version %1\n").arg(STR(QCEPLIB_TIFF_VERSION));
689  about += tr("LevMar Version %1\n").arg(STR(QCEPLIB_LEVMAR_VERSION));
690 #ifdef QCEPLIB_ZLIB_VERSION
691  about += tr("ZLIB Version %1\n").arg(STR(QCEPLIB_ZLIB_VERSION));
692 #endif
693 
694 #ifdef QCEPLIB_SZIP_VERSION
695  about += tr("SZIP Version %1\n").arg(STR(QCEPLIB_SZIP_VERSION));
696 #endif
697 
698 #ifdef QCEPLIB_HDF5_VERSION
699  about += tr("HDF5 Version %1\n").arg(STR(QCEPLIB_HDF5_VERSION));
700 #endif
701  about += tr("Spec Server Version %1\n").arg(STR(QCEPLIB_SPECSERVER_VERSION));
702 
703  QMessageBox::about(NULL, "QXRD", about);
704 }
705 
707 {
708  QDesktopServices::openUrl(QUrl("http://qxrd.sourceforge.net/"));
709 }
710 
711 void QxrdApplication::doOpenURL(QString url)
712 {
713  QDesktopServices::openUrl(QUrl(url));
714 }
715 
717 {
718  QxrdGlobalPreferencesDialog prefs(this);
719 
720  prefs.exec();
721 }
722 
723 void QxrdApplication::debugChanged(qint64 newValue)
724 {
725  if (g_DebugLevel) {
726  printMessage(tr("Debug level changed from %1 to %2").arg(g_DebugLevel->debugLevel()).arg(newValue));
727 
728  g_DebugLevel->setDebugLevel(newValue);
729  }
730 }
731 
732 static void qxrdTIFFWarningHandler(const char* /*module*/, const char* /*fmt*/, va_list /*ap*/)
733 {
734  // char msg[100];
735  //
736  // vsnprintf(msg, sizeof(msg), fmt, ap);
737  //
738  // g_Application -> tiffWarning(module, msg);
739 }
740 
741 static void qxrdTIFFErrorHandler(const char* module, const char* fmt, va_list ap)
742 {
743  char msg[100];
744 
745  vsnprintf(msg, sizeof(msg), fmt, ap);
746 
747  QxrdApplication *app = qobject_cast<QxrdApplication*>(g_Application);
748 
749  if (app) {
750  app -> tiffError(module, msg);
751  }
752 }
753 
755 {
756  TIFFSetErrorHandler (&qxrdTIFFErrorHandler);
757  TIFFSetErrorHandlerExt (NULL);
758  TIFFSetWarningHandler (&qxrdTIFFWarningHandler);
759  TIFFSetWarningHandlerExt (NULL);
760 }
761 
762 void QxrdApplication::tiffWarning(const char *module, const char *msg)
763 {
764  QxrdApplication *app = qobject_cast<QxrdApplication*>(g_Application);
765 
766  if (app) {
767  app->criticalMessage(tr("TIFF Warning from %1 : %2").arg(module).arg(msg));
768  }
769 }
770 
771 void QxrdApplication::tiffError(const char *module, const char *msg)
772 {
773  QxrdApplication *app = qobject_cast<QxrdApplication*>(g_Application);
774 
775  if (app) {
776  app->criticalMessage(tr("TIFF Error from %1 : %2").arg(module).arg(msg));
777  }
778 }
779 
780 bool QxrdApplication::event(QEvent *ev)
781 {
782  QTime tick;
783  tick.start();
784 
785  bool res = QApplication::event(ev);
786 
787  int elapsed = tick.restart();
788 
789  if (elapsed > 1000) {
790  printMessage("event processing took more than 1 sec");
791  }
792 
793  return res;
794 }
795 
797 {
798  if (obj) {
799  obj->set_Creator("QXRD");
800  obj->set_Version(STR(QXRD_VERSION));
801  obj->set_QtVersion(QT_VERSION_STR);
802  }
803 }
804 
806 {
807  QSettings settings("cep.xor.aps.anl.gov", "qxrd-defaults");
808 
809  set_RecentExperiments(settings.value("recentExperiments").toStringList());
810  set_RecentExperimentsSize(settings.value("recentExperimentsSize", 8).toInt());
811  set_CurrentExperiment(settings.value("currentExperiment").toString());
812 }
813 
815 {
816  QSettings settings("cep.xor.aps.anl.gov", "qxrd-defaults");
817 
818  settings.setValue("recentExperiments", get_RecentExperiments());
819  settings.setValue("recentExperimentsSize", get_RecentExperimentsSize());
820  settings.setValue("currentExperiment", get_CurrentExperiment());
821 }
822 
824 {
826 }
827 
829 {
830  QString res = QFileDialog::getOpenFileName(NULL,
831  "Open an existing experiment...",
832  get_CurrentExperiment(),
833  "QXRD Experiments (*.qxrdp);;Other Files (*)");
834 
835  if (res.length() > 0) {
836  openExperiment(res);
837  }
838 }
839 
841 {
842  if (path.length() > 0) {
843  QxrdExperimentSettings settings(path);
844 
846 
847  QxrdExperimentPtr exp = experimentThread->experiment();
848 
849  printMessage("");
850  printMessage(tr("===== Open Experiment %1").arg(path));
851 
852  openedExperiment(experimentThread);
853  }
854 }
855 
857 {
858  if (qcepDebug(DEBUG_APP)) {
859  printf("QxrdApplication::closeExperiment(%p)\n", exp.data());
860  }
861 
862  QxrdExperimentPtr expp(exp);
863 
864  if (expp) {
865  QxrdExperimentThreadPtr expthrd = expp -> experimentThread();
866 
867  if (expthrd) {
868  closedExperiment(expthrd);
869  }
870  }
871 }
872 
874 {
875  QStringList recent = get_RecentExperiments();
876 
877  recent.prepend(path);
878  recent.removeDuplicates();
879 
880  while(recent.length() > get_RecentExperimentsSize()) {
881  recent.removeLast();
882  }
883 
884  set_RecentExperiments(recent);
885 }
886 
888 {
889  QFileInfo info(filename);
890  QString exten = info.suffix();
891 
892  if (exten != "qxrdp") {
893  return filename+".qxrdp";
894  } else {
895  return filename;
896  }
897 }
898 
899 void QxrdApplication::setNewExperimentSettings(QSettings &settings, int type, QString filename)
900 {
901  QFileInfo info(filename);
902  QString path = info.path();
903  QString base = info.completeBaseName();
904 
905  settings.setValue("application/d", type);
906  settings.setValue("processor/outputDirectory", path);
907  settings.setValue("application/logFileName", base+".log");
908  settings.setValue("processor/integratedFileName", base+".avg");
909  settings.setValue("window/inputFileBrowser/rootDirectory", path);
910  settings.setValue("window/outputFileBrowser/rootDirectory", path);
911 
912  switch(type) {
913  case 1: // Perkin Elmer
914  break;
915 
916  case 2: // Pilatus
917  break;
918 
919  case 4: // Analysis
920  settings.setValue("application/runSpecServer", false);
921  settings.setValue("application/runSimpleServer", false);
922  break;
923  }
924 }
925 
926 //QString QxrdApplication::newAnalysisExperiment(QString path)
927 //{
928 // path = normalizeExperimentName(path);
929 
930 // if (path.length() > 0) {
931 // savePreferences(path);
932 
933 // QSettings settings(path, QSettings::IniFormat);
934 
935 // setNewExperimentSettings(settings, 4, path);
936 
937 // return path;
938 // } else {
939 // return QString();
940 // }
941 //}
942 
943 //QString QxrdApplication::newPerkinElmerExperiment(QString path)
944 //{
945 // path = normalizeExperimentName(path);
946 
947 // if (path.length() > 0) {
948 // savePreferences(path);
949 
950 // QSettings settings(path, QSettings::IniFormat);
951 
952 // setNewExperimentSettings(settings, 1, path);
953 
954 // return path;
955 // } else {
956 // return QString();
957 // }
958 //}
959 
960 //QString QxrdApplication::newPilatusExperiment(QString path)
961 //{
962 // path = normalizeExperimentName(path);
963 
964 // if (path.length() > 0) {
965 // savePreferences(path);
966 
967 // QSettings settings(path, QSettings::IniFormat);
968 
969 // setNewExperimentSettings(settings, 2, path);
970 
971 // return path;
972 // } else {
973 // return QString();
974 // }
975 //}
976 
978 {
979  QFileInfo info(path);
980 
981  if (info.exists()) {
982  openExperiment(path);
983  } else {
984  printMessage(tr("Experiment %1 does not exist").arg(path));
985  }
986 }
987 
989 {
990  QxrdExperimentThreadPtr expthrd(expthrdw);
991 
992  if (expthrd) {
993  QxrdExperimentPtr expt = expthrd->experiment();
994 
995  if (expt) {
996  QString path = expt->experimentFilePath();
997  set_CurrentExperiment(path);
999 
1000  m_ExperimentThreads.append(expthrd);
1001  m_Experiments.append(expt);
1002 
1003  printMessage("");
1004  printMessage("New experiment loaded");
1005  printMessage("");
1006 
1008 
1009  expt->openWindows();
1010  }
1011  }
1012 }
1013 
1015 {
1016  QxrdExperimentThreadPtr expthrd(expthrdw);
1017 
1018  if (expthrd) {
1019  QxrdExperimentPtr expt = expthrd->experiment();
1020 
1021  m_ExperimentThreads.removeAll(expthrd);
1022  m_Experiments.removeAll(expt);
1023  }
1024 }
1025 
1026 QList<QxrdExperimentWPtr> &QxrdApplication::experiments()
1027 {
1028  return m_Experiments;
1029 }
1030 
1032 {
1033  return m_Experiments.value(i);
1034 }
1035 
1037 {
1038  foreach(QxrdExperimentPtr exp, m_Experiments) {
1039  if (exp->experimentFilePath() == path) {
1040  QxrdWindowPtr win = exp->window();
1041 
1042  if (win) {
1043  win->activateWindow();
1044  }
1045  }
1046  }
1047 }
1048 
1049 //void QxrdApplication::doNewPerkinElmerAcquisition()
1050 //{
1051 // QString newExperiment = QFileDialog::getSaveFileName(NULL,
1052 // "New Perkin Elmer Experiment",
1053 // get_CurrentExperiment(),
1054 // "QXRD Experiments (*.qxrdp)");
1055 
1056 // if (newExperiment.length() >= 1) {
1057 // QxrdExperimentThreadPtr docThread = QxrdExperimentThread::newExperimentPerkinElmerAcquisition(normalizeExperimentName(newExperiment), m_Application);
1058 
1059 // docThread->init(docThread);
1060 
1061 // openedExperiment(docThread);
1062 // }
1063 //}
1064 
1065 //void QxrdApplication::doNewPilatusAcquisition()
1066 //{
1067 // QString newExperiment = QFileDialog::getSaveFileName(NULL,
1068 // "New Pilatus Experiment",
1069 // get_CurrentExperiment(),
1070 // "QXRD Experiments (*.qxrdp)");
1071 
1072 // if (newExperiment.length() >= 1) {
1073 // QxrdExperimentThreadPtr docThread = QxrdExperimentThread::newExperimentPilatusAcquisition(normalizeExperimentName(newExperiment), m_Application);
1074 
1075 // docThread->init(docThread);
1076 
1077 // openedExperiment(docThread);
1078 // }
1079 //}
1080 
1081 //void QxrdApplication::doNewSimulatedAcquisition()
1082 //{
1083 // QString newExperiment = QFileDialog::getSaveFileName(NULL,
1084 // "New Simulated Acquisition Experiment",
1085 // get_CurrentExperiment(),
1086 // "QXRD Experiments (*.qxrdp)");
1087 
1088 // if (newExperiment.length() >= 1) {
1089 // QxrdExperimentThreadPtr docThread = QxrdExperimentThread::newExperimentSimulatedAcquisition(normalizeExperimentName(newExperiment), m_Application);
1090 
1091 // docThread->init(docThread);
1092 
1093 // openedExperiment(docThread);
1094 // }
1095 //}
1096 
1097 //void QxrdApplication::doNewPerkinElmerAnalysis()
1098 //{
1099 // QString newExperiment = QFileDialog::getSaveFileName(NULL,
1100 // "New Perkin Elmer Analysis Experiment",
1101 // get_CurrentExperiment(),
1102 // "QXRD Experiments (*.qxrdp)");
1103 
1104 // if (newExperiment.length() >= 1) {
1105 // QxrdExperimentThreadPtr docThread = QxrdExperimentThread::newExperimentPerkinElmerAnalysis(normalizeExperimentName(newExperiment), m_Application);
1106 
1107 // docThread->init(docThread);
1108 
1109 // openedExperiment(docThread);
1110 // }
1111 //}
1112 
1113 //void QxrdApplication::doNewPilatusAnalysis()
1114 //{
1115 // QString newExperiment = QFileDialog::getSaveFileName(NULL,
1116 // "New Pilatus Analysis Experiment",
1117 // get_CurrentExperiment(),
1118 // "QXRD Experiments (*.qxrdp)");
1119 
1120 // if (newExperiment.length() >= 1) {
1121 // QxrdExperimentThreadPtr docThread = QxrdExperimentThread::newExperimentPilatusAnalysis(normalizeExperimentName(newExperiment), m_Application);
1122 
1123 // docThread->init(docThread);
1124 
1125 // openedExperiment(docThread);
1126 // }
1127 //}
1128 
1129 //void QxrdApplication::doNewGenericAnalysis()
1130 //{
1131 // QString newExperiment = QFileDialog::getSaveFileName(NULL,
1132 // "New Generic Analysis Experiment",
1133 // get_CurrentExperiment(),
1134 // "QXRD Experiments (*.qxrdp)");
1135 
1136 // if (newExperiment.length() >= 1) {
1137 // QxrdExperimentThreadPtr docThread = QxrdExperimentThread::newExperimentGenericAnalysis(normalizeExperimentName(newExperiment), m_Application);
1138 
1139 // docThread->init(docThread);
1140 
1141 // openedExperiment(docThread);
1142 // }
1143 //}
1144 
1146 {
1147  prop_LockerCount()->incValue(1);
1148 }
1149 
1151 {
1152  double elapsed = m_LastLockerTime.restart();
1153 
1154  double rate = 1000.0*(get_LockerCount() - m_LastLockerCount)/elapsed;
1155 
1156 // set_LockerRate(rate);
1157 
1158 // m_LastLockerTime = QTime::currentTime();
1159  m_LastLockerCount= get_LockerCount();
1160 
1161  if (rate>10000) {
1162  printMessage(tr("Locker rate %1 locks/sec").arg(/*get_LockerRate()*/rate));
1163  }
1164 }
QSharedPointer< QxrdExperiment > QxrdExperimentPtr
void tiffError(const char *module, const char *msg)
QcepAllocatorWPtr m_Allocator
QSharedPointer< QxrdWindow > QxrdWindowPtr
Definition: qxrdwindow-ptr.h:6
virtual ~QxrdApplication()
void appendRecentExperiment(QString path)
QxrdApplication(int &argc, char **argv)
void activateExperiment(QString path)
static void readSettings(QObject *object, QSettings *settings, QString section)
qint64 qcepDebug(int cond)
Definition: qcepdebug.cpp:26
static void registerMetaTypes()
QxrdExperimentPtr experiment(int i)
bool init(QxrdApplicationWPtr app, int &argc, char **argv)
void doOpenURL(QString url)
void savePreferences(QString path)
void chooseExistingExperiment()
QList< QxrdExperimentWPtr > m_Experiments
bool event(QEvent *ev)
#define STR(s)
Definition: qcepmacros.h:16
QSharedPointer< QxrdPerkinElmerPluginInterface > QxrdPerkinElmerPluginInterfacePtr
void debugChanged(qint64 newValue)
static QxrdImageDataFormatHis fmt
#define GUI_THREAD_CHECK
Definition: qcepmacros.h:10
static void registerMetaTypes()
QList< QxrdExperimentWPtr > & experiments()
static QxrdExperimentThreadPtr newExperiment(QString path, QxrdApplicationWPtr app, QSettings *settings)
QxrdResponseTimer * m_ResponseTimer
#define xstr(s)
QxrdApplicationWPtr m_Application
QStringList argv
QStringList makeStringListFromArgs(int argc, char **argv)
void openedExperiment(QxrdExperimentThreadPtr expthrdw)
void logMessage(QString msg)
QWeakPointer< QxrdApplication > QxrdApplicationWPtr
void tiffWarning(const char *module, const char *msg)
QWeakPointer< QxrdExperiment > QxrdExperimentWPtr
void openRecentExperiment(QString path)
QxrdWelcomeWindowPtr m_WelcomeWindow
QcepObjectNamer m_ObjectNamer
QSharedPointer< QcepAllocatorThread > QcepAllocatorThreadPtr
QSharedPointer< QxrdExperimentThread > QxrdExperimentThreadPtr
QxrdNIDAQPluginInterfacePtr nidaqPlugin()
#define INVOKE_CHECK(res)
Definition: qcepmacros.h:13
static void qxrdTIFFErrorHandler(const char *module, const char *fmt, va_list ap)
QcepAllocatorThreadPtr m_AllocatorThread
static void writeSettings(QObject *object, QSettings *settings, QString section)
QString normalizeExperimentName(QString filename)
static void registerMetaTypes()
void warningMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
void splashMessage(QString msg)
void setNewExperimentSettings(QSettings &settings, int type, QString filename)
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
void loadPreferences(QString path)
QWeakPointer< QcepAllocator > QcepAllocatorWPtr
QSharedPointer< QcepSettingsSaver > QcepSettingsSaverPtr
void valueChanged(qint64 val, int index)
QSharedPointer< QxrdNIDAQPluginInterface > QxrdNIDAQPluginInterfacePtr
int eventCounter
static void qxrdTIFFWarningHandler(const char *, const char *, va_list)
void statusMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
virtual void setDefaultObjectData(QcepDataObject *obj)
QcepApplication * g_Application
QSharedPointer< QcepDebugDictionary > g_DebugLevel
Definition: qcepdebug.cpp:4
void closedExperiment(QxrdExperimentThreadPtr expthrdw)
void criticalMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
QSharedPointer< QcepAllocator > QcepAllocatorPtr
QList< QxrdExperimentThreadPtr > m_ExperimentThreads
QSharedPointer< QxrdSplashScreen > QxrdSplashScreenPtr
QcepAllocatorWPtr allocator() const
QxrdNIDAQPluginInterfacePtr m_NIDAQPluginInterface
void closeExperiment(QxrdExperimentWPtr exp)
void openExperiment(QString path)
QxrdSplashScreenPtr m_Splash
QcepSettingsSaverPtr m_Saver