CustusX  15.4.0-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, "org_custusx_registration_method_commandline_elastix_widget", "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 
77  QVBoxLayout* topLayout = new QVBoxLayout(this);
78  topLayout->setMargin(0);
79 
80  mOptionsWidget = this->createOptionsWidget();
81  mOptionsWidget->setVisible(settings()->value("registration/elastixShowDetails").toBool());
82 
83  mTimedAlgorithmProgressBar = new cx::TimedAlgorithmProgressBar;
84  mTimedAlgorithmProgressBar->attach(mElastixManager->getExecuter());
85 
86  QGridLayout* entryLayout = new QGridLayout;
87  entryLayout->setColumnStretch(1, 1);
88 
90  new LabeledComboBoxWidget(this, mFixedImage, entryLayout, 0);
92  new LabeledComboBoxWidget(this, mMovingImage, entryLayout, 1);
93 
94  new LabeledComboBoxWidget(this, mElastixManager->getParameters()->getCurrentPreset(), entryLayout, 2);
95 
96  QHBoxLayout* buttonsLayout = new QHBoxLayout;
97  buttonsLayout->addWidget(mRegisterButton);
98 
99  this->createAction(this,
100  QIcon(":/icons/open_icon_library/system-run-5.png"),
101  "Details", "Show Elastix Settings Details",
102  SLOT(toggleDetailsSlot()),
103  buttonsLayout);
104 
105  topLayout->addLayout(entryLayout);
106  topLayout->addLayout(buttonsLayout);
107  topLayout->addWidget(mOptionsWidget, 1);
108  topLayout->addStretch();
109  topLayout->addWidget(mTimedAlgorithmProgressBar);
110 
111  this->elastixChangedSlot();
112 }
113 
114 void ElastixWidget::toggleDetailsSlot()
115 {
116  mOptionsWidget->setVisible(!mOptionsWidget->isVisible());
117  settings()->setValue("registration/elastixShowDetails", mOptionsWidget->isVisible());
118 }
119 
120 QWidget* ElastixWidget::createOptionsWidget()
121 {
122  QWidget* retval = new QWidget(this);
123  QGridLayout* layout = new QGridLayout(retval);
124  layout->setMargin(0);
125 
126  int line = 0;
127 
128  layout->addWidget(this->createHorizontalLine(), line, 0, 1, 3);
129  ++line;
130 
131  layout->addWidget(new QLabel("Parameter File", this), line, 0);
132  mParameterFileWidget0 = new FileSelectWidget(this);
133  connect(mParameterFileWidget0, SIGNAL(fileSelected(QString)), this, SLOT(userParameterFileSelected(QString)));
134  layout->addWidget(mParameterFileWidget0, line, 1, 1, 2);
135  ++line;
136 
137  layout->addWidget(new QLabel("Executable", this), line, 0);
138  mExecutableEdit = new QLineEdit(this);
139  connect(mExecutableEdit, SIGNAL(editingFinished()), this, SLOT(executableEditFinishedSlot()));
140  layout->addWidget(mExecutableEdit, line, 1);
141 
142  QAction* browseExecutableAction = new QAction(QIcon(":/icons/open.png"), "Browse", this);
143  browseExecutableAction->setStatusTip("Select the elastiX executable");
144  connect(browseExecutableAction, SIGNAL(triggered()), this, SLOT(browseExecutableSlot()));
145  QToolButton* button = new QToolButton();
146  button->setDefaultAction(browseExecutableAction);
147  layout->addWidget(button, line, 2);
148  ++line;
149 
150  QHBoxLayout* buttonsLayout = new QHBoxLayout;
151  layout->addLayout(buttonsLayout, line, 0, 1, 3);
152 
153  buttonsLayout->addWidget(new CheckBoxWidget(this, mElastixManager->getDisplayProcessMessages()));
154  buttonsLayout->addWidget(new CheckBoxWidget(this, mElastixManager->getDisableRendering()));
155 
156  this->createAction(this,
157  QIcon(":/icons/preset_remove.png"),
158  "Delete the current preset", "",
159  SLOT(deletePresetSlot()),
160  buttonsLayout);
161 
162  this->createAction(this,
163  QIcon(":/icons/preset_save.png"),
164  "Add the current setting as a preset", "",
165  SLOT(savePresetSlot()),
166  buttonsLayout);
167 
168  ++line;
169 
170  mFilePreviewWidget = new FilePreviewWidget(this);
171  mFilePreviewWidget->setSyntaxHighLighter<ElastixSyntaxHighlighter>();
172  mFilePreviewWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
173  layout->addWidget(mFilePreviewWidget, line, 0, 1, 3);
174  ++line;
175 
176  return retval;
177 }
178 
180 {}
181 
182 void ElastixWidget::savePresetSlot()
183 {
184  ElastixParametersPtr par = mElastixManager->getParameters();
185 
186  QString newName = par->getPresetNameSuggesion();
187 
188  bool ok;
189  QString text = QInputDialog::getText(this, "Save Preset",
190  "Custom Preset Name", QLineEdit::Normal,
191  newName, &ok);
192  if (!ok || text.isEmpty())
193  return;
194 
195  par->saveCurrentPreset(text);
196 }
197 
198 void ElastixWidget::deletePresetSlot()
199 {
200  mElastixManager->getParameters()->removeCurrentPreset();
201 }
202 
203 void ElastixWidget::executableEditFinishedSlot()
204 {
205  mElastixManager->getParameters()->setActiveExecutable(mExecutableEdit->text());
206 }
207 
208 void ElastixWidget::browseExecutableSlot()
209 {
210  QString fileName = QFileDialog::getOpenFileName(this, tr("Select Executable"), "~");
211  if (fileName.isEmpty())
212  return;
213 
214  mElastixManager->getParameters()->setActiveExecutable(fileName);
215 }
216 
217 void ElastixWidget::userParameterFileSelected(QString filename)
218 {
219  mElastixManager->getParameters()->setActiveParameterFile0(filename);
220 }
221 
222 void ElastixWidget::elastixChangedSlot()
223 {
224  ElastixParametersPtr par = mElastixManager->getParameters();
225  QStringList folders = par->getParameterFilesDir();
226  for (int i=0; i<folders.size(); ++i)
227  {
228  QDir folder(folders[i]);
229  folder.mkpath(".");
230  }
231 
232  mParameterFileWidget0->setPaths(folders);
233  QStringList nameFilters;
234  nameFilters << "*.*";
235  mParameterFileWidget0->setNameFilter(nameFilters);
236  mParameterFileWidget0->setFilename(par->getActiveParameterFile0());
237 
238  mFilePreviewWidget->previewFileSlot(par->getActiveParameterFile0());
239 
240  mExecutableEdit->blockSignals(true);
241  mExecutableEdit->setText(par->getActiveExecutable());
242  mExecutableEdit->blockSignals(false);
243 }
244 
245 void ElastixWidget::registerSlot()
246 {
247  mElastixManager->execute();
248 }
249 
250 
251 
252 } /* namespace cx */
void setNameFilter(QStringList filter)
virtual void prePaintEvent()
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:129
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