Fraxinus  18.10
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxElastixWidget.h"
13 
14 #include <QPushButton>
15 #include <QLabel>
16 #include <QSpinBox>
17 
18 #include "cxTypeConversions.h"
19 
20 #include "cxTimedAlgorithm.h"
23 #include "cxFileSelectWidget.h"
25 #include "cxCheckBoxWidget.h"
26 #include "cxSettings.h"
28 #include "cxElastixExecuter.h"
29 #include "cxStringProperty.h"
30 #include "cxFilePathProperty.h"
31 #include "cxDataLocations.h"
32 #include "cxHelperWidgets.h"
33 
34 namespace cx
35 {
36 
37 ElastixWidget::ElastixWidget(RegServicesPtr services, QWidget* parent) :
38  RegistrationBaseWidget(services, parent, "org_custusx_registration_method_commandline_elastix_widget", "ElastiX Registration"),
39  mRegisterButton(NULL),
40  mParameterFileWidget0(NULL),
41  mFilePreviewWidget(NULL),
42  mTimedAlgorithmProgressBar(NULL),
43  mOptionsWidget(NULL)
44 {
45  this->setModified();
46 }
47 
49 {
50  if (!mElastixManager)
51  {
52  this->createUI();
53  }
54 }
55 
56 void ElastixWidget::createUI()
57 {
58  mElastixManager.reset(new ElastixManager(mServices));
59  connect(mElastixManager.get(), SIGNAL(elastixChanged()), this, SLOT(elastixChangedSlot()));
60 
61  mRegisterButton = new QPushButton("Register");
62  connect(mRegisterButton, SIGNAL(clicked()), this, SLOT(registerSlot()));
63 
64  QVBoxLayout* topLayout = new QVBoxLayout(this);
65  topLayout->setMargin(0);
66 
67  mOptionsWidget = this->createOptionsWidget();
68  mOptionsWidget->setVisible(settings()->value("registration/elastixShowDetails").toBool());
69 
70  mTimedAlgorithmProgressBar = new cx::TimedAlgorithmProgressBar;
71  mTimedAlgorithmProgressBar->attach(mElastixManager->getExecuter());
72 
73  QGridLayout* entryLayout = new QGridLayout;
74  entryLayout->setColumnStretch(1, 1);
75 
76  mFixedImage.reset(new StringPropertyRegistrationFixedImage(mServices->registration(), mServices->patient()));
77  new LabeledComboBoxWidget(this, mFixedImage, entryLayout, 0);
78  mMovingImage.reset(new StringPropertyRegistrationMovingImage(mServices->registration(), mServices->patient()));
79  new LabeledComboBoxWidget(this, mMovingImage, entryLayout, 1);
80 
81  new LabeledComboBoxWidget(this, mElastixManager->getParameters()->getCurrentPreset(), entryLayout, 2);
82 
83  QHBoxLayout* buttonsLayout = new QHBoxLayout;
84  buttonsLayout->addWidget(mRegisterButton);
85 
86  this->createAction(this,
87  QIcon(":/icons/open_icon_library/system-run-5.png"),
88  "Details", "Show Elastix Settings Details",
89  SLOT(toggleDetailsSlot()),
90  buttonsLayout);
91 
92  topLayout->addLayout(entryLayout);
93  topLayout->addLayout(buttonsLayout);
94  topLayout->addWidget(mOptionsWidget, 1);
95  topLayout->addStretch();
96  topLayout->addWidget(mTimedAlgorithmProgressBar);
97 
98  this->elastixChangedSlot();
99 }
100 
101 void ElastixWidget::toggleDetailsSlot()
102 {
103  mOptionsWidget->setVisible(!mOptionsWidget->isVisible());
104  settings()->setValue("registration/elastixShowDetails", mOptionsWidget->isVisible());
105 }
106 
107 QWidget* ElastixWidget::createOptionsWidget()
108 {
109  QWidget* retval = new QWidget(this);
110  QGridLayout* layout = new QGridLayout(retval);
111  layout->setMargin(0);
112 
113  int line = 0;
114 
115  layout->addWidget(this->createHorizontalLine(), line, 0, 1, 3);
116  ++line;
117 
118  layout->addWidget(new QLabel("Parameter File", this), line, 0);
119  mParameterFileWidget0 = new FileSelectWidget(this);
120  connect(mParameterFileWidget0, SIGNAL(fileSelected(QString)), this, SLOT(userParameterFileSelected(QString)));
121  layout->addWidget(mParameterFileWidget0, line, 1, 1, 2);
122  ++line;
123 
124  QWidget* executableWidget = sscCreateDataWidget(this, mElastixManager->getParameters()->getActiveExecutable());
125  layout->addWidget(executableWidget, line, 0, 1, 3);
126  ++line;
127 
128  QHBoxLayout* buttonsLayout = new QHBoxLayout;
129  layout->addLayout(buttonsLayout, line, 0, 1, 3);
130 
131  buttonsLayout->addWidget(new CheckBoxWidget(this, mElastixManager->getDisplayProcessMessages()));
132  buttonsLayout->addWidget(new CheckBoxWidget(this, mElastixManager->getDisableRendering()));
133 
134  this->createAction(this,
135  QIcon(":/icons/preset_remove.png"),
136  "Delete the current preset", "",
137  SLOT(deletePresetSlot()),
138  buttonsLayout);
139 
140  this->createAction(this,
141  QIcon(":/icons/preset_save.png"),
142  "Add the current setting as a preset", "",
143  SLOT(savePresetSlot()),
144  buttonsLayout);
145 
146  ++line;
147 
148  mFilePreviewWidget = new FilePreviewWidget(this);
149  mFilePreviewWidget->setSyntaxHighLighter<ElastixSyntaxHighlighter>();
150  mFilePreviewWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
151  layout->addWidget(mFilePreviewWidget, line, 0, 1, 3);
152  ++line;
153 
154  return retval;
155 }
156 
158 {}
159 
160 
161 void ElastixWidget::savePresetSlot()
162 {
163  ElastixParametersPtr par = mElastixManager->getParameters();
164 
165  QString newName = par->getPresetNameSuggesion();
166 
167  bool ok;
168  QString text = QInputDialog::getText(this, "Save Preset",
169  "Custom Preset Name", QLineEdit::Normal,
170  newName, &ok);
171  if (!ok || text.isEmpty())
172  return;
173 
174  par->saveCurrentPreset(text);
175 }
176 
177 void ElastixWidget::deletePresetSlot()
178 {
179  mElastixManager->getParameters()->removeCurrentPreset();
180 }
181 
182 void ElastixWidget::userParameterFileSelected(QString filename)
183 {
184  mElastixManager->getParameters()->getActiveParameterFile0()->setValue(filename);
185 }
186 
187 void ElastixWidget::recurseParameterFolders(QString root, QStringList* retval)
188 {
189  QDir folder(root);
190  folder.setFilter(QDir::AllDirs|QDir::NoDotAndDotDot);
191  QFileInfoList info = folder.entryInfoList();
192  for (int i=0; i<info.size(); ++i)
193  {
194  QString current = info[i].absoluteFilePath();
195  if (current.endsWith("/par"))
196  {
197  retval->append(current);
198  }
199 
200  this->recurseParameterFolders(current, retval);
201  }
202 }
203 
204 void ElastixWidget::elastixChangedSlot()
205 {
206  ElastixParametersPtr par = mElastixManager->getParameters();
207  EmbeddedFilepath par0 = par->getActiveParameterFile0()->getEmbeddedPath();
208  QStringList folders = par0.getRootPaths();
209  QStringList parfolders;
210  for (int i=0; i<folders.size(); ++i)
211  this->recurseParameterFolders(folders[i], &parfolders);
212 
213  mParameterFileWidget0->setPaths(parfolders);
214  QStringList nameFilters;
215  nameFilters << "*";
216  mParameterFileWidget0->setNameFilter(nameFilters);
217  mParameterFileWidget0->setFilename(par0.getAbsoluteFilepath());
218 
219 
220  mFilePreviewWidget->previewFileSlot(par0.getAbsoluteFilepath());
221 }
222 
223 void ElastixWidget::registerSlot()
224 {
225  mElastixManager->execute();
226 }
227 
228 
229 
230 } /* namespace cx */
QStringList getRootPaths() const
return the root of the existing root, first if no existing.
void setNameFilter(QStringList filter)
virtual void prePaintEvent()
Widget for the BoolPropertyBase.
QString getAbsoluteFilepath() const
return absolute filepath, select the existing root
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:58
Manager for interfacing to the ElastiX registration package.
virtual void previewFileSlot(const QString &absoluteFilePath)
View a xml document.
static QFrame * createHorizontalLine()
Creates a horizontal line which can be inserted into widgets.
ElastixWidget(RegServicesPtr services, QWidget *parent=NULL)
void setFilename(QString name)
boost::shared_ptr< class RegServices > RegServicesPtr
Definition: cxRegServices.h:20
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21
void attach(TimedAlgorithmPtr algorithm)
void setPaths(QStringList paths)
QWidget * sscCreateDataWidget(QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.
Widget for displaying and selecting a single file.
Namespace for all CustusX production code.