MIRA
FrameworkWithGui.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_FRAMEWORKWITHGUI_H_
48 #define _MIRA_FRAMEWORKWITHGUI_H_
49 
50 #ifndef Q_MOC_RUN
51 #include <boost/scoped_ptr.hpp>
52 #endif
53 
54 #include <QApplication>
55 #include <QMessageBox>
56 #include <error/LoggingAux.h>
57 #include <fw/Framework.h>
58 #include <thread/ThreadMonitor.h>
59 
60 #include <widgets/SplashScreen.h>
61 
62 namespace mira {
63 
65 
67 {
68 public:
69 
77  class Application: public QApplication
78  {
79  public:
80 
81  Application(int& argc, char** argv): QApplication(argc,argv)
82  {}
83 
87  bool notify(QObject *rec, QEvent *ev)
88  {
89  try {
90  return QApplication::notify(rec, ev);
91  }
92  catch (std::exception& ex) {
93  MIRA_LOG_EXCEPTION(ERROR, ex) << "Unhandled exception:\n";
95  std::string msg = ex.what() +
96  std::string("\n\nDo you want to continue or abort");
97  if(QMessageBox::critical(NULL, "Unhandled exception", msg.c_str() ,
98  QMessageBox::Ignore | QMessageBox::Abort) == QMessageBox::Abort)
99  abort();
100  }
101  catch (...) {
102  MIRA_LOG(ERROR) << "Unhandled exception: <unknown exception>";
104  std::string msg = "<unknown exception>" +
105  std::string("\n\nDo you want to continue or abort");
106  if(QMessageBox::critical(NULL, "Unhandled exception", msg.c_str() ,
107  QMessageBox::Ignore | QMessageBox::Abort) == QMessageBox::Abort)
108  abort();
109  }
110  return true;
111  }
112  };
113 
114 public:
115 
117  FrameworkWithGui(int& argc, char** argv, bool startImmediately = false) :
118  Framework(argc, argv, startImmediately),
119  mQApp(new Application(argc, argv)),
120  mCreatedApp(true)
121  {}
122 
127  FrameworkWithGui(int& argc, char** argv, QApplication* app, bool startImmediately = false) :
128  Framework(argc, argv, startImmediately),
129  mQApp(app),
130  mCreatedApp(false)
131  {}
132 
134  {
135  if(mCreatedApp)
136  delete mQApp;
137  }
138 
139 public:
140 
146  virtual int exec() {
147  ThreadMonitor::instance().addThisThread("Main Qt thread");
148  mInExec = true;
149  mQApp->exec();
150  mInExec = false;
151  MIRA_LOG(DEBUG) << "Application was closed, terminating ...";
152 
153  return getTerminationExitCode();
154  }
155 
156  virtual void requestTermination(int exitcode=0) {
157  QCoreApplication::instance ()->quit();
159  }
160 
161 private:
162 
163  QApplication* mQApp;
164  bool mCreatedApp;
165 };
166 
168 
169 }
170 
171 #endif
FrameworkWithGui(int &argc, char **argv, QApplication *app, bool startImmediately=false)
Constructor that takes previously created QApplication.
Definition: FrameworkWithGui.h:127
static Type & instance()
Returns a reference to the singleton instance.
Definition: Singleton.h:544
bool mInExec
Definition: Framework.h:393
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
A thread monitor, which collects information about the resources of all running threads of the curren...
#define MIRA_LOG(level)
Use this macro to log data.
Definition: LoggingCore.h:528
static void closeSplashScreen()
virtual void requestTermination(int exitcode=0)
Requests the termination of the framework and hence the whole application.
Definition: FrameworkWithGui.h:156
Declaration of SplashScreen.
virtual int exec()
Executes the framework and executes the underlying QApplication until the application is terminated...
Definition: FrameworkWithGui.h:146
~FrameworkWithGui()
Definition: FrameworkWithGui.h:133
Application(int &argc, char **argv)
Definition: FrameworkWithGui.h:81
Auxiliary logging macros for special entities like exceptions, etc.
Definition: FrameworkWithGui.h:66
This class represents the core element of a modular application.
Definition: Framework.h:149
virtual void requestTermination(int exitcode=0)
Requests the termination of the framework and hence the whole application.
#define MIRA_LOG_EXCEPTION(level, ex)
Log the specified exception, including all information that the exception object carries.
Definition: LoggingAux.h:107
bool notify(QObject *rec, QEvent *ev)
Overwrites QApplication::notify in order to catch exceptions.
Definition: FrameworkWithGui.h:87
Implements our own Application derived from QApplication in order to overwrite the notify method...
Definition: FrameworkWithGui.h:77
int getTerminationExitCode() const
Returns the exit code as specified by requestTermination(), or 0 if requestTermination() was not call...
Definition: LoggingCore.h:77
Definition: LoggingCore.h:74
The framework that holds all manager classes and provides startup and shutdown of all framework relat...
FrameworkWithGui(int &argc, char **argv, bool startImmediately=false)
Constructor that creates own FrameworkWithGui::Application.
Definition: FrameworkWithGui.h:117