BornAgain  1.19.79
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/View/Common/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/Util/Assert.h"
18 #include <QStackedWidget>
19 #include <QVBoxLayout>
20 
22  : SessionItemWidget(parent)
23  , m_toolbar(new ItemComboToolbar)
24  , m_stackedWidget(new QStackedWidget)
25 {
26  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
27  m_stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
28  m_blankWidget = new QWidget;
29  m_stackedWidget->addWidget(m_blankWidget);
30 
31  auto* layout = new QVBoxLayout;
32  layout->setMargin(0);
33  layout->setSpacing(0);
34  layout->setContentsMargins(0, 0, 0, 0);
35  layout->addWidget(m_toolbar);
36  layout->addWidget(m_stackedWidget);
37  setLayout(layout);
38 
40 
41  setToolbarVisible(false);
42 }
43 
44 void ItemComboWidget::registerWidget(const QString& presentationType, factory_function_t f)
45 {
46  m_widgetFactory.registerItem(presentationType, f);
47 }
48 
49 //! Sets stack to show widget corresponding to given presentation
50 void ItemComboWidget::setPresentation(const QString& presentationType)
51 {
52  if (presentationType.isEmpty()) {
53  m_stackedWidget->setCurrentWidget(m_blankWidget);
54  setToolbarVisible(false);
55  return;
56  }
57 
58  if (!activePresentationList(currentItem()).contains(presentationType))
59  return;
60 
61  setToolbarVisible(true);
62  m_toolbar->setPresentation(presentationType);
63 
64  ASSERT(currentItem());
65 
66  SessionItemWidget* widget = m_presentationTypeToWidget[presentationType];
67 
68  if (!widget) {
69  widget = m_widgetFactory.createItemPtr(presentationType).release();
70  m_stackedWidget->addWidget(widget);
71  m_presentationTypeToWidget[presentationType] = widget;
72  }
73  ASSERT(widget);
74  widget->setItem(currentItem());
76  m_stackedWidget->setCurrentWidget(widget);
77  if (widget->isHidden())
78  widget->show();
79 
81 }
82 
84 {
85  m_toolbar->setVisible(value);
86 }
87 
89 {
90  if (!item)
91  m_stackedWidget->setCurrentWidget(m_blankWidget);
93  setToolbarVisible(item && !itemPresentation().isEmpty());
94 }
95 
96 //! Returns list of active presentations for given item. Active presentation is the one
97 //! which is present in QComboBox selector and can be selected. For example, if JobItem
98 //! is fittable, the list will contain "FitComparisonWidgetName".
99 
101 {
102  Q_UNUSED(item);
103  return QStringList();
104 }
105 
106 //! Returns full list of presentations available for given item.
107 
109 {
110  return activePresentationList(item);
111 }
112 
113 //! Presentation which should be shown for current item.
114 
116 {
117  return selectedPresentation();
118 }
119 
120 //! Presentation selected in combo selector.
121 
123 {
124  return m_toolbar->currentPresentation();
125 }
126 
128 {
132 }
133 
135 {
137 }
138 
139 //! Resizes QStackedWidget to currently active page.
140 
142 {
143  for (int i = 0; i < m_stackedWidget->count(); ++i) {
144  // determine the vertical size policy
145  QSizePolicy::Policy policy = QSizePolicy::Ignored;
146  if (i == m_stackedWidget->currentIndex())
147  policy = QSizePolicy::Expanding;
148 
149  // update the size policy
150  auto* page = m_stackedWidget->widget(i);
151  page->setSizePolicy(policy, policy);
152  }
153 }
Defines class ItemComboToolbar.
Defines class ItemComboWidget.
The ItemComboToolbar class is a styled toolbar on top of ItemComboWidget. Contains ComboBox to switch...
QString currentPresentation() const
void setPresentation(const QString &name)
void comboChanged(const QString &presentation)
void setPresentationList(const QStringList &presentationList, const QStringList &activeList={})
void setActionList(const QList< QAction * > &actionList)
Sets external actions to tool bar (previous actions will be removed).
QMap< QString, SessionItemWidget * > m_presentationTypeToWidget
QWidget * m_blankWidget
QStackedWidget * m_stackedWidget
ItemComboWidget(QWidget *parent=nullptr)
ItemComboToolbar * m_toolbar
std::function< SessionItemWidget *()> factory_function_t
void setItem(SessionItem *item) override
QString selectedPresentation() const
Presentation selected in combo selector.
virtual void setPresentation(const QString &presentationType)
Sets stack to show widget corresponding to given presentation.
void onComboChanged(const QString &name)
virtual QStringList activePresentationList(SessionItem *item)
Returns list of active presentations for given item. Active presentation is the one which is present ...
virtual QStringList presentationList(SessionItem *item)
Returns full list of presentations available for given item.
void subscribeToItem() override
IFactory< QString, SessionItemWidget > m_widgetFactory
virtual QString itemPresentation() const
Presentation which should be shown for current item.
void registerWidget(const QString &presentationType, factory_function_t)
void setSizeToCurrentWidget()
Resizes QStackedWidget to currently active page.
void setToolbarVisible(bool value)
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)
Base class for a GUI data item.
Definition: SessionItem.h:204