Mantid
Loading...
Searching...
No Matches
WorkspaceSelector.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 +
7//------------------------------------------------------
8// Includes
9//------------------------------------------------------
11
12#include <Poco/AutoPtr.h>
13#include <Poco/Notification.h>
14#include <Poco/NotificationCenter.h>
15
19
20#include <QCompleter>
21#include <QDebug>
22#include <QDropEvent>
23#include <QLineEdit>
24#include <QMimeData>
25#include <QUrl>
26using namespace MantidQt::MantidWidgets;
27
34WorkspaceSelector::WorkspaceSelector(QWidget *parent, bool init)
35 : QComboBox(parent), m_addObserver(*this, &WorkspaceSelector::handleAddEvent),
36 m_remObserver(*this, &WorkspaceSelector::handleRemEvent),
37 m_clearObserver(*this, &WorkspaceSelector::handleClearEvent),
38 m_renameObserver(*this, &WorkspaceSelector::handleRenameEvent),
39 m_replaceObserver(*this, &WorkspaceSelector::handleReplaceEvent), m_init(init), m_workspaceTypes(),
40 m_showHidden(false), m_showGroups(true), m_optional(false), m_sorted(false), m_binLimits(std::make_pair(0, -1)),
41 m_suffix(), m_algName(), m_algPropName(), m_algorithm() {
42 setEditable(true);
43 if (init) {
45 }
46 this->setAcceptDrops(true);
47 this->completer()->setCompletionMode(QCompleter::PopupCompletion);
48 this->setInsertPolicy(QComboBox::NoInsert);
49}
50
56
61 if (m_init) {
62 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_addObserver);
63 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_remObserver);
64 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_clearObserver);
65 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_renameObserver);
66 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_replaceObserver);
67 m_init = false;
68 m_connected = false;
69 }
70}
71
76 Mantid::API::AnalysisDataServiceImpl &ads = Mantid::API::AnalysisDataService::Instance();
77 ads.notificationCenter.addObserver(m_addObserver);
78 ads.notificationCenter.addObserver(m_remObserver);
79 ads.notificationCenter.addObserver(m_renameObserver);
80 ads.notificationCenter.addObserver(m_clearObserver);
82 refresh();
83 m_init = true;
84 m_connected = true;
85}
86
87const QStringList &WorkspaceSelector::getWorkspaceTypes() const { return m_workspaceTypes; }
88
89void WorkspaceSelector::setWorkspaceTypes(const QStringList &types) {
90 if (types != m_workspaceTypes) {
91 m_workspaceTypes = types;
92 if (m_init) {
93 refresh();
94 }
95 }
96}
97
99
101 if (show != m_showHidden) {
102 m_showHidden = show;
103 if (m_init) {
104 refresh();
105 }
106 }
107}
108
110
112 if (show != m_showGroups) {
113 m_showGroups = show;
114 if (m_init) {
115 refresh();
116 }
117 }
118}
119
120bool WorkspaceSelector::isValid() const { return (this->currentText() != ""); }
121
123
125 if (optional != m_optional) {
126 m_optional = optional;
127 if (m_init)
128 refresh();
129 }
130}
131
132bool WorkspaceSelector::isSorted() const { return m_sorted; }
133
135 if (sorted != m_sorted) {
136 m_sorted = sorted;
137 if (m_init)
138 refresh();
139 }
140}
141
143
144const QStringList &WorkspaceSelector::getSuffixes() const { return m_suffix; }
145
146void WorkspaceSelector::setSuffixes(const QStringList &suffix) {
147 if (suffix != m_suffix) {
148 m_suffix = suffix;
149 if (m_init) {
150 refresh();
151 }
152 }
153}
154
155void WorkspaceSelector::setLowerBinLimit(int numberOfBins) { m_binLimits.first = numberOfBins; }
156
157void WorkspaceSelector::setUpperBinLimit(int numberOfBins) { m_binLimits.second = numberOfBins; }
158
159const QString &WorkspaceSelector::getValidatingAlgorithm() const { return m_algName; }
160
161void WorkspaceSelector::setValidatingAlgorithm(const QString &algName) {
162 if (algName == m_algName) {
163 return;
164 }
165 m_algName = algName;
166 if (m_init) {
167 m_algorithm = Mantid::API::AlgorithmManager::Instance().createUnmanaged(algName.toStdString());
168 m_algorithm->initialize();
169 std::vector<Mantid::Kernel::Property *> props = m_algorithm->getProperties();
170 for (auto &prop : props) {
171 if (prop->direction() == Mantid::Kernel::Direction::Input) {
172 // try to cast property to WorkspaceProperty
173 const Mantid::API::WorkspaceProperty<> *wsProp = dynamic_cast<Mantid::API::WorkspaceProperty<> *>(prop);
174 if (wsProp != nullptr) {
175 m_algPropName = QString::fromStdString(prop->name());
176 break;
177 }
178 }
179 }
180 refresh();
181 }
182}
183
185 const std::lock_guard<std::mutex> lock(m_adsMutex);
186 if (!showHiddenWorkspaces() &&
187 Mantid::API::AnalysisDataService::Instance().isHiddenDataServiceObject(pNf->objectName())) {
188 return;
189 }
190 QString name = QString::fromStdString(pNf->objectName());
191 if (checkEligibility(name, pNf->object())) {
192
193 addItem(name);
194 if (isSorted()) {
195 model()->sort(0);
196 }
197 }
198}
199
201 const std::lock_guard<std::mutex> lock(m_adsMutex);
202 QString name = QString::fromStdString(pNf->objectName());
203 int index = findText(name);
204 if (index != -1) {
206 }
207 if (currentIndex() == -1) {
208 emit emptied();
209 }
210}
211
213 const std::lock_guard<std::mutex> lock(m_adsMutex);
214 this->clear();
215 if (m_optional)
216 addItem("");
217 emit emptied();
218}
219
221 const std::lock_guard<std::mutex> lock(m_adsMutex);
222 QString name = QString::fromStdString(pNf->objectName());
223 QString newName = QString::fromStdString(pNf->newObjectName());
224 auto &ads = Mantid::API::AnalysisDataService::Instance();
225
226 bool eligible = checkEligibility(newName, ads.retrieve(pNf->newObjectName()));
227 int index = findText(name);
228 int newIndex = findText(newName);
229
230 if (eligible) {
231 if (index != -1 && newIndex == -1) {
232 this->setItemText(index, newName);
233 if (isSorted()) {
234 model()->sort(0);
235 }
236 } else if (index == -1 && newIndex == -1) {
237 addItem(newName);
238 if (isSorted()) {
239 model()->sort(0);
240 }
241 } else
243 } else {
244 if (index != -1) {
246 }
247 }
248}
249
251 const std::lock_guard<std::mutex> lock(m_adsMutex);
252 QString name = QString::fromStdString(pNf->objectName());
253 auto &ads = Mantid::API::AnalysisDataService::Instance();
254
255 bool eligible = checkEligibility(name, ads.retrieve(pNf->objectName()));
256 int index = findText(name);
257
258 // if it is inside and it is eligible do nothing
259 // if it is not inside and it is eligible insert
260 // if it is inside and it is not eligible remove
261 // if it is not inside and it is not eligible do nothing
262 bool inside = (index != -1);
263 if ((inside && eligible) || (!inside && !eligible))
264 return;
265 else if (!inside && eligible) {
266 addItem(name);
267 if (isSorted()) {
268 model()->sort(0);
269 }
270 } else // (inside && !eligible)
272}
273
275 if (m_algorithm && !m_algPropName.isEmpty()) {
276 try {
277 m_algorithm->setPropertyValue(m_algPropName.toStdString(), name.toStdString());
278 } catch (std::invalid_argument &) {
279 return false;
280 }
281 } else if ((!m_workspaceTypes.empty()) && m_workspaceTypes.indexOf(QString::fromStdString(object->id())) == -1) {
282 return false;
283 } else if (!hasValidSuffix(name)) {
284 return false;
285 } else if (!hasValidNumberOfBins(object)) {
286 return false;
287 } else if (!m_showGroups) {
288 auto group = std::dynamic_pointer_cast<Mantid::API::WorkspaceGroup>(object);
289 if (group != nullptr)
290 return false;
291 }
292
293 return true;
294}
295
296bool WorkspaceSelector::hasValidSuffix(const QString &name) const {
297 if (m_suffix.isEmpty()) {
298 return true;
299 } else {
300 for (int i = 0; i < m_suffix.size(); ++i) {
301 if (name.endsWith(m_suffix[i])) {
302 return true;
303 }
304 }
305 }
306
307 return false;
308}
309
311 if (m_binLimits.first != 0 || m_binLimits.second != -1) {
312 if (auto const workspace = std::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>(object)) {
313 auto const numberOfBins = static_cast<int>(workspace->y(0).size());
314 if (m_binLimits.second != -1)
315 return numberOfBins >= m_binLimits.first && numberOfBins <= m_binLimits.second;
316 return numberOfBins >= m_binLimits.first;
317 }
318 }
319 return true;
320}
321
323 const std::lock_guard<std::mutex> lock(m_adsMutex);
324 clear();
325 if (m_optional)
326 addItem("");
327 auto &ads = Mantid::API::AnalysisDataService::Instance();
328 std::vector<std::string> items;
329 if (showHiddenWorkspaces()) {
331 } else {
332 items = ads.getObjectNames();
333 }
334
335 QStringList namesToAdd;
336 for (auto &item : items) {
337 QString name = QString::fromStdString(item);
338 if (checkEligibility(name, ads.retrieve(item))) {
339 namesToAdd << name;
340 }
341 }
342 this->addItems(namesToAdd);
343 if (isSorted()) {
344 model()->sort(0);
345 }
346}
347
352void WorkspaceSelector::dropEvent(QDropEvent *de) {
353 const std::lock_guard<std::mutex> lock(m_adsMutex);
354 const QMimeData *mimeData = de->mimeData();
355 QString text = mimeData->text();
356 int equal_pos = text.indexOf("=");
357 QString ws_name = text.left(equal_pos - 1);
358 QString ws_name_test = text.mid(equal_pos + 7, equal_pos - 1);
359
360 if (ws_name == ws_name_test) {
361 int index = findText(ws_name);
362 if (index >= 0) {
363 setCurrentIndex(index);
364 de->acceptProposedAction();
365 }
366 }
367}
368
373void WorkspaceSelector::dragEnterEvent(QDragEnterEvent *de) {
374 const std::lock_guard<std::mutex> lock(m_adsMutex);
375 const QMimeData *mimeData = de->mimeData();
376 if (mimeData->hasText()) {
377 QString text = mimeData->text();
378 if (text.contains(" = mtd[\""))
379 de->acceptProposedAction();
380 }
381}
382
386void WorkspaceSelector::focusInEvent(QFocusEvent *) { emit focussed(); }
std::string name
Definition Run.cpp:60
IPeaksWorkspace_sptr workspace
std::map< DeltaEMode::Type, std::string > index
void removeItem(WorkspaceGroup &self, const std::string &name)
void addItem(WorkspaceGroup &self, const std::string &name)
This class defines a widget for selecting a workspace present in the AnalysisDataService.
bool m_showHidden
Whether to show "hidden" workspaces.
Poco::NObserver< WorkspaceSelector, Mantid::API::WorkspacePostDeleteNotification > m_remObserver
bool checkEligibility(const QString &name, const Mantid::API::Workspace_sptr &object) const
void connectObservers()
Subscribes this object to the Poco NotificationCentre.
void setWorkspaceTypes(const QStringList &types)
void dragEnterEvent(QDragEnterEvent *) override
Called when an item is dragged onto a control.
void handleAddEvent(Mantid::API::WorkspaceAddNotification_ptr pNf)
Poco::NObserver< WorkspaceSelector, Mantid::API::WorkspaceRenameNotification > m_renameObserver
bool hasValidSuffix(const QString &name) const
std::pair< int, int > m_binLimits
Allows you to put limits on the size of the workspace i.e. number of bins.
std::shared_ptr< Mantid::API::Algorithm > m_algorithm
void handleRenameEvent(Mantid::API::WorkspaceRenameNotification_ptr pNf)
bool m_optional
Whether to add an extra empty entry to the combobox.
void setValidatingAlgorithm(const QString &algName)
Poco::NObserver< WorkspaceSelector, Mantid::API::WorkspaceAfterReplaceNotification > m_replaceObserver
QStringList m_workspaceTypes
A list of workspace types that should be shown in the ComboBox.
void setSuffixes(const QStringList &suffix)
void handleRemEvent(Mantid::API::WorkspacePostDeleteNotification_ptr pNf)
void disconnectObservers()
De-subscribes this object from the Poco NotificationCentre.
bool m_sorted
Whetherthe combobox model should be kept sorted.
void focusInEvent(QFocusEvent *) override
Called when there is an interaction with the widget.
Poco::NObserver< WorkspaceSelector, Mantid::API::ClearADSNotification > m_clearObserver
Poco::NObserver< WorkspaceSelector, Mantid::API::WorkspaceAddNotification > m_addObserver
Poco Observers for ADS Notifications.
WorkspaceSelector(QWidget *parent=nullptr, bool init=true)
Default Constructor.
void dropEvent(QDropEvent *) override
Called when an item is dropped.
void handleClearEvent(Mantid::API::ClearADSNotification_ptr pNf)
bool hasValidNumberOfBins(const Mantid::API::Workspace_sptr &object) const
void handleReplaceEvent(Mantid::API::WorkspaceAfterReplaceNotification_ptr pNf)
The Analysis data service stores instances of the Workspace objects and anything that derives from te...
A property class for workspaces.
Poco::NotificationCenter notificationCenter
Sends notifications to observers.
const Poco::AutoPtr< Mantid::Kernel::DataService< Mantid::API::Workspace >::AfterReplaceNotification > & WorkspaceAfterReplaceNotification_ptr
const Poco::AutoPtr< Mantid::Kernel::DataService< Mantid::API::Workspace >::ClearNotification > & ClearADSNotification_ptr
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
const Poco::AutoPtr< Mantid::Kernel::DataService< Mantid::API::Workspace >::RenameNotification > & WorkspaceRenameNotification_ptr
const Poco::AutoPtr< Mantid::Kernel::DataService< Mantid::API::Workspace >::AddNotification > & WorkspaceAddNotification_ptr
const Poco::AutoPtr< Mantid::Kernel::DataService< Mantid::API::Workspace >::PostDeleteNotification > & WorkspacePostDeleteNotification_ptr
STL namespace.
@ Input
An input workspace.
Definition Property.h:53