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/NObserver.h>
16#include <Poco/Notification.h>
17#include <Poco/NotificationCenter.h>
18
22
23#include <QCompleter>
24#include <QDebug>
25#include <QHeaderView>
26#include <QLineEdit>
27#include <QStyledItemDelegate>
28
29constexpr short namesCol = 0;
30constexpr short indexCol = 1;
32
33namespace {
34
35QStringList headerLabels = {"Workspace Name", "Ws Index"};
36} // namespace
37
44namespace MantidQt {
45namespace MantidWidgets {
46
48 : QTableWidget(parent), m_addObserver(*this, &WorkspaceMultiSelector::handleAddEvent),
49 m_remObserver(*this, &WorkspaceMultiSelector::handleRemEvent),
50 m_clearObserver(*this, &WorkspaceMultiSelector::handleClearEvent),
51 m_renameObserver(*this, &WorkspaceMultiSelector::handleRenameEvent),
52 m_replaceObserver(*this, &WorkspaceMultiSelector::handleReplaceEvent), m_suffix(QStringList()) {
54 refresh();
55}
56
62
67 this->setRowCount(0);
68 this->setColumnCount(headerLabels.size());
69 this->verticalHeader()->setVisible(false);
70 this->horizontalHeader()->setVisible(true);
71 this->setHorizontalHeaderLabels(headerLabels);
72 this->setItemDelegateForColumn(1, new RegexInputDelegate(this, getRegexValidatorString(SpectraValidator)));
73 this->setSelectionMode(QAbstractItemView::ExtendedSelection);
74 this->setSortingEnabled(true);
75 this->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
76}
81 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_addObserver);
82 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_remObserver);
83 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_clearObserver);
84 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_renameObserver);
85 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_replaceObserver);
86}
87
92 Mantid::API::AnalysisDataServiceImpl &ads = Mantid::API::AnalysisDataService::Instance();
93 ads.notificationCenter.addObserver(m_addObserver);
94 ads.notificationCenter.addObserver(m_remObserver);
95 ads.notificationCenter.addObserver(m_renameObserver);
96 ads.notificationCenter.addObserver(m_clearObserver);
98}
99
101 const QTableWidgetItem *item = currentItem();
102 return (item != nullptr);
103}
104
105const QStringList &WorkspaceMultiSelector::getWSSuffixes() const { return m_suffix; }
106
107void WorkspaceMultiSelector::setWSSuffixes(const QStringList &suffix) {
108 m_suffix = suffix;
109 refresh();
110}
111
112void WorkspaceMultiSelector::addItem(const std::string &name) {
113 insertRow(rowCount());
114 auto nameItem = std::make_unique<QTableWidgetItem>(QString::fromStdString(name));
115 auto indexItem = std::make_unique<QTableWidgetItem>(QString::fromStdString(getIndexString(name)));
116
117 nameItem->setFlags(nameItem->flags() & ~Qt::ItemIsEditable);
118
119 setItem(rowCount() - 1, namesCol, nameItem.release());
120 setItem(rowCount() - 1, indexCol, indexItem.release());
121}
122
123void WorkspaceMultiSelector::renameItem(const std::string &newName, int row) {
124 // here is assuming item has already been deemed eligible
125 this->item(row, namesCol)->setText(QString::fromStdString(newName));
126 this->item(row, indexCol)->setText(QString::fromStdString(getIndexString(newName)));
127}
128
129void WorkspaceMultiSelector::addItems(const std::vector<std::string> &names) {
130 for (auto const &name : names) {
132 addItem(name);
133 }
134}
135
137
138 auto selRows = selectionModel()->selectedRows();
139 StringPairVec nameIndexPairVec;
140 nameIndexPairVec.reserve(static_cast<std::size_t>(selRows.size()));
141 for (auto const &index : selRows) {
142 std::string txt = item(index.row(), namesCol)->text().toStdString();
143 if (!txt.empty()) {
144 std::string idx = item(index.row(), indexCol)->text().toStdString();
145 nameIndexPairVec.push_back(std::make_pair(txt, idx));
146 }
147 }
148 nameIndexPairVec.shrink_to_fit();
149 return nameIndexPairVec;
150}
151
153 auto selIndex = this->selectedIndexes();
154 if (!selIndex.isEmpty()) {
155 for (auto &index : selIndex) {
156 std::string selName = this->item(index.row(), namesCol)->text().toStdString();
157 this->item(index.row(), indexCol)->setText(QString::fromStdString(getIndexString(selName)));
158 }
159 }
160}
161
163 auto selIndex = this->selectedIndexes();
164 if (!selIndex.isEmpty()) {
165 auto rangeFirst = this->item(selIndex.takeFirst().row(), indexCol)->text();
166 for (auto &index : selIndex) {
167 this->item(index.row(), indexCol)->setText(rangeFirst);
168 }
169 }
170}
171
173 const std::lock_guard<std::mutex> lock(m_adsMutex);
174 if (checkEligibility(pNf->objectName())) {
175 addItem(pNf->objectName());
176 }
177}
178
180 const std::lock_guard<std::mutex> lock(m_adsMutex);
181 QString name = QString::fromStdString(pNf->objectName());
182 auto items = findItems(name, Qt::MatchExactly);
183 if (!items.isEmpty()) {
184 for (auto &item : items)
185 removeRow(item->row());
186 }
187 if (rowCount() == 0) {
188 emit emptied();
189 }
190}
191
193 const std::lock_guard<std::mutex> lock(m_adsMutex);
194 this->clearContents();
195 while (rowCount() > 0) {
196 removeRow(0);
197 }
198 emit emptied();
199}
200
202 const std::lock_guard<std::mutex> lock(m_adsMutex);
203
204 QString newName = QString::fromStdString(pNf->newObjectName());
205 QString currName = QString::fromStdString(pNf->objectName());
206
207 bool eligible = checkEligibility(pNf->newObjectName());
208 auto currItems = findItems(currName, Qt::MatchExactly);
209 auto newItems = findItems(newName, Qt::MatchExactly);
210
211 if (eligible) {
212 if (!currItems.isEmpty() && newItems.isEmpty())
213 renameItem(pNf->newObjectName(), currItems.first()->row());
214 else if (currItems.isEmpty() && newItems.isEmpty())
215 addItem(pNf->newObjectName());
216 else if (!currItems.isEmpty() && !newItems.isEmpty()) {
217 // list reduction w. redundancies
218 removeRow(currItems.first()->row());
219 renameItem(pNf->newObjectName(), newItems.first()->row());
220 }
221 } else {
222 if (!currItems.isEmpty())
223 removeRow(currItems.first()->row());
224 }
225}
226
228 const std::lock_guard<std::mutex> lock(m_adsMutex);
229 QString name = QString::fromStdString(pNf->objectName());
230 bool eligible = checkEligibility(pNf->objectName());
231 auto items = findItems(name, Qt::MatchExactly);
232
233 if ((eligible && !items.isEmpty()) || (!eligible && items.isEmpty()))
234 return;
235 else if (items.isEmpty() && eligible) {
236 addItem(pNf->objectName());
237 } else { // (inside && !eligible)
238 removeRow(items.first()->row());
239 }
240}
241
242bool WorkspaceMultiSelector::checkEligibility(const std::string &name) const {
243 auto const workspace = Mantid::API::AnalysisDataService::Instance().retrieve(name);
244 if (workspace->isGroup() || !hasValidSuffix(name))
245 return false;
246 return true;
247}
248
249bool WorkspaceMultiSelector::hasValidSuffix(const std::string &name) const {
250 if (m_suffix.isEmpty())
251 return true;
252 if (name.find_last_of("_") < name.size())
253 return m_suffix.contains(QString::fromStdString(name.substr(name.find_last_of("_"))));
254 return false;
255}
256
258 const std::lock_guard<std::mutex> lock(m_adsMutex);
259 this->clearContents();
260 auto const items = Mantid::API::AnalysisDataService::Instance().getObjectNames();
261 addItems(items);
262}
263
267void WorkspaceMultiSelector::focusInEvent(QFocusEvent *) { emit focussed(); }
268
269} // namespace MantidWidgets
270} // 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