BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
GroupItemController.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/BaseItem/GroupItemController.cpp
6 //! @brief Implements class GroupItemController
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 
19 #include "GUI/Util/ComboProperty.h"
20 #include <utility>
21 
23  : m_groupItem(groupItem)
24  , m_groupInfo(std::move(groupInfo))
25 {
28 }
29 
31 {
32  return m_groupItem ? m_groupItem->getChildOfType(currentType()) : nullptr;
33 }
34 
36 {
37  return m_current_type;
38 }
39 
40 void GroupItemController::setCurrentType(const QString& type)
41 {
42  if (type == currentType())
43  return;
44 
45  auto* prevItem = currentItem();
46  m_current_type = type;
47 
48  if (m_groupItem) {
49  if (auto* item = m_groupItem->getChildOfType(m_current_type))
50  item->setEnabled(true);
51  else
53 
54  if (prevItem)
55  prevItem->setEnabled(false);
56  }
57 }
58 
59 //! Returns item of give type. If it doesn't exist, it will be created.
60 //! Method do _not_ change current item.
61 
63 {
64  if (!m_groupItem)
65  return nullptr;
66  if (auto* item = m_groupItem->getChildOfType(type))
67  return item;
68  SessionItem* new_item = addItem(type);
69  if (type != currentType())
70  new_item->setEnabled(false);
71 
72  m_groupItem->insertChild(-1, new_item);
73  return new_item;
74 }
75 
77 {
78  return toIndex(m_current_type);
79 }
80 
82 {
83  setCurrentType(toString(index));
84 }
85 
87 {
88  ComboProperty result;
89  result.setValues(itemLabels());
90  result.setCurrentIndex(currentIndex());
91  return result.variant();
92 }
93 
94 QStringList GroupItemController::itemTypes() const
95 {
96  return m_groupInfo.itemTypes();
97 }
98 
100 {
101  return m_groupInfo.itemLabels();
102 }
103 
104 SessionItem* GroupItemController::addItem(const QString& item_type)
105 {
106  return GUI::Model::ItemFactory::CreateItem(item_type);
107 }
108 
110 {
111  return addItem(currentType());
112 }
113 
114 int GroupItemController::toIndex(const QString& type) const
115 {
116  return itemTypes().indexOf(type);
117 }
118 
119 QString GroupItemController::toString(int index) const
120 {
121  return itemTypes().at(index);
122 }
Defines class ComboProperty.
Defines class GroupItemController.
Defines namespace GUI::Model::ItemFactory.
Defines class SessionItem.
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
QVariant variant() const
Constructs variant enclosing given ComboProperty.
void setCurrentIndex(int index)
void setValues(const QStringList &values)
Sets new list of values. Current value will be preserved, if exists in a new list.
Defines info for GroupProperty, i.e. collection of model types, their labels and the name of default ...
Definition: GroupInfo.h:25
QStringList itemLabels() const
Definition: GroupInfo.cpp:54
QString defaultType() const
Definition: GroupInfo.cpp:33
QStringList itemTypes() const
Definition: GroupInfo.cpp:46
int toIndex(const QString &type) const
void setCurrentType(const QString &type)
QString currentType() const
void setCurrentIndex(int index)
QVariant createCombo() const
SessionItem * getItemOfType(const QString &type)
Returns item of give type. If it doesn't exist, it will be created. Method do not change current item...
SessionItem * currentItem()
SessionItem * createCorrespondingItem()
SessionItem * addItem(const QString &item_type)
QStringList itemTypes() const
QStringList itemLabels() const
QString toString(int currentIndex) const
GroupItemController(SessionItem *groupItem, GroupInfo groupInfo)
Base class for a GUI data item.
Definition: SessionItem.h:204
void insertChild(int row, SessionItem *item, const QString &tag="")
Insert item into given tag into given row.
void setEnabled(bool enabled)
Flags accessors.
SessionItem * getChildOfType(const QString &type) const
Returns the first child of the given type.
SessionItem * CreateItem(const QString &model_name, SessionItem *parent=nullptr)
create SessionItem of specific type and parent
Definition: ItemFactory.cpp:19