CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxRecordSessionSelector.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 =========================================================================*/
33 
34 #include "cxRecordSessionWidget.h"
35 #include "cxRecordSession.h"
36 #include "cxToolRep3D.h"
37 #include "cxToolTracer.h"
38 #include "cxLogger.h"
39 #include "cxPatientModelService.h"
40 #include "cxViewService.h"
41 #include "cxStringProperty.h"
43 #include "cxTrackingService.h"
44 #include "cxProfile.h"
45 #include "cxHelperWidgets.h"
46 #include "cxRepContainer.h"
49 #include "cxAcquisitionService.h"
50 
51 
52 namespace cx
53 {
54 
57  VisServicesPtr services) :
58  mServices(services),
59  mOptions(options),
60  mAcquisitionService(acquisitionService),
61  mVisible(true)
62 {
63  mSessionSelector = StringProperty::initialize("tracking_session",
64  "Tracking Data",
65  "Select tracker data", "", QStringList(),
66  mOptions.getElement());
67  connect(mAcquisitionService.get(), &AcquisitionService::recordedSessionsChanged, this, &SelectRecordSession::recordedSessionsChanged);
68  connect(mSessionSelector.get(), &StringProperty::changed, this, &SelectRecordSession::showSelectedRecordingInView);
69 
70  this->recordedSessionsChanged();
71  this->updateHelpText();
72 }
73 
75 {
76  this->clearTracer();
77 }
78 
80 {
81  if (tool==mToolOverride)
82  return;
83 
84  mToolOverride = tool;
85 
86  this->showSelectedRecordingInView();
87 }
88 
89 void SelectRecordSession::recordedSessionsChanged()
90 {
91  std::vector<RecordSessionPtr> sessions = mAcquisitionService->getSessions();
92  QStringList uids;
93  std::map<QString, QString> names;
94  for(unsigned i=0; i<sessions.size(); ++i)
95  {
96  QString uid = sessions[i]->getUid();
97  uids << uid;
98  names[uid] = sessions[i]->getHumanDescription();
99  }
100  uids << "";
101  names[""] = "<none>";
102 
103  mSessionSelector->setValueRange(uids);
104  mSessionSelector->setDisplayNames(names);
105 
106  if(mSessionSelector->getValue().isEmpty() && !uids.isEmpty())
107  mSessionSelector->setValue(uids.last());
108 
109  this->showSelectedRecordingInView();
110 }
111 
112 void SelectRecordSession::warnIfNoTrackingDataInSession()
113 {
114  RecordSessionPtr session = this->getSession();
115  ToolPtr tool = this->getTool();
116  if (session && !tool)
117  {
118  CX_LOG_WARNING() << QString("Could not find any tracking data for any loaded tools in session [%2]. ")
119  .arg(session.get() ? session->getHumanDescription() : "NULL");
120  }
121 }
122 
123 RecordSessionPtr SelectRecordSession::getSession()
124 {
125  QString uid = mSessionSelector->getValue();
126  if(!uid.isEmpty())
127  return mAcquisitionService->getSession(uid);
128  return RecordSessionPtr();
129 }
130 
132 {
133  RecordSessionPtr session = this->getSession();
134  ToolPtr tool = this->getTool();
135 
136  TimedTransformMap trackerRecordedData_prMt = RecordSession::getToolHistory_prMt(tool, session, false);
137 
138  return trackerRecordedData_prMt;
139 }
140 
142 {
143  if (mToolOverride)
144  return mToolOverride;
145 
146  RecordSessionPtr session = this->getSession();
147  TrackingService::ToolMap tools = mServices->tracking()->getTools();
148 
149  ToolPtr tool = this->findToolContainingMostDataForSession(tools, session);
150  return tool;
151 }
152 
154 {
155  mVisible = on;
156  this->showSelectedRecordingInView();
157 }
158 
159 ToolPtr SelectRecordSession::findToolContainingMostDataForSession(std::map<QString, ToolPtr> tools, RecordSessionPtr session)
160 {
161  std::map<int,ToolPtr> tooldata;
162 
163  for (TrackingService::ToolMap::iterator i=tools.begin(); i!=tools.end(); ++i)
164  {
165  if (i->second->hasType(Tool::TOOL_REFERENCE))
166  continue;
167  TimedTransformMap trackerRecordedData_prMt = RecordSession::getToolHistory_prMt(i->second, session, false);
168  tooldata[trackerRecordedData_prMt.size()] = i->second;
169  }
170 
171  if (!tooldata.empty() && (tooldata.rbegin()->first!=0))
172  return tooldata.rbegin()->second;
173 
174  return ToolPtr();
175 }
176 
177 void SelectRecordSession::updateHelpText()
178 {
179  ToolPtr tool = this->getTool();
180  QString toolname = tool ? tool->getName() : "<none>";
181  mSessionSelector->setHelp(QString("Select tracker data, current data uses tool %2")
182  .arg(toolname));
183 }
184 
185 ToolRep3DPtr SelectRecordSession::getToolRepIn3DView(ToolPtr tool)
186 {
187  return mServices->view()->get3DReps(0, 0)->findFirst<ToolRep3D>(tool);
188 }
189 
190 void SelectRecordSession::showSelectedRecordingInView()
191 {
192  this->clearTracer();
193  if (!mVisible)
194  return;
195 
196  this->warnIfNoTrackingDataInSession();
197 
198  TimedTransformMap trackerRecordedData_prMt = this->getRecordedTrackerData_prMt();
199  if (trackerRecordedData_prMt.empty())
200  return;
201 
202  mCurrentTracedTool = this->getTool();
203  this->updateHelpText();
204 
205  ToolRep3DPtr activeRep3D = this->getToolRepIn3DView(mCurrentTracedTool);
206  if(!activeRep3D)
207  return;
208 
209  if(!trackerRecordedData_prMt.empty())
210  {
211  activeRep3D->getTracer()->clear();
212  activeRep3D->getTracer()->setColor(QColor("green"));
213  activeRep3D->getTracer()->addManyPositions(trackerRecordedData_prMt);
214  }
215  else
216  {
217  activeRep3D->getTracer()->clear();
218  }
219 }
220 
221 void SelectRecordSession::clearTracer()
222 {
223  ToolRep3DPtr activeRep3D = this->getToolRepIn3DView(mCurrentTracedTool);
224 
225  if(activeRep3D)
226  {
227  activeRep3D->getTracer()->clear();
228  }
229 
230  mCurrentTracedTool.reset();
231 }
232 
233 } //namespace cx
SelectRecordSession(XmlOptionFile options, AcquisitionServicePtr acquisitionService, VisServicesPtr services)
boost::shared_ptr< class AcquisitionService > AcquisitionServicePtr
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:62
QDomElement getElement()
return the current element
static TimedTransformMap getToolHistory_prMt(ToolPtr tool, RecordSessionPtr session, bool verbose)
Reference tool.
Definition: cxTool.h:105
boost::shared_ptr< class RecordSession > RecordSessionPtr
TimedTransformMap getRecordedTrackerData_prMt()
cxLogicManager_EXPORT AcquisitionServicePtr acquisitionService()
void changed()
emit when the underlying data value is changed: The user interface will be updated.
static StringPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList range, QDomNode root=QDomNode())
std::map< QString, ToolPtr > ToolMap
#define CX_LOG_WARNING
Definition: cxLogger.h:113
boost::shared_ptr< class ToolRep3D > ToolRep3DPtr
Helper class for xml files used to store ssc/cx data.
std::map< double, Transform3D > TimedTransformMap
boost::shared_ptr< class Tool > ToolPtr