Mantid
Loading...
Searching...
No Matches
WorkspaceTreeWidgetSimple.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
13#include "MantidAPI/Axis.h"
21
22#include <QMenu>
23#include <QSignalMapper>
24
25using namespace Mantid::API;
26using namespace Mantid::Kernel;
27
28namespace {
29bool hasSingleValue(const MatrixWorkspace &ws) { return (ws.getNumberHistograms() == 1 && ws.blocksize() == 1); }
30bool hasMultipleBins(const MatrixWorkspace &ws) {
31 try {
32 return (ws.blocksize() > 1);
33 } catch (...) {
34 const size_t numHist = ws.getNumberHistograms();
35 for (size_t i = 0; i < numHist; ++i) {
36 if (ws.y(i).size() > 1) {
37 return true;
38 }
39 }
40 }
41 return false;
42}
43} // namespace
44
46
48 : WorkspaceTreeWidget(new MantidTreeModel(), viewOnly, parent), m_plotSpectrum(new QAction("Spectrum...", this)),
49 m_plotBin(new QAction("Bin", this)), m_overplotSpectrum(new QAction("Overplot spectrum...", this)),
50 m_plotSpectrumWithErrs(new QAction("Spectrum with errors...", this)),
51 m_overplotSpectrumWithErrs(new QAction("Overplot spectrum with errors...", this)),
52 m_plotColorfill(new QAction("Colorfill", this)), m_sampleLogs(new QAction("Show Sample Logs", this)),
53 m_sliceViewer(new QAction("Show Slice Viewer", this)), m_showInstrument(new QAction("Show Instrument", this)),
54 m_showData(new QAction("Show Data", this)), m_showAlgorithmHistory(new QAction("Show History", this)),
55 m_showDetectors(new QAction("Show Detectors", this)), m_plotAdvanced(new QAction("Advanced...", this)),
56 m_plotSurface(new QAction("Surface", this)), m_plotWireframe(new QAction("Wireframe", this)),
57 m_plotContour(new QAction("Contour", this)), m_plotMDHisto1D(new QAction("Plot 1D MDHistogram...", this)),
58 m_overplotMDHisto1D(new QAction("Overplot 1D MDHistogram...", this)),
59 m_plotMDHisto1DWithErrs(new QAction("Plot 1D MDHistogram with errors...", this)),
60 m_overplotMDHisto1DWithErrs(new QAction("Overplot 1D MDHistogram with errors...", this)),
61 m_sampleMaterial(new QAction("Show Sample Material", this)),
62 m_sampleShape(new QAction("Show Sample Shape", this)), m_superplot(new QAction("Superplot...", this)),
63 m_superplotWithErrs(new QAction("Superplot with errors...", this)),
64 m_superplotBins(new QAction("Superplot bins...", this)),
65 m_superplotBinsWithErrs(new QAction("Superplot bins with errors...", this)) {
66
67 // Replace the double click action on the MantidTreeWidget
68 m_tree->m_doubleClickAction = [&](const QString &wsName) { emit workspaceDoubleClicked(wsName); };
69
70 connect(m_plotSpectrum, SIGNAL(triggered()), this, SLOT(onPlotSpectrumClicked()));
71 // connect event m_plotMDHisto1D to signal slot onPlotMDHistoWorkspaceClicked
72 connect(m_plotMDHisto1D, SIGNAL(triggered()), this, SLOT(onPlotMDHistoWorkspaceClicked()));
73 connect(m_overplotMDHisto1D, SIGNAL(triggered()), this, SLOT(onOverPlotMDHistoWorkspaceClicked()));
74 connect(m_plotMDHisto1DWithErrs, SIGNAL(triggered()), this, SLOT(onPlotMDHistoWorkspaceWithErrorsClicked()));
75 connect(m_overplotMDHisto1DWithErrs, SIGNAL(triggered()), this, SLOT(onOverPlotMDHistoWorkspaceWithErrorsClicked()));
76
77 connect(m_plotBin, SIGNAL(triggered()), this, SLOT(onPlotBinClicked()));
78 connect(m_overplotSpectrum, SIGNAL(triggered()), this, SLOT(onOverplotSpectrumClicked()));
79 connect(m_plotSpectrumWithErrs, SIGNAL(triggered()), this, SLOT(onPlotSpectrumWithErrorsClicked()));
80 connect(m_overplotSpectrumWithErrs, SIGNAL(triggered()), this, SLOT(onOverplotSpectrumWithErrorsClicked()));
81 connect(m_plotColorfill, SIGNAL(triggered()), this, SLOT(onPlotColorfillClicked()));
82 connect(m_sampleLogs, SIGNAL(triggered()), this, SLOT(onSampleLogsClicked()));
83 connect(m_sliceViewer, SIGNAL(triggered()), this, SLOT(onSliceViewerClicked()));
84 connect(m_showInstrument, SIGNAL(triggered()), this, SLOT(onShowInstrumentClicked()));
85 connect(m_showData, SIGNAL(triggered()), this, SLOT(onShowDataClicked()));
86 connect(m_tree, SIGNAL(itemSelectionChanged()), this, SIGNAL(treeSelectionChanged()));
87 connect(m_showAlgorithmHistory, SIGNAL(triggered()), this, SLOT(onShowAlgorithmHistoryClicked()));
88 connect(m_showDetectors, SIGNAL(triggered()), this, SLOT(onShowDetectorsClicked()));
89 connect(m_plotAdvanced, SIGNAL(triggered()), this, SLOT(onPlotAdvancedClicked()));
90 connect(m_plotSurface, SIGNAL(triggered()), this, SLOT(onPlotSurfaceClicked()));
91 connect(m_plotWireframe, SIGNAL(triggered()), this, SLOT(onPlotWireframeClicked()));
92 connect(m_plotContour, SIGNAL(triggered()), this, SLOT(onPlotContourClicked()));
93 connect(m_sampleMaterial, SIGNAL(triggered()), this, SLOT(onSampleMaterialClicked()));
94 connect(m_sampleShape, SIGNAL(triggered()), this, SLOT(onSampleShapeClicked()));
95 connect(m_superplot, SIGNAL(triggered()), this, SLOT(onSuperplotClicked()));
96 connect(m_superplotWithErrs, SIGNAL(triggered()), this, SLOT(onSuperplotWithErrsClicked()));
97 connect(m_superplotBins, SIGNAL(triggered()), this, SLOT(onSuperplotBinsClicked()));
98 connect(m_superplotBinsWithErrs, SIGNAL(triggered()), this, SLOT(onSuperplotBinsWithErrsClicked()));
99}
100
102
104 m_overplotSpectrum->setDisabled(disabled);
105 m_overplotSpectrumWithErrs->setDisabled(disabled);
106}
107
110 QTreeWidgetItem *treeItem = m_tree->itemAt(m_menuPosition);
111 selectedWsName = "";
112 if (treeItem)
113 selectedWsName = treeItem->text(0);
114 else
115 m_tree->selectionModel()->clear();
116
117 QMenu *menu(nullptr);
118 if (selectedWsName.isEmpty()) {
119 // If no workspace is here then have load items
120 menu = m_loadMenu;
121 } else {
122 // Check is defensive just in case the workspace has disappeared
124 try {
125 workspace = AnalysisDataService::Instance().retrieve(selectedWsName.toStdString());
126 } catch (Exception::NotFoundError &) {
127 return;
128 }
130 }
131
132 // Show the menu at the cursor's current position
133 menu->popup(QCursor::pos());
134}
135
138}
139
141
144}
145
148}
149
152}
153
156}
157
159
161
164}
165
167
170}
171
174}
175
178}
179
181
184}
185
187
188// Define signal
191}
192
195}
196
199}
200
203}
204
207}
208
210
212
215}
216
219}
220
223}
224
230 auto menu = new QMenu(this);
231 menu->setAttribute(Qt::WA_DeleteOnClose, true);
232 menu->setObjectName("WorkspaceContextMenu");
233
234 if (auto matrixWS = dynamic_cast<const MatrixWorkspace *>(&workspace))
235 addMatrixWorkspaceActions(menu, *matrixWS);
236 else if (auto tableWS = dynamic_cast<const ITableWorkspace *>(&workspace))
237 addTableWorkspaceActions(menu, *tableWS);
238 else if (auto mdWS = dynamic_cast<const IMDWorkspace *>(&workspace))
239 addMDWorkspaceActions(menu, *mdWS);
240 else if (auto wsGroup = dynamic_cast<const WorkspaceGroup *>(&workspace))
241 addWorkspaceGroupActions(menu, *wsGroup);
242
243 // Add for all types
245
246 return menu;
247}
248
250 // Show just data for a single value.
251 if (hasSingleValue(workspace)) {
252 menu->addAction(m_showData);
253 return;
254 }
255
256 menu->addMenu(createMatrixWorkspacePlotMenu(menu, hasMultipleBins(workspace)));
257 menu->addSeparator();
258 menu->addAction(m_showData);
259 menu->addAction(m_showAlgorithmHistory);
260 menu->addAction(m_showInstrument);
261 m_showInstrument->setEnabled(workspace.getInstrument() && !workspace.getInstrument()->getName().empty() &&
262 workspace.getAxis(1)->isSpectra());
263 menu->addAction(m_sampleLogs);
264 menu->addAction(m_sliceViewer);
265 menu->addAction(m_showDetectors);
266 if (m_tree->selectedItems().size() == 1) {
267 menu->addAction(m_sampleMaterial);
268 menu->addAction(m_sampleShape);
269 }
270}
271
273 menu->addAction(m_showData);
274 menu->addAction(m_showAlgorithmHistory);
275 if (dynamic_cast<const IPeaksWorkspace *>(&workspace)) {
276 menu->addAction(m_showDetectors);
277 }
278}
279
281 menu->addAction(m_showAlgorithmHistory);
282 menu->addAction(m_sampleLogs);
283
284 // launch slice viewer or plot spectrum conditionally
285 bool addSliceViewer = false;
286 bool add1DPlot = false;
287
288 if (workspace.isMDHistoWorkspace()) {
289 // if the number of non-integral if the number of non-integrated
290 // dimensions is 1.
291 auto num_dims = workspace.getNumNonIntegratedDims();
292 if (num_dims == 1) {
293 // number of non-integral dimension is 1: show menu item to plot
294 // spectrum
295 add1DPlot = true;
296 } else if (num_dims > 1) {
297 // number of non-integral dimension is larger than 1: show menu item
298 // to launch slice view
299 addSliceViewer = true;
300 }
301 } else if (workspace.getNumDims() > 1) {
302 addSliceViewer = true;
303 }
304
305 if (addSliceViewer) {
306 menu->addAction(m_sliceViewer);
307 } else if (add1DPlot) {
308 auto *plotSubMenu = new QMenu("Plot", menu);
309 plotSubMenu->addAction(m_plotMDHisto1D);
310 plotSubMenu->addAction(m_overplotMDHisto1D);
311 plotSubMenu->addAction(m_plotMDHisto1DWithErrs);
312 plotSubMenu->addAction(m_overplotMDHisto1DWithErrs);
313 menu->addMenu(plotSubMenu);
314 }
315}
316
318 auto workspaces = workspace.getAllItems();
319 bool containsMatrixWorkspace{false};
320 bool containsPeaksWorkspace{false};
321 for (const auto &ws : workspaces) {
322 if (std::dynamic_pointer_cast<MatrixWorkspace>(ws)) {
323 containsMatrixWorkspace = true;
324 break;
325 } else if (std::dynamic_pointer_cast<IPeaksWorkspace>(ws)) {
326 containsPeaksWorkspace = true;
327 }
328 }
329
330 // Add plotting options if the group contains at least one matrix
331 // workspace.
332 if (containsMatrixWorkspace) {
333 menu->addMenu(createMatrixWorkspacePlotMenu(menu, true));
334 menu->addSeparator();
335 }
336
337 if (containsMatrixWorkspace || containsPeaksWorkspace) {
338 menu->addAction(m_showDetectors);
339 }
340}
341
343 menu->addSeparator();
344 menu->addAction(m_rename);
345 menu->addAction(m_saveNexus);
346 menu->addSeparator();
347 menu->addAction(m_delete);
348}
349
350QMenu *WorkspaceTreeWidgetSimple::createMatrixWorkspacePlotMenu(QWidget *parent, bool hasMultipleBins) {
351 auto *plotSubMenu = new QMenu("Plot", parent);
352 if (hasMultipleBins) {
353 plotSubMenu->addAction(m_plotSpectrum);
354 plotSubMenu->addAction(m_overplotSpectrum);
355 plotSubMenu->addAction(m_plotSpectrumWithErrs);
356 plotSubMenu->addAction(m_overplotSpectrumWithErrs);
357 plotSubMenu->addAction(m_plotAdvanced);
358 plotSubMenu->addAction(m_superplot);
359 plotSubMenu->addAction(m_superplotWithErrs);
360 plotSubMenu->addSeparator();
361 plotSubMenu->addAction(m_plotColorfill);
362 // 3D
363 auto *plot3DSubMenu = new QMenu("3D", plotSubMenu);
364 plot3DSubMenu->addAction(m_plotSurface);
365 plot3DSubMenu->addAction(m_plotWireframe);
366 plot3DSubMenu->addAction(m_plotContour);
367 plotSubMenu->addMenu(plot3DSubMenu);
368
369 } else {
370 plotSubMenu->addAction(m_plotBin);
371 plotSubMenu->addAction(m_superplotBins);
372 plotSubMenu->addAction(m_superplotBinsWithErrs);
373 plotSubMenu->addSeparator();
374 plotSubMenu->addAction(m_plotColorfill);
375 }
376
377 return plotSubMenu;
378}
379
380} // namespace MantidQt::MantidWidgets
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
std::function< void(QString)> m_doubleClickAction
Action that is executed when a workspace in the tree is double clicked.
void addTableWorkspaceActions(QMenu *menu, const Mantid::API::ITableWorkspace &workspace)
void addMatrixWorkspaceActions(QMenu *menu, const Mantid::API::MatrixWorkspace &workspace)
void overplotMDHistoClicked(const QStringList &workspaceNames)
void plotBinClicked(const QStringList &workspaceNames)
void sliceViewerClicked(const QStringList &workspaceName)
void superplotClicked(const QStringList &workspaceNames)
void plotAdvancedClicked(const QStringList &workspaceNames)
void superplotWithErrsClicked(const QStringList &workspaceNames)
void plotMDHistoWithErrorsClicked(const QStringList &workspaceNames)
void showAlgorithmHistoryClicked(const QStringList &workspaceNames)
void overplotSpectrumClicked(const QStringList &workspaceNames)
void superplotBinsClicked(const QStringList &workspaceNames)
void plotMDHistoClicked(const QStringList &workspaceNames)
void overplotSpectrumWithErrorsClicked(const QStringList &workspaceNames)
QMenu * createWorkspaceContextMenu(const Mantid::API::Workspace &workspace)
Create a new QMenu object filled with appropriate items for the given workspace The created object ha...
void showDataClicked(const QStringList &workspaceNames)
void workspaceDoubleClicked(const QString &workspaceName)
void overplotMDHistoWithErrorsClicked(const QStringList &workspaceNames)
void plotSurfaceClicked(const QStringList &workspaceNames)
void plotSpectrumClicked(const QStringList &workspaceNames)
void sampleMaterialClicked(const QStringList &workspaceNames)
void superplotBinsWithErrsClicked(const QStringList &workspaceNames)
void plotColorfillClicked(const QStringList &workspaceNames)
void sampleLogsClicked(const QStringList &workspaceName)
void plotWireframeClicked(const QStringList &workspaceNames)
void showInstrumentClicked(const QStringList &workspaceNames)
WorkspaceTreeWidgetSimple(bool viewOnly=false, QWidget *parent=nullptr)
void showDetectorsClicked(const QStringList &workspaceNames)
void plotContourClicked(const QStringList &workspaceNames)
void sampleShapeClicked(const QStringList &workspaceNames)
void addMDWorkspaceActions(QMenu *menu, const Mantid::API::IMDWorkspace &workspace)
QMenu * createMatrixWorkspacePlotMenu(QWidget *parent, bool hasMultipleBins)
void plotSpectrumWithErrorsClicked(const QStringList &workspaceNames)
void addWorkspaceGroupActions(QMenu *menu, const Mantid::API::WorkspaceGroup &workspace)
Basic MD Workspace Abstract Class.
Definition: IMDWorkspace.h:40
Interface to the class Mantid::DataObjects::PeaksWorkspace.
ITableWorkspace is an implementation of Workspace in which the data are organised in columns of same ...
Base MatrixWorkspace Abstract Class.
virtual std::size_t blocksize() const =0
Returns the size of each block of data returned by the dataY accessors.
virtual std::size_t getNumberHistograms() const =0
Returns the number of histograms in the workspace.
const HistogramData::HistogramY & y(const size_t index) const
Class to hold a set of workspaces.
Base Workspace Abstract Class.
Definition: Workspace.h:30
Exception for when an item is not found in a collection.
Definition: Exception.h:145
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20