NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxElastixParameters.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 "cxElastixParameters.h"
34 
35 #include <QDir>
36 
37 #include "cxDataLocations.h"
38 #include "cxSettings.h"
39 #include "cxTypeConversions.h"
40 #include "cxProfile.h"
41 #include "cxFilePathProperty.h"
42 #include "cxLogger.h"
43 #include <QApplication>
44 
45 namespace cx
46 {
47 
49 {
50  mOptions = options;
51 
52  mActiveExecutable = this->getExecutable();
53  mActiveParameterFile0 = this->getParameterFile("0");
54  mActiveParameterFile1 = this->getParameterFile("1");
55 
56  mCurrentPreset = StringProperty::initialize("currentPreset", "Preset",
57  "Current Preset", "Select Preset...",
58  QStringList(), mOptions.getElement());
59  connect(mCurrentPreset.get(), SIGNAL(changed()), this, SLOT(currentPresetChangedSlot()));
60 
61  this->currentPresetChangedSlot();
62 }
63 
64 void ElastixParameters::addDefaultPresets()
65 {
66  FilePathPropertyPtr exe = this->getExecutable();
67 
69  {
70  exe->setValue(cx::DataLocations::findConfigFilePath("run_elastix.sh", this->getConfigUid()+"/elastix/bin"));
71  }
72  else
73  {
74  exe->setValue(DataLocations::findExecutableInStandardLocations("elastix"));
75  }
76 
77  FilePathPropertyPtr par0 = this->getParameterFile("0");
78  par0->setValue("elastix/par/p_Rigid.txt");
79 
80  this->addDefaultPreset("elastix/p_Rigid", exe->getValue(), QStringList() << par0->getValue());
81 }
82 
83 void ElastixParameters::addDefaultPreset(QString name, QString executable, QStringList parameterFiles)
84 {
85  // ignore add if already present:
86  if (!mOptions.tryDescend("preset", "name", name).isNull())
87  return;
88 
89  XmlOptionFile node = mOptions.descend("preset", "name", name);
90  node.getElement().setAttribute("executable", executable);
91  for (unsigned i=0; i<parameterFiles.size(); ++i)
92  {
93  QString parName = QString("parameterFile%1").arg(i);
94  QString parVal = parameterFiles[i];
95  node.getElement().setAttribute(parName, parVal);
96  }
97 }
98 
99 void ElastixParameters::currentPresetChangedSlot()
100 {
101  this->reloadPresets();
102 
103  XmlOptionFile node = mOptions.descend("preset", "name", mCurrentPreset->getValue());
104  mActiveExecutable->setValue(node.getElement().attribute("executable"));
105 
106  mActiveParameterFile0->setValue(node.getElement().attribute("parameterFile0"));
107  mActiveParameterFile1->setValue(node.getElement().attribute("parameterFile1"));
108 
110 }
111 
112 QString ElastixParameters::getFullParameterFilename(QString filename)
113 {
114  return DataLocations::findConfigFilePath(filename, this->getConfigUid());
115 }
116 
118 {
119  return "org.custusx.registration.method.commandline";
120 }
121 
123 {
124  return mCurrentPreset;
125 }
126 
128 {
129  XmlOptionFile node = mOptions.descend("preset", "name", mCurrentPreset->getValue());
130  node.deleteNode();
131  this->reloadPresets();
132  mCurrentPreset->setValue("Select Preset...");
133 }
134 
135 void ElastixParameters::reloadPresets()
136 {
137  this->addDefaultPresets();
138  QStringList presets;
139  presets << "Select Preset...";
140 
141  QDomNodeList presetNodeList = mOptions.getElement().elementsByTagName("preset");
142  for (int i = 0; i < presetNodeList.count(); ++i)
143  {
144  presets << presetNodeList.item(i).toElement().attribute("name");
145  }
146  presets.removeDuplicates();
147 
148  mCurrentPreset->blockSignals(true);
149  mCurrentPreset->setValueRange(presets);
150  mCurrentPreset->blockSignals(false);
151 }
152 
154 {
155  XmlOptionFile node = mOptions.descend("preset", "name", name);
156  node.getElement().setAttribute("executable", mActiveExecutable->getEmbeddedPath().getRelativeFilepath());
157  node.getElement().setAttribute("parameterFile0", mActiveParameterFile0->getEmbeddedPath().getRelativeFilepath());
158  node.getElement().setAttribute("parameterFile1", mActiveParameterFile1->getEmbeddedPath().getRelativeFilepath());
159  mCurrentPreset->setValue(name);
160 }
161 
162 FilePathPropertyPtr ElastixParameters::getExecutable()
163 {
164  QStringList paths;
166  {
168  paths = DataLocations::appendStringToAllElements(paths, "/"+this->getConfigUid());
169  }
170  else
171  {
172  paths << qApp->applicationDirPath();
173  }
174 
175  QDomElement root;
176  FilePathPropertyPtr retval;
177  retval = FilePathProperty::initialize("executable", "Executable",
178  "Name of registration executable",
179  "",
180  paths,
181  root);
183  return retval;
184 }
185 
186 FilePathPropertyPtr ElastixParameters::getParameterFile(QString uid)
187 {
188  QStringList paths = DataLocations::getRootConfigPaths();
189  paths = DataLocations::appendStringToAllElements(paths, "/"+this->getConfigUid());
190 
191  QDomElement root;
192  FilePathPropertyPtr retval;
193  retval = FilePathProperty::initialize("parameter"+uid, "Parameter"+uid,
194  "Name of parameter file "+uid,
195  "",
196  paths,
197  root);
199  return retval;
200 }
201 
202 bool ElastixParameters::validParameterFile(QString file) const
203 {
204  return QFileInfo(file).exists() && QFileInfo(file).isFile();
205 }
206 
208 {
209  QString p0 = mActiveParameterFile0->getEmbeddedPath().getAbsoluteFilepath();
210  QString p1 = mActiveParameterFile1->getEmbeddedPath().getAbsoluteFilepath();
211 
212  QStringList retval;
213  if (this->validParameterFile(p0))
214  retval << p0;
215  if (this->validParameterFile(p1))
216  retval << p1;
217  return retval;
218 }
219 
221 {
222  QString retval = QFileInfo(mActiveExecutable->getValue()).baseName();
223  QStringList parFiles = this->getActiveParameterFiles();
224  for (unsigned i=0; i<parFiles.size(); ++i)
225  retval += "/" + QFileInfo(parFiles[i]).baseName();
226  return retval;
227 }
228 
229 } /* namespace cx */
void saveCurrentPreset(QString newName)
void removeCurrentPreset()
Remove the currently selected preset. Reload.
static bool isRunFromBuildFolder()
static QStringList getRootConfigPaths()
QDomElement getElement()
return the current element
ElastixParameters(XmlOptionFile options)
boost::shared_ptr< class StringPropertyBase > StringPropertyBasePtr
QStringList getActiveParameterFiles() const
StringPropertyBasePtr getCurrentPreset()
static QString findConfigFilePath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath="")
void changed()
emit when the underlying data value is changed: The user interface will be updated.
static StringPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList range, QDomNode root=QDomNode())
static FilePathPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList paths, QDomNode root=QDomNode())
void deleteNode()
Delete the current node.
static QString findExecutableInStandardLocations(QString filename)
look for an exe in the same folder as the executable or bundle.
static QStringList appendStringToAllElements(QStringList root, QString suffix)
bool isNull() const
checks if this is null
boost::shared_ptr< class FilePathProperty > FilePathPropertyPtr
XmlOptionFile tryDescend(QString element, QString attributeName, QString attributeValue) const
Helper class for xml files used to store ssc/cx data.
XmlOptionFile descend(QString element) const
step one level down in the xml tree
QString getPresetNameSuggesion() const
create a name describing the active state, can be used as name for a new preset.