CustusX  15.3.4-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxElastixWidget.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 "cxElastixWidget.h"
34 
35 #include <QPushButton>
36 #include <QLabel>
37 #include <QSpinBox>
38 
39 #include "cxTypeConversions.h"
40 
41 #include "cxTimedAlgorithm.h"
44 #include "cxFileSelectWidget.h"
46 #include "cxCheckBoxWidget.h"
47 #include "cxSettings.h"
49 #include "cxElastixExecuter.h"
50 #include "cxStringProperty.h"
51 
52 namespace cx
53 {
54 
55 ElastixWidget::ElastixWidget(RegServices services, QWidget* parent) :
56  RegistrationBaseWidget(services, parent, "ElastiXWidget", "ElastiX Registration")
57 {
58  this->setModified();
59 }
60 
62 {
63  if (!mElastixManager)
64  {
65  this->createUI();
66  }
67 }
68 
69 void ElastixWidget::createUI()
70 {
71  mElastixManager.reset(new ElastixManager(mServices));
72  connect(mElastixManager.get(), SIGNAL(elastixChanged()), this, SLOT(elastixChangedSlot()));
73 
74  mRegisterButton = new QPushButton("Register");
75  connect(mRegisterButton, SIGNAL(clicked()), this, SLOT(registerSlot()));
76  mRegisterButton->setToolTip(this->defaultWhatsThis());
77 
78  QVBoxLayout* topLayout = new QVBoxLayout(this);
79  topLayout->setMargin(0);
80 
81  mOptionsWidget = this->createOptionsWidget();
82  mOptionsWidget->setVisible(settings()->value("registration/elastixShowDetails").toBool());
83 
84  mTimedAlgorithmProgressBar = new cx::TimedAlgorithmProgressBar;
85  mTimedAlgorithmProgressBar->attach(mElastixManager->getExecuter());
86 
87  QGridLayout* entryLayout = new QGridLayout;
88  entryLayout->setColumnStretch(1, 1);
89 
91  new LabeledComboBoxWidget(this, mFixedImage, entryLayout, 0);
93  new LabeledComboBoxWidget(this, mMovingImage, entryLayout, 1);
94 
95  new LabeledComboBoxWidget(this, mElastixManager->getParameters()->getCurrentPreset(), entryLayout, 2);
96 
97  QHBoxLayout* buttonsLayout = new QHBoxLayout;
98  buttonsLayout->addWidget(mRegisterButton);
99 
100  this->createAction(this,
101  QIcon(":/icons/open_icon_library/system-run-5.png"),
102  "Details", "Show Elastix Settings Details",
103  SLOT(toggleDetailsSlot()),
104  buttonsLayout);
105 
106  topLayout->addLayout(entryLayout);
107  topLayout->addLayout(buttonsLayout);
108  topLayout->addWidget(mOptionsWidget, 1);
109  topLayout->addStretch();
110  topLayout->addWidget(mTimedAlgorithmProgressBar);
111 
112  this->elastixChangedSlot();
113 }
114 
115 void ElastixWidget::toggleDetailsSlot()
116 {
117  mOptionsWidget->setVisible(!mOptionsWidget->isVisible());
118  settings()->setValue("registration/elastixShowDetails", mOptionsWidget->isVisible());
119 }
120 
121 QWidget* ElastixWidget::createOptionsWidget()
122 {
123  QWidget* retval = new QWidget(this);
124  QGridLayout* layout = new QGridLayout(retval);
125  layout->setMargin(0);
126 
127  int line = 0;
128 
129  layout->addWidget(this->createHorizontalLine(), line, 0, 1, 3);
130  ++line;
131 
132  layout->addWidget(new QLabel("Parameter File", this), line, 0);
133  mParameterFileWidget0 = new FileSelectWidget(this);
134  connect(mParameterFileWidget0, SIGNAL(fileSelected(QString)), this, SLOT(userParameterFileSelected(QString)));
135  layout->addWidget(mParameterFileWidget0, line, 1, 1, 2);
136  ++line;
137 
138  layout->addWidget(new QLabel("Executable", this), line, 0);
139  mExecutableEdit = new QLineEdit(this);
140  connect(mExecutableEdit, SIGNAL(editingFinished()), this, SLOT(executableEditFinishedSlot()));
141  layout->addWidget(mExecutableEdit, line, 1);
142 
143  QAction* browseExecutableAction = new QAction(QIcon(":/icons/open.png"), "Browse", this);
144  browseExecutableAction->setStatusTip("Select the elastiX executable");
145  connect(browseExecutableAction, SIGNAL(triggered()), this, SLOT(browseExecutableSlot()));
146  QToolButton* button = new QToolButton();
147  button->setDefaultAction(browseExecutableAction);
148  layout->addWidget(button, line, 2);
149  ++line;
150 
151  QHBoxLayout* buttonsLayout = new QHBoxLayout;
152  layout->addLayout(buttonsLayout, line, 0, 1, 3);
153 
154  buttonsLayout->addWidget(new CheckBoxWidget(this, mElastixManager->getDisplayProcessMessages()));
155  buttonsLayout->addWidget(new CheckBoxWidget(this, mElastixManager->getDisableRendering()));
156 
157  this->createAction(this,
158  QIcon(":/icons/preset_remove.png"),
159  "Delete the current preset", "",
160  SLOT(deletePresetSlot()),
161  buttonsLayout);
162 
163  this->createAction(this,
164  QIcon(":/icons/preset_save.png"),
165  "Add the current setting as a preset", "",
166  SLOT(savePresetSlot()),
167  buttonsLayout);
168 
169  ++line;
170 
171  mFilePreviewWidget = new FilePreviewWidget(this);
172  mFilePreviewWidget->setSyntaxHighLighter<ElastixSyntaxHighlighter>();
173  mFilePreviewWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
174  layout->addWidget(mFilePreviewWidget, line, 0, 1, 3);
175  ++line;
176 
177  return retval;
178 }
179 
181 {}
182 
184 {
185  return "<html>"
186  "<h3>ElastiX Registration.</h3>"
187  "<p>Select two datasets you want to register to each other, "
188  "and a preset suitable for your images.</p>"
189  "<p><a http://elastix.isi.uu.nl/> ElastiX </a> is normally used for the registration "
190  "as an external application, although any program with the same input/output "
191  "can be used. Add parameter files to the folder config/elastix in order to show "
192  "them to CustusX.</p>"
193  "<p>If a nonlinear registration is selected, CustusX will attempt to import the "
194  "volume produced by ElastiX.</p>"
195  "</html>";
196 }
197 
198 void ElastixWidget::savePresetSlot()
199 {
200  ElastixParametersPtr par = mElastixManager->getParameters();
201 
202  QString newName = par->getPresetNameSuggesion();
203 
204  bool ok;
205  QString text = QInputDialog::getText(this, "Save Preset",
206  "Custom Preset Name", QLineEdit::Normal,
207  newName, &ok);
208  if (!ok || text.isEmpty())
209  return;
210 
211  par->saveCurrentPreset(text);
212 }
213 
214 void ElastixWidget::deletePresetSlot()
215 {
216  mElastixManager->getParameters()->removeCurrentPreset();
217 }
218 
219 void ElastixWidget::executableEditFinishedSlot()
220 {
221  mElastixManager->getParameters()->setActiveExecutable(mExecutableEdit->text());
222 }
223 
224 void ElastixWidget::browseExecutableSlot()
225 {
226  QString fileName = QFileDialog::getOpenFileName(this, tr("Select Executable"), "~");
227  if (fileName.isEmpty())
228  return;
229 
230  mElastixManager->getParameters()->setActiveExecutable(fileName);
231 }
232 
233 void ElastixWidget::userParameterFileSelected(QString filename)
234 {
235  mElastixManager->getParameters()->setActiveParameterFile0(filename);
236 }
237 
238 void ElastixWidget::elastixChangedSlot()
239 {
240  ElastixParametersPtr par = mElastixManager->getParameters();
241  QStringList folders = par->getParameterFilesDir();
242  for (int i=0; i<folders.size(); ++i)
243  {
244  QDir folder(folders[i]);
245  folder.mkpath(".");
246  }
247 
248  mParameterFileWidget0->setPaths(folders);
249  QStringList nameFilters;
250  nameFilters << "*.*";
251  mParameterFileWidget0->setNameFilter(nameFilters);
252  mParameterFileWidget0->setFilename(par->getActiveParameterFile0());
253 
254  mFilePreviewWidget->previewFileSlot(par->getActiveParameterFile0());
255 
256  mExecutableEdit->blockSignals(true);
257  mExecutableEdit->setText(par->getActiveExecutable());
258  mExecutableEdit->blockSignals(false);
259 }
260 
261 void ElastixWidget::registerSlot()
262 {
263  mElastixManager->execute();
264 }
265 
266 
267 
268 } /* namespace cx */
void setNameFilter(QStringList filter)
virtual void prePaintEvent()
virtual QString defaultWhatsThis() const
Returns a short description of what this widget will do for you.
Composite widget for string selection.
Show progress for a TimedBaseAlgorithm.
QAction * createAction(QObject *parent, QIcon iconName, QString text, QString tip, T slot, QLayout *layout=NULL, QToolButton *button=new QToolButton())
Definition: cxBaseWidget.h:130
boost::shared_ptr< ElastixParameters > ElastixParametersPtr
void setValue(const QString &key, const QVariant &value)
Definition: cxSettings.cpp:91
Manager for interfacing to the ElastiX registration package.
virtual void previewFileSlot(const QString &absoluteFilePath)
static QFrame * createHorizontalLine()
Creates a horizontal line which can be inserted into widgets.
void setFilename(QString name)
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:42
void attach(TimedAlgorithmPtr algorithm)
void setPaths(QStringList paths)
PatientModelServicePtr patientModelService
ElastixWidget(RegServices services, QWidget *parent=NULL)
RegistrationServicePtr registrationService
Definition: cxRegServices.h:60