CustusX  15.4.0-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 <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()->initializeServices();
106  LogicManager::getInstance()->setApplicationComponent(component);
107 
108  // we might want to use this one, in order to shutdown within the main loop
109 // connect(qApp, &QApplication::aboutToQuit, LogicManager::getInstance(), &LogicManager::shutdown);
110 }
111 
113 {
114  LogicManager::getInstance()->shutdownServices();
115 
116  delete mInstance;
117  mInstance = NULL;
118 }
119 
120 void LogicManager::initializeServices()
121 {
122  CX_LOG_INFO() << " --- Initialize services for " << qApp->applicationName() << "...";
123  // resources layer
126 
127  mPluginFramework = PluginFrameworkManager::create();
128  mPluginFramework->start();
129 
130  // services layer
131  ctkPluginContext* pc = this->getPluginContext();
132 
133  mTrackingService = TrackingServiceProxy::create(pc);
134  mPatientModelService = PatientModelServiceProxy::create(pc);
135  mVideoService = VideoServiceProxy::create(pc);
136  mViewService = VisualizationServiceProxy::create(pc);
137  connect(mPluginFramework.get(), &PluginFrameworkManager::aboutToStop, mViewService.get(), &VisualizationService::aboutToStop);
138  mStateService = StateServiceProxy::create(pc);
139  mSessionStorageService = SessionStorageServiceProxy::create(pc);
140 
141  mSpaceProvider.reset(new cx::SpaceProviderImpl(mTrackingService, mPatientModelService));
142 
143  mPluginFramework->loadState();
144 
145  if (mComponent)
146  mComponent->create();
147  CX_LOG_DEBUG() << " --- End initialize services.";
148 }
149 
150 void LogicManager::setApplicationComponent(ApplicationComponentPtr component)
151 {
152  if (mComponent)
153  mComponent->destroy();
154 
155  mComponent = component;
156 
157  if (mComponent)
158  mComponent->create();
159 }
160 
162 {
163  QMetaObject::invokeMethod(this, "onRestartWithNewProfile",
164  Qt::QueuedConnection,
165  Q_ARG(QString, uid));
166 }
167 
168 void LogicManager::onRestartWithNewProfile(QString uid)
169 {
170  if (profile()->getUid()==uid)
171  return;
172  this->shutdownServices();
174  this->initializeServices();
175 }
176 
177 void LogicManager::shutdownServices()
178 {
179  CX_LOG_INFO() << " --- Shutting down " << qApp->applicationName() << "...";
180 
181  this->getPatientModelService()->autoSave();
182 
183  if (mComponent)
184  mComponent->destroy(); // this is the GUI - delete first
185 
186  mPluginFramework->stop();
187 
188  this->shutdownService(mSpaceProvider, "SpaceProvider"); // remove before patmodel and track
189  this->shutdownService(mStateService, "StateService");
190  this->shutdownService(mViewService, "ViewService");
191  this->shutdownService(mTrackingService, "TrackingService");
192  this->shutdownService(mPatientModelService, "PatientModelService");
193  this->shutdownService(mVideoService, "VideoService");
194  this->shutdownService(mSessionStorageService, "SessionStorageService");
195 
196  this->shutdownService(mPluginFramework, "PluginFramework");
197 
201 
202  CX_LOG_DEBUG() << " --- End shutdown services";
203 }
204 
205 template<class T>
206 void LogicManager::shutdownService(boost::shared_ptr<T>& service, QString name)
207 {
208  requireUnique(service, name);
209  service.reset();
210 }
211 
213 {
214  return mPatientModelService;
215 }
217 {
218  return mTrackingService;
219 }
221 {
222  return mVideoService;
223 }
225 {
226  return mStateService;
227 }
229 {
230  return mSpaceProvider;
231 }
233 {
234  return mViewService;
235 }
237 {
238  return mSessionStorageService;
239 }
241 {
242  return mPluginFramework;
243 }
245 {
246  return this->getPluginFramework()->getPluginContext();
247 }
248 
250 {
251  if (!mInstance)
252  {
253  mInstance = new LogicManager;
254  }
255  return mInstance;
256 }
257 
258 LogicManager::LogicManager()
259 {
260 }
261 
262 LogicManager::~LogicManager()
263 {
264 
265 }
266 
267 }
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
#define CX_LOG_INFO
Definition: cxLogger.h:110
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()