Fraxinus  16.5.0-fx-rc5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxLogicManager.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 #include <cxLogicManager.h>
33 
34 #include <QApplication>
35 #include <ctkPluginContext.h>
36 #include "cxLogger.h"
37 #include "cxVideoServiceProxy.h"
38 #include "cxStateService.h"
39 #include "cxGPUImageBuffer.h"
40 #include "cxSettings.h"
41 #include "cxSpaceProviderImpl.h"
42 #include "cxDataFactory.h"
43 #include "cxCoreServices.h"
44 #include "cxTypeConversions.h"
45 #include "cxSharedPointerChecker.h"
46 #include "cxPluginFramework.h"
47 #include "cxVideoServiceProxy.h"
48 #include "cxTrackingServiceProxy.h"
50 #include "cxStateServiceProxy.h"
51 #include "cxViewServiceProxy.h"
53 #include "cxReporter.h"
54 #include "cxProfile.h"
55 
56 
57 namespace cx
58 {
59 
61 {
62  return logicManager()->getTrackingService();
63 }
65 {
66  return logicManager()->getSpaceProvider();
67 }
69 {
71 }
73 {
74  return logicManager()->getVideoService();
75 }
77 {
78  return logicManager()->getStateService();
79 }
81 {
82  return logicManager()->getViewService();
83 }
85 {
87 }
88 
89 
90 //---------------------------------------------------------
91 //---------------------------------------------------------
92 //---------------------------------------------------------
93 
94 // --------------------------------------------------------
95 LogicManager* LogicManager::mInstance = NULL;
96 // --------------------------------------------------------
97 
99 {
100  return LogicManager::getInstance();
101 }
102 
104 {
105  LogicManager::getInstance()->basicSetup();
106 }
107 
109 {
110  LogicManager::getInstance()->initializeServices();
112 
113  // we might want to use this one, in order to shutdown within the main loop
114 // connect(qApp, &QApplication::aboutToQuit, LogicManager::getInstance(), &LogicManager::shutdown);
115 }
116 
118 {
119  LogicManager::getInstance()->shutdownServices();
120 
121  delete mInstance;
122  mInstance = NULL;
123 }
124 
125 void LogicManager::initializeServices()
126 {
127  CX_LOG_INFO() << " --- Initialize services for " << qApp->applicationName() << "...";
128 
129  this->basicSetup();
130 
131  mPluginFramework->loadState();
132 
133  if (mComponent)
134  mComponent->create();
135 
136  CX_LOG_DEBUG() << " --- End initialize services.";
137 }
138 
139 void LogicManager::basicSetup()
140 {
143 
144  mPluginFramework = PluginFrameworkManager::create();
145  mPluginFramework->start();
146  mPluginFramework->setSearchPaths(QStringList());
147 
148  this->createLegacyStoredServices();
149 }
150 
151 void LogicManager::createLegacyStoredServices()
152 {
153  // services layer
154  ctkPluginContext* pc = this->getPluginContext();
155 
156  mTrackingService = TrackingServiceProxy::create(pc);
157  mPatientModelService = PatientModelServiceProxy::create(pc);
158  mVideoService = VideoServiceProxy::create(pc);
159  mViewService = ViewServiceProxy::create(pc);
160  connect(mPluginFramework.get(), &PluginFrameworkManager::aboutToStop, mViewService.get(), &ViewService::aboutToStop);
161  mStateService = StateServiceProxy::create(pc);
162  mSessionStorageService = SessionStorageServiceProxy::create(pc);
163 
164  mSpaceProvider.reset(new cx::SpaceProviderImpl(mTrackingService, mPatientModelService));
165 }
166 
168 {
169  if (mComponent)
170  mComponent->destroy();
171 
172  mComponent = component;
173 
174  if (mComponent)
175  mComponent->create();
176 }
177 
179 {
180  QMetaObject::invokeMethod(this, "onRestartWithNewProfile",
181  Qt::QueuedConnection,
182  Q_ARG(QString, uid));
183 }
184 
185 void LogicManager::onRestartWithNewProfile(QString uid)
186 {
187  if (profile()->getUid()==uid)
188  return;
189  this->restartServicesWithProfile(uid);
190 }
191 
193 {
194  this->shutdownServices();
196  this->initializeServices();
197 }
198 
199 void LogicManager::shutdownServices()
200 {
201  CX_LOG_INFO() << " --- Shutting down " << qApp->applicationName() << "...";
202 
203  this->getPatientModelService()->autoSave();
204 
205  if (mComponent)
206  mComponent->destroy(); // this is the GUI - delete first
207 
208  mPluginFramework->stop();
209 
210  this->shutdownLegacyStoredServices();
211  mPluginFramework.reset();
215 
216  CX_LOG_DEBUG() << " --- End shutdown services";
217 }
218 
219 void LogicManager::shutdownLegacyStoredServices()
220 {
221  this->shutdownService(mSpaceProvider, "SpaceProvider"); // remove before patmodel and track
222  this->shutdownService(mStateService, "StateService");
223  this->shutdownService(mViewService, "ViewService");
224  this->shutdownService(mTrackingService, "TrackingService");
225  this->shutdownService(mPatientModelService, "PatientModelService");
226  this->shutdownService(mVideoService, "VideoService");
227  this->shutdownService(mSessionStorageService, "SessionStorageService");
228 }
229 
230 
231 template<class T>
232 void LogicManager::shutdownService(boost::shared_ptr<T>& service, QString name)
233 {
234  requireUnique(service, name);
235  service.reset();
236 }
237 
239 {
240  return mPatientModelService;
241 }
243 {
244  return mTrackingService;
245 }
247 {
248  return mVideoService;
249 }
251 {
252  return mStateService;
253 }
255 {
256  return mSpaceProvider;
257 }
259 {
260  return mViewService;
261 }
263 {
264  return mSessionStorageService;
265 }
267 {
268  return mPluginFramework;
269 }
271 {
272  return this->getPluginFramework()->getPluginContext();
273 }
274 
276 {
277  if (!mInstance)
278  {
279  mInstance = new LogicManager;
280  }
281  return mInstance;
282 }
283 
284 LogicManager::LogicManager()
285 {
286 }
287 
288 LogicManager::~LogicManager()
289 {
290 
291 }
292 
293 }
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:176
boost::shared_ptr< class VideoService > VideoServicePtr
boost::shared_ptr< class ApplicationComponent > ApplicationComponentPtr
static void shutdown()
void restartServicesWithProfile(QString uid)
boost::shared_ptr< class StateService > StateServicePtr
ctkPluginContext * getPluginContext()
static void initializeBasic()
boost::shared_ptr< class TrackingService > TrackingServicePtr
TrackingServicePtr getTrackingService()
static void initialize()
Definition: cxProfile.cpp:192
static void shutdown()
shutdown service, destroy static object if none holds a reference.
Definition: cxReporter.cpp:96
#define CX_LOG_INFO
Definition: cxLogger.h:111
static void initialize()
Initialize logging, static object is guaranteed to exist at least until shutdown. ...
Definition: cxReporter.cpp:83
static void shutdown()
Definition: cxProfile.cpp:197
cxLogicManager_EXPORT StateServicePtr stateService()
static SessionStorageServicePtr create(ctkPluginContext *pluginContext)
static ProfileManager * getInstance(QString defaultProfile=QString("Laboratory"))
returns the only instance of this class
Definition: cxProfile.cpp:183
boost::shared_ptr< class ViewService > ViewServicePtr
static VideoServicePtr create(ctkPluginContext *pluginContext)
void setActiveProfile(QString uid)
Definition: cxProfile.cpp:324
static TrackingServicePtr create(ctkPluginContext *pluginContext)
Control the custusx backend.
static PatientModelServicePtr create(ctkPluginContext *pluginContext)
SessionStorageServicePtr getSessionStorageService()
VideoServicePtr getVideoService()
static StateServicePtr create(ctkPluginContext *pluginContext)
cxLogicManager_EXPORT SessionStorageServicePtr sessionStorageService()
static LogicManager * getInstance()
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
void restartWithNewProfile(QString uid)
cxLogicManager_EXPORT SpaceProviderPtr spaceProvider()
LogicManager * logicManager()
#define CX_LOG_DEBUG
Definition: cxLogger.h:110
cxLogicManager_EXPORT ViewServicePtr viewService()
cxLogicManager_EXPORT VideoServicePtr videoService()
cxLogicManager_EXPORT PatientModelServicePtr patientService()
boost::shared_ptr< class PluginFrameworkManager > PluginFrameworkManagerPtr
ViewServicePtr getViewService()
cxLogicManager_EXPORT TrackingServicePtr trackingService()
static ViewServicePtr create(ctkPluginContext *pluginContext)
static PluginFrameworkManagerPtr create()
virtual void aboutToStop()=0
void setApplicationComponent(ApplicationComponentPtr component)
PluginFrameworkManagerPtr getPluginFramework()
void requireUnique(int use_count, QString objectName)
SpaceProviderPtr getSpaceProvider()
boost::shared_ptr< class SessionStorageService > SessionStorageServicePtr
static void initialize(ApplicationComponentPtr component=ApplicationComponentPtr())
PatientModelServicePtr getPatientModelService()
StateServicePtr getStateService()