CustusX  15.4.0-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxLandmarkPatientRegistrationWidget.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 
34 
35 #include <QVBoxLayout>
36 #include <QPushButton>
37 #include <QTableWidget>
38 #include <QTableWidgetItem>
39 #include <QHeaderView>
40 #include <QLabel>
41 #include <QSlider>
42 #include <QGridLayout>
43 #include <QSpinBox>
44 #include <vtkDoubleArray.h>
45 #include "cxVector3D.h"
46 #include "cxLogger.h"
47 #include "cxTypeConversions.h"
49 #include "cxLandmarkRep.h"
50 #include "cxView.h"
51 #include "cxRegistrationService.h"
52 #include "cxViewService.h"
53 #include "cxPatientModelService.h"
54 #include "cxViewGroupData.h"
55 #include "cxTrackingService.h"
56 #include "cxRepContainer.h"
57 #include "cxReporter.h"
58 #include "cxSettings.h"
59 
60 namespace cx
61 {
62 LandmarkPatientRegistrationWidget::LandmarkPatientRegistrationWidget(RegServices services,
63  QWidget* parent, QString objectName, QString windowTitle) :
64  LandmarkRegistrationWidget(services, parent, objectName, windowTitle), mToolSampleButton(new QPushButton(
65  "Sample Tool", this))
66 {
72 
73  //buttons
74  mToolSampleButton->setDisabled(true);
75  connect(mToolSampleButton, SIGNAL(clicked()), this, SLOT(toolSampleButtonClickedSlot()));
76 
77  mRemoveLandmarkButton = new QPushButton("Clear", this);
78  mRemoveLandmarkButton->setToolTip("Clear selected landmark");
79  // mRemoveLandmarkButton->setDisabled(true);
80  connect(mRemoveLandmarkButton, SIGNAL(clicked()), this, SLOT(removeLandmarkButtonClickedSlot()));
81 
82  mRegisterButton = new QPushButton("Register", this);
83  mRegisterButton->setToolTip("Perform registration");
84  connect(mRegisterButton, SIGNAL(clicked()), this, SLOT(registerSlot()));
85 
86  //toolmanager
88  connect(mActiveToolProxy.get(), SIGNAL(toolVisible(bool)), this, SLOT(updateToolSampleButton()));
89  connect(mActiveToolProxy.get(), SIGNAL(activeToolChanged(const QString&)), this, SLOT(updateToolSampleButton()));
90 
91  connect(settings(), &Settings::valueChangedFor, this, &LandmarkPatientRegistrationWidget::globalConfigurationFileChangedSlot);
92 
93  //layout
98 
99  QHBoxLayout* buttonsLayout = new QHBoxLayout;
100  buttonsLayout->addWidget(mRegisterButton);
101  buttonsLayout->addWidget(mRemoveLandmarkButton);
102  mVerticalLayout->addLayout(buttonsLayout);
103 
104  this->updateToolSampleButton();
105 }
106 
108 {
109 }
110 
111 void LandmarkPatientRegistrationWidget::globalConfigurationFileChangedSlot(QString key)
112 {
113  if (key == "giveManualToolPhysicalProperties")
114  this->updateToolSampleButton();
115 }
116 
118 {
119  this->performRegistration();
120 }
121 
123 {
124  mImageLandmarkSource->setData(mServices.registrationService->getFixedData());
125 }
126 
128 {
129  ToolPtr tool = mServices.trackingService->getActiveTool();
130 
131  bool enabled = false;
132  enabled = tool && tool->getVisible() && (!tool->hasType(Tool::TOOL_MANUAL) || settings()->value("giveManualToolPhysicalProperties").toBool()); // enable only for non-manual tools.
133  mToolSampleButton->setEnabled(enabled);
134 
135  if (mServices.trackingService->getActiveTool())
136  mToolSampleButton->setText("Sample " + qstring_cast(tool->getName()));
137  else
138  mToolSampleButton->setText("No tool");
139 }
140 
142 {
143  ToolPtr tool = mServices.trackingService->getActiveTool();
144 
145  if (!tool)
146  {
147  reportError("mToolToSample is NULL!");
148  return;
149  }
150  //TODO What if the reference frame isnt visible?
151  Transform3D lastTransform_prMt = tool->get_prMt();
152  Vector3D p_pr = lastTransform_prMt.coord(Vector3D(0, 0, tool->getTooltipOffset()));
153 
154  // TODO: do we want to allow sampling points not defined in image??
155  if (mActiveLandmark.isEmpty() && !mServices.patientModelService->getLandmarkProperties().empty())
156  mActiveLandmark = mServices.patientModelService->getLandmarkProperties().begin()->first;
157 
158  mServices.patientModelService->getPatientLandmarks()->setLandmark(Landmark(mActiveLandmark, p_pr));
159  reporter()->playSampleSound();
160 
161  this->activateLandmark(this->getNextLandmark());
162 
163  this->performRegistration(); // automatic when sampling in physical patient space (Mantis #0000674)s
164 }
165 
167 {
168 // std::cout << "LandmarkPatientRegistrationWidget::showEvent" << std::endl;
170  connect(mServices.patientModelService->getPatientLandmarks().get(), SIGNAL(landmarkAdded(QString)), this, SLOT(landmarkUpdatedSlot()));
171  connect(mServices.patientModelService->getPatientLandmarks().get(), SIGNAL(landmarkRemoved(QString)), this, SLOT(landmarkUpdatedSlot()));
172 
173  mServices.visualizationService->getGroup(0)->setRegistrationMode(rsPATIENT_REGISTRATED);
174 
175  LandmarkRepPtr rep = mServices.visualizationService->get3DReps(0, 0)->findFirst<LandmarkRep>();
176  if (rep)
177  {
180  rep->setSecondaryColor(QColor::fromRgbF(0, 0.6, 0.8));
181  }
182 }
183 
185 {
186 // std::cout << "LandmarkPatientRegistrationWidget::hideEvent" << std::endl;
188  disconnect(mServices.patientModelService->getPatientLandmarks().get(), SIGNAL(landmarkAdded(QString)), this, SLOT(landmarkUpdatedSlot()));
189  disconnect(mServices.patientModelService->getPatientLandmarks().get(), SIGNAL(landmarkRemoved(QString)), this, SLOT(landmarkUpdatedSlot()));
190 
191  if(mServices.visualizationService->get3DView(0, 0))
192  {
193  LandmarkRepPtr rep = mServices.visualizationService->get3DReps(0, 0)->findFirst<LandmarkRep>();
194  if (rep)
195  {
197  rep->setSecondarySource(LandmarksSourcePtr());
198  }
199  }
200  mServices.visualizationService->getGroup(0)->setRegistrationMode(rsNOT_REGISTRATED);
201 }
202 
204 {
205  QString next = this->getNextLandmark();
206  mServices.patientModelService->getPatientLandmarks()->removeLandmark(mActiveLandmark);
207  this->activateLandmark(next);
208 }
209 
211 {
213 
214  mRemoveLandmarkButton->setEnabled(true);
215 }
216 
218 {
220 
221  std::vector<Landmark> landmarks = this->getAllLandmarks();
222  mRemoveLandmarkButton->setEnabled(!landmarks.empty() && !mActiveLandmark.isEmpty());
223 }
224 
228 {
229  return mServices.patientModelService->getPatientLandmarks()->getLandmarks();
230 }
231 
236 {
237  Transform3D rMpr = mServices.patientModelService->get_rMpr();
238  return rMpr;
239 }
240 
242 {
243  mServices.patientModelService->getPatientLandmarks()->setLandmark(Landmark(uid, p_target));
244  reporter()->playSampleSound();
245 }
246 
248 {
249  if (!mServices.registrationService->getFixedData())
250  mServices.registrationService->setFixedData(mServices.patientModelService->getActiveImage());
251 
252  if (mServices.patientModelService->getPatientLandmarks()->getLandmarks().size() < 3)
253  return;
254 
255  mServices.registrationService->doPatientRegistration();
256 
258 }
259 
261 {
262  return "Patient";
263 }
264 
265 }//namespace cx
QString qstring_cast(const T &val)
virtual void showEvent(QShowEvent *event)
updates internal info before showing the widget
void reportError(QString msg)
Definition: cxLogger.cpp:92
static ImageLandmarksSourcePtr New()
Definition: cxLandmarkRep.h:94
ReporterPtr reporter()
Definition: cxReporter.cpp:59
virtual void prePaintEvent()
populates the table widget
QLabel * mAvarageAccuracyLabel
label showing the average accuracy
One landmark, or fiducial, coordinate.
Definition: cxLandmark.h:61
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
StringPropertyRegistrationFixedImagePtr mFixedProperty
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:99
void toolSampleButtonClickedSlot()
reacts when the Sample Tool button is clicked
Composite widget for string selection.
QVBoxLayout * mVerticalLayout
vertical layout is used
void fixedDataChanged(QString uid)
virtual void hideEvent(QHideEvent *event)
static ActiveToolProxyPtr New(TrackingServicePtr trackingService)
void valueChangedFor(QString key)
static PatientLandmarksSourcePtr New(PatientModelServicePtr dataManager)
Definition: cxLandmarkRep.h:79
virtual void cellClickedSlot(int row, int column)
when a landmark i selected from the table
void setPrimarySource(LandmarksSourcePtr primary)
virtual void cellClickedSlot(int row, int column)
when a landmark i selected from the table
virtual void prePaintEvent()
populates the table widget
Representation of a mouse/keyboard-controlled virtual tool.
Definition: cxTool.h:91
rsNOT_REGISTRATED
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:42
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
VisualizationServicePtr visualizationService
Definition: cxVisServices.h:60
boost::shared_ptr< LandmarksSource > LandmarksSourcePtr
Definition: cxLandmarkRep.h:72
std::map< QString, class Landmark > LandmarkMap
QString mActiveLandmark
uid of surrently selected landmark.
TrackingServicePtr trackingService
std::vector< Landmark > getAllLandmarks() const
get all the landmarks from the image and the datamanager
QPushButton * mToolSampleButton
the Sample Tool button
virtual void setTargetLandmark(QString uid, Vector3D p_target)
PatientModelServicePtr patientModelService
virtual void showEvent(QShowEvent *event)
updates internal info before showing the widget
virtual void fixedDataChanged()
listens to the datamanager for when the active image is changed
boost::shared_ptr< class LandmarkRep > LandmarkRepPtr
RegistrationServicePtr registrationService
Definition: cxRegServices.h:60
QTableWidget * mLandmarkTableWidget
the table widget presenting the landmarks
boost::shared_ptr< class Tool > ToolPtr