Skip to content

Commit

Permalink
Fix updating right_clicked and right_clicked_item for Qt ListStrEditor (
Browse files Browse the repository at this point in the history
#1115)

* Fix right clicking item for Qt ListStrEditor not doing anything, as well as edge case for when the index is out-of-bound

* Revert an orthogonal fix.
  • Loading branch information
kitchoi authored Oct 9, 2020
1 parent b855634 commit 7841ca7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
11 changes: 9 additions & 2 deletions traitsui/qt4/list_str_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ def _on_activate(self, mi):
self.activated_index = index = mi.row()
self.activated = self.adapter.get_item(self.object, self.name, index)

def _on_context_menu(self, point):
""" Handle a context menu request.
def _on_mouse_right_click(self, point):
""" Handle a mouse right click event
"""
mi = self.list_view.indexAt(point)
if mi.isValid():
Expand Down Expand Up @@ -528,6 +528,13 @@ def _dispose(self):
"""
self.setModel(None)

def mouseReleaseEvent(self, event):
""" Reimplemented to support listening to right clicked item."""
if event.button() == QtCore.Qt.RightButton:
event.accept()
self._editor._on_mouse_right_click(event.pos())
super(_ListView, self).mouseReleaseEvent(event)

def keyPressEvent(self, event):
""" Reimplemented to support edit, insert, and delete by keyboard.
"""
Expand Down
69 changes: 64 additions & 5 deletions traitsui/tests/editors/test_liststr_editor_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,61 @@ def clear_selection(editor):
raise unittest.SkipTest("Test not implemented for this toolkit")


def right_click_item(control, index):
def right_click_item(editor, index):
""" Right clicks on the specified item.
"""

if is_wx():
import wx

control = editor.control
event = wx.ListEvent(
wx.EVT_LIST_ITEM_RIGHT_CLICK.typeId, control.GetId()
)
event.SetIndex(index)
wx.PostEvent(control, event)

elif is_qt():
# Couldn't figure out how to close the context menu programatically
from pyface.qt import QtCore
from pyface.qt.QtTest import QTest
view = editor.list_view
q_model_index = view.model().index(index, 0)
view.scrollTo(q_model_index)
rect = view.visualRect(q_model_index)
QTest.mouseClick(
view.viewport(),
QtCore.Qt.RightButton,
QtCore.Qt.NoModifier,
rect.center(),
)
else:
raise unittest.SkipTest("Test not implemented for this toolkit")


def right_click_center(editor):
""" Right click the middle of the widget.
"""

if is_wx():
import wx

control = editor.control
event = wx.ListEvent(
wx.EVT_LIST_ITEM_RIGHT_CLICK.typeId, control.GetId()
)
wx.PostEvent(control, event)

elif is_qt():
from pyface.qt import QtCore
from pyface.qt.QtTest import QTest
view = editor.list_view
rect = view.rect()
QTest.mouseClick(
view.viewport(),
QtCore.Qt.RightButton,
QtCore.Qt.NoModifier,
rect.center(),
)
else:
raise unittest.SkipTest("Test not implemented for this toolkit")

Expand Down Expand Up @@ -608,8 +646,6 @@ def test_list_str_editor_title(self):
self.setup_gui(ListStrModel(), get_view(title="testing")):
pass

# see `right_click_item` and issue enthought/traitsui#868
@requires_toolkit([ToolkitName.wx])
def test_list_str_editor_right_click(self):
class ListStrModelRightClick(HasTraits):
value = List(["one", "two", "three"])
Expand All @@ -628,12 +664,35 @@ class ListStrModelRightClick(HasTraits):
self.assertEqual(model.right_clicked, "")
self.assertEqual(model.right_clicked_index, 0)

right_click_item(editor.control, 1)
right_click_item(editor, 1)
process_cascade_events()

self.assertEqual(model.right_clicked, "two")
self.assertEqual(model.right_clicked_index, 1)

def test_list_str_editor_right_click_out_of_bound(self):
class ListStrModelRightClick(HasTraits):
value = List([])
right_clicked = Str()
right_clicked_index = Int()

model = ListStrModelRightClick()
view = get_view(
right_clicked="object.right_clicked",
right_clicked_index="object.right_clicked_index",
)
with reraise_exceptions(), \
self.setup_gui(model, view) as editor:

self.assertEqual(model.right_clicked, "")
self.assertEqual(model.right_clicked_index, 0)

right_click_center(editor)
process_cascade_events()

self.assertEqual(model.right_clicked, "")
self.assertEqual(model.right_clicked_index, 0)


class TestListStrEditorSelection(BaseTestMixin, unittest.TestCase):

Expand Down

0 comments on commit 7841ca7

Please sign in to comment.