From aae7c1d8d47490182c343c71685ed77dfeb3db90 Mon Sep 17 00:00:00 2001 From: Prabhu Ramachandran Date: Tue, 24 Jul 2018 20:44:19 -0400 Subject: [PATCH 1/2] Use tooltip metadata optionally instead of desc. This implements the suggestion in #472. If a `tooltip` metadata attribute exists for a trait, it is used as is for the tooltip, otherwise the original behavior of using the `'Specifies' + item.desc` is used. --- traitsui/qt4/editor.py | 8 +++++--- traitsui/qt4/ui_panel.py | 15 ++++++++------- traitsui/wx/editor.py | 8 +++++--- traitsui/wx/ui_panel.py | 7 +++++-- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/traitsui/qt4/editor.py b/traitsui/qt4/editor.py index 0857c4c21..333fed29c 100644 --- a/traitsui/qt4/editor.py +++ b/traitsui/qt4/editor.py @@ -106,11 +106,13 @@ def set_tooltip(self, control=None): """ desc = self.description if desc == '': - desc = self.object.base_trait(self.name).desc + desc = self.object.base_trait(self.name).tooltip if desc is None: - return False + desc = self.object.base_trait(self.name).desc + if desc is None: + return False - desc = 'Specifies ' + desc + desc = 'Specifies ' + desc if control is None: control = self.control diff --git a/traitsui/qt4/ui_panel.py b/traitsui/qt4/ui_panel.py index 7be91330a..39444db4c 100644 --- a/traitsui/qt4/ui_panel.py +++ b/traitsui/qt4/ui_panel.py @@ -832,7 +832,9 @@ def _add_items(self, content, outer=None): # Otherwise, it must be a trait Item: object = eval(item.object_, globals(), ui.context) trait = object.base_trait(name) - desc = trait.desc or '' + desc = trait.tooltip + if desc is None: + desc = 'Specifies ' + trait.desc if trait.desc else '' # Get the editor factory associated with the Item: editor_factory = item.editor @@ -1098,9 +1100,9 @@ def _create_label(self, item, ui, desc, suffix=':'): we append a suffix (by default a colon ':') at the end of the label text. - We also set the help on the QLabel control (from item.help) and - the tooltip (it item.desc exists; we add "Specifies " at the start - of the item.desc string). + We also set the help on the QLabel control (from item.help) and the + tooltip (it item.desc exists; we add "Specifies " at the start of the + item.desc string, if item.tooltip exists we just use it as is). Parameters ---------- @@ -1117,6 +1119,7 @@ def _create_label(self, item, ui, desc, suffix=':'): ------- label_control : QLabel The control for the label + """ label = item.get_label(ui) @@ -1139,10 +1142,8 @@ def _create_label(self, item, ui, desc, suffix=':'): #wx.EVT_LEFT_UP( control, show_help_popup ) label_control.help = item.get_help(ui) - # FIXME: do people rely on traitsui adding 'Specifies ' to the start - # of every tooltip? It's not flexible at all if desc != '': - label_control.setToolTip('Specifies ' + desc) + label_control.setToolTip(desc) return label_control diff --git a/traitsui/wx/editor.py b/traitsui/wx/editor.py index 9d66da97a..641b675bc 100644 --- a/traitsui/wx/editor.py +++ b/traitsui/wx/editor.py @@ -115,11 +115,13 @@ def set_tooltip(self, control=None): """ desc = self.description if desc == '': - desc = self.object.base_trait(self.name).desc + desc = self.object.base_trait(self.name).tooltip if desc is None: - return False + desc = self.object.base_trait(self.name).desc + if desc is None: + return False - desc = 'Specifies ' + desc + desc = 'Specifies ' + desc if control is None: control = self.control diff --git a/traitsui/wx/ui_panel.py b/traitsui/wx/ui_panel.py index fb9f2a6c9..f0387a6b3 100644 --- a/traitsui/wx/ui_panel.py +++ b/traitsui/wx/ui_panel.py @@ -885,7 +885,10 @@ def add_items(self, content, panel, sizer): # Otherwise, it must be a trait Item: object = eval(item.object_, globals(), ui.context) trait = object.base_trait(name) - desc = trait.desc or '' + desc = trait.tooltip + if desc is None: + desc = 'Specifies ' + trait.desc if trait.desc else '' + label = None # If we are displaying labels on the left, add the label to the @@ -1091,7 +1094,7 @@ def create_label(self, item, ui, desc, parent, sizer, suffix=':', pad_side, self.label_pad) if desc != '': - control.SetToolTipString('Specifies ' + desc) + control.SetToolTipString(desc) return control From 0f335dfce63642703879396a7d246888b2a1b773 Mon Sep 17 00:00:00 2001 From: Prabhu Ramachandran Date: Tue, 24 Jul 2018 21:32:44 -0400 Subject: [PATCH 2/2] Update docstring as per review. --- traitsui/qt4/ui_panel.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/traitsui/qt4/ui_panel.py b/traitsui/qt4/ui_panel.py index 39444db4c..d1babc38d 100644 --- a/traitsui/qt4/ui_panel.py +++ b/traitsui/qt4/ui_panel.py @@ -1101,8 +1101,9 @@ def _create_label(self, item, ui, desc, suffix=':'): label text. We also set the help on the QLabel control (from item.help) and the - tooltip (it item.desc exists; we add "Specifies " at the start of the - item.desc string, if item.tooltip exists we just use it as is). + tooltip (if the ``tooltip`` metadata on the edited trait exists, then + it will be used as-is; otherwise, if the ``desc`` metadata exists, the + string "Specifies " will be prepended to the start of ``desc``). Parameters ----------