BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
groupitem.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/model/mvvm/model/groupitem.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 
15 #include "mvvm/model/groupitem.h"
17 #include "mvvm/model/taginfo.h"
19 #include <stdexcept>
20 
21 using namespace ModelView;
22 
23 GroupItem::~GroupItem() = default;
24 
26  : SessionItem(std::move(modelType))
27  , m_catalogue(std::make_unique<ItemCatalogue>())
28  , m_default_selected_index(0)
29 {
30  registerTag(TagInfo::universalTag(T_GROUP_ITEMS), /*set_as_default*/ true);
32 }
33 
35 {
36  return data<ComboProperty>().currentIndex();
37 }
38 
39 //! Returns currently selected item.
40 
42 {
43  return is_valid_index() ? getItem("", currentIndex()) : nullptr;
44 }
45 
47 {
48  return const_cast<SessionItem*>(static_cast<const GroupItem*>(this)->currentItem());
49 }
50 
51 std::string GroupItem::currentType() const
52 {
53  return is_valid_index() ? m_catalogue->modelTypes()[static_cast<size_t>(currentIndex())] : "";
54 }
55 
56 //! Sets item corresponding to given model type.
57 
58 void GroupItem::setCurrentType(const std::string& model_type)
59 {
60  auto model_types = m_catalogue->modelTypes();
61  int index = Utils::IndexOfItem(model_types, model_type);
62  if (index == -1)
63  throw std::runtime_error("GroupItem::setCurrentType() -> Model type '" + model_type
64  + "' doesn't belong to the group");
65 
66  setCurrentIndex(index);
67 }
68 
70 {
71  auto combo = data<ComboProperty>();
72  combo.setCurrentIndex(index);
74 }
75 
77 {
78  return currentIndex() != -1;
79 }
80 
81 //! Inits group item by creating all registered items and constructing combo property
82 //! for switching between items.
83 
85 {
86  ComboProperty combo;
87  combo.setValues(m_catalogue->labels());
90  for (const auto& x : m_catalogue->modelTypes())
91  insertItem(m_catalogue->create(x).release(), TagRow::append(T_GROUP_ITEMS));
92 }
Custom property to define list of string values with multiple selections.
Definition: comboproperty.h:27
void setValues(const std::vector< std::string > &values)
Sets new list of values. Current value will be preserved, if exists in a new list.
void setCurrentIndex(int index)
Group item holds collection of predefined items.
Definition: groupitem.h:26
void init_group()
Inits group item by creating all registered items and constructing combo property for switching betwe...
Definition: groupitem.cpp:84
const SessionItem * currentItem() const
Returns currently selected item.
Definition: groupitem.cpp:41
void setCurrentType(const std::string &model_type)
Sets item corresponding to given model type.
Definition: groupitem.cpp:58
static const std::string T_GROUP_ITEMS
Definition: groupitem.h:28
GroupItem(model_type modelType=Constants::GroupItemType)
Definition: groupitem.cpp:25
std::string currentType() const
Definition: groupitem.cpp:51
bool is_valid_index() const
Definition: groupitem.cpp:76
std::unique_ptr< ItemCatalogue > m_catalogue
Definition: groupitem.h:48
void setCurrentIndex(int index)
Definition: groupitem.cpp:69
int m_default_selected_index
Definition: groupitem.h:49
int currentIndex() const
Definition: groupitem.cpp:34
Catalogue for item constructions.
Definition: itemcatalogue.h:30
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
SessionItem * getItem(const std::string &tag, int row=0) const
Returns item at given row of given tag.
void registerTag(const TagInfo &tagInfo, bool set_as_default=false)
Registers tag to hold items under given name.
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
Definition: sessionitem.h:141
bool insertItem(SessionItem *item, const TagRow &tagrow)
Insert item into given tag under the given row.
static TagInfo universalTag(std::string name, std::vector< std::string > modelTypes={})
Constructs universal tag intended for unlimited amount of various items.
Definition: taginfo.cpp:34
static TagRow append(const std::string &tag_name={})
Returns TagRow corresponding to the append to tag_name.
Definition: tagrow.cpp:36
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
const int DATA
main data role
Definition: mvvm_types.h:30
int IndexOfItem(It begin, It end, const T &item)
Returns index corresponding to the first occurance of the item in the container.
materialitems.h Collection of materials to populate MaterialModel.
std::string model_type
Definition: types.h:23
Definition: filesystem.h:81
Defines class CLASS?