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 +
11#include <QFileDialog>
12#include <QFileInfo>
13#include <QMessageBox>
14#include <QSettings>
15#include <Qsci/qscilexerpython.h>
16#include <fstream>
17#include <iosfwd>
18#include <utility>
19
22
24
25ProcessingAlgoWidgetSettings::ProcessingAlgoWidgetSettings(QString lastFile) : m_lastFile(std::move(lastFile)) {}
26
27const QString &ProcessingAlgoWidgetSettings::lastFile() const { return m_lastFile; }
28
30 return ProcessingAlgoWidgetSettings(settings.value("LastFile", QString()).toString());
31}
32
34 QSettingsChangeAware(settings).setValue("LastFile", values.lastFile());
35}
36
37//----------------------
38// Public member functions
39//----------------------
41ProcessingAlgoWidget::ProcessingAlgoWidget(QWidget *parent) : QWidget(parent) {
42 ui.setupUi(this);
43
44 // Load all available algorithms
45 ui.algoSelector->update();
46
47 // Enable syntax highlighting
48 ui.editor->setLexer(new QsciLexerPython);
49
50 // Layout tweak
51 QList<int> sizes;
52 sizes.push_back(300);
53 sizes.push_back(1000);
54 ui.splitter->setSizes(sizes);
55 ui.splitter->setStretchFactor(0, 0);
56 ui.splitter->setStretchFactor(1, 0);
57
58 //=========== SLOTS =============
59 connect(ui.algoSelector, SIGNAL(algorithmSelectionChanged(const QString &, int)), this, SLOT(changeAlgorithm()));
60
61 connect(ui.btnSave, SIGNAL(clicked()), this, SLOT(btnSaveClicked()));
62 connect(ui.btnLoad, SIGNAL(clicked()), this, SLOT(btnLoadClicked()));
63
64 QSettings settings;
65 settings.beginGroup("Mantid/ProcessingAlgoWidget");
67 settings.endGroup();
68}
69
70//------------------------------------------------------------------------------
73 QSettings settings;
74 settings.beginGroup("Mantid/ProcessingAlgoWidget");
76 settings.endGroup();
77}
78
79//------------------------------------------------------------------------------
81void ProcessingAlgoWidget::saveInput() { ui.algoProperties->saveInput(); }
82
83//------------------------------------------------------------------------------------
87
91
92//------------------------------------------------------------------------------
95 // Save to a .py file
96 QString fileselection = QFileDialog::getSaveFileName(
97 this, "Save a Python Script", QFileInfo(m_lastFile).absoluteFilePath(), "Python scripts (*.py);;All files (*)");
98 if (!fileselection.isEmpty()) {
99 m_lastFile = fileselection;
100 std::ofstream file;
101 file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
102 try {
103 file.open(fileselection.toStdString().c_str());
104 file << ui.editor->text().toStdString();
105 file.close();
106 } catch (std::ifstream::failure &e) {
107 QMessageBox::critical(this, "Exception saving file " + m_lastFile,
108 QString("The file could not be saved due to the following exception:\n") +
109 QString(e.what()));
110 }
111 }
112}
113
114//------------------------------------------------------------------------------
117 // Load a .py file
118 QString fileselection = QFileDialog::getOpenFileName(
119 this, "Load a Python Script", QFileInfo(m_lastFile).absoluteFilePath(), "Python scripts (*.py);;All files (*)");
120 if (!fileselection.isEmpty()) {
121 m_lastFile = fileselection;
122 std::ifstream file(fileselection.toStdString().c_str());
123 std::stringstream buffer;
124 buffer << file.rdbuf();
125 ui.editor->setText(QString::fromStdString(buffer.str()));
126 file.close();
127 }
128}
129
130//------------------------------------------------------------------------------
134 auto alg = ui.algoSelector->getSelectedAlgorithm();
135 try {
136 m_alg = AlgorithmManager::Instance().createUnmanaged(alg.name.toStdString(), alg.version);
137 m_alg->initialize();
138 } catch (std::runtime_error &) {
139 // Ignore when the m_algorithm is not found
141
142 // Signal that the algorithm just changed
143 emit changedAlgorithm();
144 return;
145 }
146 QStringList disabled;
147 disabled.push_back("OutputWorkspace");
148 disabled.push_back("InputWorkspace");
149 disabled.push_back("Workspace");
150 ui.algoProperties->addEnabledAndDisableLists(QStringList(), disabled);
151 // Sets the m_algorithm and also the properties from the InputHistory
152 ui.algoProperties->setAlgorithm(m_alg);
153 ui.algoProperties->hideOrDisableProperties();
154
155 // Signal that the algorithm just changed
156 emit changedAlgorithm();
157}
158
159//------------------------------------------------------------------------------
166 ui.algoSelector->setSelectedAlgorithm(algo);
167 this->changeAlgorithm();
168}
169
171QString ProcessingAlgoWidget::getScriptText() { return ui.editor->text(); }
173void ProcessingAlgoWidget::setScriptText(const QString &text) { ui.editor->setText(text); }
174
175} // namespace MantidQt::MantidWidgets
static ProcessingAlgoWidgetSettings readSettings(const QSettings &settings)
Query persistent storage without changing widgets or writing settings.
static void saveSettings(QSettings &settings, const ProcessingAlgoWidgetSettings &values)
Persist only the last-file value from a snapshot.
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 restoreSettings(const ProcessingAlgoWidgetSettings &settings)
Apply an immutable settings snapshot without persistent access.
Mantid::API::Algorithm_sptr m_alg
Current algorithm with properties set.
Ui::ProcessingAlgoWidget ui
The form generated by Qt Designer.
ProcessingAlgoWidgetSettings captureSettings() const
Capture current widget state without persistent access.
void setScriptText(const QString &text)
Set the script editor text.
A QSettings facade that applies only effective changes.
bool setValue(QString const &key, QVariant const &value)
Set a value only when the effective stored value differs.
Mantid::Kernel::SingletonHolder< AlgorithmManagerImpl > AlgorithmManager
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
Definition Algorithm.h:52
std::string toString(const T &value)
Convert values to strings.
STL namespace.