BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ModelView::ViewModelDelegate Class Reference

Model delegate to provide editing/painting for custom variants. More...

Inheritance diagram for ModelView::ViewModelDelegate:
[legend]
Collaboration diagram for ModelView::ViewModelDelegate:
[legend]

Public Slots

void onCustomEditorDataChanged ()
 

Public Member Functions

 ViewModelDelegate (QObject *parent=nullptr)
 
 ~ViewModelDelegate () override
 
QWidget * createEditor (QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
 
void setCellDecoration (std::unique_ptr< CellDecoratorInterface > cell_decoration)
 
void setEditorData (QWidget *editor, const QModelIndex &index) const override
 
void setEditorFactory (std::unique_ptr< EditorFactoryInterface > editor_factory)
 
void setModelData (QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
 
QSize sizeHint (const QStyleOptionViewItem &option, const QModelIndex &index) const override
 Increases height of the row by 20% wrt the default. More...
 
void updateEditorGeometry (QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
 Makes an editor occupying whole available space in a cell. More...
 

Protected Member Functions

void initStyleOption (QStyleOptionViewItem *option, const QModelIndex &index) const override
 

Protected Attributes

std::unique_ptr< CellDecoratorInterfacem_cell_decoration
 
std::unique_ptr< EditorFactoryInterfacem_editor_factory
 

Detailed Description

Model delegate to provide editing/painting for custom variants.

Definition at line 29 of file viewmodeldelegate.h.

Constructor & Destructor Documentation

◆ ViewModelDelegate()

ViewModelDelegate::ViewModelDelegate ( QObject *  parent = nullptr)
explicit

Definition at line 28 of file viewmodeldelegate.cpp.

29  : QStyledItemDelegate(parent)
30  , m_editor_factory(std::make_unique<DefaultEditorFactory>())
31  , m_cell_decoration(std::make_unique<DefaultCellDecorator>())
32 {
33 }
std::unique_ptr< EditorFactoryInterface > m_editor_factory
std::unique_ptr< CellDecoratorInterface > m_cell_decoration

◆ ~ViewModelDelegate()

ViewModelDelegate::~ViewModelDelegate ( )
overridedefault

Member Function Documentation

◆ createEditor()

QWidget * ViewModelDelegate::createEditor ( QWidget *  parent,
const QStyleOptionViewItem &  option,
const QModelIndex &  index 
) const
override

Definition at line 47 of file viewmodeldelegate.cpp.

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 }
void dataChanged(QVariant value)
Emmits signal when data was changed in an editor.

References ModelView::CustomEditor::dataChanged(), m_editor_factory, and onCustomEditorDataChanged().

Referenced by ViewModelDelegateTest::TestData::create_editor().

Here is the call graph for this function:

◆ initStyleOption()

void ViewModelDelegate::initStyleOption ( QStyleOptionViewItem *  option,
const QModelIndex &  index 
) const
overrideprotected

Definition at line 112 of file viewmodeldelegate.cpp.

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 }

References m_cell_decoration.

◆ onCustomEditorDataChanged

void ViewModelDelegate::onCustomEditorDataChanged ( )
slot

Definition at line 104 of file viewmodeldelegate.cpp.

105 {
106  auto editor = qobject_cast<CustomEditor*>(sender());
107  emit commitData(editor);
108  if (!editor->is_persistent())
109  emit closeEditor(editor);
110 }

Referenced by ModelView::PropertyFlatView::PropertyFlatViewImpl::create_editor(), and createEditor().

◆ setCellDecoration()

void ViewModelDelegate::setCellDecoration ( std::unique_ptr< CellDecoratorInterface cell_decoration)

Definition at line 42 of file viewmodeldelegate.cpp.

43 {
44  m_cell_decoration = std::move(cell_decoration);
45 }

References m_cell_decoration.

◆ setEditorData()

void ViewModelDelegate::setEditorData ( QWidget *  editor,
const QModelIndex &  index 
) const
override

Definition at line 59 of file viewmodeldelegate.cpp.

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 }
Base class for all custom variant editors.
Definition: customeditor.h:26

◆ setEditorFactory()

void ViewModelDelegate::setEditorFactory ( std::unique_ptr< EditorFactoryInterface editor_factory)

Definition at line 37 of file viewmodeldelegate.cpp.

38 {
39  m_editor_factory = std::move(editor_factory);
40 }

References m_editor_factory.

◆ setModelData()

void ViewModelDelegate::setModelData ( QWidget *  editor,
QAbstractItemModel *  model,
const QModelIndex &  index 
) const
override

Definition at line 70 of file viewmodeldelegate.cpp.

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 }

◆ sizeHint()

QSize ViewModelDelegate::sizeHint ( const QStyleOptionViewItem &  option,
const QModelIndex &  index 
) const
override

Increases height of the row by 20% wrt the default.

Definition at line 85 of file viewmodeldelegate.cpp.

87 {
88  QSize result = QStyledItemDelegate::sizeHint(option, index);
89  result.setHeight(static_cast<int>(result.height() * scale_default_height_factor));
90  return result;
91 }

◆ updateEditorGeometry()

void ViewModelDelegate::updateEditorGeometry ( QWidget *  editor,
const QStyleOptionViewItem &  option,
const QModelIndex &  index 
) const
override

Makes an editor occupying whole available space in a cell.

If cell contains an icon as a decoration (i.e. icon of material property), it will be hidden as soon as editor up and running.

Definition at line 97 of file viewmodeldelegate.cpp.

99 {
100  QStyledItemDelegate::updateEditorGeometry(editor, option, index);
101  editor->setGeometry(option.rect);
102 }

Member Data Documentation

◆ m_cell_decoration

std::unique_ptr<CellDecoratorInterface> ModelView::ViewModelDelegate::m_cell_decoration
protected

Definition at line 58 of file viewmodeldelegate.h.

Referenced by initStyleOption(), and setCellDecoration().

◆ m_editor_factory

std::unique_ptr<EditorFactoryInterface> ModelView::ViewModelDelegate::m_editor_factory
protected

Definition at line 57 of file viewmodeldelegate.h.

Referenced by createEditor(), and setEditorFactory().


The documentation for this class was generated from the following files: