BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
sessionitemdata.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/sessionitemdata.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 
17 #include <algorithm>
18 #include <sstream>
19 #include <stdexcept>
20 
21 using namespace ModelView;
22 
23 std::vector<int> SessionItemData::roles() const
24 {
25  std::vector<int> result;
26  for (const auto& value : m_values)
27  result.push_back(value.m_role);
28  return result;
29 }
30 
32 {
33  for (const auto& value : m_values) {
34  if (value.m_role == role)
35  return value.m_data;
36  }
37  return Variant();
38 }
39 
40 //! Sets the data for given role. Returns true if data was changed.
41 //! If variant is invalid, corresponding role will be removed.
42 
43 bool SessionItemData::setData(const Variant& value, int role)
44 {
45  assure_validity(value, role);
46 
47  for (auto it = m_values.begin(); it != m_values.end(); ++it) {
48  if (it->m_role == role) {
49  if (value.isValid()) {
50  if (Utils::IsTheSame(it->m_data, value))
51  return false;
52  it->m_data = value;
53  } else {
54  m_values.erase(it);
55  }
56  return true;
57  }
58  }
59  m_values.push_back(DataRole(value, role));
60  return true;
61 }
62 
64 {
65  return m_values.begin();
66 }
67 
69 {
70  return m_values.end();
71 }
72 
73 //! Returns true if item has data with given role.
74 
75 bool SessionItemData::hasData(int role) const
76 {
77  auto has_role = [role](const auto& x) { return x.m_role == role; };
78  return std::find_if(m_values.begin(), m_values.end(), has_role) != m_values.end();
79 }
80 
81 //! Check if variant is compatible
82 
83 void SessionItemData::assure_validity(const Variant& variant, int role)
84 {
85  if (variant.typeName() == QStringLiteral("QString"))
86  throw std::runtime_error("Attempt to set QString based variant");
87 
88  if (!Utils::CompatibleVariantTypes(data(role), variant)) {
89  std::ostringstream ostr;
90  ostr << "SessionItemData::assure_validity() -> Error. Variant types mismatch. "
91  << "Old variant type '" << data(role).typeName() << "' "
92  << "new variant type '" << variant.typeName() << "\n";
93  throw std::runtime_error(ostr.str());
94  }
95 }
#define QStringLiteral
Represents pair of data,role for SessionItemData.
Definition: datarole.h:25
std::vector< int > roles() const
void assure_validity(const Variant &variant, int role)
Check if variant is compatible.
const_iterator begin() const
const_iterator end() const
container_type::const_iterator const_iterator
bool hasData(int role) const
Returns true if item has data with given role.
bool setData(const Variant &value, int role)
Sets the data for given role.
Variant data(int role) const
Defines class CLASS?
MVVM_MODEL_EXPORT bool IsTheSame(const Variant &var1, const Variant &var2)
Returns true if given variants have same type and value.
MVVM_MODEL_EXPORT bool CompatibleVariantTypes(const Variant &oldValue, const Variant &newValue)
Returns true if variants has compatible types.
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
QVariant Variant
Definition: variant.h:23