Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxFilePathProperty.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 "cxFilePathProperty.h"
34 
35 #include <iostream>
36 #include <QDomElement>
37 #include <QStringList>
38 #include <QDir>
39 #include "cxTypeConversions.h"
40 #include "cxLogger.h"
41 
42 namespace cx
43 {
44 
45 
47 {
48  mRoots << path;
49 }
50 
51 void EmbeddedFilepath::setFilepath(QString filename)
52 {
53  mFilePath = filename;
54 }
55 
56 void EmbeddedFilepath::evaluate(QString* foundRoot, bool* found, QString* foundRelative, QString* foundAbsolute) const
57 {
58  *foundRelative = mFilePath;
59  if (!mRoots.empty())
60  *foundRoot = mRoots.front();
61  *foundAbsolute = mFilePath;
62  *found = false;
63 
64  foreach (QString root, mRoots)
65  {
66  root = QDir::cleanPath(root);
67  if (!mFilePath.isEmpty() && QDir(root).exists(mFilePath))
68  {
69  *foundRelative = QDir(root).relativeFilePath(mFilePath);
70  *foundRoot = root;
71  *foundAbsolute = QDir(root).absoluteFilePath(mFilePath);
72  *found = true;
73 
74  if (foundRelative->contains(".."))
75  {
76  // dont use relative paths outside of the roots
77  *foundRelative = *foundAbsolute;
78  }
79  else
80  {
81  // if the current hit is inside the root, accept immediately
82  return;
83  }
84  }
85  }
86 
87 }
88 
90 {
91  bool found = false;
92  QString root, relative, absolute;
93  this->evaluate(&root, &found, &relative, &absolute);
94 
95  return relative;
96 }
97 
99 {
100  bool found = false;
101  QString root, relative, absolute;
102  this->evaluate(&root, &found, &relative, &absolute);
103 
104  return absolute;
105 }
106 
108 {
109  bool found = false;
110  QString root, relative, absolute;
111  this->evaluate(&root, &found, &relative, &absolute);
112 
113  return found;
114 }
115 
117 {
118  bool found = false;
119  QString root, relative, absolute;
120  this->evaluate(&root, &found, &relative, &absolute);
121 
122  return root;
123 }
124 
126 {
127  return mRoots;
128 }
129 
130 } // namespace cx
131 
132 
133 
134 
135 namespace cx
136 {
137 
138 FilePathProperty::FilePathProperty()
139 {
140 
141 }
142 
146 FilePathPropertyPtr FilePathProperty::initialize(const QString& uid, QString name, QString help, QString value, QStringList paths, QDomNode root)
147 {
149  retval->mUid = uid;
150  retval->mName = name.isEmpty() ? uid : name;
151  retval->mHelp = help;
152  retval->mFilePath.setFilepath(value);
153  foreach (QString path, paths)
154  retval->mFilePath.appendRootPath(path);
155  retval->mStore = XmlOptionItem(uid, root.toElement());
156  retval->mFilePath.setFilepath(retval->mStore.readValue(value));
157  return retval;
158 }
159 
161 {
162  return mName;
163 }
164 
166 {
167  return mUid;
168 }
169 
171 {
172  return mHelp;
173 }
174 
175 void FilePathProperty::setHelp(QString val)
176 {
177  if (val == mHelp)
178  return;
179 
180  mHelp = val;
181  emit changed();
182 }
183 
184 
186 {
187  return mFilePath.getRelativeFilepath();
188 }
189 
190 bool FilePathProperty::setValue(const QString& val)
191 {
192  if (val == this->getValue())
193  return false;
194 
195  mFilePath.setFilepath(val);
196  mStore.writeValue(mFilePath.getRelativeFilepath());
197  emit valueWasSet();
198  emit changed();
199  return true;
200 }
201 
203 {
204  return mFilePath;
205 }
206 
208 {
209  return this->getValue();
210 }
211 
213 {
214  this->setValue(val.toString());
215 }
216 
217 } // namespace cx
virtual QString getHelp() const
return a descriptive help string for the data, used for example as a tool tip.
QStringList getRootPaths() const
return the root of the existing root, first if no existing.
virtual void setHelp(QString val)
virtual QString getValue() const
get the data value.
QString getAbsoluteFilepath() const
return absolute filepath, select the existing root
void setFilepath(QString filename)
relative to one of the root paths or absolute
Helper class for storing one string value in an xml document.
virtual void setValueFromVariant(QVariant val)
void appendRootPath(QString path)
virtual QVariant getValueAsVariant() const
virtual QString getUid() const
void changed()
emit when the underlying data value is changed: The user interface will be updated.
void writeValue(const QString &val)
static FilePathPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList paths, QDomNode root=QDomNode())
QString getRelativeFilepath() const
return filepath relative to root.
boost::shared_ptr< class FilePathProperty > FilePathPropertyPtr
virtual QString getDisplayName() const
name of data entity. Used for display to user.
virtual bool setValue(const QString &value)
set the data value.
QString getRootPath() const
return the root of the existing root, first if no existing.
EmbeddedFilepath getEmbeddedPath()