BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
VariantUtil.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Support/Type/VariantUtil.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 
16 
17 int GUI::Util::Variant::VariantType(const QVariant& variant)
18 {
19  int result = static_cast<int>(variant.type());
20  if (result == QVariant::UserType)
21  result = variant.userType();
22  return result;
23 }
24 
25 bool GUI::Util::Variant::CompatibleVariantTypes(const QVariant& oldValue, const QVariant& newValue)
26 {
27  // if olfValue is undefined than it is compatible with any value, otherwise newValue
28  // should have same variant type as oldValue
29 
30  if (oldValue.isValid())
31  return GUI::Util::Variant::VariantType(oldValue)
33  return true;
34 }
35 
36 // For custom variants (based on e.g. ComboProperty) will always return false, i.e.
37 // we will rely here on our custom editors.
38 // This is done to not to register custom comparators in main.cpp.
39 bool GUI::Util::Variant::IsTheSame(const QVariant& var1, const QVariant& var2)
40 {
41  // variants of different type are always reported as not the same
42  if (VariantType(var1) != VariantType(var2))
43  return false;
44 
45  // custom type variants are always reported as not the same
46  if (var1.type() == QVariant::UserType)
47  return false;
48 
49  // standard variants (based on double, int, etc) are compared by value they are holding
50  return var1 == var2;
51 }
Defines namespace GUI::Util::Variant.
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
int VariantType(const QVariant &variant)
Returns type of variant (additionally checks for user type).
Definition: VariantUtil.cpp:17
bool CompatibleVariantTypes(const QVariant &oldValue, const QVariant &newValue)
Returns true if variants has compatible types.
Definition: VariantUtil.cpp:25