CustusX  15.8
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 
34 #include "cxPresets.h"
35 
36 #include <iostream>
37 #include <QStringList>
38 #include <QTextStream>
39 
40 
41 namespace cx {
42 
44 {
45  mPresetFile = presetFile;
46  mCustomFile = customFile;
48 }
49 
50 void Presets::deleteCustomPreset(QString name)
51 {
53  XmlOptionFile node = this->getPresetNode(name);
54  node.deleteNode();
55 }
56 
57 void Presets::addCustomPreset(QDomElement& element)
58 {
59  mLastCustomPresetAdded = element.attribute("name");
60  this->addPreset(mCustomFile, element);
61 }
62 
64 {
65  this->getCustomFile().save();
66 }
67 
69 {
70  //TODO
71  std::cout << "TODO IMPLEMENT: void Presets::remove()" << std::endl;
72 }
73 
74 QStringList Presets::getPresetList(QString tag)
75 {
76  return this->generatePresetList(tag);
77 }
78 
79 bool Presets::isDefaultPreset(QString presetName)
80 {
81 
82  XmlOptionFile testval = mPresetFile.tryDescend("Preset", "name", presetName);
83  if (!testval.isNull())
84  return true;
85  return false;
86 }
87 
88 QStringList Presets::generatePresetList(QString tag)
89 {
90  return QStringList();
91 }
92 
94 {
95  return mCustomFile;
96 }
97 
98 XmlOptionFile Presets::getPresetNode(const QString& presetName)
99 {
100  XmlOptionFile retval = mPresetFile;
101  retval = retval.tryDescend("Preset", "name", presetName);
102  if (!retval.getDocument().isNull())
103  return retval;
104 
105  retval = this->getCustomFile();
106  retval = retval.descend("Preset", "name", presetName);
107  return retval;
108 }
109 
110 void Presets::addDefaultPreset(QDomElement& element)
111 {
112  this->addPreset(mPresetFile, element);
113 }
114 
115 void Presets::addPreset(XmlOptionFile& file, QDomElement& element)
116 {
117  QString presetName = element.attribute("name");
118  if(presetName.isEmpty())
119  return;
120 
121  file = file.root();
122  file = file.descend("Presets");
123  file.getElement().appendChild(element);
124 
125  //TODO what if preset with name already exists?
126 }
127 
128 
129 void Presets::print(QDomElement element)
130 {
131  QTextStream stream(stdout);
132  stream << "\n";
133  element.save(stream, 4);
134  stream << "\n";
135 }
136 
137 
138 } /* namespace cx */
virtual void save()
saves the presets to file
Definition: cxPresets.cpp:63
Presets(XmlOptionFile presetFile, XmlOptionFile customFile)
Definition: cxPresets.cpp:43
QDomElement getElement()
return the current element
XmlOptionFile mPresetFile
< the name of the last custom preset removed
Definition: cxPresets.h:93
virtual void deleteCustomPreset(QString name)
deletes the custom preset with the given name if it exists
Definition: cxPresets.cpp:50
XmlOptionFile getCustomFile()
Definition: cxPresets.cpp:93
QString mLastCustomPresetRemoved
< the name of the last custom preset added
Definition: cxPresets.h:91
QStringList getPresetList(QString tag="")
returns a list of the preset names for the given tag
Definition: cxPresets.cpp:74
void addCustomPreset(QDomElement &element)
adds a custom preset
Definition: cxPresets.cpp:57
virtual QStringList generatePresetList(QString tag)
internally generate the preset list
Definition: cxPresets.cpp:88
bool isDefaultPreset(QString presetName)
Check is the preset is one of the "system presets".
Definition: cxPresets.cpp:79
QDomDocument getDocument()
returns the document
QString mLastCustomPresetAdded
Definition: cxPresets.h:90
void deleteNode()
Delete the current node.
void save()
save entire document.
void addDefaultPreset(QDomElement &element)
Definition: cxPresets.cpp:110
bool isNull() const
checks if this is null
void addPreset(XmlOptionFile &file, QDomElement &element)
Definition: cxPresets.cpp:115
XmlOptionFile getPresetNode(const QString &presetName)
Look for a preset with the given name. Create one if not found.
Definition: cxPresets.cpp:98
XmlOptionFile root() const
set the current element to root
virtual void remove()
removes the presets from file
Definition: cxPresets.cpp:68
void print(QDomElement element)
debugging function for printing xml elements
Definition: cxPresets.cpp:129
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