Mantid
Loading...
Searching...
No Matches
ProcessingAlgoWidget.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 +
10#include <QFileDialog>
11#include <QFileInfo>
12#include <QMessageBox>
13#include <QSettings>
14#include <Qsci/qscilexerpython.h>
15#include <fstream>
16#include <iosfwd>
17
20
22
23//----------------------
24// Public member functions
25//----------------------
27ProcessingAlgoWidget::ProcessingAlgoWidget(QWidget *parent) : QWidget(parent) {
28 ui.setupUi(this);
29
30 // Load all available algorithms
31 ui.algoSelector->update();
32
33 // Enable syntax highlighting
34 ui.editor->setLexer(new QsciLexerPython);
35
36 // Layout tweak
37 QList<int> sizes;
38 sizes.push_back(300);
39 sizes.push_back(1000);
40 ui.splitter->setSizes(sizes);
41 ui.splitter->setStretchFactor(0, 0);
42 ui.splitter->setStretchFactor(1, 0);
43
44 //=========== SLOTS =============
45 connect(ui.algoSelector, SIGNAL(algorithmSelectionChanged(const QString &, int)), this, SLOT(changeAlgorithm()));
46
47 connect(ui.btnSave, SIGNAL(clicked()), this, SLOT(btnSaveClicked()));
48 connect(ui.btnLoad, SIGNAL(clicked()), this, SLOT(btnLoadClicked()));
49
51}
52
53//------------------------------------------------------------------------------
56
57//------------------------------------------------------------------------------
59void ProcessingAlgoWidget::saveInput() { ui.algoProperties->saveInput(); }
60
61//------------------------------------------------------------------------------------
64 QSettings settings;
65 settings.beginGroup("Mantid/ProcessingAlgoWidget");
66 m_lastFile = settings.value("LastFile", QString()).toString();
67 settings.endGroup();
68}
69
70//------------------------------------------------------------------------------------
73 QSettings settings;
74 settings.beginGroup("Mantid/ProcessingAlgoWidget");
75 settings.setValue("LastFile", m_lastFile);
76 settings.endGroup();
77}
78
79//------------------------------------------------------------------------------
82 // Save to a .py file
83 QString fileselection = QFileDialog::getSaveFileName(
84 this, "Save a Python Script", QFileInfo(m_lastFile).absoluteFilePath(), "Python scripts (*.py);;All files (*)");
85 if (!fileselection.isEmpty()) {
86 m_lastFile = fileselection;
87 std::ofstream file;
88 file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
89 try {
90 file.open(fileselection.toStdString().c_str());
91 file << ui.editor->text().toStdString();
92 file.close();
93 } catch (std::ifstream::failure &e) {
94 QMessageBox::critical(this, "Exception saving file " + m_lastFile,
95 QString("The file could not be saved due to the following exception:\n") +
96 QString(e.what()));
97 }
98 }
99}
100
101//------------------------------------------------------------------------------
104 // Load a .py file
105 QString fileselection = QFileDialog::getOpenFileName(
106 this, "Load a Python Script", QFileInfo(m_lastFile).absoluteFilePath(), "Python scripts (*.py);;All files (*)");
107 if (!fileselection.isEmpty()) {
108 m_lastFile = fileselection;
109 std::ifstream file(fileselection.toStdString().c_str());
110 std::stringstream buffer;
111 buffer << file.rdbuf();
112 ui.editor->setText(QString::fromStdString(buffer.str()));
113 file.close();
114 }
115}
116
117//------------------------------------------------------------------------------
121 auto alg = ui.algoSelector->getSelectedAlgorithm();
122 try {
123 m_alg = AlgorithmManager::Instance().createUnmanaged(alg.name.toStdString(), alg.version);
124 m_alg->initialize();
125 } catch (std::runtime_error &) {
126 // Ignore when the m_algorithm is not found
128
129 // Signal that the algorithm just changed
130 emit changedAlgorithm();
131 return;
132 }
133 QStringList disabled;
134 disabled.push_back("OutputWorkspace");
135 disabled.push_back("InputWorkspace");
136 disabled.push_back("Workspace");
137 ui.algoProperties->addEnabledAndDisableLists(QStringList(), disabled);
138 // Sets the m_algorithm and also the properties from the InputHistory
139 ui.algoProperties->setAlgorithm(m_alg);
140 ui.algoProperties->hideOrDisableProperties();
141
142 // Signal that the algorithm just changed
143 emit changedAlgorithm();
144}
145
146//------------------------------------------------------------------------------
153 ui.algoSelector->setSelectedAlgorithm(algo);
154 this->changeAlgorithm();
155}
156
158QString ProcessingAlgoWidget::getScriptText() { return ui.editor->text(); }
160void ProcessingAlgoWidget::setScriptText(const QString &text) { ui.editor->setText(text); }
161
162} // namespace MantidQt::MantidWidgets
void setSelectedAlgorithm(QString algo)
Set the name of the selected algorithm.
void saveInput()
Save the inputs to algorithm history.
ProcessingAlgoWidget(QWidget *parent=nullptr)
Default Constructor.
void changedAlgorithm()
Signal emitted when the algorithm changes.
void btnLoadClicked()
Slot called when the Load button is clicked.
void btnSaveClicked()
Slot called when the save button is clicked.
void changeAlgorithm()
Slot called when the algorithm selected changes.
void saveSettings()
Save settings for next time.
Mantid::API::Algorithm_sptr m_alg
Current algorithm with properties set.
Ui::ProcessingAlgoWidget ui
The form generated by Qt Designer.
void loadSettings()
Load QSettings from .ini-type files.
void setScriptText(const QString &text)
Set the script editor text.
Manage the lifetime of a class intended to be a singleton.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
Definition: Algorithm.h:61