BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SessionItemData.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Support/Data/SessionItemData.cpp
6 //! @brief Implements class SessionItemData
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 
18 #include <utility>
19 
21  : role(r)
22  , data(std::move(v))
23 {
24 }
25 
27 {
28  return role == other.role && GUI::Util::Variant::IsTheSame(data, other.data);
29 }
30 
31 QVector<int> SessionItemData::roles() const
32 {
33  QVector<int> result;
34  for (const auto& value : m_values)
35  result.push_back(value.role);
36  return result;
37 }
38 
39 QVariant SessionItemData::data(int role) const
40 {
41  role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
42  for (const auto& value : m_values) {
43  if (value.role == role)
44  return value.data;
45  }
46  return QVariant();
47 }
48 
49 //! Sets the data for given role. Returns true if data was changed.
50 
51 bool SessionItemData::setData(int role, const QVariant& value)
52 {
53  role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
54  for (auto* it = m_values.begin(); it != m_values.end(); ++it) {
55  if (it->role == role) {
56  if (value.isValid()) {
57  if (GUI::Util::Variant::IsTheSame(it->data, value))
58  return false;
59  it->data = value;
60  } else {
61  m_values.erase(it);
62  }
63  return true;
64  }
65  }
66  m_values.push_back(ItemData(role, value));
67  return true;
68 }
Defines class SessionItemData.
@ other
The unit has no enum value defined in here (e.g. when defined as an explicit string)
Defines namespace GUI::Util::Variant.
ItemData(int r=-1, QVariant v={})
bool operator==(const ItemData &other) const
QVector< ItemData > m_values
QVector< int > roles() const
QVariant data(int role) const
bool setData(int role, const QVariant &value)
Sets the data for given role. Returns true if data was changed.
bool IsTheSame(const QVariant &var1, const QVariant &var2)
Returns true if given variants have same type and value. For custom variants (e.g....
Definition: VariantUtil.cpp:39