BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
FitParameterDelegate.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Fit/FitParameterDelegate.cpp
6 //! @brief Implements class FitParameterDelegate
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
20 #include <QApplication>
21 
23  : QStyledItemDelegate(parent)
24 {
25 }
26 
27 void FitParameterDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
28  const QModelIndex& index) const
29 {
31  QString text = GUI::View::PropertyEditorFactory::toString(index);
32  paintCustomLabel(painter, option, index, text);
33  } else
34  QStyledItemDelegate::paint(painter, option, index);
35 }
36 
37 QWidget* FitParameterDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option,
38  const QModelIndex& index) const
39 {
40  auto* result = createEditorFromIndex(index, parent);
41 
42  if (auto* customEditor = dynamic_cast<CustomEditor*>(result)) {
43  new TabFromFocusProxy(customEditor);
44  connect(customEditor, &CustomEditor::dataChanged, this,
46  }
47 
48  if (!result) // falling back to default behaviour
49  result = QStyledItemDelegate::createEditor(parent, option, index);
50 
51  return result;
52 }
53 
54 //! Propagates changed data from the editor to the model.
55 
56 void FitParameterDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
57  const QModelIndex& index) const
58 {
59  if (!index.isValid())
60  return;
61 
62  if (auto* customEditor = dynamic_cast<CustomEditor*>(editor))
63  model->setData(index, customEditor->editorData());
64  else
65  QStyledItemDelegate::setModelData(editor, model, index);
66 }
67 
68 //! Propagates the data change from the model to the editor (if it is still opened).
69 
70 void FitParameterDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
71 {
72  if (!index.isValid())
73  return;
74 
75  if (auto* customEditor = dynamic_cast<CustomEditor*>(editor))
76  customEditor->setData(index.data());
77  else
78  QStyledItemDelegate::setEditorData(editor, index);
79 }
80 
81 //! Increases height of the row by 20% wrt the default.
82 
83 QSize FitParameterDelegate::sizeHint(const QStyleOptionViewItem& option,
84  const QModelIndex& index) const
85 {
86  QSize result = QStyledItemDelegate::sizeHint(option, index);
87  result.setHeight(static_cast<int>(result.height() * 1.2));
88  return result;
89 }
90 
91 //! Makes an editor occupying whole available space in a cell. If cell contains an icon
92 //! as a decoration (i.e. icon of material property), it will be hidden as soon as editor
93 //! up and running.
94 
95 void FitParameterDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option,
96  const QModelIndex& index) const
97 {
98  QStyledItemDelegate::updateEditorGeometry(editor, option, index);
99  editor->setGeometry(option.rect);
100 }
101 
102 
103 QWidget* FitParameterDelegate::createEditorFromIndex(const QModelIndex& index,
104  QWidget* parent) const
105 {
106  if (index.internalPointer()) {
107  auto* item = static_cast<SessionItem*>(index.internalPointer());
109  }
110  return nullptr;
111 }
112 
113 //! Notifies everyone that the editor has completed editing the data.
114 
116 {
117  auto* editor = qobject_cast<CustomEditor*>(sender());
118  ASSERT(editor);
119  emit commitData(editor);
120 }
121 
122 //! Paints custom text in a a place corresponding given index.
123 
124 void FitParameterDelegate::paintCustomLabel(QPainter* painter, const QStyleOptionViewItem& option,
125  const QModelIndex& index, const QString& text) const
126 {
127  QStyleOptionViewItem opt = option;
128  initStyleOption(&opt, index); // calling original method to take into accounts colors etc
129  opt.text = displayText(text, option.locale); // by overriding text with ours
130  const QWidget* widget = opt.widget;
131  QStyle* style = widget ? widget->style() : QApplication::style();
132  style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
133 }
Defines CustomEditors classes.
Defines classes releted to event filtering.
Defines class FitParameterDelegate.
Defines namespace GUI::View::PropertyEditorFactory.
Defines class SessionItem.
Base class for all custom variants editors.
Definition: CustomEditors.h:29
void dataChanged(const QVariant &data)
Signal emit then user changed the data through the editor.
void setEditorData(QWidget *editor, const QModelIndex &index) const override
Propagates the data change from the model to the editor (if it is still opened).
void paintCustomLabel(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index, const QString &text) const
Paints custom text in a a place corresponding given index.
void 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 (...
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Increases height of the row by 20% wrt the default.
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
FitParameterDelegate(QObject *parent)
void onCustomEditorDataChanged(const QVariant &)
Notifies everyone that the editor has completed editing the data.
virtual QWidget * createEditorFromIndex(const QModelIndex &index, QWidget *parent) const
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
Propagates changed data from the editor to the model.
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Base class for a GUI data item.
Definition: SessionItem.h:204
Propagate tab events from focusProxy to parent.
QWidget * CreateEditor(const SessionItem &item, QWidget *parent=nullptr)
Creates an editor suitable for editing of item.value()
QString toString(const QModelIndex &index)
Provides string representation of index data.
bool hasStringRepresentation(const QModelIndex &index)
Returns true if the index data has known (custom) conversion to string.