CustusX  22.04-rc5
An IGT application
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) 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 "cxElastixParameters.h"
13 
14 #include <QDir>
15 
16 #include "cxDataLocations.h"
17 #include "cxSettings.h"
18 #include "cxTypeConversions.h"
19 #include "cxProfile.h"
20 #include "cxFilePathProperty.h"
21 #include "cxLogger.h"
22 #include <QApplication>
23 
24 namespace cx
25 {
26 
28 {
29  mOptions = options;
30 
31  mActiveExecutable = this->getExecutable();
32  mActiveParameterFile0 = this->getParameterFile("0");
33  mActiveParameterFile1 = this->getParameterFile("1");
34 
35  mCurrentPreset = StringProperty::initialize("currentPreset", "Preset",
36  "Current Preset", "Select Preset...",
37  QStringList(), mOptions.getElement());
38  connect(mCurrentPreset.get(), SIGNAL(changed()), this, SLOT(currentPresetChangedSlot()));
39 
40  this->currentPresetChangedSlot();
41 }
42 
43 void ElastixParameters::addDefaultPresets()
44 {
45  FilePathPropertyPtr exe = this->getExecutable();
46 
48  {
49  exe->setValue(cx::DataLocations::findConfigFilePath("run_elastix.sh", this->getConfigUid()+"/elastix/bin"));
50  }
51  else
52  {
53  exe->setValue(DataLocations::findExecutableInStandardLocations("elastix"));
54  }
55 
56  FilePathPropertyPtr par0 = this->getParameterFile("0");
57  par0->setValue("elastix/par/p_Rigid.txt");
58 
59  this->addDefaultPreset("elastix/p_Rigid", exe->getValue(), QStringList() << par0->getValue());
60 }
61 
62 void ElastixParameters::addDefaultPreset(QString name, QString executable, QStringList parameterFiles)
63 {
64  // ignore add if already present:
65  if (!mOptions.tryDescend("preset", "name", name).isNull())
66  return;
67 
68  XmlOptionFile node = mOptions.descend("preset", "name", name);
69  node.getElement().setAttribute("executable", executable);
70  for (unsigned i=0; i<parameterFiles.size(); ++i)
71  {
72  QString parName = QString("parameterFile%1").arg(i);
73  QString parVal = parameterFiles[i];
74  node.getElement().setAttribute(parName, parVal);
75  }
76 }
77 
78 void ElastixParameters::currentPresetChangedSlot()
79 {
80  this->reloadPresets();
81 
82  XmlOptionFile node = mOptions.descend("preset", "name", mCurrentPreset->getValue());
83  mActiveExecutable->setValue(node.getElement().attribute("executable"));
84 
85  mActiveParameterFile0->setValue(node.getElement().attribute("parameterFile0"));
86  mActiveParameterFile1->setValue(node.getElement().attribute("parameterFile1"));
87 
89 }
90 
91 QString ElastixParameters::getFullParameterFilename(QString filename)
92 {
93  return DataLocations::findConfigFilePath(filename, this->getConfigUid());
94 }
95 
97 {
98  return "org.custusx.registration.method.commandline";
99 }
100 
102 {
103  return mCurrentPreset;
104 }
105 
107 {
108  XmlOptionFile node = mOptions.descend("preset", "name", mCurrentPreset->getValue());
109  node.deleteNode();
110  this->reloadPresets();
111  mCurrentPreset->setValue("Select Preset...");
112 }
113 
114 void ElastixParameters::reloadPresets()
115 {
116  this->addDefaultPresets();
117  QStringList presets;
118  presets << "Select Preset...";
119 
120  QDomNodeList presetNodeList = mOptions.getElement().elementsByTagName("preset");
121  for (int i = 0; i < presetNodeList.count(); ++i)
122  {
123  presets << presetNodeList.item(i).toElement().attribute("name");
124  }
125  presets.removeDuplicates();
126 
127  mCurrentPreset->blockSignals(true);
128  mCurrentPreset->setValueRange(presets);
129  mCurrentPreset->blockSignals(false);
130 }
131 
133 {
134  XmlOptionFile node = mOptions.descend("preset", "name", name);
135  node.getElement().setAttribute("executable", mActiveExecutable->getEmbeddedPath().getRelativeFilepath());
136  node.getElement().setAttribute("parameterFile0", mActiveParameterFile0->getEmbeddedPath().getRelativeFilepath());
137  node.getElement().setAttribute("parameterFile1", mActiveParameterFile1->getEmbeddedPath().getRelativeFilepath());
138  mCurrentPreset->setValue(name);
139 }
140 
141 FilePathPropertyPtr ElastixParameters::getExecutable()
142 {
143  QStringList paths;
145  {
147  paths = DataLocations::appendStringToAllElements(paths, "/"+this->getConfigUid());
148  }
149  else
150  {
151  paths << qApp->applicationDirPath();
152  }
153 
154  QDomElement root;
155  FilePathPropertyPtr retval;
156  retval = FilePathProperty::initialize("executable", "Executable",
157  "Name of registration executable",
158  "",
159  paths,
160  root);
162  return retval;
163 }
164 
165 FilePathPropertyPtr ElastixParameters::getParameterFile(QString uid)
166 {
167  QStringList paths = DataLocations::getRootConfigPaths();
168  paths = DataLocations::appendStringToAllElements(paths, "/"+this->getConfigUid());
169 
170  QDomElement root;
171  FilePathPropertyPtr retval;
172  retval = FilePathProperty::initialize("parameter"+uid, "Parameter"+uid,
173  "Name of parameter file "+uid,
174  "",
175  paths,
176  root);
178  return retval;
179 }
180 
181 bool ElastixParameters::validParameterFile(QString file) const
182 {
183  return QFileInfo(file).exists() && QFileInfo(file).isFile();
184 }
185 
187 {
188  QString p0 = mActiveParameterFile0->getEmbeddedPath().getAbsoluteFilepath();
189  QString p1 = mActiveParameterFile1->getEmbeddedPath().getAbsoluteFilepath();
190 
191  QStringList retval;
192  if (this->validParameterFile(p0))
193  retval << p0;
194  if (this->validParameterFile(p1))
195  retval << p1;
196  return retval;
197 }
198 
200 {
201  QString retval = QFileInfo(mActiveExecutable->getValue()).baseName();
202  QStringList parFiles = this->getActiveParameterFiles();
203  for (unsigned i=0; i<parFiles.size(); ++i)
204  retval += "/" + QFileInfo(parFiles[i]).baseName();
205  return retval;
206 }
207 
208 } /* namespace cx */
static QString getConfigUid()
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.
Namespace for all CustusX production code.