def test_active_tabwidget_after_editor_containing_tabs_gets_focus(self):
        # Regression test: if an editor contains tabs, a change in focus
        # sets the editor area pane `active_tabwidget` to one of those tabs,
        # rather than the editor's tab, after certain operations (e.g.,
        # navigating the editor tabs using keyboard shortcuts).

        window = TaskWindow()

        task = SplitEditorAreaPaneTestTask()
        editor_area = task.editor_area
        window.add_task(task)

        # Show the window.
        with event_loop():
            window.open()

        with event_loop():
            app = get_app_qt4()
            app.setActiveWindow(window.control)

        # Add and activate an editor which contains tabs.
        editor = ViewWithTabsEditor()
        with event_loop():
            editor_area.add_editor(editor)
        with event_loop():
            editor_area.activate_editor(editor)

        # Check that the active tabwidget is the right one.
        self.assertIs(editor_area.active_tabwidget,
                      editor_area.control.tabwidget())

        with event_loop():
            window.close()
    def test_active_tabwidget_after_editor_containing_tabs_gets_focus(self):
        # Regression test: if an editor contains tabs, a change in focus
        # sets the editor area pane `active_tabwidget` to one of those tabs,
        # rather than the editor's tab, after certain operations (e.g.,
        # navigating the editor tabs using keyboard shortcuts).

        window = TaskWindow()

        task = SplitEditorAreaPaneTestTask()
        editor_area = task.editor_area
        window.add_task(task)

        # Show the window.
        with event_loop():
            window.open()

        with event_loop():
            app = get_app_qt4()
            app.setActiveWindow(window.control)

        # Add and activate an editor which contains tabs.
        editor = ViewWithTabsEditor()
        with event_loop():
            editor_area.add_editor(editor)
        with event_loop():
            editor_area.activate_editor(editor)

        # Check that the active tabwidget is the right one.
        self.assertIs(editor_area.active_tabwidget,
                      editor_area.control.tabwidget())

        with event_loop():
            window.close()
    def test_no_context_menu_if_outside_tabwidgets(self):
        # Check that the case of a position not in any of the tab widgets
        # is handled correctly.
        window = TaskWindow()

        task = SplitEditorAreaPaneTestTask()
        window.add_task(task)

        with event_loop():
            window.open()

        editor_area = task.editor_area
        editor_area_widget = editor_area.control
        tab_widget, = editor_area_widget.tabwidgets()

        # Position is relative to the receiving widget, so (-1, -1) should be
        # reliably outside.
        pos = QtCore.QPoint(-1, -1)
        context_menu_event = QtGui.QContextMenuEvent(
            QtGui.QContextMenuEvent.Mouse,
            pos,
        )

        global_pos = editor_area_widget.mapToGlobal(pos)
        self.assertIsNone(editor_area.get_context_menu(global_pos))

        # Exercise the context menu code to make sure it doesn't raise. (It
        # should do nothing.)
        with event_loop():
            tab_widget.contextMenuEvent(context_menu_event)

        with event_loop():
            window.close()
    def test_active_editor_after_focus_change(self):
        window = TaskWindow(size=(800, 600))

        task = SplitEditorAreaPaneTestTask()
        editor_area = task.editor_area
        window.add_task(task)

        # Show the window.
        with event_loop():
            window.open()

        with event_loop():
            app = get_app_qt4()
            app.setActiveWindow(window.control)

        # Add and activate an editor which contains tabs.
        left_editor = ViewWithTabsEditor()
        right_editor = ViewWithTabsEditor()

        with event_loop():
            editor_area.add_editor(left_editor)
        with event_loop():
            editor_area.control.split(orientation=QtCore.Qt.Horizontal)
        with event_loop():
            editor_area.add_editor(right_editor)

        editor_area.activate_editor(right_editor)
        self.assertEqual(editor_area.active_editor, right_editor)

        with event_loop():
            left_editor.control.setFocus()

        self.assertIsNotNone(editor_area.active_editor)
        self.assertEqual(editor_area.active_editor, left_editor)

        with event_loop():
            window.close()
    def test_active_editor_after_focus_change(self):
        window = TaskWindow(size=(800, 600))

        task = SplitEditorAreaPaneTestTask()
        editor_area = task.editor_area
        window.add_task(task)

        # Show the window.
        with event_loop():
            window.open()

        with event_loop():
            app = get_app_qt4()
            app.setActiveWindow(window.control)

        # Add and activate an editor which contains tabs.
        left_editor = ViewWithTabsEditor()
        right_editor = ViewWithTabsEditor()

        with event_loop():
            editor_area.add_editor(left_editor)
        with event_loop():
            editor_area.control.split(orientation=QtCore.Qt.Horizontal)
        with event_loop():
            editor_area.add_editor(right_editor)

        editor_area.activate_editor(right_editor)
        self.assertEqual(editor_area.active_editor, right_editor)

        with event_loop():
            left_editor.control.setFocus()

        self.assertIsNotNone(editor_area.active_editor)
        self.assertEqual(editor_area.active_editor, left_editor)

        with event_loop():
            window.close()
    def test_context_menu_merge_text_top_bottom_split(self):
        # Regression test for enthought/pyface#422
        window = TaskWindow()

        task = SplitEditorAreaPaneTestTask()
        editor_area = task.editor_area
        window.add_task(task)

        with event_loop():
            window.open()

        editor_area_widget = editor_area.control
        with event_loop():
            editor_area_widget.split(orientation=QtCore.Qt.Vertical)

        # Get the tabs.
        top_tab, bottom_tab = editor_area_widget.tabwidgets()

        # Check top context menu merge text.
        top_tab_center = top_tab.mapToGlobal(top_tab.rect().center())
        top_context_menu = editor_area.get_context_menu(top_tab_center)
        self.assertEqual(
            top_context_menu.find_item("merge").action.name,
            "Merge with bottom pane",
        )

        # And the bottom context menu merge text.
        bottom_tab_center = bottom_tab.mapToGlobal(bottom_tab.rect().center())
        bottom_context_menu = editor_area.get_context_menu(bottom_tab_center)
        self.assertEqual(
            bottom_context_menu.find_item("merge").action.name,
            "Merge with top pane",
        )

        with event_loop():
            window.close()
    def test_context_menu_merge_text_left_right_split(self):
        # Regression test for enthought/pyface#422
        window = TaskWindow()

        task = SplitEditorAreaPaneTestTask()
        editor_area = task.editor_area
        window.add_task(task)

        with event_loop():
            window.open()

        editor_area_widget = editor_area.control
        with event_loop():
            editor_area_widget.split(orientation=QtCore.Qt.Horizontal)

        # Get the tabs.
        left_tab, right_tab = editor_area_widget.tabwidgets()

        # Check left context menu merge text.
        left_tab_center = left_tab.mapToGlobal(left_tab.rect().center())
        left_context_menu = editor_area.get_context_menu(left_tab_center)
        self.assertEqual(
            left_context_menu.find_item("merge").action.name,
            "Merge with right pane",
        )

        # And the right context menu merge text.
        right_tab_center = right_tab.mapToGlobal(right_tab.rect().center())
        right_context_menu = editor_area.get_context_menu(right_tab_center)
        self.assertEqual(
            right_context_menu.find_item("merge").action.name,
            "Merge with left pane",
        )

        with event_loop():
            window.close()