Mantid
Loading...
Searching...
No Matches
InstrumentSelector.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 +
12#include "MantidKernel/Logger.h"
13
14#include <set>
15
16namespace {
17Mantid::Kernel::Logger g_log("InstrumentSelector");
18}
19
21using namespace Mantid::Kernel;
22
29InstrumentSelector::InstrumentSelector(QWidget *parent, bool init)
30 : QComboBox(parent), m_changeObserver(*this, &InstrumentSelector::handleConfigChange), m_techniques(),
31 m_currentFacility(nullptr), m_init(init), m_storeChanges(false), m_updateOnFacilityChange(true),
32 m_selectedInstrument() {
33 setEditable(false);
34
35 if (init) {
37
40 }
41
42 connect(this, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(updateInstrument(const QString &)));
43}
44
50 if (m_init) {
52 }
53}
54
60QStringList InstrumentSelector::getTechniques() const { return m_techniques; }
61
68
73void InstrumentSelector::setAutoUpdate(bool autoUpdate) { m_updateOnFacilityChange = autoUpdate; }
74
80void InstrumentSelector::setTechniques(const QStringList &techniques) {
82 if (count() > 0 && m_currentFacility) {
84 }
85}
86
91QString InstrumentSelector::getFacility() { return QString::fromStdString(m_currentFacility->name()); }
92
97void InstrumentSelector::setFacility(const QString &facilityName) { fillWithInstrumentsFromFacility(facilityName); }
98
101 return;
102
103 QString prop = QString::fromStdString(pNf->key());
104 QString newV = QString::fromStdString(pNf->curValue());
105 QString oldV = QString::fromStdString(pNf->preValue());
106
107 if (newV != oldV) {
108 if ((prop == "default.facility") && (newV != QString::fromStdString(m_currentFacility->name()))) {
110 } else if ((prop == "default.instrument") && (newV != this->currentText())) {
111 this->setCurrentIndex(this->findText(newV));
112 }
113 }
114}
115
116//------------------------------------------------------
117// Public slot member functions
118//------------------------------------------------------
119
127 const ConfigServiceImpl &mantidSettings = ConfigService::Instance();
128
129 this->blockSignals(true);
130 this->clear();
131
132 try {
133 if (name.isEmpty()) {
134 m_currentFacility = &(mantidSettings.getFacility());
135 } else {
136 m_currentFacility = &(mantidSettings.getFacility(name.toStdString()));
137 }
139 // could not find the facility
140 // pick the first facility from the valid list
141 m_currentFacility = &(mantidSettings.getFacility(mantidSettings.getFacilityNames()[0]));
142 }
143
144 const auto &instruments = m_currentFacility->instruments();
145 std::set<std::string> alphabetizedNames;
146 for (const auto &instrument : instruments) {
147 alphabetizedNames.insert(instrument.name());
148 }
149 for (const auto &name_std_str : alphabetizedNames) {
150 QString instrumentName = QString::fromStdString(name_std_str);
151 std::string prefix = m_currentFacility->instrument(name_std_str).shortName();
152 QString shortName = QString::fromStdString(prefix);
153 this->addItem(instrumentName, QVariant(shortName));
154 }
156
157 QString defaultName;
158 try {
159 defaultName = QString::fromStdString(ConfigService::Instance().getString("default.instrument"));
160 } catch (Exception::NotFoundError &) {
161 defaultName = "";
162 }
163 int index = this->findText(defaultName);
164 if (index < 0) {
165 index = 0;
166 }
167
168 // Don't affect the default instrument
169 this->setCurrentIndex(index);
170 this->blockSignals(false);
171
173 updateInstrument(this->currentText());
174}
175
180void InstrumentSelector::updateInstrumentOnSelection(const bool storeChanges) { m_storeChanges = storeChanges; }
181
182//------------------------------------------------------
183// Private slot member functions
184//------------------------------------------------------
192void InstrumentSelector::updateInstrument(const QString &name) {
193 // If enabled, set instrument default
194 if (!name.isEmpty() && m_storeChanges) {
195 ConfigService::Instance().setString("default.instrument", name.toStdString());
196 }
197
198 // If this instrument is different emit the changed signal
199 if (name != m_selectedInstrument) {
201 g_log.debug() << "New instrument selected: " << m_selectedInstrument.toStdString() << '\n';
203 }
204}
205
206//------------------------------------------------------
207// Private non-slot member functions
208//------------------------------------------------------
209
216void InstrumentSelector::filterByTechniquesAtFacility(const QStringList &techniques,
217 const Mantid::Kernel::FacilityInfo &facility) {
218 if (techniques.isEmpty())
219 return;
220
221 this->blockSignals(true);
222
223 QStringList supportedInstruments;
224 QStringListIterator techItr(techniques);
225 while (techItr.hasNext()) {
226 const std::vector<InstrumentInfo> instruments = facility.instruments(techItr.next().toStdString());
227 const size_t nInstrs = instruments.size();
228 for (size_t i = 0; i < nInstrs; ++i) {
229 supportedInstruments.append(QString::fromStdString(instruments[i].name()));
230 }
231 }
232
233 // Remove those not supported
234 for (int i = 0; i < this->count();) {
235 if (!supportedInstruments.contains(itemText(i))) {
236 removeItem(i);
237 } else {
238 ++i;
239 }
240 }
241
242 this->blockSignals(false);
243
245}
246
247} // namespace MantidQt::MantidWidgets
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
int count
counter
Definition: Matrix.cpp:37
void removeItem(WorkspaceGroup &self, const std::string &name)
void addItem(WorkspaceGroup &self, const std::string &name)
This class defines a widget for selecting an instrument known to Mantid.
void fillWithInstrumentsFromFacility(const QString &name=QString())
Update list for a new facility.
InstrumentSelector(QWidget *parent=nullptr, bool init=true)
Default Constructor.
Poco::NObserver< InstrumentSelector, Mantid::Kernel::ConfigValChangeNotification > m_changeObserver
Poco Observer for Config Service Notifications.
bool m_init
Should the object be initialized.
void updateInstrument(const QString &name)
Handle an instrument seelction.
void setFacility(const QString &facilityName)
Load instruments from a given facility.
void handleConfigChange(Mantid::Kernel::ConfigValChangeNotification_ptr pNf)
bool m_storeChanges
Should the default instrument be changed when the selection changes.
const Mantid::Kernel::FacilityInfo * m_currentFacility
The current facility.
void filterByTechniquesAtFacility(const QStringList &techniques, const Mantid::Kernel::FacilityInfo &facility)
Filter the list to only show those supporting the given technique.
bool getAutoUpdate()
Returns true of auto reloading on facility change is enabled.
QString m_selectedInstrument
The last selected instrument.
void updateInstrumentOnSelection(const bool storeChanges)
Sets whether to update the default instrument on selection change.
QStringList getTechniques() const
Return the list of techniques.
void setAutoUpdate(bool autoUpdate)
Enable or disable reloading on facility change.
bool m_updateOnFacilityChange
If the instrument list should be reloaded when the facility changes.
QString getFacility()
Get the name of the facility instrumetns are currently loaded from.
void instrumentSelectionChanged(const QString &)
Indicate that the instrument selection has changed.
QStringList m_techniques
A list of technqiues.
void setTechniques(const QStringList &techniques)
Set the list of techniques.
void instrumentListUpdated()
Signals that the list of instruments has been updated.
The ConfigService class provides a simple facade to access the Configuration functionality of the Man...
Definition: ConfigService.h:63
const FacilityInfo & getFacility() const
Get the default facility.
void addObserver(const Poco::AbstractObserver &observer) const
Add an observer for a notification.
const std::vector< std::string > getFacilityNames() const
Get the list of facility names.
Exception for when an item is not found in a collection.
Definition: Exception.h:145
A class that holds information about a facility.
Definition: FacilityInfo.h:36
const std::vector< InstrumentInfo > & instruments() const
Returns a list of instruments of this facility.
Definition: FacilityInfo.h:58
const std::string & name() const
Return the name of the facility.
Definition: FacilityInfo.h:41
const InstrumentInfo & instrument(std::string iName="") const
Returns instruments with given name.
const std::string shortName() const
Return the short name of the instrument.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Kernel::Logger g_log("ExperimentInfo")
static logger object
const Poco::AutoPtr< Mantid::Kernel::ConfigServiceImpl::ValueChanged > & ConfigValChangeNotification_ptr