NorMIT-nav  16.5
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 #include "cxFilePathProperty.h"
52 #include "cxDataLocations.h"
53 #include "cxHelperWidgets.h"
54 
55 namespace cx
56 {
57 
58 ElastixWidget::ElastixWidget(RegServicesPtr services, QWidget* parent) :
59  RegistrationBaseWidget(services, parent, "org_custusx_registration_method_commandline_elastix_widget", "ElastiX Registration"),
60  mRegisterButton(NULL),
61  mParameterFileWidget0(NULL),
62  mFilePreviewWidget(NULL),
63  mTimedAlgorithmProgressBar(NULL),
64  mOptionsWidget(NULL)
65 {
66  this->setModified();
67 }
68 
70 {
71  if (!mElastixManager)
72  {
73  this->createUI();
74  }
75 }
76 
77 void ElastixWidget::createUI()
78 {
79  mElastixManager.reset(new ElastixManager(mServices));
80  connect(mElastixManager.get(), SIGNAL(elastixChanged()), this, SLOT(elastixChangedSlot()));
81 
82  mRegisterButton = new QPushButton("Register");
83  connect(mRegisterButton, SIGNAL(clicked()), this, SLOT(registerSlot()));
84 
85  QVBoxLayout* topLayout = new QVBoxLayout(this);
86  topLayout->setMargin(0);
87 
88  mOptionsWidget = this->createOptionsWidget();
89  mOptionsWidget->setVisible(settings()->value("registration/elastixShowDetails").toBool());
90 
91  mTimedAlgorithmProgressBar = new cx::TimedAlgorithmProgressBar;
92  mTimedAlgorithmProgressBar->attach(mElastixManager->getExecuter());
93 
94  QGridLayout* entryLayout = new QGridLayout;
95  entryLayout->setColumnStretch(1, 1);
96 
97  mFixedImage.reset(new StringPropertyRegistrationFixedImage(mServices->registration(), mServices->patient()));
98  new LabeledComboBoxWidget(this, mFixedImage, entryLayout, 0);
99  mMovingImage.reset(new StringPropertyRegistrationMovingImage(mServices->registration(), mServices->patient()));
100  new LabeledComboBoxWidget(this, mMovingImage, entryLayout, 1);
101 
102  new LabeledComboBoxWidget(this, mElastixManager->getParameters()->getCurrentPreset(), entryLayout, 2);
103 
104  QHBoxLayout* buttonsLayout = new QHBoxLayout;
105  buttonsLayout->addWidget(mRegisterButton);
106 
107  this->createAction(this,
108  QIcon(":/icons/open_icon_library/system-run-5.png"),
109  "Details", "Show Elastix Settings Details",
110  SLOT(toggleDetailsSlot()),
111  buttonsLayout);
112 
113  topLayout->addLayout(entryLayout);
114  topLayout->addLayout(buttonsLayout);
115  topLayout->addWidget(mOptionsWidget, 1);
116  topLayout->addStretch();
117  topLayout->addWidget(mTimedAlgorithmProgressBar);
118 
119  this->elastixChangedSlot();
120 }
121 
122 void ElastixWidget::toggleDetailsSlot()
123 {
124  mOptionsWidget->setVisible(!mOptionsWidget->isVisible());
125  settings()->setValue("registration/elastixShowDetails", mOptionsWidget->isVisible());
126 }
127 
128 QWidget* ElastixWidget::createOptionsWidget()
129 {
130  QWidget* retval = new QWidget(this);
131  QGridLayout* layout = new QGridLayout(retval);
132  layout->setMargin(0);
133 
134  int line = 0;
135 
136  layout->addWidget(this->createHorizontalLine(), line, 0, 1, 3);
137  ++line;
138 
139  layout->addWidget(new QLabel("Parameter File", this), line, 0);
140  mParameterFileWidget0 = new FileSelectWidget(this);
141  connect(mParameterFileWidget0, SIGNAL(fileSelected(QString)), this, SLOT(userParameterFileSelected(QString)));
142  layout->addWidget(mParameterFileWidget0, line, 1, 1, 2);
143  ++line;
144 
145  QWidget* executableWidget = sscCreateDataWidget(this, mElastixManager->getParameters()->getActiveExecutable());
146  layout->addWidget(executableWidget, line, 0, 1, 3);
147  ++line;
148 
149  QHBoxLayout* buttonsLayout = new QHBoxLayout;
150  layout->addLayout(buttonsLayout, line, 0, 1, 3);
151 
152  buttonsLayout->addWidget(new CheckBoxWidget(this, mElastixManager->getDisplayProcessMessages()));
153  buttonsLayout->addWidget(new CheckBoxWidget(this, mElastixManager->getDisableRendering()));
154 
155  this->createAction(this,
156  QIcon(":/icons/preset_remove.png"),
157  "Delete the current preset", "",
158  SLOT(deletePresetSlot()),
159  buttonsLayout);
160 
161  this->createAction(this,
162  QIcon(":/icons/preset_save.png"),
163  "Add the current setting as a preset", "",
164  SLOT(savePresetSlot()),
165  buttonsLayout);
166 
167  ++line;
168 
169  mFilePreviewWidget = new FilePreviewWidget(this);
170  mFilePreviewWidget->setSyntaxHighLighter<ElastixSyntaxHighlighter>();
171  mFilePreviewWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
172  layout->addWidget(mFilePreviewWidget, line, 0, 1, 3);
173  ++line;
174 
175  return retval;
176 }
177 
179 {}
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::userParameterFileSelected(QString filename)
204 {
205  mElastixManager->getParameters()->getActiveParameterFile0()->setValue(filename);
206 }
207 
208 void ElastixWidget::recurseParameterFolders(QString root, QStringList* retval)
209 {
210  QDir folder(root);
211  folder.setFilter(QDir::AllDirs|QDir::NoDotAndDotDot);
212  QFileInfoList info = folder.entryInfoList();
213  for (int i=0; i<info.size(); ++i)
214  {
215  QString current = info[i].absoluteFilePath();
216  if (current.endsWith("/par"))
217  {
218  retval->append(current);
219  }
220 
221  this->recurseParameterFolders(current, retval);
222  }
223 }
224 
225 void ElastixWidget::elastixChangedSlot()
226 {
227  ElastixParametersPtr par = mElastixManager->getParameters();
228  EmbeddedFilepath par0 = par->getActiveParameterFile0()->getEmbeddedPath();
229  QStringList folders = par0.getRootPaths();
230  QStringList parfolders;
231  for (int i=0; i<folders.size(); ++i)
232  this->recurseParameterFolders(folders[i], &parfolders);
233 
234  mParameterFileWidget0->setPaths(parfolders);
235  QStringList nameFilters;
236  nameFilters << "*";
237  mParameterFileWidget0->setNameFilter(nameFilters);
238  mParameterFileWidget0->setFilename(par0.getAbsoluteFilepath());
239 
240 
241  mFilePreviewWidget->previewFileSlot(par0.getAbsoluteFilepath());
242 }
243 
244 void ElastixWidget::registerSlot()
245 {
246  mElastixManager->execute();
247 }
248 
249 
250 
251 } /* namespace cx */
void setNameFilter(QStringList filter)
virtual void prePaintEvent()
boost::shared_ptr< class RegServices > RegServicesPtr
Definition: cxRegServices.h:41
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:149
boost::shared_ptr< ElastixParameters > ElastixParametersPtr
void setValue(const QString &key, const QVariant &value)
Definition: cxSettings.cpp:79
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.
ElastixWidget(RegServicesPtr services, QWidget *parent=NULL)
void setFilename(QString name)
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:42
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.