CustusX  15.3.4-beta
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 <ctkPluginContext.h>
35 #include "cxLogger.h"
36 #include "cxVideoServiceProxy.h"
37 #include "cxStateService.h"
38 #include "cxGPUImageBuffer.h"
39 #include "cxSettings.h"
40 #include "cxSpaceProviderImpl.h"
41 #include "cxDataFactory.h"
42 #include "cxCoreServices.h"
43 #include "cxTypeConversions.h"
44 #include "cxSharedPointerChecker.h"
45 #include "cxPluginFramework.h"
46 #include "cxVideoServiceProxy.h"
47 #include "cxTrackingServiceProxy.h"
49 #include "cxStateServiceProxy.h"
50 #include "cxViewServiceProxy.h"
52 #include "cxReporter.h"
53 #include "cxProfile.h"
54 
55 
56 namespace cx
57 {
58 
60 {
61  return logicManager()->getTrackingService();
62 }
64 {
65  return logicManager()->getSpaceProvider();
66 }
68 {
70 }
72 {
73  return logicManager()->getVideoService();
74 }
76 {
77  return logicManager()->getStateService();
78 }
80 {
81  return logicManager()->getViewService();
82 }
84 {
86 }
87 
88 
89 //---------------------------------------------------------
90 //---------------------------------------------------------
91 //---------------------------------------------------------
92 
93 // --------------------------------------------------------
94 LogicManager* LogicManager::mInstance = NULL;
95 // --------------------------------------------------------
96 
98 {
100 }
101 
103 {
104  LogicManager::getInstance()->initializeServices();
105  LogicManager::getInstance()->setApplicationComponent(component);
106 
107 }
108 
110 {
111  LogicManager::getInstance()->shutdownServices();
112 
113  delete mInstance;
114  mInstance = NULL;
115 }
116 
117 void LogicManager::initializeServices()
118 {
119  CX_LOG_DEBUG() << " --- Begin initialize services";
120  // resources layer
123 
124  mPluginFramework = PluginFrameworkManager::create();
125  mPluginFramework->start();
126 
127  // services layer
128  ctkPluginContext* pc = this->getPluginContext();
129 
130  mTrackingService = TrackingServiceProxy::create(pc);
131  mPatientModelService = PatientModelServiceProxy::create(pc);
132  mVideoService = VideoServiceProxy::create(pc);
133  mViewService = VisualizationServiceProxy::create(pc);
134  connect(mPluginFramework.get(), &PluginFrameworkManager::aboutToStop, mViewService.get(), &VisualizationService::aboutToStop);
135  mStateService = StateServiceProxy::create(pc);
136  mSessionStorageService = SessionStorageServiceProxy::create(pc);
137 
138  mSpaceProvider.reset(new cx::SpaceProviderImpl(mTrackingService, mPatientModelService));
139 
140  mPluginFramework->loadState();
141 
142  if (mComponent)
143  mComponent->create();
144  CX_LOG_DEBUG() << " --- End initialize services";
145 }
146 
147 void LogicManager::setApplicationComponent(ApplicationComponentPtr component)
148 {
149  if (mComponent)
150  mComponent->destroy();
151 
152  mComponent = component;
153 
154  if (mComponent)
155  mComponent->create();
156 }
157 
159 {
160  QMetaObject::invokeMethod(this, "onRestartWithNewProfile",
161  Qt::QueuedConnection,
162  Q_ARG(QString, uid));
163 }
164 
165 void LogicManager::onRestartWithNewProfile(QString uid)
166 {
167  if (profile()->getUid()==uid)
168  return;
169  this->shutdownServices();
171  this->initializeServices();
172 }
173 
174 void LogicManager::shutdownServices()
175 {
176  CX_LOG_DEBUG() << " --- Begin shutdown services";
177  if (mComponent)
178  mComponent->destroy(); // this is the GUI - delete first
179 
180  mPluginFramework->stop();
181 
182  this->shutdownService(mSpaceProvider, "SpaceProvider"); // remove before patmodel and track
183  this->shutdownService(mStateService, "StateService");
184  this->shutdownService(mViewService, "ViewService");
185  this->shutdownService(mTrackingService, "TrackingService");
186  this->shutdownService(mPatientModelService, "PatientModelService");
187  this->shutdownService(mVideoService, "VideoService");
188  this->shutdownService(mSessionStorageService, "SessionStorageService");
189 
190  this->shutdownService(mPluginFramework, "PluginFramework");
191 
195  CX_LOG_DEBUG() << " --- End shutdown services";
196 }
197 
198 template<class T>
199 void LogicManager::shutdownService(boost::shared_ptr<T>& service, QString name)
200 {
201  requireUnique(service, name);
202  service.reset();
203 }
204 
206 {
207  return mPatientModelService;
208 }
210 {
211  return mTrackingService;
212 }
214 {
215  return mVideoService;
216 }
218 {
219  return mStateService;
220 }
222 {
223  return mSpaceProvider;
224 }
226 {
227  return mViewService;
228 }
230 {
231  return mSessionStorageService;
232 }
234 {
235  return mPluginFramework;
236 }
238 {
239  return this->getPluginFramework()->getPluginContext();
240 }
241 
243 {
244  if (!mInstance)
245  {
246  mInstance = new LogicManager;
247  }
248  return mInstance;
249 }
250 
251 LogicManager::LogicManager()
252 {
253 }
254 
255 LogicManager::~LogicManager()
256 {
257 
258 }
259 
260 }
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:142
boost::shared_ptr< class VideoService > VideoServicePtr
boost::shared_ptr< class ApplicationComponent > ApplicationComponentPtr
static void shutdown()
boost::shared_ptr< class StateService > StateServicePtr
ctkPluginContext * getPluginContext()
boost::shared_ptr< class TrackingService > TrackingServicePtr
TrackingServicePtr getTrackingService()
static void initialize()
Definition: cxProfile.cpp:158
static void shutdown()
shutdown service, destroy static object if none holds a reference.
Definition: cxReporter.cpp:96
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:163
cxLogicManager_EXPORT StateServicePtr stateService()
static SessionStorageServicePtr create(ctkPluginContext *pluginContext)
static VideoServicePtr create(ctkPluginContext *pluginContext)
static VisualizationServicePtr create(ctkPluginContext *pluginContext)
boost::shared_ptr< class VisualizationService > ViewServicePtr
void setActiveProfile(QString uid)
Definition: cxProfile.cpp:290
static TrackingServicePtr create(ctkPluginContext *pluginContext)
Control the custusx backend.
static PatientModelServicePtr create(ctkPluginContext *pluginContext)
SessionStorageServicePtr getSessionStorageService()
VideoServicePtr getVideoService()
virtual void aboutToStop()=0
static ProfileManager * getInstance()
returns the only instance of this class
Definition: cxProfile.cpp:149
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:109
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 PluginFrameworkManagerPtr create()
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()