Permalink
Browse files

FIX: initial size of component editor is 0,0

A traitsui item with editor=ComponentEditor is
displayed in Qt with initial size equals to 0, 0.
For example, a dialog containing a Chaco plot will
not even display the frame of the window, unless
the width and height of the view is set.

The commit contains test and fix.
Fixes Issues 45 and 46 in Chaco.
  • Loading branch information...
1 parent c268611 commit 16362c71db118603e2060573ed3e9ad2f0fd643d @pberkes pberkes committed Feb 14, 2012
Showing with 102 additions and 0 deletions.
  1. +4 −0 enable/component_editor.py
  2. +44 −0 enable/qt4/base_window.py
  3. +54 −0 enable/tests/test_component_editor.py
@@ -21,6 +21,7 @@
else:
Editor = object
+
class _ComponentEditor( Editor ):
#---------------------------------------------------------------------------
@@ -55,6 +56,9 @@ def update_editor( self ):
return
+# FIXME: remove magic numbers, size = (400, 400) -> initialize to the size of
+# the corresponding item if available
+
class ComponentEditor( BasicEditorFactory ):
""" wxPython editor factory for Enable components.
"""
View
@@ -112,6 +112,40 @@ def wheelEvent(self, event):
if self._enable_window:
self._enable_window._handle_mouse_event("mouse_wheel", event)
+ def sizeHint(self, qt_size_hint):
+ """ Combine the Qt and enable size hints.
+
+ Combine the size hint coming from the Qt component (usually -1, -1)
+ with the preferred size of the enable component and the size
+ of the enable window.
+
+ The combined size hint is
+ - the Qt size hint if larger than 0
+ - the maximum of the plot's preferred size and the window size
+ (component-wise)
+
+ E.g., if
+ qt size hint = (-1, -1)
+ component preferred size = (500, 200)
+ size of enable window = (400, 400)
+
+ the final size hint will be (500, 400)
+ """
+
+ preferred_size = self._enable_window.component.get_preferred_size()
+ window_size = self._enable_window.control.size().toTuple()
+
+ if qt_size_hint.width() < 0:
+ width = max(preferred_size[0], window_size[0])
+ qt_size_hint.setWidth(width)
+
+ if qt_size_hint.height() < 0:
+ height = max(preferred_size[1], window_size[1])
+ qt_size_hint.setHeight(height)
+
+ return qt_size_hint
+
+
#------------------------------------------------------------------------
# Qt Drag and drop event handlers
#------------------------------------------------------------------------
@@ -131,6 +165,7 @@ def dropEvent(self, event):
class _QtWindow(QtGui.QWidget):
""" The Qt widget that implements the enable control. """
+
def __init__(self, parent, enable_window):
super(_QtWindow, self).__init__(parent)
self.handler = _QtWindowHandler(self, enable_window)
@@ -184,6 +219,10 @@ def dragMoveEvent(self, event):
def dropEvent(self, event):
self.handler.dropEvent(event)
+ def sizeHint(self):
+ qt_size_hint = super(_QtWindow, self).sizeHint()
+ return self.handler.sizeHint(qt_size_hint)
+
class _QtGLWindow(QtOpenGL.QGLWidget):
def __init__(self, parent, enable_window):
@@ -241,6 +280,11 @@ def dragMoveEvent(self, event):
def dropEvent(self, event):
self.handler.dropEvent(event)
+ # TODO: by symmetry this belongs here, but we need to test it
+ #def sizeHint(self):
+ # qt_size_hint = super(_QtGLWindow, self).sizeHint()
+ # return self.handler.sizeHint(qt_size_hint)
+
class _Window(AbstractWindow):
@@ -0,0 +1,54 @@
+""" Test the interaction between traitsui and enable's ComponentEditor.
+"""
+
+
+from enable.component_editor import ComponentEditor
+from traits.has_traits import HasTraits
+from traits.trait_types import Any
+from traitsui.item import Item
+from traitsui.view import View
+
+from traitsui.tests._tools import *
+
+
+class _ComponentDialog(HasTraits):
+ """ View containing an item with ComponentEditor. """
+ thing = Any
+
+ traits_view = View(
+ Item('thing', editor=ComponentEditor(), show_label=False),
+ resizable = True
+ )
+
+
+DIALOG_WIDTH, DIALOG_HEIGHT = 700, 200
+class _ComponentDialogWithSize(HasTraits):
+ """ View containing an item with ComponentEditor and given size. """
+ thing = Any
+
+ traits_view = View(
+ Item('thing', editor=ComponentEditor(), show_label=False,
+ width=DIALOG_WIDTH, height=DIALOG_HEIGHT),
+ resizable = True
+ )
+
+
+@skip_if_null
+def test_initial_component():
+ # BUG: the initial size of an Item with ComponentEditor is zero
+ # in the Qt backend
+
+ dialog = _ComponentDialog()
+ ui = dialog.edit_traits()
+
+ size = get_dialog_size(ui.control)
+ nose.tools.assert_greater(size[0], 0)
+ nose.tools.assert_greater(size[1], 0)
+
+
+
+if __name__ == '__main__':
+ # Executing the file opens the dialog for manual testing
+ vw = _ComponentDialogWithSize()
+ vw.configure_traits()
+

0 comments on commit 16362c7

Please sign in to comment.