Mantid
Loading...
Searching...
No Matches
MantidTreeModel.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#include <Poco/ActiveResult.h>
13#include <QMessageBox>
14#include <qcoreapplication.h>
15
16using namespace std;
17using namespace MantidQt;
18using namespace MantidWidgets;
19using namespace Mantid::API;
20
21namespace {
22Mantid::Kernel::Logger g_log("WorkspaceWidget");
23}
24
26
27// Data display and saving methods
28void MantidTreeModel::deleteWorkspaces(const QStringList &wsNames) {
29 try {
30 if (!wsNames.isEmpty()) {
31 auto alg = createAlgorithm("DeleteWorkspaces");
32 alg->setLogging(false);
33 std::vector<std::string> vecWsNames;
34 vecWsNames.reserve(wsNames.size());
35 foreach (auto wsName, wsNames) { vecWsNames.emplace_back(wsName.toStdString()); }
36 alg->setProperty("WorkspaceList", vecWsNames);
38 }
39 } catch (...) {
40 QMessageBox::warning(nullptr, "", "Could not delete selected workspaces.");
41 }
42}
43
44void MantidTreeModel::renameWorkspace(QStringList wsName) {
45 // Determine the algorithm
46 QString algName("RenameWorkspace");
47 if (wsName.size() > 1)
48 algName = "RenameWorkspaces";
49
51 if (wsName.size() > 1) {
52 presets["InputWorkspaces"] = wsName.join(",");
53 } else {
54 presets["InputWorkspace"] = wsName[0];
55 }
56 showAlgorithmDialog(algName, presets);
57}
58
59// Algorithm Display and Execution Methods
62 try {
63 alg = Mantid::API::AlgorithmManager::Instance().create(algName.toStdString(), version);
64 } catch (...) {
65 QString message = "Cannot create algorithm \"" + algName + "\"";
66 if (version != -1) {
67 message += " version " + QString::number(version);
68 }
69 QMessageBox::warning(nullptr, "", message);
71 }
72 return alg;
73}
74void MantidTreeModel::showAlgorithmDialog(const QString &algName, int version) {
75 // Check if Alg is valid
76 Mantid::API::IAlgorithm_sptr alg = this->createAlgorithm(algName, version);
77 if (!alg)
78 return;
79 MantidQt::API::InterfaceManager interfaceManager;
80 MantidQt::API::AlgorithmDialog *dlg = interfaceManager.createDialog(alg, nullptr, false);
81 dlg->show();
82 dlg->raise();
83 dlg->activateWindow();
84}
85
87 Mantid::API::AlgorithmObserver *obs, int version) {
88 // Get latest version of the algorithm
89 Mantid::API::IAlgorithm_sptr alg = this->createAlgorithm(algName, version);
90 if (!alg)
91 return;
92
93 try {
94 for (QHash<QString, QString>::Iterator it = paramList.begin(); it != paramList.end(); ++it) {
95 alg->setPropertyValue(it.key().toStdString(), it.value().toStdString());
96 }
97 } catch (std::exception &ex) {
98 g_log.error() << "Error setting the properties for algotithm " << algName.toStdString() << ": " << ex.what()
99 << '\n';
100 return;
101 }
103 if (obs) {
104 dlg->addAlgorithmObserver(obs);
105 }
106
107 dlg->show();
108 dlg->raise();
109 dlg->activateWindow();
110}
111
118 QStringList enabled;
119
120 // If a property was explicitly set show it as preset in the dialog
121 const std::vector<Mantid::Kernel::Property *> props = alg->getProperties();
122 std::vector<Mantid::Kernel::Property *>::const_iterator p = props.begin();
123 for (; p != props.end(); ++p) {
124 if (!(**p).isDefault()) {
125 QString property_name = QString::fromStdString((**p).name());
126 presets.insert(property_name, QString::fromStdString((**p).value()));
127 enabled.append(property_name);
128 }
129 }
130
131 // Check if a workspace is selected in the dock and set this as a preference
132 // for the input workspace
133 // This is an optional message displayed at the top of the GUI.
134 QString optional_msg(alg->summary().c_str());
135
136 MantidQt::API::InterfaceManager interfaceManager;
138 interfaceManager.createDialog(alg, nullptr, false, presets, optional_msg, enabled);
139 return dlg;
140}
141
143
145 if (wait) {
146 Poco::ActiveResult<bool> result(alg->executeAsync());
147 while (!result.available()) {
148 QCoreApplication::processEvents();
149 }
150 result.wait();
151
152 try {
153 return result.data();
154 } catch (Poco::NullPointerException &) {
155 return false;
156 }
157 } else {
158 try {
159 alg->executeAsync();
160 } catch (Poco::NoThreadAvailableException &) {
161 g_log.error() << "No thread was available to run the " << alg->name() << " algorithm in the background.\n";
162 return false;
163 }
164 return true;
165 }
166}
167
169 if (AnalysisDataService::Instance().doesExist(workspaceName.toStdString())) {
170 return AnalysisDataService::Instance().retrieve(workspaceName.toStdString());
171 }
173 return empty;
174}
175
176//===========================================================
177// Interface require functions
178//===========================================================
179// The following functions have not been implemented as they
180// require other code to be ported to the workbench
181// In the case of functions that return, they return nullptr
182
183void MantidTreeModel::updateRecentFilesList(const QString &fname) { /*Not require until tool bar is created*/
184 Q_UNUSED(fname);
185}
186void MantidTreeModel::enableSaveNexus(const QString &wsName) { /*handled by widget*/
187 Q_UNUSED(wsName);
188}
189void MantidTreeModel::disableSaveNexus() { /* handled by widget*/
190}
191
196QWidget *MantidTreeModel::getParent() { return new QWidget(nullptr); }
198void MantidTreeModel::showCritical(const QString &msg) { Q_UNUSED(msg); }
206
208 int upper, bool showDlg) {
209 Q_UNUSED(workspace);
210 Q_UNUSED(lower);
211 Q_UNUSED(upper);
212 Q_UNUSED(showDlg);
213 return nullptr;
214}
215
216void MantidTreeModel::importWorkspace(const QString &wsName, bool showDlg, bool makeVisible) {
217 Q_UNUSED(wsName);
218 Q_UNUSED(showDlg);
219 Q_UNUSED(makeVisible);
220}
221
222Table *MantidTreeModel::createDetectorTable(const QString &wsName, const std::vector<int> &indices, bool include_data) {
223 Q_UNUSED(wsName);
224 Q_UNUSED(indices);
225 Q_UNUSED(include_data);
226 return nullptr;
227}
228
229MultiLayer *MantidTreeModel::plot1D(const QMultiMap<QString, std::set<int>> &toPlot, bool spectrumPlot,
230 MantidQt::DistributionFlag distr, bool errs, MultiLayer *plotWindow,
231 bool clearWindow, bool waterfallPlot, const QString &log,
232 const std::set<double> &customLogValues) {
233 Q_UNUSED(toPlot);
234 Q_UNUSED(spectrumPlot);
235 Q_UNUSED(distr);
236 Q_UNUSED(errs);
237 Q_UNUSED(plotWindow);
238 Q_UNUSED(clearWindow);
239 Q_UNUSED(waterfallPlot);
240 Q_UNUSED(log);
241 Q_UNUSED(customLogValues);
242 return nullptr;
243}
244
245void MantidTreeModel::drawColorFillPlots(const QStringList &wsNames, GraphOptions::CurveType curveType) {
246 Q_UNUSED(wsNames);
247 Q_UNUSED(curveType);
248}
249
250MultiLayer *MantidTreeModel::plotSubplots(const QMultiMap<QString, std::set<int>> &toPlot,
251 MantidQt::DistributionFlag distr, bool errs, MultiLayer *plotWindow) {
252 Q_UNUSED(toPlot);
253 Q_UNUSED(distr);
254 Q_UNUSED(errs);
255 Q_UNUSED(plotWindow);
256
257 return nullptr;
258}
259
260void MantidTreeModel::plotSurface(bool accepted, int plotIndex, const QString &axisName, const QString &logName,
261 const std::set<double> &customLogValues, const QList<QString> &workspaceNames) {
262 Q_UNUSED(accepted);
263 Q_UNUSED(plotIndex);
264 Q_UNUSED(axisName);
265 Q_UNUSED(logName);
266 Q_UNUSED(customLogValues);
267 Q_UNUSED(workspaceNames);
268}
269
270void MantidTreeModel::plotContour(bool accepted, int plotIndex, const QString &axisName, const QString &logName,
271 const std::set<double> &customLogValues, const QList<QString> &workspaceNames) {
272 Q_UNUSED(accepted);
273 Q_UNUSED(plotIndex);
274 Q_UNUSED(axisName);
275 Q_UNUSED(logName);
276 Q_UNUSED(customLogValues);
277 Q_UNUSED(workspaceNames);
278}
279
281MantidTreeModel::createWorkspaceIndexDialog(int flags, const QStringList &wsNames, bool showWaterfall, bool showPlotAll,
282 bool showTiledOpt, bool isAdvanced) {
283 Q_UNUSED(flags);
284 Q_UNUSED(wsNames);
285 Q_UNUSED(showWaterfall);
286 Q_UNUSED(showPlotAll);
287 Q_UNUSED(showTiledOpt);
288 Q_UNUSED(isAdvanced);
289 // Return empty MantidWSIndexDialog
290 return new MantidWSIndexDialog(nullptr, Qt::WindowFlags(), QList<QString>());
291}
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
double lower
lower and upper bounds on the multiplier, if known
double upper
This class should be the basis for all customised algorithm dialogs.
void addAlgorithmObserver(Mantid::API::AlgorithmObserver *observer)
Add an AlgorithmObserver to the algorithm.
This class is responsible for creating the correct dialog for an algorithm.
AlgorithmDialog * createDialog(const std::shared_ptr< Mantid::API::IAlgorithm > &alg, QWidget *parent=nullptr, bool forScript=false, const QHash< QString, QString > &presetValues=(QHash< QString, QString >()), const QString &optional_msg=QString(), const QStringList &enabled=QStringList(), const QStringList &disabled=QStringList())
Create a new instance of the correct type of AlgorithmDialog.
bool executeAlgorithmAsync(Mantid::API::IAlgorithm_sptr alg, const bool wait=false) override
MantidQt::MantidWidgets::MantidWSIndexDialog * createWorkspaceIndexDialog(int flags, const QStringList &wsNames, bool showWaterfall, bool showPlotAll, bool showTiledOpt, bool isAdvanced=false) override
void plotSurface(bool accepted, int plotIndex, const QString &axisName, const QString &logName, const std::set< double > &customLogValues, const QList< QString > &workspaceNames) override
void plotContour(bool accepted, int plotIndex, const QString &axisName, const QString &logName, const std::set< double > &customLogValues, const QList< QString > &workspaceNames) override
void updateRecentFilesList(const QString &fname) override
MultiLayer * plotSubplots(const QMultiMap< QString, std::set< int > > &toPlot, MantidQt::DistributionFlag distr=MantidQt::DistributionDefault, bool errs=false, MultiLayer *plotWindow=nullptr) override
Table * createDetectorTable(const QString &wsName, const std::vector< int > &indices, bool include_data=false) override
void showCritical(const QString &) override
MultiLayer * plot1D(const QMultiMap< QString, std::set< int > > &toPlot, bool spectrumPlot, MantidQt::DistributionFlag distr=MantidQt::DistributionDefault, bool errs=false, MultiLayer *plotWindow=nullptr, bool clearWindow=false, bool waterfallPlot=false, const QString &log="", const std::set< double > &customLogValues=std::set< double >()) override
Mantid::API::IAlgorithm_sptr createAlgorithm(const QString &algName, int version=-1) override
void deleteWorkspaces(const QStringList &wsNames=QStringList()) override
void showAlgorithmDialog(const QString &algName, int version=-1) override
MantidQt::API::AlgorithmDialog * createAlgorithmDialog(const Mantid::API::IAlgorithm_sptr &alg)
This creates an algorithm dialog (the default property entry thingie).
void enableSaveNexus(const QString &wsName) override
Mantid::API::Workspace_const_sptr getWorkspace(const QString &workspaceName) override
void renameWorkspace(QStringList=QStringList()) override
MantidMatrix * importMatrixWorkspace(const Mantid::API::MatrixWorkspace_sptr workspace, int lower=-1, int upper=-1, bool showDlg=true) override
void drawColorFillPlots(const QStringList &wsNames, GraphOptions::CurveType curveType=GraphOptions::ColorMap) override
void executeAlgorithm(Mantid::API::IAlgorithm_sptr alg) override
Observes Algorithm notifications: start,progress,finish,error.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
CurveType
Graph curve type.
Definition: GraphOptions.h:28
The AlgorithmProgressDialogPresenter keeps track of the running algorithms and displays a progress ba...
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< const Workspace > Workspace_const_sptr
shared pointer to Mantid::API::Workspace (const version)
Definition: Workspace_fwd.h:22
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
STL namespace.