diff options
| author | Lex Neva <github.com@lexneva.name> | 2019-04-02 22:36:54 -0400 |
|---|---|---|
| committer | Lex Neva <github.com@lexneva.name> | 2019-04-02 22:36:54 -0400 |
| commit | a9cf553066c3fd5b907593751bb00f77f32ce86a (patch) | |
| tree | a13f29b0b67d644a520be02afd4ba8f4062dba66 | |
| parent | 55505369496c0986e54fe5722e7e8ddce0a9294e (diff) | |
add font description to font selector dropdown
| -rw-r--r-- | lib/extensions/lettering.py | 31 | ||||
| -rw-r--r-- | lib/gui/__init__.py | 1 | ||||
| -rw-r--r-- | lib/gui/subtitle_combo_box.py | 85 |
3 files changed, 109 insertions, 8 deletions
diff --git a/lib/extensions/lettering.py b/lib/extensions/lettering.py index a33277df..74d036cf 100644 --- a/lib/extensions/lettering.py +++ b/lib/extensions/lettering.py @@ -10,7 +10,7 @@ import inkex import wx from ..elements import nodes_to_elements -from ..gui import PresetsPanel, SimulatorPreview, info_dialog +from ..gui import PresetsPanel, SimulatorPreview, info_dialog, SubtitleComboBox from ..i18n import _ from ..lettering import Font, FontError from ..svg import get_correction_transform @@ -49,8 +49,10 @@ class LetteringFrame(wx.Frame): # text editor self.text_editor_box = wx.StaticBox(self, wx.ID_ANY, label=_("Text")) - self.font_chooser = wx.ComboBox(self, wx.ID_ANY, style=wx.CB_READONLY) self.update_font_list() + self.font_chooser = SubtitleComboBox(self, wx.ID_ANY, choices=self.get_font_names(), + subtitles=self.get_font_descriptions(), style=wx.CB_READONLY) + self.font_chooser.Bind(wx.EVT_COMBOBOX, self.update_preview) self.set_initial_font(self.settings.font) self.text_editor = wx.TextCtrl(self, style=wx.TE_MULTILINE | wx.TE_DONTWRAP, value=self.settings.text) @@ -115,12 +117,19 @@ class LetteringFrame(wx.Frame): except FontError: pass - self.font_chooser.SetItems(sorted(self.fonts)) - if len(self.fonts) == 0: info_dialog(self, _("Unable to find any fonts! Please try reinstalling Ink/Stitch.")) self.cancel() + def get_font_names(self): + font_names = [font.name for font in self.fonts.itervalues()] + font_names.sort() + + return font_names + + def get_font_descriptions(self): + return {font.name: font.description for font in self.fonts.itervalues()} + def set_initial_font(self, font_id): if font_id is not None: if font_id not in self.fonts_by_id: @@ -128,9 +137,9 @@ class LetteringFrame(wx.Frame): '''This text was created using the font "%s", but Ink/Stitch can't find that font. A default font will be substituted.''') % font_id) try: - self.font_chooser.SetValue(self.fonts_by_id[font_id].name) + self.font_chooser.SetValueByUser(self.fonts_by_id[font_id].name) except KeyError: - self.font_chooser.SetValue(self.default_font.name) + self.font_chooser.SetValueByUser(self.default_font.name) @property @cache @@ -144,8 +153,11 @@ class LetteringFrame(wx.Frame): self.settings[attribute] = event.GetEventObject().GetValue() self.preview.update() + def update_preview(self, event=None): + self.preview.update() + def update_lettering(self): - font = self.fonts_by_id.get(self.settings.font, self.default_font) + font = self.fonts.get(self.font_chooser.GetValue(), self.default_font) del self.group[:] font.render_text(self.settings.text, self.group, back_and_forth=self.settings.back_and_forth, trim=self.settings.trim) @@ -209,8 +221,11 @@ class LetteringFrame(wx.Frame): options_sizer.Add(self.trim_checkbox, 1, wx.EXPAND | wx.LEFT | wx.TOP | wx.RIGHT | wx.BOTTOM, 5) outer_sizer.Add(options_sizer, 0, wx.EXPAND | wx.LEFT | wx.TOP | wx.RIGHT, 10) + font_chooser_sizer = wx.BoxSizer(wx.VERTICAL) + font_chooser_sizer.Add(self.font_chooser, 0, wx.ALL | wx.EXPAND, 10) + text_editor_sizer = wx.StaticBoxSizer(self.text_editor_box, wx.VERTICAL) - text_editor_sizer.Add(self.font_chooser, 0, wx.ALL, 10) + text_editor_sizer.Add(font_chooser_sizer, 0, wx.RIGHT | wx.EXPAND, 100) text_editor_sizer.Add(self.text_editor, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, 10) outer_sizer.Add(text_editor_sizer, 1, wx.EXPAND | wx.LEFT | wx.TOP | wx.RIGHT, 10) diff --git a/lib/gui/__init__.py b/lib/gui/__init__.py index 060c3d93..51890cf9 100644 --- a/lib/gui/__init__.py +++ b/lib/gui/__init__.py @@ -1,3 +1,4 @@ from dialogs import info_dialog, confirm_dialog from presets import PresetsPanel from simulator import EmbroiderySimulator, SimulatorPreview, show_simulator +from subtitle_combo_box import SubtitleComboBox diff --git a/lib/gui/subtitle_combo_box.py b/lib/gui/subtitle_combo_box.py new file mode 100644 index 00000000..64c42153 --- /dev/null +++ b/lib/gui/subtitle_combo_box.py @@ -0,0 +1,85 @@ +import wx +import wx.adv +from wx.lib.wordwrap import wordwrap + + +class SubtitleComboBox(wx.adv.OwnerDrawnComboBox): + TITLE_FONT_SIZE = 12 + SUBTITLE_FONT_SIZE = 10 + + # I'd love to make this 12 too, but if I do it seems to get drawn as 10 + # initially no matter what I do. + CONTROL_FONT_SIZE = 12 + + MARGIN = 5 + + def __init__(self, *args, **kwargs): + self.titles = kwargs.get('choices', []) + subtitles = kwargs.pop('subtitles', {}) + self.subtitles = [subtitles.get(title, '') for title in self.titles] + wx.adv.OwnerDrawnComboBox.__init__(self, *args, **kwargs) + + self.control_font = wx.Font(pointSize=self.CONTROL_FONT_SIZE, family=wx.DEFAULT, style=wx.NORMAL, weight=wx.NORMAL) + self.title_font = wx.Font(pointSize=self.TITLE_FONT_SIZE, family=wx.DEFAULT, style=wx.NORMAL, weight=wx.NORMAL) + self.subtitle_font = wx.Font(pointSize=self.SUBTITLE_FONT_SIZE, family=wx.DEFAULT, style=wx.NORMAL, weight=wx.NORMAL) + + def OnMeasureItemWidth(self, item): + # This _should_ allow us to set the width of the combobox to match the + # width of the widest title. In reality, this method is never called + # and I can't figure out why. We just use self.GetSize().GetWidth() + # instead and rely on the parent window to size us appropriately. Ugh. + + title = self.titles[item] + + # technique from https://stackoverflow.com/a/23529463/4249120 + dc = wx.ScreenDC() + dc.SetFont(self.title_font) + + return dc.GetTextExtent(title).GetWidth() + 2 * self.MARGIN + + def OnMeasureItem(self, item): + title = self.titles[item] + subtitle = self.subtitles[item] + + dc = wx.ScreenDC() + dc.SetFont(self.subtitle_font) + wrapped = wordwrap(subtitle, self.GetSize().GetWidth(), dc) + subtitle_height = dc.GetTextExtent(wrapped).GetHeight() + + dc = wx.ScreenDC() + dc.SetFont(self.title_font) + title_height = dc.GetTextExtent(title).GetHeight() + + return subtitle_height + title_height + 3 * self.MARGIN + + def OnDrawBackground(self, dc, rect, item, flags): + if flags & wx.adv.ODCB_PAINTING_SELECTED: + # let the parent class draw the selected item so we don't + # hae to figure out the highlight color + wx.adv.OwnerDrawnComboBox.OnDrawBackground(self, dc, rect, item, flags) + else: + # alternate white and grey for the dropdown items, and draw the + # combo box itself as white + if flags & wx.adv.ODCB_PAINTING_CONTROL or item % 2 == 0: + background_color = wx.Colour(255, 255, 255) + else: + background_color = wx.Colour(240, 240, 240) + + dc.SetBrush(wx.Brush(background_color)) + dc.SetPen(wx.Pen(background_color)) + dc.DrawRectangle(rect) + + def OnDrawItem(self, dc, rect, item, flags): + if flags & wx.adv.ODCB_PAINTING_CONTROL: + # painting the selected item in the box + dc.SetFont(self.control_font) + dc.DrawText(self.titles[item], rect.x + self.MARGIN, rect.y + self.MARGIN) + else: + # painting the items in the popup + dc.SetFont(self.title_font) + title_height = dc.GetCharHeight() + dc.DrawText(self.titles[item], rect.x + self.MARGIN, rect.y + self.MARGIN) + + dc.SetFont(self.subtitle_font) + subtitle = wordwrap(self.subtitles[item], self.GetSize().GetWidth(), dc) + dc.DrawText(subtitle, rect.x + self.MARGIN, rect.y + title_height + self.MARGIN * 2) |
