From e1f526f7c505d88f3b269b36ceaf6cb3495735ea Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Mon, 11 Dec 2017 13:17:59 +0100
Subject: [PATCH] Method GetVariantType moved from GUIHelper to
 SessionItemUtils

---
 GUI/coregui/Models/SessionItem.cpp            |  4 +-
 GUI/coregui/Models/SessionItemUtils.cpp       | 10 ++++-
 GUI/coregui/Models/SessionItemUtils.h         |  3 ++
 .../JobWidgets/ParameterTuningDelegate.cpp    |  4 +-
 GUI/coregui/utils/GUIHelpers.cpp              |  9 -----
 GUI/coregui/utils/GUIHelpers.h                |  2 -
 Tests/UnitTests/GUI/TestSessionItemUtils.h    | 40 +++++++++++++++++++
 7 files changed, 56 insertions(+), 16 deletions(-)

diff --git a/GUI/coregui/Models/SessionItem.cpp b/GUI/coregui/Models/SessionItem.cpp
index 93d3120af88..2c2899953b3 100644
--- a/GUI/coregui/Models/SessionItem.cpp
+++ b/GUI/coregui/Models/SessionItem.cpp
@@ -465,8 +465,8 @@ QVariant SessionItem::value() const
 bool SessionItem::setValue(QVariant value)
 {
     QVariant previous_variant = this->value();
-    if (previous_variant.isValid() && GUIHelpers::getVariantType(previous_variant)
-            != GUIHelpers::getVariantType(value)) {
+    if (previous_variant.isValid() && SessionItemUtils::VariantType(previous_variant)
+            != SessionItemUtils::VariantType(value)) {
         throw GUIHelpers::Error("ParameterizedItem::setRegisteredProperty() -> Error. Type of "
                                 "previous and new variant does not coincide.");
     }
diff --git a/GUI/coregui/Models/SessionItemUtils.cpp b/GUI/coregui/Models/SessionItemUtils.cpp
index bd93c3e1f2c..0d25c9391c6 100644
--- a/GUI/coregui/Models/SessionItemUtils.cpp
+++ b/GUI/coregui/Models/SessionItemUtils.cpp
@@ -119,13 +119,21 @@ GroupInfo SessionItemUtils::GetGroupInfo(const QString& group_type)
     return groupInfoCatalogue().groupInfo(group_type);
 }
 
+int SessionItemUtils::VariantType(const QVariant& variant)
+{
+    int result = static_cast<int>(variant.type());
+    if (result == QVariant::UserType)
+        result = variant.userType();
+    return result;
+}
+
 // For custom variants (based on ExternalProperty, ComboProperty) will always return false, i.e.
 // we will rely here on our custom editors.
 // This is done to not to register custom comparators in main.cpp.
 bool SessionItemUtils::IsTheSame(const QVariant& var1, const QVariant& var2)
 {
     // variants of different type are always reported as not the same
-    if (GUIHelpers::getVariantType(var1) != GUIHelpers::getVariantType(var2))
+    if (VariantType(var1) != VariantType(var2))
         return false;
 
     // custom type variants are always reported as not the same
diff --git a/GUI/coregui/Models/SessionItemUtils.h b/GUI/coregui/Models/SessionItemUtils.h
index 5b80a441e23..99b7d1ea3ec 100644
--- a/GUI/coregui/Models/SessionItemUtils.h
+++ b/GUI/coregui/Models/SessionItemUtils.h
@@ -58,6 +58,9 @@ BA_CORE_API_ bool IsValidGroup(const QString& group_type);
 //! Returns GroupInfo for group property construction
 BA_CORE_API_ GroupInfo GetGroupInfo(const QString& group_type);
 
+//! Returns type of variant (additionally checks for user type).
+BA_CORE_API_ int VariantType(const QVariant& variant);
+
 //! Returns true if given variants have same type and value.
 //! For custom variants (ComboProperty, ExternalProperty) will always return false (see
 //! explanations in cpp file).
diff --git a/GUI/coregui/Views/JobWidgets/ParameterTuningDelegate.cpp b/GUI/coregui/Views/JobWidgets/ParameterTuningDelegate.cpp
index d73a6d7cdcd..9e0b4ab195f 100644
--- a/GUI/coregui/Views/JobWidgets/ParameterTuningDelegate.cpp
+++ b/GUI/coregui/Views/JobWidgets/ParameterTuningDelegate.cpp
@@ -15,11 +15,11 @@
 // ************************************************************************** //
 
 #include "ParameterTuningDelegate.h"
-#include "GUIHelpers.h"
 #include "ModelPath.h"
 #include "ParameterTreeItems.h"
 #include "ParameterTuningModel.h"
 #include "SessionModel.h"
+#include "SessionItemUtils.h"
 #include <QAbstractItemModel>
 #include <QApplication>
 #include <QDoubleSpinBox>
@@ -118,7 +118,7 @@ void ParameterTuningDelegate::paint(QPainter *painter,
 
         QVariant prop_value = index.model()->data(index, Qt::EditRole);
         if(prop_value.isValid()) {
-            int type = GUIHelpers::getVariantType(prop_value);
+            int type = SessionItemUtils::VariantType(prop_value);
             if (type == QVariant::Double) {
                 double value = prop_value.toDouble();
                 QString text(QString::number(value));
diff --git a/GUI/coregui/utils/GUIHelpers.cpp b/GUI/coregui/utils/GUIHelpers.cpp
index bc6fac229ab..87ac357c4f9 100644
--- a/GUI/coregui/utils/GUIHelpers.cpp
+++ b/GUI/coregui/utils/GUIHelpers.cpp
@@ -121,15 +121,6 @@ bool okToDelete(QWidget *parent, const QString &title, const QString &text, cons
     return messageBox->clickedButton() == deleteButton;
 }
 
-//! returns type of variant
-int getVariantType(const QVariant &variant)
-{
-    int result = variant.type();
-    if (result == QVariant::UserType)
-        result = variant.userType();
-    return result;
-}
-
 QString getBornAgainVersionString()
 {
     return QString::fromStdString(BornAgain::GetVersionNumber());
diff --git a/GUI/coregui/utils/GUIHelpers.h b/GUI/coregui/utils/GUIHelpers.h
index 7a400b7b9b8..7252a0c16ca 100644
--- a/GUI/coregui/utils/GUIHelpers.h
+++ b/GUI/coregui/utils/GUIHelpers.h
@@ -56,8 +56,6 @@ BA_CORE_API_ bool question(QWidget* parent, const QString& title,
 BA_CORE_API_ bool okToDelete(QWidget* parent, const QString& title,
         const QString& text, const QString& detailedText=QString());
 
-BA_CORE_API_ int getVariantType(const QVariant& variant);
-
 BA_CORE_API_ QString getBornAgainVersionString();
 
 BA_CORE_API_ QString getValidFileName(const QString& proposed_name);
diff --git a/Tests/UnitTests/GUI/TestSessionItemUtils.h b/Tests/UnitTests/GUI/TestSessionItemUtils.h
index 1a2da78b99c..658aa2cc9da 100644
--- a/Tests/UnitTests/GUI/TestSessionItemUtils.h
+++ b/Tests/UnitTests/GUI/TestSessionItemUtils.h
@@ -56,9 +56,49 @@ TEST_F(TestSessionItemUtils, test_ParentVisibleRow)
     EXPECT_EQ(SessionItemUtils::ParentVisibleRow(*z), 1);
 }
 
+//! Comparing types of variant.
+
+TEST_F(TestSessionItemUtils, VariantType)
+{
+    EXPECT_TRUE(SessionItemUtils::VariantType(QVariant::fromValue(1.0))
+                == SessionItemUtils::VariantType(QVariant::fromValue(2.0)));
+    EXPECT_FALSE(SessionItemUtils::VariantType(QVariant::fromValue(1.0))
+                 == SessionItemUtils::VariantType(QVariant::fromValue(1)));
+    EXPECT_FALSE(SessionItemUtils::VariantType(QVariant::fromValue(1.0))
+                 == SessionItemUtils::VariantType(QVariant::fromValue(QString("a"))));
+
+    QVariant v1, v2;
+    EXPECT_TRUE(SessionItemUtils::VariantType(v1) == SessionItemUtils::VariantType(v2));
+    EXPECT_FALSE(SessionItemUtils::VariantType(v1)
+                 == SessionItemUtils::VariantType(QVariant::fromValue(42.0)));
+
+    ComboProperty c1, c2;
+    EXPECT_TRUE(SessionItemUtils::VariantType(c1.variant())
+                == SessionItemUtils::VariantType(c2.variant()));
+    EXPECT_FALSE(SessionItemUtils::VariantType(c1.variant())
+                 == SessionItemUtils::VariantType(QVariant::fromValue(42.0)));
+    EXPECT_FALSE(SessionItemUtils::VariantType(c1.variant())
+                 == SessionItemUtils::VariantType(QVariant()));
+
+    ExternalProperty p1, p2;
+    EXPECT_TRUE(SessionItemUtils::VariantType(p1.variant())
+                == SessionItemUtils::VariantType(p2.variant()));
+    EXPECT_FALSE(SessionItemUtils::VariantType(p1.variant())
+                 == SessionItemUtils::VariantType(QVariant::fromValue(42.0)));
+    EXPECT_FALSE(SessionItemUtils::VariantType(p1.variant())
+                 == SessionItemUtils::VariantType(c1.variant()));
+    EXPECT_FALSE(SessionItemUtils::VariantType(p1.variant())
+                 == SessionItemUtils::VariantType(QVariant()));
+}
+
 //! Test variant equality reported by SessionItemUtils::isTheSame
+
 TEST_F(TestSessionItemUtils, IsTheSameVariant)
 {
+    // comparing undefined variants
+    QVariant v1, v2;
+    EXPECT_TRUE(SessionItemUtils::IsTheSame(v1, v2));
+
     // comparing QVariant based on double
     EXPECT_TRUE(SessionItemUtils::IsTheSame(QVariant::fromValue(1.0), QVariant::fromValue(1.0)));
     EXPECT_FALSE(SessionItemUtils::IsTheSame(QVariant::fromValue(1.0), QVariant::fromValue(2.0)));
-- 
GitLab