From 05ca4132f17a581939fd0b206319f215248e5232 Mon Sep 17 00:00:00 2001 From: George Steel Date: Sat, 27 Apr 2024 14:16:18 -0400 Subject: [PATCH] Add option to disable the stitch plan cache (#2655) Setting the cache size to 0 bypasses the cache completely. This is necessary during development to ensure newly-changed code actually gets run. Also fixes the error pane in the params gui. * make params warning pane large enough to see contents * rename sizers in preferences dialog descriptive names * add shapely version bound * add option to disable stitch plan cache * remove out-of-date wxg file * make a cache size of 0 disable the cache --- lib/elements/element.py | 8 +- lib/gui/preferences.py | 73 +++++----- lib/gui/preferences.wxg | 305 ---------------------------------------- lib/gui/warnings.py | 2 +- lib/utils/cache.py | 4 + lib/utils/settings.py | 2 +- requirements.txt | 2 +- 7 files changed, 51 insertions(+), 345 deletions(-) delete mode 100644 lib/gui/preferences.wxg diff --git a/lib/elements/element.py b/lib/elements/element.py index c9d7c377d..ae0ff5c60 100644 --- a/lib/elements/element.py +++ b/lib/elements/element.py @@ -22,7 +22,7 @@ from ..svg import (PIXELS_PER_MM, apply_transforms, convert_length, get_node_transform) from ..svg.tags import INKSCAPE_LABEL, INKSTITCH_ATTRIBS from ..utils import Point, cache -from ..utils.cache import CacheKeyGenerator, get_stitch_plan_cache +from ..utils.cache import get_stitch_plan_cache, is_cache_disabled, CacheKeyGenerator class Param(object): @@ -496,6 +496,9 @@ class EmbroideryElement(object): @debug.time def _load_cached_stitch_groups(self, previous_stitch): + if is_cache_disabled(): + return None + if not self.uses_previous_stitch(): # we don't care about the previous stitch previous_stitch = None @@ -519,6 +522,9 @@ class EmbroideryElement(object): @debug.time def _save_cached_stitch_groups(self, stitch_groups, previous_stitch): + if is_cache_disabled(): + return + stitch_plan_cache = get_stitch_plan_cache() cache_key = self.get_cache_key(previous_stitch) if cache_key not in stitch_plan_cache: diff --git a/lib/gui/preferences.py b/lib/gui/preferences.py index a8a2dc7a8..adb84248d 100644 --- a/lib/gui/preferences.py +++ b/lib/gui/preferences.py @@ -30,17 +30,17 @@ class PreferencesFrame(wx.Frame): self.this_svg_page = wx.Panel(self.notebook, wx.ID_ANY) self.notebook.AddPage(self.this_svg_page, _("This SVG")) - sizer_1 = wx.BoxSizer(wx.VERTICAL) + this_svg_margin = wx.BoxSizer(wx.VERTICAL) # add space above and below to center sizer_2 vertically - sizer_1.Add((0, 20), 1, wx.EXPAND, 0) + this_svg_margin.Add((0, 20), 1, wx.EXPAND, 0) - sizer_2 = wx.FlexGridSizer(2, 4, 15, 10) - sizer_1.Add(sizer_2, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 20) + this_svg_grid = wx.FlexGridSizer(2, 4, 15, 10) + this_svg_margin.Add(this_svg_grid, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 20) label_1 = wx.StaticText(self.this_svg_page, wx.ID_ANY, _("Minimum jump stitch length"), style=wx.ALIGN_LEFT) label_1.SetToolTip(_("Jump stitches smaller than this will be treated as normal stitches.")) - sizer_2.Add(label_1, 1, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 15) + this_svg_grid.Add(label_1, 1, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 15) self.minimum_jump_stitch_length = wx.SpinCtrlDouble( self.this_svg_page, wx.ID_ANY, inc=0.1, @@ -48,16 +48,16 @@ class PreferencesFrame(wx.Frame): style=wx.ALIGN_RIGHT | wx.SP_ARROW_KEYS ) self.minimum_jump_stitch_length.SetDigits(1) - sizer_2.Add(self.minimum_jump_stitch_length, 0, wx.ALIGN_CENTER_VERTICAL, 0) + this_svg_grid.Add(self.minimum_jump_stitch_length, 0, wx.ALIGN_CENTER_VERTICAL, 0) label_2 = wx.StaticText(self.this_svg_page, wx.ID_ANY, _("mm")) - sizer_2.Add(label_2, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 15) + this_svg_grid.Add(label_2, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 15) self.button_1 = wx.Button(self.this_svg_page, wx.ID_ANY, _("Set As Default")) - sizer_2.Add(self.button_1, 0, wx.ALIGN_CENTER_VERTICAL, 0) + this_svg_grid.Add(self.button_1, 0, wx.ALIGN_CENTER_VERTICAL, 0) label_3 = wx.StaticText(self.this_svg_page, wx.ID_ANY, _("Minimum stitch length")) - sizer_2.Add(label_3, 0, 0, 0) + this_svg_grid.Add(label_3, 0, 0, 0) self.minimum_stitch_length = wx.SpinCtrlDouble( self.this_svg_page, wx.ID_ANY, inc=0.1, @@ -65,30 +65,30 @@ class PreferencesFrame(wx.Frame): style=wx.ALIGN_RIGHT | wx.SP_ARROW_KEYS ) self.minimum_stitch_length.SetDigits(1) - sizer_2.Add(self.minimum_stitch_length, 0, wx.ALIGN_CENTER_VERTICAL, 0) + this_svg_grid.Add(self.minimum_stitch_length, 0, wx.ALIGN_CENTER_VERTICAL, 0) label_4 = wx.StaticText(self.this_svg_page, wx.ID_ANY, _("mm")) - sizer_2.Add(label_4, 0, wx.ALIGN_CENTER_VERTICAL, 0) + this_svg_grid.Add(label_4, 0, wx.ALIGN_CENTER_VERTICAL, 0) self.button_2 = wx.Button(self.this_svg_page, wx.ID_ANY, _("Set As Default")) - sizer_2.Add(self.button_2, 0, wx.ALIGN_CENTER_VERTICAL, 0) + this_svg_grid.Add(self.button_2, 0, wx.ALIGN_CENTER_VERTICAL, 0) - sizer_1.Add((0, 20), 1, wx.EXPAND, 0) + this_svg_margin.Add((0, 20), 1, wx.EXPAND, 0) self.global_page = wx.Panel(self.notebook, wx.ID_ANY) self.notebook.AddPage(self.global_page, _("Global")) - sizer_3 = wx.BoxSizer(wx.VERTICAL) + global_margin = wx.BoxSizer(wx.VERTICAL) # add space above and below to center sizer_4 vertically - sizer_3.Add((0, 20), 1, wx.EXPAND, 0) + global_margin.Add((0, 20), 1, wx.EXPAND, 0) - sizer_4 = wx.FlexGridSizer(3, 4, 15, 10) - sizer_3.Add(sizer_4, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 20) + global_grid_sizer = wx.FlexGridSizer(3, 4, 15, 10) + global_margin.Add(global_grid_sizer, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 20) label_5 = wx.StaticText(self.global_page, wx.ID_ANY, _("Default minimum jump stitch length"), style=wx.ALIGN_LEFT) label_5.SetToolTip(_("Jump stitches smaller than this will be treated as normal stitches.")) - sizer_4.Add(label_5, 1, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 15) + global_grid_sizer.Add(label_5, 1, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 15) self.default_minimum_jump_stitch_length = wx.SpinCtrlDouble( self.global_page, wx.ID_ANY, inc=0.1, @@ -96,15 +96,15 @@ class PreferencesFrame(wx.Frame): style=wx.ALIGN_RIGHT | wx.SP_ARROW_KEYS ) self.default_minimum_jump_stitch_length.SetDigits(1) - sizer_4.Add(self.default_minimum_jump_stitch_length, 0, wx.ALIGN_CENTER_VERTICAL, 0) + global_grid_sizer.Add(self.default_minimum_jump_stitch_length, 0, wx.ALIGN_CENTER_VERTICAL, 0) label_6 = wx.StaticText(self.global_page, wx.ID_ANY, _("mm")) - sizer_4.Add(label_6, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 15) + global_grid_sizer.Add(label_6, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 15) - sizer_4.Add((0, 0), 0, 0, 0) + global_grid_sizer.Add((0, 0), 0, 0, 0) label_7 = wx.StaticText(self.global_page, wx.ID_ANY, _("Minimum stitch length")) - sizer_4.Add(label_7, 0, wx.ALIGN_CENTER_VERTICAL, 0) + global_grid_sizer.Add(label_7, 0, wx.ALIGN_CENTER_VERTICAL, 0) self.default_minimum_stitch_length = wx.SpinCtrlDouble( self.global_page, wx.ID_ANY, inc=0.1, @@ -112,16 +112,15 @@ class PreferencesFrame(wx.Frame): style=wx.ALIGN_RIGHT | wx.SP_ARROW_KEYS ) self.default_minimum_stitch_length.SetDigits(1) - sizer_4.Add(self.default_minimum_stitch_length, 0, wx.ALIGN_CENTER_VERTICAL, 0) + global_grid_sizer.Add(self.default_minimum_stitch_length, 0, wx.ALIGN_CENTER_VERTICAL, 0) label_8 = wx.StaticText(self.global_page, wx.ID_ANY, _("mm")) - sizer_4.Add(label_8, 0, wx.ALIGN_CENTER_VERTICAL, 0) + global_grid_sizer.Add(label_8, 0, wx.ALIGN_CENTER_VERTICAL, 0) - sizer_4.Add((0, 20), 0, 0, 0) + global_grid_sizer.Add((0, 20), 0, 0, 0) - label_9 = wx.StaticText(self.global_page, wx.ID_ANY, _("Stitch plan cache size"), style=wx.ALIGN_LEFT) - label_9.SetToolTip(_("Jump stitches smaller than this will be treated as normal stitches.")) - sizer_4.Add(label_9, 1, wx.ALIGN_CENTER_VERTICAL, 0) + label_9 = wx.StaticText(self.global_page, wx.ID_ANY, _("Stitch plan cache size (0 to disable cache)"), style=wx.ALIGN_LEFT) + global_grid_sizer.Add(label_9, 1, wx.ALIGN_CENTER_VERTICAL, 0) self.stitch_plan_cache_size = wx.SpinCtrl( self.global_page, wx.ID_ANY, @@ -129,15 +128,15 @@ class PreferencesFrame(wx.Frame): style=wx.ALIGN_RIGHT | wx.SP_ARROW_KEYS ) self.stitch_plan_cache_size.SetIncrement(10) - sizer_4.Add(self.stitch_plan_cache_size, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT, 0) + global_grid_sizer.Add(self.stitch_plan_cache_size, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT, 0) label_10 = wx.StaticText(self.global_page, wx.ID_ANY, _("MB")) - sizer_4.Add(label_10, 0, wx.ALIGN_CENTER_VERTICAL, 0) + global_grid_sizer.Add(label_10, 0, wx.ALIGN_CENTER_VERTICAL, 0) self.clear_cache_button = wx.Button(self.global_page, wx.ID_ANY, _("Clear Stitch Plan Cache")) - sizer_4.Add(self.clear_cache_button, 0, wx.ALIGN_CENTER_VERTICAL, 0) + global_grid_sizer.Add(self.clear_cache_button, 0, wx.ALIGN_CENTER_VERTICAL, 0) - sizer_3.Add((0, 0), 1, wx.EXPAND, 0) + global_margin.Add((0, 0), 1, wx.EXPAND, 0) button_sizer = wx.BoxSizer(wx.HORIZONTAL) main_sizer.Add(button_sizer, 0, wx.BOTTOM | wx.EXPAND | wx.LEFT | wx.RIGHT, 10) @@ -150,13 +149,13 @@ class PreferencesFrame(wx.Frame): self.ok_button = wx.Button(self.panel_1, wx.ID_OK, "") button_sizer.Add(self.ok_button, 0, 0, 0) - sizer_4.AddGrowableCol(0) + global_grid_sizer.AddGrowableCol(0) - self.global_page.SetSizer(sizer_3) + self.global_page.SetSizer(global_margin) - sizer_2.AddGrowableCol(0) + this_svg_grid.AddGrowableCol(0) - self.this_svg_page.SetSizer(sizer_1) + self.this_svg_page.SetSizer(this_svg_margin) self.panel_1.SetSizer(main_sizer) @@ -193,6 +192,8 @@ class PreferencesFrame(wx.Frame): stitch_plan_cache = get_stitch_plan_cache() stitch_plan_cache.size_limit = int(global_settings['cache_size'] * 1024 * 1024) stitch_plan_cache.cull() + if not global_settings['cache_size']: + stitch_plan_cache.clear(retry=True) def cancel_button_clicked(self, event): self.Destroy() diff --git a/lib/gui/preferences.wxg b/lib/gui/preferences.wxg deleted file mode 100644 index 328ccce3e..000000000 --- a/lib/gui/preferences.wxg +++ /dev/null @@ -1,305 +0,0 @@ - - - - - - self.SetSizeHints(main_sizer.CalcMin()) - Preferences - - - - wxVERTICAL - - - 10 - wxALL|wxEXPAND - - - - This SVG - Global - - - - - wxVERTICAL - - - 0 - wxEXPAND - - # add space above and below to center sizer_2 vertically - 0 - 0 - - - - - 20 - wxLEFT|wxRIGHT|wxEXPAND - - 2 - 4 - 15 - 10 - 0 - - - 15 - wxRIGHT|wxALIGN_CENTER_VERTICAL - - Jump stitches smaller than this will be treated as normal stitches. - - - - - - - 0 - wxALIGN_CENTER_VERTICAL - - - 0.0 - 1 - - - - - 15 - wxRIGHT|wxALIGN_CENTER_VERTICAL - - - - - - - 0 - wxALIGN_CENTER_VERTICAL - - - set_as_default_minimum_jump_stitch_length - - - - - - - 0 - - - - - - - 0 - wxALIGN_CENTER_VERTICAL - - - 0.0, 100.0 - 0.0 - 1 - - - - - 0 - wxALIGN_CENTER_VERTICAL - - - - - - - 0 - wxALIGN_CENTER_VERTICAL - - - set_as_default_minimum_stitch_length - - - - - - - - - 0 - wxEXPAND - - 0 - 0 - - - - - - - - wxVERTICAL - - - 0 - wxEXPAND - - # add space above and below to center sizer_4 vertically - 0 - 0 - - - - - 20 - wxLEFT|wxRIGHT|wxEXPAND - - 3 - 4 - 15 - 10 - 0 - - - 15 - wxRIGHT|wxALIGN_CENTER_VERTICAL - - Jump stitches smaller than this will be treated as normal stitches. - - - - - - - 0 - wxALIGN_CENTER_VERTICAL - - - 0.0, 100.0 - 0.0 - 1 - - - - - 15 - wxRIGHT|wxALIGN_CENTER_VERTICAL - - - - - - - - 0 - wxALIGN_CENTER_VERTICAL - - - - - - - 0 - wxALIGN_CENTER_VERTICAL - - - 0.0, 100.0 - 0.0 - 1 - - - - - 0 - wxALIGN_CENTER_VERTICAL - - - - - - - - 0 - wxALIGN_CENTER_VERTICAL - - Jump stitches smaller than this will be treated as normal stitches. - - - - - - - 0 - wxALIGN_CENTER_VERTICAL - - - 0.0, 100.0 - 0.0 - 1 - - - - - 0 - wxALIGN_CENTER_VERTICAL - - - - - - - 0 - wxALIGN_CENTER_VERTICAL - - - - - - - - - 0 - wxEXPAND - - 0 - 0 - - - - - - - - - 10 - wxLEFT|wxRIGHT|wxBOTTOM|wxEXPAND - - wxHORIZONTAL - - - 0 - - 0 - 0 - - - - - 10 - wxRIGHT - - - CANCEL - - - - - 0 - - - OK - - - - - - - - diff --git a/lib/gui/warnings.py b/lib/gui/warnings.py index eda1ca2e7..c8359fc9a 100644 --- a/lib/gui/warnings.py +++ b/lib/gui/warnings.py @@ -23,7 +23,7 @@ class WarningPanel(wx.Panel): self.main_sizer.Add(self.warning, 1, wx.LEFT | wx.BOTTOM | wx.EXPAND, 10) tc_style = wx.TE_MULTILINE | wx.TE_READONLY | wx.VSCROLL | wx.TE_RICH2 - self.warning_text = wx.TextCtrl(self, size=(300, 100), style=tc_style) + self.warning_text = wx.TextCtrl(self, size=(300, 300), style=tc_style) font = self.warning_text.GetFont() font.SetFamily(wx.FONTFAMILY_TELETYPE) self.warning_text.SetFont(font) diff --git a/lib/utils/cache.py b/lib/utils/cache.py index 1ede8ce16..543aaf15e 100644 --- a/lib/utils/cache.py +++ b/lib/utils/cache.py @@ -39,6 +39,10 @@ def get_stitch_plan_cache(): return __stitch_plan_cache +def is_cache_disabled(): + return not global_settings['cache_size'] + + class CacheKeyGenerator(object): """Generate cache keys given arbitrary data. diff --git a/lib/utils/settings.py b/lib/utils/settings.py index 51cfcdb81..44310537f 100644 --- a/lib/utils/settings.py +++ b/lib/utils/settings.py @@ -13,7 +13,7 @@ DEFAULT_METADATA = { DEFAULT_SETTINGS = { "cache_size": 100, - "pop_out_simulator": False + "pop_out_simulator": False, } diff --git a/requirements.txt b/requirements.txt index 5763d128f..a067d665d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,7 +7,7 @@ wxPython>=4.1.1 backports.functools_lru_cache networkx -shapely +shapely>=2.0.0 lxml appdirs numpy