BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ItemStackPresenter< T > Class Template Reference

Description

template<class T>
class ItemStackPresenter< T >

The ItemStackPresenter templated class extends ItemStackWidget so it could operate with SesionItem editor's of specified type, while still keeping signal/slots alive.

Definition at line 29 of file ItemStackPresenter.h.

Inheritance diagram for ItemStackPresenter< T >:
[legend]
Collaboration diagram for ItemStackPresenter< T >:
[legend]

Public Slots

virtual void onModelAboutToBeReset ()
 
virtual void onRowsAboutToBeRemoved (const QModelIndex &parent, int first, int)
 

Public Member Functions

 ItemStackPresenter (bool single_widget=false)
 
T * currentWidget ()
 
void hideWidgets ()
 
T * itemWidget (SessionItem *item)
 
QSize minimumSizeHint () const override
 
template<class U >
void setItem (U *item, bool *isNew=nullptr)
 Shows the widget for given item (and hides previous one). If no widget yet exists, it will be created (flag isNew will become 'true' in this case). More...
 
void setModel (SessionModel *model)
 
void setSizeHint (const QSize &size_hint)
 
QSize sizeHint () const override
 

Protected Member Functions

void connectModel ()
 
void disconnectModel ()
 
void removeWidgetForItem (SessionItem *item) override
 
void removeWidgets () override
 
void validateItem (SessionItem *item)
 Checks if model was set correctly. More...
 

Protected Attributes

SessionModelm_model
 
QSize m_size_hint
 
class QStackedWidget * m_stackedWidget
 

Private Attributes

QMap< SessionItem *, T * > m_itemToWidget
 
bool m_single_widget
 Different items will be served by same widget. More...
 

Constructor & Destructor Documentation

◆ ItemStackPresenter()

template<class T >
ItemStackPresenter< T >::ItemStackPresenter ( bool  single_widget = false)
inline

Definition at line 31 of file ItemStackPresenter.h.

32  : m_single_widget(single_widget)
33  {
34  }
bool m_single_widget
Different items will be served by same widget.

Member Function Documentation

◆ connectModel()

void ItemStackWidget::connectModel ( )
protectedinherited

Definition at line 74 of file ItemStackWidget.cpp.

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 }
virtual void onModelAboutToBeReset()
SessionModel * m_model
virtual void onRowsAboutToBeRemoved(const QModelIndex &parent, int first, int)

References ItemStackWidget::m_model, ItemStackWidget::onModelAboutToBeReset(), and ItemStackWidget::onRowsAboutToBeRemoved().

Referenced by ItemStackWidget::setModel().

Here is the call graph for this function:

◆ currentWidget()

template<class T >
T * ItemStackPresenter< T >::currentWidget

Definition at line 86 of file ItemStackPresenter.h.

87 {
88  return dynamic_cast<T*>(m_stackedWidget->currentWidget());
89 }
class QStackedWidget * m_stackedWidget

◆ disconnectModel()

void ItemStackWidget::disconnectModel ( )
protectedinherited

Definition at line 86 of file ItemStackWidget.cpp.

87 {
88  if (!m_model)
89  return;
90 
91  disconnect(m_model, &SessionModel::modelAboutToBeReset, this,
93 
94  disconnect(m_model, &SessionModel::rowsAboutToBeRemoved, this,
96 }

References ItemStackWidget::m_model, ItemStackWidget::onModelAboutToBeReset(), and ItemStackWidget::onRowsAboutToBeRemoved().

Referenced by ItemStackWidget::setModel().

Here is the call graph for this function:

◆ hideWidgets()

template<class T >
void ItemStackPresenter< T >::hideWidgets

Definition at line 105 of file ItemStackPresenter.h.

106 {
107  if (m_stackedWidget->currentWidget())
108  m_stackedWidget->currentWidget()->hide();
109 }

Referenced by JobRealTimeWidget::setItem().

◆ itemWidget()

template<class T >
T * ItemStackPresenter< T >::itemWidget ( SessionItem item)

Definition at line 92 of file ItemStackPresenter.h.

93 {
94  if (m_single_widget) {
95  if (!m_itemToWidget.empty())
96  return m_itemToWidget.first();
97  } else {
98  return m_itemToWidget[item];
99  }
100 
101  return nullptr;
102 }
QMap< SessionItem *, T * > m_itemToWidget

Referenced by JobRealTimeWidget::parameterTuningWidget().

◆ minimumSizeHint()

QSize ItemStackWidget::minimumSizeHint ( ) const
overrideinherited

Definition at line 53 of file ItemStackWidget.cpp.

54 {
55  return QSize(25, 25);
56 }

◆ onModelAboutToBeReset

void ItemStackWidget::onModelAboutToBeReset ( )
virtualslotinherited

Definition at line 63 of file ItemStackWidget.cpp.

64 {
65  removeWidgets();
66 }
virtual void removeWidgets()=0

References ItemStackWidget::removeWidgets().

Referenced by ItemStackWidget::connectModel(), and ItemStackWidget::disconnectModel().

Here is the call graph for this function:

◆ onRowsAboutToBeRemoved

void ItemStackWidget::onRowsAboutToBeRemoved ( const QModelIndex &  parent,
int  first,
int   
)
virtualslotinherited

Definition at line 68 of file ItemStackWidget.cpp.

69 {
70  SessionItem* item = m_model->itemForIndex(m_model->index(first, 0, parent));
71  removeWidgetForItem(item);
72 }
virtual void removeWidgetForItem(SessionItem *item)=0
Base class for a GUI data item.
Definition: SessionItem.h:204
SessionItem * itemForIndex(const QModelIndex &index) const
QModelIndex index(int row, int column, const QModelIndex &parent) const override

References SessionModel::index(), SessionModel::itemForIndex(), ItemStackWidget::m_model, and ItemStackWidget::removeWidgetForItem().

Referenced by ItemStackWidget::connectModel(), and ItemStackWidget::disconnectModel().

Here is the call graph for this function:

◆ removeWidgetForItem()

template<class T >
void ItemStackPresenter< T >::removeWidgetForItem ( SessionItem item)
overrideprotectedvirtual

Implements ItemStackWidget.

Definition at line 112 of file ItemStackPresenter.h.

113 {
114  ASSERT(item);
115 
116  if (m_single_widget)
117  return;
118 
119  T* widget = m_itemToWidget[item];
120  if (!widget)
121  return;
122 
123  typename QMap<SessionItem*, T*>::iterator it = m_itemToWidget.begin();
124  while (it != m_itemToWidget.end()) {
125  if (it.value() == widget)
126  it = m_itemToWidget.erase(it);
127  else
128  ++it;
129  }
130 
131  m_stackedWidget->removeWidget(widget);
132  delete widget;
133 }

◆ removeWidgets()

template<class T >
void ItemStackPresenter< T >::removeWidgets
overrideprotectedvirtual

Implements ItemStackWidget.

Definition at line 136 of file ItemStackPresenter.h.

137 {
138  typename QMap<SessionItem*, T*>::iterator it = m_itemToWidget.begin();
139  while (it != m_itemToWidget.end()) {
140  m_stackedWidget->removeWidget(it.value());
141  delete it.value();
142  ++it;
143  }
144  m_itemToWidget.clear();
145 }

◆ setItem()

template<class T >
template<class U >
void ItemStackPresenter< T >::setItem ( U *  item,
bool *  isNew = nullptr 
)

Shows the widget for given item (and hides previous one). If no widget yet exists, it will be created (flag isNew will become 'true' in this case).

Definition at line 56 of file ItemStackPresenter.h.

57 {
58  validateItem(item);
59 
60  if (isNew)
61  *isNew = false;
62 
63  if (!item) {
64  hideWidgets();
65  return;
66  }
67 
68  T* widget = itemWidget(item);
69 
70  if (!widget) {
71  widget = new T();
72  if (isNew)
73  *isNew = true;
74  m_stackedWidget->addWidget(widget);
75  m_itemToWidget[item] = widget;
76  }
77 
78  m_stackedWidget->setCurrentWidget(widget);
79  if (widget->isHidden())
80  widget->show();
81 
82  widget->setItem(item);
83 }
T * itemWidget(SessionItem *item)
void validateItem(SessionItem *item)
Checks if model was set correctly.

Referenced by ImportDataView::onSelectionChanged(), and JobRealTimeWidget::setItem().

◆ setModel()

void ItemStackWidget::setModel ( SessionModel model)
inherited

Definition at line 38 of file ItemStackWidget.cpp.

39 {
40  if (model == m_model)
41  return;
42 
44  m_model = model;
45  connectModel();
46 }

References ItemStackWidget::connectModel(), ItemStackWidget::disconnectModel(), and ItemStackWidget::m_model.

Referenced by ImportDataView::ImportDataView(), JobRealTimeWidget::JobRealTimeWidget(), and ItemStackWidget::validateItem().

Here is the call graph for this function:

◆ setSizeHint()

void ItemStackWidget::setSizeHint ( const QSize &  size_hint)
inherited

Definition at line 58 of file ItemStackWidget.cpp.

59 {
60  m_size_hint = size_hint;
61 }

References ItemStackWidget::m_size_hint.

Referenced by ImportDataView::ImportDataView().

◆ sizeHint()

QSize ItemStackWidget::sizeHint ( ) const
overrideinherited

Definition at line 48 of file ItemStackWidget.cpp.

49 {
50  return m_size_hint;
51 }

References ItemStackWidget::m_size_hint.

◆ validateItem()

void ItemStackWidget::validateItem ( SessionItem item)
protectedinherited

Checks if model was set correctly.

Definition at line 100 of file ItemStackWidget.cpp.

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 }
void setModel(SessionModel *model)
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:60

References Error, ItemStackWidget::m_model, SessionItem::model(), and ItemStackWidget::setModel().

Here is the call graph for this function:

Member Data Documentation

◆ m_itemToWidget

template<class T >
QMap<SessionItem*, T*> ItemStackPresenter< T >::m_itemToWidget
private

Definition at line 50 of file ItemStackPresenter.h.

◆ m_model

◆ m_single_widget

template<class T >
bool ItemStackPresenter< T >::m_single_widget
private

Different items will be served by same widget.

Definition at line 51 of file ItemStackPresenter.h.

◆ m_size_hint

QSize ItemStackWidget::m_size_hint
protectedinherited

Definition at line 56 of file ItemStackWidget.h.

Referenced by ItemStackWidget::setSizeHint(), and ItemStackWidget::sizeHint().

◆ m_stackedWidget

class QStackedWidget* ItemStackWidget::m_stackedWidget
protectedinherited

Definition at line 54 of file ItemStackWidget.h.

Referenced by ItemStackWidget::ItemStackWidget().


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