Mantid
Loading...
Searching...
No Matches
DataSelector.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 +
11#include "MantidAPI/Workspace.h"
13
14#include <QFileInfo>
15
16#include <QDebug>
17#include <QDropEvent>
18#include <QMimeData>
19#include <QUrl>
20
21using namespace Mantid::API;
22
23namespace {
24
25bool doesExistInADS(std::string const &workspaceName) {
26 return AnalysisDataService::Instance().doesExist(workspaceName);
27}
28
29std::string cutLastOf(const std::string &str, const std::string &delimiter) {
30 const auto cutIndex = str.rfind(delimiter);
31 if (cutIndex != std::string::npos)
32 return str.substr(0, cutIndex);
33 return str;
34}
35
36std::string extractLastOf(const std::string &str, const std::string &delimiter) {
37 const auto cutIndex = str.rfind(delimiter);
38 if (cutIndex != std::string::npos)
39 return str.substr(cutIndex + 1, str.size() - cutIndex);
40 return str;
41}
42
43bool fileFound(std::string const &file) { return !FileFinder::Instance().getFullPath(file).empty(); }
44
45std::string loadAlgName(const std::string &filePath) {
46 const auto suffix = extractLastOf(filePath, ".");
47 return suffix == "dave" ? "LoadDaveGrp" : "Load";
48}
49
50void loadFile(std::string const &filename, std::string const &workspaceName) {
51 auto const loadAlg = AlgorithmManager::Instance().createUnmanaged(loadAlgName(filename));
52 loadAlg->initialize();
53 loadAlg->setProperty("Filename", filename);
54 loadAlg->setProperty("OutputWorkspace", workspaceName);
55 loadAlg->execute();
56}
57
58} // namespace
59
61
63 : API::MantidWidget(parent), m_algRunner(), m_autoLoad(true), m_showLoad(true) {
64 m_uiForm.setupUi(this);
65 connect(m_uiForm.cbInputType, SIGNAL(currentIndexChanged(int)), this, SLOT(handleViewChanged(int)));
66 connect(m_uiForm.pbLoadFile, SIGNAL(clicked()), this, SIGNAL(loadClicked()));
67
68 // data selected changes
69 connect(m_uiForm.rfFileInput, SIGNAL(filesFoundChanged()), this, SLOT(handleFileInput()));
70 connect(m_uiForm.wsWorkspaceInput, SIGNAL(currentIndexChanged(int)), this, SLOT(handleWorkspaceInput()));
71 connect(m_uiForm.pbLoadFile, SIGNAL(clicked()), this, SLOT(handleFileInput()));
72
73 connect(&m_algRunner, SIGNAL(algorithmComplete(bool)), this, SLOT(handleAutoLoadComplete(bool)));
74 this->setAcceptDrops(true);
75 m_uiForm.rfFileInput->setAcceptDrops(false);
76}
77
79
84bool DataSelector::isOptional() const { return m_isOptional; }
85
90void DataSelector::isOptional(bool optional) {
92 m_uiForm.rfFileInput->isOptional(optional);
93 m_uiForm.wsWorkspaceInput->setOptional(optional);
94}
95
100 // Get filename and check it's not empty
101 QString filename = m_uiForm.rfFileInput->getUserInput().toString();
102
103 if (filename.isEmpty()) {
104 return;
105 }
106
107 // attempt to load the file
108 if (m_autoLoad) {
109 autoLoadFile(filename);
110 } else {
111 // files were found
112 emit filesFound();
113 }
114}
115
119void DataSelector::setSelectorIndex(int index) { m_uiForm.cbInputType->setCurrentIndex(index); }
120
124void DataSelector::setTypeSelectorVisible(bool visible) { m_uiForm.cbInputType->setVisible(visible); }
125
132 int index = m_uiForm.stackedDataSelect->currentIndex();
133 return (index == 0);
134}
135
142
152 bool isValid = false;
153
154 if (isFileSelectorVisible()) {
155 isValid = m_uiForm.rfFileInput->isValid();
156
157 // check to make sure the user hasn't deleted the auto-loaded file
158 // since choosing it.
159 if (isValid && m_autoLoad) {
160 auto const wsName = getCurrentDataName().toStdString();
161
162 isValid = !wsName.empty();
163 if (isValid && !doesExistInADS(wsName)) {
164 // attempt to reload if we can
165 // don't use algorithm runner because we need to know instantly.
166 auto const filepath = m_uiForm.rfFileInput->getUserInput().toString().toStdString();
167 if (!filepath.empty())
168 loadFile(filepath, wsName);
169
170 isValid = doesExistInADS(wsName);
171
172 if (!isValid) {
173 m_uiForm.rfFileInput->setFileProblem("The specified workspace is "
174 "missing from the analysis data "
175 "service");
176 }
177 }
178 }
179 } else {
180 isValid = m_uiForm.wsWorkspaceInput->isValid();
181 }
182
183 return isValid;
184}
185
191 QString problem = "";
192 if (isFileSelectorVisible()) {
193 problem = m_uiForm.rfFileInput->getFileProblem();
194 if (problem.compare("") == 0) {
195 problem = "Input field is empty";
196 }
197 } else {
198 problem = "A valid workspace has not been selected";
199 }
200
201 return problem;
202}
203
212void DataSelector::autoLoadFile(const QString &filepath) {
213 const auto baseName = getWsNameFromFiles().toStdString();
214
215 // create instance of load algorithm
216 const auto loadAlg = AlgorithmManager::Instance().createUnmanaged(loadAlgName(filepath.toStdString()));
217 loadAlg->initialize();
218 loadAlg->setProperty("Filename", filepath.toStdString());
219 loadAlg->setProperty("OutputWorkspace", baseName);
220
222}
223
230 if (!error) {
231
232 // emit that we got a valid workspace/file to work with
234 } else {
235 m_uiForm.rfFileInput->setFileProblem("Could not load file. See log for details.");
236 }
237}
238
243 if (m_uiForm.stackedDataSelect->currentIndex() > 0) {
244 // Get text of name of workspace to use
245 QString filename = m_uiForm.wsWorkspaceInput->currentText();
246 if (filename.isEmpty())
247 return;
248
249 // emit that we got a valid workspace/file to work with
250 emit dataReady(filename);
251 }
252}
253
260 // Index indicates which view is visible.
261 m_uiForm.stackedDataSelect->setCurrentIndex(index);
262
263 // 0 is always file view
264 switch (index) {
265 case 0:
266 emit fileViewVisible();
267 break;
268 case 1:
271 break;
272 }
273}
274
280QString DataSelector::getFullFilePath() const { return m_uiForm.rfFileInput->getUserInput().toString(); }
281
288 QString filepath = DataSelector::getFullFilePath();
289 QFileInfo qfio(filepath);
290 QString baseName = qfio.completeBaseName();
291
292 // make up a name for the group workspace, if multiple files are specified
293 if (m_uiForm.rfFileInput->allowMultipleFiles() && filepath.count(",") > 0) {
294 baseName += "_group";
295 }
296
297 return baseName;
298}
299
313 QString filename("");
314
315 int index = m_uiForm.stackedDataSelect->currentIndex();
316
317 switch (index) {
318 case 0:
319 // the file selector is visible
320 if (m_uiForm.rfFileInput->isValid()) {
321 if (m_uiForm.rfFileInput->allowMultipleFiles() && !m_autoLoad) {
322 // if multiple files are allowed, auto-loading is not on, return the
323 // full user input
324 filename = getFullFilePath();
325 } else {
326 filename = getWsNameFromFiles();
327 }
328 }
329 break;
330 case 1:
331 // the workspace selector is visible
332 filename = m_uiForm.wsWorkspaceInput->currentText();
333 break;
334 }
335
336 return filename;
337}
338
345
351void DataSelector::setAutoLoad(bool load) { m_autoLoad = load; }
352
358QString DataSelector::getLoadBtnText() const { return m_uiForm.pbLoadFile->text(); }
359
365void DataSelector::setLoadBtnText(const QString &text) { m_uiForm.pbLoadFile->setText(text); }
366
371void DataSelector::readSettings(const QString &group) { m_uiForm.rfFileInput->readSettings(group); }
372
377void DataSelector::saveSettings(const QString &group) { m_uiForm.rfFileInput->saveSettings(group); }
378
385
393 m_uiForm.pbLoadFile->setEnabled(load);
394 m_uiForm.pbLoadFile->setVisible(load);
395 m_showLoad = load;
396}
397
402void DataSelector::dropEvent(QDropEvent *de) {
403 const QMimeData *mimeData = de->mimeData();
404 auto before_action = de->dropAction();
405
406 auto const dragData = mimeData->text().toStdString();
407
408 if (de->mimeData() && doesExistInADS(dragData)) {
409 m_uiForm.wsWorkspaceInput->dropEvent(de);
410 if (de->dropAction() == before_action) {
411 setWorkspaceSelectorIndex(mimeData->text());
412 m_uiForm.cbInputType->setCurrentIndex(1);
413 return;
414 }
415 de->setDropAction(before_action);
416 }
417
418 m_uiForm.rfFileInput->dropEvent(de);
419 if (de->dropAction() == before_action) {
420 m_uiForm.cbInputType->setCurrentIndex(0);
421 }
422
423 auto const filepath = m_uiForm.rfFileInput->getText().toStdString();
424 if (de->mimeData() && !doesExistInADS(dragData) && !filepath.empty()) {
425 auto const file = extractLastOf(filepath, "/");
426 if (fileFound(file)) {
427 auto const workspaceName = cutLastOf(file, ".");
428 loadFile(filepath, workspaceName);
429
430 setWorkspaceSelectorIndex(QString::fromStdString(workspaceName));
431 m_uiForm.cbInputType->setCurrentIndex(1);
432 }
433 }
434}
435
441void DataSelector::setWorkspaceSelectorIndex(QString const &workspaceName) {
442 auto const index = m_uiForm.wsWorkspaceInput->findText(workspaceName);
443 m_uiForm.wsWorkspaceInput->setCurrentIndex(index != -1 ? index : 0);
444}
445
450void DataSelector::dragEnterEvent(QDragEnterEvent *de) {
451 const QMimeData *mimeData = de->mimeData();
452 if (mimeData->hasText() || mimeData->hasUrls())
453 de->acceptProposedAction();
454}
455
456} // namespace MantidQt::MantidWidgets
double error
Definition: IndexPeaks.cpp:133
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
virtual void startAlgorithm(Mantid::API::IAlgorithm_sptr alg)
Begin asynchronous execution of an algorithm and observe its execution.
This is the base class all customised widgets that do not wish to be tied to a specific Mantid algori...
Definition: MantidWidget.h:27
bool isWorkspaceSelectorVisible() const
Get whether the workspace selector is currently being shown.
void setShowLoad(bool load)
Set if the load button should be shown.
MantidQt::API::AlgorithmRunner m_algRunner
Algorithm Runner used to run the load algorithm.
Definition: DataSelector.h:367
void setTypeSelectorVisible(bool visible)
Sets if the option to choose selector is visible.
bool willAutoLoad() const
Gets will auto load.
virtual bool isValid()
Checks if widget is in a valid state.
Ui::DataSelector m_uiForm
Member containing the widgets child widgets.
Definition: DataSelector.h:365
void handleViewChanged(int index)
Slot called when the current view is changed.
void setAutoLoad(bool)
Sets will auto load.
void dropEvent(QDropEvent *) override
Called when an item is dropped.
void autoLoadFile(const QString &filenames)
Attempt to automatically load a file.
void filesFound()
Signal emitted when files were found but widget isn't autoloading.
void saveSettings(const QString &)
Save settings in the given group.
void setWorkspaceSelectorIndex(QString const &workspaceName)
Set the index of the combobox containing the loaded workspace.
void fileViewVisible()
Signal emitted when file input is visible.
bool isFileSelectorVisible() const
Get whether the file selector is currently being shown.
void setLoadBtnText(const QString &)
Sets the load button text.
QString getWsNameFromFiles() const
Get the workspace name from the list of files.
QString getFullFilePath() const
Get the current file path in the FileFinderWidget widget.
void dragEnterEvent(QDragEnterEvent *) override
Called when an item is dragged onto a control.
bool willShowLoad()
Check if the widget will show the load button.
bool m_isOptional
Flag if optional.
Definition: DataSelector.h:373
bool m_showLoad
Flag to show or hide the load button. By default this is set to true.
Definition: DataSelector.h:371
void readSettings(const QString &)
Read settings from the given group.
void handleAutoLoadComplete(bool error)
Slot called if the widget fails to auto load the file.
QString getProblem() const
Get file problem, empty string means no error.
bool isOptional() const
Gets if optional.
DataSelector(QWidget *parent=nullptr)
virtual QString getCurrentDataName() const
Get the currently available file or workspace name.
void handleWorkspaceInput()
Slot called when workspace input is available.
bool m_autoLoad
Flag to enable auto loading. By default this is set to true.
Definition: DataSelector.h:369
void dataReady(const QString &wsname)
Signal emitted when data is ready from a workspace selector or file browser.
QString getLoadBtnText() const
Gets the load button text.
void setSelectorIndex(int index)
Sets which selector (file or workspace) is visible.
void loadClicked()
Signal emitted when the load button is clicked.
void workspaceViewVisible()
Signal emitted when workspace selector is visible.
void handleFileInput()
Slot called when file input is available.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
MANTID_KERNEL_DLL std::string loadFile(const std::string &filepath, const bool unixEOL)
Load contents of file into a string.