Mantid
Loading...
Searching...
No Matches
FileDialogHandler.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2009 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
11#include <boost/regex.hpp>
12#include <sstream>
13
14namespace { // anonymous namespace
15const QString ALL_FILES("All Files (*)");
16
17QString getExtensionFromFilter(const QString &selectedFilter) {
18 QString extension;
19 // search for single extension
20 static const boost::regex FILE_EXT_REG_EXP{R"(\*\.[[:word:]]+)"};
21 boost::smatch result;
22 const auto filter = selectedFilter.toStdString();
23 if (boost::regex_search(filter, result, FILE_EXT_REG_EXP)) {
24 // clang fails to cast result[1] to std::string.
25 const std::string output = result.str(0);
26 extension = QString::fromStdString(output);
27 if (extension.startsWith("*"))
28 extension.remove(0, 1);
29 }
30 return extension;
31}
32
33} // anonymous namespace
34
40QString getSaveFileName(QWidget *parent, const Mantid::Kernel::Property *baseProp,
41 const QFileDialog::Options &options) {
42 // set up filters and dialog title
43 const auto filter = getFilter(baseProp);
44 const auto caption = getCaption("Save file", baseProp);
45
46 QString selectedFilter;
47
48 // create the file browser
49 const QString filename = QFileDialog::getSaveFileName(
50 parent, caption, AlgorithmInputHistory::Instance().getPreviousDirectory(), filter, &selectedFilter, options);
51 return addExtension(filename, selectedFilter);
52}
53
54QString addExtension(const QString &filename, const QString &selectedFilter) {
55 // just return an empty string if that is what was given
56 if (filename.isEmpty())
57 return filename;
58
59 // Check the filename and append the selected filter if necessary
60 if (QFileInfo(filename).completeSuffix().isEmpty()) {
61 auto ext = getExtensionFromFilter(selectedFilter);
62 if (filename.endsWith(".") && ext.startsWith(".")) {
63 ext = ext.remove(0, 1);
64 }
65 return filename + ext;
66 } else {
67 return filename;
68 }
69}
70
71QString getFilter(const Mantid::Kernel::Property *baseProp) {
72 if (!baseProp)
73 return ALL_FILES;
74
75 // multiple file version
76 const auto *multiProp = dynamic_cast<const Mantid::API::MultipleFileProperty *>(baseProp);
77 if (multiProp)
78 return getFilter(multiProp->getExts());
79
80 // regular file version
81 const auto *singleProp = dynamic_cast<const Mantid::API::FileProperty *>(baseProp);
82 // The allowed values in this context are file extensions
83 if (singleProp)
84 return getFilter(singleProp->allowedValues());
85
86 // otherwise only the all files exists
87 return ALL_FILES;
88}
89
96QString getFilter(const std::vector<std::string> &exts) {
97 QString filter("");
98
99 if (!exts.empty()) {
100 // Generate the display all filter
101 if (exts.size() > 1) {
102 QString displayAllFilter = "Data Files (";
103 for (auto const &itr : exts) {
104 // Add a space to between each extension
105 displayAllFilter.append(" ");
106 displayAllFilter.append(formatExtension(itr));
107 }
108 displayAllFilter.append(" );;");
109 filter.append(displayAllFilter);
110 }
111
112 // Append individual file filters
113 for (auto const &itr : exts) {
114 filter.append(QString::fromStdString(itr) + " (*" + QString::fromStdString(itr) + ");;");
115 }
116 filter = filter.trimmed();
117 }
118 filter.append(ALL_FILES);
119 return filter;
120}
121
127QString formatExtension(const std::string &extension) {
128 QString formattedExtension = QString::fromStdString(extension);
129 if (extension.empty()) {
130 return formattedExtension;
131 }
132 if (extension.at(0) == '*' && extension.at(1) == '.') {
133 return formattedExtension;
134 } else {
135 if (extension.at(0) == '*') {
136 formattedExtension.insert(1, ".");
137 } else if (extension.at(0) == '.') {
138 formattedExtension.prepend("*");
139 } else {
140 formattedExtension.prepend("*.");
141 }
142 }
143 return formattedExtension;
144}
145
146QString getCaption(const std::string &dialogName, const Mantid::Kernel::Property *prop) {
147 // generate the dialog title
148 auto dialogTitle = QString::fromStdString(dialogName);
149 if (bool(prop)) {
150 const auto &name = prop->name();
151 if (name != "Filename" && prop->name() != "Directory" && prop->name() != "Dir") {
152 dialogTitle.append(" - ");
153 dialogTitle.append(QString::fromStdString(name));
154 }
155 }
156 return dialogTitle;
157}
158} // namespace MantidQt::API::FileDialogHandler
std::string name
Definition Run.cpp:60
A specialized class for dealing with file properties.
A property to allow a user to specify multiple files to load.
Base class for properties.
Definition Property.h:94
const std::string & name() const
Get the property's name.
Definition Property.cpp:61
Contains modifications to Qt functions where problems have been found on certain operating systems.
DLLExport QString getCaption(const std::string &dialogName, const Mantid::Kernel::Property *prop)
DLLExport QString formatExtension(const std::string &extension)
Format extension into expected form (*.ext)
DLLExport QString addExtension(const QString &filename, const QString &selectedFilter)
For file dialogs.
DLLExport QString getFilter(const Mantid::Kernel::Property *baseProp)
DLLExport QString getSaveFileName(QWidget *parent=nullptr, const Mantid::Kernel::Property *baseProp=nullptr, const QFileDialog::Options &options=QFileDialog::Options())
Contains modifications to Qt functions where problems have been found on certain operating systems.