From 517bcfbe1c794458523c73a97b29524cf4ff4e71 Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Mon, 4 Dec 2017 16:03:56 +0100
Subject: [PATCH] ExternalPropertyEditor machinery to call custom dialog

---
 GUI/coregui/Models/LayerItem.cpp                 |  3 ++-
 GUI/coregui/Models/ParticleItem.cpp              |  3 ++-
 GUI/coregui/Models/item_constants.h              |  1 +
 .../Views/MaterialEditor/ExternalProperty.h      |  5 +++--
 .../Views/PropertyEditor/CustomEditors.cpp       | 16 +++++++++++++++-
 GUI/coregui/Views/PropertyEditor/CustomEditors.h |  3 +++
 .../PropertyEditor/PropertyEditorFactory.cpp     | 10 ++++++----
 7 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/GUI/coregui/Models/LayerItem.cpp b/GUI/coregui/Models/LayerItem.cpp
index 03290972dfe..4ae971cfc79 100644
--- a/GUI/coregui/Models/LayerItem.cpp
+++ b/GUI/coregui/Models/LayerItem.cpp
@@ -39,7 +39,8 @@ LayerItem::LayerItem()
         .setToolTip(QStringLiteral("Thickness of a layer in nanometers"));
 
     addProperty(P_MATERIAL, MaterialItemUtils::defaultMaterialProperty().variant())
-            ->setToolTip(QStringLiteral("Material the layer is made of"));
+            ->setToolTip(QStringLiteral("Material the layer is made of"))
+            .setEditorType(Constants::MaterialEditorExternalType);
 
     addProperty(P_NSLICES, 1)->setLimits(RealLimits::lowerLimited(0.0))
             .setToolTip(layer_nslices_tooltip);
diff --git a/GUI/coregui/Models/ParticleItem.cpp b/GUI/coregui/Models/ParticleItem.cpp
index f898cd65e42..27c9e4ad262 100644
--- a/GUI/coregui/Models/ParticleItem.cpp
+++ b/GUI/coregui/Models/ParticleItem.cpp
@@ -48,7 +48,8 @@ ParticleItem::ParticleItem()
 {
     addGroupProperty(P_FORM_FACTOR, Constants::FormFactorGroup);
     addProperty(P_MATERIAL, MaterialItemUtils::defaultMaterialProperty().variant())
-        ->setToolTip(QStringLiteral("Material of particle"));
+        ->setToolTip(QStringLiteral("Material of particle"))
+        .setEditorType(Constants::MaterialEditorExternalType);
 
     addProperty(P_ABUNDANCE, 1.0)->setLimits(RealLimits::limited(0.0, 1.0)).setDecimals(3)
         .setToolTip(abundance_tooltip);
diff --git a/GUI/coregui/Models/item_constants.h b/GUI/coregui/Models/item_constants.h
index 08a74a099ec..0941cf19b68 100644
--- a/GUI/coregui/Models/item_constants.h
+++ b/GUI/coregui/Models/item_constants.h
@@ -276,6 +276,7 @@ const ModelType GroupPropertyType = "GroupProperty_t";
 // --- Custom editors for variant propertues ---
 const ModelType DefaultEditorType = "Default";
 const ModelType ScientificEditorType = "SceintificDouble";
+const ModelType MaterialEditorExternalType = "ExtMaterialEditor";
 }
 
 #endif // ITEM_CONSTANTS_H
diff --git a/GUI/coregui/Views/MaterialEditor/ExternalProperty.h b/GUI/coregui/Views/MaterialEditor/ExternalProperty.h
index 578663410e2..bc89186ee60 100644
--- a/GUI/coregui/Views/MaterialEditor/ExternalProperty.h
+++ b/GUI/coregui/Views/MaterialEditor/ExternalProperty.h
@@ -24,8 +24,9 @@
 #include <QString>
 #include <QVariant>
 
-//! The MaterialProperty class defines unique identifier to help LayerItem, ParticleItem etc
-//! to access materials from MaterialEditor;
+//! The ExternalProperty class defines custom QVariant property to carry the text, color and
+//! an identifier.
+
 class BA_CORE_API_ ExternalProperty
 {
 public:
diff --git a/GUI/coregui/Views/PropertyEditor/CustomEditors.cpp b/GUI/coregui/Views/PropertyEditor/CustomEditors.cpp
index ad1d57f9876..67d0b9daeb0 100644
--- a/GUI/coregui/Views/PropertyEditor/CustomEditors.cpp
+++ b/GUI/coregui/Views/PropertyEditor/CustomEditors.cpp
@@ -21,6 +21,7 @@
 #include "ComboProperty.h"
 #include "ColorProperty.h"
 #include "MaterialItemUtils.h"
+#include "GUIHelpers.h"
 #include <QBoxLayout>
 #include <QLabel>
 #include <QToolButton>
@@ -56,6 +57,7 @@ ExternalPropertyEditor::ExternalPropertyEditor(QWidget* parent)
     , m_textLabel(new QLabel)
     , m_pixmapLabel(new QLabel)
     , m_focusFilter(new LostFocusFilter(this))
+    , m_extDialogType(Constants::MaterialEditorExternalType)
 {
     setMouseTracking(true);
     setAutoFillBackground(true);
@@ -82,12 +84,24 @@ ExternalPropertyEditor::ExternalPropertyEditor(QWidget* parent)
     setLayout(layout);
 }
 
+void ExternalPropertyEditor::setExternalDialogType(const QString& editorType)
+{
+    m_extDialogType = editorType;
+}
+
 void ExternalPropertyEditor::buttonClicked()
 {
     // temporarily installing filter to prevent loss of focus caused by too insistent dialog
     installEventFilter(m_focusFilter);
     ExternalProperty materialProperty = m_data.value<ExternalProperty>();
-    ExternalProperty mat = MaterialItemUtils::selectMaterialProperty(materialProperty);
+
+    ExternalProperty mat;
+    if (m_extDialogType == Constants::MaterialEditorExternalType) {
+        mat = MaterialItemUtils::selectMaterialProperty(materialProperty);
+    } else {
+        throw GUIHelpers::Error("ExternalPropertyEditor::buttonClicked() -> Unexpected dialog");
+    }
+
     removeEventFilter(m_focusFilter);
 
     if (mat.isValid())
diff --git a/GUI/coregui/Views/PropertyEditor/CustomEditors.h b/GUI/coregui/Views/PropertyEditor/CustomEditors.h
index 13b5cea6d8a..e4f0348d2b2 100644
--- a/GUI/coregui/Views/PropertyEditor/CustomEditors.h
+++ b/GUI/coregui/Views/PropertyEditor/CustomEditors.h
@@ -56,6 +56,8 @@ class BA_CORE_API_ ExternalPropertyEditor : public CustomEditor
 public:
     explicit ExternalPropertyEditor(QWidget* parent = nullptr);
 
+    void setExternalDialogType(const QString& dialogType);
+
 private slots:
     void buttonClicked();
 
@@ -66,6 +68,7 @@ private:
     QLabel* m_textLabel;
     QLabel* m_pixmapLabel;
     LostFocusFilter* m_focusFilter;
+    QString m_extDialogType; //!< Type of the dialog which will be created on button click
 };
 
 //! Editor for ColorProperty variant.
diff --git a/GUI/coregui/Views/PropertyEditor/PropertyEditorFactory.cpp b/GUI/coregui/Views/PropertyEditor/PropertyEditorFactory.cpp
index faff902bcf2..33b4c6e52ab 100644
--- a/GUI/coregui/Views/PropertyEditor/PropertyEditorFactory.cpp
+++ b/GUI/coregui/Views/PropertyEditor/PropertyEditorFactory.cpp
@@ -51,7 +51,7 @@ bool isIntProperty(const QVariant& variant)
     return variant.type() == QVariant::Int;
 }
 
-bool isMaterialProperty(const QVariant& variant)
+bool isExternalProperty(const QVariant& variant)
 {
     return variant.canConvert<ExternalProperty>();
 }
@@ -85,7 +85,7 @@ bool isBoolProperty(const QVariant& variant)
 
 bool PropertyEditorFactory::IsCustomVariant(const QVariant& variant)
 {
-    if (isMaterialProperty(variant))
+    if (isExternalProperty(variant))
         return true;
     if (isColorProperty(variant))
         return true;
@@ -102,7 +102,7 @@ bool PropertyEditorFactory::IsCustomVariant(const QVariant& variant)
 // TODO replace with template method when custom variants refactored
 QString PropertyEditorFactory::ToString(const QVariant& variant)
 {
-    if (isMaterialProperty(variant))
+    if (isExternalProperty(variant))
         return variant.value<ExternalProperty>().text();
     if (isColorProperty(variant))
         return variant.value<ColorProperty>().getText();
@@ -145,9 +145,11 @@ QWidget* PropertyEditorFactory::CreateEditor(const SessionItem& item, QWidget* p
         result = createCustomStringEditor(item);
     }
 
-    else if(isMaterialProperty(item.value())) {
+    else if(isExternalProperty(item.value())) {
         auto editor = new ExternalPropertyEditor;
         editor->setData(item.value());
+        if (item.editorType() != Constants::DefaultEditorType)
+            editor->setExternalDialogType(item.editorType());
         result = editor;
     }
 
-- 
GitLab