BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
externalpropertycomboeditor.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/externalpropertycomboeditor.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 <QColor>
18 #include <QComboBox>
19 #include <QStandardItemModel>
20 #include <QVBoxLayout>
21 
22 using namespace ModelView;
23 
25  : CustomEditor(parent)
26  , m_getPropertiesCallback(std::move(callback))
27  , m_box(new QComboBox)
28  , m_comboModel(new QStandardItemModel(this))
29 {
30  setAutoFillBackground(true);
31  setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
32 
33  auto layout = new QVBoxLayout;
34  layout->setMargin(0);
35  layout->setSpacing(0);
36  layout->addWidget(m_box);
37  setLayout(layout);
38 
39  m_box->setModel(m_comboModel);
40 
41  setConnected(true);
42 }
43 
45 {
46  return m_box->sizeHint();
47 }
48 
50 {
51  return m_box->minimumSizeHint();
52 }
53 
55 {
56  auto property = m_data.value<ModelView::ExternalProperty>();
57  auto mdata = m_getPropertiesCallback();
58 
59  if (index >= 0 && index < static_cast<int>(mdata.size())) {
60  if (property != mdata[static_cast<size_t>(index)])
61  setDataIntern(QVariant::fromValue(mdata[static_cast<size_t>(index)]));
62  }
63 }
64 
66 {
67  setConnected(false);
68 
69  m_comboModel->clear();
70 
71  QStandardItem* parentItem = m_comboModel->invisibleRootItem();
72  for (auto prop : m_getPropertiesCallback()) {
73  auto item = new QStandardItem(QString::fromStdString(prop.text()));
74  parentItem->appendRow(item);
75  item->setData(prop.color(), Qt::DecorationRole);
76  }
77 
78  m_box->setCurrentIndex(internIndex());
79 
80  setConnected(true);
81 }
82 
83 //! Returns index for QComboBox.
84 
86 {
87  if (!m_data.canConvert<ModelView::ExternalProperty>())
88  return 0;
89 
90  auto property = m_data.value<ModelView::ExternalProperty>();
91  int result(-1);
92  for (auto prop : m_getPropertiesCallback()) {
93  ++result;
94  if (property.identifier() == prop.identifier())
95  return result;
96  }
97 
98  return result;
99 }
100 
102 {
103  if (isConnected)
104  connect(m_box, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
105  &ExternalPropertyComboEditor::onIndexChanged, Qt::UniqueConnection);
106  else
107  disconnect(m_box, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
109 }
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.
int internIndex()
Returns index for QComboBox.
ExternalPropertyComboEditor(callback_t callback, QWidget *parent=nullptr)
void update_components() override
Should update widget components from m_data, if necessary.
std::function< std::vector< ModelView::ExternalProperty >()> callback_t
Property to carry text, color and identifier.
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
QVariant DecorationRole(const SessionItem &item)
Returns tooltip for given item.
Definition: filesystem.h:81