11#include <boost/regex.hpp>
15const QString ALL_FILES(
"All Files (*)");
17QString getExtensionFromFilter(
const QString &selectedFilter) {
20 static const boost::regex FILE_EXT_REG_EXP{R
"(\*\.[[:word:]]+)"};
22 const auto filter = selectedFilter.toStdString();
23 if (boost::regex_search(filter, result, FILE_EXT_REG_EXP)) {
25 const std::string output = result.str(0);
26 extension = QString::fromStdString(output);
27 if (extension.startsWith(
"*"))
28 extension.remove(0, 1);
41 const QFileDialog::Options &options) {
44 const auto caption =
getCaption(
"Save file", baseProp);
46 QString selectedFilter;
49 const QString filename = QFileDialog::getSaveFileName(
54QString
addExtension(
const QString &filename,
const QString &selectedFilter) {
56 if (filename.isEmpty())
60 if (QFileInfo(filename).completeSuffix().isEmpty()) {
61 auto ext = getExtensionFromFilter(selectedFilter);
62 if (filename.endsWith(
".") && ext.startsWith(
".")) {
63 ext = ext.remove(0, 1);
65 return filename + ext;
84 return getFilter(singleProp->allowedValues());
96QString
getFilter(
const std::vector<std::string> &exts) {
101 if (exts.size() > 1) {
102 QString displayAllFilter =
"Data Files (";
103 for (
auto &itr : exts) {
105 displayAllFilter.append(
" ");
108 displayAllFilter.append(
" );;");
109 filter.append(displayAllFilter);
113 for (
auto &itr : exts) {
114 filter.append(QString::fromStdString(itr) +
" (*" + QString::fromStdString(itr) +
");;");
116 filter = filter.trimmed();
118 filter.append(ALL_FILES);
128 QString formattedExtension = QString::fromStdString(extension);
129 if (extension.empty()) {
130 return formattedExtension;
132 if (extension.at(0) ==
'*' && extension.at(1) ==
'.') {
133 return formattedExtension;
135 if (extension.at(0) ==
'*') {
136 formattedExtension.insert(1,
".");
137 }
else if (extension.at(0) ==
'.') {
138 formattedExtension.prepend(
"*");
140 formattedExtension.prepend(
"*.");
143 return formattedExtension;
148 auto dialogTitle = QString::fromStdString(dialogName);
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));
A specialized class for dealing with file properties.
A property to allow a user to specify multiple files to load.
Base class for properties.
const std::string & name() const
Get the property's name.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
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.