Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Application.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLQt/src/ICLQt/Application.h                          **
00010 ** Module : ICLQt                                                  **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLUtils/CompatMacros.h>
00034 #include <ICLUtils/Exception.h>
00035 #include <ICLUtils/Function.h>
00036 #include <string>
00037 #include <vector>
00038 
00039 #include <QApplication>
00040 #include <QtOpenGL/QGLWidget>
00041 
00042 namespace icl{
00043 
00044   namespace qt{
00045 
00047     struct ExecThread;
00050 
00051 
00100     class ICLQt_API ICLApplication : public QObject{
00101       
00102       public:
00103       QApplication *app;
00104       QGLWidget *sharedWidget;
00105       
00107       typedef void (*callback)(void);
00108       
00110       struct SecondSingeltonException : public utils::ICLException{
00112         SecondSingeltonException(const std::string &reason):utils::ICLException(reason){}
00113       };
00114   
00116 
00128       ICLApplication(int argc, char **argv, const std::string &paInitString="", 
00129                      callback init=0, callback run=0,
00130                      callback run2=0, callback run3=0,
00131                      callback run4=0, callback run5=0)throw (SecondSingeltonException);
00132   
00134       ~ICLApplication();
00135       
00137 
00139       void addThread(callback cb);
00140   
00142 
00143       void addInit(callback cb);
00144       
00146 
00147       void addFinalization(callback cb);
00148   
00150 
00155       int exec();
00156       
00158       struct AsynchronousEvent{
00160         virtual void execute() = 0;
00161         
00163         virtual ~AsynchronousEvent(){}
00164       };
00165       
00167 
00170       void executeInGUIThread(AsynchronousEvent *event, bool blocking = false, bool forcePostEvent = false);
00171       
00173 
00174       template<class T>
00175       void executeInGUIThread(utils::Function<void,T> f, T data, bool blocking = false, bool forcePostEvent = false){
00176         struct TmpAsynchronousEvent : public AsynchronousEvent{
00177           T data;
00178           utils::Function<void,T> f;
00179           TmpAsynchronousEvent(T data, utils::Function<void,T> f):data(data),f(f){}
00180           void execute(){
00181             f(data);
00182           }
00183         };
00184         executeInGUIThread(new TmpAsynchronousEvent(data,f),blocking, forcePostEvent);
00185       }
00186       
00188 
00189       template<class T,class U>
00190       void executeInGUIThread(utils::Function<void,T,U> f, T t, U u, bool blocking = false, bool forcePostEvent = false){
00191         struct TmpAsynchronousEvent : public AsynchronousEvent{
00192           T t;
00193           U u;
00194           utils::Function<void,T,U> f;
00195           TmpAsynchronousEvent(T t, U u, utils::Function<void,T,U> f):t(t),u(u),f(f){}
00196           void execute(){
00197             f(t,u);
00198           }
00199         };
00200         executeInGUIThread(new TmpAsynchronousEvent(t,u,f),blocking, forcePostEvent);
00201       }
00202       
00203 
00205       virtual bool event(QEvent *eIn);
00206       
00208       static ICLApplication *instance();
00209       
00211       static bool isGUIThreadActive();
00212 
00213       private:
00215       static ICLApplication *s_app;
00216       
00218       static std::vector<ExecThread*> s_threads;
00219       
00221       static std::vector<callback> s_inits;
00222       
00223       
00225       static std::vector<callback> s_callbacks;
00226   
00228       static std::vector<callback> s_finalizes;
00229     };
00230   
00232     typedef ICLApplication ICLApp; 
00233   } // namespace qt
00234 }
00235 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines