BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ItemComboToolBar.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/ItemComboToolBar.cpp
6 //! @brief Implements class ItemComboToolBar
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"
17 #include <QAction>
18 #include <QComboBox>
19 #include <QStandardItemModel>
20 
22  : StyledToolBar(parent), m_comboBox(new QComboBox), m_comboBoxAction(nullptr)
23 {
24  setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
25 
26  m_comboBox->setToolTip("Select type of graphical presentation.");
27 
28  m_comboBoxAction = addWidget(m_comboBox);
29 
30  setComboConnected(true);
31 }
32 
34 {
35  setComboConnected(false);
36  m_comboBox->setCurrentText(name);
37  setComboConnected(true);
38 }
39 
40 void ItemComboToolBar::setPresentationList(const QStringList& presentationList,
41  const QStringList& activeList)
42 {
43  ASSERT(presentationList.size());
44 
45  QString previous = currentPresentation();
46 
47  setComboConnected(false);
48  m_comboBox->clear();
49  m_comboBox->addItems(presentationList);
50  if (activeList.contains(previous))
51  m_comboBox->setCurrentText(previous);
52 
53  if (!activeList.isEmpty())
54  makeItemsEnabled(activeList);
55 
56  setComboConnected(true);
57 }
58 
60 {
61  return m_comboBox->currentText();
62 }
63 
64 //! Sets external actions to tool bar (previous actions will be removed).
65 
66 void ItemComboToolBar::setActionList(const QList<QAction*>& actionList)
67 {
68  for (auto action : actions())
69  removeAction(action);
70 
71  for (auto action : actionList) {
72  addAction(action);
73  addSpacing();
74  }
76  addAction(m_comboBoxAction);
77 }
78 
80 {
81  if (value)
82  connect(m_comboBox,
83  static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged),
84  this, &ItemComboToolBar::comboChanged, Qt::UniqueConnection);
85  else
86  disconnect(
87  m_comboBox,
88  static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged), this,
90 }
91 
92 //! All items in QComboBox which are not in given list, will be disabled (gray and unselectable).
93 
94 void ItemComboToolBar::makeItemsEnabled(const QStringList& activePresentations)
95 {
96  const QStandardItemModel* model = dynamic_cast<const QStandardItemModel*>(m_comboBox->model());
97  ASSERT(model);
98 
99  for (int row = 0; row < m_comboBox->count(); ++row) {
100  QString text = m_comboBox->itemText(row);
101  model->item(row)->setEnabled(activePresentations.contains(text));
102  }
103 }
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:31
Defines class ItemComboToolBar.
void comboChanged(const QString &presentation)
void setActionList(const QList< QAction * > &actionList)
Sets external actions to tool bar (previous actions will be removed).
ItemComboToolBar(QWidget *parent=0)
QComboBox * m_comboBox
void setComboConnected(bool value)
void setPresentation(const QString &name)
void setPresentationList(const QStringList &presentationList, const QStringList &activeList={})
QAction * m_comboBoxAction
QString currentPresentation() const
void makeItemsEnabled(const QStringList &activePresentations)
All items in QComboBox which are not in given list, will be disabled (gray and unselectable).
The StyledToolBar class represents our standard narrow toolbar with the height 24 pixels.
Definition: StyledToolBar.h:22
void addStyledExpand()
void addSpacing(int width=2)
Width of the spacing between buttons.
QString const & name(EShape k)
Definition: particles.cpp:21