BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
sessionitemcontainer.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/sessionitemcontainer.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 
16 #include "mvvm/model/sessionitem.h"
18 
19 using namespace ModelView;
20 
22  : m_tag_info(std::move(tag_info))
23 {
24 }
25 
27 {
28  for (auto item : m_items)
29  delete item;
30 }
31 
33 {
34  return m_items.empty();
35 }
36 
37 //! Returns number of items in given tag.
38 
40 {
41  return static_cast<int>(m_items.size());
42 }
43 
44 //! Returns vector of items in this container.
45 
46 std::vector<SessionItem*> SessionItemContainer::items() const
47 {
48  return m_items;
49 }
50 
51 /*!
52 @brief Inserts item in a vector of children at given index, returns true in the case of success.
53 @param item Item to be inserted, ownership will be taken.
54 @param index Item insert index in a range [0, itemCount]
55 
56 Insert index is an index which item will have after insertion. If item can't be inserted
57 (wrong model type, wrong index or maximum number of items reached), will return false.
58 */
59 
61 {
62  if (!canInsertItem(item, index))
63  return false;
64 
65  m_items.insert(std::next(m_items.begin(), index), item);
66  return true;
67 }
68 
69 //! Removes item at given index and returns it to the user.
70 
72 {
73  if (minimum_reached())
74  return nullptr;
75 
76  SessionItem* result = itemAt(index);
77  if (result)
78  m_items.erase(std::next(m_items.begin(), index));
79 
80  return result;
81 }
82 
83 //! Returns true if item can be taken.
84 
85 bool SessionItemContainer::canTakeItem(int index) const
86 {
87  return itemAt(index) && !minimum_reached();
88 }
89 
90 //! Returns true if given item can be inserted under given index.
91 
92 bool SessionItemContainer::canInsertItem(const SessionItem* item, int index) const
93 {
94  const bool valid_index = (index >= 0 && index <= itemCount());
95  const bool enough_place = !maximum_reached();
96  return valid_index && enough_place && is_valid_item(item);
97 }
98 
99 //! Returns index of item in vector of items.
100 //! Returns -1 if item doesn't belong to us.
101 
103 {
104  return Utils::IndexOfItem(m_items, item);
105 }
106 
107 //! Returns item at given index. Returns nullptr if index is invalid.
108 
110 {
111  return index >= 0 && index < itemCount() ? m_items[static_cast<size_t>(index)] : nullptr;
112 }
113 
114 //! Returns the name of SessionItemTag.
115 
116 std::string SessionItemContainer::name() const
117 {
118  return m_tag_info.name();
119 }
120 
122 {
123  return m_tag_info;
124 }
125 
127 {
128  return m_items.begin();
129 }
130 
132 {
133  return m_items.end();
134 }
135 
136 //! Returns true if no more items are allowed.
137 
139 {
140  return m_tag_info.max() != -1 && m_tag_info.max() == itemCount();
141 }
142 
143 //! Returns true if less items than now is not allowed.
144 
146 {
147  return m_tag_info.min() != -1 && m_tag_info.min() == itemCount();
148 }
149 
150 //! Returns true if item's modelType is intended for this tag.
151 
153 {
154  return item && m_tag_info.isValidChild(item->modelType());
155 }
bool canInsertItem(const SessionItem *item, int index) const
Returns true if given item can be inserted under given index.
bool maximum_reached() const
Returns true if no more items are allowed.
bool insertItem(SessionItem *item, int index)
Inserts item in a vector of children at given index, returns true in the case of success.
bool minimum_reached() const
Returns true if less items than now is not allowed.
bool is_valid_item(const SessionItem *item) const
Returns true if item's modelType is intended for this tag.
std::vector< SessionItem * > items() const
Returns vector of items in this container.
container_t::const_iterator const_iterator
int itemCount() const
Returns number of items in given tag.
int indexOfItem(const SessionItem *item) const
Returns index of item in vector of items.
SessionItem * itemAt(int index) const
Returns item at given index. Returns nullptr if index is invalid.
SessionItem * takeItem(int index)
Removes item at given index and returns it to the user.
bool canTakeItem(int index) const
Returns true if item can be taken.
std::string name() const
Returns the name of SessionItemTag.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
model_type modelType() const
Returns item's model type.
Definition: sessionitem.cpp:80
Holds info about single tag for SessionItem.
Definition: taginfo.h:28
std::string name() const
Definition: taginfo.cpp:45
int max() const
Definition: taginfo.cpp:55
bool isValidChild(const std::string &modelType) const
Returns true if given modelType matches the list of possible model types.
Definition: taginfo.cpp:67
int min() const
Definition: taginfo.cpp:50
Defines class CLASS?
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.
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?