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