Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
QmitkSafeNotify.h
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef QMITKSAFENOTIFY_H
18 #define QMITKSAFENOTIFY_H
19 
20 #include <mitkException.h>
21 #include <mitkLogMacros.h>
22 
23 #include <QMessageBox>
24 
25 template <class A>
26 bool QmitkSafeNotify(A *app, QObject *receiver, QEvent *event)
27 {
28  QString msg;
29  try
30  {
31  return app->A::notify(receiver, event);
32  }
33  catch (mitk::Exception &e)
34  {
35  msg = QString("MITK Exception:\n\n") + QString("Description: ") + QString(e.GetDescription()) + QString("\n\n") +
36  QString("Filename: ") + QString(e.GetFile()) + QString("\n\n") + QString("Line: ") +
37  QString::number(e.GetLine());
38  }
39  catch (std::exception &e)
40  {
41  msg = e.what();
42  }
43  catch (...)
44  {
45  msg = "Unknown exception";
46  }
47  MITK_ERROR << "An error occurred: " << msg.toStdString();
48 
49  QMessageBox msgBox;
50  msgBox.setText("An error occurred. You should save all data and quit the program to prevent possible data loss.");
51  msgBox.setDetailedText(msg);
52  msgBox.setIcon(QMessageBox::Critical);
53  msgBox.addButton(app->trUtf8("Exit immediately"), QMessageBox::YesRole);
54  msgBox.addButton(app->trUtf8("Ignore"), QMessageBox::NoRole);
55 
56  int ret = msgBox.exec();
57 
58  switch (ret)
59  {
60  case 0:
61  MITK_ERROR << "The program was closed.";
62  app->closeAllWindows();
63  break;
64  case 1:
66  << "The error was ignored by the user. The program may be in a corrupt state and don't behave like expected!";
67  break;
68  }
69 
70  return false;
71 }
72 
73 #endif // QMITKSAFENOTIFY_H
#define MITK_ERROR
Definition: mitkLogMacros.h:24
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
bool QmitkSafeNotify(A *app, QObject *receiver, QEvent *event)