BornAgain  1.19.0
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/coregui/Views/CommonWidgets/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_COREGUI_VIEWS_COMMONWIDGETS_ITEMSTACKPRESENTER_H
16 #define BORNAGAIN_GUI_COREGUI_VIEWS_COMMONWIDGETS_ITEMSTACKPRESENTER_H
17 
18 #include "Base/Utils/Assert.h"
20 #include <QDebug>
21 #include <QMap>
22 #include <QStackedWidget>
23 
24 class SessionItem;
25 
26 //! The ItemStackPresenter templated class extends ItemStackWidget so it could operate with
27 //! SesionItem editor's of specified type, while still keeping signal/slots alive.
28 
29 template <class T> class ItemStackPresenter : public ItemStackWidget {
30 public:
31  ItemStackPresenter(bool single_widget = false) : m_single_widget(single_widget) {}
32 
33  //! Shows the widget for given item (and hides previous one).
34  //! If no widget yet exists, it will be created (flag isNew will become 'true' in this case).
35  template <class U> void setItem(U* item, bool* isNew = 0);
36 
39  void hideWidgets();
40 
41 protected:
43  void removeWidgets();
44 
45 private:
46  QMap<SessionItem*, T*> m_itemToWidget;
47  bool m_single_widget; //!< Different items will be served by same widget
48 };
49 
50 template <class T> template <class U> void ItemStackPresenter<T>::setItem(U* item, bool* isNew)
51 {
52  validateItem(item);
53 
54  if (isNew)
55  *isNew = false;
56 
57  if (!item) {
58  hideWidgets();
59  return;
60  }
61 
62  T* widget = itemWidget(item);
63 
64  if (!widget) {
65  widget = new T();
66  if (isNew)
67  *isNew = true;
68  m_stackedWidget->addWidget(widget);
69  m_itemToWidget[item] = widget;
70  }
71 
72  m_stackedWidget->setCurrentWidget(widget);
73  if (widget->isHidden())
74  widget->show();
75 
76  widget->setItem(item);
77 }
78 
79 template <class T> T* ItemStackPresenter<T>::currentWidget()
80 {
81  return dynamic_cast<T*>(m_stackedWidget->currentWidget());
82 }
83 
84 template <class T> T* ItemStackPresenter<T>::itemWidget(SessionItem* item)
85 {
86  if (m_single_widget) {
87  if (!m_itemToWidget.empty())
88  return m_itemToWidget.first();
89  } else {
90  return m_itemToWidget[item];
91  }
92 
93  return nullptr;
94 }
95 
96 template <class T> void ItemStackPresenter<T>::hideWidgets()
97 {
98  if (m_stackedWidget->currentWidget())
99  m_stackedWidget->currentWidget()->hide();
100 }
101 
103 {
104  ASSERT(item);
105 
106  if (m_single_widget)
107  return;
108 
109  T* widget = m_itemToWidget[item];
110  if (!widget)
111  return;
112 
113  typename QMap<SessionItem*, T*>::iterator it = m_itemToWidget.begin();
114  while (it != m_itemToWidget.end()) {
115  if (it.value() == widget)
116  it = m_itemToWidget.erase(it);
117  else
118  ++it;
119  }
120 
121  m_stackedWidget->removeWidget(widget);
122  delete widget;
123 }
124 
125 template <class T> void ItemStackPresenter<T>::removeWidgets()
126 {
127  typename QMap<SessionItem*, T*>::iterator it = m_itemToWidget.begin();
128  while (it != m_itemToWidget.end()) {
129  m_stackedWidget->removeWidget(it.value());
130  delete it.value();
131  ++it;
132  }
133  m_itemToWidget.clear();
134 }
135 
136 #endif // BORNAGAIN_GUI_COREGUI_VIEWS_COMMONWIDGETS_ITEMSTACKPRESENTER_H
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:31
Defines class ItemStackWidget.
The ItemStackPresenter templated class extends ItemStackWidget so it could operate with SesionItem ed...
ItemStackPresenter(bool single_widget=false)
void setItem(U *item, bool *isNew=0)
Shows the widget for given item (and hides previous one).
void removeWidgetForItem(SessionItem *item)
bool m_single_widget
Different items will be served by same widget.
QMap< SessionItem *, T * > m_itemToWidget
T * itemWidget(SessionItem *item)
The ItemStackWidget class contains a stack of widgets presenting top level items of SessionModel.