arcsappmode.cpp
1 /*
2  name: lib/arcsappmode.cpp
3 
4  This file is part of ARCS - Augmented Reality Component System
5  (version 2-current), written by Jean-Yves Didier
6  for IBISC Laboratory (http://www.ibisc.univ-evry.fr)
7 
8  Copyright (C) 2013 Université d'Evry-Val d'Essonne
9 
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Please send bugreports with examples or suggestions to
25  jean-yves.didier__at__ibisc.univ-evry.fr
26 */
27 
28 
29 #include <arcs/arcsappmode.h>
30 #include <arcs/arcsprocess.h>
31 #include <arcs/arcscontrollercomponent.h>
32 #include <arcs/arcslog.h>
33 
34 #include <QCoreApplication>
35 #include <QObject>
36 #include <QLibrary>
37 #include <QEvent>
38 //#include <iostream>
39 
40 ARCSAppMode::ARCSAppMode(ARCSProcess* parent) //: QObject(parent)
41 {
42  parentProcess = parent ;
43 }
44 
45 /**********************************************************
46  * Classe AppBase
47  **********************************************************/
48 ARCSAppBase::ARCSAppBase(ARCSProcess* parent) : ARCSAppMode(parent)
49 {
50 
51 }
52 
54 {
55  ARCSLog::logInformation("Application mode","base");
56  parent()->getController()->getStateMachine()->start();
57  while(!parent()->hasFinished())
58  parent()->setupNextSheet();
59 }
60 
62 {
63 
64 }
65 
67 {
68 
69 }
70 
71 /**********************************************************
72  * Classe AppEvent
73  **********************************************************/
74 
75 int ARCSAppEvent::eventType = QEvent::registerEventType();
76 
77 ARCSAppEvent::ARCSAppEvent(ARCSProcess* parent) : ARCSAppMode(parent)
78 {
79  initHook();
80 }
81 
82 void ARCSAppEvent::initHook()
83 {
84  int dargc = 1 ;
85  const char* dargv[] = {"ARCS"};
86 
87  if (QCoreApplication::instance() == NULL)
88  /*QCoreApplication* coreApp =*/ new QCoreApplication(dargc, const_cast<char**>(dargv));
89 }
90 
92 {
93  ARCSLog::logInformation("Application mode","event");
94  QCoreApplication* app = QCoreApplication::instance();
95 
96  if (app == NULL)
97  return ;
98 
99  QObject::connect(parent(), SIGNAL(finished()), app, SLOT(quit()), Qt::QueuedConnection);
100  parent()->getController()->getStateMachine()->start();
101 
102  //QCoreApplication::postEvent(parent(), new QEvent(QEvent::ApplicationActivate));
103  app->exec();
104 }
105 
107 {
108  QCoreApplication::postEvent(parent(), new QEvent((QEvent::Type)eventType));
109 }
110 
112 {
113 
114 
115 }
116 
117 
118 /**********************************************************
119  * Classe AppThread
120  **********************************************************/
121 ARCSAppThread::ARCSAppThread(ARCSProcess* parent) : QThread(parent), ARCSAppBase(parent)
122 {
123 
124 }
125 
127 {
128  ARCSLog::logInformation("Application mode","thread");
129  start();
130 }
131 
132 
133 void ARCSAppThread::run()
134 {
135  ARCSAppBase::parent()->getController()->getStateMachine()->start();
136  ARCSAppBase::parent()->getController()->getStateMachine()->moveToThread(this);
137 
138  while(!ARCSAppMode::parent()->hasFinished())
139  ARCSAppMode::parent()->setupNextSheet();
140 }
141 
143 {
144 
145 }
146 
147 /**********************************************************
148  * Classe AppThreadEvent
149  **********************************************************/
150 ARCSAppThreadEvent::ARCSAppThreadEvent(ARCSProcess* parent): QThread(parent), ARCSAppEvent(parent)
151 {
152 
153 
154 }
155 
157 {
158  ARCSLog::logInformation("Application mode","thread event");
159 
160  QThread::parent()->moveToThread(this);
161  ARCSAppEvent::parent()->getController()->getStateMachine()->moveToThread(this);
162  ARCSAppEvent::parent()->getController()->getStateMachine()->start();
163 
164  QObject::connect(QThread::parent(), SIGNAL(finished()), this, SLOT(quit()), Qt::QueuedConnection);
165  start();
166 }
167 
169 {
170  QCoreApplication::postEvent(QThread::parent(),
171  new QEvent((QEvent::Type)ARCSAppEvent::eventType));
172 }
173 
175 {
176 
177 }
178 
179 
180 void ARCSAppThreadEvent::run()
181 {
182  QCoreApplication::postEvent(QThread::parent(), new QEvent(QEvent::ApplicationActivate));
183  exec();
184 }
185 
186 
187 
188 
189 /**********************************************************
190  * Classe AppGui
191  **********************************************************/
192 ARCSAppGUI::ARCSAppGUI(ARCSProcess* parent) : ARCSAppBase(parent)
193 {
194  initHook();
195 }
196 
197 void ARCSAppGUI::initHook()
198 {
199  ARCSLog::logInformation("Application mode","GUI");
200 
201  lib.setLoadHints(QLibrary::ResolveAllSymbolsHint|QLibrary::ExportExternalSymbolsHint);
202  lib.setFileName("arcsguiw");
203  lib.load();
204 
205  if (lib.isLoaded())
206  {
207  start = (VoidProto)lib.resolve("start_gui_loop");
208  init = (VoidProto)lib.resolve("create_gui_application");
209  if (init)
210  init();
211  else
212  {
213  ARCSLog::logCritical("Application mode","cannot start GUI application.");
214  ARCSLog::logCritical("Application mode",lib.errorString());
215  }
216  }
217  else
218  {
219  ARCSLog::logCritical("Application mode","cannot load GUI library.");
220  ARCSLog::logCritical("Application mode",lib.errorString());
221  }
222 
223  // debugging purpose
224  //QCoreApplication* app = QCoreApplication::instance();
225 }
226 
228 {
229  QCoreApplication* app = QCoreApplication::instance();
230 
231  if (app == NULL)
232  {
233  ARCSLog::logCritical("Application mode","GUI does not exist.");
234  return ;
235  }
236 
237  if (app->inherits("QApplication"))
238  QObject::connect(parent(), SIGNAL(finished()), app, SLOT(closeAllWindows()));
239  QObject::connect(parent(), SIGNAL(finished()), app, SLOT(quit()));
240  parent()->getController()->getStateMachine()->start();
241 
242  QCoreApplication::postEvent(parent(), new QEvent(QEvent::ApplicationActivate));
243  if (start)
244  start();
245 }
246 
248 {
249  QCoreApplication::postEvent(parent(), new QEvent((QEvent::Type)ARCSAppEvent::eventType));
250 }
251 
253 {
254 
255 
256 }
257 
258 
virtual void startHook()
Hook for starting applications Subclasses should reimplement this.
Definition: arcsappmode.cpp:53
This class is representing a process under ARCS.
Definition: arcsprocess.h:52
virtual void startHook()
Hook for starting applications Subclasses should reimplement this.
virtual void stopHook()
Hook for stopping applications Subclasses should reimplement this.
virtual void startHook()
Hook for starting applications Subclasses should reimplement this.
Definition: arcsappmode.cpp:91
virtual void startHook()
Hook for starting applications Subclasses should reimplement this.
virtual void startHook()
Hook for starting applications Subclasses should reimplement this.
ARCSStateMachine * getStateMachine()
Base class for launching and handling ARCS applications.
Definition: arcsappmode.h:45
virtual void stopHook()
Hook for stopping applications Subclasses should reimplement this.
virtual void tokenHook()
Hook for handling tokens Subclasses should reimplement this.
virtual void stopHook()
Hook for stopping applications Subclasses should reimplement this.
Definition: arcsappmode.cpp:66
virtual void stopHook()
Hook for stopping applications Subclasses should reimplement this.
Event-loop based application handler.
Definition: arcsappmode.h:92
virtual void tokenHook()
Hook for handling tokens Subclasses should reimplement this.
Definition: arcsappmode.cpp:61
ARCSControllerComponent * getController()
Definition: arcsprocess.h:135
virtual void tokenHook()
Hook for handling tokens Subclasses should reimplement this.
virtual void tokenHook()
Hook for handling tokens Subclasses should reimplement this.
void setupNextSheet()
virtual void stopHook()
Hook for stopping applications Subclasses should reimplement this.
Basic application handler.
Definition: arcsappmode.h:77