NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxTSFPresets.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 "cxTSFPresets.h"
34 
35 #include <iostream>
36 #include <QDir>
37 #include <QTextStream>
38 #include "cxLogger.h"
39 #include "cxDataLocations.h"
40 #include "tsf-config.h"
41 
42 namespace cx
43 {
44 
46  Presets(XmlOptionFile("TSFPresets"), XmlOptionFile("Custom"))
47 {
48  mPresetPath = cx::DataLocations::findConfigFolder("/tsf", QString(KERNELS_DIR)) + "/parameters";
49  this->loadPresetsFromFiles();
50 }
51 
53 {}
54 
55 QDomElement TSFPresets::createPresetElement(QString name, std::map<QString, QString>& parameters)
56 {
57  QStringList ignoreParametersWithName;
58  ignoreParametersWithName << "centerline-vtk-file";
59  ignoreParametersWithName << "storage-dir";
60 
61  QDomDocument doc;
62  QDomElement retval = doc.createElement("Preset");
63  retval.setAttribute("name", name);
64  std::map<QString, QString>::iterator it;
65  for (it = parameters.begin(); it != parameters.end(); ++it)
66  {
67  if (!ignoreParametersWithName.contains(it->first))
68  retval.setAttribute(it->first, it->second);
69  }
70  return retval;
71 }
72 
74 {
75  QDomDocument doc = this->getCustomFile().getDocument();
76  QDomNodeList presetNodes = doc.elementsByTagName("Preset");
77  for (int i = 0; i < presetNodes.count(); ++i)
78  {
79  QDomNode node = presetNodes.at(i);
80  if (!node.isElement())
81  break;
82  QDomElement element = node.toElement();
83 
84  QString folderPath;
85  folderPath = mPresetPath + "/centerline-gpu/";
86  std::map<QString, QString> parameters;
87  QDomNamedNodeMap attributes = element.attributes();
88  for (int j = 0; j < attributes.count(); ++j)
89  {
90  QDomNode attribute = attributes.item(j);
91  if (attribute.isNull())
92  continue;
93  if (attribute.nodeName() != "name")
94  parameters[attribute.nodeName()] = attribute.nodeValue();
95  }
96  if (mLastCustomPresetAdded == element.attribute("name")) //Save only current preset
97  this->saveFile(folderPath, parameters);
98  }
99 }
100 
102 {
103  QString folderPath = mPresetPath + "/centerline-gpu/" + mLastCustomPresetRemoved;
104  this->deleteFile(folderPath);
105 }
106 
107 QStringList TSFPresets::generatePresetList(QString tag)
108 {
109  this->getPresetsNameAndPath();
110  QStringList retval;
111  std::map<QString, QString>::iterator it;
112  for (it = mPresetsMap.begin(); it != mPresetsMap.end(); ++it)
113  {
114  retval << it->first;
115  }
116  return retval;
117 }
118 
120 {
121  this->getPresetsNameAndPath();
123 }
124 
125 void TSFPresets::convertToInternalFormat(std::map<QString,QString>& presets)
126 {
127  std::map<QString,QString>::iterator it;
128  for (it = presets.begin();it != presets.end();++it)
129  this->addAsCustomPreset(it);
130 
131 }
132 
133 std::map<QString, QString> TSFPresets::readFile(QString& filePath)
134 {
135  std::map<QString, QString> retval;
136  if (!QFile::exists(filePath))
137  {
138  reportError("File does not exists: " + filePath);
139  return retval;
140  }
141  QFile file(filePath);
142  if (file.open(QFile::ReadOnly))
143  {
144  QTextStream in(&file);
145  while (!in.atEnd())
146  {
147  QString line = in.readLine();
148  QStringList lineItems = line.split(" ");
149  if (lineItems.count() == 2)
150  retval[lineItems.at(0)] = lineItems.at(1);
151  }
152  }
153 
154  return retval;
155 }
156 
157 void TSFPresets::saveFile(QString folderpath, std::map<QString, QString> parameters)
158 {
159  QFile file(folderpath + mLastCustomPresetAdded);
160  QString customPresetName = QFileInfo(file).fileName();
161  QTextStream outPresetFile;
162  if (!file.open(QFile::WriteOnly))
163  {
164  reportError("Could not open the file " + file.fileName() + " for writing.");
165  return;
166  }
167  outPresetFile.setDevice(&file);
168  std::map<QString, QString>::iterator it;
169  for (it = parameters.begin(); it != parameters.end(); ++it)
170  {
171  QString line = it->first + " " + it->second;
172  outPresetFile << line << "\n";
173  }
174  outPresetFile << flush;
175  file.close();
176 }
177 
178 void TSFPresets::deleteFile(QString filePath)
179 {
180  QFile file(filePath);
181  QString customPresetName = QFileInfo(file).fileName();
182  if (!file.remove())
183  reportError("File: " + filePath + " not removed...");
184 }
185 
187 {
188  mPresetsMap.clear();
189  QDir parametersDir(mPresetPath + "/centerline-gpu");
190  if (!parametersDir.exists())
191  reportError("Preset directory "+parametersDir.path()+" not found.");
192 
193  QFileInfoList fileInfoList = parametersDir.entryInfoList(QDir::Files);
194  foreach(QFileInfo info, fileInfoList){
195  QString name = info.baseName();
196  mPresetsMap[name] = info.absoluteFilePath();
197  }
198 }
199 
200 void TSFPresets::addAsCustomPreset(std::map<QString,QString>::iterator it)
201 {
202  std::map<QString,QString> params = this->readFile(it->second);
203  QDomElement preset = TSFPresets::createPresetElement(it->first, params);
204  Presets::addCustomPreset(preset);
205 }
206 
207 } /* namespace cx */
208 
std::map< QString, QString > mPresetsMap
Definition: cxTSFPresets.h:77
void convertToInternalFormat(std::map< QString, QString > &presets)
void reportError(QString msg)
Definition: cxLogger.cpp:92
void loadPresetsFromFiles()
Base class for a group of presets in the system.
Definition: cxPresets.h:61
virtual ~TSFPresets()
void getPresetsNameAndPath()
XmlOptionFile getCustomFile()
Definition: cxPresets.cpp:94
static QString findConfigFolder(QString pathRelativeToConfigRoot, QString alternativeAbsolutePath="")
virtual void save()
Saves last custom preset (current)
void saveFile(QString folderPath, std::map< QString, QString > parameters)
QString mLastCustomPresetRemoved
< the name of the last custom preset added
Definition: cxPresets.h:93
void addCustomPreset(QDomElement &element)
adds a custom preset
Definition: cxPresets.cpp:58
virtual void remove()
removes the presets from file
static QDomElement createPresetElement(QString name, std::map< QString, QString > &parameters)
void deleteFile(QString filePath)
QDomDocument getDocument()
returns the document
QString mLastCustomPresetAdded
Definition: cxPresets.h:92
std::map< QString, QString > readFile(QString &filePath)
void addAsCustomPreset(std::map< QString, QString >::iterator it)
virtual QStringList generatePresetList(QString tag)
internally generate the preset list
QString mPresetPath
Definition: cxTSFPresets.h:76
Helper class for xml files used to store ssc/cx data.