Mantid
Loading...
Searching...
No Matches
ListPropertyWidget.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 +
10#include "MantidKernel/System.h"
11#include <QLabel>
12
13#include <sstream>
14
15using namespace Mantid::Kernel;
16using namespace Mantid::API;
17
18namespace MantidQt::API {
19
20//----------------------------------------------------------------------------------------------
24
25//----------------------------------------------------------------------------------------------
28ListPropertyWidget::ListPropertyWidget(Mantid::Kernel::Property *prop, QWidget *parent, QGridLayout *layout, int row)
29 : PropertyWidget(prop, parent, layout, row) {
30 // Label at column 0
31 m_label = new QLabel(QString::fromStdString(prop->name()), m_parent);
32 m_label->setToolTip(m_doc);
33 m_gridLayout->addWidget(m_label, m_row, 0);
34 m_widgets.push_back(m_label);
35
36 // It is a choice of certain allowed values and can use a list box
37 // Check if this is the row that matches the one that we want to link to the
38 // output box
39 m_list = new QListWidget(this);
40 m_list->setToolTip(m_doc);
41 m_list->setSortingEnabled(false);
42 m_list->setSelectionMode(QAbstractItemView::ExtendedSelection);
43 m_widgets.push_back(m_list);
44
45 std::vector<std::string> items = prop->allowedValues();
46 for (auto &item : items) {
47 m_list->addItem(QString::fromStdString(item));
48 }
49 // Make current value visible
50 this->setValue(QString::fromStdString(m_prop->value()));
51
52 // Make sure the connection comes after updating any values
53 connect(m_list, SIGNAL(itemSelectionChanged()), this, SLOT(userEditedProperty()));
54
55 // Put the combo in column 1
56 m_gridLayout->addWidget(m_list, m_row, 1);
57}
58
59//----------------------------------------------------------------------------------------------
62 auto selectedItems = m_list->selectedItems();
63 std::stringstream ss;
64 for (int i = 0; i < selectedItems.size(); ++i) {
65 if (i != 0) {
66 ss << ",";
67 }
68 ss << selectedItems[i]->text().toStdString();
69 }
70 return QString::fromStdString(ss.str());
71}
72
73//----------------------------------------------------------------------------------------------
78 const QString temp = value.isEmpty() ? QString::fromStdString(m_prop->getDefault()) : value;
79
80 auto items = m_list->findItems(temp, Qt::MatchWildcard);
81 for (auto item : items) {
82 item->setSelected(true);
83 m_list->setCurrentItem(item);
84 m_list->scrollToItem(item);
85 }
86}
87} // namespace MantidQt::API
double value
The value of the point.
Definition: FitMW.cpp:51
QListWidget * m_list
List box with the allowed List.
QLabel * m_label
Label (name of the property)
ListPropertyWidget(Mantid::Kernel::Property *prop, QWidget *parent=nullptr, QGridLayout *layout=nullptr, int row=-1)
Constructor.
~ListPropertyWidget() override
Destructor.
void setValueImpl(const QString &value) override
Set the value into the GUI.
Base class for widgets that will set Mantid::Kernel::Property* types.
void setValue(const QString &value)
Set the value of the property given into the GUI state.
QGridLayout * m_gridLayout
Grid layout of the dialog to which we are adding widgets.
QString m_doc
Documentation string (tooltip)
QVector< QWidget * > m_widgets
All contained widgets.
QWidget * m_parent
Parent widget to add sub-widgets to.
void userEditedProperty()
To be called when a user edits a property, as opposed to one being set programmatically.
int m_row
If using the GridLayout, this is the row where the widget was inserted.
Mantid::Kernel::Property * m_prop
Property being looked at. This is NOT owned by the widget.
Base class for properties.
Definition: Property.h:94
const std::string & name() const
Get the property's name.
Definition: Property.cpp:60
virtual std::vector< std::string > allowedValues() const
Returns the set of valid values for this property, if such a set exists.
Definition: Property.cpp:140
virtual std::string getDefault() const =0
Get the default value for the property which is the value the property was initialised with.
virtual std::string value() const =0
Returns the value of the property as a string.