From 6d6f889b910079670d4caf8660d44aa9a2aa77ba Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Mon, 20 Mar 2017 15:50:48 +0100
Subject: [PATCH] InstrumentSelectorWidget now is derived from
 ItemSelectorWidget

---
 .../CommonWidgets/ItemSelectorWidget.cpp      | 74 +++++++------------
 .../Views/CommonWidgets/ItemSelectorWidget.h  | 25 +++----
 GUI/coregui/Views/InstrumentView.cpp          | 42 +++++++----
 GUI/coregui/Views/InstrumentView.h            |  3 +
 .../InstrumentEditorWidget.cpp                |  8 +-
 .../InstrumentSelectorWidget.cpp              | 65 +++++-----------
 .../InstrumentSelectorWidget.h                | 30 ++------
 7 files changed, 101 insertions(+), 146 deletions(-)

diff --git a/GUI/coregui/Views/CommonWidgets/ItemSelectorWidget.cpp b/GUI/coregui/Views/CommonWidgets/ItemSelectorWidget.cpp
index 66a754893ea..6819f430b85 100644
--- a/GUI/coregui/Views/CommonWidgets/ItemSelectorWidget.cpp
+++ b/GUI/coregui/Views/CommonWidgets/ItemSelectorWidget.cpp
@@ -21,15 +21,12 @@
 #include <QListView>
 #include <QVBoxLayout>
 
-
-ItemSelectorWidget::ItemSelectorWidget(QWidget *parent)
-    : QWidget(parent)
-    , m_listView(new QListView(this))
-    , m_model(0)
+ItemSelectorWidget::ItemSelectorWidget(QWidget* parent)
+    : QWidget(parent), m_listView(new QListView), m_model(nullptr)
 {
     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
 
-    QVBoxLayout *layout = new QVBoxLayout;
+    QVBoxLayout* layout = new QVBoxLayout;
     layout->setMargin(0);
 
     layout->addWidget(m_listView);
@@ -38,9 +35,8 @@ ItemSelectorWidget::ItemSelectorWidget(QWidget *parent)
     m_listView->setContextMenuPolicy(Qt::CustomContextMenu);
     m_listView->setAttribute(Qt::WA_MacShowFocusRect, false);
 
-    connect(m_listView, SIGNAL(customContextMenuRequested(const QPoint &)),
-            this, SLOT(onCustomContextMenuRequested(const QPoint &)));
-
+    connect(m_listView, SIGNAL(customContextMenuRequested(const QPoint&)), this,
+            SLOT(onCustomContextMenuRequested(const QPoint&)));
 }
 
 QSize ItemSelectorWidget::sizeHint() const
@@ -48,14 +44,11 @@ QSize ItemSelectorWidget::sizeHint() const
     return QSize(Constants::ITEM_SELECTOR_WIDGET_WIDTH, Constants::ITEM_SELECTOR_WIDGET_HEIGHT);
 }
 
-QSize ItemSelectorWidget::minimumSizeHint() const
-{
-    return QSize(25, 25);
-}
+QSize ItemSelectorWidget::minimumSizeHint() const { return QSize(25, 25); }
 
-void ItemSelectorWidget::setModel(SessionModel *model)
+void ItemSelectorWidget::setModel(SessionModel* model)
 {
-    if(model == m_model)
+    if (model == m_model)
         return;
 
     disconnectModel();
@@ -63,67 +56,56 @@ void ItemSelectorWidget::setModel(SessionModel *model)
     connectModel();
 }
 
-void ItemSelectorWidget::setItemDelegate(QAbstractItemDelegate *delegate)
+void ItemSelectorWidget::setItemDelegate(QAbstractItemDelegate* delegate)
 {
     m_listView->setItemDelegate(delegate);
 }
 
-QItemSelectionModel *ItemSelectorWidget::selectionModel()
-{
-    return m_listView->selectionModel();
-}
+QItemSelectionModel* ItemSelectorWidget::selectionModel() { return m_listView->selectionModel(); }
 
-QListView *ItemSelectorWidget::listView()
-{
-    return m_listView;
-}
+QListView* ItemSelectorWidget::listView() { return m_listView; }
 
-void ItemSelectorWidget::onSelectionChanged(const QItemSelection &selected, const QItemSelection &)
+void ItemSelectorWidget::onSelectionChanged(const QItemSelection& selected, const QItemSelection&)
 {
     QModelIndexList indexes = selected.indexes();
-    SessionItem *selectedItem(0);
-    if(indexes.size()) {
+    SessionItem* selectedItem(0);
+
+    if (indexes.size())
         selectedItem = m_model->itemForIndex(indexes.back());
-    }
+
     emit selectionChanged(selectedItem);
 }
 
-void ItemSelectorWidget::onCustomContextMenuRequested(const QPoint &point)
+void ItemSelectorWidget::onCustomContextMenuRequested(const QPoint& point)
 {
     emit contextMenuRequest(m_listView->mapToGlobal(point), m_listView->indexAt(point));
 }
 
 void ItemSelectorWidget::connectModel()
 {
-    if(!m_model)
+    if (!m_model)
         return;
 
     m_listView->setModel(m_model);
 
     connect(m_listView->selectionModel(),
-            SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
-            this,
+            SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this,
             SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)),
-            Qt::UniqueConnection
-    );
-
+            Qt::UniqueConnection);
 }
 
 void ItemSelectorWidget::disconnectModel()
 {
-    m_listView->setModel(0);
-    m_model = 0;
+    m_listView->setModel(nullptr);
+    m_model = nullptr;
 }
 
 //! provide default selection when widget is shown
-void ItemSelectorWidget::showEvent(QShowEvent *)
+void ItemSelectorWidget::showEvent(QShowEvent*)
 {
-    if(!m_model || !selectionModel()) return;
-
-    if(selectionModel()->selectedIndexes().isEmpty()) {
-        if(m_model->rowCount(QModelIndex()) != 0) {
-            selectionModel()->select(m_model->index(0, 0, QModelIndex()),
-                                     QItemSelectionModel::Select);
-        }
-    }
+    if (!m_model || !selectionModel())
+        return;
+
+    if (selectionModel()->selectedIndexes().isEmpty() && m_model->rowCount(QModelIndex()) != 0)
+        selectionModel()->select(m_model->index(0, 0, QModelIndex()), QItemSelectionModel::Select);
 }
diff --git a/GUI/coregui/Views/CommonWidgets/ItemSelectorWidget.h b/GUI/coregui/Views/CommonWidgets/ItemSelectorWidget.h
index ae074e84f8a..4bb72ca2a54 100644
--- a/GUI/coregui/Views/CommonWidgets/ItemSelectorWidget.h
+++ b/GUI/coregui/Views/CommonWidgets/ItemSelectorWidget.h
@@ -36,33 +36,32 @@ class BA_CORE_API_ ItemSelectorWidget : public QWidget
     Q_OBJECT
 
 public:
-    ItemSelectorWidget(QWidget *parent = 0);
+    ItemSelectorWidget(QWidget* parent = 0);
 
     QSize sizeHint() const;
     QSize minimumSizeHint() const;
 
-    void setModel(SessionModel *model);
-    void setItemDelegate(QAbstractItemDelegate *delegate);
+    void setModel(SessionModel* model);
+    void setItemDelegate(QAbstractItemDelegate* delegate);
 
-    QItemSelectionModel *selectionModel();
-    QListView *listView();
+    QItemSelectionModel* selectionModel();
+    QListView* listView();
 
 signals:
-    void selectionChanged(SessionItem *item);
-    void contextMenuRequest(const QPoint &point, const QModelIndex &index);
+    void selectionChanged(SessionItem* item);
+    void contextMenuRequest(const QPoint& point, const QModelIndex& index);
 
 private slots:
-    void onSelectionChanged(const QItemSelection &selected, const QItemSelection&);
-    void onCustomContextMenuRequested(const QPoint &point);
+    void onSelectionChanged(const QItemSelection& selected, const QItemSelection&);
+    void onCustomContextMenuRequested(const QPoint& point);
 
 protected:
     void connectModel();
     void disconnectModel();
-    void showEvent(class QShowEvent *);
+    void showEvent(class QShowEvent*);
 
-private:
-    class QListView *m_listView;
-    SessionModel *m_model;
+    QListView* m_listView;
+    SessionModel* m_model;
 };
 
 #endif // ITEMSELECTORWIDGET_H
diff --git a/GUI/coregui/Views/InstrumentView.cpp b/GUI/coregui/Views/InstrumentView.cpp
index 62714ee7d9d..3055a9e5d8b 100644
--- a/GUI/coregui/Views/InstrumentView.cpp
+++ b/GUI/coregui/Views/InstrumentView.cpp
@@ -112,14 +112,14 @@ void InstrumentView::onAddInstrument()
     SessionItem *instrument = m_instrumentModel->insertNewItem(Constants::InstrumentType);
     instrument->setItemName(getNewInstrumentName("Default GISAS"));
     QModelIndex itemIndex = m_instrumentModel->indexOfItem(instrument);
-    m_instrumentSelector->getSelectionModel()->clearSelection();
-    m_instrumentSelector->getSelectionModel()->select(itemIndex, QItemSelectionModel::Select);
+    m_instrumentSelector->selectionModel()->clearSelection();
+    m_instrumentSelector->selectionModel()->select(itemIndex, QItemSelectionModel::Select);
 }
 
 
 void InstrumentView::onRemoveInstrument()
 {
-    QModelIndex currentIndex = m_instrumentSelector->getSelectionModel()->currentIndex();
+    QModelIndex currentIndex = m_instrumentSelector->selectionModel()->currentIndex();
     if(currentIndex.isValid())
         m_instrumentModel->removeRows(currentIndex.row(), 1, QModelIndex());
 }
@@ -153,13 +153,30 @@ void InstrumentView::onExtendedDetectorEditorRequest(DetectorItem *detectorItem)
     dialog->show();
 }
 
-void InstrumentView::setupConnections()
+void InstrumentView::onItemSelectionChanged(SessionItem* instrumentItem)
 {
-    connect(m_instrumentSelector,
-        SIGNAL( selectionChanged(const QItemSelection&, const QItemSelection&) ),
-        this,
-        SLOT( onSelectionChanged(const QItemSelection&, const QItemSelection&) )
-        );
+    InstrumentEditorWidget *widget = m_instrumentToEditor[instrumentItem];
+
+    if( !widget) {
+        widget = new InstrumentEditorWidget();
+        connect(widget,
+                SIGNAL(extendedDetectorEditorRequest(DetectorItem *)),
+                this,
+                SLOT(onExtendedDetectorEditorRequest(DetectorItem *))
+                );
+
+        widget->setInstrumentItem(instrumentItem);
+        m_stackWidget->addWidget(widget);
+        m_instrumentToEditor[instrumentItem] = widget;
+    }
+    m_stackWidget->setCurrentWidget(widget);
+
+}
+
+void InstrumentView::setupConnections()
+{    
+    connect(m_instrumentSelector, SIGNAL(selectionChanged(SessionItem*)),
+            this, SLOT(onItemSelectionChanged(SessionItem*)));
 
     connect(m_instrumentModel,
             SIGNAL(modelAboutToBeReset()),
@@ -210,10 +227,9 @@ void InstrumentView::setupActions()
                       "Remove currently selected instrument", this);
     connect(m_removeInstrumentAction, SIGNAL(triggered()), this, SLOT(onRemoveInstrument()));
 
-    Q_ASSERT(m_instrumentSelector->getListView());
-    m_instrumentSelector->getListView()->setContextMenuPolicy(Qt::ActionsContextMenu);
-    m_instrumentSelector->getListView()->addAction(m_addInstrumentAction);
-    m_instrumentSelector->getListView()->addAction(m_removeInstrumentAction);
+    m_instrumentSelector->listView()->setContextMenuPolicy(Qt::ActionsContextMenu);
+    m_instrumentSelector->listView()->addAction(m_addInstrumentAction);
+    m_instrumentSelector->listView()->addAction(m_removeInstrumentAction);
 }
 
 
diff --git a/GUI/coregui/Views/InstrumentView.h b/GUI/coregui/Views/InstrumentView.h
index 155e1828ab9..9202449554b 100644
--- a/GUI/coregui/Views/InstrumentView.h
+++ b/GUI/coregui/Views/InstrumentView.h
@@ -47,6 +47,9 @@ public slots:
     void onRowsAboutToBeRemoved(QModelIndex,int,int);
     void onExtendedDetectorEditorRequest(DetectorItem *detectorItem);
 
+private slots:
+    void onItemSelectionChanged(SessionItem* instrumentItem);
+
 private:
     void setupConnections();
     void setupActions();
diff --git a/GUI/coregui/Views/InstrumentWidgets/InstrumentEditorWidget.cpp b/GUI/coregui/Views/InstrumentWidgets/InstrumentEditorWidget.cpp
index 7219ee61aa6..509df3082f6 100644
--- a/GUI/coregui/Views/InstrumentWidgets/InstrumentEditorWidget.cpp
+++ b/GUI/coregui/Views/InstrumentWidgets/InstrumentEditorWidget.cpp
@@ -109,7 +109,9 @@ InstrumentEditorWidget::InstrumentEditorWidget(QWidget *parent)
 
 void InstrumentEditorWidget::setInstrumentItem(SessionItem *instrument)
 {
-    Q_ASSERT(instrument);
+    if(!instrument)
+        return;
+
     if(instrument != m_currentItem) {
         if(m_currentItem) {
             // TODO restore logic
@@ -135,8 +137,10 @@ void InstrumentEditorWidget::setInstrumentItem(SessionItem *instrument)
 //                   this,
 //                   SLOT(onPropertyChanged(QString))
 //                   );
-        updateWidgets();
+            updateWidgets();
     }
+
+
     InstrumentItem *instrumentItem = dynamic_cast<InstrumentItem *>(instrument);
 
     m_instrumentComponents->setInstrumentItem(instrumentItem);
diff --git a/GUI/coregui/Views/InstrumentWidgets/InstrumentSelectorWidget.cpp b/GUI/coregui/Views/InstrumentWidgets/InstrumentSelectorWidget.cpp
index 4e57bcb578b..7cb8aa8122d 100644
--- a/GUI/coregui/Views/InstrumentWidgets/InstrumentSelectorWidget.cpp
+++ b/GUI/coregui/Views/InstrumentWidgets/InstrumentSelectorWidget.cpp
@@ -24,24 +24,19 @@
 #include <QVBoxLayout>
 
 InstrumentSelectorWidget::InstrumentSelectorWidget(InstrumentModel *model, QWidget *parent)
-    : QWidget(parent)
-    , m_instrumentModel(0)
-    , m_listView(0)
+    : ItemSelectorWidget(parent)
 {
     setMinimumSize(128, 400);
     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
 
-    m_listView = new QListView;
-    m_listView->setViewMode(QListView::IconMode);
-    m_listView->setIconSize(QSize(96, 84));
-    m_listView->setMovement(QListView::Static);
-    m_listView->setMaximumWidth(200);
-    m_listView->setSpacing(12);
-    m_listView->setAttribute(Qt::WA_MacShowFocusRect, false);
-    //m_listView->setModel(m_instrumentModel);
+    listView()->setViewMode(QListView::IconMode);
+    listView()->setIconSize(QSize(96, 84));
+    listView()->setMovement(QListView::Static);
+    listView()->setMaximumWidth(200);
+    listView()->setSpacing(12);
 
-    m_listView->setObjectName("listView");
-    m_listView->setStyleSheet(QString::fromUtf8("QListView#listView\n"
+    listView()->setObjectName("listView");
+    listView()->setStyleSheet(QString::fromUtf8("QListView#listView\n"
     "{\n"
     "   selection-background-color : rgb(98,100,105); \n"
     "   selection-color: rgb(255,255,255);\n"
@@ -49,52 +44,26 @@ InstrumentSelectorWidget::InstrumentSelectorWidget(InstrumentModel *model, QWidg
     "}\n"
     ""));
 
-    QVBoxLayout *verticaLayout = new QVBoxLayout;
-    verticaLayout->setMargin(10);
-    verticaLayout->setSpacing(10);
-    verticaLayout->addWidget(m_listView, 3);
-
-    setLayout(verticaLayout);
-
-    setInstrumentModel(model);
+    setModel(model);
 }
 
-
-void InstrumentSelectorWidget::setInstrumentModel(InstrumentModel *model)
+QSize InstrumentSelectorWidget::sizeHint() const
 {
-    Q_ASSERT(model);
-    Q_ASSERT(m_listView);
-
-    if(model != m_instrumentModel) {
-        m_instrumentModel = model;
-
-        if(m_instrumentModel) {
-            m_listView->setModel(model);
-
-            connect(m_listView->selectionModel(),
-                    SIGNAL( selectionChanged(const QItemSelection&, const QItemSelection&)),
-                    this,
-                    SIGNAL( selectionChanged(const QItemSelection&, const QItemSelection&)),
-                    Qt::UniqueConnection
-            );
-        }
-
-    }
+    return QSize(200, 400);
 }
 
-
-QItemSelectionModel *InstrumentSelectorWidget::getSelectionModel()
+QSize InstrumentSelectorWidget::minimumSizeHint() const
 {
-    return m_listView->selectionModel();
+    return QSize(128, 200);
 }
 
 
 //! select last item if no selection exists
 void InstrumentSelectorWidget::updateSelection()
 {
-    if (!getSelectionModel()->hasSelection()) {
-        QModelIndex itemIndex = m_instrumentModel->index(
-            m_instrumentModel->rowCount(QModelIndex()) - 1, 0, QModelIndex());
-        getSelectionModel()->select(itemIndex, QItemSelectionModel::Select);
+    if (!selectionModel()->hasSelection()) {
+        QModelIndex itemIndex = m_model->index(
+            m_model->rowCount(QModelIndex()) - 1, 0, QModelIndex());
+        selectionModel()->select(itemIndex, QItemSelectionModel::Select);
     }
 }
diff --git a/GUI/coregui/Views/InstrumentWidgets/InstrumentSelectorWidget.h b/GUI/coregui/Views/InstrumentWidgets/InstrumentSelectorWidget.h
index 8e8cee972fe..9e35b600f65 100644
--- a/GUI/coregui/Views/InstrumentWidgets/InstrumentSelectorWidget.h
+++ b/GUI/coregui/Views/InstrumentWidgets/InstrumentSelectorWidget.h
@@ -17,41 +17,23 @@
 #ifndef INSTRUMENTSELECTORWIDGET_H
 #define INSTRUMENTSELECTORWIDGET_H
 
-#include "WinDllMacros.h"
-#include <QWidget>
-class InstrumentModel;
-class QListView;
-class QAction;
-class QItemSelectionModel;
-class QItemSelection;
+#include "ItemSelectorWidget.h"
 
+class InstrumentModel;
 
-class BA_CORE_API_ InstrumentSelectorWidget : public QWidget
+class BA_CORE_API_ InstrumentSelectorWidget : public ItemSelectorWidget
 {
     Q_OBJECT
 
 public:
-    InstrumentSelectorWidget(InstrumentModel *model, QWidget *parent = 0);
-
-    QSize sizeHint() const { return QSize(200, 400); }
-    QSize minimumSizeHint() const { return QSize(128, 200); }
-
-    void setInstrumentModel(InstrumentModel *model);
+    InstrumentSelectorWidget(InstrumentModel* model, QWidget* parent = 0);
 
-    QItemSelectionModel *getSelectionModel();
-    QListView *getListView() { return m_listView; }
-
-signals:
-    void selectionChanged(const QItemSelection&, const QItemSelection&);
+    QSize sizeHint() const;
+    QSize minimumSizeHint() const;
 
 public slots:
     void updateSelection();
-
-private:
-    InstrumentModel *m_instrumentModel;
-    QListView *m_listView;
 };
 
-
 #endif // INSTRUMENTSELECTORWIDGET_H
 
-- 
GitLab