BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ItemComboWidget.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Views/CommonWidgets/ItemComboWidget.cpp
6 //! @brief Implements class ItemComboWidget
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 
16 #include "Base/Utils/Assert.h"
19 #include <QComboBox>
20 #include <QEvent>
21 #include <QStackedWidget>
22 #include <QVBoxLayout>
23 
25  : SessionItemWidget(parent)
26  , m_toolBar(new ItemComboToolBar)
27  , m_stackedWidget(new QStackedWidget)
28 {
29  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
30  m_stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
31 
32  QVBoxLayout* layout = new QVBoxLayout;
33  layout->setMargin(0);
34  layout->setSpacing(0);
35  layout->setContentsMargins(0, 0, 0, 0);
36  layout->addWidget(m_toolBar);
37  layout->addWidget(m_stackedWidget);
38  setLayout(layout);
39 
41 }
42 
43 void ItemComboWidget::registerWidget(const QString& presentationType, factory_function_t f)
44 {
45  m_widgetFactory.registerItem(presentationType, f);
46 }
47 
48 //! Sets stack to show widget corresponding to given presentation
49 
50 void ItemComboWidget::setPresentation(const QString& presentationType)
51 {
52  if (!activePresentationList(currentItem()).contains(presentationType))
53  return;
54 
55  m_toolBar->setPresentation(presentationType);
56 
58 
59  SessionItemWidget* widget = m_presentationTypeToWidget[presentationType];
60 
61  if (!widget) {
62  widget = m_widgetFactory.createItemPtr(presentationType).release();
63  m_stackedWidget->addWidget(widget);
64  m_presentationTypeToWidget[presentationType] = widget;
65  }
66  ASSERT(widget);
67  widget->setItem(currentItem());
69  m_stackedWidget->setCurrentWidget(widget);
70  if (widget->isHidden())
71  widget->show();
72 
74 }
75 
77 {
78  m_toolBar->setVisible(value);
79 }
80 
81 //! Returns list of active presentations for given item. Active presentation is the one
82 //! which is present in QComboBox selector and can be selected. For example, if JobItem
83 //! is fittable, the list will contain "FitComparisonWidgetName".
84 
86 {
87  Q_UNUSED(item);
88  return QStringList();
89 }
90 
91 //! Returns full list of presentations available for given item.
92 
94 {
95  return activePresentationList(item);
96 }
97 
98 //! Presentation which should be shown for current item.
99 
101 {
102  return selectedPresentation();
103 }
104 
105 //! Presentation selected in combo selector.
106 
108 {
109  return m_toolBar->currentPresentation();
110 }
111 
113 {
117 }
118 
120 {
122 }
123 
124 //! Resizes QStackedWidget to currently active page.
125 
127 {
128  for (int i = 0; i < m_stackedWidget->count(); ++i) {
129  // determine the vertical size policy
130  QSizePolicy::Policy policy = QSizePolicy::Ignored;
131  if (i == m_stackedWidget->currentIndex())
132  policy = QSizePolicy::Expanding;
133 
134  // update the size policy
135  auto page = m_stackedWidget->widget(i);
136  page->setSizePolicy(policy, policy);
137  }
138 }
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:31
Defines class GUIHelpers functions.
Defines class ItemComboToolBar.
Defines class ItemComboWidget.
bool registerItem(const Key &item_key, CreateItemCallback CreateFn)
Registers object's creation function.
Definition: IFactory.h:53
std::unique_ptr< AbstractProduct > createItemPtr(const Key &item_key) const
Definition: IFactory.h:46
The ItemComboToolBar class is a styled toolbar on top of ItemComboWidget.
void comboChanged(const QString &presentation)
void setActionList(const QList< QAction * > &actionList)
Sets external actions to tool bar (previous actions will be removed).
void setPresentation(const QString &name)
void setPresentationList(const QStringList &presentationList, const QStringList &activeList={})
QString currentPresentation() const
QMap< QString, SessionItemWidget * > m_presentationTypeToWidget
QStackedWidget * m_stackedWidget
void setToolBarVisible(bool value)
std::function< SessionItemWidget *()> factory_function_t
QString selectedPresentation() const
Presentation selected in combo selector.
virtual void setPresentation(const QString &presentationType)
Sets stack to show widget corresponding to given presentation.
ItemComboToolBar * m_toolBar
void onComboChanged(const QString &name)
virtual QStringList activePresentationList(SessionItem *item)
Returns list of active presentations for given item.
virtual QStringList presentationList(SessionItem *item)
Returns full list of presentations available for given item.
IFactory< QString, SessionItemWidget > m_widgetFactory
virtual QString itemPresentation() const
Presentation which should be shown for current item.
ItemComboWidget(QWidget *parent=0)
void registerWidget(const QString &presentationType, factory_function_t)
void setSizeToCurrentWidget()
Resizes QStackedWidget to currently active page.
The SessionItemWidget class is a base for all widgets representing the content of SessionItem.
virtual QList< QAction * > actionList()
SessionItem * currentItem()
virtual void setItem(SessionItem *item)