Fraxinus  17.12-rc4
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) 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 namespace cx
57 {
58 // --------------------------------------------------------
59 LogicManager* LogicManager::mInstance = NULL;
60 // --------------------------------------------------------
61 
63 {
65 }
66 
68 {
69  LogicManager::getInstance()->basicSetup();
70 }
71 
73 {
74  LogicManager::getInstance()->initializeServices();
76 
77  // we might want to use this one, in order to shutdown within the main loop
78 // connect(qApp, &QApplication::aboutToQuit, LogicManager::getInstance(), &LogicManager::shutdown);
79 }
80 
82 {
83  LogicManager::getInstance()->shutdownServices();
84 
85  delete mInstance;
86  mInstance = NULL;
87 }
88 
89 void LogicManager::initializeServices()
90 {
91  CX_LOG_INFO() << " --- Initialize services for " << qApp->applicationName() << "...";
92 
93  this->basicSetup();
94 
95  mPluginFramework->loadState();
96 
97  if (mComponent)
98  mComponent->create();
99 
100  CX_LOG_DEBUG() << " --- End initialize services.";
101 }
102 
103 void LogicManager::basicSetup()
104 {
107 
108  mPluginFramework = PluginFrameworkManager::create();
109  mPluginFramework->start();
110  mPluginFramework->setSearchPaths(QStringList());
111 
112  this->createLegacyStoredServices();
113 }
114 
115 void LogicManager::createLegacyStoredServices()
116 {
117  // services layer
118  ctkPluginContext* pc = this->getPluginContext();
119 
120  mTrackingService = TrackingServiceProxy::create(pc);
121  mPatientModelService = PatientModelServiceProxy::create(pc);
122  mVideoService = VideoServiceProxy::create(pc);
123  mViewService = ViewServiceProxy::create(pc);
124  connect(mPluginFramework.get(), &PluginFrameworkManager::aboutToStop, mViewService.get(), &ViewService::aboutToStop);
125  mStateService = StateServiceProxy::create(pc);
126  mSessionStorageService = SessionStorageServiceProxy::create(pc);
127 
128  mSpaceProvider.reset(new cx::SpaceProviderImpl(mTrackingService, mPatientModelService));
129 }
130 
132 {
133  if (mComponent)
134  mComponent->destroy();
135 
136  mComponent = component;
137 
138  if (mComponent)
139  mComponent->create();
140 }
141 
143 {
144  QMetaObject::invokeMethod(this, "onRestartWithNewProfile",
145  Qt::QueuedConnection,
146  Q_ARG(QString, uid));
147 }
148 
149 void LogicManager::onRestartWithNewProfile(QString uid)
150 {
151  if (profile()->getUid()==uid)
152  return;
153  this->restartServicesWithProfile(uid);
154 }
155 
157 {
158  this->shutdownServices();
160  this->initializeServices();
161 }
162 
163 void LogicManager::shutdownServices()
164 {
165  CX_LOG_INFO() << " --- Shutting down " << qApp->applicationName() << "...";
166 
167  this->getPatientModelService()->autoSave();
168 
169  if (mComponent)
170  mComponent->destroy(); // this is the GUI - delete first
171 
172  mPluginFramework->stop();
173 
174  this->shutdownLegacyStoredServices();
175  mPluginFramework.reset();
179 
180  CX_LOG_DEBUG() << " --- End shutdown services";
181 }
182 
183 void LogicManager::shutdownLegacyStoredServices()
184 {
185  this->shutdownService(mSpaceProvider, "SpaceProvider"); // remove before patmodel and track
186  this->shutdownService(mStateService, "StateService");
187  this->shutdownService(mViewService, "ViewService");
188  this->shutdownService(mTrackingService, "TrackingService");
189  this->shutdownService(mPatientModelService, "PatientModelService");
190  this->shutdownService(mVideoService, "VideoService");
191  this->shutdownService(mSessionStorageService, "SessionStorageService");
192 }
193 
194 
195 template<class T>
196 void LogicManager::shutdownService(boost::shared_ptr<T>& service, QString name)
197 {
198  requireUnique(service, name);
199  service.reset();
200 }
201 
203 {
204  return mPatientModelService;
205 }
207 {
208  return mTrackingService;
209 }
211 {
212  return mVideoService;
213 }
215 {
216  return mStateService;
217 }
219 {
220  return mSpaceProvider;
221 }
223 {
224  return mViewService;
225 }
227 {
228  return mSessionStorageService;
229 }
231 {
232  return mPluginFramework;
233 }
235 {
236  return this->getPluginFramework()->getPluginContext();
237 }
238 
240 {
241  if (!mInstance)
242  {
243  mInstance = new LogicManager;
244  }
245  return mInstance;
246 }
247 
248 LogicManager::LogicManager()
249 {
250 }
251 
252 LogicManager::~LogicManager()
253 {
254 
255 }
256 
257 }
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:181
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:197
static void shutdown()
shutdown service, destroy static object if none holds a reference.
Definition: cxReporter.cpp:96
#define CX_LOG_INFO
Definition: cxLogger.h:117
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:202
static SessionStorageServicePtr create(ctkPluginContext *pluginContext)
static ProfileManager * getInstance(QString defaultProfile=QString("Laboratory"))
returns the only instance of this class
Definition: cxProfile.cpp:188
boost::shared_ptr< class ViewService > ViewServicePtr
static VideoServicePtr create(ctkPluginContext *pluginContext)
void setActiveProfile(QString uid)
Definition: cxProfile.cpp:329
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:116
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.