Fraxinus  18.10
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 #include <cxLogicManager.h>
12 
13 #include <QApplication>
14 #include <ctkPluginContext.h>
15 #include "cxLogger.h"
16 #include "cxVideoServiceProxy.h"
17 #include "cxStateService.h"
18 #include "cxGPUImageBuffer.h"
19 #include "cxSettings.h"
20 #include "cxSpaceProviderImpl.h"
21 #include "cxDataFactory.h"
22 #include "cxCoreServices.h"
23 #include "cxTypeConversions.h"
24 #include "cxSharedPointerChecker.h"
25 #include "cxPluginFramework.h"
26 #include "cxVideoServiceProxy.h"
27 #include "cxTrackingServiceProxy.h"
29 #include "cxStateServiceProxy.h"
30 #include "cxViewServiceProxy.h"
32 #include "cxReporter.h"
33 #include "cxProfile.h"
34 
35 namespace cx
36 {
37 // --------------------------------------------------------
38 LogicManager* LogicManager::mInstance = NULL;
39 // --------------------------------------------------------
40 
42 {
44 }
45 
47 {
48  LogicManager::getInstance()->basicSetup();
49 }
50 
52 {
53  LogicManager::getInstance()->initializeServices();
55 
56  // we might want to use this one, in order to shutdown within the main loop
57 // connect(qApp, &QApplication::aboutToQuit, LogicManager::getInstance(), &LogicManager::shutdown);
58 }
59 
61 {
62  LogicManager::getInstance()->shutdownServices();
63 
64  delete mInstance;
65  mInstance = NULL;
66 }
67 
68 void LogicManager::initializeServices()
69 {
70  CX_LOG_INFO() << " --- Initialize services for " << qApp->applicationName() << "...";
71 
72  this->basicSetup();
73 
74  mPluginFramework->loadState();
75 
76  if (mComponent)
77  mComponent->create();
78 
79  CX_LOG_DEBUG() << " --- End initialize services.";
80 }
81 
82 void LogicManager::basicSetup()
83 {
86 
87  mPluginFramework = PluginFrameworkManager::create();
88  mPluginFramework->start();
89  mPluginFramework->setSearchPaths(QStringList());
90 
91  this->createLegacyStoredServices();
92 }
93 
94 void LogicManager::createLegacyStoredServices()
95 {
96  // services layer
97  ctkPluginContext* pc = this->getPluginContext();
98 
99  mTrackingService = TrackingServiceProxy::create(pc);
100  mPatientModelService = PatientModelServiceProxy::create(pc);
101  mVideoService = VideoServiceProxy::create(pc);
102  mViewService = ViewServiceProxy::create(pc);
103  connect(mPluginFramework.get(), &PluginFrameworkManager::aboutToStop, mViewService.get(), &ViewService::aboutToStop);
104  mStateService = StateServiceProxy::create(pc);
105  mSessionStorageService = SessionStorageServiceProxy::create(pc);
106 
107  mSpaceProvider.reset(new cx::SpaceProviderImpl(mTrackingService, mPatientModelService));
108 }
109 
111 {
112  if (mComponent)
113  mComponent->destroy();
114 
115  mComponent = component;
116 
117  if (mComponent)
118  mComponent->create();
119 }
120 
122 {
123  QMetaObject::invokeMethod(this, "onRestartWithNewProfile",
124  Qt::QueuedConnection,
125  Q_ARG(QString, uid));
126 }
127 
128 void LogicManager::onRestartWithNewProfile(QString uid)
129 {
130  if (profile()->getUid()==uid)
131  return;
132  this->restartServicesWithProfile(uid);
133 }
134 
136 {
137  this->shutdownServices();
139  this->initializeServices();
140 }
141 
142 void LogicManager::shutdownServices()
143 {
144  CX_LOG_INFO() << " --- Shutting down " << qApp->applicationName() << "...";
145 
146  this->getPatientModelService()->autoSave();
147 
148  if (mComponent)
149  mComponent->destroy(); // this is the GUI - delete first
150 
151  mPluginFramework->stop();
152 
153  this->shutdownLegacyStoredServices();
154  mPluginFramework.reset();
158 
159  CX_LOG_DEBUG() << " --- End shutdown services";
160 }
161 
162 void LogicManager::shutdownLegacyStoredServices()
163 {
164  this->shutdownService(mSpaceProvider, "SpaceProvider"); // remove before patmodel and track
165  this->shutdownService(mStateService, "StateService");
166  this->shutdownService(mViewService, "ViewService");
167  this->shutdownService(mTrackingService, "TrackingService");
168  this->shutdownService(mPatientModelService, "PatientModelService");
169  this->shutdownService(mVideoService, "VideoService");
170  this->shutdownService(mSessionStorageService, "SessionStorageService");
171 }
172 
173 
174 template<class T>
175 void LogicManager::shutdownService(boost::shared_ptr<T>& service, QString name)
176 {
177  requireUnique(service, name);
178  service.reset();
179 }
180 
182 {
183  return mPatientModelService;
184 }
186 {
187  return mTrackingService;
188 }
190 {
191  return mVideoService;
192 }
194 {
195  return mStateService;
196 }
198 {
199  return mSpaceProvider;
200 }
202 {
203  return mViewService;
204 }
206 {
207  return mSessionStorageService;
208 }
210 {
211  return mPluginFramework;
212 }
214 {
215  return this->getPluginFramework()->getPluginContext();
216 }
217 
219 {
220  if (!mInstance)
221  {
222  mInstance = new LogicManager;
223  }
224  return mInstance;
225 }
226 
227 LogicManager::LogicManager()
228 {
229 }
230 
231 LogicManager::~LogicManager()
232 {
233 
234 }
235 
236 }
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:160
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:176
static void shutdown()
shutdown service, destroy static object if none holds a reference.
Definition: cxReporter.cpp:75
#define CX_LOG_INFO
Definition: cxLogger.h:96
static void initialize()
Initialize logging, static object is guaranteed to exist at least until shutdown. ...
Definition: cxReporter.cpp:62
static void shutdown()
Definition: cxProfile.cpp:181
static SessionStorageServicePtr create(ctkPluginContext *pluginContext)
static ProfileManager * getInstance(QString defaultProfile=QString("Laboratory"))
returns the only instance of this class
Definition: cxProfile.cpp:167
boost::shared_ptr< class ViewService > ViewServicePtr
static VideoServicePtr create(ctkPluginContext *pluginContext)
void setActiveProfile(QString uid)
Definition: cxProfile.cpp:308
static TrackingServicePtr create(ctkPluginContext *pluginContext)
Control the custusx backend.
static PatientModelServicePtr create(ctkPluginContext *pluginContext)
SessionStorageServicePtr getSessionStorageService()
VideoServicePtr getVideoService()
static StateServicePtr create(ctkPluginContext *pluginContext)
static LogicManager * getInstance()
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
void restartWithNewProfile(QString uid)
LogicManager * logicManager()
#define CX_LOG_DEBUG
Definition: cxLogger.h:95
boost::shared_ptr< class PluginFrameworkManager > PluginFrameworkManagerPtr
ViewServicePtr getViewService()
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()
Namespace for all CustusX production code.