CustusX  2020.11.07-dev+develop.4ce4a
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"
33 #include "cxReporter.h"
34 #include "cxProfile.h"
35 
36 namespace cx
37 {
38 // --------------------------------------------------------
39 LogicManager* LogicManager::mInstance = NULL;
40 // --------------------------------------------------------
41 
43 {
45 }
46 
48 {
49  LogicManager::getInstance()->basicSetup();
50 }
51 
53 {
54  LogicManager::getInstance()->initializeServices();
56 
57  // we might want to use this one, in order to shutdown within the main loop
58 // connect(qApp, &QApplication::aboutToQuit, LogicManager::getInstance(), &LogicManager::shutdown);
59 }
60 
62 {
63  LogicManager::getInstance()->shutdownServices();
64 
65  delete mInstance;
66  mInstance = NULL;
67 }
68 
69 void LogicManager::initializeServices()
70 {
71  CX_LOG_INFO() << " --- Initialize services for " << qApp->applicationName() << "...";
72 
73  this->basicSetup();
74 
75  mPluginFramework->loadState();
76 
77  if (mComponent)
78  mComponent->create();
79 
80  mShutdown = false;
81  CX_LOG_DEBUG() << " --- End initialize services.";
82 }
83 
84 void LogicManager::basicSetup()
85 {
88 
89  mPluginFramework = PluginFrameworkManager::create();
90  mPluginFramework->start();
91  mPluginFramework->setSearchPaths(QStringList());
92 
93  this->createLegacyStoredServices();
94 }
95 
96 void LogicManager::createLegacyStoredServices()
97 {
98  // services layer
99  ctkPluginContext* pc = this->getPluginContext();
100 
101  //mFileManagerService = FileManagerServiceProxy::create(pc);
102  mTrackingService = TrackingServiceProxy::create(pc);
103  mPatientModelService = PatientModelServiceProxy::create(pc);
104  mVideoService = VideoServiceProxy::create(pc);
105  mViewService = ViewServiceProxy::create(pc);
106  connect(mPluginFramework.get(), &PluginFrameworkManager::aboutToStop, mViewService.get(), &ViewService::aboutToStop);
107  mStateService = StateServiceProxy::create(pc);
108  mSessionStorageService = SessionStorageServiceProxy::create(pc);
109 
110  mSpaceProvider.reset(new cx::SpaceProviderImpl(mTrackingService, mPatientModelService));
111 }
112 
114 {
115  if (mComponent)
116  mComponent->destroy();
117 
118  mComponent = component;
119 
120  if (mComponent)
121  mComponent->create();
122 }
123 
125 {
126  QMetaObject::invokeMethod(this, "onRestartWithNewProfile",
127  Qt::QueuedConnection,
128  Q_ARG(QString, uid));
129 }
130 
131 void LogicManager::onRestartWithNewProfile(QString uid)
132 {
133  if (profile()->getUid()==uid)
134  return;
135  this->restartServicesWithProfile(uid);
136 }
137 
139 {
140  this->shutdownServices();
142  this->initializeServices();
143 }
144 
145 void LogicManager::shutdownServices()
146 {
147  if(mShutdown)
148  {
149  CX_LOG_ERROR() << "Trying to shutdown logicmanager when it already shutdown. Aborting shutdown, fix code.";
150  return;
151  }
152 
153  CX_LOG_INFO() << " --- Shutting down " << qApp->applicationName() << "...";
154 
155  this->getPatientModelService()->autoSave();
156 
157  if (mComponent)
158  mComponent->destroy(); // this is the GUI - delete first
159 
160  mPluginFramework->stop();
161 
162  this->shutdownLegacyStoredServices();
163  mPluginFramework.reset();
167 
168  mShutdown = true;
169  CX_LOG_DEBUG() << " --- End shutdown services";
170 }
171 
172 void LogicManager::shutdownLegacyStoredServices()
173 {
174  this->shutdownService(mSpaceProvider, "SpaceProvider"); // remove before patmodel and track
175  this->shutdownService(mStateService, "StateService");
176  this->shutdownService(mViewService, "ViewService");
177  this->shutdownService(mTrackingService, "TrackingService");
178  this->shutdownService(mPatientModelService, "PatientModelService");
179  this->shutdownService(mVideoService, "VideoService");
180  this->shutdownService(mSessionStorageService, "SessionStorageService");
181 }
182 
183 
184 template<class T>
185 void LogicManager::shutdownService(boost::shared_ptr<T>& service, QString name)
186 {
187  requireUnique(service, name);
188  service.reset();
189 }
190 
192 {
193  return mPatientModelService;
194 }
196 {
197  return mTrackingService;
198 }
200 {
201  return mVideoService;
202 }
204 {
205  return mStateService;
206 }
208 {
209  return mSpaceProvider;
210 }
212 {
213  return mViewService;
214 }
216 {
217  return mSessionStorageService;
218 }
219 
220 //FileManagerServicePtr LogicManager::getFileManagerService()
221 //{
222 // return mFileManagerService;
223 //}
224 
226 {
227  return mPluginFramework;
228 }
230 {
231  return this->getPluginFramework()->getPluginContext();
232 }
233 
235 {
236  if (!mInstance)
237  {
238  mInstance = new LogicManager;
239  }
240  return mInstance;
241 }
242 
243 LogicManager::LogicManager()
244 {
245 }
246 
247 LogicManager::~LogicManager()
248 {
249 
250 }
251 
252 }
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:73
#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:60
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()
#define CX_LOG_ERROR
Definition: cxLogger.h:99
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.