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 +
12
14#include "MantidAPI/Axis.h"
22
23#include <QMenu>
24#include <QSignalMapper>
25
26using namespace Mantid::API;
27using namespace Mantid::Kernel;
28
29namespace {
30bool hasSingleValue(const MatrixWorkspace &ws) {
31 return (ws.getNumberHistograms() == 1 && ws.blocksize() == 1 && ws.getNumDims() == 0);
32}
33bool hasMultipleBins(const MatrixWorkspace &ws) {
34 try {
35 return (ws.blocksize() > 1);
36 } catch (...) {
37 const size_t numHist = ws.getNumberHistograms();
38 for (size_t i = 0; i < numHist; ++i) {
39 if (ws.y(i).size() > 1) {
40 return true;
41 }
42 }
43 }
44 return false;
45}
46} // namespace
47
49
51 : WorkspaceTreeWidget(new MantidTreeModel(), viewOnly, parent), m_plotSpectrum(new QAction("Spectrum...", this)),
52 m_plotBin(new QAction("Bin", this)), m_overplotSpectrum(new QAction("Overplot spectrum...", this)),
53 m_plotSpectrumWithErrs(new QAction("Spectrum with errors...", this)),
54 m_overplotSpectrumWithErrs(new QAction("Overplot spectrum with errors...", this)),
55 m_plotColorfill(new QAction("Colorfill", this)), m_sampleLogs(new QAction("Show Sample Logs", this)),
56 m_sliceViewer(new QAction("Show Slice Viewer", this)), m_showInstrument(new QAction("Show Instrument", this)),
57 m_showData(new QAction("Show Data", this)), m_showAlgorithmHistory(new QAction("Show History", this)),
58 m_showDetectors(new QAction("Show Detectors", this)), m_plotAdvanced(new QAction("Advanced...", this)),
59 m_plotSurface(new QAction("Surface", this)), m_plotWireframe(new QAction("Wireframe", this)),
60 m_plotContour(new QAction("Contour", this)), m_plotMDHisto1D(new QAction("Plot 1D MDHistogram...", this)),
61 m_overplotMDHisto1D(new QAction("Overplot 1D MDHistogram...", this)),
62 m_plotMDHisto1DWithErrs(new QAction("Plot 1D MDHistogram with errors...", this)),
63 m_overplotMDHisto1DWithErrs(new QAction("Overplot 1D MDHistogram with errors...", this)),
64 m_sampleMaterial(new QAction("Show Sample Material", this)),
65 m_sampleShape(new QAction("Show Sample Shape", this)), m_superplot(new QAction("Superplot...", this)),
66 m_superplotWithErrs(new QAction("Superplot with errors...", this)),
67 m_superplotBins(new QAction("Superplot bins...", this)),
68 m_superplotBinsWithErrs(new QAction("Superplot bins with errors...", this)),
69 m_showNewInstrumentView(new QAction("(Experimental) Show Instrument", this)) {
70
71 // Replace the double click action on the MantidTreeWidget
72 m_tree->m_doubleClickAction = [&](const QString &wsName) { emit workspaceDoubleClicked(wsName); };
73
74 connect(m_plotSpectrum, SIGNAL(triggered()), this, SLOT(onPlotSpectrumClicked()));
75 // connect event m_plotMDHisto1D to signal slot onPlotMDHistoWorkspaceClicked
76 connect(m_plotMDHisto1D, SIGNAL(triggered()), this, SLOT(onPlotMDHistoWorkspaceClicked()));
77 connect(m_overplotMDHisto1D, SIGNAL(triggered()), this, SLOT(onOverPlotMDHistoWorkspaceClicked()));
78 connect(m_plotMDHisto1DWithErrs, SIGNAL(triggered()), this, SLOT(onPlotMDHistoWorkspaceWithErrorsClicked()));
79 connect(m_overplotMDHisto1DWithErrs, SIGNAL(triggered()), this, SLOT(onOverPlotMDHistoWorkspaceWithErrorsClicked()));
80
81 connect(m_plotBin, SIGNAL(triggered()), this, SLOT(onPlotBinClicked()));
82 connect(m_overplotSpectrum, SIGNAL(triggered()), this, SLOT(onOverplotSpectrumClicked()));
83 connect(m_plotSpectrumWithErrs, SIGNAL(triggered()), this, SLOT(onPlotSpectrumWithErrorsClicked()));
84 connect(m_overplotSpectrumWithErrs, SIGNAL(triggered()), this, SLOT(onOverplotSpectrumWithErrorsClicked()));
85 connect(m_plotColorfill, SIGNAL(triggered()), this, SLOT(onPlotColorfillClicked()));
86 connect(m_sampleLogs, SIGNAL(triggered()), this, SLOT(onSampleLogsClicked()));
87 connect(m_sliceViewer, SIGNAL(triggered()), this, SLOT(onSliceViewerClicked()));
88 connect(m_showInstrument, SIGNAL(triggered()), this, SLOT(onShowInstrumentClicked()));
89 connect(m_showData, SIGNAL(triggered()), this, SLOT(onShowDataClicked()));
90 connect(m_tree, SIGNAL(itemSelectionChanged()), this, SIGNAL(treeSelectionChanged()));
91 connect(m_showAlgorithmHistory, SIGNAL(triggered()), this, SLOT(onShowAlgorithmHistoryClicked()));
92 connect(m_showDetectors, SIGNAL(triggered()), this, SLOT(onShowDetectorsClicked()));
93 connect(m_plotAdvanced, SIGNAL(triggered()), this, SLOT(onPlotAdvancedClicked()));
94 connect(m_plotSurface, SIGNAL(triggered()), this, SLOT(onPlotSurfaceClicked()));
95 connect(m_plotWireframe, SIGNAL(triggered()), this, SLOT(onPlotWireframeClicked()));
96 connect(m_plotContour, SIGNAL(triggered()), this, SLOT(onPlotContourClicked()));
97 connect(m_sampleMaterial, SIGNAL(triggered()), this, SLOT(onSampleMaterialClicked()));
98 connect(m_sampleShape, SIGNAL(triggered()), this, SLOT(onSampleShapeClicked()));
99 connect(m_superplot, SIGNAL(triggered()), this, SLOT(onSuperplotClicked()));
100 connect(m_superplotWithErrs, SIGNAL(triggered()), this, SLOT(onSuperplotWithErrsClicked()));
101 connect(m_superplotBins, SIGNAL(triggered()), this, SLOT(onSuperplotBinsClicked()));
102 connect(m_superplotBinsWithErrs, SIGNAL(triggered()), this, SLOT(onSuperplotBinsWithErrsClicked()));
103 connect(m_showNewInstrumentView, SIGNAL(triggered()), this, SLOT(onShowNewInstrumentViewClicked()));
104}
105
107
109 m_overplotSpectrum->setDisabled(disabled);
110 m_overplotSpectrumWithErrs->setDisabled(disabled);
111}
112
115 QTreeWidgetItem *treeItem = m_tree->itemAt(m_menuPosition);
116 selectedWsName = "";
117 if (treeItem)
118 selectedWsName = treeItem->text(0);
119 else
120 m_tree->selectionModel()->clear();
121
122 QMenu *menu(nullptr);
123 if (selectedWsName.isEmpty()) {
124 // If no workspace is here then have load items
125 menu = m_loadMenu;
126 } else {
127 // Check is defensive just in case the workspace has disappeared
129 try {
130 workspace = AnalysisDataService::Instance().retrieve(selectedWsName.toStdString());
131 } catch (Exception::NotFoundError &) {
132 return;
133 }
135 }
136
137 // Show the menu at the cursor's current position
138 menu->popup(QCursor::pos());
139}
140
144
146
150
154
158
162
164
166
170
172
176
180
184
186
190
192
193// Define signal
197
201
205
209
213
215
217
221
225
229
233
239 auto menu = new QMenu(this);
240 menu->setAttribute(Qt::WA_DeleteOnClose, true);
241 menu->setObjectName("WorkspaceContextMenu");
242
243 if (auto matrixWS = dynamic_cast<const MatrixWorkspace *>(&workspace))
244 addMatrixWorkspaceActions(menu, *matrixWS);
245 else if (auto tableWS = dynamic_cast<const ITableWorkspace *>(&workspace))
246 addTableWorkspaceActions(menu, *tableWS);
247 else if (auto mdWS = dynamic_cast<const IMDWorkspace *>(&workspace))
248 addMDWorkspaceActions(menu, *mdWS);
249 else if (auto wsGroup = dynamic_cast<const WorkspaceGroup *>(&workspace))
250 addWorkspaceGroupActions(menu, *wsGroup);
251
252 // Add for all types
254
255 return menu;
256}
257
259 // Show just data for a single value.
260 if (hasSingleValue(workspace)) {
261 menu->addAction(m_showData);
262 return;
263 }
264
265 menu->addMenu(createMatrixWorkspacePlotMenu(menu, hasMultipleBins(workspace)));
266 menu->addSeparator();
267 menu->addAction(m_showData);
268 menu->addAction(m_showAlgorithmHistory);
269 menu->addAction(m_showInstrument);
270 m_showInstrument->setEnabled(workspace.getInstrument() && !workspace.getInstrument()->getName().empty() &&
271 workspace.getAxis(1)->isSpectra());
272 menu->addAction(m_sampleLogs);
273 menu->addAction(m_sliceViewer);
274 menu->addAction(m_showDetectors);
275 if (m_tree->selectedItems().size() == 1) {
276 menu->addAction(m_sampleMaterial);
277 menu->addAction(m_sampleShape);
278 }
279 menu->addAction(m_showNewInstrumentView);
280 m_showNewInstrumentView->setEnabled(workspace.getInstrument() && !workspace.getInstrument()->getName().empty() &&
281 workspace.getAxis(1)->isSpectra() &&
282 !workspace.detectorInfo().detectorIDs().empty());
283}
284
286 menu->addAction(m_showData);
287 menu->addAction(m_showAlgorithmHistory);
288 if (dynamic_cast<const IPeaksWorkspace *>(&workspace)) {
289 menu->addAction(m_showDetectors);
290 }
291}
292
294 menu->addAction(m_showAlgorithmHistory);
295 menu->addAction(m_sampleLogs);
296
297 // launch slice viewer or plot spectrum conditionally
298 bool addSliceViewer = false;
299 bool add1DPlot = false;
300
301 if (workspace.isMDHistoWorkspace()) {
302 // if the number of non-integral if the number of non-integrated
303 // dimensions is 1.
304 auto num_dims = workspace.getNumNonIntegratedDims();
305 if (num_dims == 1) {
306 // number of non-integral dimension is 1: show menu item to plot
307 // spectrum
308 add1DPlot = true;
309 } else if (num_dims > 1) {
310 // number of non-integral dimension is larger than 1: show menu item
311 // to launch slice view
312 addSliceViewer = true;
313 }
314 } else if (workspace.getNumDims() > 1) {
315 addSliceViewer = true;
316 }
317
318 if (addSliceViewer) {
319 menu->addAction(m_sliceViewer);
320 } else if (add1DPlot) {
321 auto *plotSubMenu = new QMenu("Plot", menu);
322 plotSubMenu->addAction(m_plotMDHisto1D);
323 plotSubMenu->addAction(m_overplotMDHisto1D);
324 plotSubMenu->addAction(m_plotMDHisto1DWithErrs);
325 plotSubMenu->addAction(m_overplotMDHisto1DWithErrs);
326 menu->addMenu(plotSubMenu);
327 }
328}
329
331 auto workspaces = workspace.getAllItems();
332 bool containsMatrixWorkspace{false};
333 bool containsPeaksWorkspace{false};
334 for (const auto &ws : workspaces) {
335 if (std::dynamic_pointer_cast<MatrixWorkspace>(ws)) {
336 containsMatrixWorkspace = true;
337 break;
338 } else if (std::dynamic_pointer_cast<IPeaksWorkspace>(ws)) {
339 containsPeaksWorkspace = true;
340 }
341 }
342
343 // Add plotting options if the group contains at least one matrix
344 // workspace.
345 if (containsMatrixWorkspace) {
346 menu->addMenu(createMatrixWorkspacePlotMenu(menu, true));
347 menu->addSeparator();
348 menu->addAction(m_showDetectors);
349 }
350
351 if (containsPeaksWorkspace) {
352 menu->addAction(m_showData);
353 menu->addAction(m_showDetectors);
354 }
355}
356
358 menu->addSeparator();
359 menu->addAction(m_rename);
360 menu->addAction(m_saveNexus);
361 menu->addSeparator();
362 menu->addAction(m_delete);
363}
364
365QMenu *WorkspaceTreeWidgetSimple::createMatrixWorkspacePlotMenu(QWidget *parent, bool hasMultipleBins) {
366 auto *plotSubMenu = new QMenu("Plot", parent);
367 if (hasMultipleBins) {
368 plotSubMenu->addAction(m_plotSpectrum);
369 plotSubMenu->addAction(m_overplotSpectrum);
370 plotSubMenu->addAction(m_plotSpectrumWithErrs);
371 plotSubMenu->addAction(m_overplotSpectrumWithErrs);
372 plotSubMenu->addAction(m_plotAdvanced);
373 plotSubMenu->addAction(m_superplot);
374 plotSubMenu->addAction(m_superplotWithErrs);
375 plotSubMenu->addSeparator();
376 plotSubMenu->addAction(m_plotColorfill);
377 // 3D
378 auto *plot3DSubMenu = new QMenu("3D", plotSubMenu);
379 plot3DSubMenu->addAction(m_plotSurface);
380 plot3DSubMenu->addAction(m_plotWireframe);
381 plot3DSubMenu->addAction(m_plotContour);
382 plotSubMenu->addMenu(plot3DSubMenu);
383
384 } else {
385 plotSubMenu->addAction(m_plotBin);
386 plotSubMenu->addAction(m_superplotBins);
387 plotSubMenu->addAction(m_superplotBinsWithErrs);
388 plotSubMenu->addSeparator();
389 plotSubMenu->addAction(m_plotColorfill);
390 }
391
392 return plotSubMenu;
393}
394
395} // namespace MantidQt::MantidWidgets
IPeaksWorkspace_sptr workspace
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 showNewInstrumentViewClicked(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.
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.
size_t getNumDims() const override
const HistogramData::HistogramY & y(const size_t index) const
Class to hold a set of workspaces.
Base Workspace Abstract Class.
Definition Workspace.h:29
Exception for when an item is not found in a collection.
Definition Exception.h:145
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace