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