BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
SessionItemUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/SessionItemUtils.cpp
6 //! @brief Implements namespace SessionItemUtils
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 
22 #include <QColor>
23 #include <QIcon>
24 #include <QPixmap>
25 
26 namespace {
27 const GroupInfoCatalog& groupInfoCatalog()
28 {
29  static GroupInfoCatalog s_catalog = {};
30  return s_catalog;
31 }
32 
33 QStringList parents_with_abundance()
34 {
35  return QStringList() << "ParticleCoreShell"
36  << "ParticleComposition"
37  << "ParticleDistribution"
38  << "MesoCrystal";
39 }
40 
41 } // namespace
42 
44 {
45  if (item.parent())
46  return item.parent()->rowOfChild(const_cast<SessionItem*>(&item));
47  return -1;
48 }
49 
51 {
52  int result(-1);
53 
54  if (!item.parent() || !item.isVisible())
55  return result;
56 
57  for (auto child : item.parent()->children()) {
58  if (child->isVisible())
59  ++result;
60 
61  if (&item == child)
62  return result;
63  }
64 
65  return result;
66 }
67 
69 {
70  return item.isEnabled() ? QVariant() : QColor(Qt::gray);
71 }
72 
73 QVariant SessionItemUtils::ToolTipRole(const SessionItem& item, int ncol)
74 {
75  QString result = item.toolTip();
76  if (result.isEmpty()) {
77  result = item.displayName();
78  if (ncol == 1 && item.value().canConvert<QString>())
79  result = item.value().toString();
80  }
81 
82  return QVariant(result);
83 }
84 
86 {
87  if (item.value().canConvert<ExternalProperty>())
88  return QIcon(item.value().value<ExternalProperty>().pixmap());
89 
90  return QVariant();
91 }
92 
94 {
95  if (item.value().type() == QVariant::Bool)
96  return item.value().toBool() ? Qt::Checked : Qt::Unchecked;
97  return QVariant();
98 }
99 
100 bool SessionItemUtils::IsValidGroup(const QString& group_type)
101 {
102  return groupInfoCatalog().containsGroup(group_type);
103 }
104 
105 GroupInfo SessionItemUtils::GetGroupInfo(const QString& group_type)
106 {
107  return groupInfoCatalog().groupInfo(group_type);
108 }
109 
110 int SessionItemUtils::VariantType(const QVariant& variant)
111 {
112  int result = static_cast<int>(variant.type());
113  if (result == QVariant::UserType)
114  result = variant.userType();
115  return result;
116 }
117 
118 bool SessionItemUtils::CompatibleVariantTypes(const QVariant& oldValue, const QVariant& newValue)
119 {
120  // if olfValue is undefined than it is compatible with any value, otherwise newValue
121  // should have same variant type as oldValue
122 
123  if (oldValue.isValid())
124  return SessionItemUtils::VariantType(oldValue) == SessionItemUtils::VariantType(newValue);
125  else
126  return true;
127 }
128 
129 // For custom variants (based on ExternalProperty, ComboProperty) will always return false, i.e.
130 // we will rely here on our custom editors.
131 // This is done to not to register custom comparators in main.cpp.
132 bool SessionItemUtils::IsTheSame(const QVariant& var1, const QVariant& var2)
133 {
134  // variants of different type are always reported as not the same
135  if (VariantType(var1) != VariantType(var2))
136  return false;
137 
138  // custom type variants are always reported as not the same
139  if (var1.type() == QVariant::UserType)
140  return false;
141 
142  // standard variants (based on double, int, etc) are compared by value they are holding
143  return var1 == var2;
144 }
145 
147 {
148  if (item.modelType() == "Property"
151  return true;
152 
153  return false;
154 }
155 
157 {
158  static QStringList special_parent = parents_with_abundance();
159  return item ? special_parent.contains(item->modelType()) : false;
160 }
Defines class ExternalProperty.
Defines class GUIHelpers functions.
Defines class GroupInfoCatalog.
Defines class MaterialItem.
Defines class SessionGraphicsItem.
Defines namespace SessionItemUtils.
Defines class VectorItem.
The ExternalProperty class defines custom QVariant property to carry the text, color and an identifie...
QPixmap pixmap() const
Catalog to hold info for GroupProperty creation.
Defines info for GroupProperty, i.e.
Definition: GroupInfo.h:25
static const QString P_XPOS
static const QString P_YPOS
bool isVisible() const
QString displayName() const
Get display name of item, append index if ambigue.
QVariant value() const
Get value.
bool isEnabled() const
int rowOfChild(SessionItem *child) const
Returns row index of given child.
SessionItem * parent() const
Returns parent of this item.
Definition: SessionItem.cpp:73
QString toolTip() const
QVector< SessionItem * > children() const
Returns vector of all children.
QString modelType() const
Get model type.
QVariant DecorationRole(const SessionItem &item)
Returns tooltip for given item.
bool IsTheSame(const QVariant &var1, const QVariant &var2)
Returns true if given variants have same type and value.
int VariantType(const QVariant &variant)
Returns type of variant (additionally checks for user type).
QVariant CheckStateRole(const SessionItem &item)
Returns check state for given item.
QVariant ToolTipRole(const SessionItem &item, int ncol=0)
Returns tooltip for given item.
QVariant ForegroundRole(const SessionItem &item)
Returns text color for given item.
bool CompatibleVariantTypes(const QVariant &oldValue, const QVariant &newValue)
Returns true if variants has compatible types.
bool IsPositionRelated(const SessionItem &item)
Returns true if current item is related to the position on sample editor canvas.
bool IsValidGroup(const QString &group_type)
Returns true if there is registered group.
int ParentVisibleRow(const SessionItem &item)
Returns the row of the given item within its parent not accounting for all hidden items above.
int ParentRow(const SessionItem &item)
Returns the index of the given item within its parent. Returns -1 when no parent is set.
GroupInfo GetGroupInfo(const QString &group_type)
Returns GroupInfo for group property construction.
bool HasOwnAbundance(const SessionItem *item)