37IValidator_sptr createValidator(
unsigned int action,
const std::vector<std::string> &exts) {
50void addExtension(
const std::string &extension, std::vector<std::string> &extensions) {
51 if (std::find(extensions.begin(), extensions.end(), extension) != extensions.end())
54 extensions.emplace_back(extension);
62const std::string &getHomePath() {
63 static std::string homePath;
64 static bool initialised(
false);
71 char *home = std::getenv(
"HOME");
73 homePath = std::string(home);
77 char *userProfile = std::getenv(
"USERPROFILE");
79 homePath = userProfile ? std::string(userProfile) :
"";
93std::string expandUser(
const std::string &filepath) {
94 auto start = filepath.begin();
95 auto end = filepath.end();
98 if (start == end || *start !=
'~')
102 auto nextSlash = find_if(start, end, [](
const char &c) {
return c ==
'/' || c ==
'\\'; });
105 if (std::distance(start, nextSlash) != 1)
108 return getHomePath() + std::string(nextSlash, end);
116std::string createDirectory(
const std::string &path) {
117 Poco::Path stempath(path);
118 if (stempath.isFile()) {
119 stempath.makeParent();
122 if (!stempath.toString().empty()) {
123 Poco::File stem(stempath);
124 if (!stem.exists()) {
126 stem.createDirectories();
127 }
catch (Poco::Exception &e) {
128 std::stringstream msg;
129 msg <<
"Failed to create directory \"" << stempath.toString() <<
"\": " << e.what();
134 return "Invalid directory.";
154 const std::vector<std::string> &exts,
unsigned int direction)
156 m_defaultExt((!exts.empty()) ? exts.front() :
""), m_runFileProp(isLoadProperty() && extsMatchRunFiles()),
157 m_oldLoadPropValue(
""), m_oldLoadFoundFile(
"") {}
169 const std::string &ext,
unsigned int direction)
182 std::initializer_list<std::string> exts,
unsigned int direction)
221 if (strippedValue.empty()) {
227 strippedValue = expandUser(strippedValue);
232 if (Poco::Path(strippedValue).isAbsolute()) {
234 std::string
error = createDirectory(strippedValue);
242 std::string errorMsg;
261 const std::string &
value = (*this)();
276 return "No file specified.";
290 const std::vector<std::string> facilityExts = facilityInfo.
extensions();
291 const std::vector<std::string> allowedExts = this->
allowedValues();
292 match = std::any_of(allowedExts.cbegin(), allowedExts.cend(), [&facilityExts](
const auto &ext) {
293 return std::find(facilityExts.cbegin(), facilityExts.cend(), ext) != facilityExts.cend();
310 std::string foundFile;
319 if (foundFile.empty()) {
323 std::vector<std::string> exts;
328 addExtension(
lower, exts);
331 addExtension(
upper, exts);
333 for (
auto &ext : allowedExts) {
334 std::string
lower(ext);
335 std::string
upper(ext);
336 std::transform(ext.begin(), ext.end(),
lower.begin(), tolower);
337 std::transform(ext.begin(), ext.end(),
upper.begin(), toupper);
338 addExtension(ext, exts);
339 addExtension(
lower, exts);
340 addExtension(
upper, exts);
342 foundFile = FileFinder::Instance().findRun(propValue, exts).result();
345 foundFile = FileFinder::Instance().getFullPath(propValue);
352 if (foundFile.empty()) {
365 if (propValue.empty()) {
369 return "Empty filename not allowed.";
371 std::string errorMsg;
376 std::string save_path = ConfigService::Instance().getString(
"defaultsave.directory");
378 if (save_path.empty()) {
379 save_dir = Poco::Path(propValue).parent();
382 if (save_dir.toString().empty()) {
383 save_dir = Poco::Path::current();
386 save_dir = Poco::Path(save_path).makeDirectory();
388 errorMsg = createDirectory(save_dir.toString());
389 if (errorMsg.empty()) {
390 std::string fullpath = save_dir.resolve(propValue).toString();
double lower
lower and upper bounds on the multiplier, if known
A specialized class for dealing with file properties.
unsigned int m_action
The action type of this property, i.e. load/save.
bool isLoadProperty() const
Check if this is a load type property.
std::string setValue(const std::string &propValue) override
Overridden setValue method.
bool isOptional() const
Check if this property is optional.
bool isDirectoryProperty() const
Check if this is a directory type property.
std::string isEmptyValueValid() const
Returns a string depending on whether an empty value is valid.
std::string setLoadProperty(const std::string &propValue)
Handles the filename if this is a save property.
std::string m_oldLoadPropValue
Last value of propValue used in FileProperty::setLoadProperty.
std::string setSaveProperty(const std::string &propValue)
Handles the filename if this is a save property.
bool m_runFileProp
Is this property for run files?
bool isSaveProperty() const
Check if this is a save type property.
std::string m_oldLoadFoundFile
Last value of foundFile used in FileProperty::setLoadProperty.
FileProperty(const std::string &name, const std::string &defaultValue, unsigned int action, const std::vector< std::string > &exts=std::vector< std::string >(), unsigned int direction=Kernel::Direction::Input)
Constructor taking a list of extensions as a vector.
bool extsMatchRunFiles()
Do the allowed values match the facility preference extensions for run files.
std::string m_defaultExt
The default file extension associated with the type of file this property will handle.
std::string isValid() const override
Returns an empty string if the property is valid, otherwise contains an error message.
@ Directory
to specify a directory that must exist
@ OptionalSave
to specify a file to write to but an empty string is
@ OptionalLoad
to specify a file to read but the file doesn't have to exist
@ OptionalDirectory
to specify a directory that does not have to exist
@ Save
to specify a file to write to, the file may or may not exist
@ Load
allowed here which will be passed to the algorithm
Loads a workspace from a data file.
DirectoryValidator is a validator that checks that a directory path is valid.
Exception for when an item is not found in a collection.
A class that holds information about a facility.
const std::vector< std::string > & extensions() const
Returns a list of file extensions.
FileValidator is a validator that checks that a filepath is valid.
The concrete, templated class for properties.
std::string setValue(const std::string &value) override
Set the value of the property from a string representation.
std::vector< std::string > allowedValues() const override
Returns the set of valid values for this property, if such a set exists.
std::string value() const override
Get the value of the property as a string.
std::string isValid() const override
Check the value chosen for the property is OK, unless overidden it just calls the validator's isValid...
DLLExport QString addExtension(const QString &filename, const QString &selectedFilter)
For file dialogs.
MANTID_KERNEL_DLL std::string toLower(const std::string &input)
Converts string to all lowercase.
MANTID_KERNEL_DLL std::string strip(const std::string &A)
strip pre/post spaces
MANTID_KERNEL_DLL std::string toUpper(const std::string &input)
Converts string to all uppercase.
Mantid::Kernel::SingletonHolder< ConfigServiceImpl > ConfigService
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.