From ab33837b55ed7f89dddebb6b46bde9a93f98ce80 Mon Sep 17 00:00:00 2001
From: Federico Montesino Pouzols <federico.montesino-pouzols@stfc.ac.uk>
Date: Tue, 6 Oct 2015 12:14:36 +0100
Subject: [PATCH] casts and prepare event filter, re #13140

---
 .../Tomography/IImageCoRView.h                | 44 +++++++--
 .../Tomography/ImageCoRPresenter.h            |  3 +
 .../Tomography/ImageCoRViewQtWidget.h         | 12 ++-
 .../src/Tomography/ImageCoRPresenter.cpp      | 31 +++---
 .../src/Tomography/ImageCoRViewQtWidget.cpp   | 96 +++++++++++++------
 .../test/ImageCoRPresenterTest.h              |  5 +-
 .../CustomInterfaces/test/ImageCoRViewMock.h  | 11 ++-
 7 files changed, 139 insertions(+), 63 deletions(-)

diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Tomography/IImageCoRView.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Tomography/IImageCoRView.h
index 4590201f45a..d0094440c0b 100644
--- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Tomography/IImageCoRView.h
+++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Tomography/IImageCoRView.h
@@ -44,6 +44,16 @@ public:
   IImageCoRView(){};
   virtual ~IImageCoRView(){};
 
+  // Selection states
+  enum SelectionState {
+    SelectNone,          ///< Init, or after any reset
+    SelectCoR,
+    SelectROIFirst,
+    SelectROISecond,
+    SelectNormAreaFirst,
+    SelectNormAreaSecond
+  };
+
   /**
    * Sets the user selection. This should guarantee that all widgets
    * are updated (including spin boxes, image, slider through the
@@ -63,14 +73,11 @@ public:
   virtual ImageStackPreParams userSelection() const = 0;
 
   /**
-   * Path to a stack of images that the user has requested to
-   * display. The path would be expected to point to a recognized
-   * directory structure (sample/dark/white) or image file (as a
-   * particular case).
+   * Update to a new state (for example select CoR).
    *
-   * @return directory of file path as a string
+   * @param new state we're transitioning into.
    */
-  virtual std::string stackPath() const = 0;
+  virtual void changeSelectionState(const SelectionState state) = 0;
 
   /**
    * Display a special case of stack of images: individual image, from
@@ -94,8 +101,22 @@ public:
    * @param ws Workspace group where every workspace is a FITS or
    * similar image that has been loaded with LoadFITS or similar
    * algorithm.
+   *
+   * @param the (valid) path for this stack, from which it the
+   * workspace group was loaded, in whatever directory layout is being
+   * used (unknown to this view).
+   */
+  virtual void showStack(Mantid::API::WorkspaceGroup_sptr &ws,
+                         const std::string &m_stackPath) = 0;
+
+  /**
+   * Get the stack of images currently being displayed (it has been
+   * shown using showStack()), as a workspace group.
+   *
+   * @return workspace group containing the individual images, which
+   * can be empty if no stack has been loaded.
    */
-  virtual void showStack(Mantid::API::WorkspaceGroup_sptr &ws) = 0;
+  virtual const Mantid::API::WorkspaceGroup_sptr stack() const = 0;
 
   /**
    * Normally one image (projection for tomography stacks) will be
@@ -150,11 +171,14 @@ public:
   virtual void updateImgWithIndex(size_t idx) = 0;
 
   /**
-   * Get from the user the path/location of a stack of images (or
-   * single image as a particular case).
+   * Get the path/location of a stack of images (or single image as a
+   * particular case) that the user is requesting to display.  The
+   * path would be expected to point to a recognized directory
+   * structure (sample/dark/white) or image file (as a particular
+   * case).
    *
    * @return location (can be a directory, file, etc.) that needs to
-   * be figured out
+   * be figured out elsewhere.
    */
   virtual std::string askImgOrStackPath() = 0;
 
diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Tomography/ImageCoRPresenter.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Tomography/ImageCoRPresenter.h
index 67bb1e1aa60..b0031c5f840 100644
--- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Tomography/ImageCoRPresenter.h
+++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Tomography/ImageCoRPresenter.h
@@ -78,6 +78,9 @@ private:
 
   void loadFITSImage(const std::string &path, const std::string &wsName);
 
+  /// path to the image stack being visualized
+  std::string m_stackPath;
+
   /// Associated view for this presenter (MVP pattern)
   IImageCoRView *const m_view;
 
diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Tomography/ImageCoRViewQtWidget.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Tomography/ImageCoRViewQtWidget.h
index d7b12a2aec6..501924775b0 100644
--- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Tomography/ImageCoRViewQtWidget.h
+++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Tomography/ImageCoRViewQtWidget.h
@@ -59,13 +59,15 @@ public:
 
   ImageStackPreParams userSelection() const;
 
-  virtual std::string stackPath() const { return m_stackPath; };
+  void changeSelectionState(const SelectionState state);
 
   /// show a stack of images given the path to the files
   void showStack(const std::string &path);
 
   /// show a stack of images that have been loaded into a group of workspaces
-  void showStack(Mantid::API::WorkspaceGroup_sptr &ws);
+  void showStack(Mantid::API::WorkspaceGroup_sptr &ws, const std::string &path);
+
+  const Mantid::API::WorkspaceGroup_sptr stack() const { return m_stack; }
 
   void showProjection(const Mantid::API::WorkspaceGroup_sptr &wsg, size_t idx);
 
@@ -130,6 +132,8 @@ private:
   void refreshROI();
   void refreshNormArea();
 
+  bool eventFilter(QObject *obj, QEvent *event);
+
   Ui::ImageSelectCoRAndRegions m_ui;
 
   Mantid::API::WorkspaceGroup_sptr m_stack;
@@ -144,8 +148,8 @@ private:
   /// parameters currently set by the user
   ImageStackPreParams m_params;
 
-  /// path to the image stack being visualized
-  std::string m_stackPath;
+  /// are we picking the CoR, or the first point of the ROI, etc.
+  SelectionState m_selectionState;
 
   // presenter as in the model-view-presenter
   boost::scoped_ptr<IImageCoRPresenter> m_presenter;
diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/Tomography/ImageCoRPresenter.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/Tomography/ImageCoRPresenter.cpp
index b19e3248de3..e65df38784c 100644
--- a/Code/Mantid/MantidQt/CustomInterfaces/src/Tomography/ImageCoRPresenter.cpp
+++ b/Code/Mantid/MantidQt/CustomInterfaces/src/Tomography/ImageCoRPresenter.cpp
@@ -94,9 +94,11 @@ void ImageCoRPresenter::processInit() {
 void ImageCoRPresenter::processBrowseImg() {
   const std::string path = m_view->askImgOrStackPath();
 
-  if (!path.empty()) {
-    processNewStack();
-  }
+  if (path.empty())
+    return;
+
+  m_stackPath = path;
+  processNewStack();
 }
 
 /**
@@ -129,9 +131,7 @@ StackOfImagesDirs ImageCoRPresenter::checkInputStack(const std::string &path) {
 }
 
 void ImageCoRPresenter::processNewStack() {
-  const std::string pstr = m_view->stackPath();
-
-  StackOfImagesDirs soid = checkInputStack(pstr);
+  StackOfImagesDirs soid = checkInputStack(m_stackPath);
 
   std::vector<std::string> imgs = soid.sampleFiles();
   if (0 >= imgs.size()) {
@@ -153,11 +153,11 @@ void ImageCoRPresenter::processNewStack() {
         "Even though a directory apparently holding a stack of images was "
         "found, "
         "it was not possible to load any image file correctly from: " +
-            pstr);
+            m_stackPath);
     return;
   }
 
-  m_view->showStack(wsg);
+  m_view->showStack(wsg, m_stackPath);
 
   // clean-up container group workspace? Not for now
   if (false && wsg)
@@ -168,11 +168,17 @@ void ImageCoRPresenter::processUpdateImgIndex() {
   m_view->updateImgWithIndex(m_view->currentImgIndex());
 }
 
-void ImageCoRPresenter::processSelectCoR() {}
+void ImageCoRPresenter::processSelectCoR() {
+  m_view->changeSelectionState(IImageCoRView::SelectCoR);
+}
 
-void ImageCoRPresenter::processSelectROI() {}
+void ImageCoRPresenter::processSelectROI() {
+  m_view->changeSelectionState(IImageCoRView::SelectROIFirst);
+}
 
-void ImageCoRPresenter::processSelectNormalization() {}
+void ImageCoRPresenter::processSelectNormalization() {
+  m_view->changeSelectionState(IImageCoRView::SelectNormAreaFirst);
+}
 
 void ImageCoRPresenter::processFinishedCoR() {}
 
@@ -182,14 +188,17 @@ void ImageCoRPresenter::processFinishedNormalization() {}
 
 void ImageCoRPresenter::processResetCoR() {
   m_view->resetCoR();
+  m_view->changeSelectionState(IImageCoRView::SelectNone);
 }
 
 void ImageCoRPresenter::processResetROI() {
   m_view->resetROI();
+  m_view->changeSelectionState(IImageCoRView::SelectNone);
 }
 
 void ImageCoRPresenter::processResetNormalization() {
   m_view->resetNormArea();
+  m_view->changeSelectionState(IImageCoRView::SelectNone);
 }
 
 void ImageCoRPresenter::processShutDown() { m_view->saveSettings(); }
diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/Tomography/ImageCoRViewQtWidget.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/Tomography/ImageCoRViewQtWidget.cpp
index 98f7285fc14..109349cbfeb 100644
--- a/Code/Mantid/MantidQt/CustomInterfaces/src/Tomography/ImageCoRViewQtWidget.cpp
+++ b/Code/Mantid/MantidQt/CustomInterfaces/src/Tomography/ImageCoRViewQtWidget.cpp
@@ -23,8 +23,14 @@ const std::string ImageCoRViewQtWidget::m_settingsGroup =
     "CustomInterfaces/ImageCoRView";
 
 ImageCoRViewQtWidget::ImageCoRViewQtWidget(QWidget *parent)
-    : QWidget(parent), IImageCoRView(), m_presenter(NULL) {
+    : QWidget(parent), IImageCoRView(), m_selectionState(SelectNone),
+      m_presenter(NULL) {
   initLayout();
+
+  // using an event filter. might be worth refactoring into a specific
+  // QLabel + selection of ROI+NormArea+CoR class
+  // not using Qwt Pickers to avoid Qwt version issues..
+  m_ui.label_img->installEventFilter(this);
 }
 
 void ImageCoRViewQtWidget::initLayout() {
@@ -107,6 +113,17 @@ void ImageCoRViewQtWidget::setupConnections() {
           SLOT(valueUpdatedNormArea(int)));
 }
 
+bool ImageCoRViewQtWidget::eventFilter(QObject *obj, QEvent *event) {
+  if (m_ui.label_img == obj) {
+    if (event->type() == QEvent::MouseButtonPress) {
+    } else if (event->type() == QEvent::MouseButtonRelease) {
+    } else if (event->type() == QEvent::MouseMove) {
+    }
+  }
+  // pass on the event up to the parent class
+  return false;
+}
+
 void ImageCoRViewQtWidget::valueUpdatedCoR(int) {
   grabCoRFromWidgets();
   refreshROIetAl();
@@ -155,10 +172,14 @@ void ImageCoRViewQtWidget::refreshCoR() {
   QPainter painter(&toDisplay);
   QPen pen(Qt::red);
   painter.setPen(pen);
-  painter.drawLine(m_params.cor.X() - 5, m_params.cor.Y(), m_params.cor.X() + 5,
-                   m_params.cor.Y());
-  painter.drawLine(m_params.cor.X(), m_params.cor.Y() - 5, m_params.cor.X(),
-                   m_params.cor.Y() + 5);
+  painter.drawLine(static_cast<int>(m_params.cor.X() - 5),
+                   static_cast<int>(m_params.cor.Y()),
+                   static_cast<int>(m_params.cor.X() + 5),
+                   static_cast<int>(m_params.cor.Y()));
+  painter.drawLine(static_cast<int>(m_params.cor.X()),
+                   static_cast<int>(m_params.cor.Y() - 5),
+                   static_cast<int>(m_params.cor.X()),
+                   static_cast<int>(m_params.cor.Y() + 5));
   m_ui.label_img->setPixmap(toDisplay);
 }
 
@@ -177,25 +198,31 @@ void ImageCoRViewQtWidget::refreshROIetAl() {
 
   QPen penCoR(Qt::red);
   painter.setPen(penCoR);
-  painter.drawLine(m_params.cor.X() - 5, m_params.cor.Y(), m_params.cor.X() + 5,
-                   m_params.cor.Y());
-  painter.drawLine(m_params.cor.X(), m_params.cor.Y() - 5, m_params.cor.X(),
-                   m_params.cor.Y() + 5);
+  painter.drawLine(static_cast<int>(m_params.cor.X() - 5),
+                   static_cast<int>(m_params.cor.Y()),
+                   static_cast<int>(m_params.cor.X() + 5),
+                   static_cast<int>(m_params.cor.Y()));
+  painter.drawLine(static_cast<int>(m_params.cor.X()),
+                   static_cast<int>(m_params.cor.Y() - 5),
+                   static_cast<int>(m_params.cor.X()),
+                   static_cast<int>(m_params.cor.Y() + 5));
 
   QPen penROI(Qt::green);
   painter.setPen(penROI);
-  painter.drawRect(m_params.roi.first.X(), m_params.roi.first.Y(),
-                   m_params.roi.second.X() - m_params.roi.first.X(),
-                   m_params.roi.second.Y() - m_params.roi.first.Y());
+  painter.drawRect(
+      static_cast<int>(m_params.roi.first.X()),
+      static_cast<int>(m_params.roi.first.Y()),
+      static_cast<int>(m_params.roi.second.X() - m_params.roi.first.X()),
+      static_cast<int>(m_params.roi.second.Y() - m_params.roi.first.Y()));
 
   QPen penNA(Qt::yellow);
   painter.setPen(penNA);
-  painter.drawRect(m_params.normalizationRegion.first.X(),
-                   m_params.normalizationRegion.first.Y(),
-                   m_params.normalizationRegion.second.X() -
-                       m_params.normalizationRegion.first.X(),
-                   m_params.normalizationRegion.second.Y() -
-                       m_params.normalizationRegion.first.Y());
+  painter.drawRect(static_cast<int>(m_params.normalizationRegion.first.X()),
+                   static_cast<int>(m_params.normalizationRegion.first.Y()),
+                   static_cast<int>(m_params.normalizationRegion.second.X() -
+                                    m_params.normalizationRegion.first.X()),
+                   static_cast<int>(m_params.normalizationRegion.second.Y() -
+                                    m_params.normalizationRegion.first.Y()));
 
   m_ui.label_img->setPixmap(toDisplay);
 }
@@ -213,9 +240,11 @@ void ImageCoRViewQtWidget::refreshROI() {
   QPainter painter(&toDisplay);
   QPen pen(Qt::green);
   painter.setPen(pen);
-  painter.drawRect(m_params.roi.first.X(), m_params.roi.first.Y(),
-                   m_params.roi.second.X() - m_params.roi.first.X(),
-                   m_params.roi.second.Y() - m_params.roi.first.Y());
+  painter.drawRect(
+      static_cast<int>(m_params.roi.first.X()),
+      static_cast<int>(m_params.roi.first.Y()),
+      static_cast<int>(m_params.roi.second.X() - m_params.roi.first.X()),
+      static_cast<int>(m_params.roi.second.Y() - m_params.roi.first.Y()));
   m_ui.label_img->setPixmap(toDisplay);
 }
 
@@ -232,12 +261,12 @@ void ImageCoRViewQtWidget::refreshNormArea() {
   QPainter painter(&toDisplay);
   QPen pen(Qt::yellow);
   painter.setPen(pen);
-  painter.drawRect(m_params.normalizationRegion.first.X(),
-                   m_params.normalizationRegion.first.Y(),
-                   m_params.normalizationRegion.second.X() -
-                       m_params.normalizationRegion.first.X(),
-                   m_params.normalizationRegion.second.Y() -
-                       m_params.normalizationRegion.first.Y());
+  painter.drawRect(static_cast<int>(m_params.normalizationRegion.first.X()),
+                   static_cast<int>(m_params.normalizationRegion.first.Y()),
+                   static_cast<int>(m_params.normalizationRegion.second.X() -
+                                    m_params.normalizationRegion.first.X()),
+                   static_cast<int>(m_params.normalizationRegion.second.Y() -
+                                    m_params.normalizationRegion.first.Y()));
   m_ui.label_img->setPixmap(toDisplay);
 }
 
@@ -306,6 +335,11 @@ ImageStackPreParams ImageCoRViewQtWidget::userSelection() const {
   return m_params;
 }
 
+void ImageCoRViewQtWidget::changeSelectionState(
+    const IImageCoRView::SelectionState state) {
+  m_selectionState = state;
+}
+
 void ImageCoRViewQtWidget::corClicked() {
   m_presenter->notify(IImageCoRPresenter::SelectCoR);
 }
@@ -324,7 +358,7 @@ void ImageCoRViewQtWidget::normAreaClicked() {
   m_presenter->notify(IImageCoRPresenter::SelectROI);
 }
 void ImageCoRViewQtWidget::normAreaResetClicked() {
-  m_presenter->notify(IImageCoRPresenter::ResetROI);
+  m_presenter->notify(IImageCoRPresenter::ResetNormalization);
 }
 
 void ImageCoRViewQtWidget::browseImgClicked() {
@@ -391,8 +425,7 @@ std::string ImageCoRViewQtWidget::askImgOrStackPath() {
     MantidQt::API::AlgorithmInputHistory::Instance().setPreviousDirectory(path);
   }
 
-  m_stackPath = path.toStdString();
-  return m_stackPath;
+  return path.toStdString();
 }
 
 void ImageCoRViewQtWidget::showStack(const std::string & /*path*/) {
@@ -403,7 +436,8 @@ void ImageCoRViewQtWidget::showStack(const std::string & /*path*/) {
   // b) load as workspace group - this is done in the overloaded method below
 }
 
-void ImageCoRViewQtWidget::showStack(Mantid::API::WorkspaceGroup_sptr &wsg) {
+void ImageCoRViewQtWidget::showStack(Mantid::API::WorkspaceGroup_sptr &wsg,
+                                     const std::string &stackPath) {
   if (0 == wsg->size())
     return;
 
diff --git a/Code/Mantid/MantidQt/CustomInterfaces/test/ImageCoRPresenterTest.h b/Code/Mantid/MantidQt/CustomInterfaces/test/ImageCoRPresenterTest.h
index 7e04ed47ef6..41fdb967e99 100644
--- a/Code/Mantid/MantidQt/CustomInterfaces/test/ImageCoRPresenterTest.h
+++ b/Code/Mantid/MantidQt/CustomInterfaces/test/ImageCoRPresenterTest.h
@@ -76,12 +76,11 @@ public:
     EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);
 
     // should not get there:
-    EXPECT_CALL(mockView, stackPath()).Times(0);
     EXPECT_CALL(mockView, showStack(testing::An<const std::string &>()))
         .Times(0);
     EXPECT_CALL(mockView,
-                showStack(testing::An<Mantid::API::WorkspaceGroup_sptr &>()))
-        .Times(0);
+                showStack(testing::An<Mantid::API::WorkspaceGroup_sptr &>(),
+                          testing::An<const std::string &>())).Times(0);
     EXPECT_CALL(mockView, updateImgWithIndex(testing::_)).Times(0);
 
     pres.notify(IImageCoRPresenter::BrowseImgOrStack);
diff --git a/Code/Mantid/MantidQt/CustomInterfaces/test/ImageCoRViewMock.h b/Code/Mantid/MantidQt/CustomInterfaces/test/ImageCoRViewMock.h
index 17e886ce5bb..297bc1b4918 100644
--- a/Code/Mantid/MantidQt/CustomInterfaces/test/ImageCoRViewMock.h
+++ b/Code/Mantid/MantidQt/CustomInterfaces/test/ImageCoRViewMock.h
@@ -15,14 +15,17 @@ public:
   MOCK_CONST_METHOD0(userSelection,
                      MantidQt::CustomInterfaces::ImageStackPreParams());
 
-  // virtual std::string stackPath() const;
-  MOCK_CONST_METHOD0(stackPath, std::string());
+  // void changeSelectionState(const SelectionState state);
+  MOCK_METHOD1(changeSelectionState, void(IImageCoRView::SelectionState));
 
   // void showStack(const std::string &path);
   MOCK_METHOD1(showStack, void(const std::string &));
 
   // void showStack(const Mantid::API::WorkspaceGroup_sptr &ws);
-  MOCK_METHOD1(showStack, void(Mantid::API::WorkspaceGroup_sptr &));
+  MOCK_METHOD2(showStack, void(Mantid::API::WorkspaceGroup_sptr &, const std::string &));
+
+  // const Mantid::API::WorkspaceGroup_sptr stack() const;
+  MOCK_CONST_METHOD0(stack, const Mantid::API::WorkspaceGroup_sptr());
 
   // void showProjection(const Mantid::API::WorkspaceGroup_sptr &wsg, size_t idx);
   MOCK_METHOD2(showProjection, void(const Mantid::API::WorkspaceGroup_sptr &wsg, size_t idx));
@@ -41,7 +44,7 @@ public:
   // void updateImgWithIndex(size_t idx)
   MOCK_METHOD1(updateImgWithIndex, void(size_t));
 
-  // std::string askImgOrStackPath() = 0;
+  // std::string askImgOrStackPath();
   MOCK_METHOD0(askImgOrStackPath, std::string());
 
   // void saveSettings() const {}
-- 
GitLab