BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ItemStackWidget.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Common/ItemStackWidget.cpp
6 //! @brief Implements class ItemStackWidget
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 
17 #include "GUI/Util/Error.h"
18 #include <QStackedWidget>
19 #include <QVBoxLayout>
20 
22  : QWidget(parent)
23  , m_stackedWidget(new QStackedWidget)
24  , m_model(nullptr)
25  , m_size_hint(QSize(1024, 1024))
26 {
27  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
28  m_stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
29 
30  auto* layout = new QVBoxLayout;
31  layout->setMargin(0);
32  layout->setSpacing(0);
33  layout->setContentsMargins(0, 0, 0, 0);
34  layout->addWidget(m_stackedWidget);
35  setLayout(layout);
36 }
37 
39 {
40  if (model == m_model)
41  return;
42 
44  m_model = model;
45  connectModel();
46 }
47 
49 {
50  return m_size_hint;
51 }
52 
54 {
55  return QSize(25, 25);
56 }
57 
58 void ItemStackWidget::setSizeHint(const QSize& size_hint)
59 {
60  m_size_hint = size_hint;
61 }
62 
64 {
65  removeWidgets();
66 }
67 
68 void ItemStackWidget::onRowsAboutToBeRemoved(const QModelIndex& parent, int first, int)
69 {
70  SessionItem* item = m_model->itemForIndex(m_model->index(first, 0, parent));
71  removeWidgetForItem(item);
72 }
73 
75 {
76  if (!m_model)
77  return;
78 
79  connect(m_model, &SessionModel::modelAboutToBeReset, this,
80  &ItemStackWidget::onModelAboutToBeReset, Qt::UniqueConnection);
81 
82  connect(m_model, &SessionModel::rowsAboutToBeRemoved, this,
83  &ItemStackWidget::onRowsAboutToBeRemoved, Qt::UniqueConnection);
84 }
85 
87 {
88  if (!m_model)
89  return;
90 
91  disconnect(m_model, &SessionModel::modelAboutToBeReset, this,
93 
94  disconnect(m_model, &SessionModel::rowsAboutToBeRemoved, this,
96 }
97 
98 //! Checks if model was set correctly.
99 
101 {
102  if (!item)
103  return;
104 
105  if (m_model) {
106  if (m_model != item->model())
107  // in principle it should be possible, but should be tested
108  throw Error("ItemStackWidget::validateItem() -> Error. "
109  "Attempt to use items from different models.");
110  } else {
111  setModel(item->model());
112  }
113 }
Defines error class.
Defines class ItemStackWidget.
Defines class SessionModel.
class QStackedWidget * m_stackedWidget
void setModel(SessionModel *model)
QSize minimumSizeHint() const override
virtual void removeWidgets()=0
void setSizeHint(const QSize &size_hint)
QSize sizeHint() const override
virtual void onModelAboutToBeReset()
virtual void removeWidgetForItem(SessionItem *item)=0
void validateItem(SessionItem *item)
Checks if model was set correctly.
ItemStackWidget(QWidget *parent=nullptr)
SessionModel * m_model
virtual void onRowsAboutToBeRemoved(const QModelIndex &parent, int first, int)
Base class for a GUI data item.
Definition: SessionItem.h:204
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:60
Base class for a GUI data collection. A collection is e.g. all real data (RealDataModel)....
Definition: SessionModel.h:42
SessionItem * itemForIndex(const QModelIndex &index) const
QModelIndex index(int row, int column, const QModelIndex &parent) const override