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 std::transform(wsNames.cbegin(), wsNames.cend(), std::back_inserter(vecWsNames),
36 [](const auto &wsName) { return wsName.toStdString(); });
37 alg->setProperty("WorkspaceList", vecWsNames);
39 }
40 } catch (...) {
41 QMessageBox::warning(nullptr, "", "Could not delete selected workspaces.");
42 }
43}
44
45void MantidTreeModel::renameWorkspace(QStringList wsName) {
46 // Determine the algorithm
47 QString algName("RenameWorkspace");
48 if (wsName.size() > 1)
49 algName = "RenameWorkspaces";
50
52 if (wsName.size() > 1) {
53 presets["InputWorkspaces"] = wsName.join(",");
54 } else {
55 presets["InputWorkspace"] = wsName[0];
56 }
57 showAlgorithmDialog(algName, presets);
58}
59
60// Algorithm Display and Execution Methods
63 try {
64 alg = Mantid::API::AlgorithmManager::Instance().create(algName.toStdString(), version);
65 } catch (...) {
66 QString message = "Cannot create algorithm \"" + algName + "\"";
67 if (version != -1) {
68 message += " version " + QString::number(version);
69 }
70 QMessageBox::warning(nullptr, "", message);
72 }
73 return alg;
74}
75void MantidTreeModel::showAlgorithmDialog(const QString &algName, int version) {
76 // Check if Alg is valid
77 Mantid::API::IAlgorithm_sptr alg = this->createAlgorithm(algName, version);
78 if (!alg)
79 return;
80 MantidQt::API::InterfaceManager interfaceManager;
81 MantidQt::API::AlgorithmDialog *dlg = interfaceManager.createDialog(alg, nullptr, false);
82 dlg->show();
83 dlg->raise();
84 dlg->activateWindow();
85}
86
88 Mantid::API::AlgorithmObserver *obs, int version) {
89 // Get latest version of the algorithm
90 Mantid::API::IAlgorithm_sptr alg = this->createAlgorithm(algName, version);
91 if (!alg)
92 return;
93
94 try {
95 for (QHash<QString, QString>::Iterator it = paramList.begin(); it != paramList.end(); ++it) {
96 alg->setPropertyValue(it.key().toStdString(), it.value().toStdString());
97 }
98 } catch (std::exception &ex) {
99 g_log.error() << "Error setting the properties for algotithm " << algName.toStdString() << ": " << ex.what()
100 << '\n';
101 return;
102 }
104 if (obs) {
105 dlg->addAlgorithmObserver(obs);
106 }
107
108 dlg->show();
109 dlg->raise();
110 dlg->activateWindow();
111}
112
119 QStringList enabled;
120
121 // If a property was explicitly set show it as preset in the dialog
122 const std::vector<Mantid::Kernel::Property *> props = alg->getProperties();
123 std::vector<Mantid::Kernel::Property *>::const_iterator p = props.begin();
124 for (; p != props.end(); ++p) {
125 if (!(**p).isDefault()) {
126 QString property_name = QString::fromStdString((**p).name());
127 presets.insert(property_name, QString::fromStdString((**p).value()));
128 enabled.append(property_name);
129 }
130 }
131
132 // Check if a workspace is selected in the dock and set this as a preference
133 // for the input workspace
134 // This is an optional message displayed at the top of the GUI.
135 QString optional_msg(alg->summary().c_str());
136
137 MantidQt::API::InterfaceManager interfaceManager;
139 interfaceManager.createDialog(alg, nullptr, false, presets, optional_msg, enabled);
140 return dlg;
141}
142
144
146 if (wait) {
147 Poco::ActiveResult<bool> result(alg->executeAsync());
148 while (!result.available()) {
149 QCoreApplication::processEvents();
150 }
151 result.wait();
152
153 try {
154 return result.data();
155 } catch (Poco::NullPointerException &) {
156 return false;
157 }
158 } else {
159 try {
160 alg->executeAsync();
161 } catch (Poco::NoThreadAvailableException &) {
162 g_log.error() << "No thread was available to run the " << alg->name() << " algorithm in the background.\n";
163 return false;
164 }
165 return true;
166 }
167}
168
170 if (AnalysisDataService::Instance().doesExist(workspaceName.toStdString())) {
171 return AnalysisDataService::Instance().retrieve(workspaceName.toStdString());
172 }
174 return empty;
175}
176
177//===========================================================
178// Interface require functions
179//===========================================================
180// The following functions have not been implemented as they
181// require other code to be ported to the workbench
182// In the case of functions that return, they return nullptr
183
184void MantidTreeModel::updateRecentFilesList(const QString &fname) { /*Not require until tool bar is created*/
185 Q_UNUSED(fname);
186}
187void MantidTreeModel::enableSaveNexus(const QString &wsName) { /*handled by widget*/ Q_UNUSED(wsName); }
188void MantidTreeModel::disableSaveNexus() { /* handled by widget*/ }
189
194QWidget *MantidTreeModel::getParent() { return new QWidget(nullptr); }
196void MantidTreeModel::showCritical(const QString &msg) { Q_UNUSED(msg); }
204
206 int upper, bool showDlg) {
207 Q_UNUSED(workspace);
208 Q_UNUSED(lower);
209 Q_UNUSED(upper);
210 Q_UNUSED(showDlg);
211 return nullptr;
212}
213
214void MantidTreeModel::importWorkspace(const QString &wsName, bool showDlg, bool makeVisible) {
215 Q_UNUSED(wsName);
216 Q_UNUSED(showDlg);
217 Q_UNUSED(makeVisible);
218}
219
220Table *MantidTreeModel::createDetectorTable(const QString &wsName, const std::vector<int> &indices, bool include_data) {
221 Q_UNUSED(wsName);
222 Q_UNUSED(indices);
223 Q_UNUSED(include_data);
224 return nullptr;
225}
226
227MultiLayer *MantidTreeModel::plot1D(const QMultiMap<QString, std::set<int>> &toPlot, bool spectrumPlot,
228 MantidQt::DistributionFlag distr, bool errs, MultiLayer *plotWindow,
229 bool clearWindow, bool waterfallPlot, const QString &log,
230 const std::set<double> &customLogValues) {
231 Q_UNUSED(toPlot);
232 Q_UNUSED(spectrumPlot);
233 Q_UNUSED(distr);
234 Q_UNUSED(errs);
235 Q_UNUSED(plotWindow);
236 Q_UNUSED(clearWindow);
237 Q_UNUSED(waterfallPlot);
238 Q_UNUSED(log);
239 Q_UNUSED(customLogValues);
240 return nullptr;
241}
242
243void MantidTreeModel::drawColorFillPlots(const QStringList &wsNames, GraphOptions::CurveType curveType) {
244 Q_UNUSED(wsNames);
245 Q_UNUSED(curveType);
246}
247
248MultiLayer *MantidTreeModel::plotSubplots(const QMultiMap<QString, std::set<int>> &toPlot,
249 MantidQt::DistributionFlag distr, bool errs, MultiLayer *plotWindow) {
250 Q_UNUSED(toPlot);
251 Q_UNUSED(distr);
252 Q_UNUSED(errs);
253 Q_UNUSED(plotWindow);
254
255 return nullptr;
256}
257
258void MantidTreeModel::plotSurface(bool accepted, int plotIndex, const QString &axisName, const QString &logName,
259 const std::set<double> &customLogValues, const QList<QString> &workspaceNames) {
260 Q_UNUSED(accepted);
261 Q_UNUSED(plotIndex);
262 Q_UNUSED(axisName);
263 Q_UNUSED(logName);
264 Q_UNUSED(customLogValues);
265 Q_UNUSED(workspaceNames);
266}
267
268void MantidTreeModel::plotContour(bool accepted, int plotIndex, const QString &axisName, const QString &logName,
269 const std::set<double> &customLogValues, const QList<QString> &workspaceNames) {
270 Q_UNUSED(accepted);
271 Q_UNUSED(plotIndex);
272 Q_UNUSED(axisName);
273 Q_UNUSED(logName);
274 Q_UNUSED(customLogValues);
275 Q_UNUSED(workspaceNames);
276}
277
279MantidTreeModel::createWorkspaceIndexDialog(int flags, const QStringList &wsNames, bool showWaterfall, bool showPlotAll,
280 bool showTiledOpt, bool isAdvanced) {
281 Q_UNUSED(flags);
282 Q_UNUSED(wsNames);
283 Q_UNUSED(showWaterfall);
284 Q_UNUSED(showPlotAll);
285 Q_UNUSED(showTiledOpt);
286 Q_UNUSED(isAdvanced);
287 // Return empty MantidWSIndexDialog
288 return new MantidWSIndexDialog(nullptr, Qt::WindowFlags(), QList<QString>());
289}
IPeaksWorkspace_sptr workspace
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:51
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
CurveType
Graph curve type.
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)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
STL namespace.