CustusX  15.3.4-beta
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 
42 namespace cx
43 {
44 
46 {
47  mOptions = options;
48 
49  mCurrentPreset = StringProperty::initialize("currentPreset", "Preset", "Current Elastix Preset", "Select Preset...", QStringList(), mOptions.getElement());
50  connect(mCurrentPreset.get(), SIGNAL(changed()), this, SLOT(currentPresetChangedSlot()));
51 
52  this->currentPresetChangedSlot();
53 }
54 
55 void ElastixParameters::addDefaultPresets()
56 {
57  QString defaultExecutable = cx::DataLocations::findConfigFilePath("run_elastix.sh", "/elastix/bin");
58  this->addDefaultPreset("elastix/p_Rigid", defaultExecutable, QStringList() << "p_Rigid.txt");
59 }
60 
61 void ElastixParameters::addDefaultPreset(QString name, QString executable, QStringList parameterFiles)
62 {
63  // ignore add if already present:
64  if (!mOptions.tryDescend("preset", "name", name).isNull())
65  return;
66 
67  XmlOptionFile node = mOptions.descend("preset", "name", name);
68  node.getElement().setAttribute("executable", executable);
69  for (unsigned i=0; i<parameterFiles.size(); ++i)
70  {
71  QString parName = QString("parameterFile%1").arg(i);
72  QString parVal = QFileInfo(parameterFiles[i]).fileName();
73  node.getElement().setAttribute(parName, parVal);
74  }
75 }
76 
77 void ElastixParameters::currentPresetChangedSlot()
78 {
79  this->reloadPresets();
80 
81  XmlOptionFile node = mOptions.descend("preset", "name", mCurrentPreset->getValue());
82  mActiveExecutable = node.getElement().attribute("executable");
83  mActiveParameterFile0 = this->getFullParameterFilename(node.getElement().attribute("parameterFile0"));
84  mActiveParameterFile1 = this->getFullParameterFilename(node.getElement().attribute("parameterFile1"));
86 }
87 
88 QString ElastixParameters::getFullParameterFilename(QString filename)
89 {
90  return DataLocations::findConfigFilePath(filename, "elastix/parameterFiles");
91 }
92 
94 {
95  QStringList folders = profile()->getAllRootConfigPaths();
96  for(int i=0; i<folders.size(); ++i)
97  folders[i] = folders[i] + "/elastix/parameterFiles";
98  return folders;
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);
136  node.getElement().setAttribute("parameterFile0", QFileInfo(mActiveParameterFile0).fileName());
137  node.getElement().setAttribute("parameterFile1", QFileInfo(mActiveParameterFile1).fileName());
138  mCurrentPreset->setValue(name);
139 }
140 
142 {
143  mActiveParameterFile0 = filename;
145 }
146 
148 {
149  if (this->validParameterFile(mActiveParameterFile0))
150  return mActiveParameterFile0;
151  return "";
152 }
153 
155 {
156  mActiveParameterFile1 = filename;
158 }
159 
161 {
162  if (this->validParameterFile(mActiveParameterFile1))
163  return mActiveParameterFile1;
164  return "";
165 }
166 
168 {
169  mActiveExecutable = filename;
171 }
172 
174 {
175  return mActiveExecutable;
176 }
177 
178 bool ElastixParameters::validParameterFile(QString file) const
179 {
180  return QFileInfo(file).exists() && QFileInfo(file).isFile();
181 }
182 
184 {
185  QStringList retval;
186  if (this->validParameterFile(mActiveParameterFile0))
187  retval << mActiveParameterFile0;
188  if (this->validParameterFile(mActiveParameterFile1))
189  retval << mActiveParameterFile1;
190  return retval;
191 }
192 
194 {
195  QString retval = QFileInfo(mActiveExecutable).baseName();
196  QStringList parFiles = this->getActiveParameterFiles();
197  for (unsigned i=0; i<parFiles.size(); ++i)
198  retval += "/" + QFileInfo(parFiles[i]).baseName();
199  return retval;
200 }
201 
202 } /* namespace cx */
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:142
QString getActiveParameterFile0() const
void saveCurrentPreset(QString newName)
void removeCurrentPreset()
Remove the currently selected preset. Reload.
void setActiveParameterFile1(QString filename)
QDomElement getElement()
return the current element
ElastixParameters(XmlOptionFile options)
void setActiveExecutable(QString filename)
QStringList getParameterFilesDir() const
QStringList getActiveParameterFiles() const
StringPropertyBasePtr getCurrentPreset()
static QString findConfigFilePath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath="")
QString getActiveExecutable() const
static StringPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList range, QDomNode root=QDomNode())
void deleteNode()
Delete the current node.
bool isNull() const
checks if this is null
XmlOptionFile tryDescend(QString element, QString attributeName, QString attributeValue) const
Helper class for xml files used to store ssc/cx data.
QString getActiveParameterFile1() const
void setActiveParameterFile0(QString filename)
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.