Mantid
Loading...
Searching...
No Matches
SelectWorkspacesDialog.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//---------------------------------------
10
12
15#include <QDialogButtonBox>
16#include <QListWidget>
17#include <QPushButton>
18#include <QVBoxLayout>
19#include <set>
20
22
27private:
28 const std::string m_type;
30
31public:
32 explicit WorkspaceIsNotOfType(const std::string &type)
33 : m_type(type), m_isMatrixWorkspace(type == "MatrixWorkspace") {}
35 if (m_type.empty())
36 return false;
38 return dynamic_cast<Mantid::API::MatrixWorkspace *>(ws.get()) == nullptr;
39 }
40 return ws->id() != m_type;
41 }
42};
43
44//---------------------------------------
45// Public member functions
46//---------------------------------------
47
55SelectWorkspacesDialog::SelectWorkspacesDialog(QWidget *parent, const std::string &typeFilter,
56 const std::string &customButtonLabel,
57 QAbstractItemView::SelectionMode mode)
58 : QDialog(parent), m_wsList(nullptr), m_okButton(nullptr), m_customButton(nullptr) {
59 setWindowTitle("Mantid - Select workspace");
60 m_wsList = new QListWidget(parent);
61
62 const Mantid::API::AnalysisDataServiceImpl &ADS = Mantid::API::AnalysisDataService::Instance();
63 using VecWorkspaces = std::vector<Mantid::API::Workspace_sptr>;
64 VecWorkspaces workspaces = ADS.getObjects();
65 WorkspaceIsNotOfType comparitor(typeFilter);
66 workspaces.erase(std::remove_if(workspaces.begin(), workspaces.end(), comparitor), workspaces.end());
67 QStringList tmp;
68 for (VecWorkspaces::const_iterator it = workspaces.begin(); it != workspaces.end(); ++it) {
69 // if(useFilter && ADS::
70 tmp << QString::fromStdString((*it)->getName());
71 }
72
73 m_wsList->addItems(tmp);
74 m_wsList->setSelectionMode(mode);
75
76 auto *btnBox = new QDialogButtonBox(Qt::Horizontal);
77
78 if (!customButtonLabel.empty()) {
79 m_customButton = new QPushButton(QString::fromStdString(customButtonLabel));
80 btnBox->addButton(m_customButton, QDialogButtonBox::DestructiveRole);
81 connect(m_customButton, SIGNAL(clicked()), this, SLOT(customButtonPress()));
82 }
83
84 m_okButton = new QPushButton("Select");
85 auto *cancelButton = new QPushButton("Cancel");
86 btnBox->addButton(m_okButton, QDialogButtonBox::AcceptRole);
87 btnBox->addButton(cancelButton, QDialogButtonBox::RejectRole);
88 connect(btnBox, SIGNAL(accepted()), this, SLOT(accept()));
89 connect(btnBox, SIGNAL(rejected()), this, SLOT(reject()));
90
91 auto *vLayout = new QVBoxLayout();
92 vLayout->addWidget(m_wsList);
93 vLayout->addWidget(btnBox);
94
95 setLayout(vLayout);
96
97 connect(m_wsList, SIGNAL(itemSelectionChanged()), this, SLOT(selectionChanged()));
98
100}
101
103 QList<QListWidgetItem *> items = m_wsList->selectedItems();
104 QStringList res;
105 foreach (QListWidgetItem *item, items) {
106 res << item->text();
107 }
108 return res;
109}
110
112void SelectWorkspacesDialog::selectionChanged() { m_okButton->setEnabled(m_wsList->selectionModel()->hasSelection()); }
113
116} // namespace MantidQt::MantidWidgets
gsl_vector * tmp
void selectionChanged()
Slot to monitor the workspace selection status.
QListWidget * m_wsList
Displays available workspace names.
QStringList getSelectedNames() const
Return the selected names.
static const int CustomButton
return value of the Custom button
void customButtonPress()
slot to handle the custom button press
SelectWorkspacesDialog(QWidget *parent=nullptr, const std::string &typeFilter="", const std::string &customButtonLabel="", QAbstractItemView::SelectionMode mode=QAbstractItemView::MultiSelection)
Constructor.
Helper comparitor class used to determine if a workspace is not of a given type.
bool operator()(const Mantid::API::Workspace_sptr &ws) const
The Analysis data service stores instances of the Workspace objects and anything that derives from te...
Base MatrixWorkspace Abstract Class.
virtual const std::string id() const =0
A string ID for the class.
std::vector< std::shared_ptr< T > > getObjects(DataServiceHidden includeHidden=DataServiceHidden::Auto) const
Get a vector of the pointers to the data objects stored by the service.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace