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