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 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/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 
17 
18 SessionItemData::ItemData::ItemData(int r, const QVariant& v) : role(r), data(v) {}
19 
21 {
22  return role == other.role && SessionItemUtils::IsTheSame(data, other.data);
23 }
24 
25 QVector<int> SessionItemData::roles() const
26 {
27  QVector<int> result;
28  for (const auto& value : m_values)
29  result.push_back(value.role);
30  return result;
31 }
32 
33 QVariant SessionItemData::data(int role) const
34 {
35  role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
36  for (const auto& value : m_values) {
37  if (value.role == role)
38  return value.data;
39  }
40  return QVariant();
41 }
42 
43 //! Sets the data for given role. Returns true if data was changed.
44 
45 bool SessionItemData::setData(int role, const QVariant& value)
46 {
47  role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
48  for (auto it = m_values.begin(); it != m_values.end(); ++it) {
49  if (it->role == role) {
50  if (value.isValid()) {
51  if (SessionItemUtils::IsTheSame(it->data, value))
52  return false;
53  it->data = value;
54  } else {
55  m_values.erase(it);
56  }
57  return true;
58  }
59  }
60  m_values.push_back(ItemData(role, value));
61  return true;
62 }
Defines class SessionItemData.
Defines namespace SessionItemUtils.
ItemData(int r=-1, const 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.