Fraxinus  17.12-rc4
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) 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 RegistrationTypeWidget::RegistrationTypeWidget(QString type, QString defVal, XmlOptionFile options, QWidget* parent) :
56  BaseWidget(parent, type, type),
57  mOptions(options)
58 {
59  mStackedWidget = new QStackedWidget(this);
60  mStackedWidget->setFocusPolicy(Qt::StrongFocus);
61 
62  QVBoxLayout *layoutV = new QVBoxLayout(this);
63 // layoutV->setMargin(0);
64 
65  mMethodSelector = StringProperty::initialize(type,
66  "Method",
67  "Select registration method",
68  defVal,
69  QStringList(),
70  mOptions.getElement());
71  connect(mMethodSelector.get(), &StringProperty::valueWasSet, this, &RegistrationTypeWidget::onIndexChanged);
72 
73  layoutV->addWidget(new LabeledComboBoxWidget(this, mMethodSelector));
74  layoutV->addWidget(mStackedWidget);
75 }
76 
78 {
79  mMethodSelector->setValue(selectRegistrationMethod);
80 }
81 
82 void RegistrationTypeWidget::onIndexChanged()
83 {
84  QString method = mMethodSelector->getValue();
85  int pos = mMethodSelector->getValueRange().indexOf(method);
86  mStackedWidget->setCurrentIndex(pos);
87  this->setObjectName(mStackedWidget->currentWidget()->objectName());
88 }
89 
91 {
92  mStackedWidget->addWidget(service->createWidget());
93  QStringList values = mMethodSelector->getValueRange();
94  values << service->getRegistrationMethod();
95  mMethodSelector->setValueRange(values);
96 
97  // initialize if not set
98  if (mMethodSelector->getValue().isEmpty())
99  {
100  mMethodSelector->setValue(service->getRegistrationMethod());
101  }
102 
103  if (mMethodSelector->getValue() == service->getRegistrationMethod())
104  this->onIndexChanged();
105 }
106 
108 {
109  this->removeWidgetFromStackedWidget(service->getWidgetName());
110 
111  QStringList values = mMethodSelector->getValueRange();
112  if (!values.removeOne(service->getRegistrationMethod()))
113  reportWarning("RegistrationWidget::onServiceRemoved: Cannot find and remove service from combobox: "+ service->getRegistrationMethod());
114  mMethodSelector->setValueRange(values);
115 }
116 
117 void RegistrationTypeWidget::removeWidgetFromStackedWidget(QString widgetName)
118 {
119  for(int i = 0; i < mStackedWidget->count(); ++i)
120  {
121  QWidget* widget = mStackedWidget->widget(i);
122  if(widget->objectName() == widgetName)
123  {
124  mStackedWidget->removeWidget(widget);
125  delete widget;
126  }
127  }
128 }
129 
130 
131 //---------------------------------------------------------
132 //---------------------------------------------------------
133 //---------------------------------------------------------
134 
135 
136 RegistrationWidget::RegistrationWidget(ctkPluginContext *pluginContext, QWidget* parent) :
137  TabbedWidget(parent, "org_custusx_registration_gui_widget", "Registration"),
138  mPluginContext(pluginContext),
139  mOptions(profile()->getXmlSettings().descend("RegistrationWidget"))
140 {
141  connect(mTabWidget, &QTabWidget::currentChanged, this, &RegistrationWidget::onCurrentChanged);
142  this->initRegistrationTypesWidgets();
143  this->initServiceListener();
144 }
145 
146 void RegistrationWidget::selectRegistrationMethod(QString registrationType, QString registrationMethodName)
147 {
148  RegistrationTypeWidget* widget = mRegistrationTypeMap[registrationType];
149  if(widget)
150  widget->selectRegistrationMethod(registrationMethodName);
151 }
152 
153 void RegistrationWidget::initRegistrationTypesWidgets()
154 {
155  mRegistrationTypes << "ImageToPatient" << "ImageToImage" << "ImageTransform";
156  QStringList typeDefaults;
157  typeDefaults << "Landmark" << "Landmark" << "";
158 
159  for(int i = 0; i < mRegistrationTypes.count(); ++i)
160  {
161  QString type = mRegistrationTypes[i];
162  RegistrationTypeWidget* widget =
163  new RegistrationTypeWidget(type,
164  typeDefaults[i],
165  mOptions, this);
166 
167  mRegistrationTypeMap[type] = widget;
168  this->addTab(widget, type);
169 
170  }
171 
172  // Create typeselector after the widgets, as the addwidget trigger
173  // a signal that causes type to be overwritten.
174  mTypeSelector = StringProperty::initialize("RegistrationTypes",
175  "Registration Types",
176  "Select registration type",
177  "",
178  mRegistrationTypes,
179  mOptions.getElement());
180  if (mRegistrationTypeMap.count(mTypeSelector->getValue()))
181  mTabWidget->setCurrentWidget(mRegistrationTypeMap[mTypeSelector->getValue()]);
182 }
183 
184 void RegistrationWidget::onCurrentChanged(int index)
185 {
186  if (index<0)
187  return;
188  if (mTypeSelector)
189  mTypeSelector->setValue(mRegistrationTypes[index]);
190 }
191 
192 void RegistrationWidget::initServiceListener()
193 {
194  mServiceListener.reset(new ServiceTrackerListener<RegistrationMethodService>(
195  mPluginContext,
196  boost::bind(&RegistrationWidget::onServiceAdded, this, _1),
197  boost::function<void (RegistrationMethodService*)>(),
198  boost::bind(&RegistrationWidget::onServiceRemoved, this, _1)
199  ));
200  mServiceListener->open();
201 }
202 
203 void RegistrationWidget::onServiceAdded(RegistrationMethodService* service)
204 {
205  QString type = service->getRegistrationType();
206  if(!mRegistrationTypeMap.count(type))
207  {
208  reportError("Unknown registrationType : " + type);
209  return;
210  }
211  mRegistrationTypeMap[type]->addMethod(service);
212 }
213 
214 void RegistrationWidget::onServiceRemoved(RegistrationMethodService *service)
215 {
216  QString type = service->getRegistrationType();
217  mRegistrationTypeMap[type]->removeMethod(service);
218 }
219 
220 } /* namespace cx */
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:181
void reportError(QString msg)
Definition: cxLogger.cpp:92
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:91
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:109
CompositeGenerator< T > values(T val1, T val2)
Definition: catch.hpp:3128
Helper class for xml files used to store ssc/cx data.
Namespace for all CustusX production code.