BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
PropertyWidgetItem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.cpp
6 //! @brief Implements class PropertyWidgetItem
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 <QComboBox>
21 #include <QDataWidgetMapper>
22 #include <QDebug>
23 #include <QDoubleSpinBox>
24 #include <QGridLayout>
25 #include <QLabel>
26 #include <QSpinBox>
27 #include <QWidget>
28 
30  : QObject(parent)
31  , m_label(new QLabel)
32  , m_editor(nullptr)
33  , m_dataMapper(new QDataWidgetMapper(this))
34  , m_delegate(new SessionModelDelegate(nullptr))
35  , m_item(nullptr)
36 {
37  m_label->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed));
38 }
39 
41 {
42  m_editor->clearFocus();
43  delete m_label;
44  delete m_editor;
45  delete m_delegate;
46 }
47 
48 void PropertyWidgetItem::setItemEditor(const SessionItem* item, QWidget* editor)
49 {
50  ASSERT(m_item == nullptr);
51  m_item = item;
52  m_editor = editor;
53 
54  m_label->setText(item->displayName());
55 
56  m_editor->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
57 
58  m_dataMapper->setModel(item->model());
59  m_dataMapper->setRootIndex(item->parent()->index());
60  m_dataMapper->setCurrentModelIndex(item->index());
61  m_dataMapper->addMapping(m_label, 0);
62  m_dataMapper->addMapping(m_editor, 1);
63  m_dataMapper->setItemDelegate(m_delegate);
64 
65  QModelIndex valueIndex = item->index().sibling(item->index().row(), 1);
66  if (valueIndex.isValid())
67  m_delegate->setEditorData(editor, valueIndex);
68 
69  connectEditor(editor);
70 
72 }
73 
74 void PropertyWidgetItem::addToGrid(QGridLayout* gridLayout, int nrow)
75 {
76  ASSERT(m_label);
78 
79  gridLayout->addWidget(m_label, nrow, 0);
80  gridLayout->addWidget(m_editor, nrow, 1);
81 }
82 
84 {
85  ASSERT(m_item);
86  m_label->setEnabled(m_item->isEnabled());
87  m_editor->setEnabled(m_item->isEnabled());
88  m_label->setToolTip(SessionItemUtils::ToolTipRole(*m_item).toString());
89  m_editor->setToolTip(SessionItemUtils::ToolTipRole(*m_item).toString());
90 }
91 
93 {
94  return m_item;
95 }
96 
97 //! Provide additional connections of editor to model mapper.
98 
99 void PropertyWidgetItem::connectEditor(QWidget* editor)
100 {
101  if (auto customEditor = dynamic_cast<CustomEditor*>(editor)) {
102  connect(customEditor, &CustomEditor::dataChanged, m_delegate,
104  }
105 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines CustomEditors classes.
Defines class PropertyWidgetItem.
Defines namespace SessionItemUtils.
Defines class SessionModelDelegate.
Defines class SessionModel.
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.
const SessionItem * item()
void addToGrid(QGridLayout *gridLayout, int nrow)
SessionModelDelegate * m_delegate
const SessionItem * m_item
PropertyWidgetItem(QWidget *parent=nullptr)
QDataWidgetMapper * m_dataMapper
void connectEditor(QWidget *editor)
Provide additional connections of editor to model mapper.
void setItemEditor(const SessionItem *item, QWidget *editor)
QString displayName() const
Get display name of item, append index if ambigue.
bool isEnabled() const
SessionItem * parent() const
Returns parent of this item.
Definition: SessionItem.cpp:73
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:66
QModelIndex index() const
Returns model index of this item.
Definition: SessionItem.cpp:80
The SessionModelDelegate class presents the content of SessionModel items in standard QTreeView.
void onCustomEditorDataChanged(const QVariant &)
Notifies everyone that the editor has completed editing the data.
void setEditorData(QWidget *editor, const QModelIndex &index) const
Propagates the data change from the model to the editor (if it is still opened).
QVariant ToolTipRole(const SessionItem &item, int ncol=0)
Returns tooltip for given item.