Fraxinus  18.10
An IGT application
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) 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 #include "cxFilePathProperty.h"
13 
14 #include <iostream>
15 #include <QDomElement>
16 #include <QStringList>
17 #include <QDir>
18 #include "cxTypeConversions.h"
19 #include "cxLogger.h"
20 
21 namespace cx
22 {
23 
24 
26 {
27  mRoots << path;
28 }
29 
30 void EmbeddedFilepath::setFilepath(QString filename)
31 {
32  mFilePath = filename;
33 }
34 
35 void EmbeddedFilepath::evaluate(QString* foundRoot, bool* found, QString* foundRelative, QString* foundAbsolute) const
36 {
37  *foundRelative = mFilePath;
38  if (!mRoots.empty())
39  *foundRoot = mRoots.front();
40  *foundAbsolute = mFilePath;
41  *found = false;
42 
43  foreach (QString root, mRoots)
44  {
45  root = QDir::cleanPath(root);
46  if (!mFilePath.isEmpty() && QDir(root).exists(mFilePath))
47  {
48  *foundRelative = QDir(root).relativeFilePath(mFilePath);
49  *foundRoot = root;
50  *foundAbsolute = QDir(root).absoluteFilePath(mFilePath);
51  *found = true;
52 
53  if (foundRelative->contains(".."))
54  {
55  // dont use relative paths outside of the roots
56  *foundRelative = *foundAbsolute;
57  }
58  else
59  {
60  // if the current hit is inside the root, accept immediately
61  return;
62  }
63  }
64  }
65 
66 }
67 
69 {
70  bool found = false;
71  QString root, relative, absolute;
72  this->evaluate(&root, &found, &relative, &absolute);
73 
74  return relative;
75 }
76 
78 {
79  bool found = false;
80  QString root, relative, absolute;
81  this->evaluate(&root, &found, &relative, &absolute);
82 
83  return absolute;
84 }
85 
87 {
88  bool found = false;
89  QString root, relative, absolute;
90  this->evaluate(&root, &found, &relative, &absolute);
91 
92  return found;
93 }
94 
96 {
97  bool found = false;
98  QString root, relative, absolute;
99  this->evaluate(&root, &found, &relative, &absolute);
100 
101  return root;
102 }
103 
105 {
106  return mRoots;
107 }
108 
109 } // namespace cx
110 
111 
112 
113 
114 namespace cx
115 {
116 
117 FilePathProperty::FilePathProperty()
118 {
119 
120 }
121 
125 FilePathPropertyPtr FilePathProperty::initialize(const QString& uid, QString name, QString help, QString value, QStringList paths, QDomNode root)
126 {
128  retval->mUid = uid;
129  retval->mName = name.isEmpty() ? uid : name;
130  retval->mHelp = help;
131  retval->mFilePath.setFilepath(value);
132  foreach (QString path, paths)
133  retval->mFilePath.appendRootPath(path);
134  retval->mStore = XmlOptionItem(uid, root.toElement());
135  retval->mFilePath.setFilepath(retval->mStore.readValue(value));
136  return retval;
137 }
138 
140 {
141  return mName;
142 }
143 
145 {
146  return mUid;
147 }
148 
150 {
151  return mHelp;
152 }
153 
154 void FilePathProperty::setHelp(QString val)
155 {
156  if (val == mHelp)
157  return;
158 
159  mHelp = val;
160  emit changed();
161 }
162 
163 
165 {
166  return mFilePath.getRelativeFilepath();
167 }
168 
169 bool FilePathProperty::setValue(const QString& val)
170 {
171  if (val == this->getValue())
172  return false;
173 
174  mFilePath.setFilepath(val);
175  mStore.writeValue(mFilePath.getRelativeFilepath());
176  emit valueWasSet();
177  emit changed();
178  return true;
179 }
180 
182 {
183  return mFilePath;
184 }
185 
187 {
188  return this->getValue();
189 }
190 
192 {
193  this->setValue(val.toString());
194 }
195 
196 } // 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
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()
Namespace for all CustusX production code.