From 17b9a2bfd8cdf191e7f16cb12539e8bb7b740bfa Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 21 Oct 2018 13:38:18 -0700 Subject: [PATCH] auto-wrap group descriptions + style fixes --- gooey/gui/components/config.py | 15 ++++++++++----- .../gui/components/util/wrapped_static_text.py | 17 +++++++++++++++-- gooey/gui/components/widgets/bases.py | 2 ++ gooey/python_bindings/argparse_to_json.py | 14 +++++++++++++- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/gooey/gui/components/config.py b/gooey/gui/components/config.py index ee74c80..d5c5f96 100644 --- a/gooey/gui/components/config.py +++ b/gooey/gui/components/config.py @@ -1,9 +1,9 @@ import wx from wx.lib.scrolledpanel import ScrolledPanel +from gooey.gui.components.util.wrapped_static_text import AutoWrappedStaticText from gooey.gui.util import wx_util -from gooey.util.functional import getin, flatmap, merge, compact, indexunique -from gooey.gui.components.widgets.radio_group import RadioGroup +from gooey.util.functional import getin, flatmap, compact, indexunique class ConfigPage(ScrolledPanel): @@ -13,6 +13,7 @@ class ConfigPage(ScrolledPanel): self.rawWidgets = rawWidgets self.reifiedWidgets = [] self.layoutComponent() + self.Layout() self.widgetsMap = indexunique(lambda x: x._id, self.reifiedWidgets) ## TODO: need to rethink what uniquely identifies an argument. ## Out-of-band IDs, while simple, make talking to the client program difficult @@ -100,12 +101,16 @@ class ConfigPage(ScrolledPanel): boxSizer = wx.BoxSizer(wx.VERTICAL) boxSizer.AddSpacer(10) if group['name']: - boxSizer.Add(wx_util.h1(parent, group['name'] or ''), 0, wx.TOP | wx.BOTTOM | wx.LEFT, 8) + groupName = wx_util.h1(parent, group['name'] or '') + groupName.SetForegroundColour(getin(group, ['options', 'label_color'])) + boxSizer.Add(groupName, 0, wx.TOP | wx.BOTTOM | wx.LEFT, 8) group_description = getin(group, ['description']) if group_description: - description = wx.StaticText(parent, label=group_description) - boxSizer.Add(description, 0, wx.EXPAND | wx.LEFT, 10) + description = AutoWrappedStaticText(parent, label=group_description, target=boxSizer) + description.SetForegroundColour(getin(group, ['options', 'description_color'])) + description.SetMinSize((0, -1)) + boxSizer.Add(description, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, 10) # apply an underline when a grouping border is not specified # unless the user specifically requests not to show it diff --git a/gooey/gui/components/util/wrapped_static_text.py b/gooey/gui/components/util/wrapped_static_text.py index 4f8770a..350e656 100644 --- a/gooey/gui/components/util/wrapped_static_text.py +++ b/gooey/gui/components/util/wrapped_static_text.py @@ -5,21 +5,31 @@ from wx.lib.wordwrap import wordwrap class AutoWrappedStaticText(wx.StaticText): """ - Copy/pasta of wx.lib.agw.infobar.AutoWrapStaticText with two modifications: + Copy/pasta of wx.lib.agw.infobar.AutoWrapStaticText with 3 modifications: 1. Extends wx.StaticText rather than GenStaticText 2. Does not set the fore/background colors to sys defaults + 3. takes an optional `target` parameter for sizing info The behavior of GenStaticText's background color is pretty buggy cross- platform. It doesn't reliably match its parent components background colors[0] (for instance when rendered inside of a Notebook) which leads to ugly 'boxing' around the text components. + There is either a bug in WX, or or human error on my end, which causes + EVT_SIZE events to continuously spawn from this (and AutoWrapStaticText) but + with ever decreasing widths (in response to the SetLabel action in the + wrap handler). The end result is a single skinny column of letters. + + The work around is to respond the EVT_SIZE event, but follow the size of the + `target` component rather than relying on the size of the event. + [0] more specifically, they'll match 1:1 on paper, but still ultimately render differently. """ def __init__(self, parent, *args, **kwargs): + self.target = kwargs.pop('target', None) super(AutoWrappedStaticText, self).__init__(parent, *args, **kwargs) self.label = kwargs.get('label') self.Bind(wx.EVT_SIZE, self.OnSize) @@ -33,7 +43,10 @@ class AutoWrappedStaticText(wx.StaticText): """ event.Skip() - self.Wrap(event.GetSize().width) + if self.target: + self.Wrap(self.target.GetSize().width) + else: + self.Wrap(event.GetSize().width) def Wrap(self, width): """ diff --git a/gooey/gui/components/widgets/bases.py b/gooey/gui/components/widgets/bases.py index dd0e5b0..c579cf1 100644 --- a/gooey/gui/components/widgets/bases.py +++ b/gooey/gui/components/widgets/bases.py @@ -73,6 +73,7 @@ class TextContainer(BaseWidget): if self._options.get('show_label', True): layout.Add(self.label, 0, wx.EXPAND) else: + self.label.Show(False) layout.AddStretchSpacer(1) layout.AddSpacer(2) @@ -80,6 +81,7 @@ class TextContainer(BaseWidget): layout.Add(self.help_text, 1, wx.EXPAND) layout.AddSpacer(2) else: + self.help_text.Show(False) layout.AddStretchSpacer(1) layout.Add(self.getSublayout(), 0, wx.EXPAND) layout.Add(self.error, 1, wx.EXPAND) diff --git a/gooey/python_bindings/argparse_to_json.py b/gooey/python_bindings/argparse_to_json.py index a2793ee..13a1d7b 100644 --- a/gooey/python_bindings/argparse_to_json.py +++ b/gooey/python_bindings/argparse_to_json.py @@ -15,6 +15,7 @@ from collections import OrderedDict from functools import partial from uuid import uuid4 +from gooey.python_bindings.gooey_parser import GooeyParser from gooey.util.functional import merge, getin @@ -75,6 +76,9 @@ def convert(parser, **kwargs): 'widgets': OrderedDict( (choose_name(name, sub_parser), { 'command': name, + 'name': choose_name(name, sub_parser), + 'help': get_subparser_help(sub_parser), + 'description': '', 'contents': process(sub_parser, getattr(sub_parser, 'widgets', {}), getattr(sub_parser, 'options', {})) @@ -115,6 +119,13 @@ def iter_parsers(parser): return iter([('::gooey/default', parser)]) +def get_subparser_help(parser): + if isinstance(parser, GooeyParser): + return getattr(parser.parser, 'usage', '') + else: + return getattr(parser, 'usage', '') + + def extract_groups(action_group): ''' Recursively extract argument groups and associated actions @@ -178,12 +189,13 @@ def reapply_mutex_groups(mutex_groups, action_groups): def categorize2(groups, widget_dict, options): + defaults = {'label_color': '#000000', 'description_color': '#363636'} return [{ 'name': group['name'], 'items': list(categorize(group['items'], widget_dict, options)), 'groups': categorize2(group['groups'], widget_dict, options), 'description': group['description'], - 'options': group['options'] + 'options': merge(defaults ,group['options']) } for group in groups]