NorMIT-nav  22.09
An IGT application
cxToolTipSampleWidget.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 =========================================================================*/
11 #include <cxToolTipSampleWidget.h>
12 
13 #include <QPushButton>
14 #include <QTextStream>
15 #include <QFileDialog>
16 #include <QMessageBox>
17 #include "cxTypeConversions.h"
18 #include "cxLogger.h"
19 #include "cxVector3D.h"
21 #include "cxProfile.h"
23 #include "cxSpaceProvider.h"
24 #include "cxPatientModelService.h"
25 #include "cxReporter.h"
26 #include "cxVisServices.h"
29 #include "cxEnumConversion.h"
30 
31 namespace cx
32 {
33 
35  BaseWidget(parent, "tool_tip_sample_widget", "ToolTip Sample"),
36  mServices(services),
37  mSampleButton(new QPushButton("Sample")),
38  mSaveToFileNameLabel(new QLabel("<font color=red> No file selected </font>")),
39  mSaveFileButton(new QPushButton("Save to...")),
40  mTruncateFile(false)
41 {
42  QVBoxLayout* toplayout = new QVBoxLayout(this);
43 
44  this->setToolTip("Sample the tool tip position");
45  mCoordinateSystems = StringPropertySelectCoordinateSystem::New(services->tracking());
46  mTools = StringPropertySelectTool::New(mServices->tracking());
47  mData = StringPropertySelectData::New(mServices->patient());
48 
49  mCoordinateSystemComboBox = new LabeledComboBoxWidget(this, mCoordinateSystems);
50  mToolComboBox = new LabeledComboBoxWidget(this, mTools);
51  mDataComboBox = new LabeledComboBoxWidget(this, mData);
52 
53  toplayout->addWidget(new QLabel("<b>Select coordinate system to sample in: </b>"));
54  toplayout->addWidget(mCoordinateSystemComboBox);
55  toplayout->addWidget(mToolComboBox);
56  toplayout->addWidget(mDataComboBox);
57  toplayout->addWidget(this->createHorizontalLine());
58  toplayout->addWidget(mSampleButton);
59  toplayout->addWidget(this->createHorizontalLine());
60  toplayout->addWidget(mSaveFileButton);
61  toplayout->addWidget(mSaveToFileNameLabel);
62  toplayout->addStretch();
63 
64  connect(mSaveFileButton, SIGNAL(clicked()), this, SLOT(saveFileSlot()));
65  connect(mSampleButton, SIGNAL(clicked()), this, SLOT(sampleSlot()));
66  connect(mCoordinateSystems.get(), SIGNAL(changed()), this, SLOT(coordinateSystemChanged()));
67 
68  //setting initial state
69  this->coordinateSystemChanged();
70 }
71 
73 {}
74 
75 void ToolTipSampleWidget::saveFileSlot()
76 {
77  QString configPath = profile()->getPath();
78  if(mServices->patient()->isPatientValid())
79  configPath = mServices->patient()->getActivePatientFolder();
80 
81  QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
82  configPath+"/SampledPoints.txt",
83  tr("Text (*.txt)"));
84  if(fileName.isEmpty())
85  return;
86  else if(QFile::exists(fileName))
87  mTruncateFile = true;
88 
89  mSaveToFileNameLabel->setText(fileName);
90 }
91 
92 void ToolTipSampleWidget::sampleSlot()
93 {
94  QFile samplingFile(mSaveToFileNameLabel->text());
95 
96  CoordinateSystem to = this->getSelectedCoordinateSystem();
97  Vector3D toolPoint = mServices->spaceProvider()->getActiveToolTipPoint(to, false);
98 
99  if(!samplingFile.open(QIODevice::WriteOnly | (mTruncateFile ? QIODevice::Truncate : QIODevice::Append)))
100  {
101  reportWarning("Could not open "+samplingFile.fileName());
102  report("Sampled point: "+qstring_cast(toolPoint));
103  return;
104  }
105  else
106  {
107  if(mTruncateFile)
108  mTruncateFile = false;
109  }
110 
111  QString sampledPoint = qstring_cast(toolPoint);
112 
113  QTextStream streamer(&samplingFile);
114  streamer << sampledPoint;
115  streamer << endl;
116 
117  reporter()->playSampleSound();
118  report("Sampled point in "+qstring_cast(to.mId)+" ("+to.mRefObject+") space, result: "+sampledPoint);
119 }
120 
121 void ToolTipSampleWidget::coordinateSystemChanged()
122 {
123  switch (string2enum<COORDINATE_SYSTEM>(mCoordinateSystems->getValue()))
124  {
125  case csDATA:
126  mDataComboBox->show();
127  mToolComboBox->hide();
128  break;
129  case csTOOL:
130  mToolComboBox->show();
131  mDataComboBox->hide();
132  break;
133  case csSENSOR:
134  mToolComboBox->show();
135  mDataComboBox->hide();
136  break;
137  default:
138  mDataComboBox->hide();
139  mToolComboBox->hide();
140  break;
141  };
142 }
143 
144 CoordinateSystem ToolTipSampleWidget::getSelectedCoordinateSystem()
145 {
146  CoordinateSystem retval(csCOUNT);
147 
148  retval.mId = string2enum<COORDINATE_SYSTEM>(mCoordinateSystems->getValue());
149 
150  switch (retval.mId)
151  {
152  case csDATA:
153  retval = mServices->spaceProvider()->getD(mData->getData());
154  break;
155  case csTOOL:
156  retval = mServices->spaceProvider()->getT(mTools->getTool());
157  break;
158  case csSENSOR:
159  retval = mServices->spaceProvider()->getT(mTools->getTool());
160  break;
161  default:
162  retval.mRefObject = "";
163  break;
164  };
165 
166  return retval;
167 }
168 //------------------------------------------------------------------------------
169 
170 
171 }
cx::LabeledComboBoxWidget
Composite widget for string selection.
Definition: cxLabeledComboBoxWidget.h:36
csSENSOR
csSENSOR
a tools sensor space (s)
Definition: cxDefinitions.h:94
cx::StringPropertySelectData::New
static StringPropertySelectDataPtr New(PatientModelServicePtr patientModelService, QString typeRegexp=".*")
Definition: cxSelectDataStringProperty.h:101
cxLogger.h
qstring_cast
QString qstring_cast(const T &val)
Definition: cxTypeConversions.h:46
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::BaseWidget
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:88
cx::report
void report(QString msg)
Definition: cxLogger.cpp:69
cx::VisServicesPtr
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:40
cxLabeledComboBoxWidget.h
csDATA
csDATA
a datas space (d)
Definition: cxDefinitions.h:91
cxReporter.h
cx::reporter
ReporterPtr reporter()
Definition: cxReporter.cpp:36
cxVector3D.h
cxStringPropertySelectTool.h
cx::BaseWidget::createHorizontalLine
static QFrame * createHorizontalLine()
Creates a horizontal line which can be inserted into widgets.
Definition: cxBaseWidget.cpp:66
cxToolTipSampleWidget.h
cx::StringPropertySelectCoordinateSystem::New
static StringPropertySelectCoordinateSystemPtr New(TrackingServicePtr trackingService)
Definition: cxStringPropertySelectCoordinateSystem.h:60
csTOOL
csTOOL
a tools rspace (t)
Definition: cxDefinitions.h:93
cxProfile.h
cxStringPropertySelectCoordinateSystem.h
cxTypeConversions.h
cxPatientModelService.h
cx::profile
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:160
cx::StringPropertySelectTool::New
static StringPropertySelectToolPtr New(TrackingServicePtr trackingService)
Definition: cxStringPropertySelectTool.h:40
cxSelectDataStringProperty.h
cxEnumConversion.h
cx::Vector3D
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
cx::ToolTipSampleWidget::ToolTipSampleWidget
ToolTipSampleWidget(VisServicesPtr services, QWidget *parent)
Definition: cxToolTipSampleWidget.cpp:34
cx::ToolTipSampleWidget::~ToolTipSampleWidget
~ToolTipSampleWidget()
Definition: cxToolTipSampleWidget.cpp:72
cx::reportWarning
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
cxSpaceProvider.h
cxVisServices.h