CustusX  2023.01.05-dev+develop.0da12
An IGT application
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) 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 =========================================================================*/
12 
13 #include "cxRecordSessionWidget.h"
14 #include "cxRecordSession.h"
15 #include "cxToolRep3D.h"
16 #include "cxToolTracer.h"
17 #include "cxLogger.h"
18 #include "cxPatientModelService.h"
19 #include "cxViewService.h"
20 #include "cxStringProperty.h"
22 #include "cxTrackingService.h"
23 #include "cxProfile.h"
24 #include "cxHelperWidgets.h"
25 #include "cxRepContainer.h"
28 #include "cxAcquisitionService.h"
29 
30 
31 namespace cx
32 {
33 
35  AcquisitionServicePtr acquisitionService,
36  VisServicesPtr services) :
37  mServices(services),
38  mOptions(options),
39  mAcquisitionService(acquisitionService),
40  mVisible(true)
41 {
42  mSessionSelector = StringProperty::initialize("tracking_session",
43  "Tracking Data",
44  "Select tracker data", "", QStringList(),
45  mOptions.getElement());
46  connect(mAcquisitionService.get(), &AcquisitionService::recordedSessionsChanged, this, &SelectRecordSession::recordedSessionsChanged);
47  connect(mSessionSelector.get(), &StringProperty::changed, this, &SelectRecordSession::showSelectedRecordingInView);
48 
49  this->recordedSessionsChanged();
50  this->updateHelpText();
51 }
52 
54 {
55  this->clearTracer();
56 }
57 
59 {
60  if (tool==mToolOverride)
61  return;
62 
63  mToolOverride = tool;
64 
65  this->showSelectedRecordingInView();
66 }
67 
68 void SelectRecordSession::recordedSessionsChanged()
69 {
70  std::vector<RecordSessionPtr> sessions = mAcquisitionService->getSessions();
71  QStringList uids;
72  std::map<QString, QString> names;
73  for(unsigned i=0; i<sessions.size(); ++i)
74  {
75  QString uid = sessions[i]->getUid();
76  uids << uid;
77  names[uid] = sessions[i]->getHumanDescription();
78  }
79  uids << "";
80  names[""] = "<none>";
81 
82  mSessionSelector->setValueRange(uids);
83  mSessionSelector->setDisplayNames(names);
84 
85  if(mSessionSelector->getValue().isEmpty() && !uids.isEmpty())
86  mSessionSelector->setValue(uids.last());
87 
88  this->showSelectedRecordingInView();
89 }
90 
91 void SelectRecordSession::warnIfNoTrackingDataInSession()
92 {
93  RecordSessionPtr session = this->getSession();
94  ToolPtr tool = this->getTool();
95  if (session && !tool)
96  {
97  CX_LOG_WARNING() << QString("Could not find any tracking data for any loaded tools in session [%2]. ")
98  .arg(session.get() ? session->getHumanDescription() : "NULL");
99  }
100 }
101 
102 RecordSessionPtr SelectRecordSession::getSession()
103 {
104  QString uid = mSessionSelector->getValue();
105  if(!uid.isEmpty())
106  return mAcquisitionService->getSession(uid);
107  return RecordSessionPtr();
108 }
109 
111 {
112  RecordSessionPtr session = this->getSession();
113  ToolPtr tool = this->getTool();
114 
115  TimedTransformMap trackerRecordedData_prMt = RecordSession::getToolHistory_prMt(tool, session, false);
116 
117  return trackerRecordedData_prMt;
118 }
119 
121 {
122  if (mToolOverride)
123  return mToolOverride;
124 
125  RecordSessionPtr session = this->getSession();
126  TrackingService::ToolMap tools = mServices->tracking()->getTools();
127 
128  ToolPtr tool = this->findToolContainingMostDataForSession(tools, session);
129  return tool;
130 }
131 
133 {
134  mVisible = on;
135  this->showSelectedRecordingInView();
136 }
137 
138 ToolPtr SelectRecordSession::findToolContainingMostDataForSession(std::map<QString, ToolPtr> tools, RecordSessionPtr session)
139 {
140  std::map<int,ToolPtr> tooldata;
141 
142  for (TrackingService::ToolMap::iterator i=tools.begin(); i!=tools.end(); ++i)
143  {
144  if (i->second->hasType(Tool::TOOL_REFERENCE))
145  continue;
146  TimedTransformMap trackerRecordedData_prMt = RecordSession::getToolHistory_prMt(i->second, session, false);
147  tooldata[trackerRecordedData_prMt.size()] = i->second;
148  }
149 
150  if (!tooldata.empty() && (tooldata.rbegin()->first!=0))
151  return tooldata.rbegin()->second;
152 
153  return ToolPtr();
154 }
155 
156 void SelectRecordSession::updateHelpText()
157 {
158  ToolPtr tool = this->getTool();
159  QString toolname = tool ? tool->getName() : "<none>";
160  mSessionSelector->setHelp(QString("Select tracker data, current data uses tool %2")
161  .arg(toolname));
162 }
163 
164 ToolRep3DPtr SelectRecordSession::getToolRepIn3DView(ToolPtr tool)
165 {
166  return mServices->view()->get3DReps(0, 0)->findFirst<ToolRep3D>(tool);
167 }
168 
169 void SelectRecordSession::showSelectedRecordingInView()
170 {
171  this->clearTracer();
172  if (!mVisible)
173  return;
174 
175  this->warnIfNoTrackingDataInSession();
176 
177  TimedTransformMap trackerRecordedData_prMt = this->getRecordedTrackerData_prMt();
178  if (trackerRecordedData_prMt.empty())
179  return;
180 
181  mCurrentTracedTool = this->getTool();
182  this->updateHelpText();
183 
184  ToolRep3DPtr activeRep3D = this->getToolRepIn3DView(mCurrentTracedTool);
185  if(!activeRep3D)
186  return;
187 
188  if(!trackerRecordedData_prMt.empty())
189  {
190  activeRep3D->getTracer()->clear();
191  activeRep3D->getTracer()->setColor(QColor("green"));
192  activeRep3D->getTracer()->addManyPositions(trackerRecordedData_prMt);
193  }
194  else
195  {
196  activeRep3D->getTracer()->clear();
197  }
198 }
199 
200 void SelectRecordSession::clearTracer()
201 {
202  ToolRep3DPtr activeRep3D = this->getToolRepIn3DView(mCurrentTracedTool);
203 
204  if(activeRep3D)
205  {
206  activeRep3D->getTracer()->clear();
207  }
208 
209  mCurrentTracedTool.reset();
210 }
211 
212 } //namespace cx
SelectRecordSession(XmlOptionFile options, AcquisitionServicePtr acquisitionService, VisServicesPtr services)
boost::shared_ptr< class AcquisitionService > AcquisitionServicePtr
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:40
QDomElement getElement()
return the current element
static TimedTransformMap getToolHistory_prMt(ToolPtr tool, RecordSessionPtr session, bool verbose)
Reference tool.
Definition: cxTool.h:84
Display a Tool in 3D.
Definition: cxToolRep3D.h:51
boost::shared_ptr< class RecordSession > RecordSessionPtr
TimedTransformMap getRecordedTrackerData_prMt()
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:98
boost::shared_ptr< class ToolRep3D > ToolRep3DPtr
Helper class for xml files used to store ssc/cx data.
std::map< double, Transform3D > TimedTransformMap
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr