BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
combopropertyeditor.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/viewmodel/mvvm/editors/combopropertyeditor.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include <QComboBox>
18 #include <QVBoxLayout>
19 
20 namespace {
21 QStringList toList(const std::vector<std::string>& container)
22 {
23  QStringList result;
24  for (const auto& str : container)
25  result.push_back(QString::fromStdString(str));
26  return result;
27 }
28 } // namespace
29 
30 using namespace ModelView;
31 
33  : CustomEditor(parent), m_box(new QComboBox)
34 {
35  setAutoFillBackground(true);
36  setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
37 
38  auto layout = new QVBoxLayout;
39  layout->setMargin(0);
40  layout->setSpacing(0);
41  layout->addWidget(m_box);
42  setLayout(layout);
43 
44  setConnected(true);
45 }
46 
48 {
49  return m_box->sizeHint();
50 }
51 
53 {
54  return m_box->minimumSizeHint();
55 }
56 
58 {
59  return true;
60 }
61 
63 {
64  auto comboProperty = m_data.value<ComboProperty>();
65 
66  if (comboProperty.currentIndex() != index) {
67  comboProperty.setCurrentIndex(index);
68  setDataIntern(QVariant::fromValue<ComboProperty>(comboProperty));
69  }
70 }
71 
73 {
74  setConnected(false);
75 
76  m_box->clear();
77  m_box->insertItems(0, toList(internLabels()));
78  m_box->setCurrentIndex(internIndex());
79 
80  setConnected(true);
81 }
82 
83 //! Returns list of labels for QComboBox
84 
85 std::vector<std::string> ComboPropertyEditor::internLabels()
86 {
87  if (!m_data.canConvert<ComboProperty>())
88  return {};
89  auto comboProperty = m_data.value<ComboProperty>();
90  return comboProperty.values();
91 }
92 
93 //! Returns index for QComboBox.
94 
96 {
97  if (!m_data.canConvert<ComboProperty>())
98  return 0;
99  auto comboProperty = m_data.value<ComboProperty>();
100  return comboProperty.currentIndex();
101 }
102 
103 void ComboPropertyEditor::setConnected(bool isConnected)
104 {
105  if (isConnected)
106  connect(m_box, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
107  &ComboPropertyEditor::onIndexChanged, Qt::UniqueConnection);
108  else
109  disconnect(m_box, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
111 }
int internIndex()
Returns index for QComboBox.
virtual void onIndexChanged(int index)
QSize minimumSizeHint() const override
std::vector< std::string > internLabels()
Returns list of labels for QComboBox.
bool is_persistent() const override
Returns true if editor should remains alive after editing finished.
void update_components() override
Should update widget components from m_data, if necessary.
ComboPropertyEditor(QWidget *parent=nullptr)
Custom property to define list of string values with multiple selections.
Definition: comboproperty.h:27
std::vector< std::string > values() const
void setCurrentIndex(int index)
Base class for all custom variant editors.
Definition: customeditor.h:26
void setDataIntern(const QVariant &data)
Saves the data as given by editor's internal components and notifies the model.
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.