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/NObserver.h>
14#include <Poco/Notification.h>
15#include <Poco/NotificationCenter.h>
16
20
21#include <QCompleter>
22#include <QDebug>
23#include <QDropEvent>
24#include <QLineEdit>
25#include <QMimeData>
26#include <QUrl>
27using namespace MantidQt::MantidWidgets;
28
35WorkspaceSelector::WorkspaceSelector(QWidget *parent, bool init)
36 : QComboBox(parent), m_addObserver(*this, &WorkspaceSelector::handleAddEvent),
37 m_remObserver(*this, &WorkspaceSelector::handleRemEvent),
38 m_clearObserver(*this, &WorkspaceSelector::handleClearEvent),
39 m_renameObserver(*this, &WorkspaceSelector::handleRenameEvent),
40 m_replaceObserver(*this, &WorkspaceSelector::handleReplaceEvent), m_init(init), m_workspaceTypes(),
41 m_showHidden(false), m_showGroups(true), m_optional(false), m_sorted(false), m_binLimits(std::make_pair(0, -1)),
42 m_suffix(), m_algName(), m_algPropName(), m_algorithm() {
43 setEditable(true);
44 if (init) {
46 }
47 this->setAcceptDrops(true);
48 this->completer()->setCompletionMode(QCompleter::PopupCompletion);
49 this->setInsertPolicy(QComboBox::NoInsert);
50}
51
57
62 if (m_init) {
63 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_addObserver);
64 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_remObserver);
65 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_clearObserver);
66 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_renameObserver);
67 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_replaceObserver);
68 m_init = false;
69 m_connected = false;
70 }
71}
72
78 ads.notificationCenter.addObserver(m_addObserver);
79 ads.notificationCenter.addObserver(m_remObserver);
80 ads.notificationCenter.addObserver(m_renameObserver);
81 ads.notificationCenter.addObserver(m_clearObserver);
83 refresh();
84 m_init = true;
85 m_connected = true;
86}
87
89
90void WorkspaceSelector::setWorkspaceTypes(const QStringList &types) {
91 if (types != m_workspaceTypes) {
92 m_workspaceTypes = types;
93 if (m_init) {
94 refresh();
95 }
96 }
97}
98
100
102 if (show != m_showHidden) {
103 m_showHidden = show;
104 if (m_init) {
105 refresh();
106 }
107 }
108}
109
111
113 if (show != m_showGroups) {
114 m_showGroups = show;
115 if (m_init) {
116 refresh();
117 }
118 }
119}
120
121bool WorkspaceSelector::isValid() const { return (this->currentText() != ""); }
122
124
126 if (optional != m_optional) {
127 m_optional = optional;
128 if (m_init)
129 refresh();
130 }
131}
132
133bool WorkspaceSelector::isSorted() const { return m_sorted; }
134
136 if (sorted != m_sorted) {
137 m_sorted = sorted;
138 if (m_init)
139 refresh();
140 }
141}
142
144
145QStringList WorkspaceSelector::getSuffixes() const { return m_suffix; }
146
147void WorkspaceSelector::setSuffixes(const QStringList &suffix) {
148 if (suffix != m_suffix) {
149 m_suffix = suffix;
150 if (m_init) {
151 refresh();
152 }
153 }
154}
155
156void WorkspaceSelector::setLowerBinLimit(int numberOfBins) { m_binLimits.first = numberOfBins; }
157
158void WorkspaceSelector::setUpperBinLimit(int numberOfBins) { m_binLimits.second = numberOfBins; }
159
161
162void WorkspaceSelector::setValidatingAlgorithm(const QString &algName) {
163 if (algName == m_algName) {
164 return;
165 }
166 m_algName = algName;
167 if (m_init) {
168 m_algorithm = Mantid::API::AlgorithmManager::Instance().createUnmanaged(algName.toStdString());
169 m_algorithm->initialize();
170 std::vector<Mantid::Kernel::Property *> props = m_algorithm->getProperties();
171 for (auto &prop : props) {
172 if (prop->direction() == Mantid::Kernel::Direction::Input) {
173 // try to cast property to WorkspaceProperty
175 if (wsProp != nullptr) {
176 m_algPropName = QString::fromStdString(prop->name());
177 break;
178 }
179 }
180 }
181 refresh();
182 }
183}
184
186 const std::lock_guard<std::mutex> lock(m_adsMutex);
187 if (!showHiddenWorkspaces() &&
188 Mantid::API::AnalysisDataService::Instance().isHiddenDataServiceObject(pNf->objectName())) {
189 return;
190 }
191 QString name = QString::fromStdString(pNf->objectName());
192 if (checkEligibility(name, pNf->object())) {
193
194 addItem(name);
195 if (isSorted()) {
196 model()->sort(0);
197 }
198 }
199}
200
202 const std::lock_guard<std::mutex> lock(m_adsMutex);
203 QString name = QString::fromStdString(pNf->objectName());
204 int index = findText(name);
205 if (index != -1) {
207 }
208 if (currentIndex() == -1) {
209 emit emptied();
210 }
211}
212
214 const std::lock_guard<std::mutex> lock(m_adsMutex);
215 this->clear();
216 if (m_optional)
217 addItem("");
218 emit emptied();
219}
220
222 const std::lock_guard<std::mutex> lock(m_adsMutex);
223 QString name = QString::fromStdString(pNf->objectName());
224 QString newName = QString::fromStdString(pNf->newObjectName());
226
227 bool eligible = checkEligibility(newName, ads.retrieve(pNf->newObjectName()));
228 int index = findText(name);
229 int newIndex = findText(newName);
230
231 if (eligible) {
232 if (index != -1 && newIndex == -1) {
233 this->setItemText(index, newName);
234 if (isSorted()) {
235 model()->sort(0);
236 }
237 } else if (index == -1 && newIndex == -1) {
238 addItem(newName);
239 if (isSorted()) {
240 model()->sort(0);
241 }
242 } else
244 } else {
245 if (index != -1) {
247 }
248 }
249}
250
252 const std::lock_guard<std::mutex> lock(m_adsMutex);
253 QString name = QString::fromStdString(pNf->objectName());
255
256 bool eligible = checkEligibility(name, ads.retrieve(pNf->objectName()));
257 int index = findText(name);
258
259 // if it is inside and it is eligible do nothing
260 // if it is not inside and it is eligible insert
261 // if it is inside and it is not eligible remove
262 // if it is not inside and it is not eligible do nothing
263 bool inside = (index != -1);
264 if ((inside && eligible) || (!inside && !eligible))
265 return;
266 else if (!inside && eligible) {
267 addItem(name);
268 if (isSorted()) {
269 model()->sort(0);
270 }
271 } else // (inside && !eligible)
273}
274
275bool WorkspaceSelector::checkEligibility(const QString &name, const Mantid::API::Workspace_sptr &object) const {
276 if (m_algorithm && !m_algPropName.isEmpty()) {
277 try {
278 m_algorithm->setPropertyValue(m_algPropName.toStdString(), name.toStdString());
279 } catch (std::invalid_argument &) {
280 return false;
281 }
282 } else if ((!m_workspaceTypes.empty()) && m_workspaceTypes.indexOf(QString::fromStdString(object->id())) == -1) {
283 return false;
284 } else if (!hasValidSuffix(name)) {
285 return false;
286 } else if (!hasValidNumberOfBins(object)) {
287 return false;
288 } else if (!m_showGroups) {
289 auto group = std::dynamic_pointer_cast<Mantid::API::WorkspaceGroup>(object);
290 if (group != nullptr)
291 return false;
292 }
293
294 return true;
295}
296
297bool WorkspaceSelector::hasValidSuffix(const QString &name) const {
298 if (m_suffix.isEmpty()) {
299 return true;
300 } else {
301 for (int i = 0; i < m_suffix.size(); ++i) {
302 if (name.endsWith(m_suffix[i])) {
303 return true;
304 }
305 }
306 }
307
308 return false;
309}
310
312 if (m_binLimits.first != 0 || m_binLimits.second != -1) {
313 if (auto const workspace = std::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>(object)) {
314 auto const numberOfBins = static_cast<int>(workspace->y(0).size());
315 if (m_binLimits.second != -1)
316 return numberOfBins >= m_binLimits.first && numberOfBins <= m_binLimits.second;
317 return numberOfBins >= m_binLimits.first;
318 }
319 }
320 return true;
321}
322
324 const std::lock_guard<std::mutex> lock(m_adsMutex);
325 clear();
326 if (m_optional)
327 addItem("");
329 std::vector<std::string> items;
330 if (showHiddenWorkspaces()) {
332 } else {
333 items = ads.getObjectNames();
334 }
335
336 QStringList namesToAdd;
337 for (auto &item : items) {
338 QString name = QString::fromStdString(item);
339 if (checkEligibility(name, ads.retrieve(item))) {
340 namesToAdd << name;
341 }
342 }
343 this->addItems(namesToAdd);
344 if (isSorted()) {
345 model()->sort(0);
346 }
347}
348
353void WorkspaceSelector::dropEvent(QDropEvent *de) {
354 const std::lock_guard<std::mutex> lock(m_adsMutex);
355 const QMimeData *mimeData = de->mimeData();
356 QString text = mimeData->text();
357 int equal_pos = text.indexOf("=");
358 QString ws_name = text.left(equal_pos - 1);
359 QString ws_name_test = text.mid(equal_pos + 7, equal_pos - 1);
360
361 if (ws_name == ws_name_test) {
362 int index = findText(ws_name);
363 if (index >= 0) {
364 setCurrentIndex(index);
365 de->acceptProposedAction();
366 }
367 }
368}
369
374void WorkspaceSelector::dragEnterEvent(QDragEnterEvent *de) {
375 const std::lock_guard<std::mutex> lock(m_adsMutex);
376 const QMimeData *mimeData = de->mimeData();
377 if (mimeData->hasText()) {
378 QString text = mimeData->text();
379 if (text.contains(" = mtd[\""))
380 de->acceptProposedAction();
381 }
382}
383
387void WorkspaceSelector::focusInEvent(QFocusEvent *) { emit focussed(); }
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
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.
Definition: DataService.h:484
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
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
Definition: Workspace_fwd.h:20
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