Mantid
Loading...
Searching...
No Matches
FilePropertyWidget.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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 +
12
13using namespace Mantid::Kernel;
14
15namespace MantidQt::API {
16
17//----------------------------------------------------------------------------------------------
20FilePropertyWidget::FilePropertyWidget(Mantid::Kernel::Property *prop, QWidget *parent, QGridLayout *layout, int row)
21 : TextPropertyWidget(prop, parent, layout, row) {
22 m_fileProp = dynamic_cast<Mantid::API::FileProperty *>(prop);
24
25 // Create a browse button
26 m_browseButton = new QPushButton(tr("Browse"), m_parent);
27 // Make current value visible
28 this->setValue(QString::fromStdString(m_prop->value()));
29 // Make sure the connection comes after updating any values
30 connect(m_browseButton, SIGNAL(clicked()), this, SLOT(browseClicked()));
31 m_widgets.push_back(m_browseButton);
32
33 // Add to the 2nd column
34 m_gridLayout->addWidget(m_browseButton, m_row, 2);
35}
36
37//----------------------------------------------------------------------------------------------
41
42//----------------------------------------------------------------------------------------------
45 // Open dialog to get the filename
46 QString filename;
47 if (m_fileProp) {
48 filename = openFileDialog(m_prop);
49 } else if (m_multipleFileProp) {
50 // Current filename text
51 filename = m_textbox->text();
52
53 // Adjust the starting directory from the current file
54 if (!filename.isEmpty()) {
55 QStringList files = filename.split(",");
56 if (files.size() > 0) {
57 QString firstFile = files[0];
58 AlgorithmInputHistory::Instance().setPreviousDirectory(QFileInfo(firstFile).absoluteDir().path());
59 }
60 }
61
62 // Open multiple files in the dialog
64
65 // Make into comma-sep string
66 filename.clear();
67 QStringList list = files;
68 QStringList::Iterator it = list.begin();
69 while (it != list.end()) {
70 if (it != list.begin())
71 filename += ",";
72 filename += *it;
73 it++;
74 }
75 }
76
77 // TODO: set the value.
78 if (!filename.isEmpty()) {
79 m_textbox->clear();
80 m_textbox->setText(filename);
82 }
83}
84
85//----------------------------------------------------------------------------------------------
92 auto *prop = dynamic_cast<Mantid::API::FileProperty *>(baseProp);
93 if (!prop)
94 return "";
95
96 /* MG 20/07/09: Static functions such as these that use native Windows and MAC
97 dialogs
98 in those environments are alot faster. This is unforunately at the expense
99 of
100 shell-like pattern matching, i.e. [0-9].
101 */
102 QString filename;
103 if (prop->isLoadProperty()) {
104 const auto filter = FileDialogHandler::getFilter(baseProp);
105 const auto caption = FileDialogHandler::getCaption("Open file", baseProp);
106 filename = QFileDialog::getOpenFileName(nullptr, caption, AlgorithmInputHistory::Instance().getPreviousDirectory(),
107 filter);
108 } else if (prop->isSaveProperty()) {
109 filename = FileDialogHandler::getSaveFileName(nullptr, prop);
110 } else if (prop->isDirectoryProperty()) {
111 const auto caption = FileDialogHandler::getCaption("Choose a Directory", baseProp);
112 filename =
113 QFileDialog::getExistingDirectory(nullptr, caption, AlgorithmInputHistory::Instance().getPreviousDirectory());
114 } else {
115 throw std::runtime_error("Invalid type of file property! This should not happen.");
116 }
117
118 if (!filename.isEmpty()) {
119 AlgorithmInputHistory::Instance().setPreviousDirectory(QFileInfo(filename).absoluteDir().path());
120 }
121 return filename;
122}
123
124//-------------------------------------------------------------------------------------------------
132 if (!baseProp)
133 return QStringList();
134 auto *prop = dynamic_cast<Mantid::API::MultipleFileProperty *>(baseProp);
135 if (!prop)
136 return QStringList();
137
138 const auto filter = FileDialogHandler::getFilter(baseProp);
139 QStringList files = QFileDialog::getOpenFileNames(nullptr, "Open Multiple Files",
140 AlgorithmInputHistory::Instance().getPreviousDirectory(), filter);
141
142 return files;
143}
144
145} // namespace MantidQt::API
Mantid::API::MultipleFileProperty * m_multipleFileProp
Is a multiple file property.
Mantid::API::FileProperty * m_fileProp
Is a file property.
static QStringList openMultipleFileDialog(Mantid::Kernel::Property *baseProp)
Open a file selection box to select Multiple files to load.
QPushButton * m_browseButton
"Browse" button
FilePropertyWidget(Mantid::Kernel::Property *prop, QWidget *parent=nullptr, QGridLayout *layout=nullptr, int row=-1)
Constructor.
void browseClicked()
Slot called when the browse button is clicked.
~FilePropertyWidget() override
Destructor.
static QString openFileDialog(Mantid::Kernel::Property *baseProp)
Open the file dialog for a given property.
void setValue(const QString &value)
Set the value of the property given into the GUI state.
QGridLayout * m_gridLayout
Grid layout of the dialog to which we are adding widgets.
QVector< QWidget * > m_widgets
All contained widgets.
QWidget * m_parent
Parent widget to add sub-widgets to.
void userEditedProperty()
To be called when a user edits a property, as opposed to one being set programmatically.
int m_row
If using the GridLayout, this is the row where the widget was inserted.
Mantid::Kernel::Property * m_prop
Property being looked at. This is NOT owned by the widget.
The most generic widgets for Property's that are only a simple string.
QLineEdit * m_textbox
The text box to edit.
A specialized class for dealing with file properties.
Definition: FileProperty.h:42
A property to allow a user to specify multiple files to load.
Base class for properties.
Definition: Property.h:94
virtual std::string value() const =0
Returns the value of the property as a string.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
DLLExport QString getCaption(const std::string &dialogName, const Mantid::Kernel::Property *prop)
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.