CustusX  15.3.4-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 
33 #include "cxRegistrationWidget.h"
34 #include <boost/bind.hpp>
35 #include <boost/function.hpp>
36 #include <QLabel>
37 #include <QVBoxLayout>
38 #include <QGroupBox>
39 #include <QStackedWidget>
40 #include <QComboBox>
41 #include <QStringList>
42 
43 #include "cxTypeConversions.h"
44 #include "cxLogger.h"
47 #include "cxRegistrationService.h"
48 #include "cxPatientModelService.h"
49 #include "cxStringProperty.h"
50 #include "cxProfile.h"
51 
52 namespace cx
53 {
54 
55 RegistrationWidget::RegistrationWidget(ctkPluginContext *pluginContext, QWidget* parent) :
56  mPluginContext(pluginContext),
57  QTabWidget(parent),
58  mVerticalLayout(new QVBoxLayout(this)),
59  mOptions(profile()->getXmlSettings().descend("RegistrationWidget"))
60 {
61  this->setObjectName("RegistrationWidget");
62  this->setWindowTitle("Registration");
63  this->setWhatsThis(this->defaultWhatsThis());
64 
65  connect(this, &QTabWidget::currentChanged, this, &RegistrationWidget::onCurrentChanged);
66  this->initRegistrationTypesWidgets();
67  this->initServiceListener();
68 }
69 
71 {
72 }
73 
74 void RegistrationWidget::initRegistrationTypesWidgets()
75 {
76  mRegistrationTypes << "ImageToPatient" << "ImageToImage" << "ImageTransform";
77 
78  mTypeSelector = StringProperty::initialize("RegistrationTypes",
79  "Registration Types",
80  "Select registration type",
81  "",
82  mRegistrationTypes,
83  mOptions.getElement());
84  this->blockSignals(true); // we dont want the onCurrentChanged() to be called while constructing
85 
86  for(int i = 0; i < mRegistrationTypes.count(); ++i)
87  {
88  QWidget *widget = new QWidget(this);
89  QStackedWidget *registrationTypeWidget = new QStackedWidget(widget);
90  mRegistrationTypeMap[mRegistrationTypes[i]] = registrationTypeWidget;
91 
92  QVBoxLayout *layoutV = new QVBoxLayout(widget);
93 // layoutV->setMargin(0);
94 
95  StringPropertyPtr methodSelector = StringProperty::initialize(mRegistrationTypes[i],
96  "Method",
97  "Select registration method",
98  "",
99  QStringList(),
100  mOptions.getElement());
101  mMethodsSelectorMap[mRegistrationTypes[i]] = methodSelector;
102  boost::function<void()> f = boost::bind(&RegistrationWidget::indexChanged, this, mRegistrationTypes[i]);
103  connect(methodSelector.get(), &StringProperty::valueWasSet, f);
104 
105  layoutV->addWidget(new LabeledComboBoxWidget(this, methodSelector));
106  layoutV->addWidget(registrationTypeWidget);
107 
108  mVerticalLayout->addWidget(widget);
109  this->addTab(widget, mRegistrationTypes[i]);
110 
111  if (mTypeSelector->getValue() == mRegistrationTypes[i])
112  this->setCurrentIndex(i);
113  }
114 
115  this->blockSignals(false);
116 }
117 
118 void RegistrationWidget::onCurrentChanged(int index)
119 {
120  if (index<0)
121  return;
122  mTypeSelector->setValue(mRegistrationTypes[index]);
123 }
124 
125 void RegistrationWidget::indexChanged(QString registrationType)
126 {
127  StringPropertyPtr methodSelector = mMethodsSelectorMap[registrationType];
128  QStackedWidget *stackedWidget = mRegistrationTypeMap[registrationType];
129  this->selectStackWidget(methodSelector, stackedWidget);
130 }
131 
132 void RegistrationWidget::selectStackWidget(StringPropertyPtr methodSelector, QStackedWidget *stackedWidget)
133 {
134  QString method = methodSelector->getValue();
135  int pos = methodSelector->getValueRange().indexOf(method);
136  stackedWidget->setCurrentIndex(pos);
137 }
138 
139 void RegistrationWidget::initServiceListener()
140 {
141  mServiceListener.reset(new ServiceTrackerListener<RegistrationMethodService>(
142  mPluginContext,
143  boost::bind(&RegistrationWidget::onServiceAdded, this, _1),
144  boost::function<void (RegistrationMethodService*)>(),
145  boost::bind(&RegistrationWidget::onServiceRemoved, this, _1)
146  ));
147  mServiceListener->open();
148 }
149 
150 void RegistrationWidget::onServiceAdded(RegistrationMethodService* service)
151 {
152  if(!this->knownType(service->getRegistrationType()))
153  return;
154 
155  StringPropertyPtr methodSelector = mMethodsSelectorMap[service->getRegistrationType()];
156  QStackedWidget *stackedWidget = mRegistrationTypeMap[service->getRegistrationType()];
157 
158  stackedWidget->addWidget(service->createWidget());
159  QStringList values = methodSelector->getValueRange();
160  values << service->getRegistrationMethod();
161  methodSelector->setValueRange(values);
162 
163  // initialize if not set
164  if (methodSelector->getValue().isEmpty())
165  {
166  methodSelector->setValue(service->getRegistrationMethod());
167  }
168 
169  if (methodSelector->getValue() == service->getRegistrationMethod())
170  this->selectStackWidget(methodSelector, stackedWidget);
171 }
172 
173 bool RegistrationWidget::knownType(QString registrationType)
174 {
175  if(!mRegistrationTypes.contains(registrationType))
176  {
177  reportError("Unknown registrationType : " + registrationType);
178  return false;
179  }
180  return true;
181 }
182 
183 void RegistrationWidget::onServiceRemoved(RegistrationMethodService *service)
184 {
185  QStackedWidget *stackedWidget = mRegistrationTypeMap[service->getRegistrationType()];
186  this->removeWidgetFromStackedWidget(service->getWidgetName(), stackedWidget);
187 
188  StringPropertyPtr comboBox = mMethodsSelectorMap[service->getRegistrationType()];
189  QStringList values = comboBox->getValueRange();
190  if (!values.removeOne(service->getRegistrationMethod()))
191  reportWarning("RegistrationWidget::onServiceRemoved: Cannot find and remove service from combobox: "+ service->getRegistrationMethod());
192  comboBox->setValueRange(values);
193 }
194 
195 void RegistrationWidget::removeWidgetFromStackedWidget(QString widgetName, QStackedWidget *stackedWidget)
196 {
197  for(int i = 0; i < stackedWidget->count(); ++i)
198  {
199  QWidget* widget = stackedWidget->widget(i);
200  if(widget->objectName() == widgetName)
201  {
202  stackedWidget->removeWidget(widget);
203  delete widget;
204  }
205  }
206 }
207 
208 QString RegistrationWidget::defaultWhatsThis() const
209 {
210  return "<html>"
211  "<h3>Example plugin.</h3>"
212  "<p>Collection of all registration methods</p>"
213  "</html>";
214 }
215 
216 } /* namespace cx */
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:142
void reportError(QString msg)
Definition: cxLogger.cpp:92
Composite widget for string selection.
QDomElement getElement()
return the current element
boost::shared_ptr< class StringProperty > StringPropertyPtr
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
RegistrationWidget(ctkPluginContext *pluginContext, QWidget *parent=0)
static StringPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList range, QDomNode root=QDomNode())