BornAgain  1.19.0
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/coregui/Models/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 
19 
21  : m_groupItem(groupItem), m_groupInfo(groupInfo)
22 {
25 }
26 
28 {
29  return m_groupItem ? m_groupItem->getChildOfType(currentType()) : nullptr;
30 }
31 
33 {
34  return m_current_type;
35 }
36 
37 void GroupItemController::setCurrentType(const QString& type)
38 {
39  if (type == currentType())
40  return;
41 
42  auto prevItem = currentItem();
43  m_current_type = type;
44 
45  if (m_groupItem) {
46  if (auto item = m_groupItem->getChildOfType(m_current_type)) {
47  item->setVisible(true);
48  item->setEnabled(true);
49  } else {
51  }
52  if (prevItem) {
53  prevItem->setVisible(false);
54  prevItem->setEnabled(false);
55  }
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  if (auto item = m_groupItem->getChildOfType(type)) {
66  return item;
67  } else {
68  SessionItem* new_item = addItem(type);
69  if (type != currentType()) {
70  new_item->setVisible(false);
71  new_item->setEnabled(false);
72  }
73  m_groupItem->insertItem(-1, new_item);
74  return new_item;
75  }
76  }
77  return nullptr;
78 }
79 
81 {
82  return toIndex(m_current_type);
83 }
84 
86 {
87  setCurrentType(toString(index));
88 }
89 
91 {
92  ComboProperty result;
93  result.setValues(itemLabels());
94  result.setCurrentIndex(currentIndex());
95  return result.variant();
96 }
97 
98 QStringList GroupItemController::itemTypes() const
99 {
100  return m_groupInfo.itemTypes();
101 }
102 
104 {
105  return m_groupInfo.itemLabels();
106 }
107 
108 SessionItem* GroupItemController::addItem(const QString& item_type)
109 {
110  return ItemFactory::CreateItem(item_type);
111 }
112 
114 {
115  return addItem(currentType());
116 }
117 
118 int GroupItemController::toIndex(const QString& type) const
119 {
120  return itemTypes().indexOf(type);
121 }
122 
123 QString GroupItemController::toString(int index) const
124 {
125  return itemTypes().at(index);
126 }
Defines class ComboProperty.
Defines class GroupItemController.
Defines class ItemFactory.
Defines class SessionItem.
Custom property to define list of string values with multiple selections.
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.
Definition: GroupInfo.h:25
QStringList itemLabels() const
Definition: GroupInfo.cpp:69
QString defaultType() const
Definition: GroupInfo.cpp:40
QStringList itemTypes() const
Definition: GroupInfo.cpp:60
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.
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)
bool insertItem(int row, SessionItem *item, const QString &tag="")
Insert item into given tag into given row.
void setVisible(bool enabled)
Flags accessors.
void setEnabled(bool enabled)
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:30