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