Mantid
Loading...
Searching...
No Matches
WorkspaceMultiSelector.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//------------------------------------------------------
13
14#include <Poco/AutoPtr.h>
15#include <Poco/Notification.h>
16#include <Poco/NotificationCenter.h>
17
21
22#include <QCompleter>
23#include <QDebug>
24#include <QHeaderView>
25#include <QLineEdit>
26#include <QStyledItemDelegate>
27
28constexpr short namesCol = 0;
29constexpr short indexCol = 1;
31
32namespace {
33
34QStringList headerLabels = {"Workspace Name", "Ws Index"};
35} // namespace
36
43namespace MantidQt {
44namespace MantidWidgets {
45
47 : QTableWidget(parent), m_addObserver(*this, &WorkspaceMultiSelector::handleAddEvent),
48 m_remObserver(*this, &WorkspaceMultiSelector::handleRemEvent),
49 m_clearObserver(*this, &WorkspaceMultiSelector::handleClearEvent),
50 m_renameObserver(*this, &WorkspaceMultiSelector::handleRenameEvent),
51 m_replaceObserver(*this, &WorkspaceMultiSelector::handleReplaceEvent), m_suffix(QStringList()) {
53 refresh();
54}
55
61
66 this->setRowCount(0);
67 this->setColumnCount(headerLabels.size());
68 this->verticalHeader()->setVisible(false);
69 this->horizontalHeader()->setVisible(true);
70 this->setHorizontalHeaderLabels(headerLabels);
71 this->setItemDelegateForColumn(1, new RegexInputDelegate(this, getRegexValidatorString(SpectraValidator)));
72 this->setSelectionMode(QAbstractItemView::ExtendedSelection);
73 this->setSortingEnabled(true);
74 this->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
75}
80 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_addObserver);
81 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_remObserver);
82 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_clearObserver);
83 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_renameObserver);
84 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_replaceObserver);
85}
86
91 Mantid::API::AnalysisDataServiceImpl &ads = Mantid::API::AnalysisDataService::Instance();
92 ads.notificationCenter.addObserver(m_addObserver);
93 ads.notificationCenter.addObserver(m_remObserver);
94 ads.notificationCenter.addObserver(m_renameObserver);
95 ads.notificationCenter.addObserver(m_clearObserver);
97}
98
100 const QTableWidgetItem *item = currentItem();
101 return (item != nullptr);
102}
103
104const QStringList &WorkspaceMultiSelector::getWSSuffixes() const { return m_suffix; }
105
106void WorkspaceMultiSelector::setWSSuffixes(const QStringList &suffix) {
107 m_suffix = suffix;
108 refresh();
109}
110
111void WorkspaceMultiSelector::addItem(const std::string &name) {
112 insertRow(rowCount());
113 auto nameItem = std::make_unique<QTableWidgetItem>(QString::fromStdString(name));
114 auto indexItem = std::make_unique<QTableWidgetItem>(QString::fromStdString(getIndexString(name)));
115
116 nameItem->setFlags(nameItem->flags() & ~Qt::ItemIsEditable);
117
118 setItem(rowCount() - 1, namesCol, nameItem.release());
119 setItem(rowCount() - 1, indexCol, indexItem.release());
120}
121
122void WorkspaceMultiSelector::renameItem(const std::string &newName, int row) {
123 // here is assuming item has already been deemed eligible
124 this->item(row, namesCol)->setText(QString::fromStdString(newName));
125 this->item(row, indexCol)->setText(QString::fromStdString(getIndexString(newName)));
126}
127
128void WorkspaceMultiSelector::addItems(const std::vector<std::string> &names) {
129 for (auto const &name : names) {
131 addItem(name);
132 }
133}
134
136
137 auto selRows = selectionModel()->selectedRows();
138 StringPairVec nameIndexPairVec;
139 nameIndexPairVec.reserve(static_cast<std::size_t>(selRows.size()));
140 for (auto const &index : selRows) {
141 std::string txt = item(index.row(), namesCol)->text().toStdString();
142 if (!txt.empty()) {
143 std::string idx = item(index.row(), indexCol)->text().toStdString();
144 nameIndexPairVec.push_back(std::make_pair(txt, idx));
145 }
146 }
147 nameIndexPairVec.shrink_to_fit();
148 return nameIndexPairVec;
149}
150
152 auto selIndex = this->selectedIndexes();
153 if (!selIndex.isEmpty()) {
154 for (auto &index : selIndex) {
155 std::string selName = this->item(index.row(), namesCol)->text().toStdString();
156 this->item(index.row(), indexCol)->setText(QString::fromStdString(getIndexString(selName)));
157 }
158 }
159}
160
162 auto selIndex = this->selectedIndexes();
163 if (!selIndex.isEmpty()) {
164 auto rangeFirst = this->item(selIndex.takeFirst().row(), indexCol)->text();
165 for (auto &index : selIndex) {
166 this->item(index.row(), indexCol)->setText(rangeFirst);
167 }
168 }
169}
170
172 const std::lock_guard<std::mutex> lock(m_adsMutex);
173 if (checkEligibility(pNf->objectName())) {
174 addItem(pNf->objectName());
175 }
176}
177
179 const std::lock_guard<std::mutex> lock(m_adsMutex);
180 QString name = QString::fromStdString(pNf->objectName());
181 auto items = findItems(name, Qt::MatchExactly);
182 if (!items.isEmpty()) {
183 for (auto &item : items)
184 removeRow(item->row());
185 }
186 if (rowCount() == 0) {
187 emit emptied();
188 }
189}
190
192 const std::lock_guard<std::mutex> lock(m_adsMutex);
193 this->clearContents();
194 while (rowCount() > 0) {
195 removeRow(0);
196 }
197 emit emptied();
198}
199
201 const std::lock_guard<std::mutex> lock(m_adsMutex);
202
203 QString newName = QString::fromStdString(pNf->newObjectName());
204 QString currName = QString::fromStdString(pNf->objectName());
205
206 bool eligible = checkEligibility(pNf->newObjectName());
207 auto currItems = findItems(currName, Qt::MatchExactly);
208 auto newItems = findItems(newName, Qt::MatchExactly);
209
210 if (eligible) {
211 if (!currItems.isEmpty() && newItems.isEmpty())
212 renameItem(pNf->newObjectName(), currItems.first()->row());
213 else if (currItems.isEmpty() && newItems.isEmpty())
214 addItem(pNf->newObjectName());
215 else if (!currItems.isEmpty() && !newItems.isEmpty()) {
216 // list reduction w. redundancies
217 removeRow(currItems.first()->row());
218 renameItem(pNf->newObjectName(), newItems.first()->row());
219 }
220 } else {
221 if (!currItems.isEmpty())
222 removeRow(currItems.first()->row());
223 }
224}
225
227 const std::lock_guard<std::mutex> lock(m_adsMutex);
228 QString name = QString::fromStdString(pNf->objectName());
229 bool eligible = checkEligibility(pNf->objectName());
230 auto items = findItems(name, Qt::MatchExactly);
231
232 if ((eligible && !items.isEmpty()) || (!eligible && items.isEmpty()))
233 return;
234 else if (items.isEmpty() && eligible) {
235 addItem(pNf->objectName());
236 } else { // (inside && !eligible)
237 removeRow(items.first()->row());
238 }
239}
240
241bool WorkspaceMultiSelector::checkEligibility(const std::string &name) const {
242 auto const workspace = Mantid::API::AnalysisDataService::Instance().retrieve(name);
243 if (workspace->isGroup() || !hasValidSuffix(name))
244 return false;
245 return true;
246}
247
248bool WorkspaceMultiSelector::hasValidSuffix(const std::string &name) const {
249 if (m_suffix.isEmpty())
250 return true;
251 if (name.find_last_of("_") < name.size())
252 return m_suffix.contains(QString::fromStdString(name.substr(name.find_last_of("_"))));
253 return false;
254}
255
257 const std::lock_guard<std::mutex> lock(m_adsMutex);
258 this->clearContents();
259 auto const items = Mantid::API::AnalysisDataService::Instance().getObjectNames();
260 addItems(items);
261}
262
266void WorkspaceMultiSelector::focusInEvent(QFocusEvent *) { emit focussed(); }
267
268} // namespace MantidWidgets
269} // namespace MantidQt
std::string name
Definition Run.cpp:60
IPeaksWorkspace_sptr workspace
std::map< DeltaEMode::Type, std::string > index
constexpr short indexCol
constexpr short namesCol
std::vector< std::pair< std::string, std::string > > StringPairVec
This class defines a widget for selecting multiple workspace present in the AnalysisDataService and a...
void connectObservers() const
Subscribes this object to the Poco NotificationCentre.
WorkspaceMultiSelector(QWidget *parent=nullptr)
Default Constructor.
void disconnectObservers() const
De-subscribes this object from the Poco NotificationCentre.
Poco::NObserver< WorkspaceMultiSelector, Mantid::API::ClearADSNotification > m_clearObserver
void renameItem(const std::string &newName, int row)
void handleRemEvent(Mantid::API::WorkspacePostDeleteNotification_ptr pNf)
Poco::NObserver< WorkspaceMultiSelector, Mantid::API::WorkspacePostDeleteNotification > m_remObserver
Poco::NObserver< WorkspaceMultiSelector, Mantid::API::WorkspaceAddNotification > m_addObserver
Poco Observers for ADS Notifications.
void setupTable()
Setup table dimensions and headers from the parent class.
void addItems(const std::vector< std::string > &names)
void handleReplaceEvent(Mantid::API::WorkspaceAfterReplaceNotification_ptr pNf)
void handleAddEvent(Mantid::API::WorkspaceAddNotification_ptr pNf)
void handleClearEvent(Mantid::API::ClearADSNotification_ptr pNf)
void handleRenameEvent(Mantid::API::WorkspaceRenameNotification_ptr pNf)
Poco::NObserver< WorkspaceMultiSelector, Mantid::API::WorkspaceAfterReplaceNotification > m_replaceObserver
Poco::NObserver< WorkspaceMultiSelector, Mantid::API::WorkspaceRenameNotification > m_renameObserver
void focusInEvent(QFocusEvent *) override
Called when there is an interaction with the widget.
The Analysis data service stores instances of the Workspace objects and anything that derives from te...
Poco::NotificationCenter notificationCenter
Sends notifications to observers.
EXPORT_OPT_MANTIDQT_COMMON std::string getIndexString(const std::string &workspaceName)
EXPORT_OPT_MANTIDQT_COMMON std::string getRegexValidatorString(const RegexValidatorStrings &validatorMask)
The AlgorithmProgressDialogPresenter keeps track of the running algorithms and displays a progress ba...
const Poco::AutoPtr< Mantid::Kernel::DataService< Mantid::API::Workspace >::AfterReplaceNotification > & WorkspaceAfterReplaceNotification_ptr
const Poco::AutoPtr< Mantid::Kernel::DataService< Mantid::API::Workspace >::ClearNotification > & ClearADSNotification_ptr
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