From b156d7dd8ae26560a8d54c4f5e0167a8930d046a Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Mon, 16 Oct 2017 16:31:09 +0200
Subject: [PATCH] JobSelectorToolBar introduced to decouple toolbar appearance
 from disabled status of corresponding actions.

---
 .../Views/JobWidgets/JobSelectorActions.cpp   | 61 ++++++++-----------
 .../Views/JobWidgets/JobSelectorActions.h     | 27 ++++----
 .../Views/JobWidgets/JobSelectorToolBar.cpp   | 40 ++++++++++++
 .../Views/JobWidgets/JobSelectorToolBar.h     | 41 +++++++++++++
 .../Views/JobWidgets/JobSelectorWidget.cpp    |  4 +-
 .../Views/JobWidgets/JobSelectorWidget.h      |  6 +-
 6 files changed, 125 insertions(+), 54 deletions(-)
 create mode 100644 GUI/coregui/Views/JobWidgets/JobSelectorToolBar.cpp
 create mode 100644 GUI/coregui/Views/JobWidgets/JobSelectorToolBar.h

diff --git a/GUI/coregui/Views/JobWidgets/JobSelectorActions.cpp b/GUI/coregui/Views/JobWidgets/JobSelectorActions.cpp
index 837fc07619a..feba99d018e 100644
--- a/GUI/coregui/Views/JobWidgets/JobSelectorActions.cpp
+++ b/GUI/coregui/Views/JobWidgets/JobSelectorActions.cpp
@@ -22,44 +22,34 @@
 #include <QItemSelectionModel>
 #include <QMenu>
 
-JobSelectorActions::JobSelectorActions(JobModel *jobModel, QObject *parent)
+JobSelectorActions::JobSelectorActions(JobModel* jobModel, QObject* parent)
     : QObject(parent)
-    , m_runJobAction(0)
-    , m_removeJobAction(0)
-    , m_selectionModel(0)
+    , m_runJobAction(nullptr)
+    , m_removeJobAction(nullptr)
+    , m_selectionModel(nullptr)
     , m_jobModel(jobModel)
 {
     m_runJobAction = new QAction(QStringLiteral("Run"), this);
     m_runJobAction->setIcon(QIcon(":/images/toolbar16light_run.svg"));
     m_runJobAction->setToolTip("Run currently selected job");
-    connect(m_runJobAction, SIGNAL(triggered()), this, SLOT(onRunJob()));
+    connect(m_runJobAction, &QAction::triggered, this, &JobSelectorActions::onRunJob);
 
-    // plot properties button
     m_removeJobAction = new QAction(QStringLiteral("Remove"), this);
     m_removeJobAction->setIcon(QIcon(":/images/toolbar16light_recycle.svg"));
     m_removeJobAction->setToolTip("Remove currently selected job.");
-    connect(m_removeJobAction, SIGNAL(triggered()), this, SLOT(onRemoveJob()));
+    connect(m_removeJobAction, &QAction::triggered, this, &JobSelectorActions::onRemoveJob);
 }
 
-void JobSelectorActions::setSelectionModel(QItemSelectionModel *selectionModel)
+void JobSelectorActions::setSelectionModel(QItemSelectionModel* selectionModel)
 {
     m_selectionModel = selectionModel;
 }
 
-//! Adds local actions to the external toolbar
-
-void JobSelectorActions::setToolBar(StyledToolBar *toolBar)
-{
-    toolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
-    toolBar->addAction(m_runJobAction);
-    toolBar->addAction(m_removeJobAction);
-}
-
 void JobSelectorActions::onRunJob()
 {
     QModelIndexList indexList = m_selectionModel->selectedIndexes();
-    foreach(QModelIndex index, indexList) {
-        if(canRunJob(index))
+    foreach (QModelIndex index, indexList) {
+        if (canRunJob(index))
             m_jobModel->runJob(index);
     }
 }
@@ -69,19 +59,18 @@ void JobSelectorActions::onRemoveJob()
     Q_ASSERT(m_selectionModel);
     QModelIndexList indexList = m_selectionModel->selectedIndexes();
 
-    while(indexList.size()) {
-        if(canRemoveJob(indexList.first())) {
+    while (indexList.size()) {
+        if (canRemoveJob(indexList.first())) {
             m_jobModel->removeJob(indexList.first());
             indexList = m_selectionModel->selectedIndexes();
         }
     }
-
 }
 
 //! Generates context menu at given point. If indexAtPoint is provided, the actions will be done
 //! for corresponding JobItem
 
-void JobSelectorActions::onContextMenuRequest(const QPoint &point, const QModelIndex &indexAtPoint)
+void JobSelectorActions::onContextMenuRequest(const QPoint& point, const QModelIndex& indexAtPoint)
 {
     QMenu menu;
     initItemContextMenu(menu, indexAtPoint);
@@ -89,15 +78,15 @@ void JobSelectorActions::onContextMenuRequest(const QPoint &point, const QModelI
     setAllActionsEnabled(true);
 }
 
-void JobSelectorActions::initItemContextMenu(QMenu &menu, const QModelIndex &indexAtPoint)
+void JobSelectorActions::initItemContextMenu(QMenu& menu, const QModelIndex& indexAtPoint)
 {
     menu.addAction(m_runJobAction);
     menu.addAction(m_removeJobAction);
 
     QModelIndex targetIndex = indexAtPoint;
-    if(!targetIndex.isValid()) {
+    if (!targetIndex.isValid()) {
         QModelIndexList indexList = m_selectionModel->selectedIndexes();
-        if(indexList.size())
+        if (indexList.size())
             targetIndex = indexList.first();
     }
 
@@ -111,21 +100,23 @@ void JobSelectorActions::setAllActionsEnabled(bool value)
     m_removeJobAction->setEnabled(value);
 }
 
-bool JobSelectorActions::canRunJob(const QModelIndex &index) const
+bool JobSelectorActions::canRunJob(const QModelIndex& index) const
 {
-    if(!index.isValid()) return false;
+    if (!index.isValid())
+        return false;
 
-    const JobItem *jobItem = m_jobModel->getJobItemForIndex(index);
-    if(jobItem->isRunning()) return false;
-    return true;
+    const JobItem* jobItem = m_jobModel->getJobItemForIndex(index);
+    return jobItem->isRunning() ? false : true;
 }
 
-bool JobSelectorActions::canRemoveJob(const QModelIndex &index) const
+bool JobSelectorActions::canRemoveJob(const QModelIndex& index) const
 {
-    if(!index.isValid()) return false;
+    if (!index.isValid())
+        return false;
 
-    const JobItem *jobItem = m_jobModel->getJobItemForIndex(index);
-    if(jobItem->isRunning()) return false;
+    const JobItem* jobItem = m_jobModel->getJobItemForIndex(index);
+    if (jobItem->isRunning())
+        return false;
 
     return true;
 }
diff --git a/GUI/coregui/Views/JobWidgets/JobSelectorActions.h b/GUI/coregui/Views/JobWidgets/JobSelectorActions.h
index 6227e5c164b..b5c81974eb3 100644
--- a/GUI/coregui/Views/JobWidgets/JobSelectorActions.h
+++ b/GUI/coregui/Views/JobWidgets/JobSelectorActions.h
@@ -29,30 +29,29 @@ class QModelIndex;
 //! The JobSelectorActions class contains actions to run/remove jobs. Actions are used by the
 //! toolbar and JobSelectorList's context menu.
 
-class BA_CORE_API_ JobSelectorActions : public QObject {
+class BA_CORE_API_ JobSelectorActions : public QObject
+{
     Q_OBJECT
 public:
-    JobSelectorActions(JobModel *jobModel, QObject *parent = 0);
+    JobSelectorActions(JobModel* jobModel, QObject* parent = 0);
 
-    void setSelectionModel(QItemSelectionModel *selectionModel);
-    void setToolBar(class StyledToolBar *toolBar);
+    void setSelectionModel(QItemSelectionModel* selectionModel);
 
 public slots:
     void onRunJob();
     void onRemoveJob();
-    void onContextMenuRequest(const QPoint &point, const QModelIndex &indexAtPoint = QModelIndex());
+    void onContextMenuRequest(const QPoint& point, const QModelIndex& indexAtPoint = QModelIndex());
 
 private:
-    void initItemContextMenu(class QMenu &menu, const QModelIndex &indexAtPoint);
+    void initItemContextMenu(class QMenu& menu, const QModelIndex& indexAtPoint);
     void setAllActionsEnabled(bool value);
-    bool canRunJob(const QModelIndex &index) const;
-    bool canRemoveJob(const QModelIndex &index) const;
-
-    QAction *m_runJobAction;
-    QAction *m_removeJobAction;
-    //QSignalMapper *m_signalMapper;
-    QItemSelectionModel *m_selectionModel;
-    JobModel *m_jobModel;
+    bool canRunJob(const QModelIndex& index) const;
+    bool canRemoveJob(const QModelIndex& index) const;
+
+    QAction* m_runJobAction;
+    QAction* m_removeJobAction;
+    QItemSelectionModel* m_selectionModel;
+    JobModel* m_jobModel;
 };
 
 #endif // JOBSELECTORACTIONS_H
diff --git a/GUI/coregui/Views/JobWidgets/JobSelectorToolBar.cpp b/GUI/coregui/Views/JobWidgets/JobSelectorToolBar.cpp
new file mode 100644
index 00000000000..6b1ec1ee4e3
--- /dev/null
+++ b/GUI/coregui/Views/JobWidgets/JobSelectorToolBar.cpp
@@ -0,0 +1,40 @@
+// ************************************************************************** //
+//
+//  BornAgain: simulate and fit scattering at grazing incidence
+//
+//! @file      GUI/coregui/Views/JobWidgets/JobSelectorToolBar.cpp
+//! @brief     Implements class JobSelectorToolBar
+//!
+//! @homepage  http://www.bornagainproject.org
+//! @license   GNU General Public License v3 or higher (see COPYING)
+//! @copyright Forschungszentrum Jülich GmbH 2016
+//! @authors   Scientific Computing Group at MLZ Garching
+//! @authors   Céline Durniak, Marina Ganeva, David Li, Gennady Pospelov
+//! @authors   Walter Van Herck, Joachim Wuttke
+//
+// ************************************************************************** //
+
+#include "JobSelectorToolBar.h"
+#include "JobSelectorActions.h"
+#include <QToolButton>
+
+JobSelectorToolBar::JobSelectorToolBar(JobSelectorActions* actions, QWidget* parent)
+    : StyledToolBar(parent)
+    , m_runJobButton(new QToolButton)
+    , m_removeJobButton(new QToolButton)
+{
+    m_runJobButton->setText(QStringLiteral("Run"));
+    m_runJobButton->setIcon(QIcon(":/images/toolbar16light_run.svg"));
+    m_runJobButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
+    m_runJobButton->setToolTip("Run currently selected job");
+    addWidget(m_runJobButton);
+
+    m_removeJobButton->setText("Remove");
+    m_removeJobButton->setIcon(QIcon(":/images/toolbar16light_recycle.svg"));
+    m_removeJobButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
+    m_removeJobButton->setToolTip("Remove currently selected job.");
+    addWidget(m_removeJobButton);
+
+    connect(m_runJobButton, &QToolButton::clicked, actions, &JobSelectorActions::onRunJob);
+    connect(m_removeJobButton, &QToolButton::clicked, actions, &JobSelectorActions::onRemoveJob);
+}
diff --git a/GUI/coregui/Views/JobWidgets/JobSelectorToolBar.h b/GUI/coregui/Views/JobWidgets/JobSelectorToolBar.h
new file mode 100644
index 00000000000..e6b097f7f49
--- /dev/null
+++ b/GUI/coregui/Views/JobWidgets/JobSelectorToolBar.h
@@ -0,0 +1,41 @@
+// ************************************************************************** //
+//
+//  BornAgain: simulate and fit scattering at grazing incidence
+//
+//! @file      GUI/coregui/Views/JobWidgets/JobSelectorToolBar.h
+//! @brief     Defines class JobSelectorToolBar
+//!
+//! @homepage  http://www.bornagainproject.org
+//! @license   GNU General Public License v3 or higher (see COPYING)
+//! @copyright Forschungszentrum Jülich GmbH 2016
+//! @authors   Scientific Computing Group at MLZ Garching
+//! @authors   Céline Durniak, Marina Ganeva, David Li, Gennady Pospelov
+//! @authors   Walter Van Herck, Joachim Wuttke
+//
+// ************************************************************************** //
+
+#ifndef JOBSELECTORTOOLBAR_H
+#define JOBSELECTORTOOLBAR_H
+
+#include "StyledToolBar.h"
+
+class QAction;
+class QToolButton;
+class JobSelectorActions;
+
+//! Styled tool bar on top of JobSelector with run/remove job buttons.
+
+class BA_CORE_API_ JobSelectorToolBar : public StyledToolBar
+{
+    Q_OBJECT
+
+public:
+    explicit JobSelectorToolBar(JobSelectorActions* actions, QWidget* parent = nullptr);
+
+private:
+    QToolButton* m_runJobButton;
+    QToolButton* m_removeJobButton;
+};
+
+#endif // INSTRUMENTVIEWTOOLBAR_H
+
diff --git a/GUI/coregui/Views/JobWidgets/JobSelectorWidget.cpp b/GUI/coregui/Views/JobWidgets/JobSelectorWidget.cpp
index 081b5b89f0c..11c0aa3c361 100644
--- a/GUI/coregui/Views/JobWidgets/JobSelectorWidget.cpp
+++ b/GUI/coregui/Views/JobWidgets/JobSelectorWidget.cpp
@@ -19,6 +19,7 @@
 #include "JobListWidget.h"
 #include "JobModel.h"
 #include "JobPropertiesWidget.h"
+#include "JobSelectorToolBar.h"
 #include "JobSelectorActions.h"
 #include "StyledToolBar.h"
 #include "mainwindow_constants.h"
@@ -28,8 +29,8 @@
 JobSelectorWidget::JobSelectorWidget(JobModel* jobModel, QWidget* parent)
     : QWidget(parent)
     , m_splitter(new Manhattan::MiniSplitter)
-    , m_toolBar(new StyledToolBar)
     , m_jobSelectorActions(new JobSelectorActions(jobModel, this))
+    , m_toolBar(new JobSelectorToolBar(m_jobSelectorActions, this))
     , m_jobListWidget(new JobListWidget)
     , m_jobProperties(new JobPropertiesWidget)
     , m_jobModel(nullptr)
@@ -54,7 +55,6 @@ JobSelectorWidget::JobSelectorWidget(JobModel* jobModel, QWidget* parent)
     setLayout(mainLayout);
 
     m_jobSelectorActions->setSelectionModel(m_jobListWidget->selectionModel());
-    m_jobSelectorActions->setToolBar(m_toolBar);
 
     connect(m_jobListWidget, &JobListWidget::contextMenuRequest,
             m_jobSelectorActions, &JobSelectorActions::onContextMenuRequest);
diff --git a/GUI/coregui/Views/JobWidgets/JobSelectorWidget.h b/GUI/coregui/Views/JobWidgets/JobSelectorWidget.h
index 033656146ec..32ffb3cf875 100644
--- a/GUI/coregui/Views/JobWidgets/JobSelectorWidget.h
+++ b/GUI/coregui/Views/JobWidgets/JobSelectorWidget.h
@@ -22,7 +22,7 @@
 
 class JobModel;
 class JobItem;
-class StyledToolBar;
+class JobSelectorToolBar;
 class JobSelectorActions;
 class JobListWidget;
 class JobPropertiesWidget;
@@ -40,7 +40,7 @@ class BA_CORE_API_ JobSelectorWidget : public QWidget
     Q_OBJECT
 
 public:
-    explicit JobSelectorWidget(JobModel* jobModel, QWidget* parent = 0);
+    explicit JobSelectorWidget(JobModel* jobModel, QWidget* parent = nullptr);
 
     void setModel(JobModel* jobModel);
 
@@ -60,8 +60,8 @@ private slots:
 
 private:
     Manhattan::MiniSplitter* m_splitter;
-    StyledToolBar* m_toolBar;
     JobSelectorActions* m_jobSelectorActions;
+    JobSelectorToolBar* m_toolBar;
     JobListWidget* m_jobListWidget;
     JobPropertiesWidget* m_jobProperties;
     JobModel* m_jobModel;
-- 
GitLab