From d53548fb5b09e772fbc07860b10c27e283ae3231 Mon Sep 17 00:00:00 2001
From: Martyn Gigg <martyn.gigg@gmail.com>
Date: Wed, 17 Jan 2018 21:44:31 +0000
Subject: [PATCH] Fill plot selection dialog with workspace details

Refs #21567
---
 .../workbench/plugins/workspacewidget.py      |  6 +-
 qt/python/CMakeLists.txt                      |  2 +
 .../workspacewidget/plotselectiondialog.ui    | 39 +++++++------
 .../widgets/workspacewidget/test/__init__.py  |  0
 .../test/test_plotselectiondialog.py          | 56 +++++++++++++++++++
 .../workspacewidget/workspacetreewidget.py    | 34 ++++++++++-
 6 files changed, 116 insertions(+), 21 deletions(-)
 create mode 100644 qt/python/mantidqt/widgets/workspacewidget/test/__init__.py
 create mode 100644 qt/python/mantidqt/widgets/workspacewidget/test/test_plotselectiondialog.py

diff --git a/qt/applications/workbench/workbench/plugins/workspacewidget.py b/qt/applications/workbench/workbench/plugins/workspacewidget.py
index fa8480f95af..e87b87f93a5 100644
--- a/qt/applications/workbench/workbench/plugins/workspacewidget.py
+++ b/qt/applications/workbench/workbench/plugins/workspacewidget.py
@@ -19,6 +19,7 @@ from __future__ import (absolute_import, unicode_literals)
 # system imports
 
 # third-party library imports
+from mantid.api import AnalysisDataService
 from mantidqt.widgets.workspacewidget.mantidtreemodel import MantidTreeModel
 from mantidqt.widgets.workspacewidget.workspacetreewidget import PlotSelectionDialog, WorkspaceTreeWidget
 from qtpy.QtWidgets import QVBoxLayout
@@ -53,6 +54,7 @@ class WorkspaceWidget(PluginWidget):
 
     # ----------------- Behaviour --------------------
 
-    def _do_plot1d(self):
-        selection_dlg = PlotSelectionDialog([], parent=self)
+    def _do_plot1d(self, selected_ws):
+        workspaces = [AnalysisDataService.Instance()[selected_ws.encode('utf-8')]]
+        selection_dlg = PlotSelectionDialog(workspaces, parent=self)
         selection_dlg.exec_()
diff --git a/qt/python/CMakeLists.txt b/qt/python/CMakeLists.txt
index e9ba664c036..d4977026709 100644
--- a/qt/python/CMakeLists.txt
+++ b/qt/python/CMakeLists.txt
@@ -66,6 +66,8 @@ if ( ENABLE_WORKBENCH )
     mantidqt/widgets/codeeditor/test/test_interpreter.py
     mantidqt/widgets/codeeditor/test/test_multifileinterpreter.py
 
+    mantidqt/widgets/workspacewidget/test/test_plotselectiondialog.py
+
     mantidqt/widgets/test/test_algorithmselector.py
     mantidqt/widgets/test/test_jupyterconsole.py
     mantidqt/widgets/test/test_messagedisplay.py
diff --git a/qt/python/mantidqt/widgets/workspacewidget/plotselectiondialog.ui b/qt/python/mantidqt/widgets/workspacewidget/plotselectiondialog.ui
index e8f0f3e32ec..51e988e2a8f 100644
--- a/qt/python/mantidqt/widgets/workspacewidget/plotselectiondialog.ui
+++ b/qt/python/mantidqt/widgets/workspacewidget/plotselectiondialog.ui
@@ -14,19 +14,6 @@
    <string>Plot 1D</string>
   </property>
   <layout class="QGridLayout" name="gridLayout">
-   <item row="0" column="1">
-    <widget class="QLineEdit" name="specNums"/>
-   </item>
-   <item row="1" column="1">
-    <widget class="QLineEdit" name="wkspIndices"/>
-   </item>
-   <item row="1" column="0">
-    <widget class="QLabel" name="label_2">
-     <property name="text">
-      <string>Workspace Indices:</string>
-     </property>
-    </widget>
-   </item>
    <item row="0" column="0">
     <widget class="QLabel" name="label">
      <property name="text">
@@ -34,7 +21,7 @@
      </property>
     </widget>
    </item>
-   <item row="3" column="0">
+   <item row="4" column="0">
     <widget class="QLabel" name="label_3">
      <property name="text">
       <string>Plot type:</string>
@@ -44,7 +31,7 @@
      </property>
     </widget>
    </item>
-   <item row="4" column="1">
+   <item row="5" column="1">
     <widget class="QDialogButtonBox" name="buttonBox">
      <property name="standardButtons">
       <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::YesToAll</set>
@@ -54,7 +41,20 @@
      </property>
     </widget>
    </item>
-   <item row="3" column="1">
+   <item row="0" column="1">
+    <widget class="QLineEdit" name="specNums"/>
+   </item>
+   <item row="2" column="0">
+    <widget class="QLabel" name="label_2">
+     <property name="text">
+      <string>Workspace Indices:</string>
+     </property>
+    </widget>
+   </item>
+   <item row="2" column="1">
+    <widget class="QLineEdit" name="wkspIndices"/>
+   </item>
+   <item row="4" column="1">
     <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
       <widget class="QComboBox" name="plotType">
@@ -86,6 +86,13 @@
      </item>
     </layout>
    </item>
+   <item row="1" column="1">
+    <widget class="QLabel" name="label_4">
+     <property name="text">
+      <string>Or</string>
+     </property>
+    </widget>
+   </item>
   </layout>
  </widget>
  <resources/>
diff --git a/qt/python/mantidqt/widgets/workspacewidget/test/__init__.py b/qt/python/mantidqt/widgets/workspacewidget/test/__init__.py
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/qt/python/mantidqt/widgets/workspacewidget/test/test_plotselectiondialog.py b/qt/python/mantidqt/widgets/workspacewidget/test/test_plotselectiondialog.py
new file mode 100644
index 00000000000..707f782719d
--- /dev/null
+++ b/qt/python/mantidqt/widgets/workspacewidget/test/test_plotselectiondialog.py
@@ -0,0 +1,56 @@
+#  This file is part of the mantidqt package
+#
+#  Copyright (C) 2017 mantidproject
+#
+#  This program is free software: you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation, either version 3 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# std imports
+import unittest
+
+# 3rdparty imports
+from mantid.simpleapi import CreateSampleWorkspace, CropWorkspace
+
+# local imports
+from mantidqt.utils.qt.testing import requires_qapp
+from mantidqt.widgets.workspacewidget.workspacetreewidget import PlotSelectionDialog
+
+
+@requires_qapp
+class PlotSelectionDialogTest(unittest.TestCase):
+
+    def test_filling_workspace_details_single_workspace(self):
+        workspaces = [CreateSampleWorkspace(OutputWorkspace='ws', StoreInADS=False)]
+        dlg = PlotSelectionDialog(workspaces)
+        self.assertEqual("1-200", dlg._ui.specNums.placeholderText())
+        self.assertEqual("0-199", dlg._ui.wkspIndices.placeholderText())
+
+    def test_filling_workspace_details_multiple_workspaces_of_same_size(self):
+        workspaces = [CreateSampleWorkspace(OutputWorkspace='ws', StoreInADS=False),
+                      CreateSampleWorkspace(OutputWorkspace='ws2', StoreInADS=False)]
+        dlg = PlotSelectionDialog(workspaces)
+        self.assertEqual("1-200", dlg._ui.specNums.placeholderText())
+        self.assertEqual("0-199", dlg._ui.wkspIndices.placeholderText())
+
+    def test_filling_workspace_details_multiple_workspaces_of_different_sizes(self):
+        ws1 = CreateSampleWorkspace(OutputWorkspace='ws', NumBanks=1, StoreInADS=False)
+        ws1 = CropWorkspace(ws1, StartWorkspaceIndex=50)
+        ws2 = CreateSampleWorkspace(OutputWorkspace='ws', StoreInADS=False)
+
+        dlg = PlotSelectionDialog([ws1, ws2])
+        self.assertEqual("51-100", dlg._ui.specNums.placeholderText())
+        self.assertEqual("0-49", dlg._ui.wkspIndices.placeholderText())
+
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/qt/python/mantidqt/widgets/workspacewidget/workspacetreewidget.py b/qt/python/mantidqt/widgets/workspacewidget/workspacetreewidget.py
index a43c29eae54..83fcbb4b2fd 100644
--- a/qt/python/mantidqt/widgets/workspacewidget/workspacetreewidget.py
+++ b/qt/python/mantidqt/widgets/workspacewidget/workspacetreewidget.py
@@ -16,8 +16,10 @@
 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 from __future__ import (absolute_import)
 
+# std imports
+
 # 3rd party imports
-from qtpy.QtWidgets import QDialog, QVBoxLayout
+from qtpy.QtWidgets import QDialogButtonBox
 
 # local imports
 from mantidqt.utils.qt import import_qtlib, load_ui
@@ -32,9 +34,35 @@ PlotSelectionDialogUI, PlotSelectionDialogUIBase = load_ui(__file__, 'plotselect
 class PlotSelectionDialog(PlotSelectionDialogUIBase):
 
     def __init__(self, workspaces,
-               parent=None):
+                 parent=None):
         super(PlotSelectionDialog, self).__init__(parent)
+        # attributes
+        self._workspaces = workspaces
 
+        # layout
         ui = PlotSelectionDialogUI()
         ui.setupUi(self)
-        self.ui = ui
+        self._ui = ui
+        # overwrite the "Yes to All" button text
+        ui.buttonBox.button(QDialogButtonBox.YesToAll).setText('Plot All')
+
+        # populate details
+        self._fill_workspace_details()
+
+    # ------------------- Private -------------------------
+    def _fill_workspace_details(self):
+        """Sets placeholder text to indicate the ranges possible"""
+        workspaces = self._workspaces
+        # workspace index range
+        wi_max = min([ws.getNumberHistograms() - 1 for ws in workspaces])
+        self._ui.wkspIndices.setPlaceholderText("{}-{}".format(0, wi_max))
+
+        # spectra range
+        ws_spectra = [{ws.getSpectrum(i).getSpectrumNo() for i in range(ws.getNumberHistograms())} for ws in workspaces]
+        plottable =  ws_spectra[0]
+        if len(ws_spectra) > 1:
+            for sp_set in ws_spectra[1:]:
+                plottable = plottable.intersection(sp_set)
+        plottable = sorted(plottable)
+        spec_min, spec_max = min(plottable), max(plottable)
+        self._ui.specNums.setPlaceholderText("{}-{}".format(spec_min, spec_max))
-- 
GitLab