Skip to content

Commit

Permalink
Merge pull request enthought#125 from MatthieuDartiailh/master
Browse files Browse the repository at this point in the history
Patch for list_editor with the qt4 backend
  • Loading branch information
corranwebster committed Oct 22, 2013
2 parents 57d1e8f + 2921e7d commit 974d60a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
2 changes: 1 addition & 1 deletion traitsui/qt4/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def __init__(self, icon, slot):
self.setFlat(True)
self.setFocusPolicy(QtCore.Qt.NoFocus)

QtCore.QObject.connect(self, QtCore.SIGNAL('clicked()'), slot)
self.clicked.connect(slot)

#-------------------------------------------------------------------------------
# Dock-related stubs.
Expand Down
43 changes: 31 additions & 12 deletions traitsui/qt4/list_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD license.
# However, when used with the GPL version of PyQt the additional terms described in the PyQt GPL exception also apply
# However, when used with the GPL version of PyQt the additional terms described
# in the PyQt GPL exception also apply

#
# Author: Riverbank Computing Limited
Expand All @@ -20,7 +21,7 @@

from pyface.api import ImageResource

from traits.api import Str, Any, Bool, Dict
from traits.api import Str, Any, Bool, Dict, Instance
from traits.trait_base import user_name_for, enumerate, xgetattr

# FIXME: ToolkitEditorFactory is a proxy class defined here just for backward
Expand Down Expand Up @@ -53,6 +54,10 @@ class SimpleEditor ( Editor ):
# Is the list of items being edited mutable?
mutable = Bool( True )

# Signal mapper allowing to identify which icon button requested a context
# menu
mapper = Instance(QtCore.QSignalMapper)

#---------------------------------------------------------------------------
# Class constants:
#---------------------------------------------------------------------------
Expand Down Expand Up @@ -105,6 +110,9 @@ def init ( self, parent ):
self.control.setFrameShape(QtGui.QFrame.NoFrame)
self.control.setWidgetResizable(True)

#Create a mapper to identify which icon button requested a contextmenu
self.mapper = QtCore.QSignalMapper(self.control)

# Create a widget with a grid layout as the container.
self._list_pane = QtGui.QWidget()
self._list_pane.setSizePolicy(QtGui.QSizePolicy.Expanding,
Expand Down Expand Up @@ -152,6 +160,7 @@ def update_editor ( self ):
""" Updates the editor when the object trait changes externally to the
editor.
"""
self.mapper = QtCore.QSignalMapper(self.control)
# Disconnect the editor from any control about to be destroyed:
self._dispose_items()

Expand All @@ -166,7 +175,10 @@ def update_editor ( self ):

is_fake = (resizable and (len( self.value ) == 0))
if is_fake:
self.empty_list()
self.empty_list()
else:
# Asking the mapper to send the sender to the callback method
self.mapper.mapped[QtCore.QObject].connect(self.popup_menu)

editor = self._editor
for index, value in enumerate(self.value):
Expand All @@ -177,7 +189,12 @@ def update_editor ( self ):
column = column * 2

if resizable:
control = IconButton('list_editor.png', self.popup_menu)
# Connecting the new button to the mapper
control = IconButton('list_editor.png', self.mapper.map)
# Setting the mapping and asking it to send the index of the
# sender to the callback method
self.mapper.setMapping(control, control)

layout.addWidget(control, row, column)

proxy = ListItemProxy( self.object, self.name, index, item_trait,
Expand Down Expand Up @@ -232,7 +249,12 @@ def update_editor_item ( self, event ):
def empty_list ( self ):
""" Creates an empty list entry (so the user can add a new item).
"""
control = IconButton('list_editor.png', self.popup_empty_menu)
# Connecting the new button to the mapper
control = IconButton('list_editor.png', self.mapper.map)
# Setting the mapping and asking it to send the sender to the
# callback method
self.mapper.setMapping(control, control)
self.mapper.mapped[QtCore.QObject].connect(self.popup_empty_menu)
control.is_empty = True
self._cur_control = control

Expand All @@ -258,23 +280,20 @@ def get_info ( self ):
# Displays the empty list editor popup menu:
#---------------------------------------------------------------------------

def popup_empty_menu ( self ):
def popup_empty_menu ( self , sender):
""" Displays the empty list editor popup menu.
"""
self._cur_control = control = self.control.sender()
self._cur_control = control = sender
menu = MakeMenu( self.empty_list_menu, self, True, control ).menu
menu.exec_(control.mapToGlobal(QtCore.QPoint(0, 0)))

#---------------------------------------------------------------------------
# Displays the list editor popup menu:
#---------------------------------------------------------------------------

def popup_menu ( self ):
def popup_menu ( self , sender):
""" Displays the list editor popup menu.
"""
layout = self._list_pane.layout()
sender = layout.sender()

self._cur_control = sender

proxy = sender.proxy
Expand Down Expand Up @@ -569,7 +588,7 @@ def update_editor ( self ):
# Remember the page for later deletion processing:
self._uis.append([ui.control, ui, view_object, monitoring])

if self.selected:
if self.selected:
self._selected_changed(self.selected)

#---------------------------------------------------------------------------
Expand Down
16 changes: 8 additions & 8 deletions traitsui/qt4/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,16 @@ def parse ( self, menu, indent ):
if name:
self.names[name] = act
setattr(self.owner, name, MakeMenuItem(self, act))
else:
# Else must be the start of a sub menu:
submenu = QtGui.QMenu(line.strip())

# Else must be the start of a sub menu:
submenu = QtGui.QMenu(line.strip())
# Recursively parse the sub-menu:
self.parse(submenu, indented)

# Recursively parse the sub-menu:
self.parse(submenu, indented)

# Add the menu to its parent:
act = menu.addMenu(submenu)
act.setStatusTip(help)
# Add the menu to its parent:
act = menu.addMenu(submenu)
act.setStatusTip(help)

#---------------------------------------------------------------------------
# Returns the body of an inline method:
Expand Down

0 comments on commit 974d60a

Please sign in to comment.