CustusX  15.3.4-beta
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  mCoordinateSystems = StringPropertySelectCoordinateSystem::New(services->getToolManager());
66  mTools = StringPropertySelectTool::New(mServices->getToolManager());
67  mData = StringPropertySelectData::New(mServices->getPatientService());
68 
69  mCoordinateSystemComboBox = new LabeledComboBoxWidget(this, mCoordinateSystems);
70  mToolComboBox = new LabeledComboBoxWidget(this, mTools);
71  mDataComboBox = new LabeledComboBoxWidget(this, mData);
72 
73  toplayout->addWidget(new QLabel("<b>Select coordinate system to sample in: </b>"));
74  toplayout->addWidget(mCoordinateSystemComboBox);
75  toplayout->addWidget(mToolComboBox);
76  toplayout->addWidget(mDataComboBox);
77  toplayout->addWidget(this->createHorizontalLine());
78  toplayout->addWidget(mSampleButton);
79  toplayout->addWidget(this->createHorizontalLine());
80  toplayout->addWidget(mSaveFileButton);
81  toplayout->addWidget(mSaveToFileNameLabel);
82  toplayout->addStretch();
83 
84  connect(mSaveFileButton, SIGNAL(clicked()), this, SLOT(saveFileSlot()));
85  connect(mSampleButton, SIGNAL(clicked()), this, SLOT(sampleSlot()));
86  connect(mCoordinateSystems.get(), SIGNAL(changed()), this, SLOT(coordinateSystemChanged()));
87 
88  //setting initial state
89  this->coordinateSystemChanged();
90 }
91 
93 {}
94 
96 {
97  return "<html>"
98  "<h3>Tool tip sampling.</h3>"
99  "<p>You can sample the active tools tooltip in any coordinate system and get the results written to file.</p>"
100  "</html>";
101 }
102 
103 void ToolTipSampleWidget::saveFileSlot()
104 {
105  QString configPath = profile()->getPath();
106  if(mServices->getPatientService()->isPatientValid())
107  configPath = mServices->getPatientService()->getActivePatientFolder();
108 
109  QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
110  configPath+"/SampledPoints.txt",
111  tr("Text (*.txt)"));
112  if(fileName.isEmpty())
113  return;
114  else if(QFile::exists(fileName))
115  mTruncateFile = true;
116 
117  mSaveToFileNameLabel->setText(fileName);
118 }
119 
120 void ToolTipSampleWidget::sampleSlot()
121 {
122  QFile samplingFile(mSaveToFileNameLabel->text());
123 
124  CoordinateSystem to = this->getSelectedCoordinateSystem();
125  Vector3D toolPoint = mServices->getSpaceProvider()->getActiveToolTipPoint(to, false);
126 
127  if(!samplingFile.open(QIODevice::WriteOnly | (mTruncateFile ? QIODevice::Truncate : QIODevice::Append)))
128  {
129  reportWarning("Could not open "+samplingFile.fileName());
130  report("Sampled point: "+qstring_cast(toolPoint));
131  return;
132  }
133  else
134  {
135  if(mTruncateFile)
136  mTruncateFile = false;
137  }
138 
139  QString sampledPoint = qstring_cast(toolPoint);
140 
141  QTextStream streamer(&samplingFile);
142  streamer << sampledPoint;
143  streamer << endl;
144 
145  reporter()->playSampleSound();
146  report("Sampled point in "+qstring_cast(to.mId)+" ("+to.mRefObject+") space, result: "+sampledPoint);
147 }
148 
149 void ToolTipSampleWidget::coordinateSystemChanged()
150 {
151  switch (string2enum<COORDINATE_SYSTEM>(mCoordinateSystems->getValue()))
152  {
153  case csDATA:
154  mDataComboBox->show();
155  mToolComboBox->hide();
156  break;
157  case csTOOL:
158  mToolComboBox->show();
159  mDataComboBox->hide();
160  break;
161  case csSENSOR:
162  mToolComboBox->show();
163  mDataComboBox->hide();
164  break;
165  default:
166  mDataComboBox->hide();
167  mToolComboBox->hide();
168  break;
169  };
170 }
171 
172 CoordinateSystem ToolTipSampleWidget::getSelectedCoordinateSystem()
173 {
174  CoordinateSystem retval(csCOUNT);
175 
176  retval.mId = string2enum<COORDINATE_SYSTEM>(mCoordinateSystems->getValue());
177 
178  switch (retval.mId)
179  {
180  case csDATA:
181  retval = mServices->getSpaceProvider()->getD(mData->getData());
182  break;
183  case csTOOL:
184  retval = mServices->getSpaceProvider()->getT(mTools->getTool());
185  break;
186  case csSENSOR:
187  retval = mServices->getSpaceProvider()->getT(mTools->getTool());
188  break;
189  default:
190  retval.mRefObject = "";
191  break;
192  };
193 
194  return retval;
195 }
196 //------------------------------------------------------------------------------
197 
198 
199 }
QString qstring_cast(const T &val)
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:142
ReporterPtr reporter()
Definition: cxReporter.cpp:59
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:61
csSENSOR
a tools sensor space (s)
static StringPropertySelectCoordinateSystemPtr New(TrackingServicePtr trackingService)
virtual QString defaultWhatsThis() const
Returns a short description of what this widget will do for you.
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
static StringPropertySelectDataPtr New(PatientModelServicePtr patientModelService)
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)