BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ComponentFlatView.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Views/PropertyEditor/ComponentFlatView.cpp
6 //! @brief Implements class ComponentFlatView
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
21 #include <QComboBox>
22 #include <QDataWidgetMapper>
23 #include <QGridLayout>
24 #include <QLabel>
25 #include <QSpinBox>
26 #include <QVBoxLayout>
27 
29  : ComponentView(parent)
30  , m_mainLayout(new QVBoxLayout)
31  , m_gridLayout(nullptr)
32  , m_model(nullptr)
33  , m_show_children(true)
34 {
35  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
36 
37  m_mainLayout->setMargin(10);
38  m_mainLayout->setSpacing(0);
39 
41 
42  setLayout(m_mainLayout);
43 }
44 
46 
48 {
49  clearEditor();
50 
51  m_topItems.push_back(item);
52  setModel(item->model());
54 }
55 
57 {
58  if (m_topItems.isEmpty()) {
59  setItem(item);
60  return;
61  }
62  m_topItems.push_back(item);
64 }
65 
67 {
68  if (m_model) {
69  disconnect(m_model, &SessionModel::dataChanged, this, &ComponentFlatView::onDataChanged);
70  }
71  m_model = model;
72  if (m_model) {
73  connect(m_model, &SessionModel::dataChanged, this, &ComponentFlatView::onDataChanged);
74  }
75 }
76 
78 {
81 
82  for (auto widget : m_widgetItems)
83  widget->deleteLater();
84  m_widgetItems.clear();
85 }
86 
88 {
89  m_show_children = show;
90 }
91 
92 void ComponentFlatView::onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight,
93  const QVector<int>& roles)
94 {
95  Q_UNUSED(bottomRight);
96  SessionItem* item = m_model->itemForIndex(topLeft);
97  ASSERT(item);
98  if (item->modelType() == "GroupProperty")
100 
101  if (roles.contains(SessionFlags::FlagRole))
102  updateItemRoles(item);
103 }
104 
106 {
107  m_topItems.clear();
108  clearLayout();
109 }
110 
112 {
113  clearLayout();
114 
115  QList<const SessionItem*> allitems;
116  for (auto item : m_topItems)
117  allitems += ComponentUtils::componentItems(*item);
118 
119  int nrow(0);
120  for (auto child : allitems) {
121  auto widget = createWidget(child);
122  if (!widget)
123  continue;
124  widget->addToGrid(m_gridLayout, ++nrow);
125  m_widgetItems.push_back(widget);
126  if (!m_show_children)
127  break;
128  }
129 }
130 
132 {
133  for (auto widget : m_widgetItems)
134  if (widget->item() == item)
135  widget->updateItemRoles();
136 }
137 
139 {
140  delete m_gridLayout;
141  m_gridLayout = new QGridLayout;
142  m_gridLayout->setSpacing(6);
143  m_mainLayout->addLayout(m_gridLayout);
144  m_mainLayout->addStretch(1);
145 }
146 
148 {
149  auto editor = PropertyEditorFactory::CreateEditor(*item);
150  if (!editor)
151  return nullptr;
152 
153  auto result = new PropertyWidgetItem(this);
154  result->setItemEditor(item, editor);
155 
156  return result;
157 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines class ComponentFlatView.
Defines ComponentUtils namespace.
Defines LayoutUtils namespace.
Defines PropertyEditorFactory namespace.
Defines class PropertyWidgetItem.
Defines class SessionModel.
QVector< PropertyWidgetItem * > m_widgetItems
void setModel(SessionModel *model)
QVector< const SessionItem * > m_topItems
PropertyWidgetItem * createWidget(const SessionItem *item)
void addItem(SessionItem *item)
void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles)
void setItem(SessionItem *item)
ComponentFlatView(QWidget *parent=nullptr)
QBoxLayout * m_mainLayout
SessionModel * m_model
void setShowChildren(bool show)
void updateItemRoles(SessionItem *item)
QGridLayout * m_gridLayout
Base class for ComponentTreeView and ComponentFlatView.
Definition: ComponentView.h:24
Container to hold label and editor for PropertyItem.
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:66
QString modelType() const
Get model type.
SessionItem * itemForIndex(const QModelIndex &index) const
QList< const SessionItem * > componentItems(const SessionItem &item)
Returns list of SessionItem's children suitable for editing in property editors.
void clearGridLayout(QGridLayout *layout, bool deleteWidgets=true)
Clear layout completely.
Definition: LayoutUtils.cpp:81
QWidget * CreateEditor(const SessionItem &item, QWidget *parent=nullptr)
Creates an editor suitable for editing of item.value()