Fraxinus  18.10
An IGT application
cxPresets.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 
13 #include "cxPresets.h"
14 
15 #include <iostream>
16 #include <QStringList>
17 #include <QTextStream>
18 
19 
20 namespace cx {
21 
23 {
24  mPresetFile = presetFile;
25  mCustomFile = customFile;
27  mId = mPresetFile.getFileName();
28 }
29 
30 void Presets::deleteCustomPreset(QString name)
31 {
33  XmlOptionFile node = this->getPresetNode(name);
34  node.deleteNode();
35 }
36 
37 void Presets::addCustomPreset(QDomElement& element)
38 {
39  mLastCustomPresetAdded = element.attribute("name");
40  this->addPreset(mCustomFile, element);
41 }
42 
44 {
45  this->getCustomFile().save();
46 }
47 
49 {
50  //TODO
51  std::cout << "TODO IMPLEMENT: void Presets::remove()" << std::endl;
52 }
53 
54 QStringList Presets::getPresetList(QString tag)
55 {
56  return this->generatePresetList(tag);
57 }
58 
59 bool Presets::isDefaultPreset(QString presetName)
60 {
61 
62  XmlOptionFile testval = mPresetFile.tryDescend("Preset", "name", presetName);
63  if (!testval.isNull())
64  return true;
65  return false;
66 }
67 
68 QStringList Presets::generatePresetList(QString tag)
69 {
70  return QStringList();
71 }
72 
74 {
75  return mCustomFile;
76 }
77 
78 XmlOptionFile Presets::getPresetNode(const QString& presetName)
79 {
80  XmlOptionFile retval = mPresetFile;
81  retval = retval.tryDescend("Preset", "name", presetName);
82  if (!retval.getDocument().isNull())
83  return retval;
84 
85  retval = this->getCustomFile();
86  retval = retval.descend("Preset", "name", presetName);
87  return retval;
88 }
89 
90 void Presets::addDefaultPreset(QDomElement& element)
91 {
92  this->addPreset(mPresetFile, element);
93 }
94 
95 void Presets::addPreset(XmlOptionFile& file, QDomElement& element)
96 {
97  QString presetName = element.attribute("name");
98  if(presetName.isEmpty())
99  return;
100 
101  file = file.root();
102  file = file.descend("Presets");
103  file.getElement().appendChild(element);
104 
105  //TODO what if preset with name already exists?
106 }
107 
108 
109 void Presets::print(QDomElement element)
110 {
111  QTextStream stream(stdout);
112  stream << "\n";
113  element.save(stream, 4);
114  stream << "\n";
115 }
116 
117 QString Presets::getId() const
118 {
119  return mId;
120 }
121 
122 
123 } /* namespace cx */
virtual void save()
saves the presets to file
Definition: cxPresets.cpp:43
Presets(XmlOptionFile presetFile, XmlOptionFile customFile)
Definition: cxPresets.cpp:22
QDomElement getElement()
return the current element
XmlOptionFile mPresetFile
< the name of the last custom preset removed
Definition: cxPresets.h:74
virtual void deleteCustomPreset(QString name)
deletes the custom preset with the given name if it exists
Definition: cxPresets.cpp:30
XmlOptionFile getCustomFile()
Definition: cxPresets.cpp:73
QString mLastCustomPresetRemoved
< the name of the last custom preset added
Definition: cxPresets.h:72
QStringList getPresetList(QString tag="")
returns a list of the preset names for the given tag
Definition: cxPresets.cpp:54
void addCustomPreset(QDomElement &element)
adds a custom preset
Definition: cxPresets.cpp:37
virtual QStringList generatePresetList(QString tag)
internally generate the preset list
Definition: cxPresets.cpp:68
bool isDefaultPreset(QString presetName)
Check is the preset is one of the "system presets".
Definition: cxPresets.cpp:59
QDomDocument getDocument()
returns the document
QString mLastCustomPresetAdded
Definition: cxPresets.h:71
void deleteNode()
Delete the current node.
void save()
save entire document.
void addDefaultPreset(QDomElement &element)
Definition: cxPresets.cpp:90
bool isNull() const
checks if this is null
void addPreset(XmlOptionFile &file, QDomElement &element)
Definition: cxPresets.cpp:95
XmlOptionFile getPresetNode(const QString &presetName)
Look for a preset with the given name. Create one if not found.
Definition: cxPresets.cpp:78
XmlOptionFile root() const
set the current element to root
virtual void remove()
removes the presets from file
Definition: cxPresets.cpp:48
void print(QDomElement element)
debugging function for printing xml elements
Definition: cxPresets.cpp:109
QString getId() const
Definition: cxPresets.cpp:117
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
Namespace for all CustusX production code.