BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
viewmodeldelegate.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/viewmodel/viewmodeldelegate.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 
20 #include <QApplication>
21 
22 namespace {
23 const double scale_default_height_factor{1.2};
24 }
25 
26 using namespace ModelView;
27 
29  : QStyledItemDelegate(parent)
30  , m_editor_factory(std::make_unique<DefaultEditorFactory>())
31  , m_cell_decoration(std::make_unique<DefaultCellDecorator>())
32 {
33 }
34 
36 
37 void ViewModelDelegate::setEditorFactory(std::unique_ptr<EditorFactoryInterface> editor_factory)
38 {
39  m_editor_factory = std::move(editor_factory);
40 }
41 
42 void ViewModelDelegate::setCellDecoration(std::unique_ptr<CellDecoratorInterface> cell_decoration)
43 {
44  m_cell_decoration = std::move(cell_decoration);
45 }
46 
47 QWidget* ViewModelDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option,
48  const QModelIndex& index) const
49 {
50  if (auto editor = m_editor_factory->createEditor(index)) {
51  editor->setParent(parent);
52  connect(editor.get(), &CustomEditor::dataChanged, this,
54  return editor.release();
55  }
56  return QStyledItemDelegate::createEditor(parent, option, index);
57 }
58 
59 void ViewModelDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
60 {
61  if (!index.isValid())
62  return;
63 
64  if (auto customEditor = dynamic_cast<CustomEditor*>(editor))
65  customEditor->setData(index.data());
66  else
67  QStyledItemDelegate::setEditorData(editor, index);
68 }
69 
70 void ViewModelDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
71  const QModelIndex& index) const
72 {
73  if (!index.isValid())
74  return;
75 
76  if (auto customEditor = dynamic_cast<CustomEditor*>(editor)) {
77  model->setData(index, customEditor->data());
78  } else {
79  QStyledItemDelegate::setModelData(editor, model, index);
80  }
81 }
82 
83 //! Increases height of the row by 20% wrt the default.
84 
85 QSize ViewModelDelegate::sizeHint(const QStyleOptionViewItem& option,
86  const QModelIndex& index) const
87 {
88  QSize result = QStyledItemDelegate::sizeHint(option, index);
89  result.setHeight(static_cast<int>(result.height() * scale_default_height_factor));
90  return result;
91 }
92 
93 //! Makes an editor occupying whole available space in a cell. If cell contains an icon
94 //! as a decoration (i.e. icon of material property), it will be hidden as soon as editor
95 //! up and running.
96 
97 void ViewModelDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option,
98  const QModelIndex& index) const
99 {
100  QStyledItemDelegate::updateEditorGeometry(editor, option, index);
101  editor->setGeometry(option.rect);
102 }
103 
105 {
106  auto editor = qobject_cast<CustomEditor*>(sender());
107  emit commitData(editor);
108  if (!editor->is_persistent())
109  emit closeEditor(editor);
110 }
111 
112 void ViewModelDelegate::initStyleOption(QStyleOptionViewItem* option,
113  const QModelIndex& index) const
114 {
115  QStyledItemDelegate::initStyleOption(option, index);
116 
117  if (m_cell_decoration && m_cell_decoration->hasCustomDecoration(index))
118  m_cell_decoration->initStyleOption(option, index);
119 }
Base class for all custom variant editors.
Definition: customeditor.h:26
void dataChanged(QVariant value)
Emmits signal when data was changed in an editor.
Generates default cell decorations for Qt trees and tables.
Default editor factory for cell editors in Qt trees and tables.
void setEditorFactory(std::unique_ptr< EditorFactoryInterface > editor_factory)
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Increases height of the row by 20% wrt the default.
void setEditorData(QWidget *editor, const QModelIndex &index) const override
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Makes an editor occupying whole available space in a cell.
ViewModelDelegate(QObject *parent=nullptr)
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
std::unique_ptr< EditorFactoryInterface > m_editor_factory
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override
void setCellDecoration(std::unique_ptr< CellDecoratorInterface > cell_decoration)
std::unique_ptr< CellDecoratorInterface > m_cell_decoration
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81
Defines class CLASS?