BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
collapsiblebar.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/view/mvvm/widgets/collapsiblebar.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include <QEvent>
18 #include <QHBoxLayout>
19 #include <QIcon>
20 #include <QLabel>
21 #include <QMouseEvent>
22 
23 using namespace ModelView;
24 
26  : QFrame(parent), m_pixmapLabel(new QLabel), m_titleLabel(new QLabel)
27 {
28  m_pixmapLabel->setPixmap(QPixmap(":/icons/chevron-down.svg"));
29 
30  auto layout = new QHBoxLayout(this);
31  layout->setContentsMargins(0, 4, 0, 0);
32 
33  layout->addWidget(m_pixmapLabel, Qt::AlignLeft);
34  layout->addWidget(m_titleLabel, Qt::AlignCenter);
35 
36  setFixedHeight(ModelView::Utils::HeightOfLetterM() * 2);
37  setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
38 }
39 
40 void CollapsibleBar::setWidget(QWidget* widget, const QString& title)
41 {
42  m_controlledWidget = widget;
43  m_titleLabel->setText(title);
44  widget->installEventFilter(this);
45  updatePixmap();
46 }
47 
48 void CollapsibleBar::mousePressEvent(QMouseEvent* event)
49 {
50  if (event->button() == Qt::LeftButton)
51  m_controlledWidget->setHidden(m_controlledWidget->isVisible());
52  updatePixmap();
53 }
54 
55 //! Listens for widget signals and update collapse/expand icon on visibility change.
56 
57 bool CollapsibleBar::eventFilter(QObject* obj, QEvent* event)
58 {
59  bool is_event_of_interest = (event->type() == QEvent::Show || event->type() == QEvent::Hide);
60  if (obj == m_controlledWidget && is_event_of_interest)
61  updatePixmap();
62  return QObject::eventFilter(obj, event);
63 }
64 
65 //! Set pixmap depending from the visibility of the widget.
66 
68 {
69  if (m_controlledWidget->isVisible()) {
70  m_pixmapLabel->setPixmap(QPixmap(":/icons/chevron-down.svg"));
71  setFrameStyle(QFrame::StyledPanel);
72  } else {
73  m_pixmapLabel->setPixmap(QPixmap(":/icons/chevron-right.svg"));
74  setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
75  }
76 }
void mousePressEvent(QMouseEvent *event) override
void setWidget(QWidget *widget, const QString &title)
bool eventFilter(QObject *obj, QEvent *event) override
Listens for widget signals and update collapse/expand icon on visibility change.
CollapsibleBar(QWidget *parent=nullptr)
void updatePixmap()
Set pixmap depending from the visibility of the widget.
Defines class CLASS?
MVVM_VIEW_EXPORT int HeightOfLetterM()
Returns height of the letter 'M' deduced from current font metrics.
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?