BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
propertyflatview.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/view/mvvm/widgets/propertyflatview.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 
19 #include "mvvm/model/groupitem.h"
20 #include "mvvm/model/sessionitem.h"
25 #include <QDataWidgetMapper>
26 #include <QDebug>
27 #include <QGridLayout>
28 #include <QLabel>
29 
30 using namespace ModelView;
31 
33  std::unique_ptr<ViewModel> view_model;
34  std::unique_ptr<ViewModelDelegate> m_delegate;
35  std::unique_ptr<DefaultEditorFactory> editor_factory;
36  std::vector<std::unique_ptr<QDataWidgetMapper>> widget_mappers;
37  std::map<ViewItem*, QWidget*> item_to_widget;
38 
39  QGridLayout* grid_layout{nullptr};
41  : m_delegate(std::make_unique<ViewModelDelegate>())
42  , editor_factory(std::make_unique<DefaultEditorFactory>())
43  , grid_layout(new QGridLayout)
44  {
45  }
46 
47  //! Creates label for given index.
48 
49  std::unique_ptr<QLabel> create_label(ViewItem* view_item)
50  {
51  auto result = std::make_unique<QLabel>(view_item->data(Qt::DisplayRole).toString());
52  result->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed));
53  result->setEnabled(view_item->item()->isEnabled());
54  return result;
55  }
56 
57  //! Creates custom editor for given index.
58 
59  std::unique_ptr<CustomEditor> create_editor(const QModelIndex& index)
60  {
61  auto editor = editor_factory->createEditor(index);
62  m_delegate->setEditorData(editor.get(), index);
63  connect(editor.get(), &CustomEditor::dataChanged, m_delegate.get(),
65  editor->setEnabled(view_model->sessionItemFromIndex(index)->isEnabled());
66  return editor;
67  }
68 
69  //! Connect model.
70 
72  {
73  auto on_data_change = [this](const QModelIndex& topLeft, const QModelIndex&,
74  const QVector<int>& roles) {
75 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
76  QVector<int> expected_roles = {Qt::ForegroundRole};
77 #else
78  QVector<int> expected_roles = {Qt::TextColorRole};
79 #endif
80  if (roles == expected_roles) {
81  auto view_item = view_model->viewItemFromIndex(topLeft);
82  auto it = item_to_widget.find(view_item);
83  if (it != item_to_widget.end())
84  it->second->setEnabled(view_item->item()->isEnabled());
85  }
86  };
87  connect(view_model.get(), &ViewModel::dataChanged, on_data_change);
88 
89  auto on_row_inserted = [this](const QModelIndex&, int, int) { update_grid_layout(); };
90  connect(view_model.get(), &ViewModel::rowsInserted, on_row_inserted);
91 
92  auto on_row_removed = [this](const QModelIndex&, int, int) { update_grid_layout(); };
93  connect(view_model.get(), &ViewModel::rowsRemoved, on_row_removed);
94  }
95 
96  //! Creates widget for given index to appear in grid layout.
97 
98  std::unique_ptr<QWidget> create_widget(const QModelIndex& index)
99  {
100  auto view_item = view_model->viewItemFromIndex(index);
101  if (auto label_item = dynamic_cast<ViewLabelItem*>(view_item); label_item)
102  return create_label(label_item);
103 
104  return create_editor(index);
105  }
106 
107  //! Creates row of widget mappers. Each widget mapper will serve all editors in a column.
108 
110  {
111  widget_mappers.clear();
112  for (int row = 0; row < view_model->rowCount(); ++row) {
113  auto mapper = std::make_unique<QDataWidgetMapper>();
114  mapper->setModel(view_model.get());
115  mapper->setItemDelegate(m_delegate.get());
116  mapper->setRootIndex(QModelIndex());
117  mapper->setCurrentModelIndex(view_model->index(row, 0));
118  widget_mappers.emplace_back(std::move(mapper));
119  }
120  }
121 
122  //! Updates grid layout with all editors corresponding to the model.
123 
125  {
127 
128  update_mappers();
129  item_to_widget.clear();
130  for (int row = 0; row < view_model->rowCount(); ++row) {
131  for (int col = 0; col < view_model->columnCount(); ++col) {
132  auto index = view_model->index(row, col);
133  auto widget = create_widget(index);
134  item_to_widget[view_model->viewItemFromIndex(index)] = widget.get();
135  widget_mappers[static_cast<size_t>(row)]->addMapping(widget.get(), col);
136  grid_layout->addWidget(widget.release(), row, col);
137  }
138  }
139  }
140 };
141 
143  : QWidget(parent), p_impl(std::make_unique<PropertyFlatViewImpl>())
144 {
145  auto main_layout = new QVBoxLayout;
146  main_layout->setMargin(0);
147  main_layout->setSpacing(0);
148 
149  p_impl->grid_layout->setSpacing(6);
150  main_layout->addLayout(p_impl->grid_layout);
151  main_layout->addStretch(1);
152 
153  setLayout(main_layout);
154 }
155 
157 
159 {
160  p_impl->view_model = Factory::CreatePropertyFlatViewModel(item->model());
161  p_impl->view_model->setRootSessionItem(item);
162  p_impl->connect_model();
163  p_impl->update_grid_layout();
164 }
void dataChanged(QVariant value)
Emmits signal when data was changed in an editor.
Default editor factory for cell editors in Qt trees and tables.
std::unique_ptr< PropertyFlatViewImpl > p_impl
void setItem(SessionItem *item)
PropertyFlatView(QWidget *parent=nullptr)
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
bool isEnabled() const
Returns true if this item has enabled flag set.
SessionModel * model() const
Returns the model to which given item belongs to.
Represents the view of SessionItem's data in a single cell of ViewModel.
Definition: viewitem.h:29
virtual QVariant data(int qt_role) const
Returns the data for given role according to Qt::ItemDataRole namespace definitions.
Definition: viewitem.cpp:199
SessionItem * item() const
Definition: viewitem.cpp:168
Represents display name of SessionItem in any cell of Qt's trees and tables.
Model delegate to provide editing/painting for custom variants.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
void clearGridLayout(QGridLayout *layout, bool deleteWidgets=true)
Clear layout completely.
Definition: LayoutUtils.cpp:81
MVVM_VIEWMODEL_EXPORT std::unique_ptr< ViewModel > CreatePropertyFlatViewModel(SessionModel *model)
Creates view model to represent SessionModel for Qt views.
MVVM_VIEWMODEL_EXPORT QVariant TextColorRole(const SessionItem &item)
Returns text color for given item.
materialitems.h Collection of materials to populate MaterialModel.
QVariant ForegroundRole(const SessionItem &item)
Returns text color for given item.
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
std::unique_ptr< CustomEditor > create_editor(const QModelIndex &index)
Creates custom editor for given index.
std::unique_ptr< QLabel > create_label(ViewItem *view_item)
Creates label for given index.
std::map< ViewItem *, QWidget * > item_to_widget
std::unique_ptr< DefaultEditorFactory > editor_factory
void update_grid_layout()
Updates grid layout with all editors corresponding to the model.
std::vector< std::unique_ptr< QDataWidgetMapper > > widget_mappers
std::unique_ptr< QWidget > create_widget(const QModelIndex &index)
Creates widget for given index to appear in grid layout.
std::unique_ptr< ViewModelDelegate > m_delegate
void update_mappers()
Creates row of widget mappers. Each widget mapper will serve all editors in a column.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?