CustusX  15.8
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 
44 namespace cx
45 {
46 
48 {
49  mOptions = options;
50 
51  mActiveExecutable = this->getExecutable();
52  mActiveParameterFile0 = this->getParameterFile("0");
53  mActiveParameterFile1 = this->getParameterFile("1");
54 
55  mCurrentPreset = StringProperty::initialize("currentPreset", "Preset",
56  "Current Preset", "Select Preset...",
57  QStringList(), mOptions.getElement());
58  connect(mCurrentPreset.get(), SIGNAL(changed()), this, SLOT(currentPresetChangedSlot()));
59 
60  this->currentPresetChangedSlot();
61 }
62 
63 void ElastixParameters::addDefaultPresets()
64 {
65  FilePathPropertyPtr exe = this->getExecutable();
66  exe->setValue(cx::DataLocations::findConfigFilePath("run_elastix.sh", this->getConfigUid()+"/elastix/bin"));
67  FilePathPropertyPtr par0 = this->getParameterFile("0");
68  par0->setValue("elastix/par/p_Rigid.txt");
69 
70  this->addDefaultPreset("elastix/p_Rigid", exe->getValue(), QStringList() << par0->getValue());
71 }
72 
73 void ElastixParameters::addDefaultPreset(QString name, QString executable, QStringList parameterFiles)
74 {
75  // ignore add if already present:
76  if (!mOptions.tryDescend("preset", "name", name).isNull())
77  return;
78 
79  XmlOptionFile node = mOptions.descend("preset", "name", name);
80  node.getElement().setAttribute("executable", executable);
81  for (unsigned i=0; i<parameterFiles.size(); ++i)
82  {
83  QString parName = QString("parameterFile%1").arg(i);
84  QString parVal = parameterFiles[i];
85  node.getElement().setAttribute(parName, parVal);
86  }
87 }
88 
89 void ElastixParameters::currentPresetChangedSlot()
90 {
91  this->reloadPresets();
92 
93  XmlOptionFile node = mOptions.descend("preset", "name", mCurrentPreset->getValue());
94  mActiveExecutable->setValue(node.getElement().attribute("executable"));
95 
96  mActiveParameterFile0->setValue(node.getElement().attribute("parameterFile0"));
97  mActiveParameterFile1->setValue(node.getElement().attribute("parameterFile1"));
98 
100 }
101 
102 QString ElastixParameters::getFullParameterFilename(QString filename)
103 {
104  return DataLocations::findConfigFilePath(filename, this->getConfigUid());
105 }
106 
108 {
109  return "org.custusx.registration.method.commandline";
110 }
111 
113 {
114  return mCurrentPreset;
115 }
116 
118 {
119  XmlOptionFile node = mOptions.descend("preset", "name", mCurrentPreset->getValue());
120  node.deleteNode();
121  this->reloadPresets();
122  mCurrentPreset->setValue("Select Preset...");
123 }
124 
125 void ElastixParameters::reloadPresets()
126 {
127  this->addDefaultPresets();
128  QStringList presets;
129  presets << "Select Preset...";
130 
131  QDomNodeList presetNodeList = mOptions.getElement().elementsByTagName("preset");
132  for (int i = 0; i < presetNodeList.count(); ++i)
133  {
134  presets << presetNodeList.item(i).toElement().attribute("name");
135  }
136  presets.removeDuplicates();
137 
138  mCurrentPreset->blockSignals(true);
139  mCurrentPreset->setValueRange(presets);
140  mCurrentPreset->blockSignals(false);
141 }
142 
144 {
145  XmlOptionFile node = mOptions.descend("preset", "name", name);
146  node.getElement().setAttribute("executable", mActiveExecutable->getEmbeddedPath().getRelativeFilepath());
147  node.getElement().setAttribute("parameterFile0", mActiveParameterFile0->getEmbeddedPath().getRelativeFilepath());
148  node.getElement().setAttribute("parameterFile1", mActiveParameterFile1->getEmbeddedPath().getRelativeFilepath());
149  mCurrentPreset->setValue(name);
150 }
151 
152 FilePathPropertyPtr ElastixParameters::getExecutable()
153 {
154  QStringList paths = DataLocations::getRootConfigPaths();
155  paths = DataLocations::appendStringToAllElements(paths, "/"+this->getConfigUid());
156 
157  QDomElement root;
158  FilePathPropertyPtr retval;
159  retval = FilePathProperty::initialize("executable", "Executable",
160  "Name of registration executable",
161  "",
162  paths,
163  root);
165  return retval;
166 }
167 
168 FilePathPropertyPtr ElastixParameters::getParameterFile(QString uid)
169 {
170  QStringList paths = DataLocations::getRootConfigPaths();
171  paths = DataLocations::appendStringToAllElements(paths, "/"+this->getConfigUid());
172 
173  QDomElement root;
174  FilePathPropertyPtr retval;
175  retval = FilePathProperty::initialize("parameter"+uid, "Parameter"+uid,
176  "Name of parameter file "+uid,
177  "",
178  paths,
179  root);
181  return retval;
182 }
183 
184 bool ElastixParameters::validParameterFile(QString file) const
185 {
186  return QFileInfo(file).exists() && QFileInfo(file).isFile();
187 }
188 
190 {
191  QString p0 = mActiveParameterFile0->getEmbeddedPath().getAbsoluteFilepath();
192  QString p1 = mActiveParameterFile1->getEmbeddedPath().getAbsoluteFilepath();
193 
194  QStringList retval;
195  if (this->validParameterFile(p0))
196  retval << p0;
197  if (this->validParameterFile(p1))
198  retval << p1;
199  return retval;
200 }
201 
203 {
204  QString retval = QFileInfo(mActiveExecutable->getValue()).baseName();
205  QStringList parFiles = this->getActiveParameterFiles();
206  for (unsigned i=0; i<parFiles.size(); ++i)
207  retval += "/" + QFileInfo(parFiles[i]).baseName();
208  return retval;
209 }
210 
211 } /* namespace cx */
void saveCurrentPreset(QString newName)
void removeCurrentPreset()
Remove the currently selected preset. Reload.
static QStringList getRootConfigPaths()
QDomElement getElement()
return the current element
ElastixParameters(XmlOptionFile options)
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 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.
boost::shared_ptr< class StringPropertyBase > StringPropertyBasePtr
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.