Exemple #1
0
    def __init__(
        self,
        ws,
        parent=None,
        window_flags=Qt.Window,
        plot=None,
        model=None,
        view=None,
        name=None,
        ads_observer=None,
        container=None,
        window_width=600,
        window_height=400,
        batch=False,
    ):
        """
        Creates a display for the provided workspace.

        :param ws: Workspace to be displayed
        :param parent: Parent of the widget
        :param window_flags: An optional set of window flags
        :param plot: Plotting function that will be used to plot workspaces. This requires Matplotlib directly.
                     Passed in as parameter to allow mocking
        :param model: Model to be used by the widget. Passed in as parameter to allow mocking
        :param view: View to be used by the widget. Passed in as parameter to allow mocking
        :param name: Custom name for the window
        :param ads_observer: ADS observer to be used by the presenter. If not provided the default
                             one is used. Mainly intended for testing.
        """
        view, model = self.create_table(ws, parent, window_flags, model, view,
                                        batch)
        self.view = view
        self.model = model
        self.name = name if name else model.get_name()
        self.container = (container if container else StatusBarView(
            parent,
            view,
            self.name,
            window_width=window_width,
            window_height=window_height,
            window_flags=window_flags,
            presenter=self,
        ))

        DataCopier.__init__(self, self.container.status_bar)

        self.parent = parent
        self.plot = plot

        self.ads_observer = (ads_observer if ads_observer else
                             WorkspaceDisplayADSObserver(self))

        self.presenter.refresh()
Exemple #2
0
    def setUp(self):
        self.mock_status_bar = MockQStatusBar()
        self.mock_clipboard = MockQClipboard()
        self.mock_clipboard.setText = Mock()
        self.data_copier = DataCopier(self.mock_status_bar)

        mock_selection_model = MockQSelectionModel(has_selection=True)
        mock_selection_model.selectedRows = Mock(
            return_value=[MockQModelIndex(1, 1), MockQModelIndex(2, 2), MockQModelIndex(3, 3)])
        mock_selection_model.selectedColumns = Mock(
            return_value=[MockQModelIndex(1, 1), MockQModelIndex(2, 2), MockQModelIndex(3, 3)])
        self.table = Mock(spec=TableWorkspaceDisplayView)
        self.table.mock_selection_model = mock_selection_model
Exemple #3
0
    def __init__(self,
                 ws,
                 plot=None,
                 parent=None,
                 model=None,
                 view=None,
                 name=None,
                 ads_observer=None,
                 container=None,
                 window_width=600,
                 window_height=400):
        """
        Creates a display for the provided workspace.

        :param ws: Workspace to be displayed
        :param parent: Parent of the widget
        :param plot: Plotting function that will be used to plot workspaces. This requires Matplotlib directly.
                     Passed in as parameter to allow mocking
        :param model: Model to be used by the widget. Passed in as parameter to allow mocking
        :param view: View to be used by the widget. Passed in as parameter to allow mocking
        :param name: Custom name for the window
        :param ads_observer: ADS observer to be used by the presenter. If not provided the default
                             one is used. Mainly intended for testing.
        """
        model = model if model is not None else TableWorkspaceDisplayModel(ws)
        view = view if view else TableWorkspaceDisplayView(self, parent)
        TableWorkspaceDataPresenter.__init__(self, model, view)

        self.name = name if name else self.model.get_name()
        self.container = container if container else StatusBarView(
            parent,
            self.view,
            self.name,
            window_width=window_width,
            window_height=window_height,
            presenter=self)

        DataCopier.__init__(self, self.container.status_bar)

        self.parent = parent
        self.plot = plot
        self.view.set_context_menu_actions(self.view)

        self.ads_observer = ads_observer if ads_observer else WorkspaceDisplayADSObserver(
            self)

        self.refresh()

        # connect to cellChanged signal after the data has been loaded
        self.view.itemChanged.connect(self.handleItemChanged)
Exemple #4
0
class DataCopierTest(TestCase):
    show_mouse_toast_package = 'mantidqt.widgets.workspacedisplay.user_notifier.UserNotifier.show_mouse_toast'
    copy_to_clipboard_package = 'mantidqt.widgets.workspacedisplay.data_copier.DataCopier.copy_to_clipboard'

    def assertNotCalled(self, mock):
        self.assertEqual(0, mock.call_count)

    def setUp(self):
        self.mock_status_bar = MockQStatusBar()
        self.mock_clipboard = MockQClipboard()
        self.mock_clipboard.setText = Mock()
        self.data_copier = DataCopier(self.mock_status_bar)

        mock_selection_model = MockQSelectionModel(has_selection=True)
        mock_selection_model.selectedRows = Mock(return_value=[
            MockQModelIndex(1, 1),
            MockQModelIndex(2, 2),
            MockQModelIndex(3, 3)
        ])
        mock_selection_model.selectedColumns = Mock(return_value=[
            MockQModelIndex(1, 1),
            MockQModelIndex(2, 2),
            MockQModelIndex(3, 3)
        ])
        self.table = Mock(spec=TableWorkspaceDisplayView)
        self.table.mock_selection_model = mock_selection_model

    @patch(show_mouse_toast_package)
    @patch(copy_to_clipboard_package)
    def test_action_copy_spectrum_values(self, mock_copy,
                                         mock_show_mouse_toast):
        mock_table = MockQTableView()

        # two rows are selected in different positions
        mock_indexes = [MockQModelIndex(0, 1), MockQModelIndex(3, 1)]
        mock_table.mock_selection_model.selectedRows = Mock(
            return_value=mock_indexes)

        mock_read = Mock(return_value=[43, 99])
        expected_string = "43\t99\n43\t99"

        self.data_copier.copy_spectrum_values(mock_table, mock_read)

        mock_table.selectionModel.assert_called_once_with()
        mock_table.mock_selection_model.hasSelection.assert_called_once_with()
        mock_copy.assert_called_once_with(expected_string)
        mock_show_mouse_toast.assert_called_once_with(
            UserNotifier.COPY_SUCCESSFUL_MESSAGE)

    @patch(show_mouse_toast_package)
    @patch(copy_to_clipboard_package)
    def test_action_copy_spectrum_values_no_selection(self, mock_copy,
                                                      mock_show_mouse_toast):
        mock_table = MockQTableView()
        mock_table.mock_selection_model.hasSelection = Mock(return_value=False)
        mock_table.mock_selection_model.selectedRows = Mock()

        self.data_copier.copy_spectrum_values(mock_table, ws_read=None)

        mock_table.selectionModel.assert_called_once_with()
        mock_table.mock_selection_model.hasSelection.assert_called_once_with()
        # the action should never look for rows if there is no selection
        self.assertNotCalled(mock_table.mock_selection_model.selectedRows)
        self.assertNotCalled(mock_copy)
        mock_show_mouse_toast.assert_called_once_with(
            UserNotifier.NO_SELECTION_MESSAGE)

    @patch(show_mouse_toast_package)
    @patch(copy_to_clipboard_package)
    def test_action_copy_bin_values(self, mock_copy, mock_show_mouse_toast):
        mock_table = MockQTableView()

        # two columns are selected at different positions
        mock_indexes = [MockQModelIndex(0, 0), MockQModelIndex(0, 3)]
        mock_table.mock_selection_model.selectedColumns = Mock(
            return_value=mock_indexes)
        # change the mock ws to have 3 histograms
        num_hist = 3

        mock_read = Mock(return_value=[83, 11, 33, 70])
        expected_string = "83\t70\n83\t70\n83\t70"

        self.data_copier.copy_bin_values(mock_table, mock_read, num_hist)

        mock_table.selectionModel.assert_called_once_with()
        mock_table.mock_selection_model.hasSelection.assert_called_once_with()
        mock_copy.assert_called_once_with(expected_string)
        mock_show_mouse_toast.assert_called_once_with(
            UserNotifier.COPY_SUCCESSFUL_MESSAGE)

    @patch(show_mouse_toast_package)
    @patch(copy_to_clipboard_package)
    def test_action_copy_bin_values_no_selection(self, mock_copy,
                                                 mock_show_mouse_toast):
        mock_table = MockQTableView()
        mock_table.mock_selection_model.hasSelection = Mock(return_value=False)
        mock_table.mock_selection_model.selectedColumns = Mock()

        self.data_copier.copy_bin_values(mock_table, None, None)

        mock_table.selectionModel.assert_called_once_with()
        mock_table.mock_selection_model.hasSelection.assert_called_once_with()
        # the action should never look for rows if there is no selection
        self.assertNotCalled(mock_table.mock_selection_model.selectedColumns)
        self.assertNotCalled(mock_copy)
        mock_show_mouse_toast.assert_called_once_with(
            UserNotifier.NO_SELECTION_MESSAGE)

    @patch(show_mouse_toast_package)
    @patch(copy_to_clipboard_package)
    def test_copy_cells_no_selection(self, mock_copy, mock_show_mouse_toast):
        mock_table = MockQTableView()
        mock_table.mock_selection_model.hasSelection = Mock(return_value=False)

        self.data_copier.copy_cells(mock_table)

        mock_table.selectionModel.assert_called_once_with()
        mock_table.mock_selection_model.hasSelection.assert_called_once_with()
        mock_show_mouse_toast.assert_called_once_with(
            UserNotifier.NO_SELECTION_MESSAGE)

        self.assertNotCalled(mock_copy)

    @patch(show_mouse_toast_package)
    @patch(copy_to_clipboard_package)
    def test_copy_cells(self, mock_copy, mock_show_mouse_toast):
        mock_table = MockQTableView()

        # two columns are selected at different positions
        mock_model = mock_table.model()
        mock_index = MockQModelIndex(None, None)
        mock_table.mock_selection_model.currentIndex = Mock(
            return_value=mock_index)

        self.data_copier.copy_cells(mock_table)

        mock_table.selectionModel.assert_called_once_with()
        self.assertEqual(1, mock_copy.call_count)
        self.assertEqual(9, mock_model.createIndex.call_count)
        mock_show_mouse_toast.assert_called_once_with(
            UserNotifier.COPY_SUCCESSFUL_MESSAGE)

    @patch('qtpy.QtWidgets.QMessageBox.question', return_value=QMessageBox.Yes)
    def test_ask_confirmation(self, mock_question):
        message = "Hello"
        title = "Title"
        reply = self.data_copier.ask_confirmation(message, title)
        mock_question.assert_called_once_with(self.data_copier, title, message,
                                              QMessageBox.Yes, QMessageBox.No)
        self.assertEqual(reply, True)
class DataCopierTest(TestCase):
    show_mouse_toast_package = 'mantidqt.widgets.workspacedisplay.user_notifier.UserNotifier.show_mouse_toast'
    copy_to_clipboard_package = 'mantidqt.widgets.workspacedisplay.data_copier.DataCopier.copy_to_clipboard'

    def assertNotCalled(self, mock):
        self.assertEqual(0, mock.call_count)

    def setUp(self):
        self.mock_status_bar = MockQStatusBar()
        self.mock_clipboard = MockQClipboard()
        self.mock_clipboard.setText = Mock()
        self.data_copier = DataCopier(self.mock_status_bar)

        mock_selection_model = MockQSelectionModel(has_selection=True)
        mock_selection_model.selectedRows = Mock(
            return_value=[MockQModelIndex(1, 1), MockQModelIndex(2, 2), MockQModelIndex(3, 3)])
        mock_selection_model.selectedColumns = Mock(
            return_value=[MockQModelIndex(1, 1), MockQModelIndex(2, 2), MockQModelIndex(3, 3)])
        self.table = Mock(spec=TableWorkspaceDisplayView)
        self.table.mock_selection_model = mock_selection_model

    @patch(show_mouse_toast_package)
    @patch(copy_to_clipboard_package)
    def test_action_copy_spectrum_values(self, mock_copy, mock_show_mouse_toast):
        mock_table = MockQTableView()

        # two rows are selected in different positions
        mock_indexes = [MockQModelIndex(0, 1), MockQModelIndex(3, 1)]
        mock_table.mock_selection_model.selectedRows = Mock(return_value=mock_indexes)

        mock_read = Mock(return_value=[43, 99])
        expected_string = "43\t99\n43\t99"

        self.data_copier.copy_spectrum_values(mock_table, mock_read)

        mock_table.selectionModel.assert_called_once_with()
        mock_table.mock_selection_model.hasSelection.assert_called_once_with()
        mock_copy.assert_called_once_with(expected_string)
        mock_show_mouse_toast.assert_called_once_with(UserNotifier.COPY_SUCCESSFUL_MESSAGE)

    @patch(show_mouse_toast_package)
    @patch(copy_to_clipboard_package)
    def test_action_copy_spectrum_values_no_selection(self, mock_copy,
                                                      mock_show_mouse_toast):
        mock_table = MockQTableView()
        mock_table.mock_selection_model.hasSelection = Mock(return_value=False)
        mock_table.mock_selection_model.selectedRows = Mock()

        self.data_copier.copy_spectrum_values(mock_table, ws_read=None)

        mock_table.selectionModel.assert_called_once_with()
        mock_table.mock_selection_model.hasSelection.assert_called_once_with()
        # the action should never look for rows if there is no selection
        self.assertNotCalled(mock_table.mock_selection_model.selectedRows)
        self.assertNotCalled(mock_copy)
        mock_show_mouse_toast.assert_called_once_with(UserNotifier.NO_SELECTION_MESSAGE)

    @patch(show_mouse_toast_package)
    @patch(copy_to_clipboard_package)
    def test_action_copy_bin_values(self, mock_copy, mock_show_mouse_toast):
        mock_table = MockQTableView()

        # two columns are selected at different positions
        mock_indexes = [MockQModelIndex(0, 0), MockQModelIndex(0, 3)]
        mock_table.mock_selection_model.selectedColumns = Mock(return_value=mock_indexes)
        # change the mock ws to have 3 histograms
        num_hist = 3

        mock_read = Mock(return_value=[83, 11, 33, 70])
        expected_string = "83\t70\n83\t70\n83\t70"

        self.data_copier.copy_bin_values(mock_table, mock_read, num_hist)

        mock_table.selectionModel.assert_called_once_with()
        mock_table.mock_selection_model.hasSelection.assert_called_once_with()
        mock_copy.assert_called_once_with(expected_string)
        mock_show_mouse_toast.assert_called_once_with(UserNotifier.COPY_SUCCESSFUL_MESSAGE)

    @patch(show_mouse_toast_package)
    @patch(copy_to_clipboard_package)
    def test_action_copy_bin_values_no_selection(self, mock_copy, mock_show_mouse_toast):
        mock_table = MockQTableView()
        mock_table.mock_selection_model.hasSelection = Mock(return_value=False)
        mock_table.mock_selection_model.selectedColumns = Mock()

        self.data_copier.copy_bin_values(mock_table, None, None)

        mock_table.selectionModel.assert_called_once_with()
        mock_table.mock_selection_model.hasSelection.assert_called_once_with()
        # the action should never look for rows if there is no selection
        self.assertNotCalled(mock_table.mock_selection_model.selectedColumns)
        self.assertNotCalled(mock_copy)
        mock_show_mouse_toast.assert_called_once_with(UserNotifier.NO_SELECTION_MESSAGE)

    @patch(show_mouse_toast_package)
    @patch(copy_to_clipboard_package)
    def test_copy_cells_no_selection(self, mock_copy, mock_show_mouse_toast):
        mock_table = MockQTableView()
        mock_table.mock_selection_model.hasSelection = Mock(return_value=False)

        self.data_copier.copy_cells(mock_table)

        mock_table.selectionModel.assert_called_once_with()
        mock_table.mock_selection_model.hasSelection.assert_called_once_with()
        mock_show_mouse_toast.assert_called_once_with(UserNotifier.NO_SELECTION_MESSAGE)

        self.assertNotCalled(mock_copy)

    @patch(show_mouse_toast_package)
    @patch(copy_to_clipboard_package)
    def test_copy_cells(self, mock_copy, mock_show_mouse_toast):
        mock_table = MockQTableView()

        # two columns are selected at different positions
        mock_index = MockQModelIndex(None, None)
        mock_table.mock_selection_model.currentIndex = Mock(return_value=mock_index)

        self.data_copier.copy_cells(mock_table)

        mock_table.selectionModel.assert_called_once_with()
        self.assertEqual(1, mock_copy.call_count)
        self.assertEqual(9, mock_index.sibling.call_count)
        mock_show_mouse_toast.assert_called_once_with(UserNotifier.COPY_SUCCESSFUL_MESSAGE)

    @patch('qtpy.QtWidgets.QMessageBox.question', return_value=QMessageBox.Yes)
    def test_ask_confirmation(self, mock_question):
        message = "Hello"
        title = "Title"
        reply = self.data_copier.ask_confirmation(message, title)
        mock_question.assert_called_once_with(self.data_copier, title, message, QMessageBox.Yes, QMessageBox.No)
        self.assertEqual(reply, True)