Image Component Library (ICL)
Application.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLQt/src/ICLQt/Application.h **
10 ** Module : ICLQt **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLUtils/Exception.h>
35 #include <ICLUtils/Function.h>
36 #include <string>
37 #include <vector>
38 
39 #include <QApplication>
40 #include <QtOpenGL/QGLWidget>
41 
42 namespace icl{
43 
44  namespace qt{
45 
47  struct ExecThread;
50 
100  class ICLQt_API ICLApplication : public QObject{
101  Q_OBJECT;
102 
103  private Q_SLOTS:
104  void lastWindowClosed();
105 
106  public:
107  QApplication *app;
108  QGLWidget *sharedWidget;
109 
111  typedef void (*callback)(void);
112 
116  SecondSingeltonException(const std::string &reason):utils::ICLException(reason){}
117  };
118 
120 
132  ICLApplication(int argc, char **argv, const std::string &paInitString="",
133  callback init=0, callback run=0,
134  callback run2=0, callback run3=0,
135  callback run4=0, callback run5=0);
136 
138  ~ICLApplication();
139 
141 
143  void addThread(callback cb);
144 
146 
147  void addInit(callback cb);
148 
150 
151  void addFinalization(callback cb);
152 
154 
155  void addPrepareShutDown(callback cb);
156 
158 
163  int exec();
164 
168  virtual void execute() = 0;
169 
171  virtual ~AsynchronousEvent(){}
172  };
173 
175 
178  void executeInGUIThread(AsynchronousEvent *event, bool blocking = false, bool forcePostEvent = false);
179 
181 
182  template<class T>
183  void executeInGUIThread(utils::Function<void,T> f, T data, bool blocking = false, bool forcePostEvent = false){
184  struct TmpAsynchronousEvent : public AsynchronousEvent{
185  T data;
187  TmpAsynchronousEvent(T data, utils::Function<void,T> f):data(data),f(f){}
188  void execute(){
189  f(data);
190  }
191  };
192  executeInGUIThread(new TmpAsynchronousEvent(data,f),blocking, forcePostEvent);
193  }
194 
196 
197  template<class T,class U>
198  void executeInGUIThread(utils::Function<void,T,U> f, T t, U u, bool blocking = false, bool forcePostEvent = false){
199  struct TmpAsynchronousEvent : public AsynchronousEvent{
200  T t;
201  U u;
203  TmpAsynchronousEvent(T t, U u, utils::Function<void,T,U> f):t(t),u(u),f(f){}
204  void execute(){
205  f(t,u);
206  }
207  };
208  executeInGUIThread(new TmpAsynchronousEvent(t,u,f),blocking, forcePostEvent);
209  }
210 
211 
213  virtual bool event(QEvent *eIn);
214 
216  static ICLApplication *instance();
217 
219  static bool isGUIThreadActive();
220 
221  private:
224 
226  static std::vector<ExecThread*> s_threads;
227 
229  static std::vector<callback> s_inits;
230 
231 
233  static std::vector<callback> s_callbacks;
234 
236  static std::vector<callback> s_finalizes;
237 
239  static std::vector<callback> s_prepare_shutdowns;
240  };
241 
244  } // namespace qt
245 }
246 
The General Function Template.
Definition: Function.h:284
static std::vector< ExecThread * > s_threads
list of threads
Definition: Application.h:226
QApplication * app
Definition: Application.h:107
undocument this line if you encounter any issues!
Definition: Any.h:37
static std::vector< callback > s_inits
list of initialization functions
Definition: Application.h:229
QApplication extension for ICL based applications.
Definition: Application.h:100
void executeInGUIThread(utils::Function< void, T > f, T data, bool blocking=false, bool forcePostEvent=false)
utility class for executing functions with given arguments in the GUI thread
Definition: Application.h:183
static std::vector< callback > s_callbacks
list of callback functions
Definition: Application.h:233
static std::vector< callback > s_prepare_shutdowns
list of finalization functions
Definition: Application.h:239
ICLQt_API ImgROI data(ImgQ &r)
creates full ROI ROI-struct
interface for events that must be executed in the GUI Thread
Definition: Application.h:166
SecondSingeltonException(const std::string &reason)
Basic constructor.
Definition: Application.h:116
#define ICLQt_API
Definition: CompatMacros.h:178
virtual ~AsynchronousEvent()
virtual destructor
Definition: Application.h:171
Base class for Exception handling in the ICL.
Definition: Exception.h:42
void executeInGUIThread(utils::Function< void, T, U > f, T t, U u, bool blocking=false, bool forcePostEvent=false)
utility class for executing functions with given arguments in the GUI thread
Definition: Application.h:198
ICLApplication ICLApp
this is just a shortcut typedef
Definition: Application.h:243
QGLWidget * sharedWidget
Definition: Application.h:108
static ICLApplication * s_app
singelton instance
Definition: Application.h:223
Such an exception is returned if a 2nd instance of ICLApplication is created.
Definition: Application.h:114
static std::vector< callback > s_finalizes
list of finalization functions
Definition: Application.h:236