NorMIT-nav  16.5
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->shutdownServices();
191  this->initializeServices();
192 }
193 
194 void LogicManager::shutdownServices()
195 {
196  CX_LOG_INFO() << " --- Shutting down " << qApp->applicationName() << "...";
197 
198  this->getPatientModelService()->autoSave();
199 
200  if (mComponent)
201  mComponent->destroy(); // this is the GUI - delete first
202 
203  mPluginFramework->stop();
204 
205  this->shutdownLegacyStoredServices();
206  mPluginFramework.reset();
210 
211  CX_LOG_DEBUG() << " --- End shutdown services";
212 }
213 
214 void LogicManager::shutdownLegacyStoredServices()
215 {
216  this->shutdownService(mSpaceProvider, "SpaceProvider"); // remove before patmodel and track
217  this->shutdownService(mStateService, "StateService");
218  this->shutdownService(mViewService, "ViewService");
219  this->shutdownService(mTrackingService, "TrackingService");
220  this->shutdownService(mPatientModelService, "PatientModelService");
221  this->shutdownService(mVideoService, "VideoService");
222  this->shutdownService(mSessionStorageService, "SessionStorageService");
223 }
224 
225 
226 template<class T>
227 void LogicManager::shutdownService(boost::shared_ptr<T>& service, QString name)
228 {
229  requireUnique(service, name);
230  service.reset();
231 }
232 
234 {
235  return mPatientModelService;
236 }
238 {
239  return mTrackingService;
240 }
242 {
243  return mVideoService;
244 }
246 {
247  return mStateService;
248 }
250 {
251  return mSpaceProvider;
252 }
254 {
255  return mViewService;
256 }
258 {
259  return mSessionStorageService;
260 }
262 {
263  return mPluginFramework;
264 }
266 {
267  return this->getPluginFramework()->getPluginContext();
268 }
269 
271 {
272  if (!mInstance)
273  {
274  mInstance = new LogicManager;
275  }
276  return mInstance;
277 }
278 
279 LogicManager::LogicManager()
280 {
281 }
282 
283 LogicManager::~LogicManager()
284 {
285 
286 }
287 
288 }
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()
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)
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 ProfileManager * getInstance()
returns the only instance of this class
Definition: cxProfile.cpp:183
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()