Mantid
Loading...
Searching...
No Matches
AddWorkspaceDialog.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 +
8
14
15#include <QMessageBox>
16#include <limits>
17
19
20using namespace Mantid::API;
21
22namespace {
23MatrixWorkspace_sptr getMatrixWorkspace(const QString &name) {
24 return AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(name.toStdString());
25}
26WorkspaceGroup_sptr getWorkspaceGroup(const QString &name) {
27 return AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(name.toStdString());
28}
29} // namespace
30
33AddWorkspaceDialog::AddWorkspaceDialog(QWidget *parent) : QDialog(parent), m_maxIndex(0) {
34 m_uiForm.setupUi(this);
35 // populate the combo box with names of eligible workspaces
36 QStringList workspaceNames = availableWorkspaces();
37 connect(m_uiForm.cbWorkspaceName, SIGNAL(currentIndexChanged(const QString &)), this,
38 SLOT(workspaceNameChanged(const QString &)));
39 m_uiForm.cbWorkspaceName->addItems(workspaceNames);
40
41 connect(m_uiForm.cbAllSpectra, SIGNAL(stateChanged(int)), this, SLOT(selectAllSpectra(int)));
42 connect(m_uiForm.pbCancel, SIGNAL(clicked()), this, SLOT(handleCancelClicked()));
43 connect(m_uiForm.pbOK, SIGNAL(clicked()), this, SLOT(handleOKClicked()));
44}
45
46std::vector<MatrixWorkspace_const_sptr> AddWorkspaceDialog::getWorkspaces() const {
47 auto const workspaceName = this->workspaceName().trimmed().toStdString();
48 std::vector<MatrixWorkspace_const_sptr> workspaces;
49
51 if (ads.doesExist(workspaceName)) {
52 if (auto const workspace = ads.retrieveWS<MatrixWorkspace>(workspaceName))
53 workspaces.emplace_back(workspace);
54 else if (auto const group = ads.retrieveWS<WorkspaceGroup>(workspaceName))
55 addWorkspacesFromGroup(workspaces, group);
56 }
57 return workspaces;
58}
59
60void AddWorkspaceDialog::addWorkspacesFromGroup(std::vector<MatrixWorkspace_const_sptr> &workspaces,
61 WorkspaceGroup_const_sptr const &group) const {
62 auto const groupSize = static_cast<std::size_t>(group->getNumberOfEntries());
63 for (auto i = 0u; i < groupSize; ++i) {
64 if (auto const workspace = std::dynamic_pointer_cast<MatrixWorkspace>(group->getItem(i))) {
65 workspaces.emplace_back(workspace);
66 }
67 }
68}
69
72void AddWorkspaceDialog::workspaceNameChanged(const QString &wsName) {
73 findCommonMaxIndex(wsName);
74 auto text = m_maxIndex > 0 ? QString("0-%1").arg(m_maxIndex) : "0";
75 if (m_uiForm.cbAllSpectra->isChecked() || m_maxIndex == 0) {
76 m_uiForm.leWSIndices->setText(text);
77 } else {
78 m_uiForm.leWSIndices->clear();
79 m_uiForm.leWSIndices->setPlaceholderText(text);
80 }
81}
82
86 if (state == Qt::Checked) {
87 m_uiForm.leWSIndices->setText(QString("0-%1").arg(m_maxIndex));
88 m_uiForm.leWSIndices->setEnabled(false);
89 } else {
90 m_uiForm.leWSIndices->setEnabled(true);
91 }
92}
93
96 QStringList workspaceNames;
97 auto wsNames = ADS.getObjectNames(Mantid::Kernel::DataServiceSort::Sorted);
98 for (auto &wsName : wsNames) {
99 if (ADS.retrieveWS<Mantid::API::MatrixWorkspace>(wsName)) {
100 workspaceNames << QString::fromStdString(wsName);
101 continue;
102 }
103 auto grp = ADS.retrieveWS<Mantid::API::WorkspaceGroup>(wsName);
104 if (grp) {
105 bool hasMatrixWorkspace = false;
106 for (auto ws : grp->getAllItems()) {
107 if (dynamic_cast<Mantid::API::MatrixWorkspace *>(ws.get())) {
108 hasMatrixWorkspace = true;
109 break;
110 }
111 }
112 if (hasMatrixWorkspace) {
113 workspaceNames << QString::fromStdString(wsName);
114 }
115 }
116 }
117 return workspaceNames;
118}
119
120void AddWorkspaceDialog::findCommonMaxIndex(const QString &wsName) {
121 m_maxIndex = 0;
122 auto mws = getMatrixWorkspace(wsName);
123 if (mws) {
124 m_maxIndex = static_cast<int>(mws->getNumberHistograms()) - 1;
125 } else {
126 auto grp = getWorkspaceGroup(wsName);
127 if (grp) {
128 int maxIndex = std::numeric_limits<int>::max();
129 for (auto ws : grp->getAllItems()) {
130 mws = std::dynamic_pointer_cast<MatrixWorkspace>(ws);
131 if (mws) {
132 maxIndex = std::min(maxIndex, static_cast<int>(mws->getNumberHistograms()) - 1);
133 }
134 }
135 m_maxIndex = maxIndex < std::numeric_limits<int>::max() ? maxIndex : 0;
136 }
137 }
138 if (m_maxIndex < 0) {
139 m_maxIndex = 0;
140 }
141}
142
144 m_workspaceName.clear();
145 m_wsIndices.clear();
146 emit closeDialog();
147}
148
150 m_workspaceName = m_uiForm.cbWorkspaceName->currentText();
151 m_wsIndices.clear();
152 QString indexInput = m_uiForm.leWSIndices->text();
153 if (!m_workspaceName.isEmpty() && !indexInput.isEmpty()) {
154 auto validator = std::make_shared<Mantid::Kernel::ArrayBoundedValidator<int>>(0, m_maxIndex);
155 Mantid::Kernel::ArrayProperty<int> prop("Indices", validator);
156 std::string err = prop.setValue(indexInput.toStdString());
157 if (err.empty()) {
158 m_wsIndices = prop;
159 } else {
160 QMessageBox::warning(this, "Mantid - Error",
161 QString("Some of the indices are outside the allowed range [0,%1]").arg(m_maxIndex));
162 }
163 }
164 if (m_wsIndices.empty()) {
165 QMessageBox::warning(this, "Mantid - Warning", QString("No indices have been selected."));
166 return;
167 }
168
169 emit okClicked(!m_uiForm.ckKeepOpen->isChecked());
170}
171
172} // namespace MantidQt::MantidWidgets
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
std::vector< int > m_wsIndices
Selected workspace index.
void addWorkspacesFromGroup(std::vector< Mantid::API::MatrixWorkspace_const_sptr > &workspaces, Mantid::API::WorkspaceGroup_const_sptr const &group) const
QString m_workspaceName
Name of the selected workspace.
int m_maxIndex
Maximum index in the selected workspace.
std::vector< Mantid::API::MatrixWorkspace_const_sptr > getWorkspaces() const
AddWorkspaceDialog(QWidget *parent=nullptr)
Constructor.
Base MatrixWorkspace Abstract Class.
Class to hold a set of workspaces.
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
std::shared_ptr< const WorkspaceGroup > WorkspaceGroup_const_sptr
shared pointer to Mantid::API::WorkspaceGroup, pointer to const version
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class