CustusX  15.4.0-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 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 
77 void RegistrationTypeWidget::onIndexChanged()
78 {
79  QString method = mMethodSelector->getValue();
80  int pos = mMethodSelector->getValueRange().indexOf(method);
81  mStackedWidget->setCurrentIndex(pos);
82  this->setObjectName(mStackedWidget->currentWidget()->objectName());
83 }
84 
86 {
87  mStackedWidget->addWidget(service->createWidget());
88  QStringList values = mMethodSelector->getValueRange();
89  values << service->getRegistrationMethod();
90  mMethodSelector->setValueRange(values);
91 
92  // initialize if not set
93  if (mMethodSelector->getValue().isEmpty())
94  {
95  mMethodSelector->setValue(service->getRegistrationMethod());
96  }
97 
98  if (mMethodSelector->getValue() == service->getRegistrationMethod())
99  this->onIndexChanged();
100 }
101 
103 {
104  this->removeWidgetFromStackedWidget(service->getWidgetName());
105 
106  QStringList values = mMethodSelector->getValueRange();
107  if (!values.removeOne(service->getRegistrationMethod()))
108  reportWarning("RegistrationWidget::onServiceRemoved: Cannot find and remove service from combobox: "+ service->getRegistrationMethod());
109  mMethodSelector->setValueRange(values);
110 }
111 
112 void RegistrationTypeWidget::removeWidgetFromStackedWidget(QString widgetName)
113 {
114  for(int i = 0; i < mStackedWidget->count(); ++i)
115  {
116  QWidget* widget = mStackedWidget->widget(i);
117  if(widget->objectName() == widgetName)
118  {
119  mStackedWidget->removeWidget(widget);
120  delete widget;
121  }
122  }
123 }
124 
125 
126 //---------------------------------------------------------
127 //---------------------------------------------------------
128 //---------------------------------------------------------
129 
130 
131 RegistrationWidget::RegistrationWidget(ctkPluginContext *pluginContext, QWidget* parent) :
132  TabbedWidget(parent, "org_custusx_registration_gui_widget", "Registration"),
133  mPluginContext(pluginContext),
134  mOptions(profile()->getXmlSettings().descend("RegistrationWidget"))
135 {
136  connect(mTabWidget, &QTabWidget::currentChanged, this, &RegistrationWidget::onCurrentChanged);
137  this->initRegistrationTypesWidgets();
138  this->initServiceListener();
139 }
140 
141 void RegistrationWidget::initRegistrationTypesWidgets()
142 {
143  mRegistrationTypes << "ImageToPatient" << "ImageToImage" << "ImageTransform";
144  QStringList typeDefaults;
145  typeDefaults << "Landmark" << "Landmark" << "";
146 
147  mTypeSelector = StringProperty::initialize("RegistrationTypes",
148  "Registration Types",
149  "Select registration type",
150  "",
151  mRegistrationTypes,
152  mOptions.getElement());
153  this->blockSignals(true); // we dont want the onCurrentChanged() to be called while constructing
154 
155  for(int i = 0; i < mRegistrationTypes.count(); ++i)
156  {
157  QString type = mRegistrationTypes[i];
158  RegistrationTypeWidget* widget =
159  new RegistrationTypeWidget(type,
160  typeDefaults[i],
161  mOptions, this);
162 
163  mRegistrationTypeMap[type] = widget;
164  this->addTab(widget, type);
165 
166  if (mTypeSelector->getValue() == type)
167  mTabWidget->setCurrentIndex(i);
168  }
169 
170  this->blockSignals(false);
171 }
172 
173 void RegistrationWidget::onCurrentChanged(int index)
174 {
175  if (index<0)
176  return;
177  mTypeSelector->setValue(mRegistrationTypes[index]);
178 }
179 
180 void RegistrationWidget::initServiceListener()
181 {
182  mServiceListener.reset(new ServiceTrackerListener<RegistrationMethodService>(
183  mPluginContext,
184  boost::bind(&RegistrationWidget::onServiceAdded, this, _1),
185  boost::function<void (RegistrationMethodService*)>(),
186  boost::bind(&RegistrationWidget::onServiceRemoved, this, _1)
187  ));
188  mServiceListener->open();
189 }
190 
191 void RegistrationWidget::onServiceAdded(RegistrationMethodService* service)
192 {
193  QString type = service->getRegistrationType();
194  if(!mRegistrationTypeMap.count(type))
195  {
196  reportError("Unknown registrationType : " + type);
197  return;
198  }
199  mRegistrationTypeMap[type]->addMethod(service);
200 }
201 
202 void RegistrationWidget::onServiceRemoved(RegistrationMethodService *service)
203 {
204  QString type = service->getRegistrationType();
205  mRegistrationTypeMap[type]->removeMethod(service);
206 }
207 
208 } /* namespace cx */
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:142
void reportError(QString msg)
Definition: cxLogger.cpp:92
void removeMethod(RegistrationMethodService *service)
virtual QWidget * createWidget()=0
virtual QString getRegistrationMethod()=0
Composite widget for string selection.
QTabWidget * mTabWidget
QDomElement getElement()
return the current element
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())
void addMethod(RegistrationMethodService *service)
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:108
Helper class for xml files used to store ssc/cx data.