BornAgain  1.19.79
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/View/Common/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/Util/Assert.h"
17 #include <QComboBox>
18 #include <QStandardItemModel>
19 
21  : StyledToolbar(parent)
22  , m_comboBox(new QComboBox)
23  , m_comboBoxAction(nullptr)
24 {
25  setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
26 
27  m_comboBox->setToolTip("Select type of graphical presentation.");
28 
29  m_comboBoxAction = addWidget(m_comboBox);
30 
31  setComboConnected(true);
32 }
33 
35 {
36  setComboConnected(false);
37  m_comboBox->setCurrentText(name);
38  setComboConnected(true);
39 }
40 
41 void ItemComboToolbar::setPresentationList(const QStringList& presentationList,
42  const QStringList& activeList)
43 {
44  QString previous = currentPresentation();
45 
46  setComboConnected(false);
47  m_comboBox->clear();
48  m_comboBox->addItems(presentationList);
49  if (activeList.contains(previous))
50  m_comboBox->setCurrentText(previous);
51 
52  if (!activeList.isEmpty())
53  makeItemsEnabled(activeList);
54 
55  setComboConnected(true);
56 }
57 
59 {
60  return m_comboBox->currentText();
61 }
62 
63 //! Sets external actions to tool bar (previous actions will be removed).
64 
65 void ItemComboToolbar::setActionList(const QList<QAction*>& actionList)
66 {
67  for (auto* action : actions())
68  removeAction(action);
69 
70  for (auto* action : actionList) {
71  addAction(action);
72  addSpacing();
73  }
75  addAction(m_comboBoxAction);
76 }
77 
79 {
80  if (value)
81  connect(m_comboBox,
82  static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged),
83  this, &ItemComboToolbar::comboChanged, Qt::UniqueConnection);
84  else
85  disconnect(
86  m_comboBox,
87  static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged), this,
89 }
90 
91 //! All items in QComboBox which are not in given list, will be disabled (gray and unselectable).
92 
93 void ItemComboToolbar::makeItemsEnabled(const QStringList& activePresentations)
94 {
95  const auto* model = dynamic_cast<const QStandardItemModel*>(m_comboBox->model());
96  ASSERT(model);
97 
98  for (int row = 0; row < m_comboBox->count(); ++row) {
99  QString text = m_comboBox->itemText(row);
100  model->item(row)->setEnabled(activePresentations.contains(text));
101  }
102 }
Defines class ItemComboToolbar.
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).
QAction * m_comboBoxAction
QComboBox * m_comboBox
void makeItemsEnabled(const QStringList &activePresentations)
All items in QComboBox which are not in given list, will be disabled (gray and unselectable).
void setComboConnected(bool value)
ItemComboToolbar(QWidget *parent=nullptr)
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:20