BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ItemStackPresenter.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Common/ItemStackPresenter.h
6 //! @brief Defines class ItemStackPresenter
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 
15 #ifndef BORNAGAIN_GUI_VIEW_COMMON_ITEMSTACKPRESENTER_H
16 #define BORNAGAIN_GUI_VIEW_COMMON_ITEMSTACKPRESENTER_H
17 
18 #include "Base/Util/Assert.h"
20 #include <QMap>
21 #include <QStackedWidget>
22 
23 class SessionItem;
24 
25 //! The ItemStackPresenter templated class extends ItemStackWidget so it could operate with
26 //! SesionItem editor's of specified type, while still keeping signal/slots alive.
27 
28 template <class T>
30 public:
31  ItemStackPresenter(bool single_widget = false)
32  : m_single_widget(single_widget)
33  {
34  }
35 
36  //! Shows the widget for given item (and hides previous one).
37  //! If no widget yet exists, it will be created (flag isNew will become 'true' in this case).
38  template <class U>
39  void setItem(U* item, bool* isNew = nullptr);
40 
43  void hideWidgets();
44 
45 protected:
46  void removeWidgetForItem(SessionItem* item) override;
47  void removeWidgets() override;
48 
49 private:
50  QMap<SessionItem*, T*> m_itemToWidget;
51  bool m_single_widget; //!< Different items will be served by same widget
52 };
53 
54 template <class T>
55 template <class U>
56 void ItemStackPresenter<T>::setItem(U* item, bool* isNew)
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 }
84 
85 template <class T>
87 {
88  return dynamic_cast<T*>(m_stackedWidget->currentWidget());
89 }
90 
91 template <class T>
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 }
103 
104 template <class T>
106 {
107  if (m_stackedWidget->currentWidget())
108  m_stackedWidget->currentWidget()->hide();
109 }
110 
111 template <class T>
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 }
134 
135 template <class T>
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 }
146 
147 #endif // BORNAGAIN_GUI_VIEW_COMMON_ITEMSTACKPRESENTER_H
Defines class ItemStackWidget.
The ItemStackPresenter templated class extends ItemStackWidget so it could operate with SesionItem ed...
ItemStackPresenter(bool single_widget=false)
void removeWidgetForItem(SessionItem *item) override
bool m_single_widget
Different items will be served by same widget.
void setItem(U *item, bool *isNew=nullptr)
Shows the widget for given item (and hides previous one). If no widget yet exists,...
QMap< SessionItem *, T * > m_itemToWidget
void removeWidgets() override
T * itemWidget(SessionItem *item)
The ItemStackWidget class contains a stack of widgets presenting top level items of SessionModel....
Base class for a GUI data item.
Definition: SessionItem.h:204