CustusX  20.03-rc1
An IGT application
cxRegistrationWidget.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 
12 #include "cxRegistrationWidget.h"
13 #include <boost/bind.hpp>
14 #include <boost/function.hpp>
15 #include <QLabel>
16 #include <QVBoxLayout>
17 #include <QGroupBox>
18 #include <QStackedWidget>
19 #include <QComboBox>
20 #include <QStringList>
21 
22 #include "cxTypeConversions.h"
23 #include "cxLogger.h"
26 #include "cxRegistrationService.h"
27 #include "cxPatientModelService.h"
28 #include "cxStringProperty.h"
29 #include "cxProfile.h"
30 
31 namespace cx
32 {
33 
34 RegistrationTypeWidget::RegistrationTypeWidget(QString type, QString defVal, XmlOptionFile options, QWidget* parent) :
35  BaseWidget(parent, type, type),
36  mOptions(options)
37 {
38  mStackedWidget = new QStackedWidget(this);
39  mStackedWidget->setFocusPolicy(Qt::StrongFocus);
40 
41  QVBoxLayout *layoutV = new QVBoxLayout(this);
42 // layoutV->setMargin(0);
43 
44  mMethodSelector = StringProperty::initialize(type,
45  "Method",
46  "Select registration method",
47  defVal,
48  QStringList(),
49  mOptions.getElement());
50  connect(mMethodSelector.get(), &StringProperty::valueWasSet, this, &RegistrationTypeWidget::onIndexChanged);
51 
52  layoutV->addWidget(new LabeledComboBoxWidget(this, mMethodSelector));
53  layoutV->addWidget(mStackedWidget);
54 }
55 
57 {
58  mMethodSelector->setValue(selectRegistrationMethod);
59 }
60 
61 void RegistrationTypeWidget::onIndexChanged()
62 {
63  QString method = mMethodSelector->getValue();
64  int pos = mMethodSelector->getValueRange().indexOf(method);
65  mStackedWidget->setCurrentIndex(pos);
66  this->setObjectName(mStackedWidget->currentWidget()->objectName());
67 }
68 
70 {
71  mStackedWidget->addWidget(service->createWidget());
72  QStringList values = mMethodSelector->getValueRange();
73  values << service->getRegistrationMethod();
74  mMethodSelector->setValueRange(values);
75 
76  // initialize if not set
77  if (mMethodSelector->getValue().isEmpty())
78  {
79  mMethodSelector->setValue(service->getRegistrationMethod());
80  }
81 
82  if (mMethodSelector->getValue() == service->getRegistrationMethod())
83  this->onIndexChanged();
84 }
85 
87 {
88  this->removeWidgetFromStackedWidget(service->getWidgetName());
89 
90  QStringList values = mMethodSelector->getValueRange();
91  if (!values.removeOne(service->getRegistrationMethod()))
92  reportWarning("RegistrationWidget::onServiceRemoved: Cannot find and remove service from combobox: "+ service->getRegistrationMethod());
93  mMethodSelector->setValueRange(values);
94 }
95 
96 void RegistrationTypeWidget::removeWidgetFromStackedWidget(QString widgetName)
97 {
98  for(int i = 0; i < mStackedWidget->count(); ++i)
99  {
100  QWidget* widget = mStackedWidget->widget(i);
101  if(widget->objectName() == widgetName)
102  {
103  mStackedWidget->removeWidget(widget);
104  delete widget;
105  }
106  }
107 }
108 
109 
110 //---------------------------------------------------------
111 //---------------------------------------------------------
112 //---------------------------------------------------------
113 
114 
115 RegistrationWidget::RegistrationWidget(ctkPluginContext *pluginContext, QWidget* parent) :
116  TabbedWidget(parent, "org_custusx_registration_gui_widget", "Registration"),
117  mPluginContext(pluginContext),
118  mOptions(profile()->getXmlSettings().descend("RegistrationWidget"))
119 {
120  connect(mTabWidget, &QTabWidget::currentChanged, this, &RegistrationWidget::onCurrentChanged);
121  this->initRegistrationTypesWidgets();
122  this->initServiceListener();
123 }
124 
125 void RegistrationWidget::selectRegistrationMethod(QString registrationType, QString registrationMethodName)
126 {
127  RegistrationTypeWidget* widget = mRegistrationTypeMap[registrationType];
128  if(widget)
129  widget->selectRegistrationMethod(registrationMethodName);
130 }
131 
132 void RegistrationWidget::initRegistrationTypesWidgets()
133 {
134  mRegistrationTypes << "ImageToPatient" << "ImageToImage" << "ImageTransform";
135  QStringList typeDefaults;
136  typeDefaults << "Landmark" << "Landmark" << "";
137 
138  for(int i = 0; i < mRegistrationTypes.count(); ++i)
139  {
140  QString type = mRegistrationTypes[i];
141  RegistrationTypeWidget* widget =
142  new RegistrationTypeWidget(type,
143  typeDefaults[i],
144  mOptions, this);
145 
146  mRegistrationTypeMap[type] = widget;
147  this->addTab(widget, type);
148 
149  }
150 
151  // Create typeselector after the widgets, as the addwidget trigger
152  // a signal that causes type to be overwritten.
153  mTypeSelector = StringProperty::initialize("RegistrationTypes",
154  "Registration Types",
155  "Select registration type",
156  "",
157  mRegistrationTypes,
158  mOptions.getElement());
159  if (mRegistrationTypeMap.count(mTypeSelector->getValue()))
160  mTabWidget->setCurrentWidget(mRegistrationTypeMap[mTypeSelector->getValue()]);
161 }
162 
163 void RegistrationWidget::onCurrentChanged(int index)
164 {
165  if (index<0)
166  return;
167  if (mTypeSelector)
168  mTypeSelector->setValue(mRegistrationTypes[index]);
169 }
170 
171 void RegistrationWidget::initServiceListener()
172 {
173  mServiceListener.reset(new ServiceTrackerListener<RegistrationMethodService>(
174  mPluginContext,
175  boost::bind(&RegistrationWidget::onServiceAdded, this, _1),
176  boost::function<void (RegistrationMethodService*)>(),
177  boost::bind(&RegistrationWidget::onServiceRemoved, this, _1)
178  ));
179  mServiceListener->open();
180 }
181 
182 void RegistrationWidget::onServiceAdded(RegistrationMethodService* service)
183 {
184  QString type = service->getRegistrationType();
185  if(!mRegistrationTypeMap.count(type))
186  {
187  reportError("Unknown registrationType : " + type);
188  return;
189  }
190  mRegistrationTypeMap[type]->addMethod(service);
191 }
192 
193 void RegistrationWidget::onServiceRemoved(RegistrationMethodService *service)
194 {
195  QString type = service->getRegistrationType();
196  mRegistrationTypeMap[type]->removeMethod(service);
197 }
198 
199 } /* namespace cx */
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:160
void reportError(QString msg)
Definition: cxLogger.cpp:71
void selectRegistrationMethod(QString registrationType, QString registrationMethodName)
void removeMethod(RegistrationMethodService *service)
virtual QWidget * createWidget()=0
virtual QString getRegistrationMethod()=0
void selectRegistrationMethod(QString registrationMethodName)
Composite widget for string selection.
QTabWidget * mTabWidget
QDomElement getElement()
return the current element
virtual QString getRegistrationType()=0
Registration Method services.
void addTab(BaseWidget *newTab, QString newTabName)
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
Interface for making widgets with a hierarchy of tabs.
RegistrationTypeWidget(QString type, QString defVal, XmlOptionFile options, QWidget *parent=0)
RegistrationWidget(ctkPluginContext *pluginContext, QWidget *parent=0)
virtual QString getWidgetName()=0
static StringPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList range, QDomNode root=QDomNode())
Helper class for listening to services being added, modified and removed.
void addMethod(RegistrationMethodService *service)
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:88
Helper class for xml files used to store ssc/cx data.
Namespace for all CustusX production code.