From e8123b72744b81302f1d264082940b58b3695584 Mon Sep 17 00:00:00 2001
From: Kaalleen <36401965+kaalleen@users.noreply.github.com>
Date: Mon, 18 Nov 2024 11:12:58 +0100
Subject: [PATCH] Thread catalog: fix broken path (#3281)
* thread catalog: fix broken path
* apply threadlist: use wxpython to also include custom lists
* apply_palette: save last choice
---
lib/extensions/apply_palette.py | 18 ++++--
lib/gui/apply_palette.py | 105 ++++++++++++++++++++++++++++++++
lib/threads/catalog.py | 13 +---
lib/utils/settings.py | 5 +-
templates/apply_palette.xml | 17 +-----
5 files changed, 125 insertions(+), 33 deletions(-)
create mode 100644 lib/gui/apply_palette.py
diff --git a/lib/extensions/apply_palette.py b/lib/extensions/apply_palette.py
index cd8c4c943..373824b66 100644
--- a/lib/extensions/apply_palette.py
+++ b/lib/extensions/apply_palette.py
@@ -4,6 +4,9 @@
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
from ..elements import Clone, FillStitch
+from ..gui.abort_message import AbortMessageApp
+from ..gui.apply_palette import ApplyPaletteApp
+from ..i18n import _
from ..threads import ThreadCatalog, ThreadColor
from .base import InkstitchExtension
@@ -12,19 +15,24 @@ class ApplyPalette(InkstitchExtension):
'''
Applies colors of a color palette to elements
'''
- def __init__(self, *args, **kwargs):
- InkstitchExtension.__init__(self, *args, **kwargs)
- self.arg_parser.add_argument("-o", "--tabs")
- self.arg_parser.add_argument("-t", "--palette", type=str, default=None, dest="palette")
def effect(self):
# Remove selection, we want all the elements in the document
self.svg.selection.clear()
if not self.get_elements():
+ app = AbortMessageApp(
+ _("There is no stitchable element in the document."),
+ _("https://inkstitch.org/")
+ )
+ app.MainLoop()
return
- palette_name = self.options.palette
+ palette_choice = ApplyPaletteApp()
+ if palette_choice.palette:
+ self.apply_palette(palette_choice.palette)
+
+ def apply_palette(self, palette_name):
palette = ThreadCatalog().get_palette_by_name(palette_name)
# Iterate through the color blocks to apply colors
diff --git a/lib/gui/apply_palette.py b/lib/gui/apply_palette.py
new file mode 100644
index 000000000..164b75818
--- /dev/null
+++ b/lib/gui/apply_palette.py
@@ -0,0 +1,105 @@
+# Authors: see git history
+#
+# Copyright (c) 2023 Authors
+# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
+
+import wx
+import wx.adv
+
+from ..i18n import _
+from ..threads import ThreadCatalog
+from ..utils.settings import global_settings
+
+
+class ApplyPaletteFrame(wx.Frame):
+
+ def __init__(self, title, **kwargs):
+ super().__init__(None, title=title)
+
+ self.SetWindowStyle(wx.FRAME_FLOAT_ON_PARENT | wx.DEFAULT_FRAME_STYLE)
+
+ self.apply_hook = kwargs.pop('on_apply', None)
+
+ self.main_panel = wx.Panel(self, wx.ID_ANY)
+
+ notebook_sizer = wx.BoxSizer(wx.VERTICAL)
+ self.notebook = wx.Notebook(self.main_panel, wx.ID_ANY)
+ notebook_sizer.Add(self.notebook, 1, wx.EXPAND, 0)
+
+ self.palettes = wx.Panel(self.notebook, wx.ID_ANY)
+ self.notebook.AddPage(self.palettes, _("Palettes"))
+
+ palette_sizer = wx.BoxSizer(wx.VERTICAL)
+ palette_text = wx.StaticText(self.palettes, -1, _("Select color palette"))
+ self.palette_list = wx.Choice(self.palettes, choices=ThreadCatalog().palette_names())
+ last_selected_pallete = self.palette_list.FindString(global_settings['last_applied_palette'])
+ self.palette_list.SetSelection(last_selected_pallete)
+
+ palette_sizer.Add(palette_text, 0, wx.ALL | wx.EXPAND, 10)
+ palette_sizer.Add(self.palette_list, 0, wx.ALL | wx.EXPAND, 10)
+
+ button_sizer = wx.StdDialogButtonSizer()
+ palette_sizer.Add(button_sizer, 1, wx.BOTTOM | wx.EXPAND, 10)
+ button_sizer.Add((0, 0), 1, 0, 0)
+ self.apply_button = wx.Button(self.palettes, wx.ID_ANY, _("Apply"))
+ button_sizer.Add(self.apply_button, 0, wx.RIGHT, 10)
+ self.Bind(wx.EVT_BUTTON, self.apply_button_clicked, self.apply_button)
+
+ self.help = wx.Panel(self.notebook, wx.ID_ANY)
+ self.notebook.AddPage(self.help, _("Help"))
+
+ help_sizer = wx.BoxSizer(wx.VERTICAL)
+
+ help_text = wx.StaticText(
+ self.help,
+ wx.ID_ANY,
+ _("This extension applies nearest colors from chosen color palette to the elements in this document."),
+ style=wx.ALIGN_LEFT
+ )
+ help_text.Wrap(500)
+ help_sizer.Add(help_text, 0, wx.ALL, 8)
+
+ help_sizer.Add((20, 20), 0, 0, 0)
+
+ website_info = wx.StaticText(self.help, wx.ID_ANY, _("More information on our website:"))
+ help_sizer.Add(website_info, 0, wx.ALL, 8)
+
+ self.website_link = wx.adv.HyperlinkCtrl(
+ self.help,
+ wx.ID_ANY,
+ _("https://inkstitch.org/docs/thread-color/#apply-palette"),
+ _("https://inkstitch.org/docs/thread-color/#apply-palette")
+ )
+ help_sizer.Add(self.website_link, 0, wx.ALL, 8)
+
+ self.help.SetSizer(help_sizer)
+ self.palettes.SetSizer(palette_sizer)
+ self.main_panel.SetSizer(notebook_sizer)
+
+ self.SetSizeHints(notebook_sizer.CalcMin())
+
+ self.Layout()
+
+ def apply_button_clicked(self, event):
+ if self.apply_hook:
+ self.apply_hook()
+ self.Destroy()
+
+
+class ApplyPaletteApp(wx.App):
+ def __init__(self):
+ self.palette = None
+
+ app = wx.App()
+ self.frame = ApplyPaletteFrame(
+ title=_("Ink/Stitch"),
+ on_apply=self.set_palette,
+ )
+ self.frame.Show()
+ app.MainLoop()
+
+ def set_palette(self):
+ if self.frame.palette_list.GetSelection() == -1:
+ return
+ self.palette = self.frame.palette_list.GetString(self.frame.palette_list.GetSelection())
+ global_settings['last_applied_palette'] = self.palette
diff --git a/lib/threads/catalog.py b/lib/threads/catalog.py
index 46eb90add..886d9aa42 100644
--- a/lib/threads/catalog.py
+++ b/lib/threads/catalog.py
@@ -4,12 +4,10 @@
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
import os
-import sys
from collections.abc import Sequence
from glob import glob
-from os.path import dirname, realpath
-from ..utils import guess_inkscape_config_path
+from ..utils import get_bundled_dir, guess_inkscape_config_path
from .palette import ThreadPalette
@@ -26,13 +24,8 @@ class _ThreadCatalog(Sequence):
2. Palette directory of inkstitch
"""
path = [os.path.join(guess_inkscape_config_path(), 'palettes')]
-
- if getattr(sys, 'frozen', None) is not None:
- inkstitch_path = os.path.join(sys._MEIPASS, "..")
- else:
- inkstitch_path = dirname(dirname(dirname(realpath(__file__))))
-
- path.append(os.path.join(inkstitch_path, 'palettes'))
+ inkstitch_path = get_bundled_dir('palettes')
+ path.append(inkstitch_path)
return path
diff --git a/lib/utils/settings.py b/lib/utils/settings.py
index c0871e491..fb17a6a1a 100644
--- a/lib/utils/settings.py
+++ b/lib/utils/settings.py
@@ -1,6 +1,6 @@
-from collections.abc import MutableMapping
import json
import os
+from collections.abc import MutableMapping
from .paths import get_user_dir
@@ -15,7 +15,8 @@ DEFAULT_SETTINGS = {
"cache_size": 100,
"pop_out_simulator": False,
"simulator_line_width": 0.1,
- "simulator_npp_size": 0.5
+ "simulator_npp_size": 0.5,
+ "last_applied_palette": ""
}
diff --git a/templates/apply_palette.xml b/templates/apply_palette.xml
index f315916c7..9c782c848 100644
--- a/templates/apply_palette.xml
+++ b/templates/apply_palette.xml
@@ -3,22 +3,7 @@
Apply Palette
org.{{ id_inkstitch }}.apply_palette
apply_palette
-
-
-
- {%- for item in threadcatalog %}
- - {{ item }}
- {%- endfor %}
-
-
-
-
-
-
-
-
-
-
+
all