Medical Imaging Interaction Toolkit  2024.06.00
Medical Imaging Interaction Toolkit
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef QmitkSafeNotify_h
14 #define QmitkSafeNotify_h
15 
16 #include <mitkException.h>
17 #include <mitkLog.h>
18 
19 #include <QMessageBox>
20 
21 template <class A>
22 bool QmitkSafeNotify(A *app, QObject *receiver, QEvent *event)
23 {
24  QString msg;
25  try
26  {
27  return app->A::notify(receiver, event);
28  }
29  catch (mitk::Exception &e)
30  {
31  msg = QString("MITK Exception:\n\n") + QString("Description: ") + QString(e.GetDescription()) + QString("\n\n") +
32  QString("Filename: ") + QString(e.GetFile()) + QString("\n\n") + QString("Line: ") +
33  QString::number(e.GetLine());
34  }
35  catch (std::exception &e)
36  {
37  msg = e.what();
38  }
39  catch (...)
40  {
41  msg = "Unknown exception";
42  }
43  MITK_ERROR << "An error occurred: " << msg.toStdString();
44 
45  QMessageBox msgBox;
46  msgBox.setText("An error occurred. You should save all data and quit the program to prevent possible data loss.");
47  msgBox.setDetailedText(msg);
48  msgBox.setIcon(QMessageBox::Critical);
49  msgBox.addButton("Exit immediately", QMessageBox::YesRole);
50  msgBox.addButton("Ignore", QMessageBox::NoRole);
51 
52  int ret = msgBox.exec();
53 
54  switch (ret)
55  {
56  case 0:
57  MITK_ERROR << "The program was closed.";
58  app->closeAllWindows();
59  break;
60  case 1:
62  << "The error was ignored by the user. The program may be in a corrupt state and don't behave like expected!";
63  break;
64  }
65 
66  return false;
67 }
68 
69 #endif
mitkException.h
mitk::Exception
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:45
QmitkSafeNotify
bool QmitkSafeNotify(A *app, QObject *receiver, QEvent *event)
Definition: QmitkSafeNotify.h:22
MITK_ERROR
#define MITK_ERROR
Definition: mitkLog.h:211
mitkLog.h