Added row header support to Qt4 TabularEditor #50

Merged
merged 3 commits into from Jan 20, 2012
@@ -49,6 +49,9 @@ class TabularEditor ( BasicEditorFactory ):
# Should column headers (i.e. titles) be displayed?
show_titles = Bool( True )
+ # Should row headers be displated (Qt4 only)?
+ show_row_titles = Bool( False )
+
# The optional extended name of the trait used to indicate that a complete
# table update is needed:
update = Str
@@ -133,6 +136,10 @@ class TabularEditor ( BasicEditorFactory ):
# of data is large.
auto_resize = Bool( False )
+ # Should the rows automatically resize (Qt4 only)? Don't allow
+ # this when the amount of data is large.
+ auto_resize_rows = Bool( False )
+
# Whether to stretch the last column to fit the available space.
stretch_last_section = Bool( True )
@@ -594,18 +594,24 @@ def __init__(self, editor):
# Configure the row headings
vheader = self.verticalHeader()
- vheader.hide()
-
- # Set a default height for rows. Although setting the resize mode to
- # ResizeToContents would provide the best sizes, this is far too
- # expensive when the TabularEditor has a large amount of data. Instead,
- # we make a reasonable guess based on the minimum size hint and the font
- # of the first row.
- size = vheader.minimumSectionSize()
- font = editor.adapter.get_font(editor.object, editor.name, 0)
- if font is not None:
- size = max(size, QtGui.QFontMetrics(QtGui.QFont(font)).height())
- vheader.setDefaultSectionSize(size)
+ if factory.show_row_titles:
+ vheader.setHighlightSections(False)
+ else:
+ vheader.hide()
+
+ if factory.show_row_titles and factory.auto_resize_rows:
+ vheader.setResizeMode(QtGui.QHeaderView.ResizeToContents)
+ else:
+ # Set a default height for rows. Although setting the resize mode to
+ # ResizeToContents would provide the best sizes, this is far too
+ # expensive when the TabularEditor has a large amount of data. Instead,
+ # we make a reasonable guess based on the minimum size hint and the font
+ # of the first row.
+ size = vheader.minimumSectionSize()
+ font = editor.adapter.get_font(editor.object, editor.name, 0)
+ if font is not None:
+ size = max(size, QtGui.QFontMetrics(QtGui.QFont(font)).height())
+ vheader.setDefaultSectionSize(size)
# Configure the column headings.
hheader = self.horizontalHeader()
@@ -156,11 +156,17 @@ def flags(self, mi):
def headerData(self, section, orientation, role):
""" Reimplemented to return the header data.
"""
- if orientation != QtCore.Qt.Horizontal or role != QtCore.Qt.DisplayRole:
+ if role != QtCore.Qt.DisplayRole:
return None
editor = self._editor
- label = editor.adapter.get_label(section, editor.object)
+
+ label = None
+ if orientation == QtCore.Qt.Vertical:
+ label = editor.adapter.get_row_label(section, editor.object)
+ elif orientation == QtCore.Qt.Horizontal:
+ label = editor.adapter.get_label(section, editor.object)
+
return label
def rowCount(self, mi):
@@ -24,9 +24,9 @@
from __future__ import absolute_import
-from traits.api import (Any, Bool, Color, Enum, Event, Float, Font, HasPrivateTraits,
- HasTraits, Instance, Int, Interface, List, Property, Str, cached_property,
- implements, on_trait_change)
+from traits.api import (Any, Bool, Color, Either, Enum, Event, Float, Font,
+ HasPrivateTraits, HasTraits, Instance, Int, Interface, List, Property,
+ Str, cached_property, implements, on_trait_change)
#-------------------------------------------------------------------------------
# 'ITabularAdapter' interface:
@@ -218,6 +218,10 @@ class TabularAdapter ( HasPrivateTraits ):
# trait):
label_map = Property( depends_on = 'columns' )
+ # The name of the trait on a row item containing the value to use
+ # as a row label. If None, the label will be the empty string.
+ row_label_name = Either(None, Str)
+
# For each adapter, specifies the column indices the adapter handles:
adapter_column_indices = Property( depends_on = 'adapters,columns' )
@@ -452,6 +456,15 @@ def get_label(self, section, obj=None):
"""Override this method if labels will vary from object to object."""
return self.label_map[section]
+ def get_row_label(self, section, obj=None):
+ if self.row_label_name is None:
+ return None
+ rows = getattr(obj, self.name, None)
+ if rows is None:
+ return None
+ item = rows[section]
+ return getattr(item, self.row_label_name, None)
+
@cached_property
def _get_label_map (self):
map = []